blob: 97852099defda678291bfa24d99e38349be466bf [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
Bernd Schubert90b08712008-05-23 13:04:38 -070077static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
78
Adrian Bunk5e563412006-06-26 00:27:42 -070079#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070085 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
89 *
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080091 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93
94static int sysctl_speed_limit_min = 1000;
95static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080096static inline int speed_min(mddev_t *mddev)
97{
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
100}
101
102static inline int speed_max(mddev_t *mddev)
103{
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static struct ctl_table_header *raid_table_header;
109
110static ctl_table raid_table[] = {
111 {
112 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
113 .procname = "speed_limit_min",
114 .data = &sysctl_speed_limit_min,
115 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700116 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .proc_handler = &proc_dointvec,
118 },
119 {
120 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .proc_handler = &proc_dointvec,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_dir_table[] = {
131 {
132 .ctl_name = DEV_RAID,
133 .procname = "raid",
134 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700135 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .child = raid_table,
137 },
138 { .ctl_name = 0 }
139};
140
141static ctl_table raid_root_table[] = {
142 {
143 .ctl_name = CTL_DEV,
144 .procname = "dev",
145 .maxlen = 0,
146 .mode = 0555,
147 .child = raid_dir_table,
148 },
149 { .ctl_name = 0 }
150};
151
152static struct block_device_operations md_fops;
153
NeilBrownf91de922005-11-08 21:39:36 -0800154static int start_readonly;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
NeilBrownd7603b72006-01-06 00:20:30 -0800157 * We have a system wide 'event count' that is incremented
158 * on any 'interesting' event, and readers of /proc/mdstat
159 * can use 'poll' or 'select' to find out when the event
160 * count increases.
161 *
162 * Events are:
163 * start array, stop array, error, add device, remove device,
164 * start build, activate spare
165 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800166static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800167static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800168void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800169{
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100172 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800173}
NeilBrown29269552006-03-27 01:18:10 -0800174EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800175
NeilBrownc331eb02006-05-30 21:27:13 -0700176/* Alternate version that can be called from interrupts
177 * when calling sysfs_notify isn't needed.
178 */
Adrian Bunk05381952006-06-26 00:28:01 -0700179static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700180{
181 atomic_inc(&md_event_count);
182 wake_up(&md_event_waiters);
183}
184
NeilBrownd7603b72006-01-06 00:20:30 -0800185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * Enables to iterate over all existing md arrays
187 * all_mddevs_lock protects this list.
188 */
189static LIST_HEAD(all_mddevs);
190static DEFINE_SPINLOCK(all_mddevs_lock);
191
192
193/*
194 * iterates through all used mddevs in the system.
195 * We take care to grab the all_mddevs_lock whenever navigating
196 * the list, and to always hold a refcount when unlocked.
197 * Any code which breaks out of this loop while own
198 * a reference to the current mddev and must mddev_put it.
199 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800200#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 \
202 for (({ spin_lock(&all_mddevs_lock); \
203 tmp = all_mddevs.next; \
204 mddev = NULL;}); \
205 ({ if (tmp != &all_mddevs) \
206 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
207 spin_unlock(&all_mddevs_lock); \
208 if (mddev) mddev_put(mddev); \
209 mddev = list_entry(tmp, mddev_t, all_mddevs); \
210 tmp != &all_mddevs;}); \
211 ({ spin_lock(&all_mddevs_lock); \
212 tmp = tmp->next;}) \
213 )
214
215
Jens Axboe165125e2007-07-24 09:28:11 +0200216static int md_fail_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
NeilBrown6712ecf2007-09-27 12:47:43 +0200218 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return 0;
220}
221
222static inline mddev_t *mddev_get(mddev_t *mddev)
223{
224 atomic_inc(&mddev->active);
225 return mddev;
226}
227
228static void mddev_put(mddev_t *mddev)
229{
230 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
231 return;
232 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
233 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800234 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500235 blk_cleanup_queue(mddev->queue);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800236 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800237 } else
238 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
241static mddev_t * mddev_find(dev_t unit)
242{
243 mddev_t *mddev, *new = NULL;
244
245 retry:
246 spin_lock(&all_mddevs_lock);
247 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
248 if (mddev->unit == unit) {
249 mddev_get(mddev);
250 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700251 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return mddev;
253 }
254
255 if (new) {
256 list_add(&new->all_mddevs, &all_mddevs);
257 spin_unlock(&all_mddevs_lock);
258 return new;
259 }
260 spin_unlock(&all_mddevs_lock);
261
NeilBrown9ffae0c2006-01-06 00:20:32 -0800262 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (!new)
264 return NULL;
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 new->unit = unit;
267 if (MAJOR(unit) == MD_MAJOR)
268 new->md_minor = MINOR(unit);
269 else
270 new->md_minor = MINOR(unit) >> MdpMinorShift;
271
NeilBrowndf5b89b2006-03-27 01:18:20 -0800272 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 INIT_LIST_HEAD(&new->disks);
274 INIT_LIST_HEAD(&new->all_mddevs);
275 init_timer(&new->safemode_timer);
276 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700277 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700278 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700279 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700280 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000281 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800282 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700283 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 new->queue = blk_alloc_queue(GFP_KERNEL);
286 if (!new->queue) {
287 kfree(new);
288 return NULL;
289 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200290 /* Can be unlocked because the queue is new: no concurrency */
291 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 blk_queue_make_request(new->queue, md_fail_request);
294
295 goto retry;
296}
297
298static inline int mddev_lock(mddev_t * mddev)
299{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800300 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static inline int mddev_trylock(mddev_t * mddev)
304{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800305 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
308static inline void mddev_unlock(mddev_t * mddev)
309{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800310 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
NeilBrown005eca52005-08-22 13:11:08 -0700312 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
NeilBrown2989ddb2006-01-06 00:20:43 -0800315static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 mdk_rdev_t * rdev;
318 struct list_head *tmp;
319
NeilBrownd089c6a2008-02-06 01:39:59 -0800320 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (rdev->desc_nr == nr)
322 return rdev;
323 }
324 return NULL;
325}
326
327static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
328{
329 struct list_head *tmp;
330 mdk_rdev_t *rdev;
331
NeilBrownd089c6a2008-02-06 01:39:59 -0800332 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (rdev->bdev->bd_dev == dev)
334 return rdev;
335 }
336 return NULL;
337}
338
NeilBrownd9d166c2006-01-06 00:20:51 -0800339static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800340{
341 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800342 list_for_each_entry(pers, &pers_list, list) {
343 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800344 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800345 if (strcmp(pers->name, clevel)==0)
346 return pers;
347 }
NeilBrown2604b702006-01-06 00:20:36 -0800348 return NULL;
349}
350
Jesper Juhl77933d72005-07-27 11:46:09 -0700351static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
354 return MD_NEW_SIZE_BLOCKS(size);
355}
356
357static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
358{
359 sector_t size;
360
361 size = rdev->sb_offset;
362
363 if (chunk_size)
364 size &= ~((sector_t)chunk_size/1024 - 1);
365 return size;
366}
367
368static int alloc_disk_sb(mdk_rdev_t * rdev)
369{
370 if (rdev->sb_page)
371 MD_BUG();
372
373 rdev->sb_page = alloc_page(GFP_KERNEL);
374 if (!rdev->sb_page) {
375 printk(KERN_ALERT "md: out of memory.\n");
376 return -EINVAL;
377 }
378
379 return 0;
380}
381
382static void free_disk_sb(mdk_rdev_t * rdev)
383{
384 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800385 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 rdev->sb_loaded = 0;
387 rdev->sb_page = NULL;
388 rdev->sb_offset = 0;
389 rdev->size = 0;
390 }
391}
392
393
NeilBrown6712ecf2007-09-27 12:47:43 +0200394static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700395{
396 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800397 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700398
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700399 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
400 printk("md: super_written gets error=%d, uptodate=%d\n",
401 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
402 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800403 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700404 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700405
NeilBrowna9701a32005-11-08 21:39:34 -0800406 if (atomic_dec_and_test(&mddev->pending_writes))
407 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700408 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700409}
410
NeilBrown6712ecf2007-09-27 12:47:43 +0200411static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800412{
413 struct bio *bio2 = bio->bi_private;
414 mdk_rdev_t *rdev = bio2->bi_private;
415 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800416
417 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
418 error == -EOPNOTSUPP) {
419 unsigned long flags;
420 /* barriers don't appear to be supported :-( */
421 set_bit(BarriersNotsupp, &rdev->flags);
422 mddev->barriers_work = 0;
423 spin_lock_irqsave(&mddev->write_lock, flags);
424 bio2->bi_next = mddev->biolist;
425 mddev->biolist = bio2;
426 spin_unlock_irqrestore(&mddev->write_lock, flags);
427 wake_up(&mddev->sb_wait);
428 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200429 } else {
430 bio_put(bio2);
431 bio->bi_private = rdev;
432 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800433 }
NeilBrowna9701a32005-11-08 21:39:34 -0800434}
435
NeilBrown7bfa19f2005-06-21 17:17:28 -0700436void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
437 sector_t sector, int size, struct page *page)
438{
439 /* write first size bytes of page to sector of rdev
440 * Increment mddev->pending_writes before returning
441 * and decrement it on completion, waking up sb_wait
442 * if zero is reached.
443 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800444 *
445 * As we might need to resubmit the request if BIO_RW_BARRIER
446 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700447 */
448 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800449 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700450
451 bio->bi_bdev = rdev->bdev;
452 bio->bi_sector = sector;
453 bio_add_page(bio, page, size, 0);
454 bio->bi_private = rdev;
455 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800456 bio->bi_rw = rw;
457
NeilBrown7bfa19f2005-06-21 17:17:28 -0700458 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800459 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
460 struct bio *rbio;
461 rw |= (1<<BIO_RW_BARRIER);
462 rbio = bio_clone(bio, GFP_NOIO);
463 rbio->bi_private = bio;
464 rbio->bi_end_io = super_written_barrier;
465 submit_bio(rw, rbio);
466 } else
467 submit_bio(rw, bio);
468}
469
470void md_super_wait(mddev_t *mddev)
471{
472 /* wait for all superblock writes that were scheduled to complete.
473 * if any had to be retried (due to BARRIER problems), retry them
474 */
475 DEFINE_WAIT(wq);
476 for(;;) {
477 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
478 if (atomic_read(&mddev->pending_writes)==0)
479 break;
480 while (mddev->biolist) {
481 struct bio *bio;
482 spin_lock_irq(&mddev->write_lock);
483 bio = mddev->biolist;
484 mddev->biolist = bio->bi_next ;
485 bio->bi_next = NULL;
486 spin_unlock_irq(&mddev->write_lock);
487 submit_bio(bio->bi_rw, bio);
488 }
489 schedule();
490 }
491 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700492}
493
NeilBrown6712ecf2007-09-27 12:47:43 +0200494static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
NeilBrowna654b9d82005-06-21 17:17:27 -0700499int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct page *page, int rw)
501{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700502 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 struct completion event;
504 int ret;
505
506 rw |= (1 << BIO_RW_SYNC);
507
508 bio->bi_bdev = bdev;
509 bio->bi_sector = sector;
510 bio_add_page(bio, page, size, 0);
511 init_completion(&event);
512 bio->bi_private = &event;
513 bio->bi_end_io = bi_complete;
514 submit_bio(rw, bio);
515 wait_for_completion(&event);
516
517 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
518 bio_put(bio);
519 return ret;
520}
NeilBrowna8745db2006-01-06 00:20:34 -0800521EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
NeilBrown0002b272005-09-09 16:23:53 -0700523static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 char b[BDEVNAME_SIZE];
526 if (!rdev->sb_page) {
527 MD_BUG();
528 return -EINVAL;
529 }
530 if (rdev->sb_loaded)
531 return 0;
532
533
NeilBrown0002b272005-09-09 16:23:53 -0700534 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto fail;
536 rdev->sb_loaded = 1;
537 return 0;
538
539fail:
540 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
541 bdevname(rdev->bdev,b));
542 return -EINVAL;
543}
544
545static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
546{
547 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
548 (sb1->set_uuid1 == sb2->set_uuid1) &&
549 (sb1->set_uuid2 == sb2->set_uuid2) &&
550 (sb1->set_uuid3 == sb2->set_uuid3))
551
552 return 1;
553
554 return 0;
555}
556
557
558static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
559{
560 int ret;
561 mdp_super_t *tmp1, *tmp2;
562
563 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
564 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
565
566 if (!tmp1 || !tmp2) {
567 ret = 0;
568 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
569 goto abort;
570 }
571
572 *tmp1 = *sb1;
573 *tmp2 = *sb2;
574
575 /*
576 * nr_disks is not constant
577 */
578 tmp1->nr_disks = 0;
579 tmp2->nr_disks = 0;
580
581 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
582 ret = 0;
583 else
584 ret = 1;
585
586abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700587 kfree(tmp1);
588 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return ret;
590}
591
NeilBrown4d167f02007-05-09 02:35:37 -0700592
593static u32 md_csum_fold(u32 csum)
594{
595 csum = (csum & 0xffff) + (csum >> 16);
596 return (csum & 0xffff) + (csum >> 16);
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599static unsigned int calc_sb_csum(mdp_super_t * sb)
600{
NeilBrown4d167f02007-05-09 02:35:37 -0700601 u64 newcsum = 0;
602 u32 *sb32 = (u32*)sb;
603 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 unsigned int disk_csum, csum;
605
606 disk_csum = sb->sb_csum;
607 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700608
609 for (i = 0; i < MD_SB_BYTES/4 ; i++)
610 newcsum += sb32[i];
611 csum = (newcsum & 0xffffffff) + (newcsum>>32);
612
613
614#ifdef CONFIG_ALPHA
615 /* This used to use csum_partial, which was wrong for several
616 * reasons including that different results are returned on
617 * different architectures. It isn't critical that we get exactly
618 * the same return value as before (we always csum_fold before
619 * testing, and that removes any differences). However as we
620 * know that csum_partial always returned a 16bit value on
621 * alphas, do a fold to maximise conformity to previous behaviour.
622 */
623 sb->sb_csum = md_csum_fold(disk_csum);
624#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700626#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return csum;
628}
629
630
631/*
632 * Handle superblock details.
633 * We want to be able to handle multiple superblock formats
634 * so we have a common interface to them all, and an array of
635 * different handlers.
636 * We rely on user-space to write the initial superblock, and support
637 * reading and updating of superblocks.
638 * Interface methods are:
639 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
640 * loads and validates a superblock on dev.
641 * if refdev != NULL, compare superblocks on both devices
642 * Return:
643 * 0 - dev has a superblock that is compatible with refdev
644 * 1 - dev has a superblock that is compatible and newer than refdev
645 * so dev should be used as the refdev in future
646 * -EINVAL superblock incompatible or invalid
647 * -othererror e.g. -EIO
648 *
649 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
650 * Verify that dev is acceptable into mddev.
651 * The first time, mddev->raid_disks will be 0, and data from
652 * dev should be merged in. Subsequent calls check that dev
653 * is new enough. Return 0 or -EINVAL
654 *
655 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
656 * Update the superblock for rdev with data in mddev
657 * This does not write to disc.
658 *
659 */
660
661struct super_type {
662 char *name;
663 struct module *owner;
664 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
665 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
666 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
667};
668
669/*
670 * load_super for 0.90.0
671 */
672static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
673{
674 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
675 mdp_super_t *sb;
676 int ret;
677 sector_t sb_offset;
678
679 /*
680 * Calculate the position of the superblock,
681 * it's at the end of the disk.
682 *
683 * It also happens to be a multiple of 4Kb.
684 */
685 sb_offset = calc_dev_sboffset(rdev->bdev);
686 rdev->sb_offset = sb_offset;
687
NeilBrown0002b272005-09-09 16:23:53 -0700688 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (ret) return ret;
690
691 ret = -EINVAL;
692
693 bdevname(rdev->bdev, b);
694 sb = (mdp_super_t*)page_address(rdev->sb_page);
695
696 if (sb->md_magic != MD_SB_MAGIC) {
697 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
698 b);
699 goto abort;
700 }
701
702 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800703 sb->minor_version < 90 ||
704 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
706 sb->major_version, sb->minor_version,
707 b);
708 goto abort;
709 }
710
711 if (sb->raid_disks <= 0)
712 goto abort;
713
NeilBrown4d167f02007-05-09 02:35:37 -0700714 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
716 b);
717 goto abort;
718 }
719
720 rdev->preferred_minor = sb->md_minor;
721 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700722 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
NeilBrowne11e93f2007-05-09 02:35:36 -0700724 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
725 if (sb->level != 1 && sb->level != 4
726 && sb->level != 5 && sb->level != 6
727 && sb->level != 10) {
728 /* FIXME use a better test */
729 printk(KERN_WARNING
730 "md: bitmaps not supported for this level.\n");
731 goto abort;
732 }
733 }
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (sb->level == LEVEL_MULTIPATH)
736 rdev->desc_nr = -1;
737 else
738 rdev->desc_nr = sb->this_disk.number;
739
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700740 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700742 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 __u64 ev1, ev2;
744 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
745 if (!uuid_equal(refsb, sb)) {
746 printk(KERN_WARNING "md: %s has different UUID to %s\n",
747 b, bdevname(refdev->bdev,b2));
748 goto abort;
749 }
750 if (!sb_equal(refsb, sb)) {
751 printk(KERN_WARNING "md: %s has same UUID"
752 " but different superblock to %s\n",
753 b, bdevname(refdev->bdev, b2));
754 goto abort;
755 }
756 ev1 = md_event(sb);
757 ev2 = md_event(refsb);
758 if (ev1 > ev2)
759 ret = 1;
760 else
761 ret = 0;
762 }
763 rdev->size = calc_dev_size(rdev, sb->chunk_size);
764
NeilBrown2bf071b2006-01-06 00:20:55 -0800765 if (rdev->size < sb->size && sb->level > 1)
766 /* "this cannot possibly happen" ... */
767 ret = -EINVAL;
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 abort:
770 return ret;
771}
772
773/*
774 * validate_super for 0.90.0
775 */
776static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
777{
778 mdp_disk_t *desc;
779 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700780 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
NeilBrown41158c72005-06-21 17:17:25 -0700782 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800783 clear_bit(Faulty, &rdev->flags);
784 clear_bit(In_sync, &rdev->flags);
785 clear_bit(WriteMostly, &rdev->flags);
786 clear_bit(BarriersNotsupp, &rdev->flags);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (mddev->raid_disks == 0) {
789 mddev->major_version = 0;
790 mddev->minor_version = sb->minor_version;
791 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800792 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 mddev->chunk_size = sb->chunk_size;
794 mddev->ctime = sb->ctime;
795 mddev->utime = sb->utime;
796 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800797 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 mddev->layout = sb->layout;
799 mddev->raid_disks = sb->raid_disks;
800 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700801 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700802 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700803 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
NeilBrownf6705572006-03-27 01:18:11 -0800805 if (mddev->minor_version >= 91) {
806 mddev->reshape_position = sb->reshape_position;
807 mddev->delta_disks = sb->delta_disks;
808 mddev->new_level = sb->new_level;
809 mddev->new_layout = sb->new_layout;
810 mddev->new_chunk = sb->new_chunk;
811 } else {
812 mddev->reshape_position = MaxSector;
813 mddev->delta_disks = 0;
814 mddev->new_level = mddev->level;
815 mddev->new_layout = mddev->layout;
816 mddev->new_chunk = mddev->chunk_size;
817 }
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (sb->state & (1<<MD_SB_CLEAN))
820 mddev->recovery_cp = MaxSector;
821 else {
822 if (sb->events_hi == sb->cp_events_hi &&
823 sb->events_lo == sb->cp_events_lo) {
824 mddev->recovery_cp = sb->recovery_cp;
825 } else
826 mddev->recovery_cp = 0;
827 }
828
829 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
830 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
831 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
832 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
833
834 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700835
836 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700837 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700838 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700839
NeilBrown41158c72005-06-21 17:17:25 -0700840 } else if (mddev->pers == NULL) {
841 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 ++ev1;
843 if (ev1 < mddev->events)
844 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700845 } else if (mddev->bitmap) {
846 /* if adding to array with a bitmap, then we can accept an
847 * older device ... but not too old.
848 */
NeilBrown41158c72005-06-21 17:17:25 -0700849 if (ev1 < mddev->bitmap->events_cleared)
850 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700851 } else {
852 if (ev1 < mddev->events)
853 /* just a hot-add of a new device, leave raid_disk at -1 */
854 return 0;
855 }
NeilBrown41158c72005-06-21 17:17:25 -0700856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 desc = sb->disks + rdev->desc_nr;
859
860 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800861 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700862 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
863 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800864 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 rdev->raid_disk = desc->raid_disk;
866 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700867 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
868 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700869 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800870 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
872}
873
874/*
875 * sync_super for 0.90.0
876 */
877static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
878{
879 mdp_super_t *sb;
880 struct list_head *tmp;
881 mdk_rdev_t *rdev2;
882 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* make rdev->sb match mddev data..
886 *
887 * 1/ zero out disks
888 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
889 * 3/ any empty disks < next_spare become removed
890 *
891 * disks[0] gets initialised to REMOVED because
892 * we cannot be sure from other fields if it has
893 * been initialised or not.
894 */
895 int i;
896 int active=0, working=0,failed=0,spare=0,nr_disks=0;
897
NeilBrown61181562005-09-09 16:24:02 -0700898 rdev->sb_size = MD_SB_BYTES;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sb = (mdp_super_t*)page_address(rdev->sb_page);
901
902 memset(sb, 0, sizeof(*sb));
903
904 sb->md_magic = MD_SB_MAGIC;
905 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 sb->patch_version = mddev->patch_version;
907 sb->gvalid_words = 0; /* ignored */
908 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
909 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
910 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
911 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
912
913 sb->ctime = mddev->ctime;
914 sb->level = mddev->level;
915 sb->size = mddev->size;
916 sb->raid_disks = mddev->raid_disks;
917 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800918 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sb->utime = mddev->utime;
920 sb->state = 0;
921 sb->events_hi = (mddev->events>>32);
922 sb->events_lo = (u32)mddev->events;
923
NeilBrownf6705572006-03-27 01:18:11 -0800924 if (mddev->reshape_position == MaxSector)
925 sb->minor_version = 90;
926 else {
927 sb->minor_version = 91;
928 sb->reshape_position = mddev->reshape_position;
929 sb->new_level = mddev->new_level;
930 sb->delta_disks = mddev->delta_disks;
931 sb->new_layout = mddev->new_layout;
932 sb->new_chunk = mddev->new_chunk;
933 }
934 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (mddev->in_sync)
936 {
937 sb->recovery_cp = mddev->recovery_cp;
938 sb->cp_events_hi = (mddev->events>>32);
939 sb->cp_events_lo = (u32)mddev->events;
940 if (mddev->recovery_cp == MaxSector)
941 sb->state = (1<< MD_SB_CLEAN);
942 } else
943 sb->recovery_cp = 0;
944
945 sb->layout = mddev->layout;
946 sb->chunk_size = mddev->chunk_size;
947
NeilBrowna654b9d82005-06-21 17:17:27 -0700948 if (mddev->bitmap && mddev->bitmap_file == NULL)
949 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800952 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800954 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800955 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
956 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800957 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800959 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800960 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 d = &sb->disks[rdev2->desc_nr];
962 nr_disks++;
963 d->number = rdev2->desc_nr;
964 d->major = MAJOR(rdev2->bdev->bd_dev);
965 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800966 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
967 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 d->raid_disk = rdev2->raid_disk;
969 else
970 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800971 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800973 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 d->state = (1<<MD_DISK_ACTIVE);
975 d->state |= (1<<MD_DISK_SYNC);
976 active++;
977 working++;
978 } else {
979 d->state = 0;
980 spare++;
981 working++;
982 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700983 if (test_bit(WriteMostly, &rdev2->flags))
984 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 /* now set the "removed" and "faulty" bits on any missing devices */
987 for (i=0 ; i < mddev->raid_disks ; i++) {
988 mdp_disk_t *d = &sb->disks[i];
989 if (d->state == 0 && d->number == 0) {
990 d->number = i;
991 d->raid_disk = i;
992 d->state = (1<<MD_DISK_REMOVED);
993 d->state |= (1<<MD_DISK_FAULTY);
994 failed++;
995 }
996 }
997 sb->nr_disks = nr_disks;
998 sb->active_disks = active;
999 sb->working_disks = working;
1000 sb->failed_disks = failed;
1001 sb->spare_disks = spare;
1002
1003 sb->this_disk = sb->disks[rdev->desc_nr];
1004 sb->sb_csum = calc_sb_csum(sb);
1005}
1006
1007/*
1008 * version 1 superblock
1009 */
1010
NeilBrown1c05b4b2006-10-21 10:24:08 -07001011static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001013 __le32 disk_csum;
1014 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 unsigned long long newcsum;
1016 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001017 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int i;
1019
1020 disk_csum = sb->sb_csum;
1021 sb->sb_csum = 0;
1022 newcsum = 0;
1023 for (i=0; size>=4; size -= 4 )
1024 newcsum += le32_to_cpu(*isuper++);
1025
1026 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001027 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1030 sb->sb_csum = disk_csum;
1031 return cpu_to_le32(csum);
1032}
1033
1034static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1035{
1036 struct mdp_superblock_1 *sb;
1037 int ret;
1038 sector_t sb_offset;
1039 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001040 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /*
1043 * Calculate the position of the superblock.
1044 * It is always aligned to a 4K boundary and
1045 * depeding on minor_version, it can be:
1046 * 0: At least 8K, but less than 12K, from end of device
1047 * 1: At start of device
1048 * 2: 4K from start of device.
1049 */
1050 switch(minor_version) {
1051 case 0:
1052 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1053 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001054 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* convert from sectors to K */
1056 sb_offset /= 2;
1057 break;
1058 case 1:
1059 sb_offset = 0;
1060 break;
1061 case 2:
1062 sb_offset = 4;
1063 break;
1064 default:
1065 return -EINVAL;
1066 }
1067 rdev->sb_offset = sb_offset;
1068
NeilBrown0002b272005-09-09 16:23:53 -07001069 /* superblock is rarely larger than 1K, but it can be larger,
1070 * and it is safe to read 4k, so we do that
1071 */
1072 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (ret) return ret;
1074
1075
1076 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1077
1078 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1079 sb->major_version != cpu_to_le32(1) ||
1080 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1081 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001082 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 return -EINVAL;
1084
1085 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1086 printk("md: invalid superblock checksum on %s\n",
1087 bdevname(rdev->bdev,b));
1088 return -EINVAL;
1089 }
1090 if (le64_to_cpu(sb->data_size) < 10) {
1091 printk("md: data_size too small on %s\n",
1092 bdevname(rdev->bdev,b));
1093 return -EINVAL;
1094 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001095 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1096 if (sb->level != cpu_to_le32(1) &&
1097 sb->level != cpu_to_le32(4) &&
1098 sb->level != cpu_to_le32(5) &&
1099 sb->level != cpu_to_le32(6) &&
1100 sb->level != cpu_to_le32(10)) {
1101 printk(KERN_WARNING
1102 "md: bitmaps not supported for this level.\n");
1103 return -EINVAL;
1104 }
1105 }
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 rdev->preferred_minor = 0xffff;
1108 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001109 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
NeilBrown0002b272005-09-09 16:23:53 -07001111 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001112 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001113 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001114 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1115
1116 if (minor_version
1117 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1118 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001119
NeilBrown31b65a02006-07-10 04:44:14 -07001120 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1121 rdev->desc_nr = -1;
1122 else
1123 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1124
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001125 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001126 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001127 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 __u64 ev1, ev2;
1129 struct mdp_superblock_1 *refsb =
1130 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1131
1132 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1133 sb->level != refsb->level ||
1134 sb->layout != refsb->layout ||
1135 sb->chunksize != refsb->chunksize) {
1136 printk(KERN_WARNING "md: %s has strangely different"
1137 " superblock to %s\n",
1138 bdevname(rdev->bdev,b),
1139 bdevname(refdev->bdev,b2));
1140 return -EINVAL;
1141 }
1142 ev1 = le64_to_cpu(sb->events);
1143 ev2 = le64_to_cpu(refsb->events);
1144
1145 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001146 ret = 1;
1147 else
1148 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
NeilBrowna1801f82008-03-04 14:29:31 -08001150 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1152 else
1153 rdev->size = rdev->sb_offset;
1154 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1155 return -EINVAL;
1156 rdev->size = le64_to_cpu(sb->data_size)/2;
1157 if (le32_to_cpu(sb->chunksize))
1158 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001159
NeilBrown1c05b4b2006-10-21 10:24:08 -07001160 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001161 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1166{
1167 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001168 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
NeilBrown41158c72005-06-21 17:17:25 -07001170 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001171 clear_bit(Faulty, &rdev->flags);
1172 clear_bit(In_sync, &rdev->flags);
1173 clear_bit(WriteMostly, &rdev->flags);
1174 clear_bit(BarriersNotsupp, &rdev->flags);
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (mddev->raid_disks == 0) {
1177 mddev->major_version = 1;
1178 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001179 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1181 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1182 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1183 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001184 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 mddev->layout = le32_to_cpu(sb->layout);
1186 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1187 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001188 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001189 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001190 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1193 memcpy(mddev->uuid, sb->set_uuid, 16);
1194
1195 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001196
NeilBrown71c08052005-09-09 16:23:51 -07001197 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001198 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001199 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001200
NeilBrownf6705572006-03-27 01:18:11 -08001201 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1202 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1203 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1204 mddev->new_level = le32_to_cpu(sb->new_level);
1205 mddev->new_layout = le32_to_cpu(sb->new_layout);
1206 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1207 } else {
1208 mddev->reshape_position = MaxSector;
1209 mddev->delta_disks = 0;
1210 mddev->new_level = mddev->level;
1211 mddev->new_layout = mddev->layout;
1212 mddev->new_chunk = mddev->chunk_size;
1213 }
1214
NeilBrown41158c72005-06-21 17:17:25 -07001215 } else if (mddev->pers == NULL) {
1216 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 ++ev1;
1218 if (ev1 < mddev->events)
1219 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001220 } else if (mddev->bitmap) {
1221 /* If adding to array with a bitmap, then we can accept an
1222 * older device, but not too old.
1223 */
NeilBrown41158c72005-06-21 17:17:25 -07001224 if (ev1 < mddev->bitmap->events_cleared)
1225 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001226 } else {
1227 if (ev1 < mddev->events)
1228 /* just a hot-add of a new device, leave raid_disk at -1 */
1229 return 0;
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 if (mddev->level != LEVEL_MULTIPATH) {
1232 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1234 switch(role) {
1235 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 break;
1237 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001238 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 break;
1240 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001241 if ((le32_to_cpu(sb->feature_map) &
1242 MD_FEATURE_RECOVERY_OFFSET))
1243 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1244 else
1245 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 rdev->raid_disk = role;
1247 break;
1248 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001249 if (sb->devflags & WriteMostly1)
1250 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001251 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001252 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 0;
1255}
1256
1257static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1258{
1259 struct mdp_superblock_1 *sb;
1260 struct list_head *tmp;
1261 mdk_rdev_t *rdev2;
1262 int max_dev, i;
1263 /* make rdev->sb match mddev and rdev data. */
1264
1265 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1266
1267 sb->feature_map = 0;
1268 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001269 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 memset(sb->pad1, 0, sizeof(sb->pad1));
1271 memset(sb->pad2, 0, sizeof(sb->pad2));
1272 memset(sb->pad3, 0, sizeof(sb->pad3));
1273
1274 sb->utime = cpu_to_le64((__u64)mddev->utime);
1275 sb->events = cpu_to_le64(mddev->events);
1276 if (mddev->in_sync)
1277 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1278 else
1279 sb->resync_offset = cpu_to_le64(0);
1280
NeilBrown1c05b4b2006-10-21 10:24:08 -07001281 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001282
NeilBrownf0ca3402006-02-02 14:28:04 -08001283 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001284 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001285
NeilBrowna654b9d82005-06-21 17:17:27 -07001286 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1287 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001288 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001289 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001290
1291 if (rdev->raid_disk >= 0 &&
1292 !test_bit(In_sync, &rdev->flags) &&
1293 rdev->recovery_offset > 0) {
1294 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1295 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1296 }
1297
NeilBrownf6705572006-03-27 01:18:11 -08001298 if (mddev->reshape_position != MaxSector) {
1299 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1300 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1301 sb->new_layout = cpu_to_le32(mddev->new_layout);
1302 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1303 sb->new_level = cpu_to_le32(mddev->new_level);
1304 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1305 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001308 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if (rdev2->desc_nr+1 > max_dev)
1310 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001311
1312 if (max_dev > le32_to_cpu(sb->max_dev))
1313 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 for (i=0; i<max_dev;i++)
1315 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1316
NeilBrownd089c6a2008-02-06 01:39:59 -08001317 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001319 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001321 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001323 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1324 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 else
1326 sb->dev_roles[i] = cpu_to_le16(0xffff);
1327 }
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 sb->sb_csum = calc_sb_1_csum(sb);
1330}
1331
1332
Adrian Bunk75c96f82005-05-05 16:16:09 -07001333static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 [0] = {
1335 .name = "0.90.0",
1336 .owner = THIS_MODULE,
1337 .load_super = super_90_load,
1338 .validate_super = super_90_validate,
1339 .sync_super = super_90_sync,
1340 },
1341 [1] = {
1342 .name = "md-1",
1343 .owner = THIS_MODULE,
1344 .load_super = super_1_load,
1345 .validate_super = super_1_validate,
1346 .sync_super = super_1_sync,
1347 },
1348};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1351{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001352 struct list_head *tmp, *tmp2;
1353 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
NeilBrownd089c6a2008-02-06 01:39:59 -08001355 rdev_for_each(rdev, tmp, mddev1)
1356 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001357 if (rdev->bdev->bd_contains ==
1358 rdev2->bdev->bd_contains)
1359 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 return 0;
1362}
1363
1364static LIST_HEAD(pending_raid_disks);
1365
1366static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1367{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001368 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001369 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001370 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001371 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 if (rdev->mddev) {
1374 MD_BUG();
1375 return -EINVAL;
1376 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001377
1378 /* prevent duplicates */
1379 if (find_rdev(mddev, rdev->bdev->bd_dev))
1380 return -EEXIST;
1381
NeilBrown2bf071b2006-01-06 00:20:55 -08001382 /* make sure rdev->size exceeds mddev->size */
1383 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001384 if (mddev->pers) {
1385 /* Cannot change size, so fail
1386 * If mddev->level <= 0, then we don't care
1387 * about aligning sizes (e.g. linear)
1388 */
1389 if (mddev->level > 0)
1390 return -ENOSPC;
1391 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001392 mddev->size = rdev->size;
1393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 /* Verify rdev->desc_nr is unique.
1396 * If it is -1, assign a free number, else
1397 * check number is not in use
1398 */
1399 if (rdev->desc_nr < 0) {
1400 int choice = 0;
1401 if (mddev->pers) choice = mddev->raid_disks;
1402 while (find_rdev_nr(mddev, choice))
1403 choice++;
1404 rdev->desc_nr = choice;
1405 } else {
1406 if (find_rdev_nr(mddev, rdev->desc_nr))
1407 return -EBUSY;
1408 }
NeilBrown19133a42005-11-08 21:39:35 -08001409 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001410 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001411 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001414 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001415
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001416 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001417 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001418
NeilBrownf637b9f2005-11-08 21:39:37 -08001419 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001420 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001421 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001422 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001423 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1424 kobject_del(&rdev->kobj);
1425 goto fail;
1426 }
1427 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001428 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001430
1431 fail:
1432 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1433 b, mdname(mddev));
1434 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
NeilBrown177a99b2008-02-06 01:39:56 -08001437static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001438{
1439 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1440 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001441 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001442}
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1445{
1446 char b[BDEVNAME_SIZE];
1447 if (!rdev->mddev) {
1448 MD_BUG();
1449 return;
1450 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001451 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 list_del_init(&rdev->same_set);
1453 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1454 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001455 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001456
1457 /* We need to delay this, otherwise we can deadlock when
1458 * writing to 'remove' to "dev/state"
1459 */
NeilBrown177a99b2008-02-06 01:39:56 -08001460 INIT_WORK(&rdev->del_work, md_delayed_delete);
1461 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001462 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
1465/*
1466 * prevent the device from being mounted, repartitioned or
1467 * otherwise reused by a RAID array (or any other kernel
1468 * subsystem), by bd_claiming the device.
1469 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001470static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
1472 int err = 0;
1473 struct block_device *bdev;
1474 char b[BDEVNAME_SIZE];
1475
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001476 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (IS_ERR(bdev)) {
1478 printk(KERN_ERR "md: could not open %s.\n",
1479 __bdevname(dev, b));
1480 return PTR_ERR(bdev);
1481 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001482 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (err) {
1484 printk(KERN_ERR "md: could not bd_claim %s.\n",
1485 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001486 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return err;
1488 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001489 if (!shared)
1490 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 rdev->bdev = bdev;
1492 return err;
1493}
1494
1495static void unlock_rdev(mdk_rdev_t *rdev)
1496{
1497 struct block_device *bdev = rdev->bdev;
1498 rdev->bdev = NULL;
1499 if (!bdev)
1500 MD_BUG();
1501 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001502 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505void md_autodetect_dev(dev_t dev);
1506
1507static void export_rdev(mdk_rdev_t * rdev)
1508{
1509 char b[BDEVNAME_SIZE];
1510 printk(KERN_INFO "md: export_rdev(%s)\n",
1511 bdevname(rdev->bdev,b));
1512 if (rdev->mddev)
1513 MD_BUG();
1514 free_disk_sb(rdev);
1515 list_del_init(&rdev->same_set);
1516#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001517 if (test_bit(AutoDetected, &rdev->flags))
1518 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519#endif
1520 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001521 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524static void kick_rdev_from_array(mdk_rdev_t * rdev)
1525{
1526 unbind_rdev_from_array(rdev);
1527 export_rdev(rdev);
1528}
1529
1530static void export_array(mddev_t *mddev)
1531{
1532 struct list_head *tmp;
1533 mdk_rdev_t *rdev;
1534
NeilBrownd089c6a2008-02-06 01:39:59 -08001535 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (!rdev->mddev) {
1537 MD_BUG();
1538 continue;
1539 }
1540 kick_rdev_from_array(rdev);
1541 }
1542 if (!list_empty(&mddev->disks))
1543 MD_BUG();
1544 mddev->raid_disks = 0;
1545 mddev->major_version = 0;
1546}
1547
1548static void print_desc(mdp_disk_t *desc)
1549{
1550 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1551 desc->major,desc->minor,desc->raid_disk,desc->state);
1552}
1553
1554static void print_sb(mdp_super_t *sb)
1555{
1556 int i;
1557
1558 printk(KERN_INFO
1559 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1560 sb->major_version, sb->minor_version, sb->patch_version,
1561 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1562 sb->ctime);
1563 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1564 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1565 sb->md_minor, sb->layout, sb->chunk_size);
1566 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1567 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1568 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1569 sb->failed_disks, sb->spare_disks,
1570 sb->sb_csum, (unsigned long)sb->events_lo);
1571
1572 printk(KERN_INFO);
1573 for (i = 0; i < MD_SB_DISKS; i++) {
1574 mdp_disk_t *desc;
1575
1576 desc = sb->disks + i;
1577 if (desc->number || desc->major || desc->minor ||
1578 desc->raid_disk || (desc->state && (desc->state != 4))) {
1579 printk(" D %2d: ", i);
1580 print_desc(desc);
1581 }
1582 }
1583 printk(KERN_INFO "md: THIS: ");
1584 print_desc(&sb->this_disk);
1585
1586}
1587
1588static void print_rdev(mdk_rdev_t *rdev)
1589{
1590 char b[BDEVNAME_SIZE];
1591 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1592 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001593 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1594 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (rdev->sb_loaded) {
1596 printk(KERN_INFO "md: rdev superblock:\n");
1597 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1598 } else
1599 printk(KERN_INFO "md: no rdev superblock!\n");
1600}
1601
Adrian Bunk5e563412006-06-26 00:27:42 -07001602static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 struct list_head *tmp, *tmp2;
1605 mdk_rdev_t *rdev;
1606 mddev_t *mddev;
1607 char b[BDEVNAME_SIZE];
1608
1609 printk("\n");
1610 printk("md: **********************************\n");
1611 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1612 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001613 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
NeilBrown32a76272005-06-21 17:17:14 -07001615 if (mddev->bitmap)
1616 bitmap_print_sb(mddev->bitmap);
1617 else
1618 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001619 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 printk("<%s>", bdevname(rdev->bdev,b));
1621 printk("\n");
1622
NeilBrownd089c6a2008-02-06 01:39:59 -08001623 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 print_rdev(rdev);
1625 }
1626 printk("md: **********************************\n");
1627 printk("\n");
1628}
1629
1630
NeilBrown42543762006-06-26 00:27:57 -07001631static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
NeilBrown42543762006-06-26 00:27:57 -07001633 /* Update each superblock (in-memory image), but
1634 * if we are allowed to, skip spares which already
1635 * have the right event counter, or have one earlier
1636 * (which would mean they aren't being marked as dirty
1637 * with the rest of the array)
1638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 mdk_rdev_t *rdev;
1640 struct list_head *tmp;
1641
NeilBrownd089c6a2008-02-06 01:39:59 -08001642 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001643 if (rdev->sb_events == mddev->events ||
1644 (nospares &&
1645 rdev->raid_disk < 0 &&
1646 (rdev->sb_events&1)==0 &&
1647 rdev->sb_events+1 == mddev->events)) {
1648 /* Don't update this superblock */
1649 rdev->sb_loaded = 2;
1650 } else {
1651 super_types[mddev->major_version].
1652 sync_super(mddev, rdev);
1653 rdev->sb_loaded = 1;
1654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 }
1656}
1657
NeilBrown850b2b42006-10-03 01:15:46 -07001658static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 struct list_head *tmp;
1661 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001662 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001663 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
NeilBrown8377bc82008-04-30 00:52:28 -07001665 if (mddev->external)
1666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001668 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001669
NeilBrown850b2b42006-10-03 01:15:46 -07001670 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1671 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1672 force_change = 1;
1673 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1674 /* just a clean<-> dirty transition, possibly leave spares alone,
1675 * though if events isn't the right even/odd, we will have to do
1676 * spares after all
1677 */
1678 nospares = 1;
1679 if (force_change)
1680 nospares = 0;
1681 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001682 /* If the array is degraded, then skipping spares is both
1683 * dangerous and fairly pointless.
1684 * Dangerous because a device that was removed from the array
1685 * might have a event_count that still looks up-to-date,
1686 * so it can be re-added without a resync.
1687 * Pointless because if there are any spares to skip,
1688 * then a recovery will happen and soon that array won't
1689 * be degraded any more and the spare can go back to sleep then.
1690 */
NeilBrown850b2b42006-10-03 01:15:46 -07001691 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001692
NeilBrown06d91a52005-06-21 17:17:12 -07001693 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001695
1696 /* If this is just a dirty<->clean transition, and the array is clean
1697 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001698 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001699 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001700 && (mddev->events & 1)
1701 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001702 mddev->events--;
1703 else {
1704 /* otherwise we have to go forward and ... */
1705 mddev->events ++;
1706 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1707 /* .. if the array isn't clean, insist on an odd 'events' */
1708 if ((mddev->events&1)==0) {
1709 mddev->events++;
1710 nospares = 0;
1711 }
1712 } else {
1713 /* otherwise insist on an even 'events' (for clean states) */
1714 if ((mddev->events&1)) {
1715 mddev->events++;
1716 nospares = 0;
1717 }
1718 }
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 if (!mddev->events) {
1722 /*
1723 * oops, this 64-bit counter should never wrap.
1724 * Either we are in around ~1 trillion A.C., assuming
1725 * 1 reboot per second, or we have a bug:
1726 */
1727 MD_BUG();
1728 mddev->events --;
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 /*
1732 * do not write anything to disk if using
1733 * nonpersistent superblocks
1734 */
NeilBrown06d91a52005-06-21 17:17:12 -07001735 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001736 if (!mddev->external)
1737 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1738
NeilBrowna9701a32005-11-08 21:39:34 -08001739 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001740 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001742 }
NeilBrowne6910632008-02-06 01:39:51 -08001743 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001744 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 dprintk(KERN_INFO
1747 "md: updating %s RAID superblock on device (in sync %d)\n",
1748 mdname(mddev),mddev->in_sync);
1749
NeilBrown4ad13662007-07-17 04:06:13 -07001750 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001751 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 char b[BDEVNAME_SIZE];
1753 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001754 if (rdev->sb_loaded != 1)
1755 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001756 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 dprintk("(skipping faulty ");
1758
1759 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001760 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001761 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001762 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001763 rdev->sb_page);
1764 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1765 bdevname(rdev->bdev,b),
1766 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001767 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 } else
1770 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001771 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 /* only need to write one superblock... */
1773 break;
1774 }
NeilBrowna9701a32005-11-08 21:39:34 -08001775 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001776 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001777
NeilBrowna9701a32005-11-08 21:39:34 -08001778 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001779 if (mddev->in_sync != sync_req ||
1780 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001781 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001782 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001783 goto repeat;
1784 }
NeilBrown850b2b42006-10-03 01:15:46 -07001785 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001786 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001787 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789}
1790
NeilBrownbce74da2006-01-06 00:20:41 -08001791/* words written to sysfs files may, or my not, be \n terminated.
1792 * We want to accept with case. For this we use cmd_match.
1793 */
1794static int cmd_match(const char *cmd, const char *str)
1795{
1796 /* See if cmd, written into a sysfs file, matches
1797 * str. They must either be the same, or cmd can
1798 * have a trailing newline
1799 */
1800 while (*cmd && *str && *cmd == *str) {
1801 cmd++;
1802 str++;
1803 }
1804 if (*cmd == '\n')
1805 cmd++;
1806 if (*str || *cmd)
1807 return 0;
1808 return 1;
1809}
1810
NeilBrown86e6ffd2005-11-08 21:39:24 -08001811struct rdev_sysfs_entry {
1812 struct attribute attr;
1813 ssize_t (*show)(mdk_rdev_t *, char *);
1814 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1815};
1816
1817static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001818state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001819{
1820 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001821 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001822
NeilBrownb2d444d2005-11-08 21:39:31 -08001823 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001824 len+= sprintf(page+len, "%sfaulty",sep);
1825 sep = ",";
1826 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001827 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001828 len += sprintf(page+len, "%sin_sync",sep);
1829 sep = ",";
1830 }
NeilBrownf6556752006-06-26 00:28:01 -07001831 if (test_bit(WriteMostly, &rdev->flags)) {
1832 len += sprintf(page+len, "%swrite_mostly",sep);
1833 sep = ",";
1834 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001835 if (test_bit(Blocked, &rdev->flags)) {
1836 len += sprintf(page+len, "%sblocked", sep);
1837 sep = ",";
1838 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001839 if (!test_bit(Faulty, &rdev->flags) &&
1840 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001841 len += sprintf(page+len, "%sspare", sep);
1842 sep = ",";
1843 }
1844 return len+sprintf(page+len, "\n");
1845}
1846
NeilBrown45dc2de2006-06-26 00:27:58 -07001847static ssize_t
1848state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1849{
1850 /* can write
1851 * faulty - simulates and error
1852 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001853 * writemostly - sets write_mostly
1854 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001855 * blocked - sets the Blocked flag
1856 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001857 */
1858 int err = -EINVAL;
1859 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1860 md_error(rdev->mddev, rdev);
1861 err = 0;
1862 } else if (cmd_match(buf, "remove")) {
1863 if (rdev->raid_disk >= 0)
1864 err = -EBUSY;
1865 else {
1866 mddev_t *mddev = rdev->mddev;
1867 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001868 if (mddev->pers)
1869 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001870 md_new_event(mddev);
1871 err = 0;
1872 }
NeilBrownf6556752006-06-26 00:28:01 -07001873 } else if (cmd_match(buf, "writemostly")) {
1874 set_bit(WriteMostly, &rdev->flags);
1875 err = 0;
1876 } else if (cmd_match(buf, "-writemostly")) {
1877 clear_bit(WriteMostly, &rdev->flags);
1878 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001879 } else if (cmd_match(buf, "blocked")) {
1880 set_bit(Blocked, &rdev->flags);
1881 err = 0;
1882 } else if (cmd_match(buf, "-blocked")) {
1883 clear_bit(Blocked, &rdev->flags);
1884 wake_up(&rdev->blocked_wait);
1885 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1886 md_wakeup_thread(rdev->mddev->thread);
1887
1888 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001889 }
1890 return err ? err : len;
1891}
NeilBrown80ca3a42006-07-10 04:44:18 -07001892static struct rdev_sysfs_entry rdev_state =
1893__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001894
1895static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001896errors_show(mdk_rdev_t *rdev, char *page)
1897{
1898 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1899}
1900
1901static ssize_t
1902errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1903{
1904 char *e;
1905 unsigned long n = simple_strtoul(buf, &e, 10);
1906 if (*buf && (*e == 0 || *e == '\n')) {
1907 atomic_set(&rdev->corrected_errors, n);
1908 return len;
1909 }
1910 return -EINVAL;
1911}
1912static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001913__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001914
NeilBrown014236d2006-01-06 00:20:55 -08001915static ssize_t
1916slot_show(mdk_rdev_t *rdev, char *page)
1917{
1918 if (rdev->raid_disk < 0)
1919 return sprintf(page, "none\n");
1920 else
1921 return sprintf(page, "%d\n", rdev->raid_disk);
1922}
1923
1924static ssize_t
1925slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1926{
1927 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001928 int err;
1929 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001930 int slot = simple_strtoul(buf, &e, 10);
1931 if (strncmp(buf, "none", 4)==0)
1932 slot = -1;
1933 else if (e==buf || (*e && *e!= '\n'))
1934 return -EINVAL;
NeilBrownc303da62008-02-06 01:39:51 -08001935 if (rdev->mddev->pers) {
1936 /* Setting 'slot' on an active array requires also
1937 * updating the 'rd%d' link, and communicating
1938 * with the personality with ->hot_*_disk.
1939 * For now we only support removing
1940 * failed/spare devices. This normally happens automatically,
1941 * but not when the metadata is externally managed.
1942 */
1943 if (slot != -1)
1944 return -EBUSY;
1945 if (rdev->raid_disk == -1)
1946 return -EEXIST;
1947 /* personality does all needed checks */
1948 if (rdev->mddev->pers->hot_add_disk == NULL)
1949 return -EINVAL;
1950 err = rdev->mddev->pers->
1951 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1952 if (err)
1953 return err;
1954 sprintf(nm, "rd%d", rdev->raid_disk);
1955 sysfs_remove_link(&rdev->mddev->kobj, nm);
1956 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1957 md_wakeup_thread(rdev->mddev->thread);
1958 } else {
1959 if (slot >= rdev->mddev->raid_disks)
1960 return -ENOSPC;
1961 rdev->raid_disk = slot;
1962 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001963 clear_bit(Faulty, &rdev->flags);
1964 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08001965 set_bit(In_sync, &rdev->flags);
1966 }
NeilBrown014236d2006-01-06 00:20:55 -08001967 return len;
1968}
1969
1970
1971static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001972__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001973
NeilBrown93c8cad2006-01-06 00:20:56 -08001974static ssize_t
1975offset_show(mdk_rdev_t *rdev, char *page)
1976{
Andrew Morton6961ece2006-01-06 00:20:59 -08001977 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001978}
1979
1980static ssize_t
1981offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1982{
1983 char *e;
1984 unsigned long long offset = simple_strtoull(buf, &e, 10);
1985 if (e==buf || (*e && *e != '\n'))
1986 return -EINVAL;
1987 if (rdev->mddev->pers)
1988 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001989 if (rdev->size && rdev->mddev->external)
1990 /* Must set offset before size, so overlap checks
1991 * can be sane */
1992 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08001993 rdev->data_offset = offset;
1994 return len;
1995}
1996
1997static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001998__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001999
NeilBrown83303b62006-01-06 00:21:06 -08002000static ssize_t
2001rdev_size_show(mdk_rdev_t *rdev, char *page)
2002{
2003 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2004}
2005
NeilBrownc5d79ad2008-02-06 01:39:54 -08002006static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2007{
2008 /* check if two start/length pairs overlap */
2009 if (s1+l1 <= s2)
2010 return 0;
2011 if (s2+l2 <= s1)
2012 return 0;
2013 return 1;
2014}
2015
NeilBrown83303b62006-01-06 00:21:06 -08002016static ssize_t
2017rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2018{
2019 char *e;
2020 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002021 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002022 mddev_t *my_mddev = rdev->mddev;
2023
NeilBrown83303b62006-01-06 00:21:06 -08002024 if (e==buf || (*e && *e != '\n'))
2025 return -EINVAL;
NeilBrown27c529b2008-03-04 14:29:33 -08002026 if (my_mddev->pers)
NeilBrown83303b62006-01-06 00:21:06 -08002027 return -EBUSY;
2028 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002029 if (size > oldsize && rdev->mddev->external) {
2030 /* need to check that all other rdevs with the same ->bdev
2031 * do not overlap. We need to unlock the mddev to avoid
2032 * a deadlock. We have already changed rdev->size, and if
2033 * we have to change it back, we will have the lock again.
2034 */
2035 mddev_t *mddev;
2036 int overlap = 0;
2037 struct list_head *tmp, *tmp2;
2038
NeilBrown27c529b2008-03-04 14:29:33 -08002039 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002040 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002041 mdk_rdev_t *rdev2;
2042
2043 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002044 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002045 if (test_bit(AllReserved, &rdev2->flags) ||
2046 (rdev->bdev == rdev2->bdev &&
2047 rdev != rdev2 &&
2048 overlaps(rdev->data_offset, rdev->size,
2049 rdev2->data_offset, rdev2->size))) {
2050 overlap = 1;
2051 break;
2052 }
2053 mddev_unlock(mddev);
2054 if (overlap) {
2055 mddev_put(mddev);
2056 break;
2057 }
2058 }
NeilBrown27c529b2008-03-04 14:29:33 -08002059 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002060 if (overlap) {
2061 /* Someone else could have slipped in a size
2062 * change here, but doing so is just silly.
2063 * We put oldsize back because we *know* it is
2064 * safe, and trust userspace not to race with
2065 * itself
2066 */
2067 rdev->size = oldsize;
2068 return -EBUSY;
2069 }
2070 }
NeilBrown27c529b2008-03-04 14:29:33 -08002071 if (size < my_mddev->size || my_mddev->size == 0)
2072 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002073 return len;
2074}
2075
2076static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002077__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002078
NeilBrown86e6ffd2005-11-08 21:39:24 -08002079static struct attribute *rdev_default_attrs[] = {
2080 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002081 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002082 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002083 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002084 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002085 NULL,
2086};
2087static ssize_t
2088rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2089{
2090 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2091 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002092 mddev_t *mddev = rdev->mddev;
2093 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002094
2095 if (!entry->show)
2096 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002097
2098 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2099 if (!rv) {
2100 if (rdev->mddev == NULL)
2101 rv = -EBUSY;
2102 else
2103 rv = entry->show(rdev, page);
2104 mddev_unlock(mddev);
2105 }
2106 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002107}
2108
2109static ssize_t
2110rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2111 const char *page, size_t length)
2112{
2113 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2114 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002115 ssize_t rv;
2116 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002117
2118 if (!entry->store)
2119 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002120 if (!capable(CAP_SYS_ADMIN))
2121 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002122 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002123 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002124 if (rdev->mddev == NULL)
2125 rv = -EBUSY;
2126 else
2127 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002128 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002129 }
2130 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002131}
2132
2133static void rdev_free(struct kobject *ko)
2134{
2135 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2136 kfree(rdev);
2137}
2138static struct sysfs_ops rdev_sysfs_ops = {
2139 .show = rdev_attr_show,
2140 .store = rdev_attr_store,
2141};
2142static struct kobj_type rdev_ktype = {
2143 .release = rdev_free,
2144 .sysfs_ops = &rdev_sysfs_ops,
2145 .default_attrs = rdev_default_attrs,
2146};
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/*
2149 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2150 *
2151 * mark the device faulty if:
2152 *
2153 * - the device is nonexistent (zero size)
2154 * - the device has no valid superblock
2155 *
2156 * a faulty rdev _never_ has rdev->sb set.
2157 */
2158static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2159{
2160 char b[BDEVNAME_SIZE];
2161 int err;
2162 mdk_rdev_t *rdev;
2163 sector_t size;
2164
NeilBrown9ffae0c2006-01-06 00:20:32 -08002165 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (!rdev) {
2167 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2168 return ERR_PTR(-ENOMEM);
2169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 if ((err = alloc_disk_sb(rdev)))
2172 goto abort_free;
2173
NeilBrownc5d79ad2008-02-06 01:39:54 -08002174 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (err)
2176 goto abort_free;
2177
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002178 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002181 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002182 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002183 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002185 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002187 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002188 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2191 if (!size) {
2192 printk(KERN_WARNING
2193 "md: %s has zero or unknown size, marking faulty!\n",
2194 bdevname(rdev->bdev,b));
2195 err = -EINVAL;
2196 goto abort_free;
2197 }
2198
2199 if (super_format >= 0) {
2200 err = super_types[super_format].
2201 load_super(rdev, NULL, super_minor);
2202 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002203 printk(KERN_WARNING
2204 "md: %s does not have a valid v%d.%d "
2205 "superblock, not importing!\n",
2206 bdevname(rdev->bdev,b),
2207 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto abort_free;
2209 }
2210 if (err < 0) {
2211 printk(KERN_WARNING
2212 "md: could not read %s's sb, not importing!\n",
2213 bdevname(rdev->bdev,b));
2214 goto abort_free;
2215 }
2216 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002219 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 return rdev;
2222
2223abort_free:
2224 if (rdev->sb_page) {
2225 if (rdev->bdev)
2226 unlock_rdev(rdev);
2227 free_disk_sb(rdev);
2228 }
2229 kfree(rdev);
2230 return ERR_PTR(err);
2231}
2232
2233/*
2234 * Check a full RAID array for plausibility
2235 */
2236
2237
NeilBrowna757e642005-04-16 15:26:42 -07002238static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
2240 int i;
2241 struct list_head *tmp;
2242 mdk_rdev_t *rdev, *freshest;
2243 char b[BDEVNAME_SIZE];
2244
2245 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002246 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 switch (super_types[mddev->major_version].
2248 load_super(rdev, freshest, mddev->minor_version)) {
2249 case 1:
2250 freshest = rdev;
2251 break;
2252 case 0:
2253 break;
2254 default:
2255 printk( KERN_ERR \
2256 "md: fatal superblock inconsistency in %s"
2257 " -- removing from array\n",
2258 bdevname(rdev->bdev,b));
2259 kick_rdev_from_array(rdev);
2260 }
2261
2262
2263 super_types[mddev->major_version].
2264 validate_super(mddev, freshest);
2265
2266 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002267 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (rdev != freshest)
2269 if (super_types[mddev->major_version].
2270 validate_super(mddev, rdev)) {
2271 printk(KERN_WARNING "md: kicking non-fresh %s"
2272 " from array!\n",
2273 bdevname(rdev->bdev,b));
2274 kick_rdev_from_array(rdev);
2275 continue;
2276 }
2277 if (mddev->level == LEVEL_MULTIPATH) {
2278 rdev->desc_nr = i++;
2279 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002280 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002281 } else if (rdev->raid_disk >= mddev->raid_disks) {
2282 rdev->raid_disk = -1;
2283 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285 }
2286
2287
2288
2289 if (mddev->recovery_cp != MaxSector &&
2290 mddev->level >= 1)
2291 printk(KERN_ERR "md: %s: raid array is not clean"
2292 " -- starting background reconstruction\n",
2293 mdname(mddev));
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295}
2296
NeilBrowneae17012005-11-08 21:39:23 -08002297static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002298safe_delay_show(mddev_t *mddev, char *page)
2299{
2300 int msec = (mddev->safemode_delay*1000)/HZ;
2301 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2302}
2303static ssize_t
2304safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2305{
2306 int scale=1;
2307 int dot=0;
2308 int i;
2309 unsigned long msec;
2310 char buf[30];
2311 char *e;
2312 /* remove a period, and count digits after it */
2313 if (len >= sizeof(buf))
2314 return -EINVAL;
2315 strlcpy(buf, cbuf, len);
2316 buf[len] = 0;
2317 for (i=0; i<len; i++) {
2318 if (dot) {
2319 if (isdigit(buf[i])) {
2320 buf[i-1] = buf[i];
2321 scale *= 10;
2322 }
2323 buf[i] = 0;
2324 } else if (buf[i] == '.') {
2325 dot=1;
2326 buf[i] = 0;
2327 }
2328 }
2329 msec = simple_strtoul(buf, &e, 10);
2330 if (e == buf || (*e && *e != '\n'))
2331 return -EINVAL;
2332 msec = (msec * 1000) / scale;
2333 if (msec == 0)
2334 mddev->safemode_delay = 0;
2335 else {
2336 mddev->safemode_delay = (msec*HZ)/1000;
2337 if (mddev->safemode_delay == 0)
2338 mddev->safemode_delay = 1;
2339 }
2340 return len;
2341}
2342static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002343__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002344
2345static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002346level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002347{
NeilBrown2604b702006-01-06 00:20:36 -08002348 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002349 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002350 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002351 else if (mddev->clevel[0])
2352 return sprintf(page, "%s\n", mddev->clevel);
2353 else if (mddev->level != LEVEL_NONE)
2354 return sprintf(page, "%d\n", mddev->level);
2355 else
2356 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002357}
2358
NeilBrownd9d166c2006-01-06 00:20:51 -08002359static ssize_t
2360level_store(mddev_t *mddev, const char *buf, size_t len)
2361{
NeilBrown20a49ff2008-02-06 01:39:57 -08002362 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002363 if (mddev->pers)
2364 return -EBUSY;
2365 if (len == 0)
2366 return 0;
2367 if (len >= sizeof(mddev->clevel))
2368 return -ENOSPC;
2369 strncpy(mddev->clevel, buf, len);
2370 if (mddev->clevel[len-1] == '\n')
2371 len--;
2372 mddev->clevel[len] = 0;
2373 mddev->level = LEVEL_NONE;
2374 return rv;
2375}
2376
2377static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002378__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002379
NeilBrownd4dbd022006-06-26 00:27:59 -07002380
2381static ssize_t
2382layout_show(mddev_t *mddev, char *page)
2383{
2384 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002385 if (mddev->reshape_position != MaxSector &&
2386 mddev->layout != mddev->new_layout)
2387 return sprintf(page, "%d (%d)\n",
2388 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002389 return sprintf(page, "%d\n", mddev->layout);
2390}
2391
2392static ssize_t
2393layout_store(mddev_t *mddev, const char *buf, size_t len)
2394{
2395 char *e;
2396 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002397
2398 if (!*buf || (*e && *e != '\n'))
2399 return -EINVAL;
2400
NeilBrown08a02ec2007-05-09 02:35:38 -07002401 if (mddev->pers)
2402 return -EBUSY;
2403 if (mddev->reshape_position != MaxSector)
2404 mddev->new_layout = n;
2405 else
2406 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002407 return len;
2408}
2409static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002410__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002411
2412
NeilBrowneae17012005-11-08 21:39:23 -08002413static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002414raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002415{
NeilBrownbb636542005-11-08 21:39:45 -08002416 if (mddev->raid_disks == 0)
2417 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002418 if (mddev->reshape_position != MaxSector &&
2419 mddev->delta_disks != 0)
2420 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2421 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002422 return sprintf(page, "%d\n", mddev->raid_disks);
2423}
2424
NeilBrownda943b992006-01-06 00:20:54 -08002425static int update_raid_disks(mddev_t *mddev, int raid_disks);
2426
2427static ssize_t
2428raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2429{
NeilBrownda943b992006-01-06 00:20:54 -08002430 char *e;
2431 int rv = 0;
2432 unsigned long n = simple_strtoul(buf, &e, 10);
2433
2434 if (!*buf || (*e && *e != '\n'))
2435 return -EINVAL;
2436
2437 if (mddev->pers)
2438 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002439 else if (mddev->reshape_position != MaxSector) {
2440 int olddisks = mddev->raid_disks - mddev->delta_disks;
2441 mddev->delta_disks = n - olddisks;
2442 mddev->raid_disks = n;
2443 } else
NeilBrownda943b992006-01-06 00:20:54 -08002444 mddev->raid_disks = n;
2445 return rv ? rv : len;
2446}
2447static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002448__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002449
NeilBrown24dd4692005-11-08 21:39:26 -08002450static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002451chunk_size_show(mddev_t *mddev, char *page)
2452{
NeilBrown08a02ec2007-05-09 02:35:38 -07002453 if (mddev->reshape_position != MaxSector &&
2454 mddev->chunk_size != mddev->new_chunk)
2455 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2456 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002457 return sprintf(page, "%d\n", mddev->chunk_size);
2458}
2459
2460static ssize_t
2461chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2462{
2463 /* can only set chunk_size if array is not yet active */
2464 char *e;
2465 unsigned long n = simple_strtoul(buf, &e, 10);
2466
NeilBrown3b343802006-01-06 00:20:47 -08002467 if (!*buf || (*e && *e != '\n'))
2468 return -EINVAL;
2469
NeilBrown08a02ec2007-05-09 02:35:38 -07002470 if (mddev->pers)
2471 return -EBUSY;
2472 else if (mddev->reshape_position != MaxSector)
2473 mddev->new_chunk = n;
2474 else
2475 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002476 return len;
2477}
2478static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002479__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002480
NeilBrowna94213b2006-06-26 00:28:00 -07002481static ssize_t
2482resync_start_show(mddev_t *mddev, char *page)
2483{
2484 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2485}
2486
2487static ssize_t
2488resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2489{
NeilBrowna94213b2006-06-26 00:28:00 -07002490 char *e;
2491 unsigned long long n = simple_strtoull(buf, &e, 10);
2492
2493 if (mddev->pers)
2494 return -EBUSY;
2495 if (!*buf || (*e && *e != '\n'))
2496 return -EINVAL;
2497
2498 mddev->recovery_cp = n;
2499 return len;
2500}
2501static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002502__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002503
NeilBrown9e653b62006-06-26 00:27:58 -07002504/*
2505 * The array state can be:
2506 *
2507 * clear
2508 * No devices, no size, no level
2509 * Equivalent to STOP_ARRAY ioctl
2510 * inactive
2511 * May have some settings, but array is not active
2512 * all IO results in error
2513 * When written, doesn't tear down array, but just stops it
2514 * suspended (not supported yet)
2515 * All IO requests will block. The array can be reconfigured.
2516 * Writing this, if accepted, will block until array is quiessent
2517 * readonly
2518 * no resync can happen. no superblocks get written.
2519 * write requests fail
2520 * read-auto
2521 * like readonly, but behaves like 'clean' on a write request.
2522 *
2523 * clean - no pending writes, but otherwise active.
2524 * When written to inactive array, starts without resync
2525 * If a write request arrives then
2526 * if metadata is known, mark 'dirty' and switch to 'active'.
2527 * if not known, block and switch to write-pending
2528 * If written to an active array that has pending writes, then fails.
2529 * active
2530 * fully active: IO and resync can be happening.
2531 * When written to inactive array, starts with resync
2532 *
2533 * write-pending
2534 * clean, but writes are blocked waiting for 'active' to be written.
2535 *
2536 * active-idle
2537 * like active, but no writes have been seen for a while (100msec).
2538 *
2539 */
2540enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2541 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002542static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002543 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2544 "write-pending", "active-idle", NULL };
2545
2546static int match_word(const char *word, char **list)
2547{
2548 int n;
2549 for (n=0; list[n]; n++)
2550 if (cmd_match(word, list[n]))
2551 break;
2552 return n;
2553}
2554
2555static ssize_t
2556array_state_show(mddev_t *mddev, char *page)
2557{
2558 enum array_state st = inactive;
2559
2560 if (mddev->pers)
2561 switch(mddev->ro) {
2562 case 1:
2563 st = readonly;
2564 break;
2565 case 2:
2566 st = read_auto;
2567 break;
2568 case 0:
2569 if (mddev->in_sync)
2570 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002571 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2572 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002573 else if (mddev->safemode)
2574 st = active_idle;
2575 else
2576 st = active;
2577 }
2578 else {
2579 if (list_empty(&mddev->disks) &&
2580 mddev->raid_disks == 0 &&
2581 mddev->size == 0)
2582 st = clear;
2583 else
2584 st = inactive;
2585 }
2586 return sprintf(page, "%s\n", array_states[st]);
2587}
2588
2589static int do_md_stop(mddev_t * mddev, int ro);
2590static int do_md_run(mddev_t * mddev);
2591static int restart_array(mddev_t *mddev);
2592
2593static ssize_t
2594array_state_store(mddev_t *mddev, const char *buf, size_t len)
2595{
2596 int err = -EINVAL;
2597 enum array_state st = match_word(buf, array_states);
2598 switch(st) {
2599 case bad_word:
2600 break;
2601 case clear:
2602 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002603 if (atomic_read(&mddev->active) > 1)
2604 return -EBUSY;
2605 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002606 break;
2607 case inactive:
2608 /* stopping an active array */
2609 if (mddev->pers) {
2610 if (atomic_read(&mddev->active) > 1)
2611 return -EBUSY;
2612 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002613 } else
2614 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002615 break;
2616 case suspended:
2617 break; /* not supported yet */
2618 case readonly:
2619 if (mddev->pers)
2620 err = do_md_stop(mddev, 1);
2621 else {
2622 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002623 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002624 err = do_md_run(mddev);
2625 }
2626 break;
2627 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002628 if (mddev->pers) {
NeilBrown648b6292008-04-30 00:52:30 -07002629 if (mddev->ro != 1)
2630 err = do_md_stop(mddev, 1);
2631 else
2632 err = restart_array(mddev);
2633 if (err == 0) {
2634 mddev->ro = 2;
2635 set_disk_ro(mddev->gendisk, 0);
2636 }
NeilBrown9e653b62006-06-26 00:27:58 -07002637 } else {
2638 mddev->ro = 2;
2639 err = do_md_run(mddev);
2640 }
2641 break;
2642 case clean:
2643 if (mddev->pers) {
2644 restart_array(mddev);
2645 spin_lock_irq(&mddev->write_lock);
2646 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002647 if (mddev->in_sync == 0) {
2648 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002649 if (mddev->safemode == 1)
2650 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002651 if (mddev->persistent)
2652 set_bit(MD_CHANGE_CLEAN,
2653 &mddev->flags);
2654 }
2655 err = 0;
2656 } else
2657 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002658 spin_unlock_irq(&mddev->write_lock);
2659 } else {
2660 mddev->ro = 0;
2661 mddev->recovery_cp = MaxSector;
2662 err = do_md_run(mddev);
2663 }
2664 break;
2665 case active:
2666 if (mddev->pers) {
2667 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002668 if (mddev->external)
2669 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002670 wake_up(&mddev->sb_wait);
2671 err = 0;
2672 } else {
2673 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002674 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002675 err = do_md_run(mddev);
2676 }
2677 break;
2678 case write_pending:
2679 case active_idle:
2680 /* these cannot be set */
2681 break;
2682 }
2683 if (err)
2684 return err;
2685 else
2686 return len;
2687}
NeilBrown80ca3a42006-07-10 04:44:18 -07002688static struct md_sysfs_entry md_array_state =
2689__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002690
NeilBrown6d7ff732006-01-06 00:21:16 -08002691static ssize_t
2692null_show(mddev_t *mddev, char *page)
2693{
2694 return -EINVAL;
2695}
2696
2697static ssize_t
2698new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2699{
2700 /* buf must be %d:%d\n? giving major and minor numbers */
2701 /* The new device is added to the array.
2702 * If the array has a persistent superblock, we read the
2703 * superblock to initialise info and check validity.
2704 * Otherwise, only checking done is that in bind_rdev_to_array,
2705 * which mainly checks size.
2706 */
2707 char *e;
2708 int major = simple_strtoul(buf, &e, 10);
2709 int minor;
2710 dev_t dev;
2711 mdk_rdev_t *rdev;
2712 int err;
2713
2714 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2715 return -EINVAL;
2716 minor = simple_strtoul(e+1, &e, 10);
2717 if (*e && *e != '\n')
2718 return -EINVAL;
2719 dev = MKDEV(major, minor);
2720 if (major != MAJOR(dev) ||
2721 minor != MINOR(dev))
2722 return -EOVERFLOW;
2723
2724
2725 if (mddev->persistent) {
2726 rdev = md_import_device(dev, mddev->major_version,
2727 mddev->minor_version);
2728 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2729 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2730 mdk_rdev_t, same_set);
2731 err = super_types[mddev->major_version]
2732 .load_super(rdev, rdev0, mddev->minor_version);
2733 if (err < 0)
2734 goto out;
2735 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002736 } else if (mddev->external)
2737 rdev = md_import_device(dev, -2, -1);
2738 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002739 rdev = md_import_device(dev, -1, -1);
2740
2741 if (IS_ERR(rdev))
2742 return PTR_ERR(rdev);
2743 err = bind_rdev_to_array(rdev, mddev);
2744 out:
2745 if (err)
2746 export_rdev(rdev);
2747 return err ? err : len;
2748}
2749
2750static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002751__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002752
2753static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002754bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2755{
2756 char *end;
2757 unsigned long chunk, end_chunk;
2758
2759 if (!mddev->bitmap)
2760 goto out;
2761 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2762 while (*buf) {
2763 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2764 if (buf == end) break;
2765 if (*end == '-') { /* range */
2766 buf = end + 1;
2767 end_chunk = simple_strtoul(buf, &end, 0);
2768 if (buf == end) break;
2769 }
2770 if (*end && !isspace(*end)) break;
2771 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2772 buf = end;
2773 while (isspace(*buf)) buf++;
2774 }
2775 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2776out:
2777 return len;
2778}
2779
2780static struct md_sysfs_entry md_bitmap =
2781__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2782
2783static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002784size_show(mddev_t *mddev, char *page)
2785{
2786 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2787}
2788
2789static int update_size(mddev_t *mddev, unsigned long size);
2790
2791static ssize_t
2792size_store(mddev_t *mddev, const char *buf, size_t len)
2793{
2794 /* If array is inactive, we can reduce the component size, but
2795 * not increase it (except from 0).
2796 * If array is active, we can try an on-line resize
2797 */
2798 char *e;
2799 int err = 0;
2800 unsigned long long size = simple_strtoull(buf, &e, 10);
2801 if (!*buf || *buf == '\n' ||
2802 (*e && *e != '\n'))
2803 return -EINVAL;
2804
2805 if (mddev->pers) {
2806 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002807 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002808 } else {
2809 if (mddev->size == 0 ||
2810 mddev->size > size)
2811 mddev->size = size;
2812 else
2813 err = -ENOSPC;
2814 }
2815 return err ? err : len;
2816}
2817
2818static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002819__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002820
NeilBrown8bb93aa2006-01-06 00:20:50 -08002821
2822/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002823 * This is one of
2824 * 'none' for arrays with no metadata (good luck...)
2825 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002826 * or N.M for internally known formats
2827 */
2828static ssize_t
2829metadata_show(mddev_t *mddev, char *page)
2830{
2831 if (mddev->persistent)
2832 return sprintf(page, "%d.%d\n",
2833 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002834 else if (mddev->external)
2835 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002836 else
2837 return sprintf(page, "none\n");
2838}
2839
2840static ssize_t
2841metadata_store(mddev_t *mddev, const char *buf, size_t len)
2842{
2843 int major, minor;
2844 char *e;
2845 if (!list_empty(&mddev->disks))
2846 return -EBUSY;
2847
2848 if (cmd_match(buf, "none")) {
2849 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002850 mddev->external = 0;
2851 mddev->major_version = 0;
2852 mddev->minor_version = 90;
2853 return len;
2854 }
2855 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002856 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002857 if (namelen >= sizeof(mddev->metadata_type))
2858 namelen = sizeof(mddev->metadata_type)-1;
2859 strncpy(mddev->metadata_type, buf+9, namelen);
2860 mddev->metadata_type[namelen] = 0;
2861 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2862 mddev->metadata_type[--namelen] = 0;
2863 mddev->persistent = 0;
2864 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002865 mddev->major_version = 0;
2866 mddev->minor_version = 90;
2867 return len;
2868 }
2869 major = simple_strtoul(buf, &e, 10);
2870 if (e==buf || *e != '.')
2871 return -EINVAL;
2872 buf = e+1;
2873 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002874 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002875 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002876 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002877 return -ENOENT;
2878 mddev->major_version = major;
2879 mddev->minor_version = minor;
2880 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002881 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002882 return len;
2883}
2884
2885static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002886__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002887
NeilBrowna35b0d62006-01-06 00:20:49 -08002888static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002889action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002890{
NeilBrown7eec3142005-11-08 21:39:44 -08002891 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002892 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002893 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002894 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2895 type = "reshape";
2896 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002897 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2898 type = "resync";
2899 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2900 type = "check";
2901 else
2902 type = "repair";
2903 } else
2904 type = "recover";
2905 }
2906 return sprintf(page, "%s\n", type);
2907}
2908
2909static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002910action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002911{
NeilBrown7eec3142005-11-08 21:39:44 -08002912 if (!mddev->pers || !mddev->pers->sync_request)
2913 return -EINVAL;
2914
NeilBrownbce74da2006-01-06 00:20:41 -08002915 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002916 if (mddev->sync_thread) {
2917 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2918 md_unregister_thread(mddev->sync_thread);
2919 mddev->sync_thread = NULL;
2920 mddev->recovery = 0;
2921 }
NeilBrown03c902e2006-01-06 00:20:46 -08002922 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2923 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002924 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002925 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002926 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002927 else if (cmd_match(page, "reshape")) {
2928 int err;
2929 if (mddev->pers->start_reshape == NULL)
2930 return -EINVAL;
2931 err = mddev->pers->start_reshape(mddev);
2932 if (err)
2933 return err;
2934 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002935 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002936 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002937 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002938 return -EINVAL;
2939 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2940 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002941 }
NeilBrown03c902e2006-01-06 00:20:46 -08002942 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002943 md_wakeup_thread(mddev->thread);
2944 return len;
2945}
2946
NeilBrown9d888832005-11-08 21:39:26 -08002947static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002948mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002949{
2950 return sprintf(page, "%llu\n",
2951 (unsigned long long) mddev->resync_mismatches);
2952}
2953
NeilBrown80ca3a42006-07-10 04:44:18 -07002954static struct md_sysfs_entry md_scan_mode =
2955__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002956
NeilBrown96de1e62005-11-08 21:39:39 -08002957
NeilBrown80ca3a42006-07-10 04:44:18 -07002958static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002959
NeilBrown88202a02006-01-06 00:21:36 -08002960static ssize_t
2961sync_min_show(mddev_t *mddev, char *page)
2962{
2963 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2964 mddev->sync_speed_min ? "local": "system");
2965}
2966
2967static ssize_t
2968sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2969{
2970 int min;
2971 char *e;
2972 if (strncmp(buf, "system", 6)==0) {
2973 mddev->sync_speed_min = 0;
2974 return len;
2975 }
2976 min = simple_strtoul(buf, &e, 10);
2977 if (buf == e || (*e && *e != '\n') || min <= 0)
2978 return -EINVAL;
2979 mddev->sync_speed_min = min;
2980 return len;
2981}
2982
2983static struct md_sysfs_entry md_sync_min =
2984__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2985
2986static ssize_t
2987sync_max_show(mddev_t *mddev, char *page)
2988{
2989 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2990 mddev->sync_speed_max ? "local": "system");
2991}
2992
2993static ssize_t
2994sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2995{
2996 int max;
2997 char *e;
2998 if (strncmp(buf, "system", 6)==0) {
2999 mddev->sync_speed_max = 0;
3000 return len;
3001 }
3002 max = simple_strtoul(buf, &e, 10);
3003 if (buf == e || (*e && *e != '\n') || max <= 0)
3004 return -EINVAL;
3005 mddev->sync_speed_max = max;
3006 return len;
3007}
3008
3009static struct md_sysfs_entry md_sync_max =
3010__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3011
Iustin Popd7f3d292007-10-16 23:30:54 -07003012static ssize_t
3013degraded_show(mddev_t *mddev, char *page)
3014{
3015 return sprintf(page, "%d\n", mddev->degraded);
3016}
3017static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003018
3019static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003020sync_force_parallel_show(mddev_t *mddev, char *page)
3021{
3022 return sprintf(page, "%d\n", mddev->parallel_resync);
3023}
3024
3025static ssize_t
3026sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3027{
3028 long n;
3029
3030 if (strict_strtol(buf, 10, &n))
3031 return -EINVAL;
3032
3033 if (n != 0 && n != 1)
3034 return -EINVAL;
3035
3036 mddev->parallel_resync = n;
3037
3038 if (mddev->sync_thread)
3039 wake_up(&resync_wait);
3040
3041 return len;
3042}
3043
3044/* force parallel resync, even with shared block devices */
3045static struct md_sysfs_entry md_sync_force_parallel =
3046__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3047 sync_force_parallel_show, sync_force_parallel_store);
3048
3049static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003050sync_speed_show(mddev_t *mddev, char *page)
3051{
3052 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07003053 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08003054 dt = ((jiffies - mddev->resync_mark) / HZ);
3055 if (!dt) dt++;
3056 db = resync - (mddev->resync_mark_cnt);
3057 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
3058}
3059
NeilBrown80ca3a42006-07-10 04:44:18 -07003060static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003061
3062static ssize_t
3063sync_completed_show(mddev_t *mddev, char *page)
3064{
3065 unsigned long max_blocks, resync;
3066
3067 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3068 max_blocks = mddev->resync_max_sectors;
3069 else
3070 max_blocks = mddev->size << 1;
3071
3072 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3073 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3074}
3075
NeilBrown80ca3a42006-07-10 04:44:18 -07003076static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003077
NeilBrowne464eaf2006-03-27 01:18:14 -08003078static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003079min_sync_show(mddev_t *mddev, char *page)
3080{
3081 return sprintf(page, "%llu\n",
3082 (unsigned long long)mddev->resync_min);
3083}
3084static ssize_t
3085min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3086{
3087 unsigned long long min;
3088 if (strict_strtoull(buf, 10, &min))
3089 return -EINVAL;
3090 if (min > mddev->resync_max)
3091 return -EINVAL;
3092 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3093 return -EBUSY;
3094
3095 /* Must be a multiple of chunk_size */
3096 if (mddev->chunk_size) {
3097 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3098 return -EINVAL;
3099 }
3100 mddev->resync_min = min;
3101
3102 return len;
3103}
3104
3105static struct md_sysfs_entry md_min_sync =
3106__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3107
3108static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003109max_sync_show(mddev_t *mddev, char *page)
3110{
3111 if (mddev->resync_max == MaxSector)
3112 return sprintf(page, "max\n");
3113 else
3114 return sprintf(page, "%llu\n",
3115 (unsigned long long)mddev->resync_max);
3116}
3117static ssize_t
3118max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3119{
3120 if (strncmp(buf, "max", 3) == 0)
3121 mddev->resync_max = MaxSector;
3122 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003123 unsigned long long max;
3124 if (strict_strtoull(buf, 10, &max))
3125 return -EINVAL;
3126 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003127 return -EINVAL;
3128 if (max < mddev->resync_max &&
3129 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3130 return -EBUSY;
3131
3132 /* Must be a multiple of chunk_size */
3133 if (mddev->chunk_size) {
3134 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3135 return -EINVAL;
3136 }
3137 mddev->resync_max = max;
3138 }
3139 wake_up(&mddev->recovery_wait);
3140 return len;
3141}
3142
3143static struct md_sysfs_entry md_max_sync =
3144__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3145
3146static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003147suspend_lo_show(mddev_t *mddev, char *page)
3148{
3149 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3150}
3151
3152static ssize_t
3153suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3154{
3155 char *e;
3156 unsigned long long new = simple_strtoull(buf, &e, 10);
3157
3158 if (mddev->pers->quiesce == NULL)
3159 return -EINVAL;
3160 if (buf == e || (*e && *e != '\n'))
3161 return -EINVAL;
3162 if (new >= mddev->suspend_hi ||
3163 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3164 mddev->suspend_lo = new;
3165 mddev->pers->quiesce(mddev, 2);
3166 return len;
3167 } else
3168 return -EINVAL;
3169}
3170static struct md_sysfs_entry md_suspend_lo =
3171__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3172
3173
3174static ssize_t
3175suspend_hi_show(mddev_t *mddev, char *page)
3176{
3177 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3178}
3179
3180static ssize_t
3181suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3182{
3183 char *e;
3184 unsigned long long new = simple_strtoull(buf, &e, 10);
3185
3186 if (mddev->pers->quiesce == NULL)
3187 return -EINVAL;
3188 if (buf == e || (*e && *e != '\n'))
3189 return -EINVAL;
3190 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3191 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3192 mddev->suspend_hi = new;
3193 mddev->pers->quiesce(mddev, 1);
3194 mddev->pers->quiesce(mddev, 0);
3195 return len;
3196 } else
3197 return -EINVAL;
3198}
3199static struct md_sysfs_entry md_suspend_hi =
3200__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3201
NeilBrown08a02ec2007-05-09 02:35:38 -07003202static ssize_t
3203reshape_position_show(mddev_t *mddev, char *page)
3204{
3205 if (mddev->reshape_position != MaxSector)
3206 return sprintf(page, "%llu\n",
3207 (unsigned long long)mddev->reshape_position);
3208 strcpy(page, "none\n");
3209 return 5;
3210}
3211
3212static ssize_t
3213reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3214{
3215 char *e;
3216 unsigned long long new = simple_strtoull(buf, &e, 10);
3217 if (mddev->pers)
3218 return -EBUSY;
3219 if (buf == e || (*e && *e != '\n'))
3220 return -EINVAL;
3221 mddev->reshape_position = new;
3222 mddev->delta_disks = 0;
3223 mddev->new_level = mddev->level;
3224 mddev->new_layout = mddev->layout;
3225 mddev->new_chunk = mddev->chunk_size;
3226 return len;
3227}
3228
3229static struct md_sysfs_entry md_reshape_position =
3230__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3231 reshape_position_store);
3232
NeilBrowne464eaf2006-03-27 01:18:14 -08003233
NeilBrowneae17012005-11-08 21:39:23 -08003234static struct attribute *md_default_attrs[] = {
3235 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003236 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003237 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003238 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003239 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003240 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003241 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003242 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003243 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003244 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003245 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003246 NULL,
3247};
3248
3249static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003250 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003251 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003252 &md_sync_min.attr,
3253 &md_sync_max.attr,
3254 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003255 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003256 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003257 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003258 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003259 &md_suspend_lo.attr,
3260 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003261 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003262 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003263 NULL,
3264};
NeilBrown411036f2005-11-08 21:39:40 -08003265static struct attribute_group md_redundancy_group = {
3266 .name = NULL,
3267 .attrs = md_redundancy_attrs,
3268};
3269
NeilBrowneae17012005-11-08 21:39:23 -08003270
3271static ssize_t
3272md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3273{
3274 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3275 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003276 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003277
3278 if (!entry->show)
3279 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003280 rv = mddev_lock(mddev);
3281 if (!rv) {
3282 rv = entry->show(mddev, page);
3283 mddev_unlock(mddev);
3284 }
NeilBrown96de1e62005-11-08 21:39:39 -08003285 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003286}
3287
3288static ssize_t
3289md_attr_store(struct kobject *kobj, struct attribute *attr,
3290 const char *page, size_t length)
3291{
3292 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3293 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003294 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003295
3296 if (!entry->store)
3297 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003298 if (!capable(CAP_SYS_ADMIN))
3299 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003300 rv = mddev_lock(mddev);
3301 if (!rv) {
3302 rv = entry->store(mddev, page, length);
3303 mddev_unlock(mddev);
3304 }
NeilBrown96de1e62005-11-08 21:39:39 -08003305 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003306}
3307
3308static void md_free(struct kobject *ko)
3309{
3310 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3311 kfree(mddev);
3312}
3313
3314static struct sysfs_ops md_sysfs_ops = {
3315 .show = md_attr_show,
3316 .store = md_attr_store,
3317};
3318static struct kobj_type md_ktype = {
3319 .release = md_free,
3320 .sysfs_ops = &md_sysfs_ops,
3321 .default_attrs = md_default_attrs,
3322};
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324int mdp_major = 0;
3325
3326static struct kobject *md_probe(dev_t dev, int *part, void *data)
3327{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003328 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 mddev_t *mddev = mddev_find(dev);
3330 struct gendisk *disk;
3331 int partitioned = (MAJOR(dev) != MD_MAJOR);
3332 int shift = partitioned ? MdpMinorShift : 0;
3333 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003334 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335
3336 if (!mddev)
3337 return NULL;
3338
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003339 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003341 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 mddev_put(mddev);
3343 return NULL;
3344 }
3345 disk = alloc_disk(1 << shift);
3346 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003347 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 mddev_put(mddev);
3349 return NULL;
3350 }
3351 disk->major = MAJOR(dev);
3352 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003353 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003355 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 disk->fops = &md_fops;
3358 disk->private_data = mddev;
3359 disk->queue = mddev->queue;
3360 add_disk(disk);
3361 mddev->gendisk = disk;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003362 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003363 "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003364 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003365 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003366 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3367 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003368 else
3369 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 return NULL;
3371}
3372
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373static void md_safemode_timeout(unsigned long data)
3374{
3375 mddev_t *mddev = (mddev_t *) data;
3376
3377 mddev->safemode = 1;
3378 md_wakeup_thread(mddev->thread);
3379}
3380
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003381static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
3383static int do_md_run(mddev_t * mddev)
3384{
NeilBrown2604b702006-01-06 00:20:36 -08003385 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 int chunk_size;
3387 struct list_head *tmp;
3388 mdk_rdev_t *rdev;
3389 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003390 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 char b[BDEVNAME_SIZE];
3392
NeilBrowna757e642005-04-16 15:26:42 -07003393 if (list_empty(&mddev->disks))
3394 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 if (mddev->pers)
3398 return -EBUSY;
3399
3400 /*
3401 * Analyze all RAID superblock(s)
3402 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003403 if (!mddev->raid_disks) {
3404 if (!mddev->persistent)
3405 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003406 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
NeilBrown2604b702006-01-06 00:20:36 -08003411 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if (chunk_size > MAX_CHUNK_SIZE) {
3413 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3414 chunk_size, MAX_CHUNK_SIZE);
3415 return -EINVAL;
3416 }
3417 /*
3418 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3419 */
3420 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003421 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return -EINVAL;
3423 }
3424 if (chunk_size < PAGE_SIZE) {
3425 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3426 chunk_size, PAGE_SIZE);
3427 return -EINVAL;
3428 }
3429
3430 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003431 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003432 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 continue;
3434 if (rdev->size < chunk_size / 1024) {
3435 printk(KERN_WARNING
3436 "md: Dev %s smaller than chunk_size:"
3437 " %lluk < %dk\n",
3438 bdevname(rdev->bdev,b),
3439 (unsigned long long)rdev->size,
3440 chunk_size / 1024);
3441 return -EINVAL;
3442 }
3443 }
3444 }
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003447 if (mddev->level != LEVEL_NONE)
3448 request_module("md-level-%d", mddev->level);
3449 else if (mddev->clevel[0])
3450 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451#endif
3452
3453 /*
3454 * Drop all container device buffers, from now on
3455 * the only valid external interface is through the md
3456 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003458 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003459 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 continue;
3461 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003462 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003463
3464 /* perform some consistency tests on the device.
3465 * We don't want the data to overlap the metadata,
3466 * Internal Bitmap issues has handled elsewhere.
3467 */
3468 if (rdev->data_offset < rdev->sb_offset) {
3469 if (mddev->size &&
3470 rdev->data_offset + mddev->size*2
3471 > rdev->sb_offset*2) {
3472 printk("md: %s: data overlaps metadata\n",
3473 mdname(mddev));
3474 return -EINVAL;
3475 }
3476 } else {
3477 if (rdev->sb_offset*2 + rdev->sb_size/512
3478 > rdev->data_offset) {
3479 printk("md: %s: metadata overlaps data\n",
3480 mdname(mddev));
3481 return -EINVAL;
3482 }
3483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
3485
3486 md_probe(mddev->unit, NULL, NULL);
3487 disk = mddev->gendisk;
3488 if (!disk)
3489 return -ENOMEM;
3490
3491 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003492 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003493 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003495 if (mddev->level != LEVEL_NONE)
3496 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3497 mddev->level);
3498 else
3499 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3500 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 return -EINVAL;
3502 }
NeilBrown2604b702006-01-06 00:20:36 -08003503 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003505 mddev->level = pers->level;
3506 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
NeilBrownf6705572006-03-27 01:18:11 -08003508 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003509 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003510 /* This personality cannot handle reshaping... */
3511 mddev->pers = NULL;
3512 module_put(pers->owner);
3513 return -EINVAL;
3514 }
3515
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003516 if (pers->sync_request) {
3517 /* Warn if this is a potentially silly
3518 * configuration.
3519 */
3520 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3521 mdk_rdev_t *rdev2;
3522 struct list_head *tmp2;
3523 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003524 rdev_for_each(rdev, tmp, mddev) {
3525 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003526 if (rdev < rdev2 &&
3527 rdev->bdev->bd_contains ==
3528 rdev2->bdev->bd_contains) {
3529 printk(KERN_WARNING
3530 "%s: WARNING: %s appears to be"
3531 " on the same physical disk as"
3532 " %s.\n",
3533 mdname(mddev),
3534 bdevname(rdev->bdev,b),
3535 bdevname(rdev2->bdev,b2));
3536 warned = 1;
3537 }
3538 }
3539 }
3540 if (warned)
3541 printk(KERN_WARNING
3542 "True protection against single-disk"
3543 " failure might be compromised.\n");
3544 }
3545
NeilBrown657390d2005-08-26 18:34:16 -07003546 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003548 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003549 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
NeilBrownf91de922005-11-08 21:39:36 -08003551 if (start_readonly)
3552 mddev->ro = 2; /* read-only, but switch on first write */
3553
NeilBrownb15c2e52006-01-06 00:20:16 -08003554 err = mddev->pers->run(mddev);
3555 if (!err && mddev->pers->sync_request) {
3556 err = bitmap_create(mddev);
3557 if (err) {
3558 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3559 mdname(mddev), err);
3560 mddev->pers->stop(mddev);
3561 }
3562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 if (err) {
3564 printk(KERN_ERR "md: pers->run() failed ...\n");
3565 module_put(mddev->pers->owner);
3566 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003567 bitmap_destroy(mddev);
3568 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003570 if (mddev->pers->sync_request) {
3571 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3572 printk(KERN_WARNING
3573 "md: cannot register extra attributes for %s\n",
3574 mdname(mddev));
3575 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003576 mddev->ro = 0;
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 atomic_set(&mddev->writes_pending,0);
3579 mddev->safemode = 0;
3580 mddev->safemode_timer.function = md_safemode_timeout;
3581 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003582 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003584
NeilBrownd089c6a2008-02-06 01:39:59 -08003585 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003586 if (rdev->raid_disk >= 0) {
3587 char nm[20];
3588 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003589 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3590 printk("md: cannot register %s for %s\n",
3591 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
3594 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3595
NeilBrown850b2b42006-10-03 01:15:46 -07003596 if (mddev->flags)
3597 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599 set_capacity(disk, mddev->array_size<<1);
3600
3601 /* If we call blk_queue_make_request here, it will
3602 * re-initialise max_sectors etc which may have been
3603 * refined inside -> run. So just set the bits we need to set.
3604 * Most initialisation happended when we called
3605 * blk_queue_make_request(..., md_fail_request)
3606 * earlier.
3607 */
3608 mddev->queue->queuedata = mddev;
3609 mddev->queue->make_request_fn = mddev->pers->make_request;
3610
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003611 /* If there is a partially-recovered drive we need to
3612 * start recovery here. If we leave it to md_check_recovery,
3613 * it will remove the drives and not do the right thing
3614 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003615 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003616 struct list_head *rtmp;
3617 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003618 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003619 if (rdev->raid_disk >= 0 &&
3620 !test_bit(In_sync, &rdev->flags) &&
3621 !test_bit(Faulty, &rdev->flags))
3622 /* complete an interrupted recovery */
3623 spares++;
3624 if (spares && mddev->pers->sync_request) {
3625 mddev->recovery = 0;
3626 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3627 mddev->sync_thread = md_register_thread(md_do_sync,
3628 mddev,
3629 "%s_resync");
3630 if (!mddev->sync_thread) {
3631 printk(KERN_ERR "%s: could not start resync"
3632 " thread...\n",
3633 mdname(mddev));
3634 /* leave the spares where they are, it shouldn't hurt */
3635 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003636 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003637 }
3638 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003639 md_wakeup_thread(mddev->thread);
3640 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003641
Linus Torvalds44ce62942007-05-09 18:51:36 -07003642 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003643 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003644 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return 0;
3646}
3647
3648static int restart_array(mddev_t *mddev)
3649{
3650 struct gendisk *disk = mddev->gendisk;
3651 int err;
3652
3653 /*
3654 * Complain if it has no devices
3655 */
3656 err = -ENXIO;
3657 if (list_empty(&mddev->disks))
3658 goto out;
3659
3660 if (mddev->pers) {
3661 err = -EBUSY;
3662 if (!mddev->ro)
3663 goto out;
3664
3665 mddev->safemode = 0;
3666 mddev->ro = 0;
3667 set_disk_ro(disk, 0);
3668
3669 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3670 mdname(mddev));
3671 /*
3672 * Kick recovery or resync if necessary
3673 */
3674 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3675 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003676 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003678 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
3681out:
3682 return err;
3683}
3684
NeilBrownacc55e22006-06-26 00:27:47 -07003685/* similar to deny_write_access, but accounts for our holding a reference
3686 * to the file ourselves */
3687static int deny_bitmap_write_access(struct file * file)
3688{
3689 struct inode *inode = file->f_mapping->host;
3690
3691 spin_lock(&inode->i_lock);
3692 if (atomic_read(&inode->i_writecount) > 1) {
3693 spin_unlock(&inode->i_lock);
3694 return -ETXTBSY;
3695 }
3696 atomic_set(&inode->i_writecount, -1);
3697 spin_unlock(&inode->i_lock);
3698
3699 return 0;
3700}
3701
3702static void restore_bitmap_write_access(struct file *file)
3703{
3704 struct inode *inode = file->f_mapping->host;
3705
3706 spin_lock(&inode->i_lock);
3707 atomic_set(&inode->i_writecount, 1);
3708 spin_unlock(&inode->i_lock);
3709}
3710
NeilBrown9e653b62006-06-26 00:27:58 -07003711/* mode:
3712 * 0 - completely stop and dis-assemble array
3713 * 1 - switch to readonly
3714 * 2 - stop but do not disassemble array
3715 */
3716static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
3718 int err = 0;
3719 struct gendisk *disk = mddev->gendisk;
3720
3721 if (mddev->pers) {
3722 if (atomic_read(&mddev->active)>2) {
3723 printk("md: %s still in use.\n",mdname(mddev));
3724 return -EBUSY;
3725 }
3726
3727 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003728 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3730 md_unregister_thread(mddev->sync_thread);
3731 mddev->sync_thread = NULL;
3732 }
3733
3734 del_timer_sync(&mddev->safemode_timer);
3735
3736 invalidate_partition(disk, 0);
3737
NeilBrown9e653b62006-06-26 00:27:58 -07003738 switch(mode) {
3739 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003741 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 goto out;
3743 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003744 break;
3745 case 0: /* disassemble */
3746 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003747 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003748 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (mddev->ro)
3750 set_disk_ro(disk, 0);
3751 blk_queue_make_request(mddev->queue, md_fail_request);
3752 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003753 mddev->queue->merge_bvec_fn = NULL;
3754 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003755 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003756 if (mddev->pers->sync_request)
3757 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 module_put(mddev->pers->owner);
3760 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003761 /* tell userspace to handle 'inactive' */
3762 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003763
3764 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003765 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 if (mddev->ro)
3768 mddev->ro = 0;
3769 }
NeilBrown850b2b42006-10-03 01:15:46 -07003770 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 /* mark array as shutdown cleanly */
3772 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003773 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 }
NeilBrown9e653b62006-06-26 00:27:58 -07003775 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003777 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
NeilBrown32a76272005-06-21 17:17:14 -07003779
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 /*
3781 * Free resources if final stop
3782 */
NeilBrown9e653b62006-06-26 00:27:58 -07003783 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003784 mdk_rdev_t *rdev;
3785 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3788
NeilBrown978f9462006-02-02 14:28:05 -08003789 bitmap_destroy(mddev);
3790 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003791 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003792 fput(mddev->bitmap_file);
3793 mddev->bitmap_file = NULL;
3794 }
3795 mddev->bitmap_offset = 0;
3796
NeilBrownd089c6a2008-02-06 01:39:59 -08003797 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003798 if (rdev->raid_disk >= 0) {
3799 char nm[20];
3800 sprintf(nm, "rd%d", rdev->raid_disk);
3801 sysfs_remove_link(&mddev->kobj, nm);
3802 }
3803
NeilBrown177a99b2008-02-06 01:39:56 -08003804 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003805 flush_scheduled_work();
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 export_array(mddev);
3808
3809 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003810 mddev->size = 0;
3811 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003812 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003813 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003814 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003815 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003816 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003817 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003818 mddev->level = LEVEL_NONE;
3819 mddev->clevel[0] = 0;
3820 mddev->flags = 0;
3821 mddev->ro = 0;
3822 mddev->metadata_type[0] = 0;
3823 mddev->chunk_size = 0;
3824 mddev->ctime = mddev->utime = 0;
3825 mddev->layout = 0;
3826 mddev->max_disks = 0;
3827 mddev->events = 0;
3828 mddev->delta_disks = 0;
3829 mddev->new_level = LEVEL_NONE;
3830 mddev->new_layout = 0;
3831 mddev->new_chunk = 0;
3832 mddev->curr_resync = 0;
3833 mddev->resync_mismatches = 0;
3834 mddev->suspend_lo = mddev->suspend_hi = 0;
3835 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3836 mddev->recovery = 0;
3837 mddev->in_sync = 0;
3838 mddev->changed = 0;
3839 mddev->degraded = 0;
3840 mddev->barriers_work = 0;
3841 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003842
NeilBrowna8a55c32006-06-26 00:27:39 -07003843 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3845 mdname(mddev));
3846 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003847 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848out:
3849 return err;
3850}
3851
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003852#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853static void autorun_array(mddev_t *mddev)
3854{
3855 mdk_rdev_t *rdev;
3856 struct list_head *tmp;
3857 int err;
3858
NeilBrowna757e642005-04-16 15:26:42 -07003859 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 printk(KERN_INFO "md: running: ");
3863
NeilBrownd089c6a2008-02-06 01:39:59 -08003864 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 char b[BDEVNAME_SIZE];
3866 printk("<%s>", bdevname(rdev->bdev,b));
3867 }
3868 printk("\n");
3869
3870 err = do_md_run (mddev);
3871 if (err) {
3872 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3873 do_md_stop (mddev, 0);
3874 }
3875}
3876
3877/*
3878 * lets try to run arrays based on all disks that have arrived
3879 * until now. (those are in pending_raid_disks)
3880 *
3881 * the method: pick the first pending disk, collect all disks with
3882 * the same UUID, remove all from the pending list and put them into
3883 * the 'same_array' list. Then order this list based on superblock
3884 * update time (freshest comes first), kick out 'old' disks and
3885 * compare superblocks. If everything's fine then run it.
3886 *
3887 * If "unit" is allocated, then bump its reference count
3888 */
3889static void autorun_devices(int part)
3890{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 struct list_head *tmp;
3892 mdk_rdev_t *rdev0, *rdev;
3893 mddev_t *mddev;
3894 char b[BDEVNAME_SIZE];
3895
3896 printk(KERN_INFO "md: autorun ...\n");
3897 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003898 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003900 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 rdev0 = list_entry(pending_raid_disks.next,
3902 mdk_rdev_t, same_set);
3903
3904 printk(KERN_INFO "md: considering %s ...\n",
3905 bdevname(rdev0->bdev,b));
3906 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003907 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 if (super_90_load(rdev, rdev0, 0) >= 0) {
3909 printk(KERN_INFO "md: adding %s ...\n",
3910 bdevname(rdev->bdev,b));
3911 list_move(&rdev->same_set, &candidates);
3912 }
3913 /*
3914 * now we have a set of devices, with all of them having
3915 * mostly sane superblocks. It's time to allocate the
3916 * mddev.
3917 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003918 if (part) {
3919 dev = MKDEV(mdp_major,
3920 rdev0->preferred_minor << MdpMinorShift);
3921 unit = MINOR(dev) >> MdpMinorShift;
3922 } else {
3923 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3924 unit = MINOR(dev);
3925 }
3926 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3928 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3929 break;
3930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
3932 md_probe(dev, NULL, NULL);
3933 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10003934 if (!mddev || !mddev->gendisk) {
3935 if (mddev)
3936 mddev_put(mddev);
3937 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 "md: cannot allocate memory for md drive.\n");
3939 break;
3940 }
3941 if (mddev_lock(mddev))
3942 printk(KERN_WARNING "md: %s locked, cannot run\n",
3943 mdname(mddev));
3944 else if (mddev->raid_disks || mddev->major_version
3945 || !list_empty(&mddev->disks)) {
3946 printk(KERN_WARNING
3947 "md: %s already running, cannot run %s\n",
3948 mdname(mddev), bdevname(rdev0->bdev,b));
3949 mddev_unlock(mddev);
3950 } else {
3951 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08003952 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08003953 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 list_del_init(&rdev->same_set);
3955 if (bind_rdev_to_array(rdev, mddev))
3956 export_rdev(rdev);
3957 }
3958 autorun_array(mddev);
3959 mddev_unlock(mddev);
3960 }
3961 /* on success, candidates will be empty, on error
3962 * it won't...
3963 */
NeilBrown73c34432008-02-06 01:39:59 -08003964 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 export_rdev(rdev);
3966 mddev_put(mddev);
3967 }
3968 printk(KERN_INFO "md: ... autorun DONE.\n");
3969}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003970#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972static int get_version(void __user * arg)
3973{
3974 mdu_version_t ver;
3975
3976 ver.major = MD_MAJOR_VERSION;
3977 ver.minor = MD_MINOR_VERSION;
3978 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3979
3980 if (copy_to_user(arg, &ver, sizeof(ver)))
3981 return -EFAULT;
3982
3983 return 0;
3984}
3985
3986static int get_array_info(mddev_t * mddev, void __user * arg)
3987{
3988 mdu_array_info_t info;
3989 int nr,working,active,failed,spare;
3990 mdk_rdev_t *rdev;
3991 struct list_head *tmp;
3992
3993 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003994 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003996 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 failed++;
3998 else {
3999 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004000 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 active++;
4002 else
4003 spare++;
4004 }
4005 }
4006
4007 info.major_version = mddev->major_version;
4008 info.minor_version = mddev->minor_version;
4009 info.patch_version = MD_PATCHLEVEL_VERSION;
4010 info.ctime = mddev->ctime;
4011 info.level = mddev->level;
4012 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004013 if (info.size != mddev->size) /* overflow */
4014 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 info.nr_disks = nr;
4016 info.raid_disks = mddev->raid_disks;
4017 info.md_minor = mddev->md_minor;
4018 info.not_persistent= !mddev->persistent;
4019
4020 info.utime = mddev->utime;
4021 info.state = 0;
4022 if (mddev->in_sync)
4023 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004024 if (mddev->bitmap && mddev->bitmap_offset)
4025 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 info.active_disks = active;
4027 info.working_disks = working;
4028 info.failed_disks = failed;
4029 info.spare_disks = spare;
4030
4031 info.layout = mddev->layout;
4032 info.chunk_size = mddev->chunk_size;
4033
4034 if (copy_to_user(arg, &info, sizeof(info)))
4035 return -EFAULT;
4036
4037 return 0;
4038}
4039
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004040static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004041{
4042 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4043 char *ptr, *buf = NULL;
4044 int err = -ENOMEM;
4045
NeilBrown2a2275d2007-01-26 00:57:11 -08004046 md_allow_write(mddev);
4047
NeilBrown32a76272005-06-21 17:17:14 -07004048 file = kmalloc(sizeof(*file), GFP_KERNEL);
4049 if (!file)
4050 goto out;
4051
4052 /* bitmap disabled, zero the first byte and copy out */
4053 if (!mddev->bitmap || !mddev->bitmap->file) {
4054 file->pathname[0] = '\0';
4055 goto copy_out;
4056 }
4057
4058 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4059 if (!buf)
4060 goto out;
4061
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004062 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4063 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004064 goto out;
4065
4066 strcpy(file->pathname, ptr);
4067
4068copy_out:
4069 err = 0;
4070 if (copy_to_user(arg, file, sizeof(*file)))
4071 err = -EFAULT;
4072out:
4073 kfree(buf);
4074 kfree(file);
4075 return err;
4076}
4077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078static int get_disk_info(mddev_t * mddev, void __user * arg)
4079{
4080 mdu_disk_info_t info;
4081 unsigned int nr;
4082 mdk_rdev_t *rdev;
4083
4084 if (copy_from_user(&info, arg, sizeof(info)))
4085 return -EFAULT;
4086
4087 nr = info.number;
4088
4089 rdev = find_rdev_nr(mddev, nr);
4090 if (rdev) {
4091 info.major = MAJOR(rdev->bdev->bd_dev);
4092 info.minor = MINOR(rdev->bdev->bd_dev);
4093 info.raid_disk = rdev->raid_disk;
4094 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004095 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004097 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 info.state |= (1<<MD_DISK_ACTIVE);
4099 info.state |= (1<<MD_DISK_SYNC);
4100 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004101 if (test_bit(WriteMostly, &rdev->flags))
4102 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 } else {
4104 info.major = info.minor = 0;
4105 info.raid_disk = -1;
4106 info.state = (1<<MD_DISK_REMOVED);
4107 }
4108
4109 if (copy_to_user(arg, &info, sizeof(info)))
4110 return -EFAULT;
4111
4112 return 0;
4113}
4114
4115static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4116{
4117 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4118 mdk_rdev_t *rdev;
4119 dev_t dev = MKDEV(info->major,info->minor);
4120
4121 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4122 return -EOVERFLOW;
4123
4124 if (!mddev->raid_disks) {
4125 int err;
4126 /* expecting a device which has a superblock */
4127 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4128 if (IS_ERR(rdev)) {
4129 printk(KERN_WARNING
4130 "md: md_import_device returned %ld\n",
4131 PTR_ERR(rdev));
4132 return PTR_ERR(rdev);
4133 }
4134 if (!list_empty(&mddev->disks)) {
4135 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4136 mdk_rdev_t, same_set);
4137 int err = super_types[mddev->major_version]
4138 .load_super(rdev, rdev0, mddev->minor_version);
4139 if (err < 0) {
4140 printk(KERN_WARNING
4141 "md: %s has different UUID to %s\n",
4142 bdevname(rdev->bdev,b),
4143 bdevname(rdev0->bdev,b2));
4144 export_rdev(rdev);
4145 return -EINVAL;
4146 }
4147 }
4148 err = bind_rdev_to_array(rdev, mddev);
4149 if (err)
4150 export_rdev(rdev);
4151 return err;
4152 }
4153
4154 /*
4155 * add_new_disk can be used once the array is assembled
4156 * to add "hot spares". They must already have a superblock
4157 * written
4158 */
4159 if (mddev->pers) {
4160 int err;
4161 if (!mddev->pers->hot_add_disk) {
4162 printk(KERN_WARNING
4163 "%s: personality does not support diskops!\n",
4164 mdname(mddev));
4165 return -EINVAL;
4166 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004167 if (mddev->persistent)
4168 rdev = md_import_device(dev, mddev->major_version,
4169 mddev->minor_version);
4170 else
4171 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 if (IS_ERR(rdev)) {
4173 printk(KERN_WARNING
4174 "md: md_import_device returned %ld\n",
4175 PTR_ERR(rdev));
4176 return PTR_ERR(rdev);
4177 }
NeilBrown41158c72005-06-21 17:17:25 -07004178 /* set save_raid_disk if appropriate */
4179 if (!mddev->persistent) {
4180 if (info->state & (1<<MD_DISK_SYNC) &&
4181 info->raid_disk < mddev->raid_disks)
4182 rdev->raid_disk = info->raid_disk;
4183 else
4184 rdev->raid_disk = -1;
4185 } else
4186 super_types[mddev->major_version].
4187 validate_super(mddev, rdev);
4188 rdev->saved_raid_disk = rdev->raid_disk;
4189
NeilBrownb2d444d2005-11-08 21:39:31 -08004190 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004191 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4192 set_bit(WriteMostly, &rdev->flags);
4193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 rdev->raid_disk = -1;
4195 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004196 if (!err && !mddev->pers->hot_remove_disk) {
4197 /* If there is hot_add_disk but no hot_remove_disk
4198 * then added disks for geometry changes,
4199 * and should be added immediately.
4200 */
4201 super_types[mddev->major_version].
4202 validate_super(mddev, rdev);
4203 err = mddev->pers->hot_add_disk(mddev, rdev);
4204 if (err)
4205 unbind_rdev_from_array(rdev);
4206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 if (err)
4208 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004209
NeilBrown17571282006-12-10 02:20:52 -08004210 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07004211 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004212 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 return err;
4214 }
4215
4216 /* otherwise, add_new_disk is only allowed
4217 * for major_version==0 superblocks
4218 */
4219 if (mddev->major_version != 0) {
4220 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4221 mdname(mddev));
4222 return -EINVAL;
4223 }
4224
4225 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4226 int err;
4227 rdev = md_import_device (dev, -1, 0);
4228 if (IS_ERR(rdev)) {
4229 printk(KERN_WARNING
4230 "md: error, md_import_device() returned %ld\n",
4231 PTR_ERR(rdev));
4232 return PTR_ERR(rdev);
4233 }
4234 rdev->desc_nr = info->number;
4235 if (info->raid_disk < mddev->raid_disks)
4236 rdev->raid_disk = info->raid_disk;
4237 else
4238 rdev->raid_disk = -1;
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004241 if (info->state & (1<<MD_DISK_SYNC))
4242 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004244 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4245 set_bit(WriteMostly, &rdev->flags);
4246
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 if (!mddev->persistent) {
4248 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4249 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4250 } else
4251 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4252 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4253
NeilBrown2bf071b2006-01-06 00:20:55 -08004254 err = bind_rdev_to_array(rdev, mddev);
4255 if (err) {
4256 export_rdev(rdev);
4257 return err;
4258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 }
4260
4261 return 0;
4262}
4263
4264static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4265{
4266 char b[BDEVNAME_SIZE];
4267 mdk_rdev_t *rdev;
4268
4269 if (!mddev->pers)
4270 return -ENODEV;
4271
4272 rdev = find_rdev(mddev, dev);
4273 if (!rdev)
4274 return -ENXIO;
4275
4276 if (rdev->raid_disk >= 0)
4277 goto busy;
4278
4279 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004280 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004281 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283 return 0;
4284busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004285 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 bdevname(rdev->bdev,b), mdname(mddev));
4287 return -EBUSY;
4288}
4289
4290static int hot_add_disk(mddev_t * mddev, dev_t dev)
4291{
4292 char b[BDEVNAME_SIZE];
4293 int err;
4294 unsigned int size;
4295 mdk_rdev_t *rdev;
4296
4297 if (!mddev->pers)
4298 return -ENODEV;
4299
4300 if (mddev->major_version != 0) {
4301 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4302 " version-0 superblocks.\n",
4303 mdname(mddev));
4304 return -EINVAL;
4305 }
4306 if (!mddev->pers->hot_add_disk) {
4307 printk(KERN_WARNING
4308 "%s: personality does not support diskops!\n",
4309 mdname(mddev));
4310 return -EINVAL;
4311 }
4312
4313 rdev = md_import_device (dev, -1, 0);
4314 if (IS_ERR(rdev)) {
4315 printk(KERN_WARNING
4316 "md: error, md_import_device() returned %ld\n",
4317 PTR_ERR(rdev));
4318 return -EINVAL;
4319 }
4320
4321 if (mddev->persistent)
4322 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4323 else
4324 rdev->sb_offset =
4325 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4326
4327 size = calc_dev_size(rdev, mddev->chunk_size);
4328 rdev->size = size;
4329
NeilBrownb2d444d2005-11-08 21:39:31 -08004330 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 printk(KERN_WARNING
4332 "md: can not hot-add faulty %s disk to %s!\n",
4333 bdevname(rdev->bdev,b), mdname(mddev));
4334 err = -EINVAL;
4335 goto abort_export;
4336 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004337 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004339 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004340 err = bind_rdev_to_array(rdev, mddev);
4341 if (err)
4342 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
4344 /*
4345 * The rest should better be atomic, we can have disk failures
4346 * noticed in interrupt contexts ...
4347 */
4348
4349 if (rdev->desc_nr == mddev->max_disks) {
4350 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4351 mdname(mddev));
4352 err = -EBUSY;
4353 goto abort_unbind_export;
4354 }
4355
4356 rdev->raid_disk = -1;
4357
NeilBrown850b2b42006-10-03 01:15:46 -07004358 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
4360 /*
4361 * Kick recovery, maybe this spare has to be added to the
4362 * array immediately.
4363 */
4364 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4365 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004366 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 return 0;
4368
4369abort_unbind_export:
4370 unbind_rdev_from_array(rdev);
4371
4372abort_export:
4373 export_rdev(rdev);
4374 return err;
4375}
4376
NeilBrown32a76272005-06-21 17:17:14 -07004377static int set_bitmap_file(mddev_t *mddev, int fd)
4378{
4379 int err;
4380
NeilBrown36fa3062005-09-09 16:23:45 -07004381 if (mddev->pers) {
4382 if (!mddev->pers->quiesce)
4383 return -EBUSY;
4384 if (mddev->recovery || mddev->sync_thread)
4385 return -EBUSY;
4386 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004387 }
4388
NeilBrown36fa3062005-09-09 16:23:45 -07004389
4390 if (fd >= 0) {
4391 if (mddev->bitmap)
4392 return -EEXIST; /* cannot add when bitmap is present */
4393 mddev->bitmap_file = fget(fd);
4394
4395 if (mddev->bitmap_file == NULL) {
4396 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4397 mdname(mddev));
4398 return -EBADF;
4399 }
4400
4401 err = deny_bitmap_write_access(mddev->bitmap_file);
4402 if (err) {
4403 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4404 mdname(mddev));
4405 fput(mddev->bitmap_file);
4406 mddev->bitmap_file = NULL;
4407 return err;
4408 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004409 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004410 } else if (mddev->bitmap == NULL)
4411 return -ENOENT; /* cannot remove what isn't there */
4412 err = 0;
4413 if (mddev->pers) {
4414 mddev->pers->quiesce(mddev, 1);
4415 if (fd >= 0)
4416 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004417 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004418 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004419 fd = -1; /* make sure to put the file */
4420 }
NeilBrown36fa3062005-09-09 16:23:45 -07004421 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004422 }
4423 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004424 if (mddev->bitmap_file) {
4425 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004426 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004427 }
NeilBrown36fa3062005-09-09 16:23:45 -07004428 mddev->bitmap_file = NULL;
4429 }
4430
NeilBrown32a76272005-06-21 17:17:14 -07004431 return err;
4432}
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434/*
4435 * set_array_info is used two different ways
4436 * The original usage is when creating a new array.
4437 * In this usage, raid_disks is > 0 and it together with
4438 * level, size, not_persistent,layout,chunksize determine the
4439 * shape of the array.
4440 * This will always create an array with a type-0.90.0 superblock.
4441 * The newer usage is when assembling an array.
4442 * In this case raid_disks will be 0, and the major_version field is
4443 * use to determine which style super-blocks are to be found on the devices.
4444 * The minor and patch _version numbers are also kept incase the
4445 * super_block handler wishes to interpret them.
4446 */
4447static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4448{
4449
4450 if (info->raid_disks == 0) {
4451 /* just setting version number for superblock loading */
4452 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004453 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 super_types[info->major_version].name == NULL) {
4455 /* maybe try to auto-load a module? */
4456 printk(KERN_INFO
4457 "md: superblock version %d not known\n",
4458 info->major_version);
4459 return -EINVAL;
4460 }
4461 mddev->major_version = info->major_version;
4462 mddev->minor_version = info->minor_version;
4463 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004464 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return 0;
4466 }
4467 mddev->major_version = MD_MAJOR_VERSION;
4468 mddev->minor_version = MD_MINOR_VERSION;
4469 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4470 mddev->ctime = get_seconds();
4471
4472 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004473 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 mddev->size = info->size;
4475 mddev->raid_disks = info->raid_disks;
4476 /* don't set md_minor, it is determined by which /dev/md* was
4477 * openned
4478 */
4479 if (info->state & (1<<MD_SB_CLEAN))
4480 mddev->recovery_cp = MaxSector;
4481 else
4482 mddev->recovery_cp = 0;
4483 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004484 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
4486 mddev->layout = info->layout;
4487 mddev->chunk_size = info->chunk_size;
4488
4489 mddev->max_disks = MD_SB_DISKS;
4490
NeilBrowne6910632008-02-06 01:39:51 -08004491 if (mddev->persistent)
4492 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004493 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
NeilBrownb2a27032005-11-28 13:44:12 -08004495 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4496 mddev->bitmap_offset = 0;
4497
NeilBrownf6705572006-03-27 01:18:11 -08004498 mddev->reshape_position = MaxSector;
4499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 /*
4501 * Generate a 128 bit UUID
4502 */
4503 get_random_bytes(mddev->uuid, 16);
4504
NeilBrownf6705572006-03-27 01:18:11 -08004505 mddev->new_level = mddev->level;
4506 mddev->new_chunk = mddev->chunk_size;
4507 mddev->new_layout = mddev->layout;
4508 mddev->delta_disks = 0;
4509
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 return 0;
4511}
4512
NeilBrowna35b0d62006-01-06 00:20:49 -08004513static int update_size(mddev_t *mddev, unsigned long size)
4514{
4515 mdk_rdev_t * rdev;
4516 int rv;
4517 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004518 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004519
4520 if (mddev->pers->resize == NULL)
4521 return -EINVAL;
4522 /* The "size" is the amount of each device that is used.
4523 * This can only make sense for arrays with redundancy.
4524 * linear and raid0 always use whatever space is available
4525 * We can only consider changing the size if no resync
4526 * or reconstruction is happening, and if the new size
4527 * is acceptable. It must fit before the sb_offset or,
4528 * if that is <data_offset, it must fit before the
4529 * size of each device.
4530 * If size is zero, we find the largest size that fits.
4531 */
4532 if (mddev->sync_thread)
4533 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004534 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004535 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004536 avail = rdev->size * 2;
4537
NeilBrowna35b0d62006-01-06 00:20:49 -08004538 if (fit && (size == 0 || size > avail/2))
4539 size = avail/2;
4540 if (avail < ((sector_t)size << 1))
4541 return -ENOSPC;
4542 }
4543 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4544 if (!rv) {
4545 struct block_device *bdev;
4546
4547 bdev = bdget_disk(mddev->gendisk, 0);
4548 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004549 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004550 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004551 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004552 bdput(bdev);
4553 }
4554 }
4555 return rv;
4556}
4557
NeilBrownda943b992006-01-06 00:20:54 -08004558static int update_raid_disks(mddev_t *mddev, int raid_disks)
4559{
4560 int rv;
4561 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004562 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004563 return -EINVAL;
4564 if (raid_disks <= 0 ||
4565 raid_disks >= mddev->max_disks)
4566 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004567 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004568 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004569 mddev->delta_disks = raid_disks - mddev->raid_disks;
4570
4571 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004572 return rv;
4573}
4574
4575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576/*
4577 * update_array_info is used to change the configuration of an
4578 * on-line array.
4579 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4580 * fields in the info are checked against the array.
4581 * Any differences that cannot be handled will cause an error.
4582 * Normally, only one change can be managed at a time.
4583 */
4584static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4585{
4586 int rv = 0;
4587 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004588 int state = 0;
4589
4590 /* calculate expected state,ignoring low bits */
4591 if (mddev->bitmap && mddev->bitmap_offset)
4592 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
4594 if (mddev->major_version != info->major_version ||
4595 mddev->minor_version != info->minor_version ||
4596/* mddev->patch_version != info->patch_version || */
4597 mddev->ctime != info->ctime ||
4598 mddev->level != info->level ||
4599/* mddev->layout != info->layout || */
4600 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004601 mddev->chunk_size != info->chunk_size ||
4602 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4603 ((state^info->state) & 0xfffffe00)
4604 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 return -EINVAL;
4606 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004607 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 if (mddev->raid_disks != info->raid_disks) cnt++;
4609 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004610 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 if (cnt == 0) return 0;
4612 if (cnt > 1) return -EINVAL;
4613
4614 if (mddev->layout != info->layout) {
4615 /* Change layout
4616 * we don't need to do anything at the md level, the
4617 * personality will take care of it all.
4618 */
4619 if (mddev->pers->reconfig == NULL)
4620 return -EINVAL;
4621 else
4622 return mddev->pers->reconfig(mddev, info->layout, -1);
4623 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004624 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004625 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
NeilBrownda943b992006-01-06 00:20:54 -08004627 if (mddev->raid_disks != info->raid_disks)
4628 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
NeilBrown36fa3062005-09-09 16:23:45 -07004630 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4631 if (mddev->pers->quiesce == NULL)
4632 return -EINVAL;
4633 if (mddev->recovery || mddev->sync_thread)
4634 return -EBUSY;
4635 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4636 /* add the bitmap */
4637 if (mddev->bitmap)
4638 return -EEXIST;
4639 if (mddev->default_bitmap_offset == 0)
4640 return -EINVAL;
4641 mddev->bitmap_offset = mddev->default_bitmap_offset;
4642 mddev->pers->quiesce(mddev, 1);
4643 rv = bitmap_create(mddev);
4644 if (rv)
4645 bitmap_destroy(mddev);
4646 mddev->pers->quiesce(mddev, 0);
4647 } else {
4648 /* remove the bitmap */
4649 if (!mddev->bitmap)
4650 return -ENOENT;
4651 if (mddev->bitmap->file)
4652 return -EINVAL;
4653 mddev->pers->quiesce(mddev, 1);
4654 bitmap_destroy(mddev);
4655 mddev->pers->quiesce(mddev, 0);
4656 mddev->bitmap_offset = 0;
4657 }
4658 }
NeilBrown850b2b42006-10-03 01:15:46 -07004659 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 return rv;
4661}
4662
4663static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4664{
4665 mdk_rdev_t *rdev;
4666
4667 if (mddev->pers == NULL)
4668 return -ENODEV;
4669
4670 rdev = find_rdev(mddev, dev);
4671 if (!rdev)
4672 return -ENODEV;
4673
4674 md_error(mddev, rdev);
4675 return 0;
4676}
4677
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004678static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4679{
4680 mddev_t *mddev = bdev->bd_disk->private_data;
4681
4682 geo->heads = 2;
4683 geo->sectors = 4;
4684 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4685 return 0;
4686}
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688static int md_ioctl(struct inode *inode, struct file *file,
4689 unsigned int cmd, unsigned long arg)
4690{
4691 int err = 0;
4692 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 mddev_t *mddev = NULL;
4694
4695 if (!capable(CAP_SYS_ADMIN))
4696 return -EACCES;
4697
4698 /*
4699 * Commands dealing with the RAID driver but not any
4700 * particular array:
4701 */
4702 switch (cmd)
4703 {
4704 case RAID_VERSION:
4705 err = get_version(argp);
4706 goto done;
4707
4708 case PRINT_RAID_DEBUG:
4709 err = 0;
4710 md_print_devices();
4711 goto done;
4712
4713#ifndef MODULE
4714 case RAID_AUTORUN:
4715 err = 0;
4716 autostart_arrays(arg);
4717 goto done;
4718#endif
4719 default:;
4720 }
4721
4722 /*
4723 * Commands creating/starting a new array:
4724 */
4725
4726 mddev = inode->i_bdev->bd_disk->private_data;
4727
4728 if (!mddev) {
4729 BUG();
4730 goto abort;
4731 }
4732
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 err = mddev_lock(mddev);
4734 if (err) {
4735 printk(KERN_INFO
4736 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4737 err, cmd);
4738 goto abort;
4739 }
4740
4741 switch (cmd)
4742 {
4743 case SET_ARRAY_INFO:
4744 {
4745 mdu_array_info_t info;
4746 if (!arg)
4747 memset(&info, 0, sizeof(info));
4748 else if (copy_from_user(&info, argp, sizeof(info))) {
4749 err = -EFAULT;
4750 goto abort_unlock;
4751 }
4752 if (mddev->pers) {
4753 err = update_array_info(mddev, &info);
4754 if (err) {
4755 printk(KERN_WARNING "md: couldn't update"
4756 " array info. %d\n", err);
4757 goto abort_unlock;
4758 }
4759 goto done_unlock;
4760 }
4761 if (!list_empty(&mddev->disks)) {
4762 printk(KERN_WARNING
4763 "md: array %s already has disks!\n",
4764 mdname(mddev));
4765 err = -EBUSY;
4766 goto abort_unlock;
4767 }
4768 if (mddev->raid_disks) {
4769 printk(KERN_WARNING
4770 "md: array %s already initialised!\n",
4771 mdname(mddev));
4772 err = -EBUSY;
4773 goto abort_unlock;
4774 }
4775 err = set_array_info(mddev, &info);
4776 if (err) {
4777 printk(KERN_WARNING "md: couldn't set"
4778 " array info. %d\n", err);
4779 goto abort_unlock;
4780 }
4781 }
4782 goto done_unlock;
4783
4784 default:;
4785 }
4786
4787 /*
4788 * Commands querying/configuring an existing array:
4789 */
NeilBrown32a76272005-06-21 17:17:14 -07004790 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004791 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004792 if ((!mddev->raid_disks && !mddev->external)
4793 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4794 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4795 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 err = -ENODEV;
4797 goto abort_unlock;
4798 }
4799
4800 /*
4801 * Commands even a read-only array can execute:
4802 */
4803 switch (cmd)
4804 {
4805 case GET_ARRAY_INFO:
4806 err = get_array_info(mddev, argp);
4807 goto done_unlock;
4808
NeilBrown32a76272005-06-21 17:17:14 -07004809 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004810 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004811 goto done_unlock;
4812
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 case GET_DISK_INFO:
4814 err = get_disk_info(mddev, argp);
4815 goto done_unlock;
4816
4817 case RESTART_ARRAY_RW:
4818 err = restart_array(mddev);
4819 goto done_unlock;
4820
4821 case STOP_ARRAY:
4822 err = do_md_stop (mddev, 0);
4823 goto done_unlock;
4824
4825 case STOP_ARRAY_RO:
4826 err = do_md_stop (mddev, 1);
4827 goto done_unlock;
4828
4829 /*
4830 * We have a problem here : there is no easy way to give a CHS
4831 * virtual geometry. We currently pretend that we have a 2 heads
4832 * 4 sectors (with a BIG number of cylinders...). This drives
4833 * dosfs just mad... ;-)
4834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 }
4836
4837 /*
4838 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004839 * superblock, so we do not allow them on read-only arrays.
4840 * However non-MD ioctls (e.g. get-size) will still come through
4841 * here and hit the 'default' below, so only disallow
4842 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 */
NeilBrownf91de922005-11-08 21:39:36 -08004844 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4845 mddev->ro && mddev->pers) {
4846 if (mddev->ro == 2) {
4847 mddev->ro = 0;
4848 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4849 md_wakeup_thread(mddev->thread);
4850
4851 } else {
4852 err = -EROFS;
4853 goto abort_unlock;
4854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
4856
4857 switch (cmd)
4858 {
4859 case ADD_NEW_DISK:
4860 {
4861 mdu_disk_info_t info;
4862 if (copy_from_user(&info, argp, sizeof(info)))
4863 err = -EFAULT;
4864 else
4865 err = add_new_disk(mddev, &info);
4866 goto done_unlock;
4867 }
4868
4869 case HOT_REMOVE_DISK:
4870 err = hot_remove_disk(mddev, new_decode_dev(arg));
4871 goto done_unlock;
4872
4873 case HOT_ADD_DISK:
4874 err = hot_add_disk(mddev, new_decode_dev(arg));
4875 goto done_unlock;
4876
4877 case SET_DISK_FAULTY:
4878 err = set_disk_faulty(mddev, new_decode_dev(arg));
4879 goto done_unlock;
4880
4881 case RUN_ARRAY:
4882 err = do_md_run (mddev);
4883 goto done_unlock;
4884
NeilBrown32a76272005-06-21 17:17:14 -07004885 case SET_BITMAP_FILE:
4886 err = set_bitmap_file(mddev, (int)arg);
4887 goto done_unlock;
4888
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 err = -EINVAL;
4891 goto abort_unlock;
4892 }
4893
4894done_unlock:
4895abort_unlock:
4896 mddev_unlock(mddev);
4897
4898 return err;
4899done:
4900 if (err)
4901 MD_BUG();
4902abort:
4903 return err;
4904}
4905
4906static int md_open(struct inode *inode, struct file *file)
4907{
4908 /*
4909 * Succeed if we can lock the mddev, which confirms that
4910 * it isn't being stopped right now.
4911 */
4912 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4913 int err;
4914
NeilBrownd63a5a72006-12-08 02:36:17 -08004915 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 goto out;
4917
4918 err = 0;
4919 mddev_get(mddev);
4920 mddev_unlock(mddev);
4921
4922 check_disk_change(inode->i_bdev);
4923 out:
4924 return err;
4925}
4926
4927static int md_release(struct inode *inode, struct file * file)
4928{
4929 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4930
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004931 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 mddev_put(mddev);
4933
4934 return 0;
4935}
4936
Linus Torvalds44ce62942007-05-09 18:51:36 -07004937static int md_media_changed(struct gendisk *disk)
4938{
4939 mddev_t *mddev = disk->private_data;
4940
4941 return mddev->changed;
4942}
4943
4944static int md_revalidate(struct gendisk *disk)
4945{
4946 mddev_t *mddev = disk->private_data;
4947
4948 mddev->changed = 0;
4949 return 0;
4950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951static struct block_device_operations md_fops =
4952{
4953 .owner = THIS_MODULE,
4954 .open = md_open,
4955 .release = md_release,
4956 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004957 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004958 .media_changed = md_media_changed,
4959 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960};
4961
Adrian Bunk75c96f82005-05-05 16:16:09 -07004962static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963{
4964 mdk_thread_t *thread = arg;
4965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 /*
4967 * md_thread is a 'system-thread', it's priority should be very
4968 * high. We avoid resource deadlocks individually in each
4969 * raid personality. (RAID5 does preallocation) We also use RR and
4970 * the very same RT priority as kswapd, thus we will never get
4971 * into a priority inversion deadlock.
4972 *
4973 * we definitely have to have equal or higher priority than
4974 * bdflush, otherwise bdflush will deadlock if there are too
4975 * many dirty RAID5 blocks.
4976 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977
NeilBrown6985c432005-10-19 21:23:47 -07004978 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004979 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
NeilBrown93588e22005-11-15 00:09:12 -08004981 /* We need to wait INTERRUPTIBLE so that
4982 * we don't add to the load-average.
4983 * That means we need to be sure no signals are
4984 * pending
4985 */
4986 if (signal_pending(current))
4987 flush_signals(current);
4988
4989 wait_event_interruptible_timeout
4990 (thread->wqueue,
4991 test_bit(THREAD_WAKEUP, &thread->flags)
4992 || kthread_should_stop(),
4993 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
4995 clear_bit(THREAD_WAKEUP, &thread->flags);
4996
NeilBrown787453c2005-11-08 21:39:43 -08004997 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004999
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 return 0;
5001}
5002
5003void md_wakeup_thread(mdk_thread_t *thread)
5004{
5005 if (thread) {
5006 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5007 set_bit(THREAD_WAKEUP, &thread->flags);
5008 wake_up(&thread->wqueue);
5009 }
5010}
5011
5012mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5013 const char *name)
5014{
5015 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
NeilBrown9ffae0c2006-01-06 00:20:32 -08005017 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 if (!thread)
5019 return NULL;
5020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 init_waitqueue_head(&thread->wqueue);
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 thread->run = run;
5024 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005025 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005026 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005027 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 kfree(thread);
5029 return NULL;
5030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 return thread;
5032}
5033
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034void md_unregister_thread(mdk_thread_t *thread)
5035{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005036 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005037
5038 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 kfree(thread);
5040}
5041
5042void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5043{
5044 if (!mddev) {
5045 MD_BUG();
5046 return;
5047 }
5048
NeilBrownb2d444d2005-11-08 21:39:31 -08005049 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005051
5052 if (mddev->external)
5053 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005054/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5056 mdname(mddev),
5057 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5058 __builtin_return_address(0),__builtin_return_address(1),
5059 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005060*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005061 if (!mddev->pers)
5062 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 if (!mddev->pers->error_handler)
5064 return;
5065 mddev->pers->error_handler(mddev,rdev);
5066 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5067 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5068 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005069 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070}
5071
5072/* seq_file implementation /proc/mdstat */
5073
5074static void status_unused(struct seq_file *seq)
5075{
5076 int i = 0;
5077 mdk_rdev_t *rdev;
5078 struct list_head *tmp;
5079
5080 seq_printf(seq, "unused devices: ");
5081
NeilBrown73c34432008-02-06 01:39:59 -08005082 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 char b[BDEVNAME_SIZE];
5084 i++;
5085 seq_printf(seq, "%s ",
5086 bdevname(rdev->bdev,b));
5087 }
5088 if (!i)
5089 seq_printf(seq, "<none>");
5090
5091 seq_printf(seq, "\n");
5092}
5093
5094
5095static void status_resync(struct seq_file *seq, mddev_t * mddev)
5096{
NeilBrown4588b422006-03-27 01:18:04 -08005097 sector_t max_blocks, resync, res;
5098 unsigned long dt, db, rt;
5099 int scale;
5100 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
5102 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5103
5104 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5105 max_blocks = mddev->resync_max_sectors >> 1;
5106 else
5107 max_blocks = mddev->size;
5108
5109 /*
5110 * Should not happen.
5111 */
5112 if (!max_blocks) {
5113 MD_BUG();
5114 return;
5115 }
NeilBrown4588b422006-03-27 01:18:04 -08005116 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5117 * in a sector_t, and (max_blocks>>scale) will fit in a
5118 * u32, as those are the requirements for sector_div.
5119 * Thus 'scale' must be at least 10
5120 */
5121 scale = 10;
5122 if (sizeof(sector_t) > sizeof(unsigned long)) {
5123 while ( max_blocks/2 > (1ULL<<(scale+32)))
5124 scale++;
5125 }
5126 res = (resync>>scale)*1000;
5127 sector_div(res, (u32)((max_blocks>>scale)+1));
5128
5129 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 {
NeilBrown4588b422006-03-27 01:18:04 -08005131 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 seq_printf(seq, "[");
5133 for (i = 0; i < x; i++)
5134 seq_printf(seq, "=");
5135 seq_printf(seq, ">");
5136 for (i = 0; i < y; i++)
5137 seq_printf(seq, ".");
5138 seq_printf(seq, "] ");
5139 }
NeilBrown4588b422006-03-27 01:18:04 -08005140 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005141 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5142 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005143 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5144 "check" :
5145 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5146 "resync" : "recovery"))),
5147 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005148 (unsigned long long) resync,
5149 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
5151 /*
5152 * We do not want to overflow, so the order of operands and
5153 * the * 100 / 100 trick are important. We do a +1 to be
5154 * safe against division by zero. We only estimate anyway.
5155 *
5156 * dt: time from mark until now
5157 * db: blocks written from mark until now
5158 * rt: remaining time
5159 */
5160 dt = ((jiffies - mddev->resync_mark) / HZ);
5161 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005162 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5163 - mddev->resync_mark_cnt;
5164 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165
5166 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5167
NeilBrownff4e8d92006-07-10 04:44:16 -07005168 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169}
5170
5171static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5172{
5173 struct list_head *tmp;
5174 loff_t l = *pos;
5175 mddev_t *mddev;
5176
5177 if (l >= 0x10000)
5178 return NULL;
5179 if (!l--)
5180 /* header */
5181 return (void*)1;
5182
5183 spin_lock(&all_mddevs_lock);
5184 list_for_each(tmp,&all_mddevs)
5185 if (!l--) {
5186 mddev = list_entry(tmp, mddev_t, all_mddevs);
5187 mddev_get(mddev);
5188 spin_unlock(&all_mddevs_lock);
5189 return mddev;
5190 }
5191 spin_unlock(&all_mddevs_lock);
5192 if (!l--)
5193 return (void*)2;/* tail */
5194 return NULL;
5195}
5196
5197static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5198{
5199 struct list_head *tmp;
5200 mddev_t *next_mddev, *mddev = v;
5201
5202 ++*pos;
5203 if (v == (void*)2)
5204 return NULL;
5205
5206 spin_lock(&all_mddevs_lock);
5207 if (v == (void*)1)
5208 tmp = all_mddevs.next;
5209 else
5210 tmp = mddev->all_mddevs.next;
5211 if (tmp != &all_mddevs)
5212 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5213 else {
5214 next_mddev = (void*)2;
5215 *pos = 0x10000;
5216 }
5217 spin_unlock(&all_mddevs_lock);
5218
5219 if (v != (void*)1)
5220 mddev_put(mddev);
5221 return next_mddev;
5222
5223}
5224
5225static void md_seq_stop(struct seq_file *seq, void *v)
5226{
5227 mddev_t *mddev = v;
5228
5229 if (mddev && v != (void*)1 && v != (void*)2)
5230 mddev_put(mddev);
5231}
5232
NeilBrownd7603b72006-01-06 00:20:30 -08005233struct mdstat_info {
5234 int event;
5235};
5236
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237static int md_seq_show(struct seq_file *seq, void *v)
5238{
5239 mddev_t *mddev = v;
5240 sector_t size;
5241 struct list_head *tmp2;
5242 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005243 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005244 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245
5246 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005247 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 seq_printf(seq, "Personalities : ");
5249 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005250 list_for_each_entry(pers, &pers_list, list)
5251 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252
5253 spin_unlock(&pers_lock);
5254 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005255 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 return 0;
5257 }
5258 if (v == (void*)2) {
5259 status_unused(seq);
5260 return 0;
5261 }
5262
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005263 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005265
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5267 seq_printf(seq, "%s : %sactive", mdname(mddev),
5268 mddev->pers ? "" : "in");
5269 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005270 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005272 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005273 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 seq_printf(seq, " %s", mddev->pers->name);
5275 }
5276
5277 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005278 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 char b[BDEVNAME_SIZE];
5280 seq_printf(seq, " %s[%d]",
5281 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005282 if (test_bit(WriteMostly, &rdev->flags))
5283 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005284 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 seq_printf(seq, "(F)");
5286 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005287 } else if (rdev->raid_disk < 0)
5288 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 size += rdev->size;
5290 }
5291
5292 if (!list_empty(&mddev->disks)) {
5293 if (mddev->pers)
5294 seq_printf(seq, "\n %llu blocks",
5295 (unsigned long long)mddev->array_size);
5296 else
5297 seq_printf(seq, "\n %llu blocks",
5298 (unsigned long long)size);
5299 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005300 if (mddev->persistent) {
5301 if (mddev->major_version != 0 ||
5302 mddev->minor_version != 90) {
5303 seq_printf(seq," super %d.%d",
5304 mddev->major_version,
5305 mddev->minor_version);
5306 }
NeilBrowne6910632008-02-06 01:39:51 -08005307 } else if (mddev->external)
5308 seq_printf(seq, " super external:%s",
5309 mddev->metadata_type);
5310 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005311 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312
5313 if (mddev->pers) {
5314 mddev->pers->status (seq, mddev);
5315 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005316 if (mddev->pers->sync_request) {
5317 if (mddev->curr_resync > 2) {
5318 status_resync (seq, mddev);
5319 seq_printf(seq, "\n ");
5320 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5321 seq_printf(seq, "\tresync=DELAYED\n ");
5322 else if (mddev->recovery_cp < MaxSector)
5323 seq_printf(seq, "\tresync=PENDING\n ");
5324 }
NeilBrown32a76272005-06-21 17:17:14 -07005325 } else
5326 seq_printf(seq, "\n ");
5327
5328 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005329 unsigned long chunk_kb;
5330 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005331 spin_lock_irqsave(&bitmap->lock, flags);
5332 chunk_kb = bitmap->chunksize >> 10;
5333 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5334 "%lu%s chunk",
5335 bitmap->pages - bitmap->missing_pages,
5336 bitmap->pages,
5337 (bitmap->pages - bitmap->missing_pages)
5338 << (PAGE_SHIFT - 10),
5339 chunk_kb ? chunk_kb : bitmap->chunksize,
5340 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005341 if (bitmap->file) {
5342 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005343 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005344 }
NeilBrown78d742d2005-06-21 17:17:15 -07005345
NeilBrown32a76272005-06-21 17:17:14 -07005346 seq_printf(seq, "\n");
5347 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 }
5349
5350 seq_printf(seq, "\n");
5351 }
5352 mddev_unlock(mddev);
5353
5354 return 0;
5355}
5356
5357static struct seq_operations md_seq_ops = {
5358 .start = md_seq_start,
5359 .next = md_seq_next,
5360 .stop = md_seq_stop,
5361 .show = md_seq_show,
5362};
5363
5364static int md_seq_open(struct inode *inode, struct file *file)
5365{
5366 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005367 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5368 if (mi == NULL)
5369 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370
5371 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005372 if (error)
5373 kfree(mi);
5374 else {
5375 struct seq_file *p = file->private_data;
5376 p->private = mi;
5377 mi->event = atomic_read(&md_event_count);
5378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 return error;
5380}
5381
NeilBrownd7603b72006-01-06 00:20:30 -08005382static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5383{
5384 struct seq_file *m = filp->private_data;
5385 struct mdstat_info *mi = m->private;
5386 int mask;
5387
5388 poll_wait(filp, &md_event_waiters, wait);
5389
5390 /* always allow read */
5391 mask = POLLIN | POLLRDNORM;
5392
5393 if (mi->event != atomic_read(&md_event_count))
5394 mask |= POLLERR | POLLPRI;
5395 return mask;
5396}
5397
Arjan van de Venfa027c22007-02-12 00:55:33 -08005398static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005399 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 .open = md_seq_open,
5401 .read = seq_read,
5402 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005403 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005404 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405};
5406
NeilBrown2604b702006-01-06 00:20:36 -08005407int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005410 list_add_tail(&p->list, &pers_list);
5411 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 spin_unlock(&pers_lock);
5413 return 0;
5414}
5415
NeilBrown2604b702006-01-06 00:20:36 -08005416int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417{
NeilBrown2604b702006-01-06 00:20:36 -08005418 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005420 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 spin_unlock(&pers_lock);
5422 return 0;
5423}
5424
5425static int is_mddev_idle(mddev_t *mddev)
5426{
5427 mdk_rdev_t * rdev;
5428 struct list_head *tmp;
5429 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005430 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431
5432 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005433 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005435 curr_events = disk_stat_read(disk, sectors[0]) +
5436 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005438 /* sync IO will cause sync_io to increase before the disk_stats
5439 * as sync_io is counted when a request starts, and
5440 * disk_stats is counted when it completes.
5441 * So resync activity will cause curr_events to be smaller than
5442 * when there was no such activity.
5443 * non-sync IO will cause disk_stat to increase without
5444 * increasing sync_io so curr_events will (eventually)
5445 * be larger than it was before. Once it becomes
5446 * substantially larger, the test below will cause
5447 * the array to appear non-idle, and resync will slow
5448 * down.
5449 * If there is a lot of outstanding resync activity when
5450 * we set last_event to curr_events, then all that activity
5451 * completing might cause the array to appear non-idle
5452 * and resync will be slowed down even though there might
5453 * not have been non-resync activity. This will only
5454 * happen once though. 'last_events' will soon reflect
5455 * the state where there is little or no outstanding
5456 * resync requests, and further resync activity will
5457 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005458 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005460 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 rdev->last_events = curr_events;
5462 idle = 0;
5463 }
5464 }
5465 return idle;
5466}
5467
5468void md_done_sync(mddev_t *mddev, int blocks, int ok)
5469{
5470 /* another "blocks" (512byte) blocks have been synced */
5471 atomic_sub(blocks, &mddev->recovery_active);
5472 wake_up(&mddev->recovery_wait);
5473 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005474 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 md_wakeup_thread(mddev->thread);
5476 // stop recovery, signal do_sync ....
5477 }
5478}
5479
5480
NeilBrown06d91a52005-06-21 17:17:12 -07005481/* md_write_start(mddev, bi)
5482 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005483 * in superblock) before writing, schedule a superblock update
5484 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005485 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005486void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487{
NeilBrown06d91a52005-06-21 17:17:12 -07005488 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005489 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005490
NeilBrownf91de922005-11-08 21:39:36 -08005491 BUG_ON(mddev->ro == 1);
5492 if (mddev->ro == 2) {
5493 /* need to switch to read/write */
5494 mddev->ro = 0;
5495 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5496 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005497 md_wakeup_thread(mddev->sync_thread);
NeilBrownf91de922005-11-08 21:39:36 -08005498 }
NeilBrown06d91a52005-06-21 17:17:12 -07005499 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005500 if (mddev->safemode == 1)
5501 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005502 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005503 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005504 if (mddev->in_sync) {
5505 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005506 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005507 md_wakeup_thread(mddev->thread);
5508 }
NeilBrowna9701a32005-11-08 21:39:34 -08005509 spin_unlock_irq(&mddev->write_lock);
NeilBrown09a44cc2008-05-23 13:04:36 -07005510 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown06d91a52005-06-21 17:17:12 -07005511 }
NeilBrown09a44cc2008-05-23 13:04:36 -07005512 wait_event(mddev->sb_wait,
5513 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5514 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515}
5516
5517void md_write_end(mddev_t *mddev)
5518{
5519 if (atomic_dec_and_test(&mddev->writes_pending)) {
5520 if (mddev->safemode == 2)
5521 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005522 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5524 }
5525}
5526
NeilBrown2a2275d2007-01-26 00:57:11 -08005527/* md_allow_write(mddev)
5528 * Calling this ensures that the array is marked 'active' so that writes
5529 * may proceed without blocking. It is important to call this before
5530 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5531 * Must be called with mddev_lock held.
5532 */
5533void md_allow_write(mddev_t *mddev)
5534{
5535 if (!mddev->pers)
5536 return;
5537 if (mddev->ro)
5538 return;
5539
5540 spin_lock_irq(&mddev->write_lock);
5541 if (mddev->in_sync) {
5542 mddev->in_sync = 0;
5543 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5544 if (mddev->safemode_delay &&
5545 mddev->safemode == 0)
5546 mddev->safemode = 1;
5547 spin_unlock_irq(&mddev->write_lock);
5548 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005549
5550 sysfs_notify(&mddev->kobj, NULL, "array_state");
5551 /* wait for the dirty state to be recorded in the metadata */
5552 wait_event(mddev->sb_wait,
5553 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5554 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08005555 } else
5556 spin_unlock_irq(&mddev->write_lock);
5557}
5558EXPORT_SYMBOL_GPL(md_allow_write);
5559
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560#define SYNC_MARKS 10
5561#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005562void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
5564 mddev_t *mddev2;
5565 unsigned int currspeed = 0,
5566 window;
NeilBrown57afd892005-06-21 17:17:13 -07005567 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 unsigned long mark[SYNC_MARKS];
5569 sector_t mark_cnt[SYNC_MARKS];
5570 int last_mark,m;
5571 struct list_head *tmp;
5572 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005573 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005574 struct list_head *rtmp;
5575 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005576 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577
5578 /* just incase thread restarts... */
5579 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5580 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005581 if (mddev->ro) /* never try to sync a read-only array */
5582 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583
NeilBrown61df9d92006-10-03 01:15:57 -07005584 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5585 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5586 desc = "data-check";
5587 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5588 desc = "requested-resync";
5589 else
5590 desc = "resync";
5591 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5592 desc = "reshape";
5593 else
5594 desc = "recovery";
5595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 /* we overload curr_resync somewhat here.
5597 * 0 == not engaged in resync at all
5598 * 2 == checking that there is no conflict with another sync
5599 * 1 == like 2, but have yielded to allow conflicting resync to
5600 * commense
5601 * other == active in resync - this many blocks
5602 *
5603 * Before starting a resync we must have set curr_resync to
5604 * 2, and then checked that every "conflicting" array has curr_resync
5605 * less than ours. When we find one that is the same or higher
5606 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5607 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5608 * This will mean we have to start checking from the beginning again.
5609 *
5610 */
5611
5612 do {
5613 mddev->curr_resync = 2;
5614
5615 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005616 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005617 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 goto skip;
5619 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005620 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 if (mddev2 == mddev)
5622 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005623 if (!mddev->parallel_resync
5624 && mddev2->curr_resync
5625 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 DEFINE_WAIT(wq);
5627 if (mddev < mddev2 && mddev->curr_resync == 2) {
5628 /* arbitrarily yield */
5629 mddev->curr_resync = 1;
5630 wake_up(&resync_wait);
5631 }
5632 if (mddev > mddev2 && mddev->curr_resync == 1)
5633 /* no need to wait here, we can wait the next
5634 * time 'round when curr_resync == 2
5635 */
5636 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005637 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5638 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005639 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005640 printk(KERN_INFO "md: delaying %s of %s"
5641 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005643 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 mddev_put(mddev2);
5645 schedule();
5646 finish_wait(&resync_wait, &wq);
5647 goto try_again;
5648 }
5649 finish_wait(&resync_wait, &wq);
5650 }
5651 }
5652 } while (mddev->curr_resync < 2);
5653
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005654 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005655 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005657 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 */
5659 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005660 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005661 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005662 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5663 j = mddev->resync_min;
5664 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005665 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005666
NeilBrownccfcc3c2006-03-27 01:18:09 -08005667 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5668 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005669 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 /* recovery follows the physical size of devices */
5671 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005672 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005673 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005674 if (rdev->raid_disk >= 0 &&
5675 !test_bit(Faulty, &rdev->flags) &&
5676 !test_bit(In_sync, &rdev->flags) &&
5677 rdev->recovery_offset < j)
5678 j = rdev->recovery_offset;
5679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
NeilBrown61df9d92006-10-03 01:15:57 -07005681 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5682 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5683 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005684 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005685 "(but not more than %d KB/sec) for %s.\n",
5686 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
5688 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005689
NeilBrown57afd892005-06-21 17:17:13 -07005690 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 for (m = 0; m < SYNC_MARKS; m++) {
5692 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005693 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 }
5695 last_mark = 0;
5696 mddev->resync_mark = mark[last_mark];
5697 mddev->resync_mark_cnt = mark_cnt[last_mark];
5698
5699 /*
5700 * Tune reconstruction:
5701 */
5702 window = 32*(PAGE_SIZE/512);
5703 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5704 window/2,(unsigned long long) max_sectors/2);
5705
5706 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 last_check = 0;
5708
5709 if (j>2) {
5710 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005711 "md: resuming %s of %s from checkpoint.\n",
5712 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 mddev->curr_resync = j;
5714 }
5715
5716 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005717 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
NeilBrown57afd892005-06-21 17:17:13 -07005719 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005720 if (j >= mddev->resync_max) {
5721 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5722 wait_event(mddev->recovery_wait,
5723 mddev->resync_max > j
5724 || kthread_should_stop());
5725 }
5726 if (kthread_should_stop())
5727 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005728 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005729 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005730 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005731 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 goto out;
5733 }
NeilBrown57afd892005-06-21 17:17:13 -07005734
5735 if (!skipped) { /* actual IO requested */
5736 io_sectors += sectors;
5737 atomic_add(sectors, &mddev->recovery_active);
5738 }
5739
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 j += sectors;
5741 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005742 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005743 if (last_check == 0)
5744 /* this is the earliers that rebuilt will be
5745 * visible in /proc/mdstat
5746 */
5747 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005748
5749 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 continue;
5751
NeilBrown57afd892005-06-21 17:17:13 -07005752 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
NeilBrowndfc70642008-05-23 13:04:39 -07005754 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 break;
5756
5757 repeat:
5758 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5759 /* step marks */
5760 int next = (last_mark+1) % SYNC_MARKS;
5761
5762 mddev->resync_mark = mark[next];
5763 mddev->resync_mark_cnt = mark_cnt[next];
5764 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005765 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 last_mark = next;
5767 }
5768
5769
NeilBrownc6207272008-02-06 01:39:52 -08005770 if (kthread_should_stop())
5771 goto interrupted;
5772
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773
5774 /*
5775 * this loop exits only if either when we are slower than
5776 * the 'hard' speed limit, or the system was IO-idle for
5777 * a jiffy.
5778 * the system might be non-idle CPU-wise, but we only care
5779 * about not overloading the IO subsystem. (things like an
5780 * e2fsck being done on the RAID array should execute fast)
5781 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005782 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 cond_resched();
5784
NeilBrown57afd892005-06-21 17:17:13 -07005785 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5786 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
NeilBrown88202a02006-01-06 00:21:36 -08005788 if (currspeed > speed_min(mddev)) {
5789 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005791 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 goto repeat;
5793 }
5794 }
5795 }
NeilBrown61df9d92006-10-03 01:15:57 -07005796 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 /*
5798 * this also signals 'finished resyncing' to md_stop
5799 */
5800 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005801 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
5803 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5804
5805 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005806 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807
NeilBrowndfc70642008-05-23 13:04:39 -07005808 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005809 mddev->curr_resync > 2) {
5810 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5811 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5812 if (mddev->curr_resync >= mddev->recovery_cp) {
5813 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005814 "md: checkpointing %s of %s.\n",
5815 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005816 mddev->recovery_cp = mddev->curr_resync;
5817 }
5818 } else
5819 mddev->recovery_cp = MaxSector;
5820 } else {
5821 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5822 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005823 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005824 if (rdev->raid_disk >= 0 &&
5825 !test_bit(Faulty, &rdev->flags) &&
5826 !test_bit(In_sync, &rdev->flags) &&
5827 rdev->recovery_offset < mddev->curr_resync)
5828 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 }
NeilBrown17571282006-12-10 02:20:52 -08005831 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 skip:
5834 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005835 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005836 mddev->resync_max = MaxSector;
5837 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 wake_up(&resync_wait);
5839 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5840 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005841 return;
5842
5843 interrupted:
5844 /*
5845 * got a signal, exit.
5846 */
5847 printk(KERN_INFO
5848 "md: md_do_sync() got signal ... exiting\n");
5849 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5850 goto out;
5851
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852}
NeilBrown29269552006-03-27 01:18:10 -08005853EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854
5855
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005856static int remove_and_add_spares(mddev_t *mddev)
5857{
5858 mdk_rdev_t *rdev;
5859 struct list_head *rtmp;
5860 int spares = 0;
5861
NeilBrownd089c6a2008-02-06 01:39:59 -08005862 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005863 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005864 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005865 (test_bit(Faulty, &rdev->flags) ||
5866 ! test_bit(In_sync, &rdev->flags)) &&
5867 atomic_read(&rdev->nr_pending)==0) {
5868 if (mddev->pers->hot_remove_disk(
5869 mddev, rdev->raid_disk)==0) {
5870 char nm[20];
5871 sprintf(nm,"rd%d", rdev->raid_disk);
5872 sysfs_remove_link(&mddev->kobj, nm);
5873 rdev->raid_disk = -1;
5874 }
5875 }
5876
5877 if (mddev->degraded) {
NeilBrowndfc70642008-05-23 13:04:39 -07005878 rdev_for_each(rdev, rtmp, mddev) {
5879 if (rdev->raid_disk >= 0 &&
5880 !test_bit(In_sync, &rdev->flags))
5881 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005882 if (rdev->raid_disk < 0
5883 && !test_bit(Faulty, &rdev->flags)) {
5884 rdev->recovery_offset = 0;
5885 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5886 char nm[20];
5887 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005888 if (sysfs_create_link(&mddev->kobj,
5889 &rdev->kobj, nm))
5890 printk(KERN_WARNING
5891 "md: cannot register "
5892 "%s for %s\n",
5893 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005894 spares++;
5895 md_new_event(mddev);
5896 } else
5897 break;
5898 }
NeilBrowndfc70642008-05-23 13:04:39 -07005899 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005900 }
5901 return spares;
5902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903/*
5904 * This routine is regularly called by all per-raid-array threads to
5905 * deal with generic issues like resync and super-block update.
5906 * Raid personalities that don't have a thread (linear/raid0) do not
5907 * need this as they never do any recovery or update the superblock.
5908 *
5909 * It does not do any resync itself, but rather "forks" off other threads
5910 * to do that as needed.
5911 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5912 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07005913 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914 * and wakeups up this thread which will reap the thread and finish up.
5915 * This thread also removes any faulty devices (with nr_pending == 0).
5916 *
5917 * The overall approach is:
5918 * 1/ if the superblock needs updating, update it.
5919 * 2/ If a recovery thread is running, don't do anything else.
5920 * 3/ If recovery has finished, clean up, possibly marking spares active.
5921 * 4/ If there are any faulty devices, remove them.
5922 * 5/ If array is degraded, try to add spares devices
5923 * 6/ If array has spares or is not in-sync, start a resync thread.
5924 */
5925void md_check_recovery(mddev_t *mddev)
5926{
5927 mdk_rdev_t *rdev;
5928 struct list_head *rtmp;
5929
5930
NeilBrown5f404022005-06-21 17:17:16 -07005931 if (mddev->bitmap)
5932 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933
5934 if (mddev->ro)
5935 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005936
5937 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07005938 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07005939 printk(KERN_INFO "md: %s in immediate safe mode\n",
5940 mdname(mddev));
5941 mddev->safemode = 2;
5942 }
5943 flush_signals(current);
5944 }
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08005947 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005949 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07005950 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005951 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5952 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 ))
5954 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005955
NeilBrowndf5b89b2006-03-27 01:18:20 -08005956 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005957 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005958
NeilBrown31a59e32008-04-30 00:52:30 -07005959 if (!mddev->external) {
5960 spin_lock_irq(&mddev->write_lock);
5961 if (mddev->safemode &&
5962 !atomic_read(&mddev->writes_pending) &&
5963 !mddev->in_sync &&
5964 mddev->recovery_cp == MaxSector) {
5965 mddev->in_sync = 1;
5966 if (mddev->persistent)
5967 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5968 }
5969 if (mddev->safemode == 1)
5970 mddev->safemode = 0;
5971 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005972 }
NeilBrownfca4d842005-06-21 17:17:11 -07005973
NeilBrown850b2b42006-10-03 01:15:46 -07005974 if (mddev->flags)
5975 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005976
NeilBrown06d91a52005-06-21 17:17:12 -07005977
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5979 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5980 /* resync/recovery still happening */
5981 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5982 goto unlock;
5983 }
5984 if (mddev->sync_thread) {
5985 /* resync has finished, collect result */
5986 md_unregister_thread(mddev->sync_thread);
5987 mddev->sync_thread = NULL;
NeilBrowndfc70642008-05-23 13:04:39 -07005988 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 /* success...*/
5990 /* activate any spares */
5991 mddev->pers->spare_active(mddev);
5992 }
NeilBrown850b2b42006-10-03 01:15:46 -07005993 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005994
5995 /* if array is no-longer degraded, then any saved_raid_disk
5996 * information must be scrapped
5997 */
5998 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08005999 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006000 rdev->saved_raid_disk = -1;
6001
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 mddev->recovery = 0;
6003 /* flag recovery needed just to double check */
6004 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08006005 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 goto unlock;
6007 }
NeilBrown24dd4692005-11-08 21:39:26 -08006008 /* Clear some bits that don't mean anything, but
6009 * might be left set
6010 */
6011 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006012 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6013 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006015 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6016 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 /* no recovery is running.
6018 * remove any failed drives, then
6019 * add spares if possible.
6020 * Spare are also removed and re-added, to allow
6021 * the personality to fail the re-add.
6022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006024 if (mddev->reshape_position != MaxSector) {
6025 if (mddev->pers->check_reshape(mddev) != 0)
6026 /* Cannot proceed */
6027 goto unlock;
6028 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6029 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006030 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6031 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6032 } else if (mddev->recovery_cp < MaxSector) {
6033 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6034 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6035 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 if (mddev->pers->sync_request) {
6039 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07006040 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6041 /* We are adding a device or devices to an array
6042 * which has the bitmap stored on all devices.
6043 * So make sure all bitmap pages get written
6044 */
6045 bitmap_write_all(mddev->bitmap);
6046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 mddev->sync_thread = md_register_thread(md_do_sync,
6048 mddev,
6049 "%s_resync");
6050 if (!mddev->sync_thread) {
6051 printk(KERN_ERR "%s: could not start resync"
6052 " thread...\n",
6053 mdname(mddev));
6054 /* leave the spares where they are, it shouldn't hurt */
6055 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006056 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006058 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 }
6060 unlock:
6061 mddev_unlock(mddev);
6062 }
6063}
6064
Dan Williams6bfe0b42008-04-30 00:52:32 -07006065void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6066{
6067 sysfs_notify(&rdev->kobj, NULL, "state");
6068 wait_event_timeout(rdev->blocked_wait,
6069 !test_bit(Blocked, &rdev->flags),
6070 msecs_to_jiffies(5000));
6071 rdev_dec_pending(rdev, mddev);
6072}
6073EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6074
Adrian Bunk75c96f82005-05-05 16:16:09 -07006075static int md_notify_reboot(struct notifier_block *this,
6076 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077{
6078 struct list_head *tmp;
6079 mddev_t *mddev;
6080
6081 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6082
6083 printk(KERN_INFO "md: stopping all md devices.\n");
6084
NeilBrown29ac4aa2008-02-06 01:39:58 -08006085 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006086 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10006088 mddev_unlock(mddev);
6089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 /*
6091 * certain more exotic SCSI devices are known to be
6092 * volatile wrt too early system reboots. While the
6093 * right place to handle this issue is the given
6094 * driver, we do want to have a safe RAID driver ...
6095 */
6096 mdelay(1000*1);
6097 }
6098 return NOTIFY_DONE;
6099}
6100
Adrian Bunk75c96f82005-05-05 16:16:09 -07006101static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102 .notifier_call = md_notify_reboot,
6103 .next = NULL,
6104 .priority = INT_MAX, /* before any real devices */
6105};
6106
6107static void md_geninit(void)
6108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6110
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006111 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112}
6113
Adrian Bunk75c96f82005-05-05 16:16:09 -07006114static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 if (register_blkdev(MAJOR_NR, "md"))
6117 return -1;
6118 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6119 unregister_blkdev(MAJOR_NR, "md");
6120 return -1;
6121 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006122 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6123 md_probe, NULL, NULL);
6124 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 md_probe, NULL, NULL);
6126
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006128 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129
6130 md_geninit();
6131 return (0);
6132}
6133
6134
6135#ifndef MODULE
6136
6137/*
6138 * Searches all registered partitions for autorun RAID arrays
6139 * at boot time.
6140 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006141
6142static LIST_HEAD(all_detected_devices);
6143struct detected_devices_node {
6144 struct list_head list;
6145 dev_t dev;
6146};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147
6148void md_autodetect_dev(dev_t dev)
6149{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006150 struct detected_devices_node *node_detected_dev;
6151
6152 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6153 if (node_detected_dev) {
6154 node_detected_dev->dev = dev;
6155 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6156 } else {
6157 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6158 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006160}
6161
6162
6163static void autostart_arrays(int part)
6164{
6165 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006166 struct detected_devices_node *node_detected_dev;
6167 dev_t dev;
6168 int i_scanned, i_passed;
6169
6170 i_scanned = 0;
6171 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172
6173 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6174
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006175 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6176 i_scanned++;
6177 node_detected_dev = list_entry(all_detected_devices.next,
6178 struct detected_devices_node, list);
6179 list_del(&node_detected_dev->list);
6180 dev = node_detected_dev->dev;
6181 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006182 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 if (IS_ERR(rdev))
6184 continue;
6185
NeilBrownb2d444d2005-11-08 21:39:31 -08006186 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 MD_BUG();
6188 continue;
6189 }
NeilBrownd0fae182008-03-04 14:29:31 -08006190 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006192 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006194
6195 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6196 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
6198 autorun_devices(part);
6199}
6200
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006201#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202
6203static __exit void md_exit(void)
6204{
6205 mddev_t *mddev;
6206 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006207
NeilBrowne8703fe2006-10-03 01:15:59 -07006208 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6209 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
6211 unregister_blkdev(MAJOR_NR,"md");
6212 unregister_blkdev(mdp_major, "mdp");
6213 unregister_reboot_notifier(&md_notifier);
6214 unregister_sysctl_table(raid_table_header);
6215 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006216 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 struct gendisk *disk = mddev->gendisk;
6218 if (!disk)
6219 continue;
6220 export_array(mddev);
6221 del_gendisk(disk);
6222 put_disk(disk);
6223 mddev->gendisk = NULL;
6224 mddev_put(mddev);
6225 }
6226}
6227
Dan Williams685784a2007-07-09 11:56:42 -07006228subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229module_exit(md_exit)
6230
NeilBrownf91de922005-11-08 21:39:36 -08006231static int get_ro(char *buffer, struct kernel_param *kp)
6232{
6233 return sprintf(buffer, "%d", start_readonly);
6234}
6235static int set_ro(const char *val, struct kernel_param *kp)
6236{
6237 char *e;
6238 int num = simple_strtoul(val, &e, 10);
6239 if (*val && (*e == '\0' || *e == '\n')) {
6240 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006241 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006242 }
6243 return -EINVAL;
6244}
6245
NeilBrown80ca3a42006-07-10 04:44:18 -07006246module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6247module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006248
NeilBrownf91de922005-11-08 21:39:36 -08006249
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250EXPORT_SYMBOL(register_md_personality);
6251EXPORT_SYMBOL(unregister_md_personality);
6252EXPORT_SYMBOL(md_error);
6253EXPORT_SYMBOL(md_done_sync);
6254EXPORT_SYMBOL(md_write_start);
6255EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256EXPORT_SYMBOL(md_register_thread);
6257EXPORT_SYMBOL(md_unregister_thread);
6258EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259EXPORT_SYMBOL(md_check_recovery);
6260MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006261MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006262MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);