blob: 8b303477c77ba6742975835936d811c14b2327de [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
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070037#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrown32a76272005-06-21 17:17:14 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define MAJOR_NR MD_MAJOR
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* 63 partitions with the alternate major number (mdp) */
52#define MdpMinorShift 6
53
54#define DEBUG 0
55#define dprintk(x...) ((void)(DEBUG && printk(x)))
56
57
58#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown2604b702006-01-06 00:20:36 -080062static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static DEFINE_SPINLOCK(pers_lock);
64
Adrian Bunk5e563412006-06-26 00:27:42 -070065static void md_print_devices(void);
66
Bernd Schubert90b08712008-05-23 13:04:38 -070067static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
73 * is 1000 KB/sec, so the extra system load does not show up that much.
74 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070075 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * subsystem is idle. There is also an 'absolute maximum' reconstruction
77 * speed limit - in case reconstruction slows down your system despite
78 * idle IO detection.
79 *
80 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080081 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
84static int sysctl_speed_limit_min = 1000;
85static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080086static inline int speed_min(mddev_t *mddev)
87{
88 return mddev->sync_speed_min ?
89 mddev->sync_speed_min : sysctl_speed_limit_min;
90}
91
92static inline int speed_max(mddev_t *mddev)
93{
94 return mddev->sync_speed_max ?
95 mddev->sync_speed_max : sysctl_speed_limit_max;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98static struct ctl_table_header *raid_table_header;
99
100static ctl_table raid_table[] = {
101 {
102 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
103 .procname = "speed_limit_min",
104 .data = &sysctl_speed_limit_min,
105 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700106 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .proc_handler = &proc_dointvec,
108 },
109 {
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
111 .procname = "speed_limit_max",
112 .data = &sysctl_speed_limit_max,
113 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700114 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .proc_handler = &proc_dointvec,
116 },
117 { .ctl_name = 0 }
118};
119
120static ctl_table raid_dir_table[] = {
121 {
122 .ctl_name = DEV_RAID,
123 .procname = "raid",
124 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700125 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .child = raid_table,
127 },
128 { .ctl_name = 0 }
129};
130
131static ctl_table raid_root_table[] = {
132 {
133 .ctl_name = CTL_DEV,
134 .procname = "dev",
135 .maxlen = 0,
136 .mode = 0555,
137 .child = raid_dir_table,
138 },
139 { .ctl_name = 0 }
140};
141
142static struct block_device_operations md_fops;
143
NeilBrownf91de922005-11-08 21:39:36 -0800144static int start_readonly;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
NeilBrownd7603b72006-01-06 00:20:30 -0800147 * We have a system wide 'event count' that is incremented
148 * on any 'interesting' event, and readers of /proc/mdstat
149 * can use 'poll' or 'select' to find out when the event
150 * count increases.
151 *
152 * Events are:
153 * start array, stop array, error, add device, remove device,
154 * start build, activate spare
155 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800156static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800157static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800158void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800159{
160 atomic_inc(&md_event_count);
161 wake_up(&md_event_waiters);
162}
NeilBrown29269552006-03-27 01:18:10 -0800163EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800164
NeilBrownc331eb02006-05-30 21:27:13 -0700165/* Alternate version that can be called from interrupts
166 * when calling sysfs_notify isn't needed.
167 */
Adrian Bunk05381952006-06-26 00:28:01 -0700168static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700169{
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
172}
173
NeilBrownd7603b72006-01-06 00:20:30 -0800174/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * Enables to iterate over all existing md arrays
176 * all_mddevs_lock protects this list.
177 */
178static LIST_HEAD(all_mddevs);
179static DEFINE_SPINLOCK(all_mddevs_lock);
180
181
182/*
183 * iterates through all used mddevs in the system.
184 * We take care to grab the all_mddevs_lock whenever navigating
185 * the list, and to always hold a refcount when unlocked.
186 * Any code which breaks out of this loop while own
187 * a reference to the current mddev and must mddev_put it.
188 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800189#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 \
191 for (({ spin_lock(&all_mddevs_lock); \
192 tmp = all_mddevs.next; \
193 mddev = NULL;}); \
194 ({ if (tmp != &all_mddevs) \
195 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
196 spin_unlock(&all_mddevs_lock); \
197 if (mddev) mddev_put(mddev); \
198 mddev = list_entry(tmp, mddev_t, all_mddevs); \
199 tmp != &all_mddevs;}); \
200 ({ spin_lock(&all_mddevs_lock); \
201 tmp = tmp->next;}) \
202 )
203
204
NeilBrownd710e132008-10-13 11:55:12 +1100205static int md_fail_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
NeilBrown6712ecf2007-09-27 12:47:43 +0200207 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209}
210
211static inline mddev_t *mddev_get(mddev_t *mddev)
212{
213 atomic_inc(&mddev->active);
214 return mddev;
215}
216
217static void mddev_put(mddev_t *mddev)
218{
219 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
220 return;
221 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
222 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800223 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500224 blk_cleanup_queue(mddev->queue);
NeilBrownb62b7592008-10-21 13:25:21 +1100225 if (mddev->sysfs_state)
226 sysfs_put(mddev->sysfs_state);
227 mddev->sysfs_state = NULL;
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800228 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800229 } else
230 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
233static mddev_t * mddev_find(dev_t unit)
234{
235 mddev_t *mddev, *new = NULL;
236
237 retry:
238 spin_lock(&all_mddevs_lock);
239 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
240 if (mddev->unit == unit) {
241 mddev_get(mddev);
242 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700243 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return mddev;
245 }
246
247 if (new) {
248 list_add(&new->all_mddevs, &all_mddevs);
249 spin_unlock(&all_mddevs_lock);
250 return new;
251 }
252 spin_unlock(&all_mddevs_lock);
253
NeilBrown9ffae0c2006-01-06 00:20:32 -0800254 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!new)
256 return NULL;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 new->unit = unit;
259 if (MAJOR(unit) == MD_MAJOR)
260 new->md_minor = MINOR(unit);
261 else
262 new->md_minor = MINOR(unit) >> MdpMinorShift;
263
NeilBrowndf5b89b2006-03-27 01:18:20 -0800264 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 INIT_LIST_HEAD(&new->disks);
266 INIT_LIST_HEAD(&new->all_mddevs);
267 init_timer(&new->safemode_timer);
268 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000269 atomic_set(&new->openers, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700270 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700271 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700272 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700273 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000274 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800275 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700276 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 new->queue = blk_alloc_queue(GFP_KERNEL);
279 if (!new->queue) {
280 kfree(new);
281 return NULL;
282 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200283 /* Can be unlocked because the queue is new: no concurrency */
284 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 blk_queue_make_request(new->queue, md_fail_request);
287
288 goto retry;
289}
290
291static inline int mddev_lock(mddev_t * mddev)
292{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800293 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static inline int mddev_trylock(mddev_t * mddev)
297{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800298 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static inline void mddev_unlock(mddev_t * mddev)
302{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800303 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
NeilBrown005eca52005-08-22 13:11:08 -0700305 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
NeilBrown2989ddb2006-01-06 00:20:43 -0800308static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 mdk_rdev_t * rdev;
311 struct list_head *tmp;
312
NeilBrownd089c6a2008-02-06 01:39:59 -0800313 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (rdev->desc_nr == nr)
315 return rdev;
316 }
317 return NULL;
318}
319
320static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
321{
322 struct list_head *tmp;
323 mdk_rdev_t *rdev;
324
NeilBrownd089c6a2008-02-06 01:39:59 -0800325 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (rdev->bdev->bd_dev == dev)
327 return rdev;
328 }
329 return NULL;
330}
331
NeilBrownd9d166c2006-01-06 00:20:51 -0800332static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800333{
334 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800335 list_for_each_entry(pers, &pers_list, list) {
336 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800337 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800338 if (strcmp(pers->name, clevel)==0)
339 return pers;
340 }
NeilBrown2604b702006-01-06 00:20:36 -0800341 return NULL;
342}
343
Andre Nollb73df2d2008-07-11 22:02:23 +1000344/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700345static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Andre Nollb73df2d2008-07-11 22:02:23 +1000347 sector_t num_sectors = bdev->bd_inode->i_size / 512;
348 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Andre Nolle7debaa2008-07-11 22:02:23 +1000351static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Andre Noll0f420352008-07-11 22:02:23 +1000353 sector_t num_sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000356 num_sectors &= ~((sector_t)chunk_size/512 - 1);
357 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360static int alloc_disk_sb(mdk_rdev_t * rdev)
361{
362 if (rdev->sb_page)
363 MD_BUG();
364
365 rdev->sb_page = alloc_page(GFP_KERNEL);
366 if (!rdev->sb_page) {
367 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000368 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 return 0;
372}
373
374static void free_disk_sb(mdk_rdev_t * rdev)
375{
376 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800377 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 rdev->sb_loaded = 0;
379 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000380 rdev->sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 rdev->size = 0;
382 }
383}
384
385
NeilBrown6712ecf2007-09-27 12:47:43 +0200386static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700387{
388 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800389 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700390
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700391 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
392 printk("md: super_written gets error=%d, uptodate=%d\n",
393 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
394 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800395 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700396 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700397
NeilBrowna9701a32005-11-08 21:39:34 -0800398 if (atomic_dec_and_test(&mddev->pending_writes))
399 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700400 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700401}
402
NeilBrown6712ecf2007-09-27 12:47:43 +0200403static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800404{
405 struct bio *bio2 = bio->bi_private;
406 mdk_rdev_t *rdev = bio2->bi_private;
407 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800408
409 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
410 error == -EOPNOTSUPP) {
411 unsigned long flags;
412 /* barriers don't appear to be supported :-( */
413 set_bit(BarriersNotsupp, &rdev->flags);
414 mddev->barriers_work = 0;
415 spin_lock_irqsave(&mddev->write_lock, flags);
416 bio2->bi_next = mddev->biolist;
417 mddev->biolist = bio2;
418 spin_unlock_irqrestore(&mddev->write_lock, flags);
419 wake_up(&mddev->sb_wait);
420 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200421 } else {
422 bio_put(bio2);
423 bio->bi_private = rdev;
424 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800425 }
NeilBrowna9701a32005-11-08 21:39:34 -0800426}
427
NeilBrown7bfa19f2005-06-21 17:17:28 -0700428void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
429 sector_t sector, int size, struct page *page)
430{
431 /* write first size bytes of page to sector of rdev
432 * Increment mddev->pending_writes before returning
433 * and decrement it on completion, waking up sb_wait
434 * if zero is reached.
435 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800436 *
437 * As we might need to resubmit the request if BIO_RW_BARRIER
438 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700439 */
440 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800441 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700442
443 bio->bi_bdev = rdev->bdev;
444 bio->bi_sector = sector;
445 bio_add_page(bio, page, size, 0);
446 bio->bi_private = rdev;
447 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800448 bio->bi_rw = rw;
449
NeilBrown7bfa19f2005-06-21 17:17:28 -0700450 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800451 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
452 struct bio *rbio;
453 rw |= (1<<BIO_RW_BARRIER);
454 rbio = bio_clone(bio, GFP_NOIO);
455 rbio->bi_private = bio;
456 rbio->bi_end_io = super_written_barrier;
457 submit_bio(rw, rbio);
458 } else
459 submit_bio(rw, bio);
460}
461
462void md_super_wait(mddev_t *mddev)
463{
464 /* wait for all superblock writes that were scheduled to complete.
465 * if any had to be retried (due to BARRIER problems), retry them
466 */
467 DEFINE_WAIT(wq);
468 for(;;) {
469 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
470 if (atomic_read(&mddev->pending_writes)==0)
471 break;
472 while (mddev->biolist) {
473 struct bio *bio;
474 spin_lock_irq(&mddev->write_lock);
475 bio = mddev->biolist;
476 mddev->biolist = bio->bi_next ;
477 bio->bi_next = NULL;
478 spin_unlock_irq(&mddev->write_lock);
479 submit_bio(bio->bi_rw, bio);
480 }
481 schedule();
482 }
483 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700484}
485
NeilBrown6712ecf2007-09-27 12:47:43 +0200486static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
NeilBrowna654b9d82005-06-21 17:17:27 -0700491int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct page *page, int rw)
493{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700494 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct completion event;
496 int ret;
497
498 rw |= (1 << BIO_RW_SYNC);
499
500 bio->bi_bdev = bdev;
501 bio->bi_sector = sector;
502 bio_add_page(bio, page, size, 0);
503 init_completion(&event);
504 bio->bi_private = &event;
505 bio->bi_end_io = bi_complete;
506 submit_bio(rw, bio);
507 wait_for_completion(&event);
508
509 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
510 bio_put(bio);
511 return ret;
512}
NeilBrowna8745db2006-01-06 00:20:34 -0800513EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
NeilBrown0002b272005-09-09 16:23:53 -0700515static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 char b[BDEVNAME_SIZE];
518 if (!rdev->sb_page) {
519 MD_BUG();
520 return -EINVAL;
521 }
522 if (rdev->sb_loaded)
523 return 0;
524
525
Andre Noll0f420352008-07-11 22:02:23 +1000526 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 goto fail;
528 rdev->sb_loaded = 1;
529 return 0;
530
531fail:
532 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
533 bdevname(rdev->bdev,b));
534 return -EINVAL;
535}
536
537static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
538{
Andre Noll05710462008-07-11 22:02:20 +1000539 return sb1->set_uuid0 == sb2->set_uuid0 &&
540 sb1->set_uuid1 == sb2->set_uuid1 &&
541 sb1->set_uuid2 == sb2->set_uuid2 &&
542 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
546{
547 int ret;
548 mdp_super_t *tmp1, *tmp2;
549
550 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
551 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
552
553 if (!tmp1 || !tmp2) {
554 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100555 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto abort;
557 }
558
559 *tmp1 = *sb1;
560 *tmp2 = *sb2;
561
562 /*
563 * nr_disks is not constant
564 */
565 tmp1->nr_disks = 0;
566 tmp2->nr_disks = 0;
567
Andre Nollce0c8e02008-07-11 22:02:20 +1000568 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700570 kfree(tmp1);
571 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return ret;
573}
574
NeilBrown4d167f02007-05-09 02:35:37 -0700575
576static u32 md_csum_fold(u32 csum)
577{
578 csum = (csum & 0xffff) + (csum >> 16);
579 return (csum & 0xffff) + (csum >> 16);
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582static unsigned int calc_sb_csum(mdp_super_t * sb)
583{
NeilBrown4d167f02007-05-09 02:35:37 -0700584 u64 newcsum = 0;
585 u32 *sb32 = (u32*)sb;
586 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 unsigned int disk_csum, csum;
588
589 disk_csum = sb->sb_csum;
590 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700591
592 for (i = 0; i < MD_SB_BYTES/4 ; i++)
593 newcsum += sb32[i];
594 csum = (newcsum & 0xffffffff) + (newcsum>>32);
595
596
597#ifdef CONFIG_ALPHA
598 /* This used to use csum_partial, which was wrong for several
599 * reasons including that different results are returned on
600 * different architectures. It isn't critical that we get exactly
601 * the same return value as before (we always csum_fold before
602 * testing, and that removes any differences). However as we
603 * know that csum_partial always returned a 16bit value on
604 * alphas, do a fold to maximise conformity to previous behaviour.
605 */
606 sb->sb_csum = md_csum_fold(disk_csum);
607#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700609#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return csum;
611}
612
613
614/*
615 * Handle superblock details.
616 * We want to be able to handle multiple superblock formats
617 * so we have a common interface to them all, and an array of
618 * different handlers.
619 * We rely on user-space to write the initial superblock, and support
620 * reading and updating of superblocks.
621 * Interface methods are:
622 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
623 * loads and validates a superblock on dev.
624 * if refdev != NULL, compare superblocks on both devices
625 * Return:
626 * 0 - dev has a superblock that is compatible with refdev
627 * 1 - dev has a superblock that is compatible and newer than refdev
628 * so dev should be used as the refdev in future
629 * -EINVAL superblock incompatible or invalid
630 * -othererror e.g. -EIO
631 *
632 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
633 * Verify that dev is acceptable into mddev.
634 * The first time, mddev->raid_disks will be 0, and data from
635 * dev should be merged in. Subsequent calls check that dev
636 * is new enough. Return 0 or -EINVAL
637 *
638 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
639 * Update the superblock for rdev with data in mddev
640 * This does not write to disc.
641 *
642 */
643
644struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000645 char *name;
646 struct module *owner;
647 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
648 int minor_version);
649 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
650 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
651 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000652 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653};
654
655/*
656 * load_super for 0.90.0
657 */
658static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
659{
660 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
661 mdp_super_t *sb;
662 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 /*
Andre Noll0f420352008-07-11 22:02:23 +1000665 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * it's at the end of the disk.
667 *
668 * It also happens to be a multiple of 4Kb.
669 */
Andre Noll0f420352008-07-11 22:02:23 +1000670 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
NeilBrown0002b272005-09-09 16:23:53 -0700672 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (ret) return ret;
674
675 ret = -EINVAL;
676
677 bdevname(rdev->bdev, b);
678 sb = (mdp_super_t*)page_address(rdev->sb_page);
679
680 if (sb->md_magic != MD_SB_MAGIC) {
681 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
682 b);
683 goto abort;
684 }
685
686 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800687 sb->minor_version < 90 ||
688 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
690 sb->major_version, sb->minor_version,
691 b);
692 goto abort;
693 }
694
695 if (sb->raid_disks <= 0)
696 goto abort;
697
NeilBrown4d167f02007-05-09 02:35:37 -0700698 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
700 b);
701 goto abort;
702 }
703
704 rdev->preferred_minor = sb->md_minor;
705 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700706 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
NeilBrowne11e93f2007-05-09 02:35:36 -0700708 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
709 if (sb->level != 1 && sb->level != 4
710 && sb->level != 5 && sb->level != 6
711 && sb->level != 10) {
712 /* FIXME use a better test */
713 printk(KERN_WARNING
714 "md: bitmaps not supported for this level.\n");
715 goto abort;
716 }
717 }
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (sb->level == LEVEL_MULTIPATH)
720 rdev->desc_nr = -1;
721 else
722 rdev->desc_nr = sb->this_disk.number;
723
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700724 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700726 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 __u64 ev1, ev2;
728 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
729 if (!uuid_equal(refsb, sb)) {
730 printk(KERN_WARNING "md: %s has different UUID to %s\n",
731 b, bdevname(refdev->bdev,b2));
732 goto abort;
733 }
734 if (!sb_equal(refsb, sb)) {
735 printk(KERN_WARNING "md: %s has same UUID"
736 " but different superblock to %s\n",
737 b, bdevname(refdev->bdev, b2));
738 goto abort;
739 }
740 ev1 = md_event(sb);
741 ev2 = md_event(refsb);
742 if (ev1 > ev2)
743 ret = 1;
744 else
745 ret = 0;
746 }
Andre Nolle7debaa2008-07-11 22:02:23 +1000747 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
NeilBrown2bf071b2006-01-06 00:20:55 -0800749 if (rdev->size < sb->size && sb->level > 1)
750 /* "this cannot possibly happen" ... */
751 ret = -EINVAL;
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 abort:
754 return ret;
755}
756
757/*
758 * validate_super for 0.90.0
759 */
760static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
761{
762 mdp_disk_t *desc;
763 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700764 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
NeilBrown41158c72005-06-21 17:17:25 -0700766 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800767 clear_bit(Faulty, &rdev->flags);
768 clear_bit(In_sync, &rdev->flags);
769 clear_bit(WriteMostly, &rdev->flags);
770 clear_bit(BarriersNotsupp, &rdev->flags);
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (mddev->raid_disks == 0) {
773 mddev->major_version = 0;
774 mddev->minor_version = sb->minor_version;
775 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800776 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 mddev->chunk_size = sb->chunk_size;
778 mddev->ctime = sb->ctime;
779 mddev->utime = sb->utime;
780 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800781 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 mddev->layout = sb->layout;
783 mddev->raid_disks = sb->raid_disks;
784 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700785 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700786 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700787 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
NeilBrownf6705572006-03-27 01:18:11 -0800789 if (mddev->minor_version >= 91) {
790 mddev->reshape_position = sb->reshape_position;
791 mddev->delta_disks = sb->delta_disks;
792 mddev->new_level = sb->new_level;
793 mddev->new_layout = sb->new_layout;
794 mddev->new_chunk = sb->new_chunk;
795 } else {
796 mddev->reshape_position = MaxSector;
797 mddev->delta_disks = 0;
798 mddev->new_level = mddev->level;
799 mddev->new_layout = mddev->layout;
800 mddev->new_chunk = mddev->chunk_size;
801 }
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (sb->state & (1<<MD_SB_CLEAN))
804 mddev->recovery_cp = MaxSector;
805 else {
806 if (sb->events_hi == sb->cp_events_hi &&
807 sb->events_lo == sb->cp_events_lo) {
808 mddev->recovery_cp = sb->recovery_cp;
809 } else
810 mddev->recovery_cp = 0;
811 }
812
813 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
814 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
815 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
816 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
817
818 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700819
820 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700821 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700822 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700823
NeilBrown41158c72005-06-21 17:17:25 -0700824 } else if (mddev->pers == NULL) {
825 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ++ev1;
827 if (ev1 < mddev->events)
828 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700829 } else if (mddev->bitmap) {
830 /* if adding to array with a bitmap, then we can accept an
831 * older device ... but not too old.
832 */
NeilBrown41158c72005-06-21 17:17:25 -0700833 if (ev1 < mddev->bitmap->events_cleared)
834 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700835 } else {
836 if (ev1 < mddev->events)
837 /* just a hot-add of a new device, leave raid_disk at -1 */
838 return 0;
839 }
NeilBrown41158c72005-06-21 17:17:25 -0700840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 desc = sb->disks + rdev->desc_nr;
843
844 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800845 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700846 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
847 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800848 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 rdev->raid_disk = desc->raid_disk;
850 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700851 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
852 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700853 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800854 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return 0;
856}
857
858/*
859 * sync_super for 0.90.0
860 */
861static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
862{
863 mdp_super_t *sb;
864 struct list_head *tmp;
865 mdk_rdev_t *rdev2;
866 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 /* make rdev->sb match mddev data..
870 *
871 * 1/ zero out disks
872 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
873 * 3/ any empty disks < next_spare become removed
874 *
875 * disks[0] gets initialised to REMOVED because
876 * we cannot be sure from other fields if it has
877 * been initialised or not.
878 */
879 int i;
880 int active=0, working=0,failed=0,spare=0,nr_disks=0;
881
NeilBrown61181562005-09-09 16:24:02 -0700882 rdev->sb_size = MD_SB_BYTES;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 sb = (mdp_super_t*)page_address(rdev->sb_page);
885
886 memset(sb, 0, sizeof(*sb));
887
888 sb->md_magic = MD_SB_MAGIC;
889 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 sb->patch_version = mddev->patch_version;
891 sb->gvalid_words = 0; /* ignored */
892 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
893 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
894 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
895 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
896
897 sb->ctime = mddev->ctime;
898 sb->level = mddev->level;
899 sb->size = mddev->size;
900 sb->raid_disks = mddev->raid_disks;
901 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800902 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 sb->utime = mddev->utime;
904 sb->state = 0;
905 sb->events_hi = (mddev->events>>32);
906 sb->events_lo = (u32)mddev->events;
907
NeilBrownf6705572006-03-27 01:18:11 -0800908 if (mddev->reshape_position == MaxSector)
909 sb->minor_version = 90;
910 else {
911 sb->minor_version = 91;
912 sb->reshape_position = mddev->reshape_position;
913 sb->new_level = mddev->new_level;
914 sb->delta_disks = mddev->delta_disks;
915 sb->new_layout = mddev->new_layout;
916 sb->new_chunk = mddev->new_chunk;
917 }
918 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if (mddev->in_sync)
920 {
921 sb->recovery_cp = mddev->recovery_cp;
922 sb->cp_events_hi = (mddev->events>>32);
923 sb->cp_events_lo = (u32)mddev->events;
924 if (mddev->recovery_cp == MaxSector)
925 sb->state = (1<< MD_SB_CLEAN);
926 } else
927 sb->recovery_cp = 0;
928
929 sb->layout = mddev->layout;
930 sb->chunk_size = mddev->chunk_size;
931
NeilBrowna654b9d82005-06-21 17:17:27 -0700932 if (mddev->bitmap && mddev->bitmap_file == NULL)
933 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800936 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800938 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800939 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
940 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800941 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800943 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800944 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 d = &sb->disks[rdev2->desc_nr];
946 nr_disks++;
947 d->number = rdev2->desc_nr;
948 d->major = MAJOR(rdev2->bdev->bd_dev);
949 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800950 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
951 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 d->raid_disk = rdev2->raid_disk;
953 else
954 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800955 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800957 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 d->state = (1<<MD_DISK_ACTIVE);
959 d->state |= (1<<MD_DISK_SYNC);
960 active++;
961 working++;
962 } else {
963 d->state = 0;
964 spare++;
965 working++;
966 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700967 if (test_bit(WriteMostly, &rdev2->flags))
968 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* now set the "removed" and "faulty" bits on any missing devices */
971 for (i=0 ; i < mddev->raid_disks ; i++) {
972 mdp_disk_t *d = &sb->disks[i];
973 if (d->state == 0 && d->number == 0) {
974 d->number = i;
975 d->raid_disk = i;
976 d->state = (1<<MD_DISK_REMOVED);
977 d->state |= (1<<MD_DISK_FAULTY);
978 failed++;
979 }
980 }
981 sb->nr_disks = nr_disks;
982 sb->active_disks = active;
983 sb->working_disks = working;
984 sb->failed_disks = failed;
985 sb->spare_disks = spare;
986
987 sb->this_disk = sb->disks[rdev->desc_nr];
988 sb->sb_csum = calc_sb_csum(sb);
989}
990
991/*
Chris Webb0cd17fe2008-06-28 08:31:46 +1000992 * rdev_size_change for 0.90.0
993 */
994static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +1000995super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +1000996{
Andre Noll15f4a5f2008-07-21 14:42:12 +1000997 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +1000998 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +1000999 if (rdev->mddev->bitmap_offset)
1000 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001001 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001002 if (!num_sectors || num_sectors > rdev->sb_start)
1003 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001004 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001005 rdev->sb_page);
1006 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001007 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001008}
1009
1010
1011/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * version 1 superblock
1013 */
1014
NeilBrown1c05b4b2006-10-21 10:24:08 -07001015static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001017 __le32 disk_csum;
1018 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 unsigned long long newcsum;
1020 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001021 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int i;
1023
1024 disk_csum = sb->sb_csum;
1025 sb->sb_csum = 0;
1026 newcsum = 0;
1027 for (i=0; size>=4; size -= 4 )
1028 newcsum += le32_to_cpu(*isuper++);
1029
1030 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001031 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1034 sb->sb_csum = disk_csum;
1035 return cpu_to_le32(csum);
1036}
1037
1038static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1039{
1040 struct mdp_superblock_1 *sb;
1041 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001042 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001044 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 /*
Andre Noll0f420352008-07-11 22:02:23 +10001047 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * It is always aligned to a 4K boundary and
1049 * depeding on minor_version, it can be:
1050 * 0: At least 8K, but less than 12K, from end of device
1051 * 1: At start of device
1052 * 2: 4K from start of device.
1053 */
1054 switch(minor_version) {
1055 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001056 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1057 sb_start -= 8*2;
1058 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 break;
1060 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001061 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 break;
1063 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001064 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 break;
1066 default:
1067 return -EINVAL;
1068 }
Andre Noll0f420352008-07-11 22:02:23 +10001069 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
NeilBrown0002b272005-09-09 16:23:53 -07001071 /* superblock is rarely larger than 1K, but it can be larger,
1072 * and it is safe to read 4k, so we do that
1073 */
1074 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 if (ret) return ret;
1076
1077
1078 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1079
1080 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1081 sb->major_version != cpu_to_le32(1) ||
1082 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001083 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001084 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -EINVAL;
1086
1087 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1088 printk("md: invalid superblock checksum on %s\n",
1089 bdevname(rdev->bdev,b));
1090 return -EINVAL;
1091 }
1092 if (le64_to_cpu(sb->data_size) < 10) {
1093 printk("md: data_size too small on %s\n",
1094 bdevname(rdev->bdev,b));
1095 return -EINVAL;
1096 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001097 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1098 if (sb->level != cpu_to_le32(1) &&
1099 sb->level != cpu_to_le32(4) &&
1100 sb->level != cpu_to_le32(5) &&
1101 sb->level != cpu_to_le32(6) &&
1102 sb->level != cpu_to_le32(10)) {
1103 printk(KERN_WARNING
1104 "md: bitmaps not supported for this level.\n");
1105 return -EINVAL;
1106 }
1107 }
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 rdev->preferred_minor = 0xffff;
1110 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001111 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
NeilBrown0002b272005-09-09 16:23:53 -07001113 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001114 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001115 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001116 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1117
1118 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001119 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001120 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001121
NeilBrown31b65a02006-07-10 04:44:14 -07001122 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1123 rdev->desc_nr = -1;
1124 else
1125 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1126
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001127 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001128 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001129 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 __u64 ev1, ev2;
1131 struct mdp_superblock_1 *refsb =
1132 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1133
1134 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1135 sb->level != refsb->level ||
1136 sb->layout != refsb->layout ||
1137 sb->chunksize != refsb->chunksize) {
1138 printk(KERN_WARNING "md: %s has strangely different"
1139 " superblock to %s\n",
1140 bdevname(rdev->bdev,b),
1141 bdevname(refdev->bdev,b2));
1142 return -EINVAL;
1143 }
1144 ev1 = le64_to_cpu(sb->events);
1145 ev2 = le64_to_cpu(refsb->events);
1146
1147 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001148 ret = 1;
1149 else
1150 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
NeilBrowna1801f82008-03-04 14:29:31 -08001152 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1154 else
Andre Noll0f420352008-07-11 22:02:23 +10001155 rdev->size = rdev->sb_start / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1157 return -EINVAL;
1158 rdev->size = le64_to_cpu(sb->data_size)/2;
1159 if (le32_to_cpu(sb->chunksize))
1160 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001161
NeilBrown1c05b4b2006-10-21 10:24:08 -07001162 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001163 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1168{
1169 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001170 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
NeilBrown41158c72005-06-21 17:17:25 -07001172 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001173 clear_bit(Faulty, &rdev->flags);
1174 clear_bit(In_sync, &rdev->flags);
1175 clear_bit(WriteMostly, &rdev->flags);
1176 clear_bit(BarriersNotsupp, &rdev->flags);
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (mddev->raid_disks == 0) {
1179 mddev->major_version = 1;
1180 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001181 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1183 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1184 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1185 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001186 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 mddev->layout = le32_to_cpu(sb->layout);
1188 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1189 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001190 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001191 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001192 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1195 memcpy(mddev->uuid, sb->set_uuid, 16);
1196
1197 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001198
NeilBrown71c08052005-09-09 16:23:51 -07001199 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001200 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001201 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001202
NeilBrownf6705572006-03-27 01:18:11 -08001203 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1204 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1205 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1206 mddev->new_level = le32_to_cpu(sb->new_level);
1207 mddev->new_layout = le32_to_cpu(sb->new_layout);
1208 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1209 } else {
1210 mddev->reshape_position = MaxSector;
1211 mddev->delta_disks = 0;
1212 mddev->new_level = mddev->level;
1213 mddev->new_layout = mddev->layout;
1214 mddev->new_chunk = mddev->chunk_size;
1215 }
1216
NeilBrown41158c72005-06-21 17:17:25 -07001217 } else if (mddev->pers == NULL) {
1218 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 ++ev1;
1220 if (ev1 < mddev->events)
1221 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001222 } else if (mddev->bitmap) {
1223 /* If adding to array with a bitmap, then we can accept an
1224 * older device, but not too old.
1225 */
NeilBrown41158c72005-06-21 17:17:25 -07001226 if (ev1 < mddev->bitmap->events_cleared)
1227 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001228 } else {
1229 if (ev1 < mddev->events)
1230 /* just a hot-add of a new device, leave raid_disk at -1 */
1231 return 0;
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (mddev->level != LEVEL_MULTIPATH) {
1234 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1236 switch(role) {
1237 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 break;
1239 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001240 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 break;
1242 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001243 if ((le32_to_cpu(sb->feature_map) &
1244 MD_FEATURE_RECOVERY_OFFSET))
1245 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1246 else
1247 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 rdev->raid_disk = role;
1249 break;
1250 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001251 if (sb->devflags & WriteMostly1)
1252 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001253 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001254 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001255
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return 0;
1257}
1258
1259static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1260{
1261 struct mdp_superblock_1 *sb;
1262 struct list_head *tmp;
1263 mdk_rdev_t *rdev2;
1264 int max_dev, i;
1265 /* make rdev->sb match mddev and rdev data. */
1266
1267 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1268
1269 sb->feature_map = 0;
1270 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001271 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 memset(sb->pad1, 0, sizeof(sb->pad1));
1273 memset(sb->pad2, 0, sizeof(sb->pad2));
1274 memset(sb->pad3, 0, sizeof(sb->pad3));
1275
1276 sb->utime = cpu_to_le64((__u64)mddev->utime);
1277 sb->events = cpu_to_le64(mddev->events);
1278 if (mddev->in_sync)
1279 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1280 else
1281 sb->resync_offset = cpu_to_le64(0);
1282
NeilBrown1c05b4b2006-10-21 10:24:08 -07001283 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001284
NeilBrownf0ca3402006-02-02 14:28:04 -08001285 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001286 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001287
NeilBrowna654b9d82005-06-21 17:17:27 -07001288 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1289 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001290 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001291 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001292
1293 if (rdev->raid_disk >= 0 &&
1294 !test_bit(In_sync, &rdev->flags) &&
1295 rdev->recovery_offset > 0) {
1296 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1297 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1298 }
1299
NeilBrownf6705572006-03-27 01:18:11 -08001300 if (mddev->reshape_position != MaxSector) {
1301 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1302 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1303 sb->new_layout = cpu_to_le32(mddev->new_layout);
1304 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1305 sb->new_level = cpu_to_le32(mddev->new_level);
1306 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1307 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001310 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (rdev2->desc_nr+1 > max_dev)
1312 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001313
1314 if (max_dev > le32_to_cpu(sb->max_dev))
1315 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 for (i=0; i<max_dev;i++)
1317 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1318
NeilBrownd089c6a2008-02-06 01:39:59 -08001319 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001321 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001323 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001325 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1326 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 else
1328 sb->dev_roles[i] = cpu_to_le16(0xffff);
1329 }
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 sb->sb_csum = calc_sb_1_csum(sb);
1332}
1333
Chris Webb0cd17fe2008-06-28 08:31:46 +10001334static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001335super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001336{
1337 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001338 sector_t max_sectors;
1339 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001340 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001341 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001342 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001343 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1344 max_sectors -= rdev->data_offset;
1345 if (!num_sectors || num_sectors > max_sectors)
1346 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001347 } else if (rdev->mddev->bitmap_offset) {
1348 /* minor version 0 with bitmap we can't move */
1349 return 0;
1350 } else {
1351 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001352 sector_t sb_start;
1353 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1354 sb_start &= ~(sector_t)(4*2 - 1);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001355 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1356 if (!num_sectors || num_sectors > max_sectors)
1357 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001358 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001359 }
1360 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001361 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001362 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001363 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001364 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365 rdev->sb_page);
1366 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001367 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Adrian Bunk75c96f82005-05-05 16:16:09 -07001370static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 [0] = {
1372 .name = "0.90.0",
1373 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001374 .load_super = super_90_load,
1375 .validate_super = super_90_validate,
1376 .sync_super = super_90_sync,
1377 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 },
1379 [1] = {
1380 .name = "md-1",
1381 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001382 .load_super = super_1_load,
1383 .validate_super = super_1_validate,
1384 .sync_super = super_1_sync,
1385 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 },
1387};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1390{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001391 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
NeilBrown4b809912008-07-21 17:05:25 +10001393 rcu_read_lock();
1394 rdev_for_each_rcu(rdev, mddev1)
1395 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001396 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001397 rdev2->bdev->bd_contains) {
1398 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001399 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001400 }
1401 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return 0;
1403}
1404
1405static LIST_HEAD(pending_raid_disks);
1406
1407static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1408{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001409 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001410 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001411 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001412 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (rdev->mddev) {
1415 MD_BUG();
1416 return -EINVAL;
1417 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001418
1419 /* prevent duplicates */
1420 if (find_rdev(mddev, rdev->bdev->bd_dev))
1421 return -EEXIST;
1422
NeilBrown2bf071b2006-01-06 00:20:55 -08001423 /* make sure rdev->size exceeds mddev->size */
1424 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001425 if (mddev->pers) {
1426 /* Cannot change size, so fail
1427 * If mddev->level <= 0, then we don't care
1428 * about aligning sizes (e.g. linear)
1429 */
1430 if (mddev->level > 0)
1431 return -ENOSPC;
1432 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001433 mddev->size = rdev->size;
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 /* Verify rdev->desc_nr is unique.
1437 * If it is -1, assign a free number, else
1438 * check number is not in use
1439 */
1440 if (rdev->desc_nr < 0) {
1441 int choice = 0;
1442 if (mddev->pers) choice = mddev->raid_disks;
1443 while (find_rdev_nr(mddev, choice))
1444 choice++;
1445 rdev->desc_nr = choice;
1446 } else {
1447 if (find_rdev_nr(mddev, rdev->desc_nr))
1448 return -EBUSY;
1449 }
NeilBrown19133a42005-11-08 21:39:35 -08001450 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001451 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001452 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001455 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001456
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001457 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001458 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001459
Tejun Heo0762b8b2008-08-25 19:56:12 +09001460 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001461 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1462 kobject_del(&rdev->kobj);
1463 goto fail;
1464 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001465 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1466
NeilBrown4b809912008-07-21 17:05:25 +10001467 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001468 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001470
1471 fail:
1472 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1473 b, mdname(mddev));
1474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
NeilBrown177a99b2008-02-06 01:39:56 -08001477static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001478{
1479 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1480 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001481 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001482}
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1485{
1486 char b[BDEVNAME_SIZE];
1487 if (!rdev->mddev) {
1488 MD_BUG();
1489 return;
1490 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001491 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001492 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1494 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001495 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001496 sysfs_put(rdev->sysfs_state);
1497 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001498 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001499 * writing to 'remove' to "dev/state". We also need
1500 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001501 */
NeilBrown4b809912008-07-21 17:05:25 +10001502 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001503 INIT_WORK(&rdev->del_work, md_delayed_delete);
1504 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001505 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507
1508/*
1509 * prevent the device from being mounted, repartitioned or
1510 * otherwise reused by a RAID array (or any other kernel
1511 * subsystem), by bd_claiming the device.
1512 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001513static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 int err = 0;
1516 struct block_device *bdev;
1517 char b[BDEVNAME_SIZE];
1518
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001519 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (IS_ERR(bdev)) {
1521 printk(KERN_ERR "md: could not open %s.\n",
1522 __bdevname(dev, b));
1523 return PTR_ERR(bdev);
1524 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001525 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (err) {
1527 printk(KERN_ERR "md: could not bd_claim %s.\n",
1528 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001529 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 return err;
1531 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001532 if (!shared)
1533 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 rdev->bdev = bdev;
1535 return err;
1536}
1537
1538static void unlock_rdev(mdk_rdev_t *rdev)
1539{
1540 struct block_device *bdev = rdev->bdev;
1541 rdev->bdev = NULL;
1542 if (!bdev)
1543 MD_BUG();
1544 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001545 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548void md_autodetect_dev(dev_t dev);
1549
1550static void export_rdev(mdk_rdev_t * rdev)
1551{
1552 char b[BDEVNAME_SIZE];
1553 printk(KERN_INFO "md: export_rdev(%s)\n",
1554 bdevname(rdev->bdev,b));
1555 if (rdev->mddev)
1556 MD_BUG();
1557 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001559 if (test_bit(AutoDetected, &rdev->flags))
1560 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561#endif
1562 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001563 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566static void kick_rdev_from_array(mdk_rdev_t * rdev)
1567{
1568 unbind_rdev_from_array(rdev);
1569 export_rdev(rdev);
1570}
1571
1572static void export_array(mddev_t *mddev)
1573{
1574 struct list_head *tmp;
1575 mdk_rdev_t *rdev;
1576
NeilBrownd089c6a2008-02-06 01:39:59 -08001577 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (!rdev->mddev) {
1579 MD_BUG();
1580 continue;
1581 }
1582 kick_rdev_from_array(rdev);
1583 }
1584 if (!list_empty(&mddev->disks))
1585 MD_BUG();
1586 mddev->raid_disks = 0;
1587 mddev->major_version = 0;
1588}
1589
1590static void print_desc(mdp_disk_t *desc)
1591{
1592 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1593 desc->major,desc->minor,desc->raid_disk,desc->state);
1594}
1595
1596static void print_sb(mdp_super_t *sb)
1597{
1598 int i;
1599
1600 printk(KERN_INFO
1601 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1602 sb->major_version, sb->minor_version, sb->patch_version,
1603 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1604 sb->ctime);
1605 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1606 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1607 sb->md_minor, sb->layout, sb->chunk_size);
1608 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1609 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1610 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1611 sb->failed_disks, sb->spare_disks,
1612 sb->sb_csum, (unsigned long)sb->events_lo);
1613
1614 printk(KERN_INFO);
1615 for (i = 0; i < MD_SB_DISKS; i++) {
1616 mdp_disk_t *desc;
1617
1618 desc = sb->disks + i;
1619 if (desc->number || desc->major || desc->minor ||
1620 desc->raid_disk || (desc->state && (desc->state != 4))) {
1621 printk(" D %2d: ", i);
1622 print_desc(desc);
1623 }
1624 }
1625 printk(KERN_INFO "md: THIS: ");
1626 print_desc(&sb->this_disk);
1627
1628}
1629
1630static void print_rdev(mdk_rdev_t *rdev)
1631{
1632 char b[BDEVNAME_SIZE];
1633 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1634 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001635 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1636 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 if (rdev->sb_loaded) {
1638 printk(KERN_INFO "md: rdev superblock:\n");
1639 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1640 } else
1641 printk(KERN_INFO "md: no rdev superblock!\n");
1642}
1643
Adrian Bunk5e563412006-06-26 00:27:42 -07001644static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 struct list_head *tmp, *tmp2;
1647 mdk_rdev_t *rdev;
1648 mddev_t *mddev;
1649 char b[BDEVNAME_SIZE];
1650
1651 printk("\n");
1652 printk("md: **********************************\n");
1653 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1654 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001655 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
NeilBrown32a76272005-06-21 17:17:14 -07001657 if (mddev->bitmap)
1658 bitmap_print_sb(mddev->bitmap);
1659 else
1660 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001661 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 printk("<%s>", bdevname(rdev->bdev,b));
1663 printk("\n");
1664
NeilBrownd089c6a2008-02-06 01:39:59 -08001665 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 print_rdev(rdev);
1667 }
1668 printk("md: **********************************\n");
1669 printk("\n");
1670}
1671
1672
NeilBrown42543762006-06-26 00:27:57 -07001673static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
NeilBrown42543762006-06-26 00:27:57 -07001675 /* Update each superblock (in-memory image), but
1676 * if we are allowed to, skip spares which already
1677 * have the right event counter, or have one earlier
1678 * (which would mean they aren't being marked as dirty
1679 * with the rest of the array)
1680 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 mdk_rdev_t *rdev;
1682 struct list_head *tmp;
1683
NeilBrownd089c6a2008-02-06 01:39:59 -08001684 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001685 if (rdev->sb_events == mddev->events ||
1686 (nospares &&
1687 rdev->raid_disk < 0 &&
1688 (rdev->sb_events&1)==0 &&
1689 rdev->sb_events+1 == mddev->events)) {
1690 /* Don't update this superblock */
1691 rdev->sb_loaded = 2;
1692 } else {
1693 super_types[mddev->major_version].
1694 sync_super(mddev, rdev);
1695 rdev->sb_loaded = 1;
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 }
1698}
1699
NeilBrown850b2b42006-10-03 01:15:46 -07001700static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 struct list_head *tmp;
1703 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001704 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001705 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
NeilBrown8377bc82008-04-30 00:52:28 -07001707 if (mddev->external)
1708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001710 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001711
NeilBrown850b2b42006-10-03 01:15:46 -07001712 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1713 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1714 force_change = 1;
1715 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1716 /* just a clean<-> dirty transition, possibly leave spares alone,
1717 * though if events isn't the right even/odd, we will have to do
1718 * spares after all
1719 */
1720 nospares = 1;
1721 if (force_change)
1722 nospares = 0;
1723 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001724 /* If the array is degraded, then skipping spares is both
1725 * dangerous and fairly pointless.
1726 * Dangerous because a device that was removed from the array
1727 * might have a event_count that still looks up-to-date,
1728 * so it can be re-added without a resync.
1729 * Pointless because if there are any spares to skip,
1730 * then a recovery will happen and soon that array won't
1731 * be degraded any more and the spare can go back to sleep then.
1732 */
NeilBrown850b2b42006-10-03 01:15:46 -07001733 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001734
NeilBrown06d91a52005-06-21 17:17:12 -07001735 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001737
1738 /* If this is just a dirty<->clean transition, and the array is clean
1739 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001740 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001741 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001742 && (mddev->events & 1)
1743 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001744 mddev->events--;
1745 else {
1746 /* otherwise we have to go forward and ... */
1747 mddev->events ++;
1748 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1749 /* .. if the array isn't clean, insist on an odd 'events' */
1750 if ((mddev->events&1)==0) {
1751 mddev->events++;
1752 nospares = 0;
1753 }
1754 } else {
1755 /* otherwise insist on an even 'events' (for clean states) */
1756 if ((mddev->events&1)) {
1757 mddev->events++;
1758 nospares = 0;
1759 }
1760 }
1761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 if (!mddev->events) {
1764 /*
1765 * oops, this 64-bit counter should never wrap.
1766 * Either we are in around ~1 trillion A.C., assuming
1767 * 1 reboot per second, or we have a bug:
1768 */
1769 MD_BUG();
1770 mddev->events --;
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 /*
1774 * do not write anything to disk if using
1775 * nonpersistent superblocks
1776 */
NeilBrown06d91a52005-06-21 17:17:12 -07001777 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001778 if (!mddev->external)
1779 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1780
NeilBrowna9701a32005-11-08 21:39:34 -08001781 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001782 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001784 }
NeilBrowne6910632008-02-06 01:39:51 -08001785 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001786 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 dprintk(KERN_INFO
1789 "md: updating %s RAID superblock on device (in sync %d)\n",
1790 mdname(mddev),mddev->in_sync);
1791
NeilBrown4ad13662007-07-17 04:06:13 -07001792 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001793 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 char b[BDEVNAME_SIZE];
1795 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001796 if (rdev->sb_loaded != 1)
1797 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001798 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 dprintk("(skipping faulty ");
1800
1801 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001802 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001803 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001804 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001805 rdev->sb_page);
1806 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1807 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10001808 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07001809 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 } else
1812 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001813 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 /* only need to write one superblock... */
1815 break;
1816 }
NeilBrowna9701a32005-11-08 21:39:34 -08001817 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001818 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001819
NeilBrowna9701a32005-11-08 21:39:34 -08001820 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001821 if (mddev->in_sync != sync_req ||
1822 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001823 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001824 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001825 goto repeat;
1826 }
NeilBrown850b2b42006-10-03 01:15:46 -07001827 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001828 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001829 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
Andre Noll7f6ce762008-03-23 18:34:54 +01001833/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08001834 * We want to accept with case. For this we use cmd_match.
1835 */
1836static int cmd_match(const char *cmd, const char *str)
1837{
1838 /* See if cmd, written into a sysfs file, matches
1839 * str. They must either be the same, or cmd can
1840 * have a trailing newline
1841 */
1842 while (*cmd && *str && *cmd == *str) {
1843 cmd++;
1844 str++;
1845 }
1846 if (*cmd == '\n')
1847 cmd++;
1848 if (*str || *cmd)
1849 return 0;
1850 return 1;
1851}
1852
NeilBrown86e6ffd2005-11-08 21:39:24 -08001853struct rdev_sysfs_entry {
1854 struct attribute attr;
1855 ssize_t (*show)(mdk_rdev_t *, char *);
1856 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1857};
1858
1859static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001860state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001861{
1862 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001863 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001864
NeilBrownb2d444d2005-11-08 21:39:31 -08001865 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001866 len+= sprintf(page+len, "%sfaulty",sep);
1867 sep = ",";
1868 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001869 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001870 len += sprintf(page+len, "%sin_sync",sep);
1871 sep = ",";
1872 }
NeilBrownf6556752006-06-26 00:28:01 -07001873 if (test_bit(WriteMostly, &rdev->flags)) {
1874 len += sprintf(page+len, "%swrite_mostly",sep);
1875 sep = ",";
1876 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001877 if (test_bit(Blocked, &rdev->flags)) {
1878 len += sprintf(page+len, "%sblocked", sep);
1879 sep = ",";
1880 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001881 if (!test_bit(Faulty, &rdev->flags) &&
1882 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001883 len += sprintf(page+len, "%sspare", sep);
1884 sep = ",";
1885 }
1886 return len+sprintf(page+len, "\n");
1887}
1888
NeilBrown45dc2de2006-06-26 00:27:58 -07001889static ssize_t
1890state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1891{
1892 /* can write
1893 * faulty - simulates and error
1894 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001895 * writemostly - sets write_mostly
1896 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001897 * blocked - sets the Blocked flag
1898 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001899 */
1900 int err = -EINVAL;
1901 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1902 md_error(rdev->mddev, rdev);
1903 err = 0;
1904 } else if (cmd_match(buf, "remove")) {
1905 if (rdev->raid_disk >= 0)
1906 err = -EBUSY;
1907 else {
1908 mddev_t *mddev = rdev->mddev;
1909 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001910 if (mddev->pers)
1911 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001912 md_new_event(mddev);
1913 err = 0;
1914 }
NeilBrownf6556752006-06-26 00:28:01 -07001915 } else if (cmd_match(buf, "writemostly")) {
1916 set_bit(WriteMostly, &rdev->flags);
1917 err = 0;
1918 } else if (cmd_match(buf, "-writemostly")) {
1919 clear_bit(WriteMostly, &rdev->flags);
1920 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001921 } else if (cmd_match(buf, "blocked")) {
1922 set_bit(Blocked, &rdev->flags);
1923 err = 0;
1924 } else if (cmd_match(buf, "-blocked")) {
1925 clear_bit(Blocked, &rdev->flags);
1926 wake_up(&rdev->blocked_wait);
1927 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1928 md_wakeup_thread(rdev->mddev->thread);
1929
1930 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001931 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001932 if (!err && rdev->sysfs_state)
1933 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07001934 return err ? err : len;
1935}
NeilBrown80ca3a42006-07-10 04:44:18 -07001936static struct rdev_sysfs_entry rdev_state =
1937__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001938
1939static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001940errors_show(mdk_rdev_t *rdev, char *page)
1941{
1942 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1943}
1944
1945static ssize_t
1946errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1947{
1948 char *e;
1949 unsigned long n = simple_strtoul(buf, &e, 10);
1950 if (*buf && (*e == 0 || *e == '\n')) {
1951 atomic_set(&rdev->corrected_errors, n);
1952 return len;
1953 }
1954 return -EINVAL;
1955}
1956static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001957__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001958
NeilBrown014236d2006-01-06 00:20:55 -08001959static ssize_t
1960slot_show(mdk_rdev_t *rdev, char *page)
1961{
1962 if (rdev->raid_disk < 0)
1963 return sprintf(page, "none\n");
1964 else
1965 return sprintf(page, "%d\n", rdev->raid_disk);
1966}
1967
1968static ssize_t
1969slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1970{
1971 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001972 int err;
1973 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001974 int slot = simple_strtoul(buf, &e, 10);
1975 if (strncmp(buf, "none", 4)==0)
1976 slot = -1;
1977 else if (e==buf || (*e && *e!= '\n'))
1978 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10001979 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001980 /* Setting 'slot' on an active array requires also
1981 * updating the 'rd%d' link, and communicating
1982 * with the personality with ->hot_*_disk.
1983 * For now we only support removing
1984 * failed/spare devices. This normally happens automatically,
1985 * but not when the metadata is externally managed.
1986 */
NeilBrownc303da62008-02-06 01:39:51 -08001987 if (rdev->raid_disk == -1)
1988 return -EEXIST;
1989 /* personality does all needed checks */
1990 if (rdev->mddev->pers->hot_add_disk == NULL)
1991 return -EINVAL;
1992 err = rdev->mddev->pers->
1993 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1994 if (err)
1995 return err;
1996 sprintf(nm, "rd%d", rdev->raid_disk);
1997 sysfs_remove_link(&rdev->mddev->kobj, nm);
1998 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1999 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002000 } else if (rdev->mddev->pers) {
2001 mdk_rdev_t *rdev2;
2002 struct list_head *tmp;
2003 /* Activating a spare .. or possibly reactivating
2004 * if we every get bitmaps working here.
2005 */
2006
2007 if (rdev->raid_disk != -1)
2008 return -EBUSY;
2009
2010 if (rdev->mddev->pers->hot_add_disk == NULL)
2011 return -EINVAL;
2012
2013 rdev_for_each(rdev2, tmp, rdev->mddev)
2014 if (rdev2->raid_disk == slot)
2015 return -EEXIST;
2016
2017 rdev->raid_disk = slot;
2018 if (test_bit(In_sync, &rdev->flags))
2019 rdev->saved_raid_disk = slot;
2020 else
2021 rdev->saved_raid_disk = -1;
2022 err = rdev->mddev->pers->
2023 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002024 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002025 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002026 return err;
Neil Brown52664732008-06-28 08:31:44 +10002027 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002028 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002029 sprintf(nm, "rd%d", rdev->raid_disk);
2030 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2031 printk(KERN_WARNING
2032 "md: cannot register "
2033 "%s for %s\n",
2034 nm, mdname(rdev->mddev));
2035
2036 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002037 } else {
2038 if (slot >= rdev->mddev->raid_disks)
2039 return -ENOSPC;
2040 rdev->raid_disk = slot;
2041 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002042 clear_bit(Faulty, &rdev->flags);
2043 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002044 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002045 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002046 }
NeilBrown014236d2006-01-06 00:20:55 -08002047 return len;
2048}
2049
2050
2051static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002052__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002053
NeilBrown93c8cad2006-01-06 00:20:56 -08002054static ssize_t
2055offset_show(mdk_rdev_t *rdev, char *page)
2056{
Andrew Morton6961ece2006-01-06 00:20:59 -08002057 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002058}
2059
2060static ssize_t
2061offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2062{
2063 char *e;
2064 unsigned long long offset = simple_strtoull(buf, &e, 10);
2065 if (e==buf || (*e && *e != '\n'))
2066 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002067 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002068 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002069 if (rdev->size && rdev->mddev->external)
2070 /* Must set offset before size, so overlap checks
2071 * can be sane */
2072 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002073 rdev->data_offset = offset;
2074 return len;
2075}
2076
2077static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002078__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002079
NeilBrown83303b62006-01-06 00:21:06 -08002080static ssize_t
2081rdev_size_show(mdk_rdev_t *rdev, char *page)
2082{
2083 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2084}
2085
NeilBrownc5d79ad2008-02-06 01:39:54 -08002086static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2087{
2088 /* check if two start/length pairs overlap */
2089 if (s1+l1 <= s2)
2090 return 0;
2091 if (s2+l2 <= s1)
2092 return 0;
2093 return 1;
2094}
2095
NeilBrown83303b62006-01-06 00:21:06 -08002096static ssize_t
2097rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2098{
Neil Brownd7027452008-07-12 10:37:50 +10002099 unsigned long long size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002100 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002101 mddev_t *my_mddev = rdev->mddev;
2102
Neil Brownd7027452008-07-12 10:37:50 +10002103 if (strict_strtoull(buf, 10, &size) < 0)
2104 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002105 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002106 if (my_mddev->persistent) {
2107 size = super_types[my_mddev->major_version].
Andre Noll15f4a5f2008-07-21 14:42:12 +10002108 rdev_size_change(rdev, size * 2);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002109 if (!size)
2110 return -EBUSY;
2111 } else if (!size) {
2112 size = (rdev->bdev->bd_inode->i_size >> 10);
2113 size -= rdev->data_offset/2;
2114 }
Chris Webb0cd17fe2008-06-28 08:31:46 +10002115 }
Chris Webb7d3c6f82008-10-13 11:55:11 +11002116 if (size < my_mddev->size)
2117 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002118
NeilBrown83303b62006-01-06 00:21:06 -08002119 rdev->size = size;
Neil Brownd7027452008-07-12 10:37:50 +10002120 if (size > oldsize && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002121 /* need to check that all other rdevs with the same ->bdev
2122 * do not overlap. We need to unlock the mddev to avoid
2123 * a deadlock. We have already changed rdev->size, and if
2124 * we have to change it back, we will have the lock again.
2125 */
2126 mddev_t *mddev;
2127 int overlap = 0;
2128 struct list_head *tmp, *tmp2;
2129
NeilBrown27c529b2008-03-04 14:29:33 -08002130 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002131 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002132 mdk_rdev_t *rdev2;
2133
2134 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002135 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002136 if (test_bit(AllReserved, &rdev2->flags) ||
2137 (rdev->bdev == rdev2->bdev &&
2138 rdev != rdev2 &&
Andre Nolld07bd3b2008-07-21 14:42:07 +10002139 overlaps(rdev->data_offset, rdev->size * 2,
2140 rdev2->data_offset,
2141 rdev2->size * 2))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002142 overlap = 1;
2143 break;
2144 }
2145 mddev_unlock(mddev);
2146 if (overlap) {
2147 mddev_put(mddev);
2148 break;
2149 }
2150 }
NeilBrown27c529b2008-03-04 14:29:33 -08002151 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002152 if (overlap) {
2153 /* Someone else could have slipped in a size
2154 * change here, but doing so is just silly.
2155 * We put oldsize back because we *know* it is
2156 * safe, and trust userspace not to race with
2157 * itself
2158 */
2159 rdev->size = oldsize;
2160 return -EBUSY;
2161 }
2162 }
NeilBrown83303b62006-01-06 00:21:06 -08002163 return len;
2164}
2165
2166static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002167__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002168
NeilBrown86e6ffd2005-11-08 21:39:24 -08002169static struct attribute *rdev_default_attrs[] = {
2170 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002171 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002172 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002173 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002174 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002175 NULL,
2176};
2177static ssize_t
2178rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2179{
2180 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2181 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002182 mddev_t *mddev = rdev->mddev;
2183 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002184
2185 if (!entry->show)
2186 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002187
2188 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2189 if (!rv) {
2190 if (rdev->mddev == NULL)
2191 rv = -EBUSY;
2192 else
2193 rv = entry->show(rdev, page);
2194 mddev_unlock(mddev);
2195 }
2196 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002197}
2198
2199static ssize_t
2200rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2201 const char *page, size_t length)
2202{
2203 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2204 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002205 ssize_t rv;
2206 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002207
2208 if (!entry->store)
2209 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002210 if (!capable(CAP_SYS_ADMIN))
2211 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002212 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002213 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002214 if (rdev->mddev == NULL)
2215 rv = -EBUSY;
2216 else
2217 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002218 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002219 }
2220 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002221}
2222
2223static void rdev_free(struct kobject *ko)
2224{
2225 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2226 kfree(rdev);
2227}
2228static struct sysfs_ops rdev_sysfs_ops = {
2229 .show = rdev_attr_show,
2230 .store = rdev_attr_store,
2231};
2232static struct kobj_type rdev_ktype = {
2233 .release = rdev_free,
2234 .sysfs_ops = &rdev_sysfs_ops,
2235 .default_attrs = rdev_default_attrs,
2236};
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238/*
2239 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2240 *
2241 * mark the device faulty if:
2242 *
2243 * - the device is nonexistent (zero size)
2244 * - the device has no valid superblock
2245 *
2246 * a faulty rdev _never_ has rdev->sb set.
2247 */
2248static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2249{
2250 char b[BDEVNAME_SIZE];
2251 int err;
2252 mdk_rdev_t *rdev;
2253 sector_t size;
2254
NeilBrown9ffae0c2006-01-06 00:20:32 -08002255 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (!rdev) {
2257 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2258 return ERR_PTR(-ENOMEM);
2259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 if ((err = alloc_disk_sb(rdev)))
2262 goto abort_free;
2263
NeilBrownc5d79ad2008-02-06 01:39:54 -08002264 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (err)
2266 goto abort_free;
2267
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002268 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002269
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002271 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002272 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002273 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002275 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002277 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002278 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2281 if (!size) {
2282 printk(KERN_WARNING
2283 "md: %s has zero or unknown size, marking faulty!\n",
2284 bdevname(rdev->bdev,b));
2285 err = -EINVAL;
2286 goto abort_free;
2287 }
2288
2289 if (super_format >= 0) {
2290 err = super_types[super_format].
2291 load_super(rdev, NULL, super_minor);
2292 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002293 printk(KERN_WARNING
2294 "md: %s does not have a valid v%d.%d "
2295 "superblock, not importing!\n",
2296 bdevname(rdev->bdev,b),
2297 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto abort_free;
2299 }
2300 if (err < 0) {
2301 printk(KERN_WARNING
2302 "md: could not read %s's sb, not importing!\n",
2303 bdevname(rdev->bdev,b));
2304 goto abort_free;
2305 }
2306 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002309 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 return rdev;
2312
2313abort_free:
2314 if (rdev->sb_page) {
2315 if (rdev->bdev)
2316 unlock_rdev(rdev);
2317 free_disk_sb(rdev);
2318 }
2319 kfree(rdev);
2320 return ERR_PTR(err);
2321}
2322
2323/*
2324 * Check a full RAID array for plausibility
2325 */
2326
2327
NeilBrowna757e642005-04-16 15:26:42 -07002328static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
2330 int i;
2331 struct list_head *tmp;
2332 mdk_rdev_t *rdev, *freshest;
2333 char b[BDEVNAME_SIZE];
2334
2335 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002336 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 switch (super_types[mddev->major_version].
2338 load_super(rdev, freshest, mddev->minor_version)) {
2339 case 1:
2340 freshest = rdev;
2341 break;
2342 case 0:
2343 break;
2344 default:
2345 printk( KERN_ERR \
2346 "md: fatal superblock inconsistency in %s"
2347 " -- removing from array\n",
2348 bdevname(rdev->bdev,b));
2349 kick_rdev_from_array(rdev);
2350 }
2351
2352
2353 super_types[mddev->major_version].
2354 validate_super(mddev, freshest);
2355
2356 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002357 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (rdev != freshest)
2359 if (super_types[mddev->major_version].
2360 validate_super(mddev, rdev)) {
2361 printk(KERN_WARNING "md: kicking non-fresh %s"
2362 " from array!\n",
2363 bdevname(rdev->bdev,b));
2364 kick_rdev_from_array(rdev);
2365 continue;
2366 }
2367 if (mddev->level == LEVEL_MULTIPATH) {
2368 rdev->desc_nr = i++;
2369 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002370 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002371 } else if (rdev->raid_disk >= mddev->raid_disks) {
2372 rdev->raid_disk = -1;
2373 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375 }
2376
2377
2378
2379 if (mddev->recovery_cp != MaxSector &&
2380 mddev->level >= 1)
2381 printk(KERN_ERR "md: %s: raid array is not clean"
2382 " -- starting background reconstruction\n",
2383 mdname(mddev));
2384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
2386
NeilBrown19052c02008-08-05 15:54:13 +10002387static void md_safemode_timeout(unsigned long data);
2388
NeilBrowneae17012005-11-08 21:39:23 -08002389static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002390safe_delay_show(mddev_t *mddev, char *page)
2391{
2392 int msec = (mddev->safemode_delay*1000)/HZ;
2393 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2394}
2395static ssize_t
2396safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2397{
2398 int scale=1;
2399 int dot=0;
2400 int i;
2401 unsigned long msec;
2402 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002403
NeilBrown16f17b32006-06-26 00:27:37 -07002404 /* remove a period, and count digits after it */
2405 if (len >= sizeof(buf))
2406 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002407 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002408 for (i=0; i<len; i++) {
2409 if (dot) {
2410 if (isdigit(buf[i])) {
2411 buf[i-1] = buf[i];
2412 scale *= 10;
2413 }
2414 buf[i] = 0;
2415 } else if (buf[i] == '.') {
2416 dot=1;
2417 buf[i] = 0;
2418 }
2419 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002420 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002421 return -EINVAL;
2422 msec = (msec * 1000) / scale;
2423 if (msec == 0)
2424 mddev->safemode_delay = 0;
2425 else {
NeilBrown19052c02008-08-05 15:54:13 +10002426 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002427 mddev->safemode_delay = (msec*HZ)/1000;
2428 if (mddev->safemode_delay == 0)
2429 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002430 if (mddev->safemode_delay < old_delay)
2431 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002432 }
2433 return len;
2434}
2435static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002436__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002437
2438static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002439level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002440{
NeilBrown2604b702006-01-06 00:20:36 -08002441 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002442 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002443 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002444 else if (mddev->clevel[0])
2445 return sprintf(page, "%s\n", mddev->clevel);
2446 else if (mddev->level != LEVEL_NONE)
2447 return sprintf(page, "%d\n", mddev->level);
2448 else
2449 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002450}
2451
NeilBrownd9d166c2006-01-06 00:20:51 -08002452static ssize_t
2453level_store(mddev_t *mddev, const char *buf, size_t len)
2454{
NeilBrown20a49ff2008-02-06 01:39:57 -08002455 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002456 if (mddev->pers)
2457 return -EBUSY;
2458 if (len == 0)
2459 return 0;
2460 if (len >= sizeof(mddev->clevel))
2461 return -ENOSPC;
2462 strncpy(mddev->clevel, buf, len);
2463 if (mddev->clevel[len-1] == '\n')
2464 len--;
2465 mddev->clevel[len] = 0;
2466 mddev->level = LEVEL_NONE;
2467 return rv;
2468}
2469
2470static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002471__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002472
NeilBrownd4dbd022006-06-26 00:27:59 -07002473
2474static ssize_t
2475layout_show(mddev_t *mddev, char *page)
2476{
2477 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002478 if (mddev->reshape_position != MaxSector &&
2479 mddev->layout != mddev->new_layout)
2480 return sprintf(page, "%d (%d)\n",
2481 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002482 return sprintf(page, "%d\n", mddev->layout);
2483}
2484
2485static ssize_t
2486layout_store(mddev_t *mddev, const char *buf, size_t len)
2487{
2488 char *e;
2489 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002490
2491 if (!*buf || (*e && *e != '\n'))
2492 return -EINVAL;
2493
NeilBrown08a02ec2007-05-09 02:35:38 -07002494 if (mddev->pers)
2495 return -EBUSY;
2496 if (mddev->reshape_position != MaxSector)
2497 mddev->new_layout = n;
2498 else
2499 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002500 return len;
2501}
2502static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002503__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002504
2505
NeilBrowneae17012005-11-08 21:39:23 -08002506static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002507raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002508{
NeilBrownbb636542005-11-08 21:39:45 -08002509 if (mddev->raid_disks == 0)
2510 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002511 if (mddev->reshape_position != MaxSector &&
2512 mddev->delta_disks != 0)
2513 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2514 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002515 return sprintf(page, "%d\n", mddev->raid_disks);
2516}
2517
NeilBrownda943b992006-01-06 00:20:54 -08002518static int update_raid_disks(mddev_t *mddev, int raid_disks);
2519
2520static ssize_t
2521raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2522{
NeilBrownda943b992006-01-06 00:20:54 -08002523 char *e;
2524 int rv = 0;
2525 unsigned long n = simple_strtoul(buf, &e, 10);
2526
2527 if (!*buf || (*e && *e != '\n'))
2528 return -EINVAL;
2529
2530 if (mddev->pers)
2531 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002532 else if (mddev->reshape_position != MaxSector) {
2533 int olddisks = mddev->raid_disks - mddev->delta_disks;
2534 mddev->delta_disks = n - olddisks;
2535 mddev->raid_disks = n;
2536 } else
NeilBrownda943b992006-01-06 00:20:54 -08002537 mddev->raid_disks = n;
2538 return rv ? rv : len;
2539}
2540static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002541__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002542
NeilBrown24dd4692005-11-08 21:39:26 -08002543static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002544chunk_size_show(mddev_t *mddev, char *page)
2545{
NeilBrown08a02ec2007-05-09 02:35:38 -07002546 if (mddev->reshape_position != MaxSector &&
2547 mddev->chunk_size != mddev->new_chunk)
2548 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2549 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002550 return sprintf(page, "%d\n", mddev->chunk_size);
2551}
2552
2553static ssize_t
2554chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2555{
2556 /* can only set chunk_size if array is not yet active */
2557 char *e;
2558 unsigned long n = simple_strtoul(buf, &e, 10);
2559
NeilBrown3b343802006-01-06 00:20:47 -08002560 if (!*buf || (*e && *e != '\n'))
2561 return -EINVAL;
2562
NeilBrown08a02ec2007-05-09 02:35:38 -07002563 if (mddev->pers)
2564 return -EBUSY;
2565 else if (mddev->reshape_position != MaxSector)
2566 mddev->new_chunk = n;
2567 else
2568 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002569 return len;
2570}
2571static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002572__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002573
NeilBrowna94213b2006-06-26 00:28:00 -07002574static ssize_t
2575resync_start_show(mddev_t *mddev, char *page)
2576{
2577 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2578}
2579
2580static ssize_t
2581resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2582{
NeilBrowna94213b2006-06-26 00:28:00 -07002583 char *e;
2584 unsigned long long n = simple_strtoull(buf, &e, 10);
2585
2586 if (mddev->pers)
2587 return -EBUSY;
2588 if (!*buf || (*e && *e != '\n'))
2589 return -EINVAL;
2590
2591 mddev->recovery_cp = n;
2592 return len;
2593}
2594static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002595__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002596
NeilBrown9e653b62006-06-26 00:27:58 -07002597/*
2598 * The array state can be:
2599 *
2600 * clear
2601 * No devices, no size, no level
2602 * Equivalent to STOP_ARRAY ioctl
2603 * inactive
2604 * May have some settings, but array is not active
2605 * all IO results in error
2606 * When written, doesn't tear down array, but just stops it
2607 * suspended (not supported yet)
2608 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002609 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002610 * readonly
2611 * no resync can happen. no superblocks get written.
2612 * write requests fail
2613 * read-auto
2614 * like readonly, but behaves like 'clean' on a write request.
2615 *
2616 * clean - no pending writes, but otherwise active.
2617 * When written to inactive array, starts without resync
2618 * If a write request arrives then
2619 * if metadata is known, mark 'dirty' and switch to 'active'.
2620 * if not known, block and switch to write-pending
2621 * If written to an active array that has pending writes, then fails.
2622 * active
2623 * fully active: IO and resync can be happening.
2624 * When written to inactive array, starts with resync
2625 *
2626 * write-pending
2627 * clean, but writes are blocked waiting for 'active' to be written.
2628 *
2629 * active-idle
2630 * like active, but no writes have been seen for a while (100msec).
2631 *
2632 */
2633enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2634 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002635static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002636 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2637 "write-pending", "active-idle", NULL };
2638
2639static int match_word(const char *word, char **list)
2640{
2641 int n;
2642 for (n=0; list[n]; n++)
2643 if (cmd_match(word, list[n]))
2644 break;
2645 return n;
2646}
2647
2648static ssize_t
2649array_state_show(mddev_t *mddev, char *page)
2650{
2651 enum array_state st = inactive;
2652
2653 if (mddev->pers)
2654 switch(mddev->ro) {
2655 case 1:
2656 st = readonly;
2657 break;
2658 case 2:
2659 st = read_auto;
2660 break;
2661 case 0:
2662 if (mddev->in_sync)
2663 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002664 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2665 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002666 else if (mddev->safemode)
2667 st = active_idle;
2668 else
2669 st = active;
2670 }
2671 else {
2672 if (list_empty(&mddev->disks) &&
2673 mddev->raid_disks == 0 &&
2674 mddev->size == 0)
2675 st = clear;
2676 else
2677 st = inactive;
2678 }
2679 return sprintf(page, "%s\n", array_states[st]);
2680}
2681
Neil Browndf5b20c2008-07-11 22:02:22 +10002682static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002683static int do_md_run(mddev_t * mddev);
2684static int restart_array(mddev_t *mddev);
2685
2686static ssize_t
2687array_state_store(mddev_t *mddev, const char *buf, size_t len)
2688{
2689 int err = -EINVAL;
2690 enum array_state st = match_word(buf, array_states);
2691 switch(st) {
2692 case bad_word:
2693 break;
2694 case clear:
2695 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10002696 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08002697 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002698 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002699 break;
2700 case inactive:
2701 /* stopping an active array */
2702 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10002703 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002704 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002705 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08002706 } else
2707 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002708 break;
2709 case suspended:
2710 break; /* not supported yet */
2711 case readonly:
2712 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10002713 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002714 else {
2715 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002716 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002717 err = do_md_run(mddev);
2718 }
2719 break;
2720 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002721 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11002722 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10002723 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11002724 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07002725 err = restart_array(mddev);
2726 if (err == 0) {
2727 mddev->ro = 2;
2728 set_disk_ro(mddev->gendisk, 0);
2729 }
NeilBrown9e653b62006-06-26 00:27:58 -07002730 } else {
2731 mddev->ro = 2;
2732 err = do_md_run(mddev);
2733 }
2734 break;
2735 case clean:
2736 if (mddev->pers) {
2737 restart_array(mddev);
2738 spin_lock_irq(&mddev->write_lock);
2739 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002740 if (mddev->in_sync == 0) {
2741 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002742 if (mddev->safemode == 1)
2743 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002744 if (mddev->persistent)
2745 set_bit(MD_CHANGE_CLEAN,
2746 &mddev->flags);
2747 }
2748 err = 0;
2749 } else
2750 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002751 spin_unlock_irq(&mddev->write_lock);
2752 } else {
2753 mddev->ro = 0;
2754 mddev->recovery_cp = MaxSector;
2755 err = do_md_run(mddev);
2756 }
2757 break;
2758 case active:
2759 if (mddev->pers) {
2760 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002761 if (mddev->external)
2762 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002763 wake_up(&mddev->sb_wait);
2764 err = 0;
2765 } else {
2766 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002767 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002768 err = do_md_run(mddev);
2769 }
2770 break;
2771 case write_pending:
2772 case active_idle:
2773 /* these cannot be set */
2774 break;
2775 }
2776 if (err)
2777 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002778 else {
NeilBrownb62b7592008-10-21 13:25:21 +11002779 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07002780 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002781 }
NeilBrown9e653b62006-06-26 00:27:58 -07002782}
NeilBrown80ca3a42006-07-10 04:44:18 -07002783static struct md_sysfs_entry md_array_state =
2784__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002785
NeilBrown6d7ff732006-01-06 00:21:16 -08002786static ssize_t
2787null_show(mddev_t *mddev, char *page)
2788{
2789 return -EINVAL;
2790}
2791
2792static ssize_t
2793new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2794{
2795 /* buf must be %d:%d\n? giving major and minor numbers */
2796 /* The new device is added to the array.
2797 * If the array has a persistent superblock, we read the
2798 * superblock to initialise info and check validity.
2799 * Otherwise, only checking done is that in bind_rdev_to_array,
2800 * which mainly checks size.
2801 */
2802 char *e;
2803 int major = simple_strtoul(buf, &e, 10);
2804 int minor;
2805 dev_t dev;
2806 mdk_rdev_t *rdev;
2807 int err;
2808
2809 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2810 return -EINVAL;
2811 minor = simple_strtoul(e+1, &e, 10);
2812 if (*e && *e != '\n')
2813 return -EINVAL;
2814 dev = MKDEV(major, minor);
2815 if (major != MAJOR(dev) ||
2816 minor != MINOR(dev))
2817 return -EOVERFLOW;
2818
2819
2820 if (mddev->persistent) {
2821 rdev = md_import_device(dev, mddev->major_version,
2822 mddev->minor_version);
2823 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2824 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2825 mdk_rdev_t, same_set);
2826 err = super_types[mddev->major_version]
2827 .load_super(rdev, rdev0, mddev->minor_version);
2828 if (err < 0)
2829 goto out;
2830 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002831 } else if (mddev->external)
2832 rdev = md_import_device(dev, -2, -1);
2833 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002834 rdev = md_import_device(dev, -1, -1);
2835
2836 if (IS_ERR(rdev))
2837 return PTR_ERR(rdev);
2838 err = bind_rdev_to_array(rdev, mddev);
2839 out:
2840 if (err)
2841 export_rdev(rdev);
2842 return err ? err : len;
2843}
2844
2845static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002846__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002847
2848static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002849bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2850{
2851 char *end;
2852 unsigned long chunk, end_chunk;
2853
2854 if (!mddev->bitmap)
2855 goto out;
2856 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2857 while (*buf) {
2858 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2859 if (buf == end) break;
2860 if (*end == '-') { /* range */
2861 buf = end + 1;
2862 end_chunk = simple_strtoul(buf, &end, 0);
2863 if (buf == end) break;
2864 }
2865 if (*end && !isspace(*end)) break;
2866 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2867 buf = end;
2868 while (isspace(*buf)) buf++;
2869 }
2870 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2871out:
2872 return len;
2873}
2874
2875static struct md_sysfs_entry md_bitmap =
2876__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2877
2878static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002879size_show(mddev_t *mddev, char *page)
2880{
2881 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2882}
2883
Andre Nolld71f9f82008-07-11 22:02:22 +10002884static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08002885
2886static ssize_t
2887size_store(mddev_t *mddev, const char *buf, size_t len)
2888{
2889 /* If array is inactive, we can reduce the component size, but
2890 * not increase it (except from 0).
2891 * If array is active, we can try an on-line resize
2892 */
2893 char *e;
2894 int err = 0;
2895 unsigned long long size = simple_strtoull(buf, &e, 10);
2896 if (!*buf || *buf == '\n' ||
2897 (*e && *e != '\n'))
2898 return -EINVAL;
2899
2900 if (mddev->pers) {
Andre Nolld71f9f82008-07-11 22:02:22 +10002901 err = update_size(mddev, size * 2);
NeilBrown850b2b42006-10-03 01:15:46 -07002902 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002903 } else {
2904 if (mddev->size == 0 ||
2905 mddev->size > size)
2906 mddev->size = size;
2907 else
2908 err = -ENOSPC;
2909 }
2910 return err ? err : len;
2911}
2912
2913static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002914__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002915
NeilBrown8bb93aa2006-01-06 00:20:50 -08002916
2917/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002918 * This is one of
2919 * 'none' for arrays with no metadata (good luck...)
2920 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002921 * or N.M for internally known formats
2922 */
2923static ssize_t
2924metadata_show(mddev_t *mddev, char *page)
2925{
2926 if (mddev->persistent)
2927 return sprintf(page, "%d.%d\n",
2928 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002929 else if (mddev->external)
2930 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002931 else
2932 return sprintf(page, "none\n");
2933}
2934
2935static ssize_t
2936metadata_store(mddev_t *mddev, const char *buf, size_t len)
2937{
2938 int major, minor;
2939 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11002940 /* Changing the details of 'external' metadata is
2941 * always permitted. Otherwise there must be
2942 * no devices attached to the array.
2943 */
2944 if (mddev->external && strncmp(buf, "external:", 9) == 0)
2945 ;
2946 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08002947 return -EBUSY;
2948
2949 if (cmd_match(buf, "none")) {
2950 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002951 mddev->external = 0;
2952 mddev->major_version = 0;
2953 mddev->minor_version = 90;
2954 return len;
2955 }
2956 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002957 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002958 if (namelen >= sizeof(mddev->metadata_type))
2959 namelen = sizeof(mddev->metadata_type)-1;
2960 strncpy(mddev->metadata_type, buf+9, namelen);
2961 mddev->metadata_type[namelen] = 0;
2962 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2963 mddev->metadata_type[--namelen] = 0;
2964 mddev->persistent = 0;
2965 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002966 mddev->major_version = 0;
2967 mddev->minor_version = 90;
2968 return len;
2969 }
2970 major = simple_strtoul(buf, &e, 10);
2971 if (e==buf || *e != '.')
2972 return -EINVAL;
2973 buf = e+1;
2974 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002975 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002976 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002977 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002978 return -ENOENT;
2979 mddev->major_version = major;
2980 mddev->minor_version = minor;
2981 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002982 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002983 return len;
2984}
2985
2986static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002987__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002988
NeilBrowna35b0d62006-01-06 00:20:49 -08002989static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002990action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002991{
NeilBrown7eec3142005-11-08 21:39:44 -08002992 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002993 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002994 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002995 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2996 type = "reshape";
2997 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002998 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2999 type = "resync";
3000 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3001 type = "check";
3002 else
3003 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003004 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003005 type = "recover";
3006 }
3007 return sprintf(page, "%s\n", type);
3008}
3009
3010static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003011action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003012{
NeilBrown7eec3142005-11-08 21:39:44 -08003013 if (!mddev->pers || !mddev->pers->sync_request)
3014 return -EINVAL;
3015
NeilBrownbce74da2006-01-06 00:20:41 -08003016 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003017 if (mddev->sync_thread) {
3018 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3019 md_unregister_thread(mddev->sync_thread);
3020 mddev->sync_thread = NULL;
3021 mddev->recovery = 0;
3022 }
NeilBrown03c902e2006-01-06 00:20:46 -08003023 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3024 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003025 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003026 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003027 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003028 else if (cmd_match(page, "recover")) {
3029 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3030 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3031 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003032 int err;
3033 if (mddev->pers->start_reshape == NULL)
3034 return -EINVAL;
3035 err = mddev->pers->start_reshape(mddev);
3036 if (err)
3037 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003038 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003039 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003040 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003041 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003042 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003043 return -EINVAL;
3044 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3045 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003046 }
NeilBrown03c902e2006-01-06 00:20:46 -08003047 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003048 md_wakeup_thread(mddev->thread);
Neil Brown72a23c22008-06-28 08:31:41 +10003049 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrown24dd4692005-11-08 21:39:26 -08003050 return len;
3051}
3052
NeilBrown9d888832005-11-08 21:39:26 -08003053static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003054mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003055{
3056 return sprintf(page, "%llu\n",
3057 (unsigned long long) mddev->resync_mismatches);
3058}
3059
NeilBrown80ca3a42006-07-10 04:44:18 -07003060static struct md_sysfs_entry md_scan_mode =
3061__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003062
NeilBrown96de1e62005-11-08 21:39:39 -08003063
NeilBrown80ca3a42006-07-10 04:44:18 -07003064static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003065
NeilBrown88202a02006-01-06 00:21:36 -08003066static ssize_t
3067sync_min_show(mddev_t *mddev, char *page)
3068{
3069 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3070 mddev->sync_speed_min ? "local": "system");
3071}
3072
3073static ssize_t
3074sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3075{
3076 int min;
3077 char *e;
3078 if (strncmp(buf, "system", 6)==0) {
3079 mddev->sync_speed_min = 0;
3080 return len;
3081 }
3082 min = simple_strtoul(buf, &e, 10);
3083 if (buf == e || (*e && *e != '\n') || min <= 0)
3084 return -EINVAL;
3085 mddev->sync_speed_min = min;
3086 return len;
3087}
3088
3089static struct md_sysfs_entry md_sync_min =
3090__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3091
3092static ssize_t
3093sync_max_show(mddev_t *mddev, char *page)
3094{
3095 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3096 mddev->sync_speed_max ? "local": "system");
3097}
3098
3099static ssize_t
3100sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3101{
3102 int max;
3103 char *e;
3104 if (strncmp(buf, "system", 6)==0) {
3105 mddev->sync_speed_max = 0;
3106 return len;
3107 }
3108 max = simple_strtoul(buf, &e, 10);
3109 if (buf == e || (*e && *e != '\n') || max <= 0)
3110 return -EINVAL;
3111 mddev->sync_speed_max = max;
3112 return len;
3113}
3114
3115static struct md_sysfs_entry md_sync_max =
3116__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3117
Iustin Popd7f3d292007-10-16 23:30:54 -07003118static ssize_t
3119degraded_show(mddev_t *mddev, char *page)
3120{
3121 return sprintf(page, "%d\n", mddev->degraded);
3122}
3123static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003124
3125static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003126sync_force_parallel_show(mddev_t *mddev, char *page)
3127{
3128 return sprintf(page, "%d\n", mddev->parallel_resync);
3129}
3130
3131static ssize_t
3132sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3133{
3134 long n;
3135
3136 if (strict_strtol(buf, 10, &n))
3137 return -EINVAL;
3138
3139 if (n != 0 && n != 1)
3140 return -EINVAL;
3141
3142 mddev->parallel_resync = n;
3143
3144 if (mddev->sync_thread)
3145 wake_up(&resync_wait);
3146
3147 return len;
3148}
3149
3150/* force parallel resync, even with shared block devices */
3151static struct md_sysfs_entry md_sync_force_parallel =
3152__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3153 sync_force_parallel_show, sync_force_parallel_store);
3154
3155static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003156sync_speed_show(mddev_t *mddev, char *page)
3157{
3158 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003159 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3160 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003161 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003162 db = resync - mddev->resync_mark_cnt;
3163 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003164}
3165
NeilBrown80ca3a42006-07-10 04:44:18 -07003166static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003167
3168static ssize_t
3169sync_completed_show(mddev_t *mddev, char *page)
3170{
3171 unsigned long max_blocks, resync;
3172
3173 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3174 max_blocks = mddev->resync_max_sectors;
3175 else
3176 max_blocks = mddev->size << 1;
3177
3178 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3179 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3180}
3181
NeilBrown80ca3a42006-07-10 04:44:18 -07003182static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003183
NeilBrowne464eaf2006-03-27 01:18:14 -08003184static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003185min_sync_show(mddev_t *mddev, char *page)
3186{
3187 return sprintf(page, "%llu\n",
3188 (unsigned long long)mddev->resync_min);
3189}
3190static ssize_t
3191min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3192{
3193 unsigned long long min;
3194 if (strict_strtoull(buf, 10, &min))
3195 return -EINVAL;
3196 if (min > mddev->resync_max)
3197 return -EINVAL;
3198 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3199 return -EBUSY;
3200
3201 /* Must be a multiple of chunk_size */
3202 if (mddev->chunk_size) {
3203 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3204 return -EINVAL;
3205 }
3206 mddev->resync_min = min;
3207
3208 return len;
3209}
3210
3211static struct md_sysfs_entry md_min_sync =
3212__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3213
3214static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003215max_sync_show(mddev_t *mddev, char *page)
3216{
3217 if (mddev->resync_max == MaxSector)
3218 return sprintf(page, "max\n");
3219 else
3220 return sprintf(page, "%llu\n",
3221 (unsigned long long)mddev->resync_max);
3222}
3223static ssize_t
3224max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3225{
3226 if (strncmp(buf, "max", 3) == 0)
3227 mddev->resync_max = MaxSector;
3228 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003229 unsigned long long max;
3230 if (strict_strtoull(buf, 10, &max))
3231 return -EINVAL;
3232 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003233 return -EINVAL;
3234 if (max < mddev->resync_max &&
3235 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3236 return -EBUSY;
3237
3238 /* Must be a multiple of chunk_size */
3239 if (mddev->chunk_size) {
3240 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3241 return -EINVAL;
3242 }
3243 mddev->resync_max = max;
3244 }
3245 wake_up(&mddev->recovery_wait);
3246 return len;
3247}
3248
3249static struct md_sysfs_entry md_max_sync =
3250__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3251
3252static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003253suspend_lo_show(mddev_t *mddev, char *page)
3254{
3255 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3256}
3257
3258static ssize_t
3259suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3260{
3261 char *e;
3262 unsigned long long new = simple_strtoull(buf, &e, 10);
3263
3264 if (mddev->pers->quiesce == NULL)
3265 return -EINVAL;
3266 if (buf == e || (*e && *e != '\n'))
3267 return -EINVAL;
3268 if (new >= mddev->suspend_hi ||
3269 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3270 mddev->suspend_lo = new;
3271 mddev->pers->quiesce(mddev, 2);
3272 return len;
3273 } else
3274 return -EINVAL;
3275}
3276static struct md_sysfs_entry md_suspend_lo =
3277__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3278
3279
3280static ssize_t
3281suspend_hi_show(mddev_t *mddev, char *page)
3282{
3283 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3284}
3285
3286static ssize_t
3287suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3288{
3289 char *e;
3290 unsigned long long new = simple_strtoull(buf, &e, 10);
3291
3292 if (mddev->pers->quiesce == NULL)
3293 return -EINVAL;
3294 if (buf == e || (*e && *e != '\n'))
3295 return -EINVAL;
3296 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3297 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3298 mddev->suspend_hi = new;
3299 mddev->pers->quiesce(mddev, 1);
3300 mddev->pers->quiesce(mddev, 0);
3301 return len;
3302 } else
3303 return -EINVAL;
3304}
3305static struct md_sysfs_entry md_suspend_hi =
3306__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3307
NeilBrown08a02ec2007-05-09 02:35:38 -07003308static ssize_t
3309reshape_position_show(mddev_t *mddev, char *page)
3310{
3311 if (mddev->reshape_position != MaxSector)
3312 return sprintf(page, "%llu\n",
3313 (unsigned long long)mddev->reshape_position);
3314 strcpy(page, "none\n");
3315 return 5;
3316}
3317
3318static ssize_t
3319reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3320{
3321 char *e;
3322 unsigned long long new = simple_strtoull(buf, &e, 10);
3323 if (mddev->pers)
3324 return -EBUSY;
3325 if (buf == e || (*e && *e != '\n'))
3326 return -EINVAL;
3327 mddev->reshape_position = new;
3328 mddev->delta_disks = 0;
3329 mddev->new_level = mddev->level;
3330 mddev->new_layout = mddev->layout;
3331 mddev->new_chunk = mddev->chunk_size;
3332 return len;
3333}
3334
3335static struct md_sysfs_entry md_reshape_position =
3336__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3337 reshape_position_store);
3338
NeilBrowne464eaf2006-03-27 01:18:14 -08003339
NeilBrowneae17012005-11-08 21:39:23 -08003340static struct attribute *md_default_attrs[] = {
3341 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003342 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003343 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003344 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003345 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003346 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003347 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003348 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003349 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003350 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003351 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003352 NULL,
3353};
3354
3355static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003356 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003357 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003358 &md_sync_min.attr,
3359 &md_sync_max.attr,
3360 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003361 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003362 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003363 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003364 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003365 &md_suspend_lo.attr,
3366 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003367 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003368 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003369 NULL,
3370};
NeilBrown411036f2005-11-08 21:39:40 -08003371static struct attribute_group md_redundancy_group = {
3372 .name = NULL,
3373 .attrs = md_redundancy_attrs,
3374};
3375
NeilBrowneae17012005-11-08 21:39:23 -08003376
3377static ssize_t
3378md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3379{
3380 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3381 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003382 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003383
3384 if (!entry->show)
3385 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003386 rv = mddev_lock(mddev);
3387 if (!rv) {
3388 rv = entry->show(mddev, page);
3389 mddev_unlock(mddev);
3390 }
NeilBrown96de1e62005-11-08 21:39:39 -08003391 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003392}
3393
3394static ssize_t
3395md_attr_store(struct kobject *kobj, struct attribute *attr,
3396 const char *page, size_t length)
3397{
3398 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3399 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003400 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003401
3402 if (!entry->store)
3403 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003404 if (!capable(CAP_SYS_ADMIN))
3405 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003406 rv = mddev_lock(mddev);
3407 if (!rv) {
3408 rv = entry->store(mddev, page, length);
3409 mddev_unlock(mddev);
3410 }
NeilBrown96de1e62005-11-08 21:39:39 -08003411 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003412}
3413
3414static void md_free(struct kobject *ko)
3415{
3416 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3417 kfree(mddev);
3418}
3419
3420static struct sysfs_ops md_sysfs_ops = {
3421 .show = md_attr_show,
3422 .store = md_attr_store,
3423};
3424static struct kobj_type md_ktype = {
3425 .release = md_free,
3426 .sysfs_ops = &md_sysfs_ops,
3427 .default_attrs = md_default_attrs,
3428};
3429
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430int mdp_major = 0;
3431
3432static struct kobject *md_probe(dev_t dev, int *part, void *data)
3433{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003434 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 mddev_t *mddev = mddev_find(dev);
3436 struct gendisk *disk;
3437 int partitioned = (MAJOR(dev) != MD_MAJOR);
3438 int shift = partitioned ? MdpMinorShift : 0;
3439 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003440 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 if (!mddev)
3443 return NULL;
3444
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003445 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003447 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 mddev_put(mddev);
3449 return NULL;
3450 }
3451 disk = alloc_disk(1 << shift);
3452 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003453 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 mddev_put(mddev);
3455 return NULL;
3456 }
3457 disk->major = MAJOR(dev);
3458 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003459 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003461 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 disk->fops = &md_fops;
3464 disk->private_data = mddev;
3465 disk->queue = mddev->queue;
3466 add_disk(disk);
3467 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003468 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3469 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003470 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003471 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003472 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3473 disk->disk_name);
NeilBrownb62b7592008-10-21 13:25:21 +11003474 else {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003475 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003476 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 return NULL;
3479}
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481static void md_safemode_timeout(unsigned long data)
3482{
3483 mddev_t *mddev = (mddev_t *) data;
3484
Neil Brown0fd62b82008-06-28 08:31:36 +10003485 if (!atomic_read(&mddev->writes_pending)) {
3486 mddev->safemode = 1;
3487 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11003488 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 md_wakeup_thread(mddev->thread);
3491}
3492
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003493static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495static int do_md_run(mddev_t * mddev)
3496{
NeilBrown2604b702006-01-06 00:20:36 -08003497 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 int chunk_size;
3499 struct list_head *tmp;
3500 mdk_rdev_t *rdev;
3501 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003502 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 char b[BDEVNAME_SIZE];
3504
NeilBrowna757e642005-04-16 15:26:42 -07003505 if (list_empty(&mddev->disks))
3506 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509 if (mddev->pers)
3510 return -EBUSY;
3511
3512 /*
3513 * Analyze all RAID superblock(s)
3514 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003515 if (!mddev->raid_disks) {
3516 if (!mddev->persistent)
3517 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003518 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522
NeilBrown2604b702006-01-06 00:20:36 -08003523 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 if (chunk_size > MAX_CHUNK_SIZE) {
3525 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3526 chunk_size, MAX_CHUNK_SIZE);
3527 return -EINVAL;
3528 }
3529 /*
NeilBrown4bbf3772008-10-13 11:55:12 +11003530 * chunk-size has to be a power of 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 */
3532 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003533 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 return -EINVAL;
3535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536
3537 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003538 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003539 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 continue;
3541 if (rdev->size < chunk_size / 1024) {
3542 printk(KERN_WARNING
3543 "md: Dev %s smaller than chunk_size:"
3544 " %lluk < %dk\n",
3545 bdevname(rdev->bdev,b),
3546 (unsigned long long)rdev->size,
3547 chunk_size / 1024);
3548 return -EINVAL;
3549 }
3550 }
3551 }
3552
NeilBrownd9d166c2006-01-06 00:20:51 -08003553 if (mddev->level != LEVEL_NONE)
3554 request_module("md-level-%d", mddev->level);
3555 else if (mddev->clevel[0])
3556 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557
3558 /*
3559 * Drop all container device buffers, from now on
3560 * the only valid external interface is through the md
3561 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003563 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003564 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 continue;
3566 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003567 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003568
3569 /* perform some consistency tests on the device.
3570 * We don't want the data to overlap the metadata,
3571 * Internal Bitmap issues has handled elsewhere.
3572 */
Andre Noll0f420352008-07-11 22:02:23 +10003573 if (rdev->data_offset < rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003574 if (mddev->size &&
3575 rdev->data_offset + mddev->size*2
Andre Noll0f420352008-07-11 22:02:23 +10003576 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003577 printk("md: %s: data overlaps metadata\n",
3578 mdname(mddev));
3579 return -EINVAL;
3580 }
3581 } else {
Andre Noll0f420352008-07-11 22:02:23 +10003582 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07003583 > rdev->data_offset) {
3584 printk("md: %s: metadata overlaps data\n",
3585 mdname(mddev));
3586 return -EINVAL;
3587 }
3588 }
NeilBrown3c0ee632008-10-21 13:25:28 +11003589 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 }
3591
3592 md_probe(mddev->unit, NULL, NULL);
3593 disk = mddev->gendisk;
3594 if (!disk)
3595 return -ENOMEM;
3596
3597 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003598 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003599 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003601 if (mddev->level != LEVEL_NONE)
3602 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3603 mddev->level);
3604 else
3605 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3606 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 return -EINVAL;
3608 }
NeilBrown2604b702006-01-06 00:20:36 -08003609 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003611 mddev->level = pers->level;
3612 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
NeilBrownf6705572006-03-27 01:18:11 -08003614 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003615 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003616 /* This personality cannot handle reshaping... */
3617 mddev->pers = NULL;
3618 module_put(pers->owner);
3619 return -EINVAL;
3620 }
3621
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003622 if (pers->sync_request) {
3623 /* Warn if this is a potentially silly
3624 * configuration.
3625 */
3626 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3627 mdk_rdev_t *rdev2;
3628 struct list_head *tmp2;
3629 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003630 rdev_for_each(rdev, tmp, mddev) {
3631 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003632 if (rdev < rdev2 &&
3633 rdev->bdev->bd_contains ==
3634 rdev2->bdev->bd_contains) {
3635 printk(KERN_WARNING
3636 "%s: WARNING: %s appears to be"
3637 " on the same physical disk as"
3638 " %s.\n",
3639 mdname(mddev),
3640 bdevname(rdev->bdev,b),
3641 bdevname(rdev2->bdev,b2));
3642 warned = 1;
3643 }
3644 }
3645 }
3646 if (warned)
3647 printk(KERN_WARNING
3648 "True protection against single-disk"
3649 " failure might be compromised.\n");
3650 }
3651
NeilBrown657390d2005-08-26 18:34:16 -07003652 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003654 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003655 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
NeilBrownf91de922005-11-08 21:39:36 -08003657 if (start_readonly)
3658 mddev->ro = 2; /* read-only, but switch on first write */
3659
NeilBrownb15c2e52006-01-06 00:20:16 -08003660 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01003661 if (err)
3662 printk(KERN_ERR "md: pers->run() failed ...\n");
3663 else if (mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08003664 err = bitmap_create(mddev);
3665 if (err) {
3666 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3667 mdname(mddev), err);
3668 mddev->pers->stop(mddev);
3669 }
3670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 module_put(mddev->pers->owner);
3673 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003674 bitmap_destroy(mddev);
3675 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003677 if (mddev->pers->sync_request) {
3678 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3679 printk(KERN_WARNING
3680 "md: cannot register extra attributes for %s\n",
3681 mdname(mddev));
3682 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003683 mddev->ro = 0;
3684
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 atomic_set(&mddev->writes_pending,0);
3686 mddev->safemode = 0;
3687 mddev->safemode_timer.function = md_safemode_timeout;
3688 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003689 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003691
NeilBrownd089c6a2008-02-06 01:39:59 -08003692 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003693 if (rdev->raid_disk >= 0) {
3694 char nm[20];
3695 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003696 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3697 printk("md: cannot register %s for %s\n",
3698 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
3701 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3702
NeilBrown850b2b42006-10-03 01:15:46 -07003703 if (mddev->flags)
3704 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
Andre Nollf233ea52008-07-21 17:05:22 +10003706 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
3708 /* If we call blk_queue_make_request here, it will
3709 * re-initialise max_sectors etc which may have been
3710 * refined inside -> run. So just set the bits we need to set.
3711 * Most initialisation happended when we called
3712 * blk_queue_make_request(..., md_fail_request)
3713 * earlier.
3714 */
3715 mddev->queue->queuedata = mddev;
3716 mddev->queue->make_request_fn = mddev->pers->make_request;
3717
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003718 /* If there is a partially-recovered drive we need to
3719 * start recovery here. If we leave it to md_check_recovery,
3720 * it will remove the drives and not do the right thing
3721 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003722 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003723 struct list_head *rtmp;
3724 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003725 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003726 if (rdev->raid_disk >= 0 &&
3727 !test_bit(In_sync, &rdev->flags) &&
3728 !test_bit(Faulty, &rdev->flags))
3729 /* complete an interrupted recovery */
3730 spares++;
3731 if (spares && mddev->pers->sync_request) {
3732 mddev->recovery = 0;
3733 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3734 mddev->sync_thread = md_register_thread(md_do_sync,
3735 mddev,
3736 "%s_resync");
3737 if (!mddev->sync_thread) {
3738 printk(KERN_ERR "%s: could not start resync"
3739 " thread...\n",
3740 mdname(mddev));
3741 /* leave the spares where they are, it shouldn't hurt */
3742 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003743 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003744 }
3745 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003746 md_wakeup_thread(mddev->thread);
3747 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003748
Linus Torvalds44ce62942007-05-09 18:51:36 -07003749 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003750 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11003751 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown72a23c22008-06-28 08:31:41 +10003752 sysfs_notify(&mddev->kobj, NULL, "sync_action");
Neil Browna99ac972008-06-28 08:31:43 +10003753 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09003754 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 return 0;
3756}
3757
3758static int restart_array(mddev_t *mddev)
3759{
3760 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
Andre Noll80fab1d2008-07-11 22:02:21 +10003762 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10003764 return -ENXIO;
3765 if (!mddev->pers)
3766 return -EINVAL;
3767 if (!mddev->ro)
3768 return -EBUSY;
3769 mddev->safemode = 0;
3770 mddev->ro = 0;
3771 set_disk_ro(disk, 0);
3772 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3773 mdname(mddev));
3774 /* Kick recovery or resync if necessary */
3775 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3776 md_wakeup_thread(mddev->thread);
3777 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11003778 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10003779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780}
3781
NeilBrownacc55e22006-06-26 00:27:47 -07003782/* similar to deny_write_access, but accounts for our holding a reference
3783 * to the file ourselves */
3784static int deny_bitmap_write_access(struct file * file)
3785{
3786 struct inode *inode = file->f_mapping->host;
3787
3788 spin_lock(&inode->i_lock);
3789 if (atomic_read(&inode->i_writecount) > 1) {
3790 spin_unlock(&inode->i_lock);
3791 return -ETXTBSY;
3792 }
3793 atomic_set(&inode->i_writecount, -1);
3794 spin_unlock(&inode->i_lock);
3795
3796 return 0;
3797}
3798
3799static void restore_bitmap_write_access(struct file *file)
3800{
3801 struct inode *inode = file->f_mapping->host;
3802
3803 spin_lock(&inode->i_lock);
3804 atomic_set(&inode->i_writecount, 1);
3805 spin_unlock(&inode->i_lock);
3806}
3807
NeilBrown9e653b62006-06-26 00:27:58 -07003808/* mode:
3809 * 0 - completely stop and dis-assemble array
3810 * 1 - switch to readonly
3811 * 2 - stop but do not disassemble array
3812 */
Neil Browndf5b20c2008-07-11 22:02:22 +10003813static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 int err = 0;
3816 struct gendisk *disk = mddev->gendisk;
3817
NeilBrownf2ea68c2008-07-21 17:05:25 +10003818 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10003819 printk("md: %s still in use.\n",mdname(mddev));
3820 return -EBUSY;
3821 }
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
3825 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003826 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3828 md_unregister_thread(mddev->sync_thread);
3829 mddev->sync_thread = NULL;
3830 }
3831
3832 del_timer_sync(&mddev->safemode_timer);
3833
NeilBrown9e653b62006-06-26 00:27:58 -07003834 switch(mode) {
3835 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003837 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 goto out;
3839 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003840 break;
3841 case 0: /* disassemble */
3842 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003843 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003844 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 if (mddev->ro)
3846 set_disk_ro(disk, 0);
3847 blk_queue_make_request(mddev->queue, md_fail_request);
3848 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003849 mddev->queue->merge_bvec_fn = NULL;
3850 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003851 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003852 if (mddev->pers->sync_request)
3853 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 module_put(mddev->pers->owner);
3856 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003857 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11003858 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08003859
3860 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003861 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 if (mddev->ro)
3864 mddev->ro = 0;
3865 }
NeilBrown850b2b42006-10-03 01:15:46 -07003866 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 /* mark array as shutdown cleanly */
3868 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003869 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
NeilBrown9e653b62006-06-26 00:27:58 -07003871 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003873 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 }
NeilBrown32a76272005-06-21 17:17:14 -07003875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 /*
3877 * Free resources if final stop
3878 */
NeilBrown9e653b62006-06-26 00:27:58 -07003879 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003880 mdk_rdev_t *rdev;
3881 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3884
NeilBrown978f9462006-02-02 14:28:05 -08003885 bitmap_destroy(mddev);
3886 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003887 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003888 fput(mddev->bitmap_file);
3889 mddev->bitmap_file = NULL;
3890 }
3891 mddev->bitmap_offset = 0;
3892
NeilBrownd089c6a2008-02-06 01:39:59 -08003893 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003894 if (rdev->raid_disk >= 0) {
3895 char nm[20];
3896 sprintf(nm, "rd%d", rdev->raid_disk);
3897 sysfs_remove_link(&mddev->kobj, nm);
3898 }
3899
NeilBrown177a99b2008-02-06 01:39:56 -08003900 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003901 flush_scheduled_work();
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 export_array(mddev);
3904
Andre Nollf233ea52008-07-21 17:05:22 +10003905 mddev->array_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003906 mddev->size = 0;
3907 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003908 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003909 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003910 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003911 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003912 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003913 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003914 mddev->level = LEVEL_NONE;
3915 mddev->clevel[0] = 0;
3916 mddev->flags = 0;
3917 mddev->ro = 0;
3918 mddev->metadata_type[0] = 0;
3919 mddev->chunk_size = 0;
3920 mddev->ctime = mddev->utime = 0;
3921 mddev->layout = 0;
3922 mddev->max_disks = 0;
3923 mddev->events = 0;
3924 mddev->delta_disks = 0;
3925 mddev->new_level = LEVEL_NONE;
3926 mddev->new_layout = 0;
3927 mddev->new_chunk = 0;
3928 mddev->curr_resync = 0;
3929 mddev->resync_mismatches = 0;
3930 mddev->suspend_lo = mddev->suspend_hi = 0;
3931 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3932 mddev->recovery = 0;
3933 mddev->in_sync = 0;
3934 mddev->changed = 0;
3935 mddev->degraded = 0;
3936 mddev->barriers_work = 0;
3937 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003938
NeilBrowna8a55c32006-06-26 00:27:39 -07003939 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3941 mdname(mddev));
3942 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003943 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11003944 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945out:
3946 return err;
3947}
3948
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003949#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950static void autorun_array(mddev_t *mddev)
3951{
3952 mdk_rdev_t *rdev;
3953 struct list_head *tmp;
3954 int err;
3955
NeilBrowna757e642005-04-16 15:26:42 -07003956 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958
3959 printk(KERN_INFO "md: running: ");
3960
NeilBrownd089c6a2008-02-06 01:39:59 -08003961 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 char b[BDEVNAME_SIZE];
3963 printk("<%s>", bdevname(rdev->bdev,b));
3964 }
3965 printk("\n");
3966
NeilBrownd710e132008-10-13 11:55:12 +11003967 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 if (err) {
3969 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11003970 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 }
3972}
3973
3974/*
3975 * lets try to run arrays based on all disks that have arrived
3976 * until now. (those are in pending_raid_disks)
3977 *
3978 * the method: pick the first pending disk, collect all disks with
3979 * the same UUID, remove all from the pending list and put them into
3980 * the 'same_array' list. Then order this list based on superblock
3981 * update time (freshest comes first), kick out 'old' disks and
3982 * compare superblocks. If everything's fine then run it.
3983 *
3984 * If "unit" is allocated, then bump its reference count
3985 */
3986static void autorun_devices(int part)
3987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 struct list_head *tmp;
3989 mdk_rdev_t *rdev0, *rdev;
3990 mddev_t *mddev;
3991 char b[BDEVNAME_SIZE];
3992
3993 printk(KERN_INFO "md: autorun ...\n");
3994 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003995 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003997 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 rdev0 = list_entry(pending_raid_disks.next,
3999 mdk_rdev_t, same_set);
4000
4001 printk(KERN_INFO "md: considering %s ...\n",
4002 bdevname(rdev0->bdev,b));
4003 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08004004 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 if (super_90_load(rdev, rdev0, 0) >= 0) {
4006 printk(KERN_INFO "md: adding %s ...\n",
4007 bdevname(rdev->bdev,b));
4008 list_move(&rdev->same_set, &candidates);
4009 }
4010 /*
4011 * now we have a set of devices, with all of them having
4012 * mostly sane superblocks. It's time to allocate the
4013 * mddev.
4014 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004015 if (part) {
4016 dev = MKDEV(mdp_major,
4017 rdev0->preferred_minor << MdpMinorShift);
4018 unit = MINOR(dev) >> MdpMinorShift;
4019 } else {
4020 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4021 unit = MINOR(dev);
4022 }
4023 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4025 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4026 break;
4027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
4029 md_probe(dev, NULL, NULL);
4030 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004031 if (!mddev || !mddev->gendisk) {
4032 if (mddev)
4033 mddev_put(mddev);
4034 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 "md: cannot allocate memory for md drive.\n");
4036 break;
4037 }
4038 if (mddev_lock(mddev))
4039 printk(KERN_WARNING "md: %s locked, cannot run\n",
4040 mdname(mddev));
4041 else if (mddev->raid_disks || mddev->major_version
4042 || !list_empty(&mddev->disks)) {
4043 printk(KERN_WARNING
4044 "md: %s already running, cannot run %s\n",
4045 mdname(mddev), bdevname(rdev0->bdev,b));
4046 mddev_unlock(mddev);
4047 } else {
4048 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004049 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08004050 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 list_del_init(&rdev->same_set);
4052 if (bind_rdev_to_array(rdev, mddev))
4053 export_rdev(rdev);
4054 }
4055 autorun_array(mddev);
4056 mddev_unlock(mddev);
4057 }
4058 /* on success, candidates will be empty, on error
4059 * it won't...
4060 */
NeilBrown4b809912008-07-21 17:05:25 +10004061 rdev_for_each_list(rdev, tmp, candidates) {
4062 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 mddev_put(mddev);
4066 }
4067 printk(KERN_INFO "md: ... autorun DONE.\n");
4068}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004069#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071static int get_version(void __user * arg)
4072{
4073 mdu_version_t ver;
4074
4075 ver.major = MD_MAJOR_VERSION;
4076 ver.minor = MD_MINOR_VERSION;
4077 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4078
4079 if (copy_to_user(arg, &ver, sizeof(ver)))
4080 return -EFAULT;
4081
4082 return 0;
4083}
4084
4085static int get_array_info(mddev_t * mddev, void __user * arg)
4086{
4087 mdu_array_info_t info;
4088 int nr,working,active,failed,spare;
4089 mdk_rdev_t *rdev;
4090 struct list_head *tmp;
4091
4092 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004093 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004095 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 failed++;
4097 else {
4098 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004099 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 active++;
4101 else
4102 spare++;
4103 }
4104 }
4105
4106 info.major_version = mddev->major_version;
4107 info.minor_version = mddev->minor_version;
4108 info.patch_version = MD_PATCHLEVEL_VERSION;
4109 info.ctime = mddev->ctime;
4110 info.level = mddev->level;
4111 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004112 if (info.size != mddev->size) /* overflow */
4113 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 info.nr_disks = nr;
4115 info.raid_disks = mddev->raid_disks;
4116 info.md_minor = mddev->md_minor;
4117 info.not_persistent= !mddev->persistent;
4118
4119 info.utime = mddev->utime;
4120 info.state = 0;
4121 if (mddev->in_sync)
4122 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004123 if (mddev->bitmap && mddev->bitmap_offset)
4124 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 info.active_disks = active;
4126 info.working_disks = working;
4127 info.failed_disks = failed;
4128 info.spare_disks = spare;
4129
4130 info.layout = mddev->layout;
4131 info.chunk_size = mddev->chunk_size;
4132
4133 if (copy_to_user(arg, &info, sizeof(info)))
4134 return -EFAULT;
4135
4136 return 0;
4137}
4138
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004139static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004140{
4141 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4142 char *ptr, *buf = NULL;
4143 int err = -ENOMEM;
4144
Dan Williamsb5470dc2008-06-27 21:44:04 -07004145 if (md_allow_write(mddev))
4146 file = kmalloc(sizeof(*file), GFP_NOIO);
4147 else
4148 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004149
NeilBrown32a76272005-06-21 17:17:14 -07004150 if (!file)
4151 goto out;
4152
4153 /* bitmap disabled, zero the first byte and copy out */
4154 if (!mddev->bitmap || !mddev->bitmap->file) {
4155 file->pathname[0] = '\0';
4156 goto copy_out;
4157 }
4158
4159 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4160 if (!buf)
4161 goto out;
4162
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004163 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4164 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004165 goto out;
4166
4167 strcpy(file->pathname, ptr);
4168
4169copy_out:
4170 err = 0;
4171 if (copy_to_user(arg, file, sizeof(*file)))
4172 err = -EFAULT;
4173out:
4174 kfree(buf);
4175 kfree(file);
4176 return err;
4177}
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179static int get_disk_info(mddev_t * mddev, void __user * arg)
4180{
4181 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 mdk_rdev_t *rdev;
4183
4184 if (copy_from_user(&info, arg, sizeof(info)))
4185 return -EFAULT;
4186
Andre Noll26ef3792008-07-11 22:02:21 +10004187 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 if (rdev) {
4189 info.major = MAJOR(rdev->bdev->bd_dev);
4190 info.minor = MINOR(rdev->bdev->bd_dev);
4191 info.raid_disk = rdev->raid_disk;
4192 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004193 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004195 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 info.state |= (1<<MD_DISK_ACTIVE);
4197 info.state |= (1<<MD_DISK_SYNC);
4198 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004199 if (test_bit(WriteMostly, &rdev->flags))
4200 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 } else {
4202 info.major = info.minor = 0;
4203 info.raid_disk = -1;
4204 info.state = (1<<MD_DISK_REMOVED);
4205 }
4206
4207 if (copy_to_user(arg, &info, sizeof(info)))
4208 return -EFAULT;
4209
4210 return 0;
4211}
4212
4213static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4214{
4215 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4216 mdk_rdev_t *rdev;
4217 dev_t dev = MKDEV(info->major,info->minor);
4218
4219 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4220 return -EOVERFLOW;
4221
4222 if (!mddev->raid_disks) {
4223 int err;
4224 /* expecting a device which has a superblock */
4225 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4226 if (IS_ERR(rdev)) {
4227 printk(KERN_WARNING
4228 "md: md_import_device returned %ld\n",
4229 PTR_ERR(rdev));
4230 return PTR_ERR(rdev);
4231 }
4232 if (!list_empty(&mddev->disks)) {
4233 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4234 mdk_rdev_t, same_set);
4235 int err = super_types[mddev->major_version]
4236 .load_super(rdev, rdev0, mddev->minor_version);
4237 if (err < 0) {
4238 printk(KERN_WARNING
4239 "md: %s has different UUID to %s\n",
4240 bdevname(rdev->bdev,b),
4241 bdevname(rdev0->bdev,b2));
4242 export_rdev(rdev);
4243 return -EINVAL;
4244 }
4245 }
4246 err = bind_rdev_to_array(rdev, mddev);
4247 if (err)
4248 export_rdev(rdev);
4249 return err;
4250 }
4251
4252 /*
4253 * add_new_disk can be used once the array is assembled
4254 * to add "hot spares". They must already have a superblock
4255 * written
4256 */
4257 if (mddev->pers) {
4258 int err;
4259 if (!mddev->pers->hot_add_disk) {
4260 printk(KERN_WARNING
4261 "%s: personality does not support diskops!\n",
4262 mdname(mddev));
4263 return -EINVAL;
4264 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004265 if (mddev->persistent)
4266 rdev = md_import_device(dev, mddev->major_version,
4267 mddev->minor_version);
4268 else
4269 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 if (IS_ERR(rdev)) {
4271 printk(KERN_WARNING
4272 "md: md_import_device returned %ld\n",
4273 PTR_ERR(rdev));
4274 return PTR_ERR(rdev);
4275 }
NeilBrown41158c72005-06-21 17:17:25 -07004276 /* set save_raid_disk if appropriate */
4277 if (!mddev->persistent) {
4278 if (info->state & (1<<MD_DISK_SYNC) &&
4279 info->raid_disk < mddev->raid_disks)
4280 rdev->raid_disk = info->raid_disk;
4281 else
4282 rdev->raid_disk = -1;
4283 } else
4284 super_types[mddev->major_version].
4285 validate_super(mddev, rdev);
4286 rdev->saved_raid_disk = rdev->raid_disk;
4287
NeilBrownb2d444d2005-11-08 21:39:31 -08004288 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004289 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4290 set_bit(WriteMostly, &rdev->flags);
4291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 rdev->raid_disk = -1;
4293 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004294 if (!err && !mddev->pers->hot_remove_disk) {
4295 /* If there is hot_add_disk but no hot_remove_disk
4296 * then added disks for geometry changes,
4297 * and should be added immediately.
4298 */
4299 super_types[mddev->major_version].
4300 validate_super(mddev, rdev);
4301 err = mddev->pers->hot_add_disk(mddev, rdev);
4302 if (err)
4303 unbind_rdev_from_array(rdev);
4304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (err)
4306 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004307 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004308 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004309
NeilBrown17571282006-12-10 02:20:52 -08004310 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004311 if (mddev->degraded)
4312 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004313 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004314 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return err;
4316 }
4317
4318 /* otherwise, add_new_disk is only allowed
4319 * for major_version==0 superblocks
4320 */
4321 if (mddev->major_version != 0) {
4322 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4323 mdname(mddev));
4324 return -EINVAL;
4325 }
4326
4327 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4328 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004329 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 if (IS_ERR(rdev)) {
4331 printk(KERN_WARNING
4332 "md: error, md_import_device() returned %ld\n",
4333 PTR_ERR(rdev));
4334 return PTR_ERR(rdev);
4335 }
4336 rdev->desc_nr = info->number;
4337 if (info->raid_disk < mddev->raid_disks)
4338 rdev->raid_disk = info->raid_disk;
4339 else
4340 rdev->raid_disk = -1;
4341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004343 if (info->state & (1<<MD_DISK_SYNC))
4344 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004346 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4347 set_bit(WriteMostly, &rdev->flags);
4348
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 if (!mddev->persistent) {
4350 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004351 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 } else
Andre Noll0f420352008-07-11 22:02:23 +10004353 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolle7debaa2008-07-11 22:02:23 +10004354 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
NeilBrown2bf071b2006-01-06 00:20:55 -08004356 err = bind_rdev_to_array(rdev, mddev);
4357 if (err) {
4358 export_rdev(rdev);
4359 return err;
4360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
4362
4363 return 0;
4364}
4365
4366static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4367{
4368 char b[BDEVNAME_SIZE];
4369 mdk_rdev_t *rdev;
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 rdev = find_rdev(mddev, dev);
4372 if (!rdev)
4373 return -ENXIO;
4374
4375 if (rdev->raid_disk >= 0)
4376 goto busy;
4377
4378 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004379 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004380 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
4382 return 0;
4383busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004384 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 bdevname(rdev->bdev,b), mdname(mddev));
4386 return -EBUSY;
4387}
4388
4389static int hot_add_disk(mddev_t * mddev, dev_t dev)
4390{
4391 char b[BDEVNAME_SIZE];
4392 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 mdk_rdev_t *rdev;
4394
4395 if (!mddev->pers)
4396 return -ENODEV;
4397
4398 if (mddev->major_version != 0) {
4399 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4400 " version-0 superblocks.\n",
4401 mdname(mddev));
4402 return -EINVAL;
4403 }
4404 if (!mddev->pers->hot_add_disk) {
4405 printk(KERN_WARNING
4406 "%s: personality does not support diskops!\n",
4407 mdname(mddev));
4408 return -EINVAL;
4409 }
4410
NeilBrownd710e132008-10-13 11:55:12 +11004411 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 if (IS_ERR(rdev)) {
4413 printk(KERN_WARNING
4414 "md: error, md_import_device() returned %ld\n",
4415 PTR_ERR(rdev));
4416 return -EINVAL;
4417 }
4418
4419 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004420 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 else
Andre Noll0f420352008-07-11 22:02:23 +10004422 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Andre Nolle7debaa2008-07-11 22:02:23 +10004424 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
NeilBrownb2d444d2005-11-08 21:39:31 -08004426 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 printk(KERN_WARNING
4428 "md: can not hot-add faulty %s disk to %s!\n",
4429 bdevname(rdev->bdev,b), mdname(mddev));
4430 err = -EINVAL;
4431 goto abort_export;
4432 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004433 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004435 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004436 err = bind_rdev_to_array(rdev, mddev);
4437 if (err)
4438 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
4440 /*
4441 * The rest should better be atomic, we can have disk failures
4442 * noticed in interrupt contexts ...
4443 */
4444
4445 if (rdev->desc_nr == mddev->max_disks) {
4446 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4447 mdname(mddev));
4448 err = -EBUSY;
4449 goto abort_unbind_export;
4450 }
4451
4452 rdev->raid_disk = -1;
4453
NeilBrown850b2b42006-10-03 01:15:46 -07004454 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
4456 /*
4457 * Kick recovery, maybe this spare has to be added to the
4458 * array immediately.
4459 */
4460 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4461 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004462 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 return 0;
4464
4465abort_unbind_export:
4466 unbind_rdev_from_array(rdev);
4467
4468abort_export:
4469 export_rdev(rdev);
4470 return err;
4471}
4472
NeilBrown32a76272005-06-21 17:17:14 -07004473static int set_bitmap_file(mddev_t *mddev, int fd)
4474{
4475 int err;
4476
NeilBrown36fa3062005-09-09 16:23:45 -07004477 if (mddev->pers) {
4478 if (!mddev->pers->quiesce)
4479 return -EBUSY;
4480 if (mddev->recovery || mddev->sync_thread)
4481 return -EBUSY;
4482 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004483 }
4484
NeilBrown36fa3062005-09-09 16:23:45 -07004485
4486 if (fd >= 0) {
4487 if (mddev->bitmap)
4488 return -EEXIST; /* cannot add when bitmap is present */
4489 mddev->bitmap_file = fget(fd);
4490
4491 if (mddev->bitmap_file == NULL) {
4492 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4493 mdname(mddev));
4494 return -EBADF;
4495 }
4496
4497 err = deny_bitmap_write_access(mddev->bitmap_file);
4498 if (err) {
4499 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4500 mdname(mddev));
4501 fput(mddev->bitmap_file);
4502 mddev->bitmap_file = NULL;
4503 return err;
4504 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004505 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004506 } else if (mddev->bitmap == NULL)
4507 return -ENOENT; /* cannot remove what isn't there */
4508 err = 0;
4509 if (mddev->pers) {
4510 mddev->pers->quiesce(mddev, 1);
4511 if (fd >= 0)
4512 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004513 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004514 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004515 fd = -1; /* make sure to put the file */
4516 }
NeilBrown36fa3062005-09-09 16:23:45 -07004517 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004518 }
4519 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004520 if (mddev->bitmap_file) {
4521 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004522 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004523 }
NeilBrown36fa3062005-09-09 16:23:45 -07004524 mddev->bitmap_file = NULL;
4525 }
4526
NeilBrown32a76272005-06-21 17:17:14 -07004527 return err;
4528}
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530/*
4531 * set_array_info is used two different ways
4532 * The original usage is when creating a new array.
4533 * In this usage, raid_disks is > 0 and it together with
4534 * level, size, not_persistent,layout,chunksize determine the
4535 * shape of the array.
4536 * This will always create an array with a type-0.90.0 superblock.
4537 * The newer usage is when assembling an array.
4538 * In this case raid_disks will be 0, and the major_version field is
4539 * use to determine which style super-blocks are to be found on the devices.
4540 * The minor and patch _version numbers are also kept incase the
4541 * super_block handler wishes to interpret them.
4542 */
4543static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4544{
4545
4546 if (info->raid_disks == 0) {
4547 /* just setting version number for superblock loading */
4548 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004549 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 super_types[info->major_version].name == NULL) {
4551 /* maybe try to auto-load a module? */
4552 printk(KERN_INFO
4553 "md: superblock version %d not known\n",
4554 info->major_version);
4555 return -EINVAL;
4556 }
4557 mddev->major_version = info->major_version;
4558 mddev->minor_version = info->minor_version;
4559 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004560 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 return 0;
4562 }
4563 mddev->major_version = MD_MAJOR_VERSION;
4564 mddev->minor_version = MD_MINOR_VERSION;
4565 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4566 mddev->ctime = get_seconds();
4567
4568 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004569 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 mddev->size = info->size;
4571 mddev->raid_disks = info->raid_disks;
4572 /* don't set md_minor, it is determined by which /dev/md* was
4573 * openned
4574 */
4575 if (info->state & (1<<MD_SB_CLEAN))
4576 mddev->recovery_cp = MaxSector;
4577 else
4578 mddev->recovery_cp = 0;
4579 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004580 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582 mddev->layout = info->layout;
4583 mddev->chunk_size = info->chunk_size;
4584
4585 mddev->max_disks = MD_SB_DISKS;
4586
NeilBrowne6910632008-02-06 01:39:51 -08004587 if (mddev->persistent)
4588 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004589 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590
NeilBrownb2a27032005-11-28 13:44:12 -08004591 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4592 mddev->bitmap_offset = 0;
4593
NeilBrownf6705572006-03-27 01:18:11 -08004594 mddev->reshape_position = MaxSector;
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 /*
4597 * Generate a 128 bit UUID
4598 */
4599 get_random_bytes(mddev->uuid, 16);
4600
NeilBrownf6705572006-03-27 01:18:11 -08004601 mddev->new_level = mddev->level;
4602 mddev->new_chunk = mddev->chunk_size;
4603 mddev->new_layout = mddev->layout;
4604 mddev->delta_disks = 0;
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 return 0;
4607}
4608
Andre Nolld71f9f82008-07-11 22:02:22 +10004609static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004610{
4611 mdk_rdev_t * rdev;
4612 int rv;
4613 struct list_head *tmp;
Andre Nolld71f9f82008-07-11 22:02:22 +10004614 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004615
4616 if (mddev->pers->resize == NULL)
4617 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10004618 /* The "num_sectors" is the number of sectors of each device that
4619 * is used. This can only make sense for arrays with redundancy.
4620 * linear and raid0 always use whatever space is available. We can only
4621 * consider changing this number if no resync or reconstruction is
4622 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10004623 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10004624 * of each device. If num_sectors is zero, we find the largest size
4625 * that fits.
4626
NeilBrowna35b0d62006-01-06 00:20:49 -08004627 */
4628 if (mddev->sync_thread)
4629 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10004630 if (mddev->bitmap)
4631 /* Sorry, cannot grow a bitmap yet, just remove it,
4632 * grow, and re-add.
4633 */
4634 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004635 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004636 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004637 avail = rdev->size * 2;
4638
Andre Nolld71f9f82008-07-11 22:02:22 +10004639 if (fit && (num_sectors == 0 || num_sectors > avail))
4640 num_sectors = avail;
4641 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004642 return -ENOSPC;
4643 }
Andre Nolld71f9f82008-07-11 22:02:22 +10004644 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004645 if (!rv) {
4646 struct block_device *bdev;
4647
4648 bdev = bdget_disk(mddev->gendisk, 0);
4649 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004650 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10004651 i_size_write(bdev->bd_inode,
4652 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004653 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004654 bdput(bdev);
4655 }
4656 }
4657 return rv;
4658}
4659
NeilBrownda943b992006-01-06 00:20:54 -08004660static int update_raid_disks(mddev_t *mddev, int raid_disks)
4661{
4662 int rv;
4663 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004664 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004665 return -EINVAL;
4666 if (raid_disks <= 0 ||
4667 raid_disks >= mddev->max_disks)
4668 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004669 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004670 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004671 mddev->delta_disks = raid_disks - mddev->raid_disks;
4672
4673 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004674 return rv;
4675}
4676
4677
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678/*
4679 * update_array_info is used to change the configuration of an
4680 * on-line array.
4681 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4682 * fields in the info are checked against the array.
4683 * Any differences that cannot be handled will cause an error.
4684 * Normally, only one change can be managed at a time.
4685 */
4686static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4687{
4688 int rv = 0;
4689 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004690 int state = 0;
4691
4692 /* calculate expected state,ignoring low bits */
4693 if (mddev->bitmap && mddev->bitmap_offset)
4694 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
4696 if (mddev->major_version != info->major_version ||
4697 mddev->minor_version != info->minor_version ||
4698/* mddev->patch_version != info->patch_version || */
4699 mddev->ctime != info->ctime ||
4700 mddev->level != info->level ||
4701/* mddev->layout != info->layout || */
4702 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004703 mddev->chunk_size != info->chunk_size ||
4704 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4705 ((state^info->state) & 0xfffffe00)
4706 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return -EINVAL;
4708 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004709 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 if (mddev->raid_disks != info->raid_disks) cnt++;
4711 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004712 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 if (cnt == 0) return 0;
4714 if (cnt > 1) return -EINVAL;
4715
4716 if (mddev->layout != info->layout) {
4717 /* Change layout
4718 * we don't need to do anything at the md level, the
4719 * personality will take care of it all.
4720 */
4721 if (mddev->pers->reconfig == NULL)
4722 return -EINVAL;
4723 else
4724 return mddev->pers->reconfig(mddev, info->layout, -1);
4725 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004726 if (info->size >= 0 && mddev->size != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10004727 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728
NeilBrownda943b992006-01-06 00:20:54 -08004729 if (mddev->raid_disks != info->raid_disks)
4730 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
NeilBrown36fa3062005-09-09 16:23:45 -07004732 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4733 if (mddev->pers->quiesce == NULL)
4734 return -EINVAL;
4735 if (mddev->recovery || mddev->sync_thread)
4736 return -EBUSY;
4737 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4738 /* add the bitmap */
4739 if (mddev->bitmap)
4740 return -EEXIST;
4741 if (mddev->default_bitmap_offset == 0)
4742 return -EINVAL;
4743 mddev->bitmap_offset = mddev->default_bitmap_offset;
4744 mddev->pers->quiesce(mddev, 1);
4745 rv = bitmap_create(mddev);
4746 if (rv)
4747 bitmap_destroy(mddev);
4748 mddev->pers->quiesce(mddev, 0);
4749 } else {
4750 /* remove the bitmap */
4751 if (!mddev->bitmap)
4752 return -ENOENT;
4753 if (mddev->bitmap->file)
4754 return -EINVAL;
4755 mddev->pers->quiesce(mddev, 1);
4756 bitmap_destroy(mddev);
4757 mddev->pers->quiesce(mddev, 0);
4758 mddev->bitmap_offset = 0;
4759 }
4760 }
NeilBrown850b2b42006-10-03 01:15:46 -07004761 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 return rv;
4763}
4764
4765static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4766{
4767 mdk_rdev_t *rdev;
4768
4769 if (mddev->pers == NULL)
4770 return -ENODEV;
4771
4772 rdev = find_rdev(mddev, dev);
4773 if (!rdev)
4774 return -ENODEV;
4775
4776 md_error(mddev, rdev);
4777 return 0;
4778}
4779
Andre Noll2f9618c2008-04-25 18:57:58 +02004780/*
4781 * We have a problem here : there is no easy way to give a CHS
4782 * virtual geometry. We currently pretend that we have a 2 heads
4783 * 4 sectors (with a BIG number of cylinders...). This drives
4784 * dosfs just mad... ;-)
4785 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004786static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4787{
4788 mddev_t *mddev = bdev->bd_disk->private_data;
4789
4790 geo->heads = 2;
4791 geo->sectors = 4;
4792 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4793 return 0;
4794}
4795
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796static int md_ioctl(struct inode *inode, struct file *file,
4797 unsigned int cmd, unsigned long arg)
4798{
4799 int err = 0;
4800 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 mddev_t *mddev = NULL;
4802
4803 if (!capable(CAP_SYS_ADMIN))
4804 return -EACCES;
4805
4806 /*
4807 * Commands dealing with the RAID driver but not any
4808 * particular array:
4809 */
4810 switch (cmd)
4811 {
4812 case RAID_VERSION:
4813 err = get_version(argp);
4814 goto done;
4815
4816 case PRINT_RAID_DEBUG:
4817 err = 0;
4818 md_print_devices();
4819 goto done;
4820
4821#ifndef MODULE
4822 case RAID_AUTORUN:
4823 err = 0;
4824 autostart_arrays(arg);
4825 goto done;
4826#endif
4827 default:;
4828 }
4829
4830 /*
4831 * Commands creating/starting a new array:
4832 */
4833
4834 mddev = inode->i_bdev->bd_disk->private_data;
4835
4836 if (!mddev) {
4837 BUG();
4838 goto abort;
4839 }
4840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 err = mddev_lock(mddev);
4842 if (err) {
4843 printk(KERN_INFO
4844 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4845 err, cmd);
4846 goto abort;
4847 }
4848
4849 switch (cmd)
4850 {
4851 case SET_ARRAY_INFO:
4852 {
4853 mdu_array_info_t info;
4854 if (!arg)
4855 memset(&info, 0, sizeof(info));
4856 else if (copy_from_user(&info, argp, sizeof(info))) {
4857 err = -EFAULT;
4858 goto abort_unlock;
4859 }
4860 if (mddev->pers) {
4861 err = update_array_info(mddev, &info);
4862 if (err) {
4863 printk(KERN_WARNING "md: couldn't update"
4864 " array info. %d\n", err);
4865 goto abort_unlock;
4866 }
4867 goto done_unlock;
4868 }
4869 if (!list_empty(&mddev->disks)) {
4870 printk(KERN_WARNING
4871 "md: array %s already has disks!\n",
4872 mdname(mddev));
4873 err = -EBUSY;
4874 goto abort_unlock;
4875 }
4876 if (mddev->raid_disks) {
4877 printk(KERN_WARNING
4878 "md: array %s already initialised!\n",
4879 mdname(mddev));
4880 err = -EBUSY;
4881 goto abort_unlock;
4882 }
4883 err = set_array_info(mddev, &info);
4884 if (err) {
4885 printk(KERN_WARNING "md: couldn't set"
4886 " array info. %d\n", err);
4887 goto abort_unlock;
4888 }
4889 }
4890 goto done_unlock;
4891
4892 default:;
4893 }
4894
4895 /*
4896 * Commands querying/configuring an existing array:
4897 */
NeilBrown32a76272005-06-21 17:17:14 -07004898 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004899 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004900 if ((!mddev->raid_disks && !mddev->external)
4901 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4902 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4903 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 err = -ENODEV;
4905 goto abort_unlock;
4906 }
4907
4908 /*
4909 * Commands even a read-only array can execute:
4910 */
4911 switch (cmd)
4912 {
4913 case GET_ARRAY_INFO:
4914 err = get_array_info(mddev, argp);
4915 goto done_unlock;
4916
NeilBrown32a76272005-06-21 17:17:14 -07004917 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004918 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004919 goto done_unlock;
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 case GET_DISK_INFO:
4922 err = get_disk_info(mddev, argp);
4923 goto done_unlock;
4924
4925 case RESTART_ARRAY_RW:
4926 err = restart_array(mddev);
4927 goto done_unlock;
4928
4929 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11004930 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 goto done_unlock;
4932
4933 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11004934 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 goto done_unlock;
4936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
4938
4939 /*
4940 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004941 * superblock, so we do not allow them on read-only arrays.
4942 * However non-MD ioctls (e.g. get-size) will still come through
4943 * here and hit the 'default' below, so only disallow
4944 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 */
Andre Nollbb57fc62008-04-25 19:06:35 +02004946 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004947 if (mddev->ro == 2) {
4948 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11004949 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004950 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4951 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08004952 } else {
4953 err = -EROFS;
4954 goto abort_unlock;
4955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 }
4957
4958 switch (cmd)
4959 {
4960 case ADD_NEW_DISK:
4961 {
4962 mdu_disk_info_t info;
4963 if (copy_from_user(&info, argp, sizeof(info)))
4964 err = -EFAULT;
4965 else
4966 err = add_new_disk(mddev, &info);
4967 goto done_unlock;
4968 }
4969
4970 case HOT_REMOVE_DISK:
4971 err = hot_remove_disk(mddev, new_decode_dev(arg));
4972 goto done_unlock;
4973
4974 case HOT_ADD_DISK:
4975 err = hot_add_disk(mddev, new_decode_dev(arg));
4976 goto done_unlock;
4977
4978 case SET_DISK_FAULTY:
4979 err = set_disk_faulty(mddev, new_decode_dev(arg));
4980 goto done_unlock;
4981
4982 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11004983 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 goto done_unlock;
4985
NeilBrown32a76272005-06-21 17:17:14 -07004986 case SET_BITMAP_FILE:
4987 err = set_bitmap_file(mddev, (int)arg);
4988 goto done_unlock;
4989
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 err = -EINVAL;
4992 goto abort_unlock;
4993 }
4994
4995done_unlock:
4996abort_unlock:
4997 mddev_unlock(mddev);
4998
4999 return err;
5000done:
5001 if (err)
5002 MD_BUG();
5003abort:
5004 return err;
5005}
5006
5007static int md_open(struct inode *inode, struct file *file)
5008{
5009 /*
5010 * Succeed if we can lock the mddev, which confirms that
5011 * it isn't being stopped right now.
5012 */
5013 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5014 int err;
5015
NeilBrownd63a5a72006-12-08 02:36:17 -08005016 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 goto out;
5018
5019 err = 0;
5020 mddev_get(mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005021 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 mddev_unlock(mddev);
5023
5024 check_disk_change(inode->i_bdev);
5025 out:
5026 return err;
5027}
5028
5029static int md_release(struct inode *inode, struct file * file)
5030{
5031 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5032
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005033 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005034 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035 mddev_put(mddev);
5036
5037 return 0;
5038}
5039
Linus Torvalds44ce62942007-05-09 18:51:36 -07005040static int md_media_changed(struct gendisk *disk)
5041{
5042 mddev_t *mddev = disk->private_data;
5043
5044 return mddev->changed;
5045}
5046
5047static int md_revalidate(struct gendisk *disk)
5048{
5049 mddev_t *mddev = disk->private_data;
5050
5051 mddev->changed = 0;
5052 return 0;
5053}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054static struct block_device_operations md_fops =
5055{
5056 .owner = THIS_MODULE,
5057 .open = md_open,
5058 .release = md_release,
5059 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005060 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005061 .media_changed = md_media_changed,
5062 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063};
5064
Adrian Bunk75c96f82005-05-05 16:16:09 -07005065static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
5067 mdk_thread_t *thread = arg;
5068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 /*
5070 * md_thread is a 'system-thread', it's priority should be very
5071 * high. We avoid resource deadlocks individually in each
5072 * raid personality. (RAID5 does preallocation) We also use RR and
5073 * the very same RT priority as kswapd, thus we will never get
5074 * into a priority inversion deadlock.
5075 *
5076 * we definitely have to have equal or higher priority than
5077 * bdflush, otherwise bdflush will deadlock if there are too
5078 * many dirty RAID5 blocks.
5079 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
NeilBrown6985c432005-10-19 21:23:47 -07005081 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005082 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
NeilBrown93588e22005-11-15 00:09:12 -08005084 /* We need to wait INTERRUPTIBLE so that
5085 * we don't add to the load-average.
5086 * That means we need to be sure no signals are
5087 * pending
5088 */
5089 if (signal_pending(current))
5090 flush_signals(current);
5091
5092 wait_event_interruptible_timeout
5093 (thread->wqueue,
5094 test_bit(THREAD_WAKEUP, &thread->flags)
5095 || kthread_should_stop(),
5096 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097
5098 clear_bit(THREAD_WAKEUP, &thread->flags);
5099
NeilBrown787453c2005-11-08 21:39:43 -08005100 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005102
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 return 0;
5104}
5105
5106void md_wakeup_thread(mdk_thread_t *thread)
5107{
5108 if (thread) {
5109 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5110 set_bit(THREAD_WAKEUP, &thread->flags);
5111 wake_up(&thread->wqueue);
5112 }
5113}
5114
5115mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5116 const char *name)
5117{
5118 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
NeilBrown9ffae0c2006-01-06 00:20:32 -08005120 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 if (!thread)
5122 return NULL;
5123
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 init_waitqueue_head(&thread->wqueue);
5125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 thread->run = run;
5127 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005128 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005129 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005130 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 kfree(thread);
5132 return NULL;
5133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 return thread;
5135}
5136
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137void md_unregister_thread(mdk_thread_t *thread)
5138{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005139 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005140
5141 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 kfree(thread);
5143}
5144
5145void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5146{
5147 if (!mddev) {
5148 MD_BUG();
5149 return;
5150 }
5151
NeilBrownb2d444d2005-11-08 21:39:31 -08005152 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005154
5155 if (mddev->external)
5156 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005157/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5159 mdname(mddev),
5160 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5161 __builtin_return_address(0),__builtin_return_address(1),
5162 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005163*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005164 if (!mddev->pers)
5165 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 if (!mddev->pers->error_handler)
5167 return;
5168 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005169 if (mddev->degraded)
5170 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005171 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5173 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5174 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005175 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176}
5177
5178/* seq_file implementation /proc/mdstat */
5179
5180static void status_unused(struct seq_file *seq)
5181{
5182 int i = 0;
5183 mdk_rdev_t *rdev;
5184 struct list_head *tmp;
5185
5186 seq_printf(seq, "unused devices: ");
5187
NeilBrown73c34432008-02-06 01:39:59 -08005188 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 char b[BDEVNAME_SIZE];
5190 i++;
5191 seq_printf(seq, "%s ",
5192 bdevname(rdev->bdev,b));
5193 }
5194 if (!i)
5195 seq_printf(seq, "<none>");
5196
5197 seq_printf(seq, "\n");
5198}
5199
5200
5201static void status_resync(struct seq_file *seq, mddev_t * mddev)
5202{
NeilBrown4588b422006-03-27 01:18:04 -08005203 sector_t max_blocks, resync, res;
5204 unsigned long dt, db, rt;
5205 int scale;
5206 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5209
5210 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5211 max_blocks = mddev->resync_max_sectors >> 1;
5212 else
5213 max_blocks = mddev->size;
5214
5215 /*
5216 * Should not happen.
5217 */
5218 if (!max_blocks) {
5219 MD_BUG();
5220 return;
5221 }
NeilBrown4588b422006-03-27 01:18:04 -08005222 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5223 * in a sector_t, and (max_blocks>>scale) will fit in a
5224 * u32, as those are the requirements for sector_div.
5225 * Thus 'scale' must be at least 10
5226 */
5227 scale = 10;
5228 if (sizeof(sector_t) > sizeof(unsigned long)) {
5229 while ( max_blocks/2 > (1ULL<<(scale+32)))
5230 scale++;
5231 }
5232 res = (resync>>scale)*1000;
5233 sector_div(res, (u32)((max_blocks>>scale)+1));
5234
5235 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 {
NeilBrown4588b422006-03-27 01:18:04 -08005237 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238 seq_printf(seq, "[");
5239 for (i = 0; i < x; i++)
5240 seq_printf(seq, "=");
5241 seq_printf(seq, ">");
5242 for (i = 0; i < y; i++)
5243 seq_printf(seq, ".");
5244 seq_printf(seq, "] ");
5245 }
NeilBrown4588b422006-03-27 01:18:04 -08005246 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005247 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5248 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005249 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5250 "check" :
5251 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5252 "resync" : "recovery"))),
5253 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005254 (unsigned long long) resync,
5255 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
5257 /*
5258 * We do not want to overflow, so the order of operands and
5259 * the * 100 / 100 trick are important. We do a +1 to be
5260 * safe against division by zero. We only estimate anyway.
5261 *
5262 * dt: time from mark until now
5263 * db: blocks written from mark until now
5264 * rt: remaining time
5265 */
5266 dt = ((jiffies - mddev->resync_mark) / HZ);
5267 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005268 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5269 - mddev->resync_mark_cnt;
5270 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271
5272 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5273
NeilBrownff4e8d92006-07-10 04:44:16 -07005274 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275}
5276
5277static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5278{
5279 struct list_head *tmp;
5280 loff_t l = *pos;
5281 mddev_t *mddev;
5282
5283 if (l >= 0x10000)
5284 return NULL;
5285 if (!l--)
5286 /* header */
5287 return (void*)1;
5288
5289 spin_lock(&all_mddevs_lock);
5290 list_for_each(tmp,&all_mddevs)
5291 if (!l--) {
5292 mddev = list_entry(tmp, mddev_t, all_mddevs);
5293 mddev_get(mddev);
5294 spin_unlock(&all_mddevs_lock);
5295 return mddev;
5296 }
5297 spin_unlock(&all_mddevs_lock);
5298 if (!l--)
5299 return (void*)2;/* tail */
5300 return NULL;
5301}
5302
5303static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5304{
5305 struct list_head *tmp;
5306 mddev_t *next_mddev, *mddev = v;
5307
5308 ++*pos;
5309 if (v == (void*)2)
5310 return NULL;
5311
5312 spin_lock(&all_mddevs_lock);
5313 if (v == (void*)1)
5314 tmp = all_mddevs.next;
5315 else
5316 tmp = mddev->all_mddevs.next;
5317 if (tmp != &all_mddevs)
5318 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5319 else {
5320 next_mddev = (void*)2;
5321 *pos = 0x10000;
5322 }
5323 spin_unlock(&all_mddevs_lock);
5324
5325 if (v != (void*)1)
5326 mddev_put(mddev);
5327 return next_mddev;
5328
5329}
5330
5331static void md_seq_stop(struct seq_file *seq, void *v)
5332{
5333 mddev_t *mddev = v;
5334
5335 if (mddev && v != (void*)1 && v != (void*)2)
5336 mddev_put(mddev);
5337}
5338
NeilBrownd7603b72006-01-06 00:20:30 -08005339struct mdstat_info {
5340 int event;
5341};
5342
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343static int md_seq_show(struct seq_file *seq, void *v)
5344{
5345 mddev_t *mddev = v;
5346 sector_t size;
5347 struct list_head *tmp2;
5348 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005349 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005350 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351
5352 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005353 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 seq_printf(seq, "Personalities : ");
5355 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005356 list_for_each_entry(pers, &pers_list, list)
5357 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
5359 spin_unlock(&pers_lock);
5360 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005361 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 return 0;
5363 }
5364 if (v == (void*)2) {
5365 status_unused(seq);
5366 return 0;
5367 }
5368
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005369 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005371
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5373 seq_printf(seq, "%s : %sactive", mdname(mddev),
5374 mddev->pers ? "" : "in");
5375 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005376 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005378 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005379 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 seq_printf(seq, " %s", mddev->pers->name);
5381 }
5382
5383 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005384 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 char b[BDEVNAME_SIZE];
5386 seq_printf(seq, " %s[%d]",
5387 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005388 if (test_bit(WriteMostly, &rdev->flags))
5389 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005390 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 seq_printf(seq, "(F)");
5392 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005393 } else if (rdev->raid_disk < 0)
5394 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 size += rdev->size;
5396 }
5397
5398 if (!list_empty(&mddev->disks)) {
5399 if (mddev->pers)
5400 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005401 (unsigned long long)
5402 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 else
5404 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005405 (unsigned long long)size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005407 if (mddev->persistent) {
5408 if (mddev->major_version != 0 ||
5409 mddev->minor_version != 90) {
5410 seq_printf(seq," super %d.%d",
5411 mddev->major_version,
5412 mddev->minor_version);
5413 }
NeilBrowne6910632008-02-06 01:39:51 -08005414 } else if (mddev->external)
5415 seq_printf(seq, " super external:%s",
5416 mddev->metadata_type);
5417 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005418 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
5420 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005421 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005423 if (mddev->pers->sync_request) {
5424 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005425 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005426 seq_printf(seq, "\n ");
5427 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5428 seq_printf(seq, "\tresync=DELAYED\n ");
5429 else if (mddev->recovery_cp < MaxSector)
5430 seq_printf(seq, "\tresync=PENDING\n ");
5431 }
NeilBrown32a76272005-06-21 17:17:14 -07005432 } else
5433 seq_printf(seq, "\n ");
5434
5435 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005436 unsigned long chunk_kb;
5437 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005438 spin_lock_irqsave(&bitmap->lock, flags);
5439 chunk_kb = bitmap->chunksize >> 10;
5440 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5441 "%lu%s chunk",
5442 bitmap->pages - bitmap->missing_pages,
5443 bitmap->pages,
5444 (bitmap->pages - bitmap->missing_pages)
5445 << (PAGE_SHIFT - 10),
5446 chunk_kb ? chunk_kb : bitmap->chunksize,
5447 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005448 if (bitmap->file) {
5449 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005450 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005451 }
NeilBrown78d742d2005-06-21 17:17:15 -07005452
NeilBrown32a76272005-06-21 17:17:14 -07005453 seq_printf(seq, "\n");
5454 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 }
5456
5457 seq_printf(seq, "\n");
5458 }
5459 mddev_unlock(mddev);
5460
5461 return 0;
5462}
5463
5464static struct seq_operations md_seq_ops = {
5465 .start = md_seq_start,
5466 .next = md_seq_next,
5467 .stop = md_seq_stop,
5468 .show = md_seq_show,
5469};
5470
5471static int md_seq_open(struct inode *inode, struct file *file)
5472{
5473 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005474 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5475 if (mi == NULL)
5476 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
5478 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005479 if (error)
5480 kfree(mi);
5481 else {
5482 struct seq_file *p = file->private_data;
5483 p->private = mi;
5484 mi->event = atomic_read(&md_event_count);
5485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 return error;
5487}
5488
NeilBrownd7603b72006-01-06 00:20:30 -08005489static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5490{
5491 struct seq_file *m = filp->private_data;
5492 struct mdstat_info *mi = m->private;
5493 int mask;
5494
5495 poll_wait(filp, &md_event_waiters, wait);
5496
5497 /* always allow read */
5498 mask = POLLIN | POLLRDNORM;
5499
5500 if (mi->event != atomic_read(&md_event_count))
5501 mask |= POLLERR | POLLPRI;
5502 return mask;
5503}
5504
Arjan van de Venfa027c22007-02-12 00:55:33 -08005505static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005506 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 .open = md_seq_open,
5508 .read = seq_read,
5509 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005510 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005511 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512};
5513
NeilBrown2604b702006-01-06 00:20:36 -08005514int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005517 list_add_tail(&p->list, &pers_list);
5518 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 spin_unlock(&pers_lock);
5520 return 0;
5521}
5522
NeilBrown2604b702006-01-06 00:20:36 -08005523int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524{
NeilBrown2604b702006-01-06 00:20:36 -08005525 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005527 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 spin_unlock(&pers_lock);
5529 return 0;
5530}
5531
5532static int is_mddev_idle(mddev_t *mddev)
5533{
5534 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005536 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
5538 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10005539 rcu_read_lock();
5540 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Tejun Heo074a7ac2008-08-25 19:56:14 +09005542 curr_events = part_stat_read(&disk->part0, sectors[0]) +
5543 part_stat_read(&disk->part0, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005545 /* sync IO will cause sync_io to increase before the disk_stats
5546 * as sync_io is counted when a request starts, and
5547 * disk_stats is counted when it completes.
5548 * So resync activity will cause curr_events to be smaller than
5549 * when there was no such activity.
5550 * non-sync IO will cause disk_stat to increase without
5551 * increasing sync_io so curr_events will (eventually)
5552 * be larger than it was before. Once it becomes
5553 * substantially larger, the test below will cause
5554 * the array to appear non-idle, and resync will slow
5555 * down.
5556 * If there is a lot of outstanding resync activity when
5557 * we set last_event to curr_events, then all that activity
5558 * completing might cause the array to appear non-idle
5559 * and resync will be slowed down even though there might
5560 * not have been non-resync activity. This will only
5561 * happen once though. 'last_events' will soon reflect
5562 * the state where there is little or no outstanding
5563 * resync requests, and further resync activity will
5564 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005565 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005567 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 rdev->last_events = curr_events;
5569 idle = 0;
5570 }
5571 }
NeilBrown4b809912008-07-21 17:05:25 +10005572 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 return idle;
5574}
5575
5576void md_done_sync(mddev_t *mddev, int blocks, int ok)
5577{
5578 /* another "blocks" (512byte) blocks have been synced */
5579 atomic_sub(blocks, &mddev->recovery_active);
5580 wake_up(&mddev->recovery_wait);
5581 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005582 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 md_wakeup_thread(mddev->thread);
5584 // stop recovery, signal do_sync ....
5585 }
5586}
5587
5588
NeilBrown06d91a52005-06-21 17:17:12 -07005589/* md_write_start(mddev, bi)
5590 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005591 * in superblock) before writing, schedule a superblock update
5592 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005593 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005594void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595{
Neil Brown0fd62b82008-06-28 08:31:36 +10005596 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005597 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005598 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005599
NeilBrownf91de922005-11-08 21:39:36 -08005600 BUG_ON(mddev->ro == 1);
5601 if (mddev->ro == 2) {
5602 /* need to switch to read/write */
5603 mddev->ro = 0;
5604 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5605 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005606 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005607 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005608 }
NeilBrown06d91a52005-06-21 17:17:12 -07005609 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005610 if (mddev->safemode == 1)
5611 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005612 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005613 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005614 if (mddev->in_sync) {
5615 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005616 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005617 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005618 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005619 }
NeilBrowna9701a32005-11-08 21:39:34 -08005620 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005621 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005622 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11005623 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07005624 wait_event(mddev->sb_wait,
5625 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5626 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627}
5628
5629void md_write_end(mddev_t *mddev)
5630{
5631 if (atomic_dec_and_test(&mddev->writes_pending)) {
5632 if (mddev->safemode == 2)
5633 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005634 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5636 }
5637}
5638
NeilBrown2a2275d2007-01-26 00:57:11 -08005639/* md_allow_write(mddev)
5640 * Calling this ensures that the array is marked 'active' so that writes
5641 * may proceed without blocking. It is important to call this before
5642 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5643 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005644 *
5645 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5646 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005647 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005648int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005649{
5650 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005651 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005652 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005653 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005654 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005655 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005656
5657 spin_lock_irq(&mddev->write_lock);
5658 if (mddev->in_sync) {
5659 mddev->in_sync = 0;
5660 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5661 if (mddev->safemode_delay &&
5662 mddev->safemode == 0)
5663 mddev->safemode = 1;
5664 spin_unlock_irq(&mddev->write_lock);
5665 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11005666 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08005667 } else
5668 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005669
5670 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5671 return -EAGAIN;
5672 else
5673 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005674}
5675EXPORT_SYMBOL_GPL(md_allow_write);
5676
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677#define SYNC_MARKS 10
5678#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005679void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
5681 mddev_t *mddev2;
5682 unsigned int currspeed = 0,
5683 window;
NeilBrown57afd892005-06-21 17:17:13 -07005684 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 unsigned long mark[SYNC_MARKS];
5686 sector_t mark_cnt[SYNC_MARKS];
5687 int last_mark,m;
5688 struct list_head *tmp;
5689 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005690 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005691 struct list_head *rtmp;
5692 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005693 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694
5695 /* just incase thread restarts... */
5696 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5697 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005698 if (mddev->ro) /* never try to sync a read-only array */
5699 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700
NeilBrown61df9d92006-10-03 01:15:57 -07005701 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5702 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5703 desc = "data-check";
5704 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5705 desc = "requested-resync";
5706 else
5707 desc = "resync";
5708 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5709 desc = "reshape";
5710 else
5711 desc = "recovery";
5712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 /* we overload curr_resync somewhat here.
5714 * 0 == not engaged in resync at all
5715 * 2 == checking that there is no conflict with another sync
5716 * 1 == like 2, but have yielded to allow conflicting resync to
5717 * commense
5718 * other == active in resync - this many blocks
5719 *
5720 * Before starting a resync we must have set curr_resync to
5721 * 2, and then checked that every "conflicting" array has curr_resync
5722 * less than ours. When we find one that is the same or higher
5723 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5724 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5725 * This will mean we have to start checking from the beginning again.
5726 *
5727 */
5728
5729 do {
5730 mddev->curr_resync = 2;
5731
5732 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005733 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005734 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 goto skip;
5736 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005737 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 if (mddev2 == mddev)
5739 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005740 if (!mddev->parallel_resync
5741 && mddev2->curr_resync
5742 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 DEFINE_WAIT(wq);
5744 if (mddev < mddev2 && mddev->curr_resync == 2) {
5745 /* arbitrarily yield */
5746 mddev->curr_resync = 1;
5747 wake_up(&resync_wait);
5748 }
5749 if (mddev > mddev2 && mddev->curr_resync == 1)
5750 /* no need to wait here, we can wait the next
5751 * time 'round when curr_resync == 2
5752 */
5753 continue;
NeilBrown97441972008-09-19 11:49:54 +10005754 /* We need to wait 'interruptible' so as not to
5755 * contribute to the load average, and not to
5756 * be caught by 'softlockup'
5757 */
5758 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08005759 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005760 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005761 printk(KERN_INFO "md: delaying %s of %s"
5762 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005764 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10005766 if (signal_pending(current))
5767 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 schedule();
5769 finish_wait(&resync_wait, &wq);
5770 goto try_again;
5771 }
5772 finish_wait(&resync_wait, &wq);
5773 }
5774 }
5775 } while (mddev->curr_resync < 2);
5776
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005777 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005778 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005780 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 */
5782 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005783 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005784 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005785 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5786 j = mddev->resync_min;
5787 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005788 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005789
NeilBrownccfcc3c2006-03-27 01:18:09 -08005790 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5791 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005792 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 /* recovery follows the physical size of devices */
5794 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005795 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005796 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005797 if (rdev->raid_disk >= 0 &&
5798 !test_bit(Faulty, &rdev->flags) &&
5799 !test_bit(In_sync, &rdev->flags) &&
5800 rdev->recovery_offset < j)
5801 j = rdev->recovery_offset;
5802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
NeilBrown61df9d92006-10-03 01:15:57 -07005804 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5805 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5806 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005807 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005808 "(but not more than %d KB/sec) for %s.\n",
5809 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
5811 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005812
NeilBrown57afd892005-06-21 17:17:13 -07005813 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 for (m = 0; m < SYNC_MARKS; m++) {
5815 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005816 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 }
5818 last_mark = 0;
5819 mddev->resync_mark = mark[last_mark];
5820 mddev->resync_mark_cnt = mark_cnt[last_mark];
5821
5822 /*
5823 * Tune reconstruction:
5824 */
5825 window = 32*(PAGE_SIZE/512);
5826 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5827 window/2,(unsigned long long) max_sectors/2);
5828
5829 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 last_check = 0;
5831
5832 if (j>2) {
5833 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005834 "md: resuming %s of %s from checkpoint.\n",
5835 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 mddev->curr_resync = j;
5837 }
5838
5839 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005840 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
NeilBrown57afd892005-06-21 17:17:13 -07005842 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005843 if (j >= mddev->resync_max) {
5844 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5845 wait_event(mddev->recovery_wait,
5846 mddev->resync_max > j
5847 || kthread_should_stop());
5848 }
5849 if (kthread_should_stop())
5850 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005851 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005852 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005853 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005854 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 goto out;
5856 }
NeilBrown57afd892005-06-21 17:17:13 -07005857
5858 if (!skipped) { /* actual IO requested */
5859 io_sectors += sectors;
5860 atomic_add(sectors, &mddev->recovery_active);
5861 }
5862
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 j += sectors;
5864 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005865 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005866 if (last_check == 0)
5867 /* this is the earliers that rebuilt will be
5868 * visible in /proc/mdstat
5869 */
5870 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005871
5872 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 continue;
5874
NeilBrown57afd892005-06-21 17:17:13 -07005875 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876
NeilBrowndfc70642008-05-23 13:04:39 -07005877 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878 break;
5879
5880 repeat:
5881 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5882 /* step marks */
5883 int next = (last_mark+1) % SYNC_MARKS;
5884
5885 mddev->resync_mark = mark[next];
5886 mddev->resync_mark_cnt = mark_cnt[next];
5887 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005888 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 last_mark = next;
5890 }
5891
5892
NeilBrownc6207272008-02-06 01:39:52 -08005893 if (kthread_should_stop())
5894 goto interrupted;
5895
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
5897 /*
5898 * this loop exits only if either when we are slower than
5899 * the 'hard' speed limit, or the system was IO-idle for
5900 * a jiffy.
5901 * the system might be non-idle CPU-wise, but we only care
5902 * about not overloading the IO subsystem. (things like an
5903 * e2fsck being done on the RAID array should execute fast)
5904 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005905 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 cond_resched();
5907
NeilBrown57afd892005-06-21 17:17:13 -07005908 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5909 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910
NeilBrown88202a02006-01-06 00:21:36 -08005911 if (currspeed > speed_min(mddev)) {
5912 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005914 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 goto repeat;
5916 }
5917 }
5918 }
NeilBrown61df9d92006-10-03 01:15:57 -07005919 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 /*
5921 * this also signals 'finished resyncing' to md_stop
5922 */
5923 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005924 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
5926 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5927
5928 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005929 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
NeilBrowndfc70642008-05-23 13:04:39 -07005931 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005932 mddev->curr_resync > 2) {
5933 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5934 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5935 if (mddev->curr_resync >= mddev->recovery_cp) {
5936 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005937 "md: checkpointing %s of %s.\n",
5938 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005939 mddev->recovery_cp = mddev->curr_resync;
5940 }
5941 } else
5942 mddev->recovery_cp = MaxSector;
5943 } else {
5944 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5945 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005946 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005947 if (rdev->raid_disk >= 0 &&
5948 !test_bit(Faulty, &rdev->flags) &&
5949 !test_bit(In_sync, &rdev->flags) &&
5950 rdev->recovery_offset < mddev->curr_resync)
5951 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 }
NeilBrown17571282006-12-10 02:20:52 -08005954 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 skip:
5957 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005958 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005959 mddev->resync_max = MaxSector;
5960 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 wake_up(&resync_wait);
5962 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5963 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005964 return;
5965
5966 interrupted:
5967 /*
5968 * got a signal, exit.
5969 */
5970 printk(KERN_INFO
5971 "md: md_do_sync() got signal ... exiting\n");
5972 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5973 goto out;
5974
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975}
NeilBrown29269552006-03-27 01:18:10 -08005976EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977
5978
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005979static int remove_and_add_spares(mddev_t *mddev)
5980{
5981 mdk_rdev_t *rdev;
5982 struct list_head *rtmp;
5983 int spares = 0;
5984
NeilBrownd089c6a2008-02-06 01:39:59 -08005985 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005986 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005987 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005988 (test_bit(Faulty, &rdev->flags) ||
5989 ! test_bit(In_sync, &rdev->flags)) &&
5990 atomic_read(&rdev->nr_pending)==0) {
5991 if (mddev->pers->hot_remove_disk(
5992 mddev, rdev->raid_disk)==0) {
5993 char nm[20];
5994 sprintf(nm,"rd%d", rdev->raid_disk);
5995 sysfs_remove_link(&mddev->kobj, nm);
5996 rdev->raid_disk = -1;
5997 }
5998 }
5999
NeilBrownc89a8ee2008-08-05 15:54:13 +10006000 if (mddev->degraded && ! mddev->ro) {
NeilBrowndfc70642008-05-23 13:04:39 -07006001 rdev_for_each(rdev, rtmp, mddev) {
6002 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006003 !test_bit(In_sync, &rdev->flags) &&
6004 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006005 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006006 if (rdev->raid_disk < 0
6007 && !test_bit(Faulty, &rdev->flags)) {
6008 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006009 if (mddev->pers->
6010 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006011 char nm[20];
6012 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006013 if (sysfs_create_link(&mddev->kobj,
6014 &rdev->kobj, nm))
6015 printk(KERN_WARNING
6016 "md: cannot register "
6017 "%s for %s\n",
6018 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006019 spares++;
6020 md_new_event(mddev);
6021 } else
6022 break;
6023 }
NeilBrowndfc70642008-05-23 13:04:39 -07006024 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006025 }
6026 return spares;
6027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028/*
6029 * This routine is regularly called by all per-raid-array threads to
6030 * deal with generic issues like resync and super-block update.
6031 * Raid personalities that don't have a thread (linear/raid0) do not
6032 * need this as they never do any recovery or update the superblock.
6033 *
6034 * It does not do any resync itself, but rather "forks" off other threads
6035 * to do that as needed.
6036 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6037 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006038 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039 * and wakeups up this thread which will reap the thread and finish up.
6040 * This thread also removes any faulty devices (with nr_pending == 0).
6041 *
6042 * The overall approach is:
6043 * 1/ if the superblock needs updating, update it.
6044 * 2/ If a recovery thread is running, don't do anything else.
6045 * 3/ If recovery has finished, clean up, possibly marking spares active.
6046 * 4/ If there are any faulty devices, remove them.
6047 * 5/ If array is degraded, try to add spares devices
6048 * 6/ If array has spares or is not in-sync, start a resync thread.
6049 */
6050void md_check_recovery(mddev_t *mddev)
6051{
6052 mdk_rdev_t *rdev;
6053 struct list_head *rtmp;
6054
6055
NeilBrown5f404022005-06-21 17:17:16 -07006056 if (mddev->bitmap)
6057 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
6059 if (mddev->ro)
6060 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006061
6062 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006063 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006064 printk(KERN_INFO "md: %s in immediate safe mode\n",
6065 mdname(mddev));
6066 mddev->safemode = 2;
6067 }
6068 flush_signals(current);
6069 }
6070
NeilBrownc89a8ee2008-08-05 15:54:13 +10006071 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6072 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006074 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006076 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006077 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006078 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6079 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080 ))
6081 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006082
NeilBrowndf5b89b2006-03-27 01:18:20 -08006083 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006084 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006085
NeilBrownc89a8ee2008-08-05 15:54:13 +10006086 if (mddev->ro) {
6087 /* Only thing we do on a ro array is remove
6088 * failed devices.
6089 */
6090 remove_and_add_spares(mddev);
6091 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6092 goto unlock;
6093 }
6094
NeilBrown31a59e32008-04-30 00:52:30 -07006095 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006096 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006097 spin_lock_irq(&mddev->write_lock);
6098 if (mddev->safemode &&
6099 !atomic_read(&mddev->writes_pending) &&
6100 !mddev->in_sync &&
6101 mddev->recovery_cp == MaxSector) {
6102 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006103 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006104 if (mddev->persistent)
6105 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6106 }
6107 if (mddev->safemode == 1)
6108 mddev->safemode = 0;
6109 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006110 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006111 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006112 }
NeilBrownfca4d842005-06-21 17:17:11 -07006113
NeilBrown850b2b42006-10-03 01:15:46 -07006114 if (mddev->flags)
6115 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006116
Neil Brown52664732008-06-28 08:31:44 +10006117 rdev_for_each(rdev, rtmp, mddev)
6118 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006119 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006120
NeilBrown06d91a52005-06-21 17:17:12 -07006121
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6123 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6124 /* resync/recovery still happening */
6125 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6126 goto unlock;
6127 }
6128 if (mddev->sync_thread) {
6129 /* resync has finished, collect result */
6130 md_unregister_thread(mddev->sync_thread);
6131 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006132 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6133 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 /* success...*/
6135 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006136 if (mddev->pers->spare_active(mddev))
6137 sysfs_notify(&mddev->kobj, NULL,
6138 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139 }
NeilBrown850b2b42006-10-03 01:15:46 -07006140 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006141
6142 /* if array is no-longer degraded, then any saved_raid_disk
6143 * information must be scrapped
6144 */
6145 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006146 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006147 rdev->saved_raid_disk = -1;
6148
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 mddev->recovery = 0;
6150 /* flag recovery needed just to double check */
6151 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006152 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006153 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154 goto unlock;
6155 }
Neil Brown72a23c22008-06-28 08:31:41 +10006156 /* Set RUNNING before clearing NEEDED to avoid
6157 * any transients in the value of "sync_action".
6158 */
6159 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6160 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006161 /* Clear some bits that don't mean anything, but
6162 * might be left set
6163 */
NeilBrown24dd4692005-11-08 21:39:26 -08006164 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6165 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006167 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6168 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 /* no recovery is running.
6170 * remove any failed drives, then
6171 * add spares if possible.
6172 * Spare are also removed and re-added, to allow
6173 * the personality to fail the re-add.
6174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006176 if (mddev->reshape_position != MaxSector) {
6177 if (mddev->pers->check_reshape(mddev) != 0)
6178 /* Cannot proceed */
6179 goto unlock;
6180 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006181 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006182 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006183 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6184 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006185 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006186 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006187 } else if (mddev->recovery_cp < MaxSector) {
6188 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006189 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006190 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6191 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006193
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006195 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6196 /* We are adding a device or devices to an array
6197 * which has the bitmap stored on all devices.
6198 * So make sure all bitmap pages get written
6199 */
6200 bitmap_write_all(mddev->bitmap);
6201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 mddev->sync_thread = md_register_thread(md_do_sync,
6203 mddev,
6204 "%s_resync");
6205 if (!mddev->sync_thread) {
6206 printk(KERN_ERR "%s: could not start resync"
6207 " thread...\n",
6208 mdname(mddev));
6209 /* leave the spares where they are, it shouldn't hurt */
6210 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006211 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 md_wakeup_thread(mddev->sync_thread);
Neil Brown72a23c22008-06-28 08:31:41 +10006213 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006214 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 }
6216 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006217 if (!mddev->sync_thread) {
6218 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6219 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6220 &mddev->recovery))
6221 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 mddev_unlock(mddev);
6224 }
6225}
6226
Dan Williams6bfe0b42008-04-30 00:52:32 -07006227void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6228{
NeilBrown3c0ee632008-10-21 13:25:28 +11006229 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006230 wait_event_timeout(rdev->blocked_wait,
6231 !test_bit(Blocked, &rdev->flags),
6232 msecs_to_jiffies(5000));
6233 rdev_dec_pending(rdev, mddev);
6234}
6235EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6236
Adrian Bunk75c96f82005-05-05 16:16:09 -07006237static int md_notify_reboot(struct notifier_block *this,
6238 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239{
6240 struct list_head *tmp;
6241 mddev_t *mddev;
6242
6243 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6244
6245 printk(KERN_INFO "md: stopping all md devices.\n");
6246
NeilBrown29ac4aa2008-02-06 01:39:58 -08006247 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006248 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006249 /* Force a switch to readonly even array
6250 * appears to still be in use. Hence
6251 * the '100'.
6252 */
NeilBrownd710e132008-10-13 11:55:12 +11006253 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006254 mddev_unlock(mddev);
6255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 /*
6257 * certain more exotic SCSI devices are known to be
6258 * volatile wrt too early system reboots. While the
6259 * right place to handle this issue is the given
6260 * driver, we do want to have a safe RAID driver ...
6261 */
6262 mdelay(1000*1);
6263 }
6264 return NOTIFY_DONE;
6265}
6266
Adrian Bunk75c96f82005-05-05 16:16:09 -07006267static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 .notifier_call = md_notify_reboot,
6269 .next = NULL,
6270 .priority = INT_MAX, /* before any real devices */
6271};
6272
6273static void md_geninit(void)
6274{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6276
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006277 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278}
6279
Adrian Bunk75c96f82005-05-05 16:16:09 -07006280static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282 if (register_blkdev(MAJOR_NR, "md"))
6283 return -1;
6284 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6285 unregister_blkdev(MAJOR_NR, "md");
6286 return -1;
6287 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006288 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6289 md_probe, NULL, NULL);
6290 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 md_probe, NULL, NULL);
6292
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006294 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295
6296 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298}
6299
6300
6301#ifndef MODULE
6302
6303/*
6304 * Searches all registered partitions for autorun RAID arrays
6305 * at boot time.
6306 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006307
6308static LIST_HEAD(all_detected_devices);
6309struct detected_devices_node {
6310 struct list_head list;
6311 dev_t dev;
6312};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313
6314void md_autodetect_dev(dev_t dev)
6315{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006316 struct detected_devices_node *node_detected_dev;
6317
6318 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6319 if (node_detected_dev) {
6320 node_detected_dev->dev = dev;
6321 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6322 } else {
6323 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6324 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326}
6327
6328
6329static void autostart_arrays(int part)
6330{
6331 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006332 struct detected_devices_node *node_detected_dev;
6333 dev_t dev;
6334 int i_scanned, i_passed;
6335
6336 i_scanned = 0;
6337 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338
6339 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6340
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006341 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6342 i_scanned++;
6343 node_detected_dev = list_entry(all_detected_devices.next,
6344 struct detected_devices_node, list);
6345 list_del(&node_detected_dev->list);
6346 dev = node_detected_dev->dev;
6347 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006348 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349 if (IS_ERR(rdev))
6350 continue;
6351
NeilBrownb2d444d2005-11-08 21:39:31 -08006352 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 MD_BUG();
6354 continue;
6355 }
NeilBrownd0fae182008-03-04 14:29:31 -08006356 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006357 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006358 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006360
6361 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6362 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
6364 autorun_devices(part);
6365}
6366
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006367#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368
6369static __exit void md_exit(void)
6370{
6371 mddev_t *mddev;
6372 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006373
NeilBrowne8703fe2006-10-03 01:15:59 -07006374 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6375 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
6377 unregister_blkdev(MAJOR_NR,"md");
6378 unregister_blkdev(mdp_major, "mdp");
6379 unregister_reboot_notifier(&md_notifier);
6380 unregister_sysctl_table(raid_table_header);
6381 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006382 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 struct gendisk *disk = mddev->gendisk;
6384 if (!disk)
6385 continue;
6386 export_array(mddev);
6387 del_gendisk(disk);
6388 put_disk(disk);
6389 mddev->gendisk = NULL;
6390 mddev_put(mddev);
6391 }
6392}
6393
Dan Williams685784a2007-07-09 11:56:42 -07006394subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395module_exit(md_exit)
6396
NeilBrownf91de922005-11-08 21:39:36 -08006397static int get_ro(char *buffer, struct kernel_param *kp)
6398{
6399 return sprintf(buffer, "%d", start_readonly);
6400}
6401static int set_ro(const char *val, struct kernel_param *kp)
6402{
6403 char *e;
6404 int num = simple_strtoul(val, &e, 10);
6405 if (*val && (*e == '\0' || *e == '\n')) {
6406 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006407 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006408 }
6409 return -EINVAL;
6410}
6411
NeilBrown80ca3a42006-07-10 04:44:18 -07006412module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6413module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006414
NeilBrownf91de922005-11-08 21:39:36 -08006415
Linus Torvalds1da177e2005-04-16 15:20:36 -07006416EXPORT_SYMBOL(register_md_personality);
6417EXPORT_SYMBOL(unregister_md_personality);
6418EXPORT_SYMBOL(md_error);
6419EXPORT_SYMBOL(md_done_sync);
6420EXPORT_SYMBOL(md_write_start);
6421EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006422EXPORT_SYMBOL(md_register_thread);
6423EXPORT_SYMBOL(md_unregister_thread);
6424EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425EXPORT_SYMBOL(md_check_recovery);
6426MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006427MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006428MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);