blob: 5d6fac1fd39ebbaa05c522d78bcd997d08758018 [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;
Neil Brown6c2fce22008-06-28 08:31:31 +10001935 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001936 /* 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 */
NeilBrownc303da62008-02-06 01:39:51 -08001943 if (rdev->raid_disk == -1)
1944 return -EEXIST;
1945 /* personality does all needed checks */
1946 if (rdev->mddev->pers->hot_add_disk == NULL)
1947 return -EINVAL;
1948 err = rdev->mddev->pers->
1949 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1950 if (err)
1951 return err;
1952 sprintf(nm, "rd%d", rdev->raid_disk);
1953 sysfs_remove_link(&rdev->mddev->kobj, nm);
1954 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1955 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10001956 } else if (rdev->mddev->pers) {
1957 mdk_rdev_t *rdev2;
1958 struct list_head *tmp;
1959 /* Activating a spare .. or possibly reactivating
1960 * if we every get bitmaps working here.
1961 */
1962
1963 if (rdev->raid_disk != -1)
1964 return -EBUSY;
1965
1966 if (rdev->mddev->pers->hot_add_disk == NULL)
1967 return -EINVAL;
1968
1969 rdev_for_each(rdev2, tmp, rdev->mddev)
1970 if (rdev2->raid_disk == slot)
1971 return -EEXIST;
1972
1973 rdev->raid_disk = slot;
1974 if (test_bit(In_sync, &rdev->flags))
1975 rdev->saved_raid_disk = slot;
1976 else
1977 rdev->saved_raid_disk = -1;
1978 err = rdev->mddev->pers->
1979 hot_add_disk(rdev->mddev, rdev);
1980 if (err != 1) {
1981 rdev->raid_disk = -1;
1982 if (err == 0)
1983 return -EEXIST;
1984 return err;
1985 }
1986 sprintf(nm, "rd%d", rdev->raid_disk);
1987 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
1988 printk(KERN_WARNING
1989 "md: cannot register "
1990 "%s for %s\n",
1991 nm, mdname(rdev->mddev));
1992
1993 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08001994 } else {
1995 if (slot >= rdev->mddev->raid_disks)
1996 return -ENOSPC;
1997 rdev->raid_disk = slot;
1998 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001999 clear_bit(Faulty, &rdev->flags);
2000 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002001 set_bit(In_sync, &rdev->flags);
2002 }
NeilBrown014236d2006-01-06 00:20:55 -08002003 return len;
2004}
2005
2006
2007static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002008__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002009
NeilBrown93c8cad2006-01-06 00:20:56 -08002010static ssize_t
2011offset_show(mdk_rdev_t *rdev, char *page)
2012{
Andrew Morton6961ece2006-01-06 00:20:59 -08002013 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002014}
2015
2016static ssize_t
2017offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2018{
2019 char *e;
2020 unsigned long long offset = simple_strtoull(buf, &e, 10);
2021 if (e==buf || (*e && *e != '\n'))
2022 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002023 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002024 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002025 if (rdev->size && rdev->mddev->external)
2026 /* Must set offset before size, so overlap checks
2027 * can be sane */
2028 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002029 rdev->data_offset = offset;
2030 return len;
2031}
2032
2033static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002034__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002035
NeilBrown83303b62006-01-06 00:21:06 -08002036static ssize_t
2037rdev_size_show(mdk_rdev_t *rdev, char *page)
2038{
2039 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2040}
2041
NeilBrownc5d79ad2008-02-06 01:39:54 -08002042static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2043{
2044 /* check if two start/length pairs overlap */
2045 if (s1+l1 <= s2)
2046 return 0;
2047 if (s2+l2 <= s1)
2048 return 0;
2049 return 1;
2050}
2051
NeilBrown83303b62006-01-06 00:21:06 -08002052static ssize_t
2053rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2054{
2055 char *e;
2056 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002057 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002058 mddev_t *my_mddev = rdev->mddev;
2059
NeilBrown83303b62006-01-06 00:21:06 -08002060 if (e==buf || (*e && *e != '\n'))
2061 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002062 if (my_mddev->pers && rdev->raid_disk >= 0)
NeilBrown83303b62006-01-06 00:21:06 -08002063 return -EBUSY;
2064 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002065 if (size > oldsize && rdev->mddev->external) {
2066 /* need to check that all other rdevs with the same ->bdev
2067 * do not overlap. We need to unlock the mddev to avoid
2068 * a deadlock. We have already changed rdev->size, and if
2069 * we have to change it back, we will have the lock again.
2070 */
2071 mddev_t *mddev;
2072 int overlap = 0;
2073 struct list_head *tmp, *tmp2;
2074
NeilBrown27c529b2008-03-04 14:29:33 -08002075 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002076 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002077 mdk_rdev_t *rdev2;
2078
2079 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002080 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002081 if (test_bit(AllReserved, &rdev2->flags) ||
2082 (rdev->bdev == rdev2->bdev &&
2083 rdev != rdev2 &&
2084 overlaps(rdev->data_offset, rdev->size,
2085 rdev2->data_offset, rdev2->size))) {
2086 overlap = 1;
2087 break;
2088 }
2089 mddev_unlock(mddev);
2090 if (overlap) {
2091 mddev_put(mddev);
2092 break;
2093 }
2094 }
NeilBrown27c529b2008-03-04 14:29:33 -08002095 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002096 if (overlap) {
2097 /* Someone else could have slipped in a size
2098 * change here, but doing so is just silly.
2099 * We put oldsize back because we *know* it is
2100 * safe, and trust userspace not to race with
2101 * itself
2102 */
2103 rdev->size = oldsize;
2104 return -EBUSY;
2105 }
2106 }
NeilBrown27c529b2008-03-04 14:29:33 -08002107 if (size < my_mddev->size || my_mddev->size == 0)
2108 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002109 return len;
2110}
2111
2112static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002113__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002114
NeilBrown86e6ffd2005-11-08 21:39:24 -08002115static struct attribute *rdev_default_attrs[] = {
2116 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002117 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002118 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002119 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002120 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002121 NULL,
2122};
2123static ssize_t
2124rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2125{
2126 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2127 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002128 mddev_t *mddev = rdev->mddev;
2129 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002130
2131 if (!entry->show)
2132 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002133
2134 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2135 if (!rv) {
2136 if (rdev->mddev == NULL)
2137 rv = -EBUSY;
2138 else
2139 rv = entry->show(rdev, page);
2140 mddev_unlock(mddev);
2141 }
2142 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002143}
2144
2145static ssize_t
2146rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2147 const char *page, size_t length)
2148{
2149 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2150 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002151 ssize_t rv;
2152 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002153
2154 if (!entry->store)
2155 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002156 if (!capable(CAP_SYS_ADMIN))
2157 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002158 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002159 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002160 if (rdev->mddev == NULL)
2161 rv = -EBUSY;
2162 else
2163 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002164 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002165 }
2166 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002167}
2168
2169static void rdev_free(struct kobject *ko)
2170{
2171 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2172 kfree(rdev);
2173}
2174static struct sysfs_ops rdev_sysfs_ops = {
2175 .show = rdev_attr_show,
2176 .store = rdev_attr_store,
2177};
2178static struct kobj_type rdev_ktype = {
2179 .release = rdev_free,
2180 .sysfs_ops = &rdev_sysfs_ops,
2181 .default_attrs = rdev_default_attrs,
2182};
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184/*
2185 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2186 *
2187 * mark the device faulty if:
2188 *
2189 * - the device is nonexistent (zero size)
2190 * - the device has no valid superblock
2191 *
2192 * a faulty rdev _never_ has rdev->sb set.
2193 */
2194static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2195{
2196 char b[BDEVNAME_SIZE];
2197 int err;
2198 mdk_rdev_t *rdev;
2199 sector_t size;
2200
NeilBrown9ffae0c2006-01-06 00:20:32 -08002201 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (!rdev) {
2203 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2204 return ERR_PTR(-ENOMEM);
2205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 if ((err = alloc_disk_sb(rdev)))
2208 goto abort_free;
2209
NeilBrownc5d79ad2008-02-06 01:39:54 -08002210 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (err)
2212 goto abort_free;
2213
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002214 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002217 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002218 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002219 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002221 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002223 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002224 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2227 if (!size) {
2228 printk(KERN_WARNING
2229 "md: %s has zero or unknown size, marking faulty!\n",
2230 bdevname(rdev->bdev,b));
2231 err = -EINVAL;
2232 goto abort_free;
2233 }
2234
2235 if (super_format >= 0) {
2236 err = super_types[super_format].
2237 load_super(rdev, NULL, super_minor);
2238 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002239 printk(KERN_WARNING
2240 "md: %s does not have a valid v%d.%d "
2241 "superblock, not importing!\n",
2242 bdevname(rdev->bdev,b),
2243 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto abort_free;
2245 }
2246 if (err < 0) {
2247 printk(KERN_WARNING
2248 "md: could not read %s's sb, not importing!\n",
2249 bdevname(rdev->bdev,b));
2250 goto abort_free;
2251 }
2252 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002255 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 return rdev;
2258
2259abort_free:
2260 if (rdev->sb_page) {
2261 if (rdev->bdev)
2262 unlock_rdev(rdev);
2263 free_disk_sb(rdev);
2264 }
2265 kfree(rdev);
2266 return ERR_PTR(err);
2267}
2268
2269/*
2270 * Check a full RAID array for plausibility
2271 */
2272
2273
NeilBrowna757e642005-04-16 15:26:42 -07002274static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 int i;
2277 struct list_head *tmp;
2278 mdk_rdev_t *rdev, *freshest;
2279 char b[BDEVNAME_SIZE];
2280
2281 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002282 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 switch (super_types[mddev->major_version].
2284 load_super(rdev, freshest, mddev->minor_version)) {
2285 case 1:
2286 freshest = rdev;
2287 break;
2288 case 0:
2289 break;
2290 default:
2291 printk( KERN_ERR \
2292 "md: fatal superblock inconsistency in %s"
2293 " -- removing from array\n",
2294 bdevname(rdev->bdev,b));
2295 kick_rdev_from_array(rdev);
2296 }
2297
2298
2299 super_types[mddev->major_version].
2300 validate_super(mddev, freshest);
2301
2302 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002303 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (rdev != freshest)
2305 if (super_types[mddev->major_version].
2306 validate_super(mddev, rdev)) {
2307 printk(KERN_WARNING "md: kicking non-fresh %s"
2308 " from array!\n",
2309 bdevname(rdev->bdev,b));
2310 kick_rdev_from_array(rdev);
2311 continue;
2312 }
2313 if (mddev->level == LEVEL_MULTIPATH) {
2314 rdev->desc_nr = i++;
2315 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002316 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002317 } else if (rdev->raid_disk >= mddev->raid_disks) {
2318 rdev->raid_disk = -1;
2319 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 }
2322
2323
2324
2325 if (mddev->recovery_cp != MaxSector &&
2326 mddev->level >= 1)
2327 printk(KERN_ERR "md: %s: raid array is not clean"
2328 " -- starting background reconstruction\n",
2329 mdname(mddev));
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331}
2332
NeilBrowneae17012005-11-08 21:39:23 -08002333static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002334safe_delay_show(mddev_t *mddev, char *page)
2335{
2336 int msec = (mddev->safemode_delay*1000)/HZ;
2337 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2338}
2339static ssize_t
2340safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2341{
2342 int scale=1;
2343 int dot=0;
2344 int i;
2345 unsigned long msec;
2346 char buf[30];
2347 char *e;
2348 /* remove a period, and count digits after it */
2349 if (len >= sizeof(buf))
2350 return -EINVAL;
2351 strlcpy(buf, cbuf, len);
2352 buf[len] = 0;
2353 for (i=0; i<len; i++) {
2354 if (dot) {
2355 if (isdigit(buf[i])) {
2356 buf[i-1] = buf[i];
2357 scale *= 10;
2358 }
2359 buf[i] = 0;
2360 } else if (buf[i] == '.') {
2361 dot=1;
2362 buf[i] = 0;
2363 }
2364 }
2365 msec = simple_strtoul(buf, &e, 10);
2366 if (e == buf || (*e && *e != '\n'))
2367 return -EINVAL;
2368 msec = (msec * 1000) / scale;
2369 if (msec == 0)
2370 mddev->safemode_delay = 0;
2371 else {
2372 mddev->safemode_delay = (msec*HZ)/1000;
2373 if (mddev->safemode_delay == 0)
2374 mddev->safemode_delay = 1;
2375 }
2376 return len;
2377}
2378static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002379__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002380
2381static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002382level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002383{
NeilBrown2604b702006-01-06 00:20:36 -08002384 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002385 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002386 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002387 else if (mddev->clevel[0])
2388 return sprintf(page, "%s\n", mddev->clevel);
2389 else if (mddev->level != LEVEL_NONE)
2390 return sprintf(page, "%d\n", mddev->level);
2391 else
2392 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002393}
2394
NeilBrownd9d166c2006-01-06 00:20:51 -08002395static ssize_t
2396level_store(mddev_t *mddev, const char *buf, size_t len)
2397{
NeilBrown20a49ff2008-02-06 01:39:57 -08002398 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002399 if (mddev->pers)
2400 return -EBUSY;
2401 if (len == 0)
2402 return 0;
2403 if (len >= sizeof(mddev->clevel))
2404 return -ENOSPC;
2405 strncpy(mddev->clevel, buf, len);
2406 if (mddev->clevel[len-1] == '\n')
2407 len--;
2408 mddev->clevel[len] = 0;
2409 mddev->level = LEVEL_NONE;
2410 return rv;
2411}
2412
2413static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002414__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002415
NeilBrownd4dbd022006-06-26 00:27:59 -07002416
2417static ssize_t
2418layout_show(mddev_t *mddev, char *page)
2419{
2420 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002421 if (mddev->reshape_position != MaxSector &&
2422 mddev->layout != mddev->new_layout)
2423 return sprintf(page, "%d (%d)\n",
2424 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002425 return sprintf(page, "%d\n", mddev->layout);
2426}
2427
2428static ssize_t
2429layout_store(mddev_t *mddev, const char *buf, size_t len)
2430{
2431 char *e;
2432 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002433
2434 if (!*buf || (*e && *e != '\n'))
2435 return -EINVAL;
2436
NeilBrown08a02ec2007-05-09 02:35:38 -07002437 if (mddev->pers)
2438 return -EBUSY;
2439 if (mddev->reshape_position != MaxSector)
2440 mddev->new_layout = n;
2441 else
2442 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002443 return len;
2444}
2445static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002446__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002447
2448
NeilBrowneae17012005-11-08 21:39:23 -08002449static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002450raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002451{
NeilBrownbb636542005-11-08 21:39:45 -08002452 if (mddev->raid_disks == 0)
2453 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002454 if (mddev->reshape_position != MaxSector &&
2455 mddev->delta_disks != 0)
2456 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2457 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002458 return sprintf(page, "%d\n", mddev->raid_disks);
2459}
2460
NeilBrownda943b992006-01-06 00:20:54 -08002461static int update_raid_disks(mddev_t *mddev, int raid_disks);
2462
2463static ssize_t
2464raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2465{
NeilBrownda943b992006-01-06 00:20:54 -08002466 char *e;
2467 int rv = 0;
2468 unsigned long n = simple_strtoul(buf, &e, 10);
2469
2470 if (!*buf || (*e && *e != '\n'))
2471 return -EINVAL;
2472
2473 if (mddev->pers)
2474 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002475 else if (mddev->reshape_position != MaxSector) {
2476 int olddisks = mddev->raid_disks - mddev->delta_disks;
2477 mddev->delta_disks = n - olddisks;
2478 mddev->raid_disks = n;
2479 } else
NeilBrownda943b992006-01-06 00:20:54 -08002480 mddev->raid_disks = n;
2481 return rv ? rv : len;
2482}
2483static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002484__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002485
NeilBrown24dd4692005-11-08 21:39:26 -08002486static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002487chunk_size_show(mddev_t *mddev, char *page)
2488{
NeilBrown08a02ec2007-05-09 02:35:38 -07002489 if (mddev->reshape_position != MaxSector &&
2490 mddev->chunk_size != mddev->new_chunk)
2491 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2492 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002493 return sprintf(page, "%d\n", mddev->chunk_size);
2494}
2495
2496static ssize_t
2497chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2498{
2499 /* can only set chunk_size if array is not yet active */
2500 char *e;
2501 unsigned long n = simple_strtoul(buf, &e, 10);
2502
NeilBrown3b343802006-01-06 00:20:47 -08002503 if (!*buf || (*e && *e != '\n'))
2504 return -EINVAL;
2505
NeilBrown08a02ec2007-05-09 02:35:38 -07002506 if (mddev->pers)
2507 return -EBUSY;
2508 else if (mddev->reshape_position != MaxSector)
2509 mddev->new_chunk = n;
2510 else
2511 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002512 return len;
2513}
2514static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002515__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002516
NeilBrowna94213b2006-06-26 00:28:00 -07002517static ssize_t
2518resync_start_show(mddev_t *mddev, char *page)
2519{
2520 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2521}
2522
2523static ssize_t
2524resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2525{
NeilBrowna94213b2006-06-26 00:28:00 -07002526 char *e;
2527 unsigned long long n = simple_strtoull(buf, &e, 10);
2528
2529 if (mddev->pers)
2530 return -EBUSY;
2531 if (!*buf || (*e && *e != '\n'))
2532 return -EINVAL;
2533
2534 mddev->recovery_cp = n;
2535 return len;
2536}
2537static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002538__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002539
NeilBrown9e653b62006-06-26 00:27:58 -07002540/*
2541 * The array state can be:
2542 *
2543 * clear
2544 * No devices, no size, no level
2545 * Equivalent to STOP_ARRAY ioctl
2546 * inactive
2547 * May have some settings, but array is not active
2548 * all IO results in error
2549 * When written, doesn't tear down array, but just stops it
2550 * suspended (not supported yet)
2551 * All IO requests will block. The array can be reconfigured.
2552 * Writing this, if accepted, will block until array is quiessent
2553 * readonly
2554 * no resync can happen. no superblocks get written.
2555 * write requests fail
2556 * read-auto
2557 * like readonly, but behaves like 'clean' on a write request.
2558 *
2559 * clean - no pending writes, but otherwise active.
2560 * When written to inactive array, starts without resync
2561 * If a write request arrives then
2562 * if metadata is known, mark 'dirty' and switch to 'active'.
2563 * if not known, block and switch to write-pending
2564 * If written to an active array that has pending writes, then fails.
2565 * active
2566 * fully active: IO and resync can be happening.
2567 * When written to inactive array, starts with resync
2568 *
2569 * write-pending
2570 * clean, but writes are blocked waiting for 'active' to be written.
2571 *
2572 * active-idle
2573 * like active, but no writes have been seen for a while (100msec).
2574 *
2575 */
2576enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2577 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002578static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002579 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2580 "write-pending", "active-idle", NULL };
2581
2582static int match_word(const char *word, char **list)
2583{
2584 int n;
2585 for (n=0; list[n]; n++)
2586 if (cmd_match(word, list[n]))
2587 break;
2588 return n;
2589}
2590
2591static ssize_t
2592array_state_show(mddev_t *mddev, char *page)
2593{
2594 enum array_state st = inactive;
2595
2596 if (mddev->pers)
2597 switch(mddev->ro) {
2598 case 1:
2599 st = readonly;
2600 break;
2601 case 2:
2602 st = read_auto;
2603 break;
2604 case 0:
2605 if (mddev->in_sync)
2606 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002607 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2608 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002609 else if (mddev->safemode)
2610 st = active_idle;
2611 else
2612 st = active;
2613 }
2614 else {
2615 if (list_empty(&mddev->disks) &&
2616 mddev->raid_disks == 0 &&
2617 mddev->size == 0)
2618 st = clear;
2619 else
2620 st = inactive;
2621 }
2622 return sprintf(page, "%s\n", array_states[st]);
2623}
2624
2625static int do_md_stop(mddev_t * mddev, int ro);
2626static int do_md_run(mddev_t * mddev);
2627static int restart_array(mddev_t *mddev);
2628
2629static ssize_t
2630array_state_store(mddev_t *mddev, const char *buf, size_t len)
2631{
2632 int err = -EINVAL;
2633 enum array_state st = match_word(buf, array_states);
2634 switch(st) {
2635 case bad_word:
2636 break;
2637 case clear:
2638 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002639 if (atomic_read(&mddev->active) > 1)
2640 return -EBUSY;
2641 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002642 break;
2643 case inactive:
2644 /* stopping an active array */
2645 if (mddev->pers) {
2646 if (atomic_read(&mddev->active) > 1)
2647 return -EBUSY;
2648 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002649 } else
2650 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002651 break;
2652 case suspended:
2653 break; /* not supported yet */
2654 case readonly:
2655 if (mddev->pers)
2656 err = do_md_stop(mddev, 1);
2657 else {
2658 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002659 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002660 err = do_md_run(mddev);
2661 }
2662 break;
2663 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002664 if (mddev->pers) {
NeilBrown648b6292008-04-30 00:52:30 -07002665 if (mddev->ro != 1)
2666 err = do_md_stop(mddev, 1);
2667 else
2668 err = restart_array(mddev);
2669 if (err == 0) {
2670 mddev->ro = 2;
2671 set_disk_ro(mddev->gendisk, 0);
2672 }
NeilBrown9e653b62006-06-26 00:27:58 -07002673 } else {
2674 mddev->ro = 2;
2675 err = do_md_run(mddev);
2676 }
2677 break;
2678 case clean:
2679 if (mddev->pers) {
2680 restart_array(mddev);
2681 spin_lock_irq(&mddev->write_lock);
2682 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002683 if (mddev->in_sync == 0) {
2684 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002685 if (mddev->safemode == 1)
2686 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002687 if (mddev->persistent)
2688 set_bit(MD_CHANGE_CLEAN,
2689 &mddev->flags);
2690 }
2691 err = 0;
2692 } else
2693 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002694 spin_unlock_irq(&mddev->write_lock);
2695 } else {
2696 mddev->ro = 0;
2697 mddev->recovery_cp = MaxSector;
2698 err = do_md_run(mddev);
2699 }
2700 break;
2701 case active:
2702 if (mddev->pers) {
2703 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002704 if (mddev->external)
2705 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002706 wake_up(&mddev->sb_wait);
2707 err = 0;
2708 } else {
2709 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002710 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002711 err = do_md_run(mddev);
2712 }
2713 break;
2714 case write_pending:
2715 case active_idle:
2716 /* these cannot be set */
2717 break;
2718 }
2719 if (err)
2720 return err;
2721 else
2722 return len;
2723}
NeilBrown80ca3a42006-07-10 04:44:18 -07002724static struct md_sysfs_entry md_array_state =
2725__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002726
NeilBrown6d7ff7382006-01-06 00:21:16 -08002727static ssize_t
2728null_show(mddev_t *mddev, char *page)
2729{
2730 return -EINVAL;
2731}
2732
2733static ssize_t
2734new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2735{
2736 /* buf must be %d:%d\n? giving major and minor numbers */
2737 /* The new device is added to the array.
2738 * If the array has a persistent superblock, we read the
2739 * superblock to initialise info and check validity.
2740 * Otherwise, only checking done is that in bind_rdev_to_array,
2741 * which mainly checks size.
2742 */
2743 char *e;
2744 int major = simple_strtoul(buf, &e, 10);
2745 int minor;
2746 dev_t dev;
2747 mdk_rdev_t *rdev;
2748 int err;
2749
2750 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2751 return -EINVAL;
2752 minor = simple_strtoul(e+1, &e, 10);
2753 if (*e && *e != '\n')
2754 return -EINVAL;
2755 dev = MKDEV(major, minor);
2756 if (major != MAJOR(dev) ||
2757 minor != MINOR(dev))
2758 return -EOVERFLOW;
2759
2760
2761 if (mddev->persistent) {
2762 rdev = md_import_device(dev, mddev->major_version,
2763 mddev->minor_version);
2764 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2765 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2766 mdk_rdev_t, same_set);
2767 err = super_types[mddev->major_version]
2768 .load_super(rdev, rdev0, mddev->minor_version);
2769 if (err < 0)
2770 goto out;
2771 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002772 } else if (mddev->external)
2773 rdev = md_import_device(dev, -2, -1);
2774 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08002775 rdev = md_import_device(dev, -1, -1);
2776
2777 if (IS_ERR(rdev))
2778 return PTR_ERR(rdev);
2779 err = bind_rdev_to_array(rdev, mddev);
2780 out:
2781 if (err)
2782 export_rdev(rdev);
2783 return err ? err : len;
2784}
2785
2786static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002787__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002788
2789static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002790bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2791{
2792 char *end;
2793 unsigned long chunk, end_chunk;
2794
2795 if (!mddev->bitmap)
2796 goto out;
2797 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2798 while (*buf) {
2799 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2800 if (buf == end) break;
2801 if (*end == '-') { /* range */
2802 buf = end + 1;
2803 end_chunk = simple_strtoul(buf, &end, 0);
2804 if (buf == end) break;
2805 }
2806 if (*end && !isspace(*end)) break;
2807 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2808 buf = end;
2809 while (isspace(*buf)) buf++;
2810 }
2811 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2812out:
2813 return len;
2814}
2815
2816static struct md_sysfs_entry md_bitmap =
2817__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2818
2819static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002820size_show(mddev_t *mddev, char *page)
2821{
2822 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2823}
2824
2825static int update_size(mddev_t *mddev, unsigned long size);
2826
2827static ssize_t
2828size_store(mddev_t *mddev, const char *buf, size_t len)
2829{
2830 /* If array is inactive, we can reduce the component size, but
2831 * not increase it (except from 0).
2832 * If array is active, we can try an on-line resize
2833 */
2834 char *e;
2835 int err = 0;
2836 unsigned long long size = simple_strtoull(buf, &e, 10);
2837 if (!*buf || *buf == '\n' ||
2838 (*e && *e != '\n'))
2839 return -EINVAL;
2840
2841 if (mddev->pers) {
2842 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002843 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002844 } else {
2845 if (mddev->size == 0 ||
2846 mddev->size > size)
2847 mddev->size = size;
2848 else
2849 err = -ENOSPC;
2850 }
2851 return err ? err : len;
2852}
2853
2854static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002855__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002856
NeilBrown8bb93aa2006-01-06 00:20:50 -08002857
2858/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002859 * This is one of
2860 * 'none' for arrays with no metadata (good luck...)
2861 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002862 * or N.M for internally known formats
2863 */
2864static ssize_t
2865metadata_show(mddev_t *mddev, char *page)
2866{
2867 if (mddev->persistent)
2868 return sprintf(page, "%d.%d\n",
2869 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002870 else if (mddev->external)
2871 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002872 else
2873 return sprintf(page, "none\n");
2874}
2875
2876static ssize_t
2877metadata_store(mddev_t *mddev, const char *buf, size_t len)
2878{
2879 int major, minor;
2880 char *e;
2881 if (!list_empty(&mddev->disks))
2882 return -EBUSY;
2883
2884 if (cmd_match(buf, "none")) {
2885 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002886 mddev->external = 0;
2887 mddev->major_version = 0;
2888 mddev->minor_version = 90;
2889 return len;
2890 }
2891 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002892 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002893 if (namelen >= sizeof(mddev->metadata_type))
2894 namelen = sizeof(mddev->metadata_type)-1;
2895 strncpy(mddev->metadata_type, buf+9, namelen);
2896 mddev->metadata_type[namelen] = 0;
2897 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2898 mddev->metadata_type[--namelen] = 0;
2899 mddev->persistent = 0;
2900 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002901 mddev->major_version = 0;
2902 mddev->minor_version = 90;
2903 return len;
2904 }
2905 major = simple_strtoul(buf, &e, 10);
2906 if (e==buf || *e != '.')
2907 return -EINVAL;
2908 buf = e+1;
2909 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002910 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002911 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002912 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002913 return -ENOENT;
2914 mddev->major_version = major;
2915 mddev->minor_version = minor;
2916 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002917 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002918 return len;
2919}
2920
2921static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002922__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002923
NeilBrowna35b0d62006-01-06 00:20:49 -08002924static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002925action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002926{
NeilBrown7eec3142005-11-08 21:39:44 -08002927 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002928 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002929 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002930 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2931 type = "reshape";
2932 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002933 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2934 type = "resync";
2935 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2936 type = "check";
2937 else
2938 type = "repair";
2939 } else
2940 type = "recover";
2941 }
2942 return sprintf(page, "%s\n", type);
2943}
2944
2945static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002946action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002947{
NeilBrown7eec3142005-11-08 21:39:44 -08002948 if (!mddev->pers || !mddev->pers->sync_request)
2949 return -EINVAL;
2950
NeilBrownbce74da2006-01-06 00:20:41 -08002951 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002952 if (mddev->sync_thread) {
2953 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2954 md_unregister_thread(mddev->sync_thread);
2955 mddev->sync_thread = NULL;
2956 mddev->recovery = 0;
2957 }
NeilBrown03c902e2006-01-06 00:20:46 -08002958 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2959 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002960 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002961 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002962 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002963 else if (cmd_match(page, "reshape")) {
2964 int err;
2965 if (mddev->pers->start_reshape == NULL)
2966 return -EINVAL;
2967 err = mddev->pers->start_reshape(mddev);
2968 if (err)
2969 return err;
2970 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002971 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002972 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002973 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002974 return -EINVAL;
2975 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2976 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002977 }
NeilBrown03c902e2006-01-06 00:20:46 -08002978 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002979 md_wakeup_thread(mddev->thread);
2980 return len;
2981}
2982
NeilBrown9d888832005-11-08 21:39:26 -08002983static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002984mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002985{
2986 return sprintf(page, "%llu\n",
2987 (unsigned long long) mddev->resync_mismatches);
2988}
2989
NeilBrown80ca3a42006-07-10 04:44:18 -07002990static struct md_sysfs_entry md_scan_mode =
2991__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002992
NeilBrown96de1e62005-11-08 21:39:39 -08002993
NeilBrown80ca3a42006-07-10 04:44:18 -07002994static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002995
NeilBrown88202a02006-01-06 00:21:36 -08002996static ssize_t
2997sync_min_show(mddev_t *mddev, char *page)
2998{
2999 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3000 mddev->sync_speed_min ? "local": "system");
3001}
3002
3003static ssize_t
3004sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3005{
3006 int min;
3007 char *e;
3008 if (strncmp(buf, "system", 6)==0) {
3009 mddev->sync_speed_min = 0;
3010 return len;
3011 }
3012 min = simple_strtoul(buf, &e, 10);
3013 if (buf == e || (*e && *e != '\n') || min <= 0)
3014 return -EINVAL;
3015 mddev->sync_speed_min = min;
3016 return len;
3017}
3018
3019static struct md_sysfs_entry md_sync_min =
3020__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3021
3022static ssize_t
3023sync_max_show(mddev_t *mddev, char *page)
3024{
3025 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3026 mddev->sync_speed_max ? "local": "system");
3027}
3028
3029static ssize_t
3030sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3031{
3032 int max;
3033 char *e;
3034 if (strncmp(buf, "system", 6)==0) {
3035 mddev->sync_speed_max = 0;
3036 return len;
3037 }
3038 max = simple_strtoul(buf, &e, 10);
3039 if (buf == e || (*e && *e != '\n') || max <= 0)
3040 return -EINVAL;
3041 mddev->sync_speed_max = max;
3042 return len;
3043}
3044
3045static struct md_sysfs_entry md_sync_max =
3046__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3047
Iustin Popd7f3d292007-10-16 23:30:54 -07003048static ssize_t
3049degraded_show(mddev_t *mddev, char *page)
3050{
3051 return sprintf(page, "%d\n", mddev->degraded);
3052}
3053static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003054
3055static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003056sync_force_parallel_show(mddev_t *mddev, char *page)
3057{
3058 return sprintf(page, "%d\n", mddev->parallel_resync);
3059}
3060
3061static ssize_t
3062sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3063{
3064 long n;
3065
3066 if (strict_strtol(buf, 10, &n))
3067 return -EINVAL;
3068
3069 if (n != 0 && n != 1)
3070 return -EINVAL;
3071
3072 mddev->parallel_resync = n;
3073
3074 if (mddev->sync_thread)
3075 wake_up(&resync_wait);
3076
3077 return len;
3078}
3079
3080/* force parallel resync, even with shared block devices */
3081static struct md_sysfs_entry md_sync_force_parallel =
3082__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3083 sync_force_parallel_show, sync_force_parallel_store);
3084
3085static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003086sync_speed_show(mddev_t *mddev, char *page)
3087{
3088 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07003089 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08003090 dt = ((jiffies - mddev->resync_mark) / HZ);
3091 if (!dt) dt++;
3092 db = resync - (mddev->resync_mark_cnt);
3093 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
3094}
3095
NeilBrown80ca3a42006-07-10 04:44:18 -07003096static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003097
3098static ssize_t
3099sync_completed_show(mddev_t *mddev, char *page)
3100{
3101 unsigned long max_blocks, resync;
3102
3103 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3104 max_blocks = mddev->resync_max_sectors;
3105 else
3106 max_blocks = mddev->size << 1;
3107
3108 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3109 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3110}
3111
NeilBrown80ca3a42006-07-10 04:44:18 -07003112static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003113
NeilBrowne464eaf2006-03-27 01:18:14 -08003114static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003115min_sync_show(mddev_t *mddev, char *page)
3116{
3117 return sprintf(page, "%llu\n",
3118 (unsigned long long)mddev->resync_min);
3119}
3120static ssize_t
3121min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3122{
3123 unsigned long long min;
3124 if (strict_strtoull(buf, 10, &min))
3125 return -EINVAL;
3126 if (min > mddev->resync_max)
3127 return -EINVAL;
3128 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3129 return -EBUSY;
3130
3131 /* Must be a multiple of chunk_size */
3132 if (mddev->chunk_size) {
3133 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3134 return -EINVAL;
3135 }
3136 mddev->resync_min = min;
3137
3138 return len;
3139}
3140
3141static struct md_sysfs_entry md_min_sync =
3142__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3143
3144static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003145max_sync_show(mddev_t *mddev, char *page)
3146{
3147 if (mddev->resync_max == MaxSector)
3148 return sprintf(page, "max\n");
3149 else
3150 return sprintf(page, "%llu\n",
3151 (unsigned long long)mddev->resync_max);
3152}
3153static ssize_t
3154max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3155{
3156 if (strncmp(buf, "max", 3) == 0)
3157 mddev->resync_max = MaxSector;
3158 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003159 unsigned long long max;
3160 if (strict_strtoull(buf, 10, &max))
3161 return -EINVAL;
3162 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003163 return -EINVAL;
3164 if (max < mddev->resync_max &&
3165 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3166 return -EBUSY;
3167
3168 /* Must be a multiple of chunk_size */
3169 if (mddev->chunk_size) {
3170 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3171 return -EINVAL;
3172 }
3173 mddev->resync_max = max;
3174 }
3175 wake_up(&mddev->recovery_wait);
3176 return len;
3177}
3178
3179static struct md_sysfs_entry md_max_sync =
3180__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3181
3182static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003183suspend_lo_show(mddev_t *mddev, char *page)
3184{
3185 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3186}
3187
3188static ssize_t
3189suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3190{
3191 char *e;
3192 unsigned long long new = simple_strtoull(buf, &e, 10);
3193
3194 if (mddev->pers->quiesce == NULL)
3195 return -EINVAL;
3196 if (buf == e || (*e && *e != '\n'))
3197 return -EINVAL;
3198 if (new >= mddev->suspend_hi ||
3199 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3200 mddev->suspend_lo = new;
3201 mddev->pers->quiesce(mddev, 2);
3202 return len;
3203 } else
3204 return -EINVAL;
3205}
3206static struct md_sysfs_entry md_suspend_lo =
3207__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3208
3209
3210static ssize_t
3211suspend_hi_show(mddev_t *mddev, char *page)
3212{
3213 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3214}
3215
3216static ssize_t
3217suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3218{
3219 char *e;
3220 unsigned long long new = simple_strtoull(buf, &e, 10);
3221
3222 if (mddev->pers->quiesce == NULL)
3223 return -EINVAL;
3224 if (buf == e || (*e && *e != '\n'))
3225 return -EINVAL;
3226 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3227 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3228 mddev->suspend_hi = new;
3229 mddev->pers->quiesce(mddev, 1);
3230 mddev->pers->quiesce(mddev, 0);
3231 return len;
3232 } else
3233 return -EINVAL;
3234}
3235static struct md_sysfs_entry md_suspend_hi =
3236__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3237
NeilBrown08a02ec2007-05-09 02:35:38 -07003238static ssize_t
3239reshape_position_show(mddev_t *mddev, char *page)
3240{
3241 if (mddev->reshape_position != MaxSector)
3242 return sprintf(page, "%llu\n",
3243 (unsigned long long)mddev->reshape_position);
3244 strcpy(page, "none\n");
3245 return 5;
3246}
3247
3248static ssize_t
3249reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3250{
3251 char *e;
3252 unsigned long long new = simple_strtoull(buf, &e, 10);
3253 if (mddev->pers)
3254 return -EBUSY;
3255 if (buf == e || (*e && *e != '\n'))
3256 return -EINVAL;
3257 mddev->reshape_position = new;
3258 mddev->delta_disks = 0;
3259 mddev->new_level = mddev->level;
3260 mddev->new_layout = mddev->layout;
3261 mddev->new_chunk = mddev->chunk_size;
3262 return len;
3263}
3264
3265static struct md_sysfs_entry md_reshape_position =
3266__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3267 reshape_position_store);
3268
NeilBrowne464eaf2006-03-27 01:18:14 -08003269
NeilBrowneae17012005-11-08 21:39:23 -08003270static struct attribute *md_default_attrs[] = {
3271 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003272 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003273 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003274 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003275 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003276 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003277 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003278 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003279 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003280 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003281 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003282 NULL,
3283};
3284
3285static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003286 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003287 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003288 &md_sync_min.attr,
3289 &md_sync_max.attr,
3290 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003291 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003292 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003293 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003294 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003295 &md_suspend_lo.attr,
3296 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003297 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003298 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003299 NULL,
3300};
NeilBrown411036f2005-11-08 21:39:40 -08003301static struct attribute_group md_redundancy_group = {
3302 .name = NULL,
3303 .attrs = md_redundancy_attrs,
3304};
3305
NeilBrowneae17012005-11-08 21:39:23 -08003306
3307static ssize_t
3308md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3309{
3310 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3311 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003312 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003313
3314 if (!entry->show)
3315 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003316 rv = mddev_lock(mddev);
3317 if (!rv) {
3318 rv = entry->show(mddev, page);
3319 mddev_unlock(mddev);
3320 }
NeilBrown96de1e62005-11-08 21:39:39 -08003321 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003322}
3323
3324static ssize_t
3325md_attr_store(struct kobject *kobj, struct attribute *attr,
3326 const char *page, size_t length)
3327{
3328 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3329 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003330 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003331
3332 if (!entry->store)
3333 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003334 if (!capable(CAP_SYS_ADMIN))
3335 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003336 rv = mddev_lock(mddev);
3337 if (!rv) {
3338 rv = entry->store(mddev, page, length);
3339 mddev_unlock(mddev);
3340 }
NeilBrown96de1e62005-11-08 21:39:39 -08003341 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003342}
3343
3344static void md_free(struct kobject *ko)
3345{
3346 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3347 kfree(mddev);
3348}
3349
3350static struct sysfs_ops md_sysfs_ops = {
3351 .show = md_attr_show,
3352 .store = md_attr_store,
3353};
3354static struct kobj_type md_ktype = {
3355 .release = md_free,
3356 .sysfs_ops = &md_sysfs_ops,
3357 .default_attrs = md_default_attrs,
3358};
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360int mdp_major = 0;
3361
3362static struct kobject *md_probe(dev_t dev, int *part, void *data)
3363{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003364 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 mddev_t *mddev = mddev_find(dev);
3366 struct gendisk *disk;
3367 int partitioned = (MAJOR(dev) != MD_MAJOR);
3368 int shift = partitioned ? MdpMinorShift : 0;
3369 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003370 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
3372 if (!mddev)
3373 return NULL;
3374
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003375 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003377 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 mddev_put(mddev);
3379 return NULL;
3380 }
3381 disk = alloc_disk(1 << shift);
3382 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003383 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 mddev_put(mddev);
3385 return NULL;
3386 }
3387 disk->major = MAJOR(dev);
3388 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003389 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003391 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 disk->fops = &md_fops;
3394 disk->private_data = mddev;
3395 disk->queue = mddev->queue;
3396 add_disk(disk);
3397 mddev->gendisk = disk;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003398 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003399 "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003400 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003401 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003402 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3403 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003404 else
3405 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return NULL;
3407}
3408
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409static void md_safemode_timeout(unsigned long data)
3410{
3411 mddev_t *mddev = (mddev_t *) data;
3412
3413 mddev->safemode = 1;
3414 md_wakeup_thread(mddev->thread);
3415}
3416
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003417static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
3419static int do_md_run(mddev_t * mddev)
3420{
NeilBrown2604b702006-01-06 00:20:36 -08003421 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 int chunk_size;
3423 struct list_head *tmp;
3424 mdk_rdev_t *rdev;
3425 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003426 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 char b[BDEVNAME_SIZE];
3428
NeilBrowna757e642005-04-16 15:26:42 -07003429 if (list_empty(&mddev->disks))
3430 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
3433 if (mddev->pers)
3434 return -EBUSY;
3435
3436 /*
3437 * Analyze all RAID superblock(s)
3438 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003439 if (!mddev->raid_disks) {
3440 if (!mddev->persistent)
3441 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003442 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
NeilBrown2604b702006-01-06 00:20:36 -08003447 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 if (chunk_size > MAX_CHUNK_SIZE) {
3449 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3450 chunk_size, MAX_CHUNK_SIZE);
3451 return -EINVAL;
3452 }
3453 /*
3454 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3455 */
3456 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003457 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 return -EINVAL;
3459 }
3460 if (chunk_size < PAGE_SIZE) {
3461 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3462 chunk_size, PAGE_SIZE);
3463 return -EINVAL;
3464 }
3465
3466 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003467 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003468 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 continue;
3470 if (rdev->size < chunk_size / 1024) {
3471 printk(KERN_WARNING
3472 "md: Dev %s smaller than chunk_size:"
3473 " %lluk < %dk\n",
3474 bdevname(rdev->bdev,b),
3475 (unsigned long long)rdev->size,
3476 chunk_size / 1024);
3477 return -EINVAL;
3478 }
3479 }
3480 }
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003483 if (mddev->level != LEVEL_NONE)
3484 request_module("md-level-%d", mddev->level);
3485 else if (mddev->clevel[0])
3486 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487#endif
3488
3489 /*
3490 * Drop all container device buffers, from now on
3491 * the only valid external interface is through the md
3492 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003494 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003495 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 continue;
3497 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003498 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003499
3500 /* perform some consistency tests on the device.
3501 * We don't want the data to overlap the metadata,
3502 * Internal Bitmap issues has handled elsewhere.
3503 */
3504 if (rdev->data_offset < rdev->sb_offset) {
3505 if (mddev->size &&
3506 rdev->data_offset + mddev->size*2
3507 > rdev->sb_offset*2) {
3508 printk("md: %s: data overlaps metadata\n",
3509 mdname(mddev));
3510 return -EINVAL;
3511 }
3512 } else {
3513 if (rdev->sb_offset*2 + rdev->sb_size/512
3514 > rdev->data_offset) {
3515 printk("md: %s: metadata overlaps data\n",
3516 mdname(mddev));
3517 return -EINVAL;
3518 }
3519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 }
3521
3522 md_probe(mddev->unit, NULL, NULL);
3523 disk = mddev->gendisk;
3524 if (!disk)
3525 return -ENOMEM;
3526
3527 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003528 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003529 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003531 if (mddev->level != LEVEL_NONE)
3532 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3533 mddev->level);
3534 else
3535 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3536 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 return -EINVAL;
3538 }
NeilBrown2604b702006-01-06 00:20:36 -08003539 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003541 mddev->level = pers->level;
3542 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543
NeilBrownf6705572006-03-27 01:18:11 -08003544 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003545 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003546 /* This personality cannot handle reshaping... */
3547 mddev->pers = NULL;
3548 module_put(pers->owner);
3549 return -EINVAL;
3550 }
3551
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003552 if (pers->sync_request) {
3553 /* Warn if this is a potentially silly
3554 * configuration.
3555 */
3556 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3557 mdk_rdev_t *rdev2;
3558 struct list_head *tmp2;
3559 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003560 rdev_for_each(rdev, tmp, mddev) {
3561 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003562 if (rdev < rdev2 &&
3563 rdev->bdev->bd_contains ==
3564 rdev2->bdev->bd_contains) {
3565 printk(KERN_WARNING
3566 "%s: WARNING: %s appears to be"
3567 " on the same physical disk as"
3568 " %s.\n",
3569 mdname(mddev),
3570 bdevname(rdev->bdev,b),
3571 bdevname(rdev2->bdev,b2));
3572 warned = 1;
3573 }
3574 }
3575 }
3576 if (warned)
3577 printk(KERN_WARNING
3578 "True protection against single-disk"
3579 " failure might be compromised.\n");
3580 }
3581
NeilBrown657390d2005-08-26 18:34:16 -07003582 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003584 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003585 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
NeilBrownf91de922005-11-08 21:39:36 -08003587 if (start_readonly)
3588 mddev->ro = 2; /* read-only, but switch on first write */
3589
NeilBrownb15c2e52006-01-06 00:20:16 -08003590 err = mddev->pers->run(mddev);
3591 if (!err && mddev->pers->sync_request) {
3592 err = bitmap_create(mddev);
3593 if (err) {
3594 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3595 mdname(mddev), err);
3596 mddev->pers->stop(mddev);
3597 }
3598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 if (err) {
3600 printk(KERN_ERR "md: pers->run() failed ...\n");
3601 module_put(mddev->pers->owner);
3602 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003603 bitmap_destroy(mddev);
3604 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003606 if (mddev->pers->sync_request) {
3607 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3608 printk(KERN_WARNING
3609 "md: cannot register extra attributes for %s\n",
3610 mdname(mddev));
3611 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003612 mddev->ro = 0;
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 atomic_set(&mddev->writes_pending,0);
3615 mddev->safemode = 0;
3616 mddev->safemode_timer.function = md_safemode_timeout;
3617 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003618 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003620
NeilBrownd089c6a2008-02-06 01:39:59 -08003621 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003622 if (rdev->raid_disk >= 0) {
3623 char nm[20];
3624 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003625 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3626 printk("md: cannot register %s for %s\n",
3627 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3631
NeilBrown850b2b42006-10-03 01:15:46 -07003632 if (mddev->flags)
3633 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 set_capacity(disk, mddev->array_size<<1);
3636
3637 /* If we call blk_queue_make_request here, it will
3638 * re-initialise max_sectors etc which may have been
3639 * refined inside -> run. So just set the bits we need to set.
3640 * Most initialisation happended when we called
3641 * blk_queue_make_request(..., md_fail_request)
3642 * earlier.
3643 */
3644 mddev->queue->queuedata = mddev;
3645 mddev->queue->make_request_fn = mddev->pers->make_request;
3646
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003647 /* If there is a partially-recovered drive we need to
3648 * start recovery here. If we leave it to md_check_recovery,
3649 * it will remove the drives and not do the right thing
3650 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003651 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003652 struct list_head *rtmp;
3653 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003654 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003655 if (rdev->raid_disk >= 0 &&
3656 !test_bit(In_sync, &rdev->flags) &&
3657 !test_bit(Faulty, &rdev->flags))
3658 /* complete an interrupted recovery */
3659 spares++;
3660 if (spares && mddev->pers->sync_request) {
3661 mddev->recovery = 0;
3662 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3663 mddev->sync_thread = md_register_thread(md_do_sync,
3664 mddev,
3665 "%s_resync");
3666 if (!mddev->sync_thread) {
3667 printk(KERN_ERR "%s: could not start resync"
3668 " thread...\n",
3669 mdname(mddev));
3670 /* leave the spares where they are, it shouldn't hurt */
3671 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003672 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003673 }
3674 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003675 md_wakeup_thread(mddev->thread);
3676 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003677
Linus Torvalds44ce62942007-05-09 18:51:36 -07003678 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003679 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003680 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 return 0;
3682}
3683
3684static int restart_array(mddev_t *mddev)
3685{
3686 struct gendisk *disk = mddev->gendisk;
3687 int err;
3688
3689 /*
3690 * Complain if it has no devices
3691 */
3692 err = -ENXIO;
3693 if (list_empty(&mddev->disks))
3694 goto out;
3695
3696 if (mddev->pers) {
3697 err = -EBUSY;
3698 if (!mddev->ro)
3699 goto out;
3700
3701 mddev->safemode = 0;
3702 mddev->ro = 0;
3703 set_disk_ro(disk, 0);
3704
3705 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3706 mdname(mddev));
3707 /*
3708 * Kick recovery or resync if necessary
3709 */
3710 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3711 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003712 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003714 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717out:
3718 return err;
3719}
3720
NeilBrownacc55e22006-06-26 00:27:47 -07003721/* similar to deny_write_access, but accounts for our holding a reference
3722 * to the file ourselves */
3723static int deny_bitmap_write_access(struct file * file)
3724{
3725 struct inode *inode = file->f_mapping->host;
3726
3727 spin_lock(&inode->i_lock);
3728 if (atomic_read(&inode->i_writecount) > 1) {
3729 spin_unlock(&inode->i_lock);
3730 return -ETXTBSY;
3731 }
3732 atomic_set(&inode->i_writecount, -1);
3733 spin_unlock(&inode->i_lock);
3734
3735 return 0;
3736}
3737
3738static void restore_bitmap_write_access(struct file *file)
3739{
3740 struct inode *inode = file->f_mapping->host;
3741
3742 spin_lock(&inode->i_lock);
3743 atomic_set(&inode->i_writecount, 1);
3744 spin_unlock(&inode->i_lock);
3745}
3746
NeilBrown9e653b62006-06-26 00:27:58 -07003747/* mode:
3748 * 0 - completely stop and dis-assemble array
3749 * 1 - switch to readonly
3750 * 2 - stop but do not disassemble array
3751 */
3752static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 int err = 0;
3755 struct gendisk *disk = mddev->gendisk;
3756
3757 if (mddev->pers) {
3758 if (atomic_read(&mddev->active)>2) {
3759 printk("md: %s still in use.\n",mdname(mddev));
3760 return -EBUSY;
3761 }
3762
3763 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003764 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3766 md_unregister_thread(mddev->sync_thread);
3767 mddev->sync_thread = NULL;
3768 }
3769
3770 del_timer_sync(&mddev->safemode_timer);
3771
3772 invalidate_partition(disk, 0);
3773
NeilBrown9e653b62006-06-26 00:27:58 -07003774 switch(mode) {
3775 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003777 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 goto out;
3779 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003780 break;
3781 case 0: /* disassemble */
3782 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003783 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003784 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 if (mddev->ro)
3786 set_disk_ro(disk, 0);
3787 blk_queue_make_request(mddev->queue, md_fail_request);
3788 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003789 mddev->queue->merge_bvec_fn = NULL;
3790 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003791 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003792 if (mddev->pers->sync_request)
3793 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3794
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 module_put(mddev->pers->owner);
3796 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003797 /* tell userspace to handle 'inactive' */
3798 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003799
3800 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003801 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 if (mddev->ro)
3804 mddev->ro = 0;
3805 }
NeilBrown850b2b42006-10-03 01:15:46 -07003806 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 /* mark array as shutdown cleanly */
3808 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003809 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 }
NeilBrown9e653b62006-06-26 00:27:58 -07003811 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003813 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 }
NeilBrown32a76272005-06-21 17:17:14 -07003815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 /*
3817 * Free resources if final stop
3818 */
NeilBrown9e653b62006-06-26 00:27:58 -07003819 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003820 mdk_rdev_t *rdev;
3821 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3824
NeilBrown978f9462006-02-02 14:28:05 -08003825 bitmap_destroy(mddev);
3826 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003827 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003828 fput(mddev->bitmap_file);
3829 mddev->bitmap_file = NULL;
3830 }
3831 mddev->bitmap_offset = 0;
3832
NeilBrownd089c6a2008-02-06 01:39:59 -08003833 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003834 if (rdev->raid_disk >= 0) {
3835 char nm[20];
3836 sprintf(nm, "rd%d", rdev->raid_disk);
3837 sysfs_remove_link(&mddev->kobj, nm);
3838 }
3839
NeilBrown177a99b2008-02-06 01:39:56 -08003840 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003841 flush_scheduled_work();
3842
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 export_array(mddev);
3844
3845 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003846 mddev->size = 0;
3847 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003848 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003849 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003850 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003851 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003852 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003853 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003854 mddev->level = LEVEL_NONE;
3855 mddev->clevel[0] = 0;
3856 mddev->flags = 0;
3857 mddev->ro = 0;
3858 mddev->metadata_type[0] = 0;
3859 mddev->chunk_size = 0;
3860 mddev->ctime = mddev->utime = 0;
3861 mddev->layout = 0;
3862 mddev->max_disks = 0;
3863 mddev->events = 0;
3864 mddev->delta_disks = 0;
3865 mddev->new_level = LEVEL_NONE;
3866 mddev->new_layout = 0;
3867 mddev->new_chunk = 0;
3868 mddev->curr_resync = 0;
3869 mddev->resync_mismatches = 0;
3870 mddev->suspend_lo = mddev->suspend_hi = 0;
3871 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3872 mddev->recovery = 0;
3873 mddev->in_sync = 0;
3874 mddev->changed = 0;
3875 mddev->degraded = 0;
3876 mddev->barriers_work = 0;
3877 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003878
NeilBrowna8a55c32006-06-26 00:27:39 -07003879 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3881 mdname(mddev));
3882 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003883 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884out:
3885 return err;
3886}
3887
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003888#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889static void autorun_array(mddev_t *mddev)
3890{
3891 mdk_rdev_t *rdev;
3892 struct list_head *tmp;
3893 int err;
3894
NeilBrowna757e642005-04-16 15:26:42 -07003895 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897
3898 printk(KERN_INFO "md: running: ");
3899
NeilBrownd089c6a2008-02-06 01:39:59 -08003900 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 char b[BDEVNAME_SIZE];
3902 printk("<%s>", bdevname(rdev->bdev,b));
3903 }
3904 printk("\n");
3905
3906 err = do_md_run (mddev);
3907 if (err) {
3908 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3909 do_md_stop (mddev, 0);
3910 }
3911}
3912
3913/*
3914 * lets try to run arrays based on all disks that have arrived
3915 * until now. (those are in pending_raid_disks)
3916 *
3917 * the method: pick the first pending disk, collect all disks with
3918 * the same UUID, remove all from the pending list and put them into
3919 * the 'same_array' list. Then order this list based on superblock
3920 * update time (freshest comes first), kick out 'old' disks and
3921 * compare superblocks. If everything's fine then run it.
3922 *
3923 * If "unit" is allocated, then bump its reference count
3924 */
3925static void autorun_devices(int part)
3926{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 struct list_head *tmp;
3928 mdk_rdev_t *rdev0, *rdev;
3929 mddev_t *mddev;
3930 char b[BDEVNAME_SIZE];
3931
3932 printk(KERN_INFO "md: autorun ...\n");
3933 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003934 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003936 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 rdev0 = list_entry(pending_raid_disks.next,
3938 mdk_rdev_t, same_set);
3939
3940 printk(KERN_INFO "md: considering %s ...\n",
3941 bdevname(rdev0->bdev,b));
3942 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003943 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 if (super_90_load(rdev, rdev0, 0) >= 0) {
3945 printk(KERN_INFO "md: adding %s ...\n",
3946 bdevname(rdev->bdev,b));
3947 list_move(&rdev->same_set, &candidates);
3948 }
3949 /*
3950 * now we have a set of devices, with all of them having
3951 * mostly sane superblocks. It's time to allocate the
3952 * mddev.
3953 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003954 if (part) {
3955 dev = MKDEV(mdp_major,
3956 rdev0->preferred_minor << MdpMinorShift);
3957 unit = MINOR(dev) >> MdpMinorShift;
3958 } else {
3959 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3960 unit = MINOR(dev);
3961 }
3962 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3964 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3965 break;
3966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967
3968 md_probe(dev, NULL, NULL);
3969 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10003970 if (!mddev || !mddev->gendisk) {
3971 if (mddev)
3972 mddev_put(mddev);
3973 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 "md: cannot allocate memory for md drive.\n");
3975 break;
3976 }
3977 if (mddev_lock(mddev))
3978 printk(KERN_WARNING "md: %s locked, cannot run\n",
3979 mdname(mddev));
3980 else if (mddev->raid_disks || mddev->major_version
3981 || !list_empty(&mddev->disks)) {
3982 printk(KERN_WARNING
3983 "md: %s already running, cannot run %s\n",
3984 mdname(mddev), bdevname(rdev0->bdev,b));
3985 mddev_unlock(mddev);
3986 } else {
3987 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08003988 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08003989 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 list_del_init(&rdev->same_set);
3991 if (bind_rdev_to_array(rdev, mddev))
3992 export_rdev(rdev);
3993 }
3994 autorun_array(mddev);
3995 mddev_unlock(mddev);
3996 }
3997 /* on success, candidates will be empty, on error
3998 * it won't...
3999 */
NeilBrown73c34432008-02-06 01:39:59 -08004000 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 export_rdev(rdev);
4002 mddev_put(mddev);
4003 }
4004 printk(KERN_INFO "md: ... autorun DONE.\n");
4005}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004006#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008static int get_version(void __user * arg)
4009{
4010 mdu_version_t ver;
4011
4012 ver.major = MD_MAJOR_VERSION;
4013 ver.minor = MD_MINOR_VERSION;
4014 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4015
4016 if (copy_to_user(arg, &ver, sizeof(ver)))
4017 return -EFAULT;
4018
4019 return 0;
4020}
4021
4022static int get_array_info(mddev_t * mddev, void __user * arg)
4023{
4024 mdu_array_info_t info;
4025 int nr,working,active,failed,spare;
4026 mdk_rdev_t *rdev;
4027 struct list_head *tmp;
4028
4029 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004030 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004032 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 failed++;
4034 else {
4035 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004036 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 active++;
4038 else
4039 spare++;
4040 }
4041 }
4042
4043 info.major_version = mddev->major_version;
4044 info.minor_version = mddev->minor_version;
4045 info.patch_version = MD_PATCHLEVEL_VERSION;
4046 info.ctime = mddev->ctime;
4047 info.level = mddev->level;
4048 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004049 if (info.size != mddev->size) /* overflow */
4050 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 info.nr_disks = nr;
4052 info.raid_disks = mddev->raid_disks;
4053 info.md_minor = mddev->md_minor;
4054 info.not_persistent= !mddev->persistent;
4055
4056 info.utime = mddev->utime;
4057 info.state = 0;
4058 if (mddev->in_sync)
4059 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004060 if (mddev->bitmap && mddev->bitmap_offset)
4061 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 info.active_disks = active;
4063 info.working_disks = working;
4064 info.failed_disks = failed;
4065 info.spare_disks = spare;
4066
4067 info.layout = mddev->layout;
4068 info.chunk_size = mddev->chunk_size;
4069
4070 if (copy_to_user(arg, &info, sizeof(info)))
4071 return -EFAULT;
4072
4073 return 0;
4074}
4075
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004076static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004077{
4078 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4079 char *ptr, *buf = NULL;
4080 int err = -ENOMEM;
4081
NeilBrown2a2275d2007-01-26 00:57:11 -08004082 md_allow_write(mddev);
4083
NeilBrown32a76272005-06-21 17:17:14 -07004084 file = kmalloc(sizeof(*file), GFP_KERNEL);
4085 if (!file)
4086 goto out;
4087
4088 /* bitmap disabled, zero the first byte and copy out */
4089 if (!mddev->bitmap || !mddev->bitmap->file) {
4090 file->pathname[0] = '\0';
4091 goto copy_out;
4092 }
4093
4094 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4095 if (!buf)
4096 goto out;
4097
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004098 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4099 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004100 goto out;
4101
4102 strcpy(file->pathname, ptr);
4103
4104copy_out:
4105 err = 0;
4106 if (copy_to_user(arg, file, sizeof(*file)))
4107 err = -EFAULT;
4108out:
4109 kfree(buf);
4110 kfree(file);
4111 return err;
4112}
4113
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114static int get_disk_info(mddev_t * mddev, void __user * arg)
4115{
4116 mdu_disk_info_t info;
4117 unsigned int nr;
4118 mdk_rdev_t *rdev;
4119
4120 if (copy_from_user(&info, arg, sizeof(info)))
4121 return -EFAULT;
4122
4123 nr = info.number;
4124
4125 rdev = find_rdev_nr(mddev, nr);
4126 if (rdev) {
4127 info.major = MAJOR(rdev->bdev->bd_dev);
4128 info.minor = MINOR(rdev->bdev->bd_dev);
4129 info.raid_disk = rdev->raid_disk;
4130 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004131 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004133 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 info.state |= (1<<MD_DISK_ACTIVE);
4135 info.state |= (1<<MD_DISK_SYNC);
4136 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004137 if (test_bit(WriteMostly, &rdev->flags))
4138 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 } else {
4140 info.major = info.minor = 0;
4141 info.raid_disk = -1;
4142 info.state = (1<<MD_DISK_REMOVED);
4143 }
4144
4145 if (copy_to_user(arg, &info, sizeof(info)))
4146 return -EFAULT;
4147
4148 return 0;
4149}
4150
4151static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4152{
4153 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4154 mdk_rdev_t *rdev;
4155 dev_t dev = MKDEV(info->major,info->minor);
4156
4157 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4158 return -EOVERFLOW;
4159
4160 if (!mddev->raid_disks) {
4161 int err;
4162 /* expecting a device which has a superblock */
4163 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4164 if (IS_ERR(rdev)) {
4165 printk(KERN_WARNING
4166 "md: md_import_device returned %ld\n",
4167 PTR_ERR(rdev));
4168 return PTR_ERR(rdev);
4169 }
4170 if (!list_empty(&mddev->disks)) {
4171 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4172 mdk_rdev_t, same_set);
4173 int err = super_types[mddev->major_version]
4174 .load_super(rdev, rdev0, mddev->minor_version);
4175 if (err < 0) {
4176 printk(KERN_WARNING
4177 "md: %s has different UUID to %s\n",
4178 bdevname(rdev->bdev,b),
4179 bdevname(rdev0->bdev,b2));
4180 export_rdev(rdev);
4181 return -EINVAL;
4182 }
4183 }
4184 err = bind_rdev_to_array(rdev, mddev);
4185 if (err)
4186 export_rdev(rdev);
4187 return err;
4188 }
4189
4190 /*
4191 * add_new_disk can be used once the array is assembled
4192 * to add "hot spares". They must already have a superblock
4193 * written
4194 */
4195 if (mddev->pers) {
4196 int err;
4197 if (!mddev->pers->hot_add_disk) {
4198 printk(KERN_WARNING
4199 "%s: personality does not support diskops!\n",
4200 mdname(mddev));
4201 return -EINVAL;
4202 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004203 if (mddev->persistent)
4204 rdev = md_import_device(dev, mddev->major_version,
4205 mddev->minor_version);
4206 else
4207 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 if (IS_ERR(rdev)) {
4209 printk(KERN_WARNING
4210 "md: md_import_device returned %ld\n",
4211 PTR_ERR(rdev));
4212 return PTR_ERR(rdev);
4213 }
NeilBrown41158c72005-06-21 17:17:25 -07004214 /* set save_raid_disk if appropriate */
4215 if (!mddev->persistent) {
4216 if (info->state & (1<<MD_DISK_SYNC) &&
4217 info->raid_disk < mddev->raid_disks)
4218 rdev->raid_disk = info->raid_disk;
4219 else
4220 rdev->raid_disk = -1;
4221 } else
4222 super_types[mddev->major_version].
4223 validate_super(mddev, rdev);
4224 rdev->saved_raid_disk = rdev->raid_disk;
4225
NeilBrownb2d444d2005-11-08 21:39:31 -08004226 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004227 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4228 set_bit(WriteMostly, &rdev->flags);
4229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 rdev->raid_disk = -1;
4231 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004232 if (!err && !mddev->pers->hot_remove_disk) {
4233 /* If there is hot_add_disk but no hot_remove_disk
4234 * then added disks for geometry changes,
4235 * and should be added immediately.
4236 */
4237 super_types[mddev->major_version].
4238 validate_super(mddev, rdev);
4239 err = mddev->pers->hot_add_disk(mddev, rdev);
4240 if (err)
4241 unbind_rdev_from_array(rdev);
4242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 if (err)
4244 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004245
NeilBrown17571282006-12-10 02:20:52 -08004246 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07004247 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004248 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 return err;
4250 }
4251
4252 /* otherwise, add_new_disk is only allowed
4253 * for major_version==0 superblocks
4254 */
4255 if (mddev->major_version != 0) {
4256 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4257 mdname(mddev));
4258 return -EINVAL;
4259 }
4260
4261 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4262 int err;
4263 rdev = md_import_device (dev, -1, 0);
4264 if (IS_ERR(rdev)) {
4265 printk(KERN_WARNING
4266 "md: error, md_import_device() returned %ld\n",
4267 PTR_ERR(rdev));
4268 return PTR_ERR(rdev);
4269 }
4270 rdev->desc_nr = info->number;
4271 if (info->raid_disk < mddev->raid_disks)
4272 rdev->raid_disk = info->raid_disk;
4273 else
4274 rdev->raid_disk = -1;
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004277 if (info->state & (1<<MD_DISK_SYNC))
4278 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004280 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4281 set_bit(WriteMostly, &rdev->flags);
4282
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (!mddev->persistent) {
4284 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4285 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4286 } else
4287 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4288 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4289
NeilBrown2bf071b2006-01-06 00:20:55 -08004290 err = bind_rdev_to_array(rdev, mddev);
4291 if (err) {
4292 export_rdev(rdev);
4293 return err;
4294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 }
4296
4297 return 0;
4298}
4299
4300static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4301{
4302 char b[BDEVNAME_SIZE];
4303 mdk_rdev_t *rdev;
4304
4305 if (!mddev->pers)
4306 return -ENODEV;
4307
4308 rdev = find_rdev(mddev, dev);
4309 if (!rdev)
4310 return -ENXIO;
4311
4312 if (rdev->raid_disk >= 0)
4313 goto busy;
4314
4315 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004316 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004317 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
4319 return 0;
4320busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004321 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 bdevname(rdev->bdev,b), mdname(mddev));
4323 return -EBUSY;
4324}
4325
4326static int hot_add_disk(mddev_t * mddev, dev_t dev)
4327{
4328 char b[BDEVNAME_SIZE];
4329 int err;
4330 unsigned int size;
4331 mdk_rdev_t *rdev;
4332
4333 if (!mddev->pers)
4334 return -ENODEV;
4335
4336 if (mddev->major_version != 0) {
4337 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4338 " version-0 superblocks.\n",
4339 mdname(mddev));
4340 return -EINVAL;
4341 }
4342 if (!mddev->pers->hot_add_disk) {
4343 printk(KERN_WARNING
4344 "%s: personality does not support diskops!\n",
4345 mdname(mddev));
4346 return -EINVAL;
4347 }
4348
4349 rdev = md_import_device (dev, -1, 0);
4350 if (IS_ERR(rdev)) {
4351 printk(KERN_WARNING
4352 "md: error, md_import_device() returned %ld\n",
4353 PTR_ERR(rdev));
4354 return -EINVAL;
4355 }
4356
4357 if (mddev->persistent)
4358 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4359 else
4360 rdev->sb_offset =
4361 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4362
4363 size = calc_dev_size(rdev, mddev->chunk_size);
4364 rdev->size = size;
4365
NeilBrownb2d444d2005-11-08 21:39:31 -08004366 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 printk(KERN_WARNING
4368 "md: can not hot-add faulty %s disk to %s!\n",
4369 bdevname(rdev->bdev,b), mdname(mddev));
4370 err = -EINVAL;
4371 goto abort_export;
4372 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004373 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004375 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004376 err = bind_rdev_to_array(rdev, mddev);
4377 if (err)
4378 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 /*
4381 * The rest should better be atomic, we can have disk failures
4382 * noticed in interrupt contexts ...
4383 */
4384
4385 if (rdev->desc_nr == mddev->max_disks) {
4386 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4387 mdname(mddev));
4388 err = -EBUSY;
4389 goto abort_unbind_export;
4390 }
4391
4392 rdev->raid_disk = -1;
4393
NeilBrown850b2b42006-10-03 01:15:46 -07004394 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
4396 /*
4397 * Kick recovery, maybe this spare has to be added to the
4398 * array immediately.
4399 */
4400 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4401 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004402 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return 0;
4404
4405abort_unbind_export:
4406 unbind_rdev_from_array(rdev);
4407
4408abort_export:
4409 export_rdev(rdev);
4410 return err;
4411}
4412
NeilBrown32a76272005-06-21 17:17:14 -07004413static int set_bitmap_file(mddev_t *mddev, int fd)
4414{
4415 int err;
4416
NeilBrown36fa3062005-09-09 16:23:45 -07004417 if (mddev->pers) {
4418 if (!mddev->pers->quiesce)
4419 return -EBUSY;
4420 if (mddev->recovery || mddev->sync_thread)
4421 return -EBUSY;
4422 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004423 }
4424
NeilBrown36fa3062005-09-09 16:23:45 -07004425
4426 if (fd >= 0) {
4427 if (mddev->bitmap)
4428 return -EEXIST; /* cannot add when bitmap is present */
4429 mddev->bitmap_file = fget(fd);
4430
4431 if (mddev->bitmap_file == NULL) {
4432 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4433 mdname(mddev));
4434 return -EBADF;
4435 }
4436
4437 err = deny_bitmap_write_access(mddev->bitmap_file);
4438 if (err) {
4439 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4440 mdname(mddev));
4441 fput(mddev->bitmap_file);
4442 mddev->bitmap_file = NULL;
4443 return err;
4444 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004445 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004446 } else if (mddev->bitmap == NULL)
4447 return -ENOENT; /* cannot remove what isn't there */
4448 err = 0;
4449 if (mddev->pers) {
4450 mddev->pers->quiesce(mddev, 1);
4451 if (fd >= 0)
4452 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004453 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004454 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004455 fd = -1; /* make sure to put the file */
4456 }
NeilBrown36fa3062005-09-09 16:23:45 -07004457 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004458 }
4459 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004460 if (mddev->bitmap_file) {
4461 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004462 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004463 }
NeilBrown36fa3062005-09-09 16:23:45 -07004464 mddev->bitmap_file = NULL;
4465 }
4466
NeilBrown32a76272005-06-21 17:17:14 -07004467 return err;
4468}
4469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470/*
4471 * set_array_info is used two different ways
4472 * The original usage is when creating a new array.
4473 * In this usage, raid_disks is > 0 and it together with
4474 * level, size, not_persistent,layout,chunksize determine the
4475 * shape of the array.
4476 * This will always create an array with a type-0.90.0 superblock.
4477 * The newer usage is when assembling an array.
4478 * In this case raid_disks will be 0, and the major_version field is
4479 * use to determine which style super-blocks are to be found on the devices.
4480 * The minor and patch _version numbers are also kept incase the
4481 * super_block handler wishes to interpret them.
4482 */
4483static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4484{
4485
4486 if (info->raid_disks == 0) {
4487 /* just setting version number for superblock loading */
4488 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004489 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 super_types[info->major_version].name == NULL) {
4491 /* maybe try to auto-load a module? */
4492 printk(KERN_INFO
4493 "md: superblock version %d not known\n",
4494 info->major_version);
4495 return -EINVAL;
4496 }
4497 mddev->major_version = info->major_version;
4498 mddev->minor_version = info->minor_version;
4499 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004500 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 return 0;
4502 }
4503 mddev->major_version = MD_MAJOR_VERSION;
4504 mddev->minor_version = MD_MINOR_VERSION;
4505 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4506 mddev->ctime = get_seconds();
4507
4508 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004509 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 mddev->size = info->size;
4511 mddev->raid_disks = info->raid_disks;
4512 /* don't set md_minor, it is determined by which /dev/md* was
4513 * openned
4514 */
4515 if (info->state & (1<<MD_SB_CLEAN))
4516 mddev->recovery_cp = MaxSector;
4517 else
4518 mddev->recovery_cp = 0;
4519 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004520 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 mddev->layout = info->layout;
4523 mddev->chunk_size = info->chunk_size;
4524
4525 mddev->max_disks = MD_SB_DISKS;
4526
NeilBrowne6910632008-02-06 01:39:51 -08004527 if (mddev->persistent)
4528 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004529 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
NeilBrownb2a27032005-11-28 13:44:12 -08004531 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4532 mddev->bitmap_offset = 0;
4533
NeilBrownf6705572006-03-27 01:18:11 -08004534 mddev->reshape_position = MaxSector;
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 /*
4537 * Generate a 128 bit UUID
4538 */
4539 get_random_bytes(mddev->uuid, 16);
4540
NeilBrownf6705572006-03-27 01:18:11 -08004541 mddev->new_level = mddev->level;
4542 mddev->new_chunk = mddev->chunk_size;
4543 mddev->new_layout = mddev->layout;
4544 mddev->delta_disks = 0;
4545
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 return 0;
4547}
4548
NeilBrowna35b0d62006-01-06 00:20:49 -08004549static int update_size(mddev_t *mddev, unsigned long size)
4550{
4551 mdk_rdev_t * rdev;
4552 int rv;
4553 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004554 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004555
4556 if (mddev->pers->resize == NULL)
4557 return -EINVAL;
4558 /* The "size" is the amount of each device that is used.
4559 * This can only make sense for arrays with redundancy.
4560 * linear and raid0 always use whatever space is available
4561 * We can only consider changing the size if no resync
4562 * or reconstruction is happening, and if the new size
4563 * is acceptable. It must fit before the sb_offset or,
4564 * if that is <data_offset, it must fit before the
4565 * size of each device.
4566 * If size is zero, we find the largest size that fits.
4567 */
4568 if (mddev->sync_thread)
4569 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004570 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004571 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004572 avail = rdev->size * 2;
4573
NeilBrowna35b0d62006-01-06 00:20:49 -08004574 if (fit && (size == 0 || size > avail/2))
4575 size = avail/2;
4576 if (avail < ((sector_t)size << 1))
4577 return -ENOSPC;
4578 }
4579 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4580 if (!rv) {
4581 struct block_device *bdev;
4582
4583 bdev = bdget_disk(mddev->gendisk, 0);
4584 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004585 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004586 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004587 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004588 bdput(bdev);
4589 }
4590 }
4591 return rv;
4592}
4593
NeilBrownda943b992006-01-06 00:20:54 -08004594static int update_raid_disks(mddev_t *mddev, int raid_disks)
4595{
4596 int rv;
4597 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004598 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004599 return -EINVAL;
4600 if (raid_disks <= 0 ||
4601 raid_disks >= mddev->max_disks)
4602 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004603 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004604 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004605 mddev->delta_disks = raid_disks - mddev->raid_disks;
4606
4607 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004608 return rv;
4609}
4610
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612/*
4613 * update_array_info is used to change the configuration of an
4614 * on-line array.
4615 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4616 * fields in the info are checked against the array.
4617 * Any differences that cannot be handled will cause an error.
4618 * Normally, only one change can be managed at a time.
4619 */
4620static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4621{
4622 int rv = 0;
4623 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004624 int state = 0;
4625
4626 /* calculate expected state,ignoring low bits */
4627 if (mddev->bitmap && mddev->bitmap_offset)
4628 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629
4630 if (mddev->major_version != info->major_version ||
4631 mddev->minor_version != info->minor_version ||
4632/* mddev->patch_version != info->patch_version || */
4633 mddev->ctime != info->ctime ||
4634 mddev->level != info->level ||
4635/* mddev->layout != info->layout || */
4636 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004637 mddev->chunk_size != info->chunk_size ||
4638 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4639 ((state^info->state) & 0xfffffe00)
4640 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 return -EINVAL;
4642 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004643 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 if (mddev->raid_disks != info->raid_disks) cnt++;
4645 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004646 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 if (cnt == 0) return 0;
4648 if (cnt > 1) return -EINVAL;
4649
4650 if (mddev->layout != info->layout) {
4651 /* Change layout
4652 * we don't need to do anything at the md level, the
4653 * personality will take care of it all.
4654 */
4655 if (mddev->pers->reconfig == NULL)
4656 return -EINVAL;
4657 else
4658 return mddev->pers->reconfig(mddev, info->layout, -1);
4659 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004660 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004661 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
NeilBrownda943b992006-01-06 00:20:54 -08004663 if (mddev->raid_disks != info->raid_disks)
4664 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
NeilBrown36fa3062005-09-09 16:23:45 -07004666 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4667 if (mddev->pers->quiesce == NULL)
4668 return -EINVAL;
4669 if (mddev->recovery || mddev->sync_thread)
4670 return -EBUSY;
4671 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4672 /* add the bitmap */
4673 if (mddev->bitmap)
4674 return -EEXIST;
4675 if (mddev->default_bitmap_offset == 0)
4676 return -EINVAL;
4677 mddev->bitmap_offset = mddev->default_bitmap_offset;
4678 mddev->pers->quiesce(mddev, 1);
4679 rv = bitmap_create(mddev);
4680 if (rv)
4681 bitmap_destroy(mddev);
4682 mddev->pers->quiesce(mddev, 0);
4683 } else {
4684 /* remove the bitmap */
4685 if (!mddev->bitmap)
4686 return -ENOENT;
4687 if (mddev->bitmap->file)
4688 return -EINVAL;
4689 mddev->pers->quiesce(mddev, 1);
4690 bitmap_destroy(mddev);
4691 mddev->pers->quiesce(mddev, 0);
4692 mddev->bitmap_offset = 0;
4693 }
4694 }
NeilBrown850b2b42006-10-03 01:15:46 -07004695 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 return rv;
4697}
4698
4699static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4700{
4701 mdk_rdev_t *rdev;
4702
4703 if (mddev->pers == NULL)
4704 return -ENODEV;
4705
4706 rdev = find_rdev(mddev, dev);
4707 if (!rdev)
4708 return -ENODEV;
4709
4710 md_error(mddev, rdev);
4711 return 0;
4712}
4713
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004714static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4715{
4716 mddev_t *mddev = bdev->bd_disk->private_data;
4717
4718 geo->heads = 2;
4719 geo->sectors = 4;
4720 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4721 return 0;
4722}
4723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724static int md_ioctl(struct inode *inode, struct file *file,
4725 unsigned int cmd, unsigned long arg)
4726{
4727 int err = 0;
4728 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 mddev_t *mddev = NULL;
4730
4731 if (!capable(CAP_SYS_ADMIN))
4732 return -EACCES;
4733
4734 /*
4735 * Commands dealing with the RAID driver but not any
4736 * particular array:
4737 */
4738 switch (cmd)
4739 {
4740 case RAID_VERSION:
4741 err = get_version(argp);
4742 goto done;
4743
4744 case PRINT_RAID_DEBUG:
4745 err = 0;
4746 md_print_devices();
4747 goto done;
4748
4749#ifndef MODULE
4750 case RAID_AUTORUN:
4751 err = 0;
4752 autostart_arrays(arg);
4753 goto done;
4754#endif
4755 default:;
4756 }
4757
4758 /*
4759 * Commands creating/starting a new array:
4760 */
4761
4762 mddev = inode->i_bdev->bd_disk->private_data;
4763
4764 if (!mddev) {
4765 BUG();
4766 goto abort;
4767 }
4768
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 err = mddev_lock(mddev);
4770 if (err) {
4771 printk(KERN_INFO
4772 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4773 err, cmd);
4774 goto abort;
4775 }
4776
4777 switch (cmd)
4778 {
4779 case SET_ARRAY_INFO:
4780 {
4781 mdu_array_info_t info;
4782 if (!arg)
4783 memset(&info, 0, sizeof(info));
4784 else if (copy_from_user(&info, argp, sizeof(info))) {
4785 err = -EFAULT;
4786 goto abort_unlock;
4787 }
4788 if (mddev->pers) {
4789 err = update_array_info(mddev, &info);
4790 if (err) {
4791 printk(KERN_WARNING "md: couldn't update"
4792 " array info. %d\n", err);
4793 goto abort_unlock;
4794 }
4795 goto done_unlock;
4796 }
4797 if (!list_empty(&mddev->disks)) {
4798 printk(KERN_WARNING
4799 "md: array %s already has disks!\n",
4800 mdname(mddev));
4801 err = -EBUSY;
4802 goto abort_unlock;
4803 }
4804 if (mddev->raid_disks) {
4805 printk(KERN_WARNING
4806 "md: array %s already initialised!\n",
4807 mdname(mddev));
4808 err = -EBUSY;
4809 goto abort_unlock;
4810 }
4811 err = set_array_info(mddev, &info);
4812 if (err) {
4813 printk(KERN_WARNING "md: couldn't set"
4814 " array info. %d\n", err);
4815 goto abort_unlock;
4816 }
4817 }
4818 goto done_unlock;
4819
4820 default:;
4821 }
4822
4823 /*
4824 * Commands querying/configuring an existing array:
4825 */
NeilBrown32a76272005-06-21 17:17:14 -07004826 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004827 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004828 if ((!mddev->raid_disks && !mddev->external)
4829 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4830 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4831 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 err = -ENODEV;
4833 goto abort_unlock;
4834 }
4835
4836 /*
4837 * Commands even a read-only array can execute:
4838 */
4839 switch (cmd)
4840 {
4841 case GET_ARRAY_INFO:
4842 err = get_array_info(mddev, argp);
4843 goto done_unlock;
4844
NeilBrown32a76272005-06-21 17:17:14 -07004845 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004846 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004847 goto done_unlock;
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 case GET_DISK_INFO:
4850 err = get_disk_info(mddev, argp);
4851 goto done_unlock;
4852
4853 case RESTART_ARRAY_RW:
4854 err = restart_array(mddev);
4855 goto done_unlock;
4856
4857 case STOP_ARRAY:
4858 err = do_md_stop (mddev, 0);
4859 goto done_unlock;
4860
4861 case STOP_ARRAY_RO:
4862 err = do_md_stop (mddev, 1);
4863 goto done_unlock;
4864
4865 /*
4866 * We have a problem here : there is no easy way to give a CHS
4867 * virtual geometry. We currently pretend that we have a 2 heads
4868 * 4 sectors (with a BIG number of cylinders...). This drives
4869 * dosfs just mad... ;-)
4870 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 }
4872
4873 /*
4874 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004875 * superblock, so we do not allow them on read-only arrays.
4876 * However non-MD ioctls (e.g. get-size) will still come through
4877 * here and hit the 'default' below, so only disallow
4878 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 */
NeilBrownf91de922005-11-08 21:39:36 -08004880 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4881 mddev->ro && mddev->pers) {
4882 if (mddev->ro == 2) {
4883 mddev->ro = 0;
4884 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4885 md_wakeup_thread(mddev->thread);
4886
4887 } else {
4888 err = -EROFS;
4889 goto abort_unlock;
4890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 }
4892
4893 switch (cmd)
4894 {
4895 case ADD_NEW_DISK:
4896 {
4897 mdu_disk_info_t info;
4898 if (copy_from_user(&info, argp, sizeof(info)))
4899 err = -EFAULT;
4900 else
4901 err = add_new_disk(mddev, &info);
4902 goto done_unlock;
4903 }
4904
4905 case HOT_REMOVE_DISK:
4906 err = hot_remove_disk(mddev, new_decode_dev(arg));
4907 goto done_unlock;
4908
4909 case HOT_ADD_DISK:
4910 err = hot_add_disk(mddev, new_decode_dev(arg));
4911 goto done_unlock;
4912
4913 case SET_DISK_FAULTY:
4914 err = set_disk_faulty(mddev, new_decode_dev(arg));
4915 goto done_unlock;
4916
4917 case RUN_ARRAY:
4918 err = do_md_run (mddev);
4919 goto done_unlock;
4920
NeilBrown32a76272005-06-21 17:17:14 -07004921 case SET_BITMAP_FILE:
4922 err = set_bitmap_file(mddev, (int)arg);
4923 goto done_unlock;
4924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 err = -EINVAL;
4927 goto abort_unlock;
4928 }
4929
4930done_unlock:
4931abort_unlock:
4932 mddev_unlock(mddev);
4933
4934 return err;
4935done:
4936 if (err)
4937 MD_BUG();
4938abort:
4939 return err;
4940}
4941
4942static int md_open(struct inode *inode, struct file *file)
4943{
4944 /*
4945 * Succeed if we can lock the mddev, which confirms that
4946 * it isn't being stopped right now.
4947 */
4948 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4949 int err;
4950
NeilBrownd63a5a72006-12-08 02:36:17 -08004951 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 goto out;
4953
4954 err = 0;
4955 mddev_get(mddev);
4956 mddev_unlock(mddev);
4957
4958 check_disk_change(inode->i_bdev);
4959 out:
4960 return err;
4961}
4962
4963static int md_release(struct inode *inode, struct file * file)
4964{
4965 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4966
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004967 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 mddev_put(mddev);
4969
4970 return 0;
4971}
4972
Linus Torvalds44ce62942007-05-09 18:51:36 -07004973static int md_media_changed(struct gendisk *disk)
4974{
4975 mddev_t *mddev = disk->private_data;
4976
4977 return mddev->changed;
4978}
4979
4980static int md_revalidate(struct gendisk *disk)
4981{
4982 mddev_t *mddev = disk->private_data;
4983
4984 mddev->changed = 0;
4985 return 0;
4986}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987static struct block_device_operations md_fops =
4988{
4989 .owner = THIS_MODULE,
4990 .open = md_open,
4991 .release = md_release,
4992 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004993 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004994 .media_changed = md_media_changed,
4995 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996};
4997
Adrian Bunk75c96f82005-05-05 16:16:09 -07004998static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999{
5000 mdk_thread_t *thread = arg;
5001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 /*
5003 * md_thread is a 'system-thread', it's priority should be very
5004 * high. We avoid resource deadlocks individually in each
5005 * raid personality. (RAID5 does preallocation) We also use RR and
5006 * the very same RT priority as kswapd, thus we will never get
5007 * into a priority inversion deadlock.
5008 *
5009 * we definitely have to have equal or higher priority than
5010 * bdflush, otherwise bdflush will deadlock if there are too
5011 * many dirty RAID5 blocks.
5012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
NeilBrown6985c432005-10-19 21:23:47 -07005014 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005015 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016
NeilBrown93588e22005-11-15 00:09:12 -08005017 /* We need to wait INTERRUPTIBLE so that
5018 * we don't add to the load-average.
5019 * That means we need to be sure no signals are
5020 * pending
5021 */
5022 if (signal_pending(current))
5023 flush_signals(current);
5024
5025 wait_event_interruptible_timeout
5026 (thread->wqueue,
5027 test_bit(THREAD_WAKEUP, &thread->flags)
5028 || kthread_should_stop(),
5029 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
5031 clear_bit(THREAD_WAKEUP, &thread->flags);
5032
NeilBrown787453c2005-11-08 21:39:43 -08005033 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 return 0;
5037}
5038
5039void md_wakeup_thread(mdk_thread_t *thread)
5040{
5041 if (thread) {
5042 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5043 set_bit(THREAD_WAKEUP, &thread->flags);
5044 wake_up(&thread->wqueue);
5045 }
5046}
5047
5048mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5049 const char *name)
5050{
5051 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052
NeilBrown9ffae0c2006-01-06 00:20:32 -08005053 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 if (!thread)
5055 return NULL;
5056
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 init_waitqueue_head(&thread->wqueue);
5058
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 thread->run = run;
5060 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005061 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005062 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005063 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 kfree(thread);
5065 return NULL;
5066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 return thread;
5068}
5069
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070void md_unregister_thread(mdk_thread_t *thread)
5071{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005072 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005073
5074 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 kfree(thread);
5076}
5077
5078void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5079{
5080 if (!mddev) {
5081 MD_BUG();
5082 return;
5083 }
5084
NeilBrownb2d444d2005-11-08 21:39:31 -08005085 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005087
5088 if (mddev->external)
5089 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005090/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5092 mdname(mddev),
5093 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5094 __builtin_return_address(0),__builtin_return_address(1),
5095 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005096*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005097 if (!mddev->pers)
5098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 if (!mddev->pers->error_handler)
5100 return;
5101 mddev->pers->error_handler(mddev,rdev);
5102 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5103 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5104 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005105 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106}
5107
5108/* seq_file implementation /proc/mdstat */
5109
5110static void status_unused(struct seq_file *seq)
5111{
5112 int i = 0;
5113 mdk_rdev_t *rdev;
5114 struct list_head *tmp;
5115
5116 seq_printf(seq, "unused devices: ");
5117
NeilBrown73c34432008-02-06 01:39:59 -08005118 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 char b[BDEVNAME_SIZE];
5120 i++;
5121 seq_printf(seq, "%s ",
5122 bdevname(rdev->bdev,b));
5123 }
5124 if (!i)
5125 seq_printf(seq, "<none>");
5126
5127 seq_printf(seq, "\n");
5128}
5129
5130
5131static void status_resync(struct seq_file *seq, mddev_t * mddev)
5132{
NeilBrown4588b422006-03-27 01:18:04 -08005133 sector_t max_blocks, resync, res;
5134 unsigned long dt, db, rt;
5135 int scale;
5136 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137
5138 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5139
5140 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5141 max_blocks = mddev->resync_max_sectors >> 1;
5142 else
5143 max_blocks = mddev->size;
5144
5145 /*
5146 * Should not happen.
5147 */
5148 if (!max_blocks) {
5149 MD_BUG();
5150 return;
5151 }
NeilBrown4588b422006-03-27 01:18:04 -08005152 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5153 * in a sector_t, and (max_blocks>>scale) will fit in a
5154 * u32, as those are the requirements for sector_div.
5155 * Thus 'scale' must be at least 10
5156 */
5157 scale = 10;
5158 if (sizeof(sector_t) > sizeof(unsigned long)) {
5159 while ( max_blocks/2 > (1ULL<<(scale+32)))
5160 scale++;
5161 }
5162 res = (resync>>scale)*1000;
5163 sector_div(res, (u32)((max_blocks>>scale)+1));
5164
5165 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 {
NeilBrown4588b422006-03-27 01:18:04 -08005167 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 seq_printf(seq, "[");
5169 for (i = 0; i < x; i++)
5170 seq_printf(seq, "=");
5171 seq_printf(seq, ">");
5172 for (i = 0; i < y; i++)
5173 seq_printf(seq, ".");
5174 seq_printf(seq, "] ");
5175 }
NeilBrown4588b422006-03-27 01:18:04 -08005176 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005177 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5178 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005179 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5180 "check" :
5181 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5182 "resync" : "recovery"))),
5183 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005184 (unsigned long long) resync,
5185 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186
5187 /*
5188 * We do not want to overflow, so the order of operands and
5189 * the * 100 / 100 trick are important. We do a +1 to be
5190 * safe against division by zero. We only estimate anyway.
5191 *
5192 * dt: time from mark until now
5193 * db: blocks written from mark until now
5194 * rt: remaining time
5195 */
5196 dt = ((jiffies - mddev->resync_mark) / HZ);
5197 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005198 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5199 - mddev->resync_mark_cnt;
5200 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
5202 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5203
NeilBrownff4e8d92006-07-10 04:44:16 -07005204 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205}
5206
5207static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5208{
5209 struct list_head *tmp;
5210 loff_t l = *pos;
5211 mddev_t *mddev;
5212
5213 if (l >= 0x10000)
5214 return NULL;
5215 if (!l--)
5216 /* header */
5217 return (void*)1;
5218
5219 spin_lock(&all_mddevs_lock);
5220 list_for_each(tmp,&all_mddevs)
5221 if (!l--) {
5222 mddev = list_entry(tmp, mddev_t, all_mddevs);
5223 mddev_get(mddev);
5224 spin_unlock(&all_mddevs_lock);
5225 return mddev;
5226 }
5227 spin_unlock(&all_mddevs_lock);
5228 if (!l--)
5229 return (void*)2;/* tail */
5230 return NULL;
5231}
5232
5233static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5234{
5235 struct list_head *tmp;
5236 mddev_t *next_mddev, *mddev = v;
5237
5238 ++*pos;
5239 if (v == (void*)2)
5240 return NULL;
5241
5242 spin_lock(&all_mddevs_lock);
5243 if (v == (void*)1)
5244 tmp = all_mddevs.next;
5245 else
5246 tmp = mddev->all_mddevs.next;
5247 if (tmp != &all_mddevs)
5248 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5249 else {
5250 next_mddev = (void*)2;
5251 *pos = 0x10000;
5252 }
5253 spin_unlock(&all_mddevs_lock);
5254
5255 if (v != (void*)1)
5256 mddev_put(mddev);
5257 return next_mddev;
5258
5259}
5260
5261static void md_seq_stop(struct seq_file *seq, void *v)
5262{
5263 mddev_t *mddev = v;
5264
5265 if (mddev && v != (void*)1 && v != (void*)2)
5266 mddev_put(mddev);
5267}
5268
NeilBrownd7603b72006-01-06 00:20:30 -08005269struct mdstat_info {
5270 int event;
5271};
5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273static int md_seq_show(struct seq_file *seq, void *v)
5274{
5275 mddev_t *mddev = v;
5276 sector_t size;
5277 struct list_head *tmp2;
5278 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005279 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005280 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281
5282 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005283 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 seq_printf(seq, "Personalities : ");
5285 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005286 list_for_each_entry(pers, &pers_list, list)
5287 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288
5289 spin_unlock(&pers_lock);
5290 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005291 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 return 0;
5293 }
5294 if (v == (void*)2) {
5295 status_unused(seq);
5296 return 0;
5297 }
5298
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005299 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005301
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5303 seq_printf(seq, "%s : %sactive", mdname(mddev),
5304 mddev->pers ? "" : "in");
5305 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005306 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005308 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005309 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 seq_printf(seq, " %s", mddev->pers->name);
5311 }
5312
5313 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005314 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 char b[BDEVNAME_SIZE];
5316 seq_printf(seq, " %s[%d]",
5317 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005318 if (test_bit(WriteMostly, &rdev->flags))
5319 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005320 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 seq_printf(seq, "(F)");
5322 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005323 } else if (rdev->raid_disk < 0)
5324 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 size += rdev->size;
5326 }
5327
5328 if (!list_empty(&mddev->disks)) {
5329 if (mddev->pers)
5330 seq_printf(seq, "\n %llu blocks",
5331 (unsigned long long)mddev->array_size);
5332 else
5333 seq_printf(seq, "\n %llu blocks",
5334 (unsigned long long)size);
5335 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005336 if (mddev->persistent) {
5337 if (mddev->major_version != 0 ||
5338 mddev->minor_version != 90) {
5339 seq_printf(seq," super %d.%d",
5340 mddev->major_version,
5341 mddev->minor_version);
5342 }
NeilBrowne6910632008-02-06 01:39:51 -08005343 } else if (mddev->external)
5344 seq_printf(seq, " super external:%s",
5345 mddev->metadata_type);
5346 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005347 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348
5349 if (mddev->pers) {
5350 mddev->pers->status (seq, mddev);
5351 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005352 if (mddev->pers->sync_request) {
5353 if (mddev->curr_resync > 2) {
5354 status_resync (seq, mddev);
5355 seq_printf(seq, "\n ");
5356 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5357 seq_printf(seq, "\tresync=DELAYED\n ");
5358 else if (mddev->recovery_cp < MaxSector)
5359 seq_printf(seq, "\tresync=PENDING\n ");
5360 }
NeilBrown32a76272005-06-21 17:17:14 -07005361 } else
5362 seq_printf(seq, "\n ");
5363
5364 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005365 unsigned long chunk_kb;
5366 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005367 spin_lock_irqsave(&bitmap->lock, flags);
5368 chunk_kb = bitmap->chunksize >> 10;
5369 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5370 "%lu%s chunk",
5371 bitmap->pages - bitmap->missing_pages,
5372 bitmap->pages,
5373 (bitmap->pages - bitmap->missing_pages)
5374 << (PAGE_SHIFT - 10),
5375 chunk_kb ? chunk_kb : bitmap->chunksize,
5376 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005377 if (bitmap->file) {
5378 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005379 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005380 }
NeilBrown78d742d2005-06-21 17:17:15 -07005381
NeilBrown32a76272005-06-21 17:17:14 -07005382 seq_printf(seq, "\n");
5383 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 }
5385
5386 seq_printf(seq, "\n");
5387 }
5388 mddev_unlock(mddev);
5389
5390 return 0;
5391}
5392
5393static struct seq_operations md_seq_ops = {
5394 .start = md_seq_start,
5395 .next = md_seq_next,
5396 .stop = md_seq_stop,
5397 .show = md_seq_show,
5398};
5399
5400static int md_seq_open(struct inode *inode, struct file *file)
5401{
5402 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005403 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5404 if (mi == NULL)
5405 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005408 if (error)
5409 kfree(mi);
5410 else {
5411 struct seq_file *p = file->private_data;
5412 p->private = mi;
5413 mi->event = atomic_read(&md_event_count);
5414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 return error;
5416}
5417
NeilBrownd7603b72006-01-06 00:20:30 -08005418static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5419{
5420 struct seq_file *m = filp->private_data;
5421 struct mdstat_info *mi = m->private;
5422 int mask;
5423
5424 poll_wait(filp, &md_event_waiters, wait);
5425
5426 /* always allow read */
5427 mask = POLLIN | POLLRDNORM;
5428
5429 if (mi->event != atomic_read(&md_event_count))
5430 mask |= POLLERR | POLLPRI;
5431 return mask;
5432}
5433
Arjan van de Venfa027c22007-02-12 00:55:33 -08005434static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005435 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 .open = md_seq_open,
5437 .read = seq_read,
5438 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005439 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005440 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441};
5442
NeilBrown2604b702006-01-06 00:20:36 -08005443int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005446 list_add_tail(&p->list, &pers_list);
5447 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 spin_unlock(&pers_lock);
5449 return 0;
5450}
5451
NeilBrown2604b702006-01-06 00:20:36 -08005452int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453{
NeilBrown2604b702006-01-06 00:20:36 -08005454 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005456 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 spin_unlock(&pers_lock);
5458 return 0;
5459}
5460
5461static int is_mddev_idle(mddev_t *mddev)
5462{
5463 mdk_rdev_t * rdev;
5464 struct list_head *tmp;
5465 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005466 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467
5468 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005469 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005471 curr_events = disk_stat_read(disk, sectors[0]) +
5472 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005474 /* sync IO will cause sync_io to increase before the disk_stats
5475 * as sync_io is counted when a request starts, and
5476 * disk_stats is counted when it completes.
5477 * So resync activity will cause curr_events to be smaller than
5478 * when there was no such activity.
5479 * non-sync IO will cause disk_stat to increase without
5480 * increasing sync_io so curr_events will (eventually)
5481 * be larger than it was before. Once it becomes
5482 * substantially larger, the test below will cause
5483 * the array to appear non-idle, and resync will slow
5484 * down.
5485 * If there is a lot of outstanding resync activity when
5486 * we set last_event to curr_events, then all that activity
5487 * completing might cause the array to appear non-idle
5488 * and resync will be slowed down even though there might
5489 * not have been non-resync activity. This will only
5490 * happen once though. 'last_events' will soon reflect
5491 * the state where there is little or no outstanding
5492 * resync requests, and further resync activity will
5493 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005494 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005496 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 rdev->last_events = curr_events;
5498 idle = 0;
5499 }
5500 }
5501 return idle;
5502}
5503
5504void md_done_sync(mddev_t *mddev, int blocks, int ok)
5505{
5506 /* another "blocks" (512byte) blocks have been synced */
5507 atomic_sub(blocks, &mddev->recovery_active);
5508 wake_up(&mddev->recovery_wait);
5509 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005510 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 md_wakeup_thread(mddev->thread);
5512 // stop recovery, signal do_sync ....
5513 }
5514}
5515
5516
NeilBrown06d91a52005-06-21 17:17:12 -07005517/* md_write_start(mddev, bi)
5518 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005519 * in superblock) before writing, schedule a superblock update
5520 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005521 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005522void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523{
NeilBrown06d91a52005-06-21 17:17:12 -07005524 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005525 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005526
NeilBrownf91de922005-11-08 21:39:36 -08005527 BUG_ON(mddev->ro == 1);
5528 if (mddev->ro == 2) {
5529 /* need to switch to read/write */
5530 mddev->ro = 0;
5531 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5532 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005533 md_wakeup_thread(mddev->sync_thread);
NeilBrownf91de922005-11-08 21:39:36 -08005534 }
NeilBrown06d91a52005-06-21 17:17:12 -07005535 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005536 if (mddev->safemode == 1)
5537 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005538 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005539 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005540 if (mddev->in_sync) {
5541 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005542 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005543 md_wakeup_thread(mddev->thread);
5544 }
NeilBrowna9701a32005-11-08 21:39:34 -08005545 spin_unlock_irq(&mddev->write_lock);
NeilBrown09a44cc2008-05-23 13:04:36 -07005546 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown06d91a52005-06-21 17:17:12 -07005547 }
NeilBrown09a44cc2008-05-23 13:04:36 -07005548 wait_event(mddev->sb_wait,
5549 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5550 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551}
5552
5553void md_write_end(mddev_t *mddev)
5554{
5555 if (atomic_dec_and_test(&mddev->writes_pending)) {
5556 if (mddev->safemode == 2)
5557 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005558 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5560 }
5561}
5562
NeilBrown2a2275d2007-01-26 00:57:11 -08005563/* md_allow_write(mddev)
5564 * Calling this ensures that the array is marked 'active' so that writes
5565 * may proceed without blocking. It is important to call this before
5566 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5567 * Must be called with mddev_lock held.
5568 */
5569void md_allow_write(mddev_t *mddev)
5570{
5571 if (!mddev->pers)
5572 return;
5573 if (mddev->ro)
5574 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10005575 if (!mddev->pers->sync_request)
5576 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08005577
5578 spin_lock_irq(&mddev->write_lock);
5579 if (mddev->in_sync) {
5580 mddev->in_sync = 0;
5581 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5582 if (mddev->safemode_delay &&
5583 mddev->safemode == 0)
5584 mddev->safemode = 1;
5585 spin_unlock_irq(&mddev->write_lock);
5586 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005587
5588 sysfs_notify(&mddev->kobj, NULL, "array_state");
5589 /* wait for the dirty state to be recorded in the metadata */
5590 wait_event(mddev->sb_wait,
5591 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5592 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08005593 } else
5594 spin_unlock_irq(&mddev->write_lock);
5595}
5596EXPORT_SYMBOL_GPL(md_allow_write);
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598#define SYNC_MARKS 10
5599#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005600void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
5602 mddev_t *mddev2;
5603 unsigned int currspeed = 0,
5604 window;
NeilBrown57afd892005-06-21 17:17:13 -07005605 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 unsigned long mark[SYNC_MARKS];
5607 sector_t mark_cnt[SYNC_MARKS];
5608 int last_mark,m;
5609 struct list_head *tmp;
5610 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005611 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005612 struct list_head *rtmp;
5613 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005614 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615
5616 /* just incase thread restarts... */
5617 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5618 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005619 if (mddev->ro) /* never try to sync a read-only array */
5620 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
NeilBrown61df9d92006-10-03 01:15:57 -07005622 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5623 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5624 desc = "data-check";
5625 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5626 desc = "requested-resync";
5627 else
5628 desc = "resync";
5629 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5630 desc = "reshape";
5631 else
5632 desc = "recovery";
5633
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 /* we overload curr_resync somewhat here.
5635 * 0 == not engaged in resync at all
5636 * 2 == checking that there is no conflict with another sync
5637 * 1 == like 2, but have yielded to allow conflicting resync to
5638 * commense
5639 * other == active in resync - this many blocks
5640 *
5641 * Before starting a resync we must have set curr_resync to
5642 * 2, and then checked that every "conflicting" array has curr_resync
5643 * less than ours. When we find one that is the same or higher
5644 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5645 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5646 * This will mean we have to start checking from the beginning again.
5647 *
5648 */
5649
5650 do {
5651 mddev->curr_resync = 2;
5652
5653 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005654 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005655 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 goto skip;
5657 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005658 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 if (mddev2 == mddev)
5660 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005661 if (!mddev->parallel_resync
5662 && mddev2->curr_resync
5663 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 DEFINE_WAIT(wq);
5665 if (mddev < mddev2 && mddev->curr_resync == 2) {
5666 /* arbitrarily yield */
5667 mddev->curr_resync = 1;
5668 wake_up(&resync_wait);
5669 }
5670 if (mddev > mddev2 && mddev->curr_resync == 1)
5671 /* no need to wait here, we can wait the next
5672 * time 'round when curr_resync == 2
5673 */
5674 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005675 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5676 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005677 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005678 printk(KERN_INFO "md: delaying %s of %s"
5679 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005681 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 mddev_put(mddev2);
5683 schedule();
5684 finish_wait(&resync_wait, &wq);
5685 goto try_again;
5686 }
5687 finish_wait(&resync_wait, &wq);
5688 }
5689 }
5690 } while (mddev->curr_resync < 2);
5691
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005692 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005693 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005695 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 */
5697 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005698 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005699 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005700 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5701 j = mddev->resync_min;
5702 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005703 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005704
NeilBrownccfcc3c2006-03-27 01:18:09 -08005705 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5706 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005707 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 /* recovery follows the physical size of devices */
5709 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005710 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005711 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005712 if (rdev->raid_disk >= 0 &&
5713 !test_bit(Faulty, &rdev->flags) &&
5714 !test_bit(In_sync, &rdev->flags) &&
5715 rdev->recovery_offset < j)
5716 j = rdev->recovery_offset;
5717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
NeilBrown61df9d92006-10-03 01:15:57 -07005719 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5720 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5721 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005722 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005723 "(but not more than %d KB/sec) for %s.\n",
5724 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
5726 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005727
NeilBrown57afd892005-06-21 17:17:13 -07005728 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 for (m = 0; m < SYNC_MARKS; m++) {
5730 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005731 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 }
5733 last_mark = 0;
5734 mddev->resync_mark = mark[last_mark];
5735 mddev->resync_mark_cnt = mark_cnt[last_mark];
5736
5737 /*
5738 * Tune reconstruction:
5739 */
5740 window = 32*(PAGE_SIZE/512);
5741 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5742 window/2,(unsigned long long) max_sectors/2);
5743
5744 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 last_check = 0;
5746
5747 if (j>2) {
5748 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005749 "md: resuming %s of %s from checkpoint.\n",
5750 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 mddev->curr_resync = j;
5752 }
5753
5754 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005755 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756
NeilBrown57afd892005-06-21 17:17:13 -07005757 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005758 if (j >= mddev->resync_max) {
5759 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5760 wait_event(mddev->recovery_wait,
5761 mddev->resync_max > j
5762 || kthread_should_stop());
5763 }
5764 if (kthread_should_stop())
5765 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005766 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005767 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005768 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005769 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 goto out;
5771 }
NeilBrown57afd892005-06-21 17:17:13 -07005772
5773 if (!skipped) { /* actual IO requested */
5774 io_sectors += sectors;
5775 atomic_add(sectors, &mddev->recovery_active);
5776 }
5777
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 j += sectors;
5779 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005780 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005781 if (last_check == 0)
5782 /* this is the earliers that rebuilt will be
5783 * visible in /proc/mdstat
5784 */
5785 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005786
5787 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 continue;
5789
NeilBrown57afd892005-06-21 17:17:13 -07005790 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791
NeilBrowndfc70642008-05-23 13:04:39 -07005792 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 break;
5794
5795 repeat:
5796 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5797 /* step marks */
5798 int next = (last_mark+1) % SYNC_MARKS;
5799
5800 mddev->resync_mark = mark[next];
5801 mddev->resync_mark_cnt = mark_cnt[next];
5802 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005803 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 last_mark = next;
5805 }
5806
5807
NeilBrownc6207272008-02-06 01:39:52 -08005808 if (kthread_should_stop())
5809 goto interrupted;
5810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
5812 /*
5813 * this loop exits only if either when we are slower than
5814 * the 'hard' speed limit, or the system was IO-idle for
5815 * a jiffy.
5816 * the system might be non-idle CPU-wise, but we only care
5817 * about not overloading the IO subsystem. (things like an
5818 * e2fsck being done on the RAID array should execute fast)
5819 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005820 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 cond_resched();
5822
NeilBrown57afd892005-06-21 17:17:13 -07005823 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5824 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825
NeilBrown88202a02006-01-06 00:21:36 -08005826 if (currspeed > speed_min(mddev)) {
5827 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005829 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 goto repeat;
5831 }
5832 }
5833 }
NeilBrown61df9d92006-10-03 01:15:57 -07005834 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 /*
5836 * this also signals 'finished resyncing' to md_stop
5837 */
5838 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005839 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840
5841 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5842
5843 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005844 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
NeilBrowndfc70642008-05-23 13:04:39 -07005846 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005847 mddev->curr_resync > 2) {
5848 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5849 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5850 if (mddev->curr_resync >= mddev->recovery_cp) {
5851 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005852 "md: checkpointing %s of %s.\n",
5853 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005854 mddev->recovery_cp = mddev->curr_resync;
5855 }
5856 } else
5857 mddev->recovery_cp = MaxSector;
5858 } else {
5859 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5860 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005861 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005862 if (rdev->raid_disk >= 0 &&
5863 !test_bit(Faulty, &rdev->flags) &&
5864 !test_bit(In_sync, &rdev->flags) &&
5865 rdev->recovery_offset < mddev->curr_resync)
5866 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 }
NeilBrown17571282006-12-10 02:20:52 -08005869 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 skip:
5872 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005873 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005874 mddev->resync_max = MaxSector;
5875 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 wake_up(&resync_wait);
5877 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5878 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005879 return;
5880
5881 interrupted:
5882 /*
5883 * got a signal, exit.
5884 */
5885 printk(KERN_INFO
5886 "md: md_do_sync() got signal ... exiting\n");
5887 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5888 goto out;
5889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890}
NeilBrown29269552006-03-27 01:18:10 -08005891EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
5893
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005894static int remove_and_add_spares(mddev_t *mddev)
5895{
5896 mdk_rdev_t *rdev;
5897 struct list_head *rtmp;
5898 int spares = 0;
5899
NeilBrownd089c6a2008-02-06 01:39:59 -08005900 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005901 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005902 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005903 (test_bit(Faulty, &rdev->flags) ||
5904 ! test_bit(In_sync, &rdev->flags)) &&
5905 atomic_read(&rdev->nr_pending)==0) {
5906 if (mddev->pers->hot_remove_disk(
5907 mddev, rdev->raid_disk)==0) {
5908 char nm[20];
5909 sprintf(nm,"rd%d", rdev->raid_disk);
5910 sysfs_remove_link(&mddev->kobj, nm);
5911 rdev->raid_disk = -1;
5912 }
5913 }
5914
5915 if (mddev->degraded) {
NeilBrowndfc70642008-05-23 13:04:39 -07005916 rdev_for_each(rdev, rtmp, mddev) {
5917 if (rdev->raid_disk >= 0 &&
5918 !test_bit(In_sync, &rdev->flags))
5919 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005920 if (rdev->raid_disk < 0
5921 && !test_bit(Faulty, &rdev->flags)) {
5922 rdev->recovery_offset = 0;
5923 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5924 char nm[20];
5925 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005926 if (sysfs_create_link(&mddev->kobj,
5927 &rdev->kobj, nm))
5928 printk(KERN_WARNING
5929 "md: cannot register "
5930 "%s for %s\n",
5931 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005932 spares++;
5933 md_new_event(mddev);
5934 } else
5935 break;
5936 }
NeilBrowndfc70642008-05-23 13:04:39 -07005937 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005938 }
5939 return spares;
5940}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941/*
5942 * This routine is regularly called by all per-raid-array threads to
5943 * deal with generic issues like resync and super-block update.
5944 * Raid personalities that don't have a thread (linear/raid0) do not
5945 * need this as they never do any recovery or update the superblock.
5946 *
5947 * It does not do any resync itself, but rather "forks" off other threads
5948 * to do that as needed.
5949 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5950 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07005951 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 * and wakeups up this thread which will reap the thread and finish up.
5953 * This thread also removes any faulty devices (with nr_pending == 0).
5954 *
5955 * The overall approach is:
5956 * 1/ if the superblock needs updating, update it.
5957 * 2/ If a recovery thread is running, don't do anything else.
5958 * 3/ If recovery has finished, clean up, possibly marking spares active.
5959 * 4/ If there are any faulty devices, remove them.
5960 * 5/ If array is degraded, try to add spares devices
5961 * 6/ If array has spares or is not in-sync, start a resync thread.
5962 */
5963void md_check_recovery(mddev_t *mddev)
5964{
5965 mdk_rdev_t *rdev;
5966 struct list_head *rtmp;
5967
5968
NeilBrown5f404022005-06-21 17:17:16 -07005969 if (mddev->bitmap)
5970 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
5972 if (mddev->ro)
5973 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005974
5975 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07005976 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07005977 printk(KERN_INFO "md: %s in immediate safe mode\n",
5978 mdname(mddev));
5979 mddev->safemode = 2;
5980 }
5981 flush_signals(current);
5982 }
5983
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08005985 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005987 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07005988 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005989 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5990 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 ))
5992 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005993
NeilBrowndf5b89b2006-03-27 01:18:20 -08005994 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005995 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005996
NeilBrown31a59e32008-04-30 00:52:30 -07005997 if (!mddev->external) {
5998 spin_lock_irq(&mddev->write_lock);
5999 if (mddev->safemode &&
6000 !atomic_read(&mddev->writes_pending) &&
6001 !mddev->in_sync &&
6002 mddev->recovery_cp == MaxSector) {
6003 mddev->in_sync = 1;
6004 if (mddev->persistent)
6005 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6006 }
6007 if (mddev->safemode == 1)
6008 mddev->safemode = 0;
6009 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07006010 }
NeilBrownfca4d842005-06-21 17:17:11 -07006011
NeilBrown850b2b42006-10-03 01:15:46 -07006012 if (mddev->flags)
6013 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006014
NeilBrown06d91a52005-06-21 17:17:12 -07006015
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6017 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6018 /* resync/recovery still happening */
6019 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6020 goto unlock;
6021 }
6022 if (mddev->sync_thread) {
6023 /* resync has finished, collect result */
6024 md_unregister_thread(mddev->sync_thread);
6025 mddev->sync_thread = NULL;
NeilBrowndfc70642008-05-23 13:04:39 -07006026 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 /* success...*/
6028 /* activate any spares */
6029 mddev->pers->spare_active(mddev);
6030 }
NeilBrown850b2b42006-10-03 01:15:46 -07006031 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006032
6033 /* if array is no-longer degraded, then any saved_raid_disk
6034 * information must be scrapped
6035 */
6036 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006037 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006038 rdev->saved_raid_disk = -1;
6039
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 mddev->recovery = 0;
6041 /* flag recovery needed just to double check */
6042 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08006043 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 goto unlock;
6045 }
NeilBrown24dd4692005-11-08 21:39:26 -08006046 /* Clear some bits that don't mean anything, but
6047 * might be left set
6048 */
6049 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006050 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6051 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006053 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6054 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 /* no recovery is running.
6056 * remove any failed drives, then
6057 * add spares if possible.
6058 * Spare are also removed and re-added, to allow
6059 * the personality to fail the re-add.
6060 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006062 if (mddev->reshape_position != MaxSector) {
6063 if (mddev->pers->check_reshape(mddev) != 0)
6064 /* Cannot proceed */
6065 goto unlock;
6066 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
6067 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006068 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6069 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
6070 } else if (mddev->recovery_cp < MaxSector) {
6071 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6072 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6073 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006075
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 if (mddev->pers->sync_request) {
6077 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07006078 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6079 /* We are adding a device or devices to an array
6080 * which has the bitmap stored on all devices.
6081 * So make sure all bitmap pages get written
6082 */
6083 bitmap_write_all(mddev->bitmap);
6084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 mddev->sync_thread = md_register_thread(md_do_sync,
6086 mddev,
6087 "%s_resync");
6088 if (!mddev->sync_thread) {
6089 printk(KERN_ERR "%s: could not start resync"
6090 " thread...\n",
6091 mdname(mddev));
6092 /* leave the spares where they are, it shouldn't hurt */
6093 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006094 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08006096 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 }
6098 unlock:
6099 mddev_unlock(mddev);
6100 }
6101}
6102
Dan Williams6bfe0b42008-04-30 00:52:32 -07006103void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6104{
6105 sysfs_notify(&rdev->kobj, NULL, "state");
6106 wait_event_timeout(rdev->blocked_wait,
6107 !test_bit(Blocked, &rdev->flags),
6108 msecs_to_jiffies(5000));
6109 rdev_dec_pending(rdev, mddev);
6110}
6111EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6112
Adrian Bunk75c96f82005-05-05 16:16:09 -07006113static int md_notify_reboot(struct notifier_block *this,
6114 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115{
6116 struct list_head *tmp;
6117 mddev_t *mddev;
6118
6119 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6120
6121 printk(KERN_INFO "md: stopping all md devices.\n");
6122
NeilBrown29ac4aa2008-02-06 01:39:58 -08006123 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006124 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10006126 mddev_unlock(mddev);
6127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 /*
6129 * certain more exotic SCSI devices are known to be
6130 * volatile wrt too early system reboots. While the
6131 * right place to handle this issue is the given
6132 * driver, we do want to have a safe RAID driver ...
6133 */
6134 mdelay(1000*1);
6135 }
6136 return NOTIFY_DONE;
6137}
6138
Adrian Bunk75c96f82005-05-05 16:16:09 -07006139static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 .notifier_call = md_notify_reboot,
6141 .next = NULL,
6142 .priority = INT_MAX, /* before any real devices */
6143};
6144
6145static void md_geninit(void)
6146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6148
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006149 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150}
6151
Adrian Bunk75c96f82005-05-05 16:16:09 -07006152static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 if (register_blkdev(MAJOR_NR, "md"))
6155 return -1;
6156 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6157 unregister_blkdev(MAJOR_NR, "md");
6158 return -1;
6159 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006160 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6161 md_probe, NULL, NULL);
6162 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006163 md_probe, NULL, NULL);
6164
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006166 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006167
6168 md_geninit();
6169 return (0);
6170}
6171
6172
6173#ifndef MODULE
6174
6175/*
6176 * Searches all registered partitions for autorun RAID arrays
6177 * at boot time.
6178 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006179
6180static LIST_HEAD(all_detected_devices);
6181struct detected_devices_node {
6182 struct list_head list;
6183 dev_t dev;
6184};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
6186void md_autodetect_dev(dev_t dev)
6187{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006188 struct detected_devices_node *node_detected_dev;
6189
6190 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6191 if (node_detected_dev) {
6192 node_detected_dev->dev = dev;
6193 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6194 } else {
6195 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6196 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198}
6199
6200
6201static void autostart_arrays(int part)
6202{
6203 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006204 struct detected_devices_node *node_detected_dev;
6205 dev_t dev;
6206 int i_scanned, i_passed;
6207
6208 i_scanned = 0;
6209 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210
6211 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6212
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006213 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6214 i_scanned++;
6215 node_detected_dev = list_entry(all_detected_devices.next,
6216 struct detected_devices_node, list);
6217 list_del(&node_detected_dev->list);
6218 dev = node_detected_dev->dev;
6219 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006220 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 if (IS_ERR(rdev))
6222 continue;
6223
NeilBrownb2d444d2005-11-08 21:39:31 -08006224 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 MD_BUG();
6226 continue;
6227 }
NeilBrownd0fae182008-03-04 14:29:31 -08006228 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006230 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006232
6233 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6234 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235
6236 autorun_devices(part);
6237}
6238
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006239#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241static __exit void md_exit(void)
6242{
6243 mddev_t *mddev;
6244 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006245
NeilBrowne8703fe2006-10-03 01:15:59 -07006246 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6247 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248
6249 unregister_blkdev(MAJOR_NR,"md");
6250 unregister_blkdev(mdp_major, "mdp");
6251 unregister_reboot_notifier(&md_notifier);
6252 unregister_sysctl_table(raid_table_header);
6253 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006254 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 struct gendisk *disk = mddev->gendisk;
6256 if (!disk)
6257 continue;
6258 export_array(mddev);
6259 del_gendisk(disk);
6260 put_disk(disk);
6261 mddev->gendisk = NULL;
6262 mddev_put(mddev);
6263 }
6264}
6265
Dan Williams685784a2007-07-09 11:56:42 -07006266subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267module_exit(md_exit)
6268
NeilBrownf91de922005-11-08 21:39:36 -08006269static int get_ro(char *buffer, struct kernel_param *kp)
6270{
6271 return sprintf(buffer, "%d", start_readonly);
6272}
6273static int set_ro(const char *val, struct kernel_param *kp)
6274{
6275 char *e;
6276 int num = simple_strtoul(val, &e, 10);
6277 if (*val && (*e == '\0' || *e == '\n')) {
6278 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006279 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006280 }
6281 return -EINVAL;
6282}
6283
NeilBrown80ca3a42006-07-10 04:44:18 -07006284module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6285module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006286
NeilBrownf91de922005-11-08 21:39:36 -08006287
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288EXPORT_SYMBOL(register_md_personality);
6289EXPORT_SYMBOL(unregister_md_personality);
6290EXPORT_SYMBOL(md_error);
6291EXPORT_SYMBOL(md_done_sync);
6292EXPORT_SYMBOL(md_write_start);
6293EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294EXPORT_SYMBOL(md_register_thread);
6295EXPORT_SYMBOL(md_unregister_thread);
6296EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297EXPORT_SYMBOL(md_check_recovery);
6298MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006299MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006300MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);