blob: b375de5c1af2bbe4164ab332d528edacf98c58f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
35#include <linux/module.h>
Ahmed S. Darwish50511da2007-05-09 02:35:34 -070036#include <linux/kernel.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070037#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/linkage.h>
39#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070040#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080043#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080044#include <linux/mutex.h>
NeilBrown16f17b32006-06-26 00:27:37 -070045#include <linux/ctype.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <linux/init.h>
49
NeilBrown32a76272005-06-21 17:17:14 -070050#include <linux/file.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef CONFIG_KMOD
53#include <linux/kmod.h>
54#endif
55
56#include <asm/unaligned.h>
57
58#define MAJOR_NR MD_MAJOR
59#define MD_DRIVER
60
61/* 63 partitions with the alternate major number (mdp) */
62#define MdpMinorShift 6
63
64#define DEBUG 0
65#define dprintk(x...) ((void)(DEBUG && printk(x)))
66
67
68#ifndef MODULE
69static void autostart_arrays (int part);
70#endif
71
NeilBrown2604b702006-01-06 00:20:36 -080072static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static DEFINE_SPINLOCK(pers_lock);
74
Adrian Bunk5e563412006-06-26 00:27:42 -070075static void md_print_devices(void);
76
77#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070083 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
86 * idle IO detection.
87 *
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080089 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91
92static int sysctl_speed_limit_min = 1000;
93static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080094static inline int speed_min(mddev_t *mddev)
95{
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
98}
99
100static inline int speed_max(mddev_t *mddev)
101{
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static struct ctl_table_header *raid_table_header;
107
108static ctl_table raid_table[] = {
109 {
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
111 .procname = "speed_limit_min",
112 .data = &sysctl_speed_limit_min,
113 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700114 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .proc_handler = &proc_dointvec,
116 },
117 {
118 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .proc_handler = &proc_dointvec,
124 },
125 { .ctl_name = 0 }
126};
127
128static ctl_table raid_dir_table[] = {
129 {
130 .ctl_name = DEV_RAID,
131 .procname = "raid",
132 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700133 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .child = raid_table,
135 },
136 { .ctl_name = 0 }
137};
138
139static ctl_table raid_root_table[] = {
140 {
141 .ctl_name = CTL_DEV,
142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
147 { .ctl_name = 0 }
148};
149
150static struct block_device_operations md_fops;
151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
NeilBrownd7603b72006-01-06 00:20:30 -0800155 * We have a system wide 'event count' that is incremented
156 * on any 'interesting' event, and readers of /proc/mdstat
157 * can use 'poll' or 'select' to find out when the event
158 * count increases.
159 *
160 * Events are:
161 * start array, stop array, error, add device, remove device,
162 * start build, activate spare
163 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800164static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800165static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800166void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800167{
168 atomic_inc(&md_event_count);
169 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100170 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800171}
NeilBrown29269552006-03-27 01:18:10 -0800172EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800173
NeilBrownc331eb02006-05-30 21:27:13 -0700174/* Alternate version that can be called from interrupts
175 * when calling sysfs_notify isn't needed.
176 */
Adrian Bunk05381952006-06-26 00:28:01 -0700177static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700178{
179 atomic_inc(&md_event_count);
180 wake_up(&md_event_waiters);
181}
182
NeilBrownd7603b72006-01-06 00:20:30 -0800183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Enables to iterate over all existing md arrays
185 * all_mddevs_lock protects this list.
186 */
187static LIST_HEAD(all_mddevs);
188static DEFINE_SPINLOCK(all_mddevs_lock);
189
190
191/*
192 * iterates through all used mddevs in the system.
193 * We take care to grab the all_mddevs_lock whenever navigating
194 * the list, and to always hold a refcount when unlocked.
195 * Any code which breaks out of this loop while own
196 * a reference to the current mddev and must mddev_put it.
197 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800198#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 \
200 for (({ spin_lock(&all_mddevs_lock); \
201 tmp = all_mddevs.next; \
202 mddev = NULL;}); \
203 ({ if (tmp != &all_mddevs) \
204 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
205 spin_unlock(&all_mddevs_lock); \
206 if (mddev) mddev_put(mddev); \
207 mddev = list_entry(tmp, mddev_t, all_mddevs); \
208 tmp != &all_mddevs;}); \
209 ({ spin_lock(&all_mddevs_lock); \
210 tmp = tmp->next;}) \
211 )
212
213
Jens Axboe165125e2007-07-24 09:28:11 +0200214static int md_fail_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
NeilBrown6712ecf2007-09-27 12:47:43 +0200216 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218}
219
220static inline mddev_t *mddev_get(mddev_t *mddev)
221{
222 atomic_inc(&mddev->active);
223 return mddev;
224}
225
226static void mddev_put(mddev_t *mddev)
227{
228 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
229 return;
230 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
231 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800232 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500233 blk_cleanup_queue(mddev->queue);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800234 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800235 } else
236 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static mddev_t * mddev_find(dev_t unit)
240{
241 mddev_t *mddev, *new = NULL;
242
243 retry:
244 spin_lock(&all_mddevs_lock);
245 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
246 if (mddev->unit == unit) {
247 mddev_get(mddev);
248 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700249 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return mddev;
251 }
252
253 if (new) {
254 list_add(&new->all_mddevs, &all_mddevs);
255 spin_unlock(&all_mddevs_lock);
256 return new;
257 }
258 spin_unlock(&all_mddevs_lock);
259
NeilBrown9ffae0c2006-01-06 00:20:32 -0800260 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!new)
262 return NULL;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 new->unit = unit;
265 if (MAJOR(unit) == MD_MAJOR)
266 new->md_minor = MINOR(unit);
267 else
268 new->md_minor = MINOR(unit) >> MdpMinorShift;
269
NeilBrowndf5b89b2006-03-27 01:18:20 -0800270 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 INIT_LIST_HEAD(&new->disks);
272 INIT_LIST_HEAD(&new->all_mddevs);
273 init_timer(&new->safemode_timer);
274 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700275 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700276 init_waitqueue_head(&new->sb_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700277 new->reshape_position = MaxSector;
NeilBrownc6207272008-02-06 01:39:52 -0800278 new->resync_max = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 new->queue = blk_alloc_queue(GFP_KERNEL);
281 if (!new->queue) {
282 kfree(new);
283 return NULL;
284 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800285 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 blk_queue_make_request(new->queue, md_fail_request);
288
289 goto retry;
290}
291
292static inline int mddev_lock(mddev_t * mddev)
293{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800294 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static inline int mddev_trylock(mddev_t * mddev)
298{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800299 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static inline void mddev_unlock(mddev_t * mddev)
303{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800304 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
NeilBrown005eca52005-08-22 13:11:08 -0700306 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
NeilBrown2989ddb2006-01-06 00:20:43 -0800309static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 mdk_rdev_t * rdev;
312 struct list_head *tmp;
313
NeilBrownd089c6a2008-02-06 01:39:59 -0800314 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (rdev->desc_nr == nr)
316 return rdev;
317 }
318 return NULL;
319}
320
321static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
322{
323 struct list_head *tmp;
324 mdk_rdev_t *rdev;
325
NeilBrownd089c6a2008-02-06 01:39:59 -0800326 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (rdev->bdev->bd_dev == dev)
328 return rdev;
329 }
330 return NULL;
331}
332
NeilBrownd9d166c2006-01-06 00:20:51 -0800333static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800334{
335 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800336 list_for_each_entry(pers, &pers_list, list) {
337 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800338 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800339 if (strcmp(pers->name, clevel)==0)
340 return pers;
341 }
NeilBrown2604b702006-01-06 00:20:36 -0800342 return NULL;
343}
344
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{
347 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
348 return MD_NEW_SIZE_BLOCKS(size);
349}
350
351static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
352{
353 sector_t size;
354
355 size = rdev->sb_offset;
356
357 if (chunk_size)
358 size &= ~((sector_t)chunk_size/1024 - 1);
359 return size;
360}
361
362static int alloc_disk_sb(mdk_rdev_t * rdev)
363{
364 if (rdev->sb_page)
365 MD_BUG();
366
367 rdev->sb_page = alloc_page(GFP_KERNEL);
368 if (!rdev->sb_page) {
369 printk(KERN_ALERT "md: out of memory.\n");
370 return -EINVAL;
371 }
372
373 return 0;
374}
375
376static void free_disk_sb(mdk_rdev_t * rdev)
377{
378 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800379 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 rdev->sb_loaded = 0;
381 rdev->sb_page = NULL;
382 rdev->sb_offset = 0;
383 rdev->size = 0;
384 }
385}
386
387
NeilBrown6712ecf2007-09-27 12:47:43 +0200388static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700389{
390 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800391 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700392
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700393 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
394 printk("md: super_written gets error=%d, uptodate=%d\n",
395 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
396 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800397 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700398 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700399
NeilBrowna9701a32005-11-08 21:39:34 -0800400 if (atomic_dec_and_test(&mddev->pending_writes))
401 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700402 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700403}
404
NeilBrown6712ecf2007-09-27 12:47:43 +0200405static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800406{
407 struct bio *bio2 = bio->bi_private;
408 mdk_rdev_t *rdev = bio2->bi_private;
409 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800410
411 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
412 error == -EOPNOTSUPP) {
413 unsigned long flags;
414 /* barriers don't appear to be supported :-( */
415 set_bit(BarriersNotsupp, &rdev->flags);
416 mddev->barriers_work = 0;
417 spin_lock_irqsave(&mddev->write_lock, flags);
418 bio2->bi_next = mddev->biolist;
419 mddev->biolist = bio2;
420 spin_unlock_irqrestore(&mddev->write_lock, flags);
421 wake_up(&mddev->sb_wait);
422 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200423 } else {
424 bio_put(bio2);
425 bio->bi_private = rdev;
426 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800427 }
NeilBrowna9701a32005-11-08 21:39:34 -0800428}
429
NeilBrown7bfa19f2005-06-21 17:17:28 -0700430void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
431 sector_t sector, int size, struct page *page)
432{
433 /* write first size bytes of page to sector of rdev
434 * Increment mddev->pending_writes before returning
435 * and decrement it on completion, waking up sb_wait
436 * if zero is reached.
437 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800438 *
439 * As we might need to resubmit the request if BIO_RW_BARRIER
440 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700441 */
442 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800443 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700444
445 bio->bi_bdev = rdev->bdev;
446 bio->bi_sector = sector;
447 bio_add_page(bio, page, size, 0);
448 bio->bi_private = rdev;
449 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800450 bio->bi_rw = rw;
451
NeilBrown7bfa19f2005-06-21 17:17:28 -0700452 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800453 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
454 struct bio *rbio;
455 rw |= (1<<BIO_RW_BARRIER);
456 rbio = bio_clone(bio, GFP_NOIO);
457 rbio->bi_private = bio;
458 rbio->bi_end_io = super_written_barrier;
459 submit_bio(rw, rbio);
460 } else
461 submit_bio(rw, bio);
462}
463
464void md_super_wait(mddev_t *mddev)
465{
466 /* wait for all superblock writes that were scheduled to complete.
467 * if any had to be retried (due to BARRIER problems), retry them
468 */
469 DEFINE_WAIT(wq);
470 for(;;) {
471 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
472 if (atomic_read(&mddev->pending_writes)==0)
473 break;
474 while (mddev->biolist) {
475 struct bio *bio;
476 spin_lock_irq(&mddev->write_lock);
477 bio = mddev->biolist;
478 mddev->biolist = bio->bi_next ;
479 bio->bi_next = NULL;
480 spin_unlock_irq(&mddev->write_lock);
481 submit_bio(bio->bi_rw, bio);
482 }
483 schedule();
484 }
485 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700486}
487
NeilBrown6712ecf2007-09-27 12:47:43 +0200488static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
NeilBrowna654b9d82005-06-21 17:17:27 -0700493int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct page *page, int rw)
495{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700496 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct completion event;
498 int ret;
499
500 rw |= (1 << BIO_RW_SYNC);
501
502 bio->bi_bdev = bdev;
503 bio->bi_sector = sector;
504 bio_add_page(bio, page, size, 0);
505 init_completion(&event);
506 bio->bi_private = &event;
507 bio->bi_end_io = bi_complete;
508 submit_bio(rw, bio);
509 wait_for_completion(&event);
510
511 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
512 bio_put(bio);
513 return ret;
514}
NeilBrowna8745db2006-01-06 00:20:34 -0800515EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
NeilBrown0002b272005-09-09 16:23:53 -0700517static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 char b[BDEVNAME_SIZE];
520 if (!rdev->sb_page) {
521 MD_BUG();
522 return -EINVAL;
523 }
524 if (rdev->sb_loaded)
525 return 0;
526
527
NeilBrown0002b272005-09-09 16:23:53 -0700528 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto fail;
530 rdev->sb_loaded = 1;
531 return 0;
532
533fail:
534 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
535 bdevname(rdev->bdev,b));
536 return -EINVAL;
537}
538
539static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
540{
541 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
542 (sb1->set_uuid1 == sb2->set_uuid1) &&
543 (sb1->set_uuid2 == sb2->set_uuid2) &&
544 (sb1->set_uuid3 == sb2->set_uuid3))
545
546 return 1;
547
548 return 0;
549}
550
551
552static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
553{
554 int ret;
555 mdp_super_t *tmp1, *tmp2;
556
557 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
558 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
559
560 if (!tmp1 || !tmp2) {
561 ret = 0;
562 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
563 goto abort;
564 }
565
566 *tmp1 = *sb1;
567 *tmp2 = *sb2;
568
569 /*
570 * nr_disks is not constant
571 */
572 tmp1->nr_disks = 0;
573 tmp2->nr_disks = 0;
574
575 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
576 ret = 0;
577 else
578 ret = 1;
579
580abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700581 kfree(tmp1);
582 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return ret;
584}
585
NeilBrown4d167f02007-05-09 02:35:37 -0700586
587static u32 md_csum_fold(u32 csum)
588{
589 csum = (csum & 0xffff) + (csum >> 16);
590 return (csum & 0xffff) + (csum >> 16);
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static unsigned int calc_sb_csum(mdp_super_t * sb)
594{
NeilBrown4d167f02007-05-09 02:35:37 -0700595 u64 newcsum = 0;
596 u32 *sb32 = (u32*)sb;
597 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 unsigned int disk_csum, csum;
599
600 disk_csum = sb->sb_csum;
601 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700602
603 for (i = 0; i < MD_SB_BYTES/4 ; i++)
604 newcsum += sb32[i];
605 csum = (newcsum & 0xffffffff) + (newcsum>>32);
606
607
608#ifdef CONFIG_ALPHA
609 /* This used to use csum_partial, which was wrong for several
610 * reasons including that different results are returned on
611 * different architectures. It isn't critical that we get exactly
612 * the same return value as before (we always csum_fold before
613 * testing, and that removes any differences). However as we
614 * know that csum_partial always returned a 16bit value on
615 * alphas, do a fold to maximise conformity to previous behaviour.
616 */
617 sb->sb_csum = md_csum_fold(disk_csum);
618#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700620#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return csum;
622}
623
624
625/*
626 * Handle superblock details.
627 * We want to be able to handle multiple superblock formats
628 * so we have a common interface to them all, and an array of
629 * different handlers.
630 * We rely on user-space to write the initial superblock, and support
631 * reading and updating of superblocks.
632 * Interface methods are:
633 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
634 * loads and validates a superblock on dev.
635 * if refdev != NULL, compare superblocks on both devices
636 * Return:
637 * 0 - dev has a superblock that is compatible with refdev
638 * 1 - dev has a superblock that is compatible and newer than refdev
639 * so dev should be used as the refdev in future
640 * -EINVAL superblock incompatible or invalid
641 * -othererror e.g. -EIO
642 *
643 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
644 * Verify that dev is acceptable into mddev.
645 * The first time, mddev->raid_disks will be 0, and data from
646 * dev should be merged in. Subsequent calls check that dev
647 * is new enough. Return 0 or -EINVAL
648 *
649 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
650 * Update the superblock for rdev with data in mddev
651 * This does not write to disc.
652 *
653 */
654
655struct super_type {
656 char *name;
657 struct module *owner;
658 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
659 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
660 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
661};
662
663/*
664 * load_super for 0.90.0
665 */
666static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
667{
668 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
669 mdp_super_t *sb;
670 int ret;
671 sector_t sb_offset;
672
673 /*
674 * Calculate the position of the superblock,
675 * it's at the end of the disk.
676 *
677 * It also happens to be a multiple of 4Kb.
678 */
679 sb_offset = calc_dev_sboffset(rdev->bdev);
680 rdev->sb_offset = sb_offset;
681
NeilBrown0002b272005-09-09 16:23:53 -0700682 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (ret) return ret;
684
685 ret = -EINVAL;
686
687 bdevname(rdev->bdev, b);
688 sb = (mdp_super_t*)page_address(rdev->sb_page);
689
690 if (sb->md_magic != MD_SB_MAGIC) {
691 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
692 b);
693 goto abort;
694 }
695
696 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800697 sb->minor_version < 90 ||
698 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
700 sb->major_version, sb->minor_version,
701 b);
702 goto abort;
703 }
704
705 if (sb->raid_disks <= 0)
706 goto abort;
707
NeilBrown4d167f02007-05-09 02:35:37 -0700708 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
710 b);
711 goto abort;
712 }
713
714 rdev->preferred_minor = sb->md_minor;
715 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700716 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
NeilBrowne11e93f2007-05-09 02:35:36 -0700718 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
719 if (sb->level != 1 && sb->level != 4
720 && sb->level != 5 && sb->level != 6
721 && sb->level != 10) {
722 /* FIXME use a better test */
723 printk(KERN_WARNING
724 "md: bitmaps not supported for this level.\n");
725 goto abort;
726 }
727 }
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (sb->level == LEVEL_MULTIPATH)
730 rdev->desc_nr = -1;
731 else
732 rdev->desc_nr = sb->this_disk.number;
733
734 if (refdev == 0)
735 ret = 1;
736 else {
737 __u64 ev1, ev2;
738 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
739 if (!uuid_equal(refsb, sb)) {
740 printk(KERN_WARNING "md: %s has different UUID to %s\n",
741 b, bdevname(refdev->bdev,b2));
742 goto abort;
743 }
744 if (!sb_equal(refsb, sb)) {
745 printk(KERN_WARNING "md: %s has same UUID"
746 " but different superblock to %s\n",
747 b, bdevname(refdev->bdev, b2));
748 goto abort;
749 }
750 ev1 = md_event(sb);
751 ev2 = md_event(refsb);
752 if (ev1 > ev2)
753 ret = 1;
754 else
755 ret = 0;
756 }
757 rdev->size = calc_dev_size(rdev, sb->chunk_size);
758
NeilBrown2bf071b2006-01-06 00:20:55 -0800759 if (rdev->size < sb->size && sb->level > 1)
760 /* "this cannot possibly happen" ... */
761 ret = -EINVAL;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 abort:
764 return ret;
765}
766
767/*
768 * validate_super for 0.90.0
769 */
770static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
771{
772 mdp_disk_t *desc;
773 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700774 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
NeilBrown41158c72005-06-21 17:17:25 -0700776 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800777 clear_bit(Faulty, &rdev->flags);
778 clear_bit(In_sync, &rdev->flags);
779 clear_bit(WriteMostly, &rdev->flags);
780 clear_bit(BarriersNotsupp, &rdev->flags);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (mddev->raid_disks == 0) {
783 mddev->major_version = 0;
784 mddev->minor_version = sb->minor_version;
785 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800786 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 mddev->chunk_size = sb->chunk_size;
788 mddev->ctime = sb->ctime;
789 mddev->utime = sb->utime;
790 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800791 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 mddev->layout = sb->layout;
793 mddev->raid_disks = sb->raid_disks;
794 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700795 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700796 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700797 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
NeilBrownf6705572006-03-27 01:18:11 -0800799 if (mddev->minor_version >= 91) {
800 mddev->reshape_position = sb->reshape_position;
801 mddev->delta_disks = sb->delta_disks;
802 mddev->new_level = sb->new_level;
803 mddev->new_layout = sb->new_layout;
804 mddev->new_chunk = sb->new_chunk;
805 } else {
806 mddev->reshape_position = MaxSector;
807 mddev->delta_disks = 0;
808 mddev->new_level = mddev->level;
809 mddev->new_layout = mddev->layout;
810 mddev->new_chunk = mddev->chunk_size;
811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (sb->state & (1<<MD_SB_CLEAN))
814 mddev->recovery_cp = MaxSector;
815 else {
816 if (sb->events_hi == sb->cp_events_hi &&
817 sb->events_lo == sb->cp_events_lo) {
818 mddev->recovery_cp = sb->recovery_cp;
819 } else
820 mddev->recovery_cp = 0;
821 }
822
823 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
824 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
825 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
826 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
827
828 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700829
830 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700831 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700832 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700833
NeilBrown41158c72005-06-21 17:17:25 -0700834 } else if (mddev->pers == NULL) {
835 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ++ev1;
837 if (ev1 < mddev->events)
838 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700839 } else if (mddev->bitmap) {
840 /* if adding to array with a bitmap, then we can accept an
841 * older device ... but not too old.
842 */
NeilBrown41158c72005-06-21 17:17:25 -0700843 if (ev1 < mddev->bitmap->events_cleared)
844 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700845 } else {
846 if (ev1 < mddev->events)
847 /* just a hot-add of a new device, leave raid_disk at -1 */
848 return 0;
849 }
NeilBrown41158c72005-06-21 17:17:25 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 desc = sb->disks + rdev->desc_nr;
853
854 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800855 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700856 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
857 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800858 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 rdev->raid_disk = desc->raid_disk;
860 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700861 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
862 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700863 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800864 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
868/*
869 * sync_super for 0.90.0
870 */
871static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
872{
873 mdp_super_t *sb;
874 struct list_head *tmp;
875 mdk_rdev_t *rdev2;
876 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /* make rdev->sb match mddev data..
880 *
881 * 1/ zero out disks
882 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
883 * 3/ any empty disks < next_spare become removed
884 *
885 * disks[0] gets initialised to REMOVED because
886 * we cannot be sure from other fields if it has
887 * been initialised or not.
888 */
889 int i;
890 int active=0, working=0,failed=0,spare=0,nr_disks=0;
891
NeilBrown61181562005-09-09 16:24:02 -0700892 rdev->sb_size = MD_SB_BYTES;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 sb = (mdp_super_t*)page_address(rdev->sb_page);
895
896 memset(sb, 0, sizeof(*sb));
897
898 sb->md_magic = MD_SB_MAGIC;
899 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sb->patch_version = mddev->patch_version;
901 sb->gvalid_words = 0; /* ignored */
902 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
903 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
904 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
905 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
906
907 sb->ctime = mddev->ctime;
908 sb->level = mddev->level;
909 sb->size = mddev->size;
910 sb->raid_disks = mddev->raid_disks;
911 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800912 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sb->utime = mddev->utime;
914 sb->state = 0;
915 sb->events_hi = (mddev->events>>32);
916 sb->events_lo = (u32)mddev->events;
917
NeilBrownf6705572006-03-27 01:18:11 -0800918 if (mddev->reshape_position == MaxSector)
919 sb->minor_version = 90;
920 else {
921 sb->minor_version = 91;
922 sb->reshape_position = mddev->reshape_position;
923 sb->new_level = mddev->new_level;
924 sb->delta_disks = mddev->delta_disks;
925 sb->new_layout = mddev->new_layout;
926 sb->new_chunk = mddev->new_chunk;
927 }
928 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (mddev->in_sync)
930 {
931 sb->recovery_cp = mddev->recovery_cp;
932 sb->cp_events_hi = (mddev->events>>32);
933 sb->cp_events_lo = (u32)mddev->events;
934 if (mddev->recovery_cp == MaxSector)
935 sb->state = (1<< MD_SB_CLEAN);
936 } else
937 sb->recovery_cp = 0;
938
939 sb->layout = mddev->layout;
940 sb->chunk_size = mddev->chunk_size;
941
NeilBrowna654b9d82005-06-21 17:17:27 -0700942 if (mddev->bitmap && mddev->bitmap_file == NULL)
943 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800946 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800948 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800949 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
950 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800951 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800953 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800954 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 d = &sb->disks[rdev2->desc_nr];
956 nr_disks++;
957 d->number = rdev2->desc_nr;
958 d->major = MAJOR(rdev2->bdev->bd_dev);
959 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800960 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
961 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 d->raid_disk = rdev2->raid_disk;
963 else
964 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800965 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800967 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 d->state = (1<<MD_DISK_ACTIVE);
969 d->state |= (1<<MD_DISK_SYNC);
970 active++;
971 working++;
972 } else {
973 d->state = 0;
974 spare++;
975 working++;
976 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700977 if (test_bit(WriteMostly, &rdev2->flags))
978 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* now set the "removed" and "faulty" bits on any missing devices */
981 for (i=0 ; i < mddev->raid_disks ; i++) {
982 mdp_disk_t *d = &sb->disks[i];
983 if (d->state == 0 && d->number == 0) {
984 d->number = i;
985 d->raid_disk = i;
986 d->state = (1<<MD_DISK_REMOVED);
987 d->state |= (1<<MD_DISK_FAULTY);
988 failed++;
989 }
990 }
991 sb->nr_disks = nr_disks;
992 sb->active_disks = active;
993 sb->working_disks = working;
994 sb->failed_disks = failed;
995 sb->spare_disks = spare;
996
997 sb->this_disk = sb->disks[rdev->desc_nr];
998 sb->sb_csum = calc_sb_csum(sb);
999}
1000
1001/*
1002 * version 1 superblock
1003 */
1004
NeilBrown1c05b4b2006-10-21 10:24:08 -07001005static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001007 __le32 disk_csum;
1008 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unsigned long long newcsum;
1010 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001011 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int i;
1013
1014 disk_csum = sb->sb_csum;
1015 sb->sb_csum = 0;
1016 newcsum = 0;
1017 for (i=0; size>=4; size -= 4 )
1018 newcsum += le32_to_cpu(*isuper++);
1019
1020 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001021 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1024 sb->sb_csum = disk_csum;
1025 return cpu_to_le32(csum);
1026}
1027
1028static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1029{
1030 struct mdp_superblock_1 *sb;
1031 int ret;
1032 sector_t sb_offset;
1033 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001034 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /*
1037 * Calculate the position of the superblock.
1038 * It is always aligned to a 4K boundary and
1039 * depeding on minor_version, it can be:
1040 * 0: At least 8K, but less than 12K, from end of device
1041 * 1: At start of device
1042 * 2: 4K from start of device.
1043 */
1044 switch(minor_version) {
1045 case 0:
1046 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1047 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001048 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* convert from sectors to K */
1050 sb_offset /= 2;
1051 break;
1052 case 1:
1053 sb_offset = 0;
1054 break;
1055 case 2:
1056 sb_offset = 4;
1057 break;
1058 default:
1059 return -EINVAL;
1060 }
1061 rdev->sb_offset = sb_offset;
1062
NeilBrown0002b272005-09-09 16:23:53 -07001063 /* superblock is rarely larger than 1K, but it can be larger,
1064 * and it is safe to read 4k, so we do that
1065 */
1066 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (ret) return ret;
1068
1069
1070 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1071
1072 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1073 sb->major_version != cpu_to_le32(1) ||
1074 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1075 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001076 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EINVAL;
1078
1079 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1080 printk("md: invalid superblock checksum on %s\n",
1081 bdevname(rdev->bdev,b));
1082 return -EINVAL;
1083 }
1084 if (le64_to_cpu(sb->data_size) < 10) {
1085 printk("md: data_size too small on %s\n",
1086 bdevname(rdev->bdev,b));
1087 return -EINVAL;
1088 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001089 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1090 if (sb->level != cpu_to_le32(1) &&
1091 sb->level != cpu_to_le32(4) &&
1092 sb->level != cpu_to_le32(5) &&
1093 sb->level != cpu_to_le32(6) &&
1094 sb->level != cpu_to_le32(10)) {
1095 printk(KERN_WARNING
1096 "md: bitmaps not supported for this level.\n");
1097 return -EINVAL;
1098 }
1099 }
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 rdev->preferred_minor = 0xffff;
1102 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001103 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
NeilBrown0002b272005-09-09 16:23:53 -07001105 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001106 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001107 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001108 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1109
1110 if (minor_version
1111 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1112 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001113
NeilBrown31b65a02006-07-10 04:44:14 -07001114 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1115 rdev->desc_nr = -1;
1116 else
1117 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001120 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 else {
1122 __u64 ev1, ev2;
1123 struct mdp_superblock_1 *refsb =
1124 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1125
1126 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1127 sb->level != refsb->level ||
1128 sb->layout != refsb->layout ||
1129 sb->chunksize != refsb->chunksize) {
1130 printk(KERN_WARNING "md: %s has strangely different"
1131 " superblock to %s\n",
1132 bdevname(rdev->bdev,b),
1133 bdevname(refdev->bdev,b2));
1134 return -EINVAL;
1135 }
1136 ev1 = le64_to_cpu(sb->events);
1137 ev2 = le64_to_cpu(refsb->events);
1138
1139 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001140 ret = 1;
1141 else
1142 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
NeilBrowna1801f82008-03-04 14:29:31 -08001144 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1146 else
1147 rdev->size = rdev->sb_offset;
1148 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1149 return -EINVAL;
1150 rdev->size = le64_to_cpu(sb->data_size)/2;
1151 if (le32_to_cpu(sb->chunksize))
1152 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001153
NeilBrown1c05b4b2006-10-21 10:24:08 -07001154 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001155 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001156 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1160{
1161 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001162 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
NeilBrown41158c72005-06-21 17:17:25 -07001164 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001165 clear_bit(Faulty, &rdev->flags);
1166 clear_bit(In_sync, &rdev->flags);
1167 clear_bit(WriteMostly, &rdev->flags);
1168 clear_bit(BarriersNotsupp, &rdev->flags);
1169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (mddev->raid_disks == 0) {
1171 mddev->major_version = 1;
1172 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001173 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1175 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1176 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1177 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001178 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mddev->layout = le32_to_cpu(sb->layout);
1180 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1181 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001182 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001183 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001184 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1187 memcpy(mddev->uuid, sb->set_uuid, 16);
1188
1189 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001190
NeilBrown71c08052005-09-09 16:23:51 -07001191 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001192 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001193 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001194
NeilBrownf6705572006-03-27 01:18:11 -08001195 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1196 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1197 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1198 mddev->new_level = le32_to_cpu(sb->new_level);
1199 mddev->new_layout = le32_to_cpu(sb->new_layout);
1200 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1201 } else {
1202 mddev->reshape_position = MaxSector;
1203 mddev->delta_disks = 0;
1204 mddev->new_level = mddev->level;
1205 mddev->new_layout = mddev->layout;
1206 mddev->new_chunk = mddev->chunk_size;
1207 }
1208
NeilBrown41158c72005-06-21 17:17:25 -07001209 } else if (mddev->pers == NULL) {
1210 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 ++ev1;
1212 if (ev1 < mddev->events)
1213 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001214 } else if (mddev->bitmap) {
1215 /* If adding to array with a bitmap, then we can accept an
1216 * older device, but not too old.
1217 */
NeilBrown41158c72005-06-21 17:17:25 -07001218 if (ev1 < mddev->bitmap->events_cleared)
1219 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001220 } else {
1221 if (ev1 < mddev->events)
1222 /* just a hot-add of a new device, leave raid_disk at -1 */
1223 return 0;
1224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 if (mddev->level != LEVEL_MULTIPATH) {
1226 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1228 switch(role) {
1229 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 break;
1231 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001232 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001235 if ((le32_to_cpu(sb->feature_map) &
1236 MD_FEATURE_RECOVERY_OFFSET))
1237 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1238 else
1239 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 rdev->raid_disk = role;
1241 break;
1242 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001243 if (sb->devflags & WriteMostly1)
1244 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001245 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001246 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return 0;
1249}
1250
1251static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1252{
1253 struct mdp_superblock_1 *sb;
1254 struct list_head *tmp;
1255 mdk_rdev_t *rdev2;
1256 int max_dev, i;
1257 /* make rdev->sb match mddev and rdev data. */
1258
1259 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1260
1261 sb->feature_map = 0;
1262 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001263 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 memset(sb->pad1, 0, sizeof(sb->pad1));
1265 memset(sb->pad2, 0, sizeof(sb->pad2));
1266 memset(sb->pad3, 0, sizeof(sb->pad3));
1267
1268 sb->utime = cpu_to_le64((__u64)mddev->utime);
1269 sb->events = cpu_to_le64(mddev->events);
1270 if (mddev->in_sync)
1271 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1272 else
1273 sb->resync_offset = cpu_to_le64(0);
1274
NeilBrown1c05b4b2006-10-21 10:24:08 -07001275 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001276
NeilBrownf0ca3402006-02-02 14:28:04 -08001277 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001278 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001279
NeilBrowna654b9d82005-06-21 17:17:27 -07001280 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1281 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001282 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001283 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001284
1285 if (rdev->raid_disk >= 0 &&
1286 !test_bit(In_sync, &rdev->flags) &&
1287 rdev->recovery_offset > 0) {
1288 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1289 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1290 }
1291
NeilBrownf6705572006-03-27 01:18:11 -08001292 if (mddev->reshape_position != MaxSector) {
1293 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1294 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1295 sb->new_layout = cpu_to_le32(mddev->new_layout);
1296 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1297 sb->new_level = cpu_to_le32(mddev->new_level);
1298 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1299 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001302 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (rdev2->desc_nr+1 > max_dev)
1304 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001305
1306 if (max_dev > le32_to_cpu(sb->max_dev))
1307 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 for (i=0; i<max_dev;i++)
1309 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1310
NeilBrownd089c6a2008-02-06 01:39:59 -08001311 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001313 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001315 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001317 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1318 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 else
1320 sb->dev_roles[i] = cpu_to_le16(0xffff);
1321 }
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 sb->sb_csum = calc_sb_1_csum(sb);
1324}
1325
1326
Adrian Bunk75c96f82005-05-05 16:16:09 -07001327static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 [0] = {
1329 .name = "0.90.0",
1330 .owner = THIS_MODULE,
1331 .load_super = super_90_load,
1332 .validate_super = super_90_validate,
1333 .sync_super = super_90_sync,
1334 },
1335 [1] = {
1336 .name = "md-1",
1337 .owner = THIS_MODULE,
1338 .load_super = super_1_load,
1339 .validate_super = super_1_validate,
1340 .sync_super = super_1_sync,
1341 },
1342};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1345{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001346 struct list_head *tmp, *tmp2;
1347 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
NeilBrownd089c6a2008-02-06 01:39:59 -08001349 rdev_for_each(rdev, tmp, mddev1)
1350 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001351 if (rdev->bdev->bd_contains ==
1352 rdev2->bdev->bd_contains)
1353 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 return 0;
1356}
1357
1358static LIST_HEAD(pending_raid_disks);
1359
1360static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1361{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001362 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001363 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001364 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001365 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
1367 if (rdev->mddev) {
1368 MD_BUG();
1369 return -EINVAL;
1370 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001371 /* make sure rdev->size exceeds mddev->size */
1372 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001373 if (mddev->pers) {
1374 /* Cannot change size, so fail
1375 * If mddev->level <= 0, then we don't care
1376 * about aligning sizes (e.g. linear)
1377 */
1378 if (mddev->level > 0)
1379 return -ENOSPC;
1380 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001381 mddev->size = rdev->size;
1382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /* Verify rdev->desc_nr is unique.
1385 * If it is -1, assign a free number, else
1386 * check number is not in use
1387 */
1388 if (rdev->desc_nr < 0) {
1389 int choice = 0;
1390 if (mddev->pers) choice = mddev->raid_disks;
1391 while (find_rdev_nr(mddev, choice))
1392 choice++;
1393 rdev->desc_nr = choice;
1394 } else {
1395 if (find_rdev_nr(mddev, rdev->desc_nr))
1396 return -EBUSY;
1397 }
NeilBrown19133a42005-11-08 21:39:35 -08001398 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001399 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001400 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001403 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001404
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001405 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001406 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001407
NeilBrownf637b9f2005-11-08 21:39:37 -08001408 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001409 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001410 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001411 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001412 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1413 kobject_del(&rdev->kobj);
1414 goto fail;
1415 }
1416 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001417 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001419
1420 fail:
1421 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1422 b, mdname(mddev));
1423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
NeilBrown177a99b2008-02-06 01:39:56 -08001426static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001427{
1428 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1429 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001430 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1434{
1435 char b[BDEVNAME_SIZE];
1436 if (!rdev->mddev) {
1437 MD_BUG();
1438 return;
1439 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001440 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 list_del_init(&rdev->same_set);
1442 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1443 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001444 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001445
1446 /* We need to delay this, otherwise we can deadlock when
1447 * writing to 'remove' to "dev/state"
1448 */
NeilBrown177a99b2008-02-06 01:39:56 -08001449 INIT_WORK(&rdev->del_work, md_delayed_delete);
1450 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001451 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
1454/*
1455 * prevent the device from being mounted, repartitioned or
1456 * otherwise reused by a RAID array (or any other kernel
1457 * subsystem), by bd_claiming the device.
1458 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001459static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 int err = 0;
1462 struct block_device *bdev;
1463 char b[BDEVNAME_SIZE];
1464
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001465 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (IS_ERR(bdev)) {
1467 printk(KERN_ERR "md: could not open %s.\n",
1468 __bdevname(dev, b));
1469 return PTR_ERR(bdev);
1470 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001471 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (err) {
1473 printk(KERN_ERR "md: could not bd_claim %s.\n",
1474 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001475 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return err;
1477 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001478 if (!shared)
1479 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 rdev->bdev = bdev;
1481 return err;
1482}
1483
1484static void unlock_rdev(mdk_rdev_t *rdev)
1485{
1486 struct block_device *bdev = rdev->bdev;
1487 rdev->bdev = NULL;
1488 if (!bdev)
1489 MD_BUG();
1490 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001491 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
1494void md_autodetect_dev(dev_t dev);
1495
1496static void export_rdev(mdk_rdev_t * rdev)
1497{
1498 char b[BDEVNAME_SIZE];
1499 printk(KERN_INFO "md: export_rdev(%s)\n",
1500 bdevname(rdev->bdev,b));
1501 if (rdev->mddev)
1502 MD_BUG();
1503 free_disk_sb(rdev);
1504 list_del_init(&rdev->same_set);
1505#ifndef MODULE
1506 md_autodetect_dev(rdev->bdev->bd_dev);
1507#endif
1508 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001509 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512static void kick_rdev_from_array(mdk_rdev_t * rdev)
1513{
1514 unbind_rdev_from_array(rdev);
1515 export_rdev(rdev);
1516}
1517
1518static void export_array(mddev_t *mddev)
1519{
1520 struct list_head *tmp;
1521 mdk_rdev_t *rdev;
1522
NeilBrownd089c6a2008-02-06 01:39:59 -08001523 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (!rdev->mddev) {
1525 MD_BUG();
1526 continue;
1527 }
1528 kick_rdev_from_array(rdev);
1529 }
1530 if (!list_empty(&mddev->disks))
1531 MD_BUG();
1532 mddev->raid_disks = 0;
1533 mddev->major_version = 0;
1534}
1535
1536static void print_desc(mdp_disk_t *desc)
1537{
1538 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1539 desc->major,desc->minor,desc->raid_disk,desc->state);
1540}
1541
1542static void print_sb(mdp_super_t *sb)
1543{
1544 int i;
1545
1546 printk(KERN_INFO
1547 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1548 sb->major_version, sb->minor_version, sb->patch_version,
1549 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1550 sb->ctime);
1551 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1552 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1553 sb->md_minor, sb->layout, sb->chunk_size);
1554 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1555 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1556 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1557 sb->failed_disks, sb->spare_disks,
1558 sb->sb_csum, (unsigned long)sb->events_lo);
1559
1560 printk(KERN_INFO);
1561 for (i = 0; i < MD_SB_DISKS; i++) {
1562 mdp_disk_t *desc;
1563
1564 desc = sb->disks + i;
1565 if (desc->number || desc->major || desc->minor ||
1566 desc->raid_disk || (desc->state && (desc->state != 4))) {
1567 printk(" D %2d: ", i);
1568 print_desc(desc);
1569 }
1570 }
1571 printk(KERN_INFO "md: THIS: ");
1572 print_desc(&sb->this_disk);
1573
1574}
1575
1576static void print_rdev(mdk_rdev_t *rdev)
1577{
1578 char b[BDEVNAME_SIZE];
1579 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1580 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001581 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1582 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (rdev->sb_loaded) {
1584 printk(KERN_INFO "md: rdev superblock:\n");
1585 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1586 } else
1587 printk(KERN_INFO "md: no rdev superblock!\n");
1588}
1589
Adrian Bunk5e563412006-06-26 00:27:42 -07001590static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
1592 struct list_head *tmp, *tmp2;
1593 mdk_rdev_t *rdev;
1594 mddev_t *mddev;
1595 char b[BDEVNAME_SIZE];
1596
1597 printk("\n");
1598 printk("md: **********************************\n");
1599 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1600 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001601 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
NeilBrown32a76272005-06-21 17:17:14 -07001603 if (mddev->bitmap)
1604 bitmap_print_sb(mddev->bitmap);
1605 else
1606 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001607 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 printk("<%s>", bdevname(rdev->bdev,b));
1609 printk("\n");
1610
NeilBrownd089c6a2008-02-06 01:39:59 -08001611 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 print_rdev(rdev);
1613 }
1614 printk("md: **********************************\n");
1615 printk("\n");
1616}
1617
1618
NeilBrown42543762006-06-26 00:27:57 -07001619static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
NeilBrown42543762006-06-26 00:27:57 -07001621 /* Update each superblock (in-memory image), but
1622 * if we are allowed to, skip spares which already
1623 * have the right event counter, or have one earlier
1624 * (which would mean they aren't being marked as dirty
1625 * with the rest of the array)
1626 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 mdk_rdev_t *rdev;
1628 struct list_head *tmp;
1629
NeilBrownd089c6a2008-02-06 01:39:59 -08001630 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001631 if (rdev->sb_events == mddev->events ||
1632 (nospares &&
1633 rdev->raid_disk < 0 &&
1634 (rdev->sb_events&1)==0 &&
1635 rdev->sb_events+1 == mddev->events)) {
1636 /* Don't update this superblock */
1637 rdev->sb_loaded = 2;
1638 } else {
1639 super_types[mddev->major_version].
1640 sync_super(mddev, rdev);
1641 rdev->sb_loaded = 1;
1642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644}
1645
NeilBrown850b2b42006-10-03 01:15:46 -07001646static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct list_head *tmp;
1649 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001650 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001651 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001654 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001655
NeilBrown850b2b42006-10-03 01:15:46 -07001656 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1657 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1658 force_change = 1;
1659 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1660 /* just a clean<-> dirty transition, possibly leave spares alone,
1661 * though if events isn't the right even/odd, we will have to do
1662 * spares after all
1663 */
1664 nospares = 1;
1665 if (force_change)
1666 nospares = 0;
1667 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001668 /* If the array is degraded, then skipping spares is both
1669 * dangerous and fairly pointless.
1670 * Dangerous because a device that was removed from the array
1671 * might have a event_count that still looks up-to-date,
1672 * so it can be re-added without a resync.
1673 * Pointless because if there are any spares to skip,
1674 * then a recovery will happen and soon that array won't
1675 * be degraded any more and the spare can go back to sleep then.
1676 */
NeilBrown850b2b42006-10-03 01:15:46 -07001677 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001678
NeilBrown06d91a52005-06-21 17:17:12 -07001679 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001681
1682 /* If this is just a dirty<->clean transition, and the array is clean
1683 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001684 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001685 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001686 && (mddev->events & 1)
1687 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001688 mddev->events--;
1689 else {
1690 /* otherwise we have to go forward and ... */
1691 mddev->events ++;
1692 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1693 /* .. if the array isn't clean, insist on an odd 'events' */
1694 if ((mddev->events&1)==0) {
1695 mddev->events++;
1696 nospares = 0;
1697 }
1698 } else {
1699 /* otherwise insist on an even 'events' (for clean states) */
1700 if ((mddev->events&1)) {
1701 mddev->events++;
1702 nospares = 0;
1703 }
1704 }
1705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 if (!mddev->events) {
1708 /*
1709 * oops, this 64-bit counter should never wrap.
1710 * Either we are in around ~1 trillion A.C., assuming
1711 * 1 reboot per second, or we have a bug:
1712 */
1713 MD_BUG();
1714 mddev->events --;
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /*
1718 * do not write anything to disk if using
1719 * nonpersistent superblocks
1720 */
NeilBrown06d91a52005-06-21 17:17:12 -07001721 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001722 if (!mddev->external)
1723 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1724
NeilBrowna9701a32005-11-08 21:39:34 -08001725 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001726 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001728 }
NeilBrowne6910632008-02-06 01:39:51 -08001729 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001730 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 dprintk(KERN_INFO
1733 "md: updating %s RAID superblock on device (in sync %d)\n",
1734 mdname(mddev),mddev->in_sync);
1735
NeilBrown4ad13662007-07-17 04:06:13 -07001736 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001737 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 char b[BDEVNAME_SIZE];
1739 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001740 if (rdev->sb_loaded != 1)
1741 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001742 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 dprintk("(skipping faulty ");
1744
1745 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001746 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001747 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001748 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001749 rdev->sb_page);
1750 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1751 bdevname(rdev->bdev,b),
1752 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001753 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 } else
1756 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001757 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /* only need to write one superblock... */
1759 break;
1760 }
NeilBrowna9701a32005-11-08 21:39:34 -08001761 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001762 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001763
NeilBrowna9701a32005-11-08 21:39:34 -08001764 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001765 if (mddev->in_sync != sync_req ||
1766 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001767 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001768 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001769 goto repeat;
1770 }
NeilBrown850b2b42006-10-03 01:15:46 -07001771 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001772 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001773 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
NeilBrownbce74da2006-01-06 00:20:41 -08001777/* words written to sysfs files may, or my not, be \n terminated.
1778 * We want to accept with case. For this we use cmd_match.
1779 */
1780static int cmd_match(const char *cmd, const char *str)
1781{
1782 /* See if cmd, written into a sysfs file, matches
1783 * str. They must either be the same, or cmd can
1784 * have a trailing newline
1785 */
1786 while (*cmd && *str && *cmd == *str) {
1787 cmd++;
1788 str++;
1789 }
1790 if (*cmd == '\n')
1791 cmd++;
1792 if (*str || *cmd)
1793 return 0;
1794 return 1;
1795}
1796
NeilBrown86e6ffd2005-11-08 21:39:24 -08001797struct rdev_sysfs_entry {
1798 struct attribute attr;
1799 ssize_t (*show)(mdk_rdev_t *, char *);
1800 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1801};
1802
1803static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001804state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001805{
1806 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001807 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001808
NeilBrownb2d444d2005-11-08 21:39:31 -08001809 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001810 len+= sprintf(page+len, "%sfaulty",sep);
1811 sep = ",";
1812 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001813 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001814 len += sprintf(page+len, "%sin_sync",sep);
1815 sep = ",";
1816 }
NeilBrownf6556752006-06-26 00:28:01 -07001817 if (test_bit(WriteMostly, &rdev->flags)) {
1818 len += sprintf(page+len, "%swrite_mostly",sep);
1819 sep = ",";
1820 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001821 if (!test_bit(Faulty, &rdev->flags) &&
1822 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001823 len += sprintf(page+len, "%sspare", sep);
1824 sep = ",";
1825 }
1826 return len+sprintf(page+len, "\n");
1827}
1828
NeilBrown45dc2de2006-06-26 00:27:58 -07001829static ssize_t
1830state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1831{
1832 /* can write
1833 * faulty - simulates and error
1834 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001835 * writemostly - sets write_mostly
1836 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001837 */
1838 int err = -EINVAL;
1839 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1840 md_error(rdev->mddev, rdev);
1841 err = 0;
1842 } else if (cmd_match(buf, "remove")) {
1843 if (rdev->raid_disk >= 0)
1844 err = -EBUSY;
1845 else {
1846 mddev_t *mddev = rdev->mddev;
1847 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001848 if (mddev->pers)
1849 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001850 md_new_event(mddev);
1851 err = 0;
1852 }
NeilBrownf6556752006-06-26 00:28:01 -07001853 } else if (cmd_match(buf, "writemostly")) {
1854 set_bit(WriteMostly, &rdev->flags);
1855 err = 0;
1856 } else if (cmd_match(buf, "-writemostly")) {
1857 clear_bit(WriteMostly, &rdev->flags);
1858 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001859 }
1860 return err ? err : len;
1861}
NeilBrown80ca3a42006-07-10 04:44:18 -07001862static struct rdev_sysfs_entry rdev_state =
1863__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001864
1865static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001866super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001867{
1868 if (rdev->sb_loaded && rdev->sb_size) {
1869 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1870 return rdev->sb_size;
1871 } else
1872 return 0;
1873}
NeilBrown96de1e62005-11-08 21:39:39 -08001874static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1875
NeilBrown4dbcdc72006-01-06 00:20:52 -08001876static ssize_t
1877errors_show(mdk_rdev_t *rdev, char *page)
1878{
1879 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1880}
1881
1882static ssize_t
1883errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1884{
1885 char *e;
1886 unsigned long n = simple_strtoul(buf, &e, 10);
1887 if (*buf && (*e == 0 || *e == '\n')) {
1888 atomic_set(&rdev->corrected_errors, n);
1889 return len;
1890 }
1891 return -EINVAL;
1892}
1893static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001894__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001895
NeilBrown014236d2006-01-06 00:20:55 -08001896static ssize_t
1897slot_show(mdk_rdev_t *rdev, char *page)
1898{
1899 if (rdev->raid_disk < 0)
1900 return sprintf(page, "none\n");
1901 else
1902 return sprintf(page, "%d\n", rdev->raid_disk);
1903}
1904
1905static ssize_t
1906slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1907{
1908 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001909 int err;
1910 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001911 int slot = simple_strtoul(buf, &e, 10);
1912 if (strncmp(buf, "none", 4)==0)
1913 slot = -1;
1914 else if (e==buf || (*e && *e!= '\n'))
1915 return -EINVAL;
NeilBrownc303da62008-02-06 01:39:51 -08001916 if (rdev->mddev->pers) {
1917 /* Setting 'slot' on an active array requires also
1918 * updating the 'rd%d' link, and communicating
1919 * with the personality with ->hot_*_disk.
1920 * For now we only support removing
1921 * failed/spare devices. This normally happens automatically,
1922 * but not when the metadata is externally managed.
1923 */
1924 if (slot != -1)
1925 return -EBUSY;
1926 if (rdev->raid_disk == -1)
1927 return -EEXIST;
1928 /* personality does all needed checks */
1929 if (rdev->mddev->pers->hot_add_disk == NULL)
1930 return -EINVAL;
1931 err = rdev->mddev->pers->
1932 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1933 if (err)
1934 return err;
1935 sprintf(nm, "rd%d", rdev->raid_disk);
1936 sysfs_remove_link(&rdev->mddev->kobj, nm);
1937 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1938 md_wakeup_thread(rdev->mddev->thread);
1939 } else {
1940 if (slot >= rdev->mddev->raid_disks)
1941 return -ENOSPC;
1942 rdev->raid_disk = slot;
1943 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001944 clear_bit(Faulty, &rdev->flags);
1945 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08001946 set_bit(In_sync, &rdev->flags);
1947 }
NeilBrown014236d2006-01-06 00:20:55 -08001948 return len;
1949}
1950
1951
1952static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001953__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001954
NeilBrown93c8cad2006-01-06 00:20:56 -08001955static ssize_t
1956offset_show(mdk_rdev_t *rdev, char *page)
1957{
Andrew Morton6961ece2006-01-06 00:20:59 -08001958 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001959}
1960
1961static ssize_t
1962offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1963{
1964 char *e;
1965 unsigned long long offset = simple_strtoull(buf, &e, 10);
1966 if (e==buf || (*e && *e != '\n'))
1967 return -EINVAL;
1968 if (rdev->mddev->pers)
1969 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001970 if (rdev->size && rdev->mddev->external)
1971 /* Must set offset before size, so overlap checks
1972 * can be sane */
1973 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08001974 rdev->data_offset = offset;
1975 return len;
1976}
1977
1978static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001979__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001980
NeilBrown83303b62006-01-06 00:21:06 -08001981static ssize_t
1982rdev_size_show(mdk_rdev_t *rdev, char *page)
1983{
1984 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1985}
1986
NeilBrownc5d79ad2008-02-06 01:39:54 -08001987static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
1988{
1989 /* check if two start/length pairs overlap */
1990 if (s1+l1 <= s2)
1991 return 0;
1992 if (s2+l2 <= s1)
1993 return 0;
1994 return 1;
1995}
1996
NeilBrown83303b62006-01-06 00:21:06 -08001997static ssize_t
1998rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1999{
2000 char *e;
2001 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002002 unsigned long long oldsize = rdev->size;
NeilBrown83303b62006-01-06 00:21:06 -08002003 if (e==buf || (*e && *e != '\n'))
2004 return -EINVAL;
2005 if (rdev->mddev->pers)
2006 return -EBUSY;
2007 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002008 if (size > oldsize && rdev->mddev->external) {
2009 /* need to check that all other rdevs with the same ->bdev
2010 * do not overlap. We need to unlock the mddev to avoid
2011 * a deadlock. We have already changed rdev->size, and if
2012 * we have to change it back, we will have the lock again.
2013 */
2014 mddev_t *mddev;
2015 int overlap = 0;
2016 struct list_head *tmp, *tmp2;
2017
2018 mddev_unlock(rdev->mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002019 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002020 mdk_rdev_t *rdev2;
2021
2022 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002023 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002024 if (test_bit(AllReserved, &rdev2->flags) ||
2025 (rdev->bdev == rdev2->bdev &&
2026 rdev != rdev2 &&
2027 overlaps(rdev->data_offset, rdev->size,
2028 rdev2->data_offset, rdev2->size))) {
2029 overlap = 1;
2030 break;
2031 }
2032 mddev_unlock(mddev);
2033 if (overlap) {
2034 mddev_put(mddev);
2035 break;
2036 }
2037 }
2038 mddev_lock(rdev->mddev);
2039 if (overlap) {
2040 /* Someone else could have slipped in a size
2041 * change here, but doing so is just silly.
2042 * We put oldsize back because we *know* it is
2043 * safe, and trust userspace not to race with
2044 * itself
2045 */
2046 rdev->size = oldsize;
2047 return -EBUSY;
2048 }
2049 }
NeilBrown83303b62006-01-06 00:21:06 -08002050 if (size < rdev->mddev->size || rdev->mddev->size == 0)
2051 rdev->mddev->size = size;
2052 return len;
2053}
2054
2055static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002056__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002057
NeilBrown86e6ffd2005-11-08 21:39:24 -08002058static struct attribute *rdev_default_attrs[] = {
2059 &rdev_state.attr,
2060 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002061 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002062 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002063 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002064 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002065 NULL,
2066};
2067static ssize_t
2068rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2069{
2070 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2071 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2072
2073 if (!entry->show)
2074 return -EIO;
2075 return entry->show(rdev, page);
2076}
2077
2078static ssize_t
2079rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2080 const char *page, size_t length)
2081{
2082 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2083 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrownca388052008-02-06 01:39:55 -08002084 int rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002085
2086 if (!entry->store)
2087 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002088 if (!capable(CAP_SYS_ADMIN))
2089 return -EACCES;
NeilBrownca388052008-02-06 01:39:55 -08002090 rv = mddev_lock(rdev->mddev);
2091 if (!rv) {
2092 rv = entry->store(rdev, page, length);
2093 mddev_unlock(rdev->mddev);
2094 }
2095 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002096}
2097
2098static void rdev_free(struct kobject *ko)
2099{
2100 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2101 kfree(rdev);
2102}
2103static struct sysfs_ops rdev_sysfs_ops = {
2104 .show = rdev_attr_show,
2105 .store = rdev_attr_store,
2106};
2107static struct kobj_type rdev_ktype = {
2108 .release = rdev_free,
2109 .sysfs_ops = &rdev_sysfs_ops,
2110 .default_attrs = rdev_default_attrs,
2111};
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113/*
2114 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2115 *
2116 * mark the device faulty if:
2117 *
2118 * - the device is nonexistent (zero size)
2119 * - the device has no valid superblock
2120 *
2121 * a faulty rdev _never_ has rdev->sb set.
2122 */
2123static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2124{
2125 char b[BDEVNAME_SIZE];
2126 int err;
2127 mdk_rdev_t *rdev;
2128 sector_t size;
2129
NeilBrown9ffae0c2006-01-06 00:20:32 -08002130 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (!rdev) {
2132 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2133 return ERR_PTR(-ENOMEM);
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 if ((err = alloc_disk_sb(rdev)))
2137 goto abort_free;
2138
NeilBrownc5d79ad2008-02-06 01:39:54 -08002139 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (err)
2141 goto abort_free;
2142
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002143 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002146 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002147 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002148 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002150 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002152 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002153 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2156 if (!size) {
2157 printk(KERN_WARNING
2158 "md: %s has zero or unknown size, marking faulty!\n",
2159 bdevname(rdev->bdev,b));
2160 err = -EINVAL;
2161 goto abort_free;
2162 }
2163
2164 if (super_format >= 0) {
2165 err = super_types[super_format].
2166 load_super(rdev, NULL, super_minor);
2167 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002168 printk(KERN_WARNING
2169 "md: %s does not have a valid v%d.%d "
2170 "superblock, not importing!\n",
2171 bdevname(rdev->bdev,b),
2172 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto abort_free;
2174 }
2175 if (err < 0) {
2176 printk(KERN_WARNING
2177 "md: could not read %s's sb, not importing!\n",
2178 bdevname(rdev->bdev,b));
2179 goto abort_free;
2180 }
2181 }
2182 INIT_LIST_HEAD(&rdev->same_set);
2183
2184 return rdev;
2185
2186abort_free:
2187 if (rdev->sb_page) {
2188 if (rdev->bdev)
2189 unlock_rdev(rdev);
2190 free_disk_sb(rdev);
2191 }
2192 kfree(rdev);
2193 return ERR_PTR(err);
2194}
2195
2196/*
2197 * Check a full RAID array for plausibility
2198 */
2199
2200
NeilBrowna757e642005-04-16 15:26:42 -07002201static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 int i;
2204 struct list_head *tmp;
2205 mdk_rdev_t *rdev, *freshest;
2206 char b[BDEVNAME_SIZE];
2207
2208 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002209 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 switch (super_types[mddev->major_version].
2211 load_super(rdev, freshest, mddev->minor_version)) {
2212 case 1:
2213 freshest = rdev;
2214 break;
2215 case 0:
2216 break;
2217 default:
2218 printk( KERN_ERR \
2219 "md: fatal superblock inconsistency in %s"
2220 " -- removing from array\n",
2221 bdevname(rdev->bdev,b));
2222 kick_rdev_from_array(rdev);
2223 }
2224
2225
2226 super_types[mddev->major_version].
2227 validate_super(mddev, freshest);
2228
2229 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002230 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (rdev != freshest)
2232 if (super_types[mddev->major_version].
2233 validate_super(mddev, rdev)) {
2234 printk(KERN_WARNING "md: kicking non-fresh %s"
2235 " from array!\n",
2236 bdevname(rdev->bdev,b));
2237 kick_rdev_from_array(rdev);
2238 continue;
2239 }
2240 if (mddev->level == LEVEL_MULTIPATH) {
2241 rdev->desc_nr = i++;
2242 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002243 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002244 } else if (rdev->raid_disk >= mddev->raid_disks) {
2245 rdev->raid_disk = -1;
2246 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248 }
2249
2250
2251
2252 if (mddev->recovery_cp != MaxSector &&
2253 mddev->level >= 1)
2254 printk(KERN_ERR "md: %s: raid array is not clean"
2255 " -- starting background reconstruction\n",
2256 mdname(mddev));
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
NeilBrowneae17012005-11-08 21:39:23 -08002260static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002261safe_delay_show(mddev_t *mddev, char *page)
2262{
2263 int msec = (mddev->safemode_delay*1000)/HZ;
2264 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2265}
2266static ssize_t
2267safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2268{
2269 int scale=1;
2270 int dot=0;
2271 int i;
2272 unsigned long msec;
2273 char buf[30];
2274 char *e;
2275 /* remove a period, and count digits after it */
2276 if (len >= sizeof(buf))
2277 return -EINVAL;
2278 strlcpy(buf, cbuf, len);
2279 buf[len] = 0;
2280 for (i=0; i<len; i++) {
2281 if (dot) {
2282 if (isdigit(buf[i])) {
2283 buf[i-1] = buf[i];
2284 scale *= 10;
2285 }
2286 buf[i] = 0;
2287 } else if (buf[i] == '.') {
2288 dot=1;
2289 buf[i] = 0;
2290 }
2291 }
2292 msec = simple_strtoul(buf, &e, 10);
2293 if (e == buf || (*e && *e != '\n'))
2294 return -EINVAL;
2295 msec = (msec * 1000) / scale;
2296 if (msec == 0)
2297 mddev->safemode_delay = 0;
2298 else {
2299 mddev->safemode_delay = (msec*HZ)/1000;
2300 if (mddev->safemode_delay == 0)
2301 mddev->safemode_delay = 1;
2302 }
2303 return len;
2304}
2305static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002306__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002307
2308static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002309level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002310{
NeilBrown2604b702006-01-06 00:20:36 -08002311 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002312 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002313 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002314 else if (mddev->clevel[0])
2315 return sprintf(page, "%s\n", mddev->clevel);
2316 else if (mddev->level != LEVEL_NONE)
2317 return sprintf(page, "%d\n", mddev->level);
2318 else
2319 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002320}
2321
NeilBrownd9d166c2006-01-06 00:20:51 -08002322static ssize_t
2323level_store(mddev_t *mddev, const char *buf, size_t len)
2324{
NeilBrown20a49ff2008-02-06 01:39:57 -08002325 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002326 if (mddev->pers)
2327 return -EBUSY;
2328 if (len == 0)
2329 return 0;
2330 if (len >= sizeof(mddev->clevel))
2331 return -ENOSPC;
2332 strncpy(mddev->clevel, buf, len);
2333 if (mddev->clevel[len-1] == '\n')
2334 len--;
2335 mddev->clevel[len] = 0;
2336 mddev->level = LEVEL_NONE;
2337 return rv;
2338}
2339
2340static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002341__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002342
NeilBrownd4dbd022006-06-26 00:27:59 -07002343
2344static ssize_t
2345layout_show(mddev_t *mddev, char *page)
2346{
2347 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002348 if (mddev->reshape_position != MaxSector &&
2349 mddev->layout != mddev->new_layout)
2350 return sprintf(page, "%d (%d)\n",
2351 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002352 return sprintf(page, "%d\n", mddev->layout);
2353}
2354
2355static ssize_t
2356layout_store(mddev_t *mddev, const char *buf, size_t len)
2357{
2358 char *e;
2359 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002360
2361 if (!*buf || (*e && *e != '\n'))
2362 return -EINVAL;
2363
NeilBrown08a02ec2007-05-09 02:35:38 -07002364 if (mddev->pers)
2365 return -EBUSY;
2366 if (mddev->reshape_position != MaxSector)
2367 mddev->new_layout = n;
2368 else
2369 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002370 return len;
2371}
2372static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002373__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002374
2375
NeilBrowneae17012005-11-08 21:39:23 -08002376static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002377raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002378{
NeilBrownbb636542005-11-08 21:39:45 -08002379 if (mddev->raid_disks == 0)
2380 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002381 if (mddev->reshape_position != MaxSector &&
2382 mddev->delta_disks != 0)
2383 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2384 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002385 return sprintf(page, "%d\n", mddev->raid_disks);
2386}
2387
NeilBrownda943b992006-01-06 00:20:54 -08002388static int update_raid_disks(mddev_t *mddev, int raid_disks);
2389
2390static ssize_t
2391raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2392{
NeilBrownda943b992006-01-06 00:20:54 -08002393 char *e;
2394 int rv = 0;
2395 unsigned long n = simple_strtoul(buf, &e, 10);
2396
2397 if (!*buf || (*e && *e != '\n'))
2398 return -EINVAL;
2399
2400 if (mddev->pers)
2401 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002402 else if (mddev->reshape_position != MaxSector) {
2403 int olddisks = mddev->raid_disks - mddev->delta_disks;
2404 mddev->delta_disks = n - olddisks;
2405 mddev->raid_disks = n;
2406 } else
NeilBrownda943b992006-01-06 00:20:54 -08002407 mddev->raid_disks = n;
2408 return rv ? rv : len;
2409}
2410static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002411__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002412
NeilBrown24dd4692005-11-08 21:39:26 -08002413static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002414chunk_size_show(mddev_t *mddev, char *page)
2415{
NeilBrown08a02ec2007-05-09 02:35:38 -07002416 if (mddev->reshape_position != MaxSector &&
2417 mddev->chunk_size != mddev->new_chunk)
2418 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2419 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002420 return sprintf(page, "%d\n", mddev->chunk_size);
2421}
2422
2423static ssize_t
2424chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2425{
2426 /* can only set chunk_size if array is not yet active */
2427 char *e;
2428 unsigned long n = simple_strtoul(buf, &e, 10);
2429
NeilBrown3b343802006-01-06 00:20:47 -08002430 if (!*buf || (*e && *e != '\n'))
2431 return -EINVAL;
2432
NeilBrown08a02ec2007-05-09 02:35:38 -07002433 if (mddev->pers)
2434 return -EBUSY;
2435 else if (mddev->reshape_position != MaxSector)
2436 mddev->new_chunk = n;
2437 else
2438 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002439 return len;
2440}
2441static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002442__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002443
NeilBrowna94213b2006-06-26 00:28:00 -07002444static ssize_t
2445resync_start_show(mddev_t *mddev, char *page)
2446{
2447 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2448}
2449
2450static ssize_t
2451resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2452{
2453 /* can only set chunk_size if array is not yet active */
2454 char *e;
2455 unsigned long long n = simple_strtoull(buf, &e, 10);
2456
2457 if (mddev->pers)
2458 return -EBUSY;
2459 if (!*buf || (*e && *e != '\n'))
2460 return -EINVAL;
2461
2462 mddev->recovery_cp = n;
2463 return len;
2464}
2465static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002466__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002467
NeilBrown9e653b62006-06-26 00:27:58 -07002468/*
2469 * The array state can be:
2470 *
2471 * clear
2472 * No devices, no size, no level
2473 * Equivalent to STOP_ARRAY ioctl
2474 * inactive
2475 * May have some settings, but array is not active
2476 * all IO results in error
2477 * When written, doesn't tear down array, but just stops it
2478 * suspended (not supported yet)
2479 * All IO requests will block. The array can be reconfigured.
2480 * Writing this, if accepted, will block until array is quiessent
2481 * readonly
2482 * no resync can happen. no superblocks get written.
2483 * write requests fail
2484 * read-auto
2485 * like readonly, but behaves like 'clean' on a write request.
2486 *
2487 * clean - no pending writes, but otherwise active.
2488 * When written to inactive array, starts without resync
2489 * If a write request arrives then
2490 * if metadata is known, mark 'dirty' and switch to 'active'.
2491 * if not known, block and switch to write-pending
2492 * If written to an active array that has pending writes, then fails.
2493 * active
2494 * fully active: IO and resync can be happening.
2495 * When written to inactive array, starts with resync
2496 *
2497 * write-pending
2498 * clean, but writes are blocked waiting for 'active' to be written.
2499 *
2500 * active-idle
2501 * like active, but no writes have been seen for a while (100msec).
2502 *
2503 */
2504enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2505 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002506static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002507 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2508 "write-pending", "active-idle", NULL };
2509
2510static int match_word(const char *word, char **list)
2511{
2512 int n;
2513 for (n=0; list[n]; n++)
2514 if (cmd_match(word, list[n]))
2515 break;
2516 return n;
2517}
2518
2519static ssize_t
2520array_state_show(mddev_t *mddev, char *page)
2521{
2522 enum array_state st = inactive;
2523
2524 if (mddev->pers)
2525 switch(mddev->ro) {
2526 case 1:
2527 st = readonly;
2528 break;
2529 case 2:
2530 st = read_auto;
2531 break;
2532 case 0:
2533 if (mddev->in_sync)
2534 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002535 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2536 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002537 else if (mddev->safemode)
2538 st = active_idle;
2539 else
2540 st = active;
2541 }
2542 else {
2543 if (list_empty(&mddev->disks) &&
2544 mddev->raid_disks == 0 &&
2545 mddev->size == 0)
2546 st = clear;
2547 else
2548 st = inactive;
2549 }
2550 return sprintf(page, "%s\n", array_states[st]);
2551}
2552
2553static int do_md_stop(mddev_t * mddev, int ro);
2554static int do_md_run(mddev_t * mddev);
2555static int restart_array(mddev_t *mddev);
2556
2557static ssize_t
2558array_state_store(mddev_t *mddev, const char *buf, size_t len)
2559{
2560 int err = -EINVAL;
2561 enum array_state st = match_word(buf, array_states);
2562 switch(st) {
2563 case bad_word:
2564 break;
2565 case clear:
2566 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002567 if (atomic_read(&mddev->active) > 1)
2568 return -EBUSY;
2569 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002570 break;
2571 case inactive:
2572 /* stopping an active array */
2573 if (mddev->pers) {
2574 if (atomic_read(&mddev->active) > 1)
2575 return -EBUSY;
2576 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002577 } else
2578 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002579 break;
2580 case suspended:
2581 break; /* not supported yet */
2582 case readonly:
2583 if (mddev->pers)
2584 err = do_md_stop(mddev, 1);
2585 else {
2586 mddev->ro = 1;
2587 err = do_md_run(mddev);
2588 }
2589 break;
2590 case read_auto:
2591 /* stopping an active array */
2592 if (mddev->pers) {
2593 err = do_md_stop(mddev, 1);
2594 if (err == 0)
2595 mddev->ro = 2; /* FIXME mark devices writable */
2596 } else {
2597 mddev->ro = 2;
2598 err = do_md_run(mddev);
2599 }
2600 break;
2601 case clean:
2602 if (mddev->pers) {
2603 restart_array(mddev);
2604 spin_lock_irq(&mddev->write_lock);
2605 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002606 if (mddev->in_sync == 0) {
2607 mddev->in_sync = 1;
2608 if (mddev->persistent)
2609 set_bit(MD_CHANGE_CLEAN,
2610 &mddev->flags);
2611 }
2612 err = 0;
2613 } else
2614 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002615 spin_unlock_irq(&mddev->write_lock);
2616 } else {
2617 mddev->ro = 0;
2618 mddev->recovery_cp = MaxSector;
2619 err = do_md_run(mddev);
2620 }
2621 break;
2622 case active:
2623 if (mddev->pers) {
2624 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002625 if (mddev->external)
2626 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002627 wake_up(&mddev->sb_wait);
2628 err = 0;
2629 } else {
2630 mddev->ro = 0;
2631 err = do_md_run(mddev);
2632 }
2633 break;
2634 case write_pending:
2635 case active_idle:
2636 /* these cannot be set */
2637 break;
2638 }
2639 if (err)
2640 return err;
2641 else
2642 return len;
2643}
NeilBrown80ca3a42006-07-10 04:44:18 -07002644static struct md_sysfs_entry md_array_state =
2645__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002646
NeilBrown6d7ff732006-01-06 00:21:16 -08002647static ssize_t
2648null_show(mddev_t *mddev, char *page)
2649{
2650 return -EINVAL;
2651}
2652
2653static ssize_t
2654new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2655{
2656 /* buf must be %d:%d\n? giving major and minor numbers */
2657 /* The new device is added to the array.
2658 * If the array has a persistent superblock, we read the
2659 * superblock to initialise info and check validity.
2660 * Otherwise, only checking done is that in bind_rdev_to_array,
2661 * which mainly checks size.
2662 */
2663 char *e;
2664 int major = simple_strtoul(buf, &e, 10);
2665 int minor;
2666 dev_t dev;
2667 mdk_rdev_t *rdev;
2668 int err;
2669
2670 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2671 return -EINVAL;
2672 minor = simple_strtoul(e+1, &e, 10);
2673 if (*e && *e != '\n')
2674 return -EINVAL;
2675 dev = MKDEV(major, minor);
2676 if (major != MAJOR(dev) ||
2677 minor != MINOR(dev))
2678 return -EOVERFLOW;
2679
2680
2681 if (mddev->persistent) {
2682 rdev = md_import_device(dev, mddev->major_version,
2683 mddev->minor_version);
2684 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2685 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2686 mdk_rdev_t, same_set);
2687 err = super_types[mddev->major_version]
2688 .load_super(rdev, rdev0, mddev->minor_version);
2689 if (err < 0)
2690 goto out;
2691 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002692 } else if (mddev->external)
2693 rdev = md_import_device(dev, -2, -1);
2694 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002695 rdev = md_import_device(dev, -1, -1);
2696
2697 if (IS_ERR(rdev))
2698 return PTR_ERR(rdev);
2699 err = bind_rdev_to_array(rdev, mddev);
2700 out:
2701 if (err)
2702 export_rdev(rdev);
2703 return err ? err : len;
2704}
2705
2706static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002707__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002708
2709static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002710bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2711{
2712 char *end;
2713 unsigned long chunk, end_chunk;
2714
2715 if (!mddev->bitmap)
2716 goto out;
2717 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2718 while (*buf) {
2719 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2720 if (buf == end) break;
2721 if (*end == '-') { /* range */
2722 buf = end + 1;
2723 end_chunk = simple_strtoul(buf, &end, 0);
2724 if (buf == end) break;
2725 }
2726 if (*end && !isspace(*end)) break;
2727 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2728 buf = end;
2729 while (isspace(*buf)) buf++;
2730 }
2731 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2732out:
2733 return len;
2734}
2735
2736static struct md_sysfs_entry md_bitmap =
2737__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2738
2739static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002740size_show(mddev_t *mddev, char *page)
2741{
2742 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2743}
2744
2745static int update_size(mddev_t *mddev, unsigned long size);
2746
2747static ssize_t
2748size_store(mddev_t *mddev, const char *buf, size_t len)
2749{
2750 /* If array is inactive, we can reduce the component size, but
2751 * not increase it (except from 0).
2752 * If array is active, we can try an on-line resize
2753 */
2754 char *e;
2755 int err = 0;
2756 unsigned long long size = simple_strtoull(buf, &e, 10);
2757 if (!*buf || *buf == '\n' ||
2758 (*e && *e != '\n'))
2759 return -EINVAL;
2760
2761 if (mddev->pers) {
2762 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002763 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002764 } else {
2765 if (mddev->size == 0 ||
2766 mddev->size > size)
2767 mddev->size = size;
2768 else
2769 err = -ENOSPC;
2770 }
2771 return err ? err : len;
2772}
2773
2774static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002775__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002776
NeilBrown8bb93aa2006-01-06 00:20:50 -08002777
2778/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002779 * This is one of
2780 * 'none' for arrays with no metadata (good luck...)
2781 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002782 * or N.M for internally known formats
2783 */
2784static ssize_t
2785metadata_show(mddev_t *mddev, char *page)
2786{
2787 if (mddev->persistent)
2788 return sprintf(page, "%d.%d\n",
2789 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002790 else if (mddev->external)
2791 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002792 else
2793 return sprintf(page, "none\n");
2794}
2795
2796static ssize_t
2797metadata_store(mddev_t *mddev, const char *buf, size_t len)
2798{
2799 int major, minor;
2800 char *e;
2801 if (!list_empty(&mddev->disks))
2802 return -EBUSY;
2803
2804 if (cmd_match(buf, "none")) {
2805 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002806 mddev->external = 0;
2807 mddev->major_version = 0;
2808 mddev->minor_version = 90;
2809 return len;
2810 }
2811 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002812 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002813 if (namelen >= sizeof(mddev->metadata_type))
2814 namelen = sizeof(mddev->metadata_type)-1;
2815 strncpy(mddev->metadata_type, buf+9, namelen);
2816 mddev->metadata_type[namelen] = 0;
2817 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2818 mddev->metadata_type[--namelen] = 0;
2819 mddev->persistent = 0;
2820 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002821 mddev->major_version = 0;
2822 mddev->minor_version = 90;
2823 return len;
2824 }
2825 major = simple_strtoul(buf, &e, 10);
2826 if (e==buf || *e != '.')
2827 return -EINVAL;
2828 buf = e+1;
2829 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002830 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002831 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002832 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002833 return -ENOENT;
2834 mddev->major_version = major;
2835 mddev->minor_version = minor;
2836 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002837 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002838 return len;
2839}
2840
2841static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002842__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002843
NeilBrowna35b0d62006-01-06 00:20:49 -08002844static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002845action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002846{
NeilBrown7eec3142005-11-08 21:39:44 -08002847 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002848 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002849 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002850 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2851 type = "reshape";
2852 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002853 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2854 type = "resync";
2855 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2856 type = "check";
2857 else
2858 type = "repair";
2859 } else
2860 type = "recover";
2861 }
2862 return sprintf(page, "%s\n", type);
2863}
2864
2865static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002866action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002867{
NeilBrown7eec3142005-11-08 21:39:44 -08002868 if (!mddev->pers || !mddev->pers->sync_request)
2869 return -EINVAL;
2870
NeilBrownbce74da2006-01-06 00:20:41 -08002871 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002872 if (mddev->sync_thread) {
2873 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2874 md_unregister_thread(mddev->sync_thread);
2875 mddev->sync_thread = NULL;
2876 mddev->recovery = 0;
2877 }
NeilBrown03c902e2006-01-06 00:20:46 -08002878 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2879 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002880 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002881 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002882 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002883 else if (cmd_match(page, "reshape")) {
2884 int err;
2885 if (mddev->pers->start_reshape == NULL)
2886 return -EINVAL;
2887 err = mddev->pers->start_reshape(mddev);
2888 if (err)
2889 return err;
2890 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002891 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002892 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002893 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002894 return -EINVAL;
2895 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2896 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002897 }
NeilBrown03c902e2006-01-06 00:20:46 -08002898 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002899 md_wakeup_thread(mddev->thread);
2900 return len;
2901}
2902
NeilBrown9d888832005-11-08 21:39:26 -08002903static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002904mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002905{
2906 return sprintf(page, "%llu\n",
2907 (unsigned long long) mddev->resync_mismatches);
2908}
2909
NeilBrown80ca3a42006-07-10 04:44:18 -07002910static struct md_sysfs_entry md_scan_mode =
2911__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002912
NeilBrown96de1e62005-11-08 21:39:39 -08002913
NeilBrown80ca3a42006-07-10 04:44:18 -07002914static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002915
NeilBrown88202a02006-01-06 00:21:36 -08002916static ssize_t
2917sync_min_show(mddev_t *mddev, char *page)
2918{
2919 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2920 mddev->sync_speed_min ? "local": "system");
2921}
2922
2923static ssize_t
2924sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2925{
2926 int min;
2927 char *e;
2928 if (strncmp(buf, "system", 6)==0) {
2929 mddev->sync_speed_min = 0;
2930 return len;
2931 }
2932 min = simple_strtoul(buf, &e, 10);
2933 if (buf == e || (*e && *e != '\n') || min <= 0)
2934 return -EINVAL;
2935 mddev->sync_speed_min = min;
2936 return len;
2937}
2938
2939static struct md_sysfs_entry md_sync_min =
2940__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2941
2942static ssize_t
2943sync_max_show(mddev_t *mddev, char *page)
2944{
2945 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2946 mddev->sync_speed_max ? "local": "system");
2947}
2948
2949static ssize_t
2950sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2951{
2952 int max;
2953 char *e;
2954 if (strncmp(buf, "system", 6)==0) {
2955 mddev->sync_speed_max = 0;
2956 return len;
2957 }
2958 max = simple_strtoul(buf, &e, 10);
2959 if (buf == e || (*e && *e != '\n') || max <= 0)
2960 return -EINVAL;
2961 mddev->sync_speed_max = max;
2962 return len;
2963}
2964
2965static struct md_sysfs_entry md_sync_max =
2966__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2967
Iustin Popd7f3d292007-10-16 23:30:54 -07002968static ssize_t
2969degraded_show(mddev_t *mddev, char *page)
2970{
2971 return sprintf(page, "%d\n", mddev->degraded);
2972}
2973static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08002974
2975static ssize_t
2976sync_speed_show(mddev_t *mddev, char *page)
2977{
2978 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002979 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002980 dt = ((jiffies - mddev->resync_mark) / HZ);
2981 if (!dt) dt++;
2982 db = resync - (mddev->resync_mark_cnt);
2983 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2984}
2985
NeilBrown80ca3a42006-07-10 04:44:18 -07002986static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002987
2988static ssize_t
2989sync_completed_show(mddev_t *mddev, char *page)
2990{
2991 unsigned long max_blocks, resync;
2992
2993 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2994 max_blocks = mddev->resync_max_sectors;
2995 else
2996 max_blocks = mddev->size << 1;
2997
2998 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2999 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3000}
3001
NeilBrown80ca3a42006-07-10 04:44:18 -07003002static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003003
NeilBrowne464eaf2006-03-27 01:18:14 -08003004static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003005max_sync_show(mddev_t *mddev, char *page)
3006{
3007 if (mddev->resync_max == MaxSector)
3008 return sprintf(page, "max\n");
3009 else
3010 return sprintf(page, "%llu\n",
3011 (unsigned long long)mddev->resync_max);
3012}
3013static ssize_t
3014max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3015{
3016 if (strncmp(buf, "max", 3) == 0)
3017 mddev->resync_max = MaxSector;
3018 else {
3019 char *ep;
3020 unsigned long long max = simple_strtoull(buf, &ep, 10);
3021 if (ep == buf || (*ep != 0 && *ep != '\n'))
3022 return -EINVAL;
3023 if (max < mddev->resync_max &&
3024 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3025 return -EBUSY;
3026
3027 /* Must be a multiple of chunk_size */
3028 if (mddev->chunk_size) {
3029 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3030 return -EINVAL;
3031 }
3032 mddev->resync_max = max;
3033 }
3034 wake_up(&mddev->recovery_wait);
3035 return len;
3036}
3037
3038static struct md_sysfs_entry md_max_sync =
3039__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3040
3041static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003042suspend_lo_show(mddev_t *mddev, char *page)
3043{
3044 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3045}
3046
3047static ssize_t
3048suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3049{
3050 char *e;
3051 unsigned long long new = simple_strtoull(buf, &e, 10);
3052
3053 if (mddev->pers->quiesce == NULL)
3054 return -EINVAL;
3055 if (buf == e || (*e && *e != '\n'))
3056 return -EINVAL;
3057 if (new >= mddev->suspend_hi ||
3058 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3059 mddev->suspend_lo = new;
3060 mddev->pers->quiesce(mddev, 2);
3061 return len;
3062 } else
3063 return -EINVAL;
3064}
3065static struct md_sysfs_entry md_suspend_lo =
3066__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3067
3068
3069static ssize_t
3070suspend_hi_show(mddev_t *mddev, char *page)
3071{
3072 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3073}
3074
3075static ssize_t
3076suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3077{
3078 char *e;
3079 unsigned long long new = simple_strtoull(buf, &e, 10);
3080
3081 if (mddev->pers->quiesce == NULL)
3082 return -EINVAL;
3083 if (buf == e || (*e && *e != '\n'))
3084 return -EINVAL;
3085 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3086 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3087 mddev->suspend_hi = new;
3088 mddev->pers->quiesce(mddev, 1);
3089 mddev->pers->quiesce(mddev, 0);
3090 return len;
3091 } else
3092 return -EINVAL;
3093}
3094static struct md_sysfs_entry md_suspend_hi =
3095__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3096
NeilBrown08a02ec2007-05-09 02:35:38 -07003097static ssize_t
3098reshape_position_show(mddev_t *mddev, char *page)
3099{
3100 if (mddev->reshape_position != MaxSector)
3101 return sprintf(page, "%llu\n",
3102 (unsigned long long)mddev->reshape_position);
3103 strcpy(page, "none\n");
3104 return 5;
3105}
3106
3107static ssize_t
3108reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3109{
3110 char *e;
3111 unsigned long long new = simple_strtoull(buf, &e, 10);
3112 if (mddev->pers)
3113 return -EBUSY;
3114 if (buf == e || (*e && *e != '\n'))
3115 return -EINVAL;
3116 mddev->reshape_position = new;
3117 mddev->delta_disks = 0;
3118 mddev->new_level = mddev->level;
3119 mddev->new_layout = mddev->layout;
3120 mddev->new_chunk = mddev->chunk_size;
3121 return len;
3122}
3123
3124static struct md_sysfs_entry md_reshape_position =
3125__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3126 reshape_position_store);
3127
NeilBrowne464eaf2006-03-27 01:18:14 -08003128
NeilBrowneae17012005-11-08 21:39:23 -08003129static struct attribute *md_default_attrs[] = {
3130 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003131 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003132 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003133 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003134 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003135 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003136 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003137 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003138 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003139 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003140 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003141 NULL,
3142};
3143
3144static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003145 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003146 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003147 &md_sync_min.attr,
3148 &md_sync_max.attr,
3149 &md_sync_speed.attr,
3150 &md_sync_completed.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003151 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003152 &md_suspend_lo.attr,
3153 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003154 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003155 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003156 NULL,
3157};
NeilBrown411036f2005-11-08 21:39:40 -08003158static struct attribute_group md_redundancy_group = {
3159 .name = NULL,
3160 .attrs = md_redundancy_attrs,
3161};
3162
NeilBrowneae17012005-11-08 21:39:23 -08003163
3164static ssize_t
3165md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3166{
3167 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3168 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003169 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003170
3171 if (!entry->show)
3172 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003173 rv = mddev_lock(mddev);
3174 if (!rv) {
3175 rv = entry->show(mddev, page);
3176 mddev_unlock(mddev);
3177 }
NeilBrown96de1e62005-11-08 21:39:39 -08003178 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003179}
3180
3181static ssize_t
3182md_attr_store(struct kobject *kobj, struct attribute *attr,
3183 const char *page, size_t length)
3184{
3185 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3186 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003187 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003188
3189 if (!entry->store)
3190 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003191 if (!capable(CAP_SYS_ADMIN))
3192 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003193 rv = mddev_lock(mddev);
3194 if (!rv) {
3195 rv = entry->store(mddev, page, length);
3196 mddev_unlock(mddev);
3197 }
NeilBrown96de1e62005-11-08 21:39:39 -08003198 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003199}
3200
3201static void md_free(struct kobject *ko)
3202{
3203 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3204 kfree(mddev);
3205}
3206
3207static struct sysfs_ops md_sysfs_ops = {
3208 .show = md_attr_show,
3209 .store = md_attr_store,
3210};
3211static struct kobj_type md_ktype = {
3212 .release = md_free,
3213 .sysfs_ops = &md_sysfs_ops,
3214 .default_attrs = md_default_attrs,
3215};
3216
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217int mdp_major = 0;
3218
3219static struct kobject *md_probe(dev_t dev, int *part, void *data)
3220{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003221 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 mddev_t *mddev = mddev_find(dev);
3223 struct gendisk *disk;
3224 int partitioned = (MAJOR(dev) != MD_MAJOR);
3225 int shift = partitioned ? MdpMinorShift : 0;
3226 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003227 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
3229 if (!mddev)
3230 return NULL;
3231
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003232 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003234 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 mddev_put(mddev);
3236 return NULL;
3237 }
3238 disk = alloc_disk(1 << shift);
3239 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003240 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 mddev_put(mddev);
3242 return NULL;
3243 }
3244 disk->major = MAJOR(dev);
3245 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003246 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003248 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 disk->fops = &md_fops;
3251 disk->private_data = mddev;
3252 disk->queue = mddev->queue;
3253 add_disk(disk);
3254 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003255 mutex_unlock(&disks_mutex);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003256 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003257 "%s", "md");
3258 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003259 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3260 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003261 else
3262 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return NULL;
3264}
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266static void md_safemode_timeout(unsigned long data)
3267{
3268 mddev_t *mddev = (mddev_t *) data;
3269
3270 mddev->safemode = 1;
3271 md_wakeup_thread(mddev->thread);
3272}
3273
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003274static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276static int do_md_run(mddev_t * mddev)
3277{
NeilBrown2604b702006-01-06 00:20:36 -08003278 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 int chunk_size;
3280 struct list_head *tmp;
3281 mdk_rdev_t *rdev;
3282 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003283 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 char b[BDEVNAME_SIZE];
3285
NeilBrowna757e642005-04-16 15:26:42 -07003286 if (list_empty(&mddev->disks))
3287 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 if (mddev->pers)
3291 return -EBUSY;
3292
3293 /*
3294 * Analyze all RAID superblock(s)
3295 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003296 if (!mddev->raid_disks) {
3297 if (!mddev->persistent)
3298 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003299 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301
3302 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
NeilBrown2604b702006-01-06 00:20:36 -08003304 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 if (chunk_size > MAX_CHUNK_SIZE) {
3306 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3307 chunk_size, MAX_CHUNK_SIZE);
3308 return -EINVAL;
3309 }
3310 /*
3311 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3312 */
3313 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003314 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 return -EINVAL;
3316 }
3317 if (chunk_size < PAGE_SIZE) {
3318 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3319 chunk_size, PAGE_SIZE);
3320 return -EINVAL;
3321 }
3322
3323 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003324 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003325 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 continue;
3327 if (rdev->size < chunk_size / 1024) {
3328 printk(KERN_WARNING
3329 "md: Dev %s smaller than chunk_size:"
3330 " %lluk < %dk\n",
3331 bdevname(rdev->bdev,b),
3332 (unsigned long long)rdev->size,
3333 chunk_size / 1024);
3334 return -EINVAL;
3335 }
3336 }
3337 }
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003340 if (mddev->level != LEVEL_NONE)
3341 request_module("md-level-%d", mddev->level);
3342 else if (mddev->clevel[0])
3343 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344#endif
3345
3346 /*
3347 * Drop all container device buffers, from now on
3348 * the only valid external interface is through the md
3349 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003351 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003352 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 continue;
3354 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003355 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003356
3357 /* perform some consistency tests on the device.
3358 * We don't want the data to overlap the metadata,
3359 * Internal Bitmap issues has handled elsewhere.
3360 */
3361 if (rdev->data_offset < rdev->sb_offset) {
3362 if (mddev->size &&
3363 rdev->data_offset + mddev->size*2
3364 > rdev->sb_offset*2) {
3365 printk("md: %s: data overlaps metadata\n",
3366 mdname(mddev));
3367 return -EINVAL;
3368 }
3369 } else {
3370 if (rdev->sb_offset*2 + rdev->sb_size/512
3371 > rdev->data_offset) {
3372 printk("md: %s: metadata overlaps data\n",
3373 mdname(mddev));
3374 return -EINVAL;
3375 }
3376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 }
3378
3379 md_probe(mddev->unit, NULL, NULL);
3380 disk = mddev->gendisk;
3381 if (!disk)
3382 return -ENOMEM;
3383
3384 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003385 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003386 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003388 if (mddev->level != LEVEL_NONE)
3389 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3390 mddev->level);
3391 else
3392 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3393 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 return -EINVAL;
3395 }
NeilBrown2604b702006-01-06 00:20:36 -08003396 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003398 mddev->level = pers->level;
3399 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
NeilBrownf6705572006-03-27 01:18:11 -08003401 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003402 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003403 /* This personality cannot handle reshaping... */
3404 mddev->pers = NULL;
3405 module_put(pers->owner);
3406 return -EINVAL;
3407 }
3408
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003409 if (pers->sync_request) {
3410 /* Warn if this is a potentially silly
3411 * configuration.
3412 */
3413 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3414 mdk_rdev_t *rdev2;
3415 struct list_head *tmp2;
3416 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003417 rdev_for_each(rdev, tmp, mddev) {
3418 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003419 if (rdev < rdev2 &&
3420 rdev->bdev->bd_contains ==
3421 rdev2->bdev->bd_contains) {
3422 printk(KERN_WARNING
3423 "%s: WARNING: %s appears to be"
3424 " on the same physical disk as"
3425 " %s.\n",
3426 mdname(mddev),
3427 bdevname(rdev->bdev,b),
3428 bdevname(rdev2->bdev,b2));
3429 warned = 1;
3430 }
3431 }
3432 }
3433 if (warned)
3434 printk(KERN_WARNING
3435 "True protection against single-disk"
3436 " failure might be compromised.\n");
3437 }
3438
NeilBrown657390d2005-08-26 18:34:16 -07003439 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003441 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003442 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443
NeilBrownf91de922005-11-08 21:39:36 -08003444 if (start_readonly)
3445 mddev->ro = 2; /* read-only, but switch on first write */
3446
NeilBrownb15c2e52006-01-06 00:20:16 -08003447 err = mddev->pers->run(mddev);
3448 if (!err && mddev->pers->sync_request) {
3449 err = bitmap_create(mddev);
3450 if (err) {
3451 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3452 mdname(mddev), err);
3453 mddev->pers->stop(mddev);
3454 }
3455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (err) {
3457 printk(KERN_ERR "md: pers->run() failed ...\n");
3458 module_put(mddev->pers->owner);
3459 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003460 bitmap_destroy(mddev);
3461 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003463 if (mddev->pers->sync_request) {
3464 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3465 printk(KERN_WARNING
3466 "md: cannot register extra attributes for %s\n",
3467 mdname(mddev));
3468 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003469 mddev->ro = 0;
3470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 atomic_set(&mddev->writes_pending,0);
3472 mddev->safemode = 0;
3473 mddev->safemode_timer.function = md_safemode_timeout;
3474 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003475 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003477
NeilBrownd089c6a2008-02-06 01:39:59 -08003478 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003479 if (rdev->raid_disk >= 0) {
3480 char nm[20];
3481 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003482 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3483 printk("md: cannot register %s for %s\n",
3484 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3488
NeilBrown850b2b42006-10-03 01:15:46 -07003489 if (mddev->flags)
3490 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491
3492 set_capacity(disk, mddev->array_size<<1);
3493
3494 /* If we call blk_queue_make_request here, it will
3495 * re-initialise max_sectors etc which may have been
3496 * refined inside -> run. So just set the bits we need to set.
3497 * Most initialisation happended when we called
3498 * blk_queue_make_request(..., md_fail_request)
3499 * earlier.
3500 */
3501 mddev->queue->queuedata = mddev;
3502 mddev->queue->make_request_fn = mddev->pers->make_request;
3503
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003504 /* If there is a partially-recovered drive we need to
3505 * start recovery here. If we leave it to md_check_recovery,
3506 * it will remove the drives and not do the right thing
3507 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003508 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003509 struct list_head *rtmp;
3510 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003511 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003512 if (rdev->raid_disk >= 0 &&
3513 !test_bit(In_sync, &rdev->flags) &&
3514 !test_bit(Faulty, &rdev->flags))
3515 /* complete an interrupted recovery */
3516 spares++;
3517 if (spares && mddev->pers->sync_request) {
3518 mddev->recovery = 0;
3519 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3520 mddev->sync_thread = md_register_thread(md_do_sync,
3521 mddev,
3522 "%s_resync");
3523 if (!mddev->sync_thread) {
3524 printk(KERN_ERR "%s: could not start resync"
3525 " thread...\n",
3526 mdname(mddev));
3527 /* leave the spares where they are, it shouldn't hurt */
3528 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003529 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003530 }
3531 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003532 md_wakeup_thread(mddev->thread);
3533 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003534
Linus Torvalds44ce62942007-05-09 18:51:36 -07003535 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003536 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003537 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 return 0;
3539}
3540
3541static int restart_array(mddev_t *mddev)
3542{
3543 struct gendisk *disk = mddev->gendisk;
3544 int err;
3545
3546 /*
3547 * Complain if it has no devices
3548 */
3549 err = -ENXIO;
3550 if (list_empty(&mddev->disks))
3551 goto out;
3552
3553 if (mddev->pers) {
3554 err = -EBUSY;
3555 if (!mddev->ro)
3556 goto out;
3557
3558 mddev->safemode = 0;
3559 mddev->ro = 0;
3560 set_disk_ro(disk, 0);
3561
3562 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3563 mdname(mddev));
3564 /*
3565 * Kick recovery or resync if necessary
3566 */
3567 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3568 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003569 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003571 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
3574out:
3575 return err;
3576}
3577
NeilBrownacc55e22006-06-26 00:27:47 -07003578/* similar to deny_write_access, but accounts for our holding a reference
3579 * to the file ourselves */
3580static int deny_bitmap_write_access(struct file * file)
3581{
3582 struct inode *inode = file->f_mapping->host;
3583
3584 spin_lock(&inode->i_lock);
3585 if (atomic_read(&inode->i_writecount) > 1) {
3586 spin_unlock(&inode->i_lock);
3587 return -ETXTBSY;
3588 }
3589 atomic_set(&inode->i_writecount, -1);
3590 spin_unlock(&inode->i_lock);
3591
3592 return 0;
3593}
3594
3595static void restore_bitmap_write_access(struct file *file)
3596{
3597 struct inode *inode = file->f_mapping->host;
3598
3599 spin_lock(&inode->i_lock);
3600 atomic_set(&inode->i_writecount, 1);
3601 spin_unlock(&inode->i_lock);
3602}
3603
NeilBrown9e653b62006-06-26 00:27:58 -07003604/* mode:
3605 * 0 - completely stop and dis-assemble array
3606 * 1 - switch to readonly
3607 * 2 - stop but do not disassemble array
3608 */
3609static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 int err = 0;
3612 struct gendisk *disk = mddev->gendisk;
3613
3614 if (mddev->pers) {
3615 if (atomic_read(&mddev->active)>2) {
3616 printk("md: %s still in use.\n",mdname(mddev));
3617 return -EBUSY;
3618 }
3619
3620 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003621 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3623 md_unregister_thread(mddev->sync_thread);
3624 mddev->sync_thread = NULL;
3625 }
3626
3627 del_timer_sync(&mddev->safemode_timer);
3628
3629 invalidate_partition(disk, 0);
3630
NeilBrown9e653b62006-06-26 00:27:58 -07003631 switch(mode) {
3632 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003634 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 goto out;
3636 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003637 break;
3638 case 0: /* disassemble */
3639 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003640 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003641 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 if (mddev->ro)
3643 set_disk_ro(disk, 0);
3644 blk_queue_make_request(mddev->queue, md_fail_request);
3645 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003646 mddev->queue->merge_bvec_fn = NULL;
3647 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003648 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003649 if (mddev->pers->sync_request)
3650 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 module_put(mddev->pers->owner);
3653 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003654
3655 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003656 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003657
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 if (mddev->ro)
3659 mddev->ro = 0;
3660 }
NeilBrown850b2b42006-10-03 01:15:46 -07003661 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 /* mark array as shutdown cleanly */
3663 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003664 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 }
NeilBrown9e653b62006-06-26 00:27:58 -07003666 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003668 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 }
NeilBrown32a76272005-06-21 17:17:14 -07003670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 /*
3672 * Free resources if final stop
3673 */
NeilBrown9e653b62006-06-26 00:27:58 -07003674 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003675 mdk_rdev_t *rdev;
3676 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3679
NeilBrown978f9462006-02-02 14:28:05 -08003680 bitmap_destroy(mddev);
3681 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003682 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003683 fput(mddev->bitmap_file);
3684 mddev->bitmap_file = NULL;
3685 }
3686 mddev->bitmap_offset = 0;
3687
NeilBrownd089c6a2008-02-06 01:39:59 -08003688 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003689 if (rdev->raid_disk >= 0) {
3690 char nm[20];
3691 sprintf(nm, "rd%d", rdev->raid_disk);
3692 sysfs_remove_link(&mddev->kobj, nm);
3693 }
3694
NeilBrown177a99b2008-02-06 01:39:56 -08003695 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003696 flush_scheduled_work();
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 export_array(mddev);
3699
3700 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003701 mddev->size = 0;
3702 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003703 mddev->recovery_cp = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003704 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003705 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003706 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003707 mddev->persistent = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003708
NeilBrowna8a55c32006-06-26 00:27:39 -07003709 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3711 mdname(mddev));
3712 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003713 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714out:
3715 return err;
3716}
3717
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003718#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719static void autorun_array(mddev_t *mddev)
3720{
3721 mdk_rdev_t *rdev;
3722 struct list_head *tmp;
3723 int err;
3724
NeilBrowna757e642005-04-16 15:26:42 -07003725 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
3728 printk(KERN_INFO "md: running: ");
3729
NeilBrownd089c6a2008-02-06 01:39:59 -08003730 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 char b[BDEVNAME_SIZE];
3732 printk("<%s>", bdevname(rdev->bdev,b));
3733 }
3734 printk("\n");
3735
3736 err = do_md_run (mddev);
3737 if (err) {
3738 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3739 do_md_stop (mddev, 0);
3740 }
3741}
3742
3743/*
3744 * lets try to run arrays based on all disks that have arrived
3745 * until now. (those are in pending_raid_disks)
3746 *
3747 * the method: pick the first pending disk, collect all disks with
3748 * the same UUID, remove all from the pending list and put them into
3749 * the 'same_array' list. Then order this list based on superblock
3750 * update time (freshest comes first), kick out 'old' disks and
3751 * compare superblocks. If everything's fine then run it.
3752 *
3753 * If "unit" is allocated, then bump its reference count
3754 */
3755static void autorun_devices(int part)
3756{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 struct list_head *tmp;
3758 mdk_rdev_t *rdev0, *rdev;
3759 mddev_t *mddev;
3760 char b[BDEVNAME_SIZE];
3761
3762 printk(KERN_INFO "md: autorun ...\n");
3763 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003764 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003766 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 rdev0 = list_entry(pending_raid_disks.next,
3768 mdk_rdev_t, same_set);
3769
3770 printk(KERN_INFO "md: considering %s ...\n",
3771 bdevname(rdev0->bdev,b));
3772 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003773 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 if (super_90_load(rdev, rdev0, 0) >= 0) {
3775 printk(KERN_INFO "md: adding %s ...\n",
3776 bdevname(rdev->bdev,b));
3777 list_move(&rdev->same_set, &candidates);
3778 }
3779 /*
3780 * now we have a set of devices, with all of them having
3781 * mostly sane superblocks. It's time to allocate the
3782 * mddev.
3783 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003784 if (part) {
3785 dev = MKDEV(mdp_major,
3786 rdev0->preferred_minor << MdpMinorShift);
3787 unit = MINOR(dev) >> MdpMinorShift;
3788 } else {
3789 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3790 unit = MINOR(dev);
3791 }
3792 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3794 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3795 break;
3796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 md_probe(dev, NULL, NULL);
3799 mddev = mddev_find(dev);
3800 if (!mddev) {
3801 printk(KERN_ERR
3802 "md: cannot allocate memory for md drive.\n");
3803 break;
3804 }
3805 if (mddev_lock(mddev))
3806 printk(KERN_WARNING "md: %s locked, cannot run\n",
3807 mdname(mddev));
3808 else if (mddev->raid_disks || mddev->major_version
3809 || !list_empty(&mddev->disks)) {
3810 printk(KERN_WARNING
3811 "md: %s already running, cannot run %s\n",
3812 mdname(mddev), bdevname(rdev0->bdev,b));
3813 mddev_unlock(mddev);
3814 } else {
3815 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08003816 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08003817 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 list_del_init(&rdev->same_set);
3819 if (bind_rdev_to_array(rdev, mddev))
3820 export_rdev(rdev);
3821 }
3822 autorun_array(mddev);
3823 mddev_unlock(mddev);
3824 }
3825 /* on success, candidates will be empty, on error
3826 * it won't...
3827 */
NeilBrown73c34432008-02-06 01:39:59 -08003828 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 export_rdev(rdev);
3830 mddev_put(mddev);
3831 }
3832 printk(KERN_INFO "md: ... autorun DONE.\n");
3833}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003834#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836static int get_version(void __user * arg)
3837{
3838 mdu_version_t ver;
3839
3840 ver.major = MD_MAJOR_VERSION;
3841 ver.minor = MD_MINOR_VERSION;
3842 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3843
3844 if (copy_to_user(arg, &ver, sizeof(ver)))
3845 return -EFAULT;
3846
3847 return 0;
3848}
3849
3850static int get_array_info(mddev_t * mddev, void __user * arg)
3851{
3852 mdu_array_info_t info;
3853 int nr,working,active,failed,spare;
3854 mdk_rdev_t *rdev;
3855 struct list_head *tmp;
3856
3857 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003858 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003860 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 failed++;
3862 else {
3863 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003864 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 active++;
3866 else
3867 spare++;
3868 }
3869 }
3870
3871 info.major_version = mddev->major_version;
3872 info.minor_version = mddev->minor_version;
3873 info.patch_version = MD_PATCHLEVEL_VERSION;
3874 info.ctime = mddev->ctime;
3875 info.level = mddev->level;
3876 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003877 if (info.size != mddev->size) /* overflow */
3878 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 info.nr_disks = nr;
3880 info.raid_disks = mddev->raid_disks;
3881 info.md_minor = mddev->md_minor;
3882 info.not_persistent= !mddev->persistent;
3883
3884 info.utime = mddev->utime;
3885 info.state = 0;
3886 if (mddev->in_sync)
3887 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003888 if (mddev->bitmap && mddev->bitmap_offset)
3889 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 info.active_disks = active;
3891 info.working_disks = working;
3892 info.failed_disks = failed;
3893 info.spare_disks = spare;
3894
3895 info.layout = mddev->layout;
3896 info.chunk_size = mddev->chunk_size;
3897
3898 if (copy_to_user(arg, &info, sizeof(info)))
3899 return -EFAULT;
3900
3901 return 0;
3902}
3903
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003904static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003905{
3906 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3907 char *ptr, *buf = NULL;
3908 int err = -ENOMEM;
3909
NeilBrown2a2275d2007-01-26 00:57:11 -08003910 md_allow_write(mddev);
3911
NeilBrown32a76272005-06-21 17:17:14 -07003912 file = kmalloc(sizeof(*file), GFP_KERNEL);
3913 if (!file)
3914 goto out;
3915
3916 /* bitmap disabled, zero the first byte and copy out */
3917 if (!mddev->bitmap || !mddev->bitmap->file) {
3918 file->pathname[0] = '\0';
3919 goto copy_out;
3920 }
3921
3922 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3923 if (!buf)
3924 goto out;
3925
3926 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3927 if (!ptr)
3928 goto out;
3929
3930 strcpy(file->pathname, ptr);
3931
3932copy_out:
3933 err = 0;
3934 if (copy_to_user(arg, file, sizeof(*file)))
3935 err = -EFAULT;
3936out:
3937 kfree(buf);
3938 kfree(file);
3939 return err;
3940}
3941
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942static int get_disk_info(mddev_t * mddev, void __user * arg)
3943{
3944 mdu_disk_info_t info;
3945 unsigned int nr;
3946 mdk_rdev_t *rdev;
3947
3948 if (copy_from_user(&info, arg, sizeof(info)))
3949 return -EFAULT;
3950
3951 nr = info.number;
3952
3953 rdev = find_rdev_nr(mddev, nr);
3954 if (rdev) {
3955 info.major = MAJOR(rdev->bdev->bd_dev);
3956 info.minor = MINOR(rdev->bdev->bd_dev);
3957 info.raid_disk = rdev->raid_disk;
3958 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003959 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003961 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 info.state |= (1<<MD_DISK_ACTIVE);
3963 info.state |= (1<<MD_DISK_SYNC);
3964 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003965 if (test_bit(WriteMostly, &rdev->flags))
3966 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 } else {
3968 info.major = info.minor = 0;
3969 info.raid_disk = -1;
3970 info.state = (1<<MD_DISK_REMOVED);
3971 }
3972
3973 if (copy_to_user(arg, &info, sizeof(info)))
3974 return -EFAULT;
3975
3976 return 0;
3977}
3978
3979static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3980{
3981 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3982 mdk_rdev_t *rdev;
3983 dev_t dev = MKDEV(info->major,info->minor);
3984
3985 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3986 return -EOVERFLOW;
3987
3988 if (!mddev->raid_disks) {
3989 int err;
3990 /* expecting a device which has a superblock */
3991 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3992 if (IS_ERR(rdev)) {
3993 printk(KERN_WARNING
3994 "md: md_import_device returned %ld\n",
3995 PTR_ERR(rdev));
3996 return PTR_ERR(rdev);
3997 }
3998 if (!list_empty(&mddev->disks)) {
3999 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4000 mdk_rdev_t, same_set);
4001 int err = super_types[mddev->major_version]
4002 .load_super(rdev, rdev0, mddev->minor_version);
4003 if (err < 0) {
4004 printk(KERN_WARNING
4005 "md: %s has different UUID to %s\n",
4006 bdevname(rdev->bdev,b),
4007 bdevname(rdev0->bdev,b2));
4008 export_rdev(rdev);
4009 return -EINVAL;
4010 }
4011 }
4012 err = bind_rdev_to_array(rdev, mddev);
4013 if (err)
4014 export_rdev(rdev);
4015 return err;
4016 }
4017
4018 /*
4019 * add_new_disk can be used once the array is assembled
4020 * to add "hot spares". They must already have a superblock
4021 * written
4022 */
4023 if (mddev->pers) {
4024 int err;
4025 if (!mddev->pers->hot_add_disk) {
4026 printk(KERN_WARNING
4027 "%s: personality does not support diskops!\n",
4028 mdname(mddev));
4029 return -EINVAL;
4030 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004031 if (mddev->persistent)
4032 rdev = md_import_device(dev, mddev->major_version,
4033 mddev->minor_version);
4034 else
4035 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if (IS_ERR(rdev)) {
4037 printk(KERN_WARNING
4038 "md: md_import_device returned %ld\n",
4039 PTR_ERR(rdev));
4040 return PTR_ERR(rdev);
4041 }
NeilBrown41158c72005-06-21 17:17:25 -07004042 /* set save_raid_disk if appropriate */
4043 if (!mddev->persistent) {
4044 if (info->state & (1<<MD_DISK_SYNC) &&
4045 info->raid_disk < mddev->raid_disks)
4046 rdev->raid_disk = info->raid_disk;
4047 else
4048 rdev->raid_disk = -1;
4049 } else
4050 super_types[mddev->major_version].
4051 validate_super(mddev, rdev);
4052 rdev->saved_raid_disk = rdev->raid_disk;
4053
NeilBrownb2d444d2005-11-08 21:39:31 -08004054 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004055 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4056 set_bit(WriteMostly, &rdev->flags);
4057
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 rdev->raid_disk = -1;
4059 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004060 if (!err && !mddev->pers->hot_remove_disk) {
4061 /* If there is hot_add_disk but no hot_remove_disk
4062 * then added disks for geometry changes,
4063 * and should be added immediately.
4064 */
4065 super_types[mddev->major_version].
4066 validate_super(mddev, rdev);
4067 err = mddev->pers->hot_add_disk(mddev, rdev);
4068 if (err)
4069 unbind_rdev_from_array(rdev);
4070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 if (err)
4072 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004073
NeilBrown17571282006-12-10 02:20:52 -08004074 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07004075 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004076 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 return err;
4078 }
4079
4080 /* otherwise, add_new_disk is only allowed
4081 * for major_version==0 superblocks
4082 */
4083 if (mddev->major_version != 0) {
4084 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4085 mdname(mddev));
4086 return -EINVAL;
4087 }
4088
4089 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4090 int err;
4091 rdev = md_import_device (dev, -1, 0);
4092 if (IS_ERR(rdev)) {
4093 printk(KERN_WARNING
4094 "md: error, md_import_device() returned %ld\n",
4095 PTR_ERR(rdev));
4096 return PTR_ERR(rdev);
4097 }
4098 rdev->desc_nr = info->number;
4099 if (info->raid_disk < mddev->raid_disks)
4100 rdev->raid_disk = info->raid_disk;
4101 else
4102 rdev->raid_disk = -1;
4103
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004105 if (info->state & (1<<MD_DISK_SYNC))
4106 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004108 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4109 set_bit(WriteMostly, &rdev->flags);
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 if (!mddev->persistent) {
4112 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4113 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4114 } else
4115 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4116 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4117
NeilBrown2bf071b2006-01-06 00:20:55 -08004118 err = bind_rdev_to_array(rdev, mddev);
4119 if (err) {
4120 export_rdev(rdev);
4121 return err;
4122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 }
4124
4125 return 0;
4126}
4127
4128static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4129{
4130 char b[BDEVNAME_SIZE];
4131 mdk_rdev_t *rdev;
4132
4133 if (!mddev->pers)
4134 return -ENODEV;
4135
4136 rdev = find_rdev(mddev, dev);
4137 if (!rdev)
4138 return -ENXIO;
4139
4140 if (rdev->raid_disk >= 0)
4141 goto busy;
4142
4143 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004144 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004145 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
4147 return 0;
4148busy:
4149 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
4150 bdevname(rdev->bdev,b), mdname(mddev));
4151 return -EBUSY;
4152}
4153
4154static int hot_add_disk(mddev_t * mddev, dev_t dev)
4155{
4156 char b[BDEVNAME_SIZE];
4157 int err;
4158 unsigned int size;
4159 mdk_rdev_t *rdev;
4160
4161 if (!mddev->pers)
4162 return -ENODEV;
4163
4164 if (mddev->major_version != 0) {
4165 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4166 " version-0 superblocks.\n",
4167 mdname(mddev));
4168 return -EINVAL;
4169 }
4170 if (!mddev->pers->hot_add_disk) {
4171 printk(KERN_WARNING
4172 "%s: personality does not support diskops!\n",
4173 mdname(mddev));
4174 return -EINVAL;
4175 }
4176
4177 rdev = md_import_device (dev, -1, 0);
4178 if (IS_ERR(rdev)) {
4179 printk(KERN_WARNING
4180 "md: error, md_import_device() returned %ld\n",
4181 PTR_ERR(rdev));
4182 return -EINVAL;
4183 }
4184
4185 if (mddev->persistent)
4186 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4187 else
4188 rdev->sb_offset =
4189 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4190
4191 size = calc_dev_size(rdev, mddev->chunk_size);
4192 rdev->size = size;
4193
NeilBrownb2d444d2005-11-08 21:39:31 -08004194 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 printk(KERN_WARNING
4196 "md: can not hot-add faulty %s disk to %s!\n",
4197 bdevname(rdev->bdev,b), mdname(mddev));
4198 err = -EINVAL;
4199 goto abort_export;
4200 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004201 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004203 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004204 err = bind_rdev_to_array(rdev, mddev);
4205 if (err)
4206 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
4208 /*
4209 * The rest should better be atomic, we can have disk failures
4210 * noticed in interrupt contexts ...
4211 */
4212
4213 if (rdev->desc_nr == mddev->max_disks) {
4214 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4215 mdname(mddev));
4216 err = -EBUSY;
4217 goto abort_unbind_export;
4218 }
4219
4220 rdev->raid_disk = -1;
4221
NeilBrown850b2b42006-10-03 01:15:46 -07004222 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224 /*
4225 * Kick recovery, maybe this spare has to be added to the
4226 * array immediately.
4227 */
4228 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4229 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004230 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 return 0;
4232
4233abort_unbind_export:
4234 unbind_rdev_from_array(rdev);
4235
4236abort_export:
4237 export_rdev(rdev);
4238 return err;
4239}
4240
NeilBrown32a76272005-06-21 17:17:14 -07004241static int set_bitmap_file(mddev_t *mddev, int fd)
4242{
4243 int err;
4244
NeilBrown36fa3062005-09-09 16:23:45 -07004245 if (mddev->pers) {
4246 if (!mddev->pers->quiesce)
4247 return -EBUSY;
4248 if (mddev->recovery || mddev->sync_thread)
4249 return -EBUSY;
4250 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004251 }
4252
NeilBrown36fa3062005-09-09 16:23:45 -07004253
4254 if (fd >= 0) {
4255 if (mddev->bitmap)
4256 return -EEXIST; /* cannot add when bitmap is present */
4257 mddev->bitmap_file = fget(fd);
4258
4259 if (mddev->bitmap_file == NULL) {
4260 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4261 mdname(mddev));
4262 return -EBADF;
4263 }
4264
4265 err = deny_bitmap_write_access(mddev->bitmap_file);
4266 if (err) {
4267 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4268 mdname(mddev));
4269 fput(mddev->bitmap_file);
4270 mddev->bitmap_file = NULL;
4271 return err;
4272 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004273 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004274 } else if (mddev->bitmap == NULL)
4275 return -ENOENT; /* cannot remove what isn't there */
4276 err = 0;
4277 if (mddev->pers) {
4278 mddev->pers->quiesce(mddev, 1);
4279 if (fd >= 0)
4280 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004281 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004282 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004283 fd = -1; /* make sure to put the file */
4284 }
NeilBrown36fa3062005-09-09 16:23:45 -07004285 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004286 }
4287 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004288 if (mddev->bitmap_file) {
4289 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004290 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004291 }
NeilBrown36fa3062005-09-09 16:23:45 -07004292 mddev->bitmap_file = NULL;
4293 }
4294
NeilBrown32a76272005-06-21 17:17:14 -07004295 return err;
4296}
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298/*
4299 * set_array_info is used two different ways
4300 * The original usage is when creating a new array.
4301 * In this usage, raid_disks is > 0 and it together with
4302 * level, size, not_persistent,layout,chunksize determine the
4303 * shape of the array.
4304 * This will always create an array with a type-0.90.0 superblock.
4305 * The newer usage is when assembling an array.
4306 * In this case raid_disks will be 0, and the major_version field is
4307 * use to determine which style super-blocks are to be found on the devices.
4308 * The minor and patch _version numbers are also kept incase the
4309 * super_block handler wishes to interpret them.
4310 */
4311static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4312{
4313
4314 if (info->raid_disks == 0) {
4315 /* just setting version number for superblock loading */
4316 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004317 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 super_types[info->major_version].name == NULL) {
4319 /* maybe try to auto-load a module? */
4320 printk(KERN_INFO
4321 "md: superblock version %d not known\n",
4322 info->major_version);
4323 return -EINVAL;
4324 }
4325 mddev->major_version = info->major_version;
4326 mddev->minor_version = info->minor_version;
4327 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004328 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 return 0;
4330 }
4331 mddev->major_version = MD_MAJOR_VERSION;
4332 mddev->minor_version = MD_MINOR_VERSION;
4333 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4334 mddev->ctime = get_seconds();
4335
4336 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004337 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 mddev->size = info->size;
4339 mddev->raid_disks = info->raid_disks;
4340 /* don't set md_minor, it is determined by which /dev/md* was
4341 * openned
4342 */
4343 if (info->state & (1<<MD_SB_CLEAN))
4344 mddev->recovery_cp = MaxSector;
4345 else
4346 mddev->recovery_cp = 0;
4347 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004348 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349
4350 mddev->layout = info->layout;
4351 mddev->chunk_size = info->chunk_size;
4352
4353 mddev->max_disks = MD_SB_DISKS;
4354
NeilBrowne6910632008-02-06 01:39:51 -08004355 if (mddev->persistent)
4356 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004357 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
NeilBrownb2a27032005-11-28 13:44:12 -08004359 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4360 mddev->bitmap_offset = 0;
4361
NeilBrownf6705572006-03-27 01:18:11 -08004362 mddev->reshape_position = MaxSector;
4363
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 /*
4365 * Generate a 128 bit UUID
4366 */
4367 get_random_bytes(mddev->uuid, 16);
4368
NeilBrownf6705572006-03-27 01:18:11 -08004369 mddev->new_level = mddev->level;
4370 mddev->new_chunk = mddev->chunk_size;
4371 mddev->new_layout = mddev->layout;
4372 mddev->delta_disks = 0;
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return 0;
4375}
4376
NeilBrowna35b0d62006-01-06 00:20:49 -08004377static int update_size(mddev_t *mddev, unsigned long size)
4378{
4379 mdk_rdev_t * rdev;
4380 int rv;
4381 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004382 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004383
4384 if (mddev->pers->resize == NULL)
4385 return -EINVAL;
4386 /* The "size" is the amount of each device that is used.
4387 * This can only make sense for arrays with redundancy.
4388 * linear and raid0 always use whatever space is available
4389 * We can only consider changing the size if no resync
4390 * or reconstruction is happening, and if the new size
4391 * is acceptable. It must fit before the sb_offset or,
4392 * if that is <data_offset, it must fit before the
4393 * size of each device.
4394 * If size is zero, we find the largest size that fits.
4395 */
4396 if (mddev->sync_thread)
4397 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004398 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004399 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004400 avail = rdev->size * 2;
4401
NeilBrowna35b0d62006-01-06 00:20:49 -08004402 if (fit && (size == 0 || size > avail/2))
4403 size = avail/2;
4404 if (avail < ((sector_t)size << 1))
4405 return -ENOSPC;
4406 }
4407 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4408 if (!rv) {
4409 struct block_device *bdev;
4410
4411 bdev = bdget_disk(mddev->gendisk, 0);
4412 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004413 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004414 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004415 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004416 bdput(bdev);
4417 }
4418 }
4419 return rv;
4420}
4421
NeilBrownda943b992006-01-06 00:20:54 -08004422static int update_raid_disks(mddev_t *mddev, int raid_disks)
4423{
4424 int rv;
4425 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004426 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004427 return -EINVAL;
4428 if (raid_disks <= 0 ||
4429 raid_disks >= mddev->max_disks)
4430 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004431 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004432 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004433 mddev->delta_disks = raid_disks - mddev->raid_disks;
4434
4435 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004436 return rv;
4437}
4438
4439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440/*
4441 * update_array_info is used to change the configuration of an
4442 * on-line array.
4443 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4444 * fields in the info are checked against the array.
4445 * Any differences that cannot be handled will cause an error.
4446 * Normally, only one change can be managed at a time.
4447 */
4448static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4449{
4450 int rv = 0;
4451 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004452 int state = 0;
4453
4454 /* calculate expected state,ignoring low bits */
4455 if (mddev->bitmap && mddev->bitmap_offset)
4456 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 if (mddev->major_version != info->major_version ||
4459 mddev->minor_version != info->minor_version ||
4460/* mddev->patch_version != info->patch_version || */
4461 mddev->ctime != info->ctime ||
4462 mddev->level != info->level ||
4463/* mddev->layout != info->layout || */
4464 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004465 mddev->chunk_size != info->chunk_size ||
4466 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4467 ((state^info->state) & 0xfffffe00)
4468 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return -EINVAL;
4470 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004471 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 if (mddev->raid_disks != info->raid_disks) cnt++;
4473 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004474 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (cnt == 0) return 0;
4476 if (cnt > 1) return -EINVAL;
4477
4478 if (mddev->layout != info->layout) {
4479 /* Change layout
4480 * we don't need to do anything at the md level, the
4481 * personality will take care of it all.
4482 */
4483 if (mddev->pers->reconfig == NULL)
4484 return -EINVAL;
4485 else
4486 return mddev->pers->reconfig(mddev, info->layout, -1);
4487 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004488 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004489 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
NeilBrownda943b992006-01-06 00:20:54 -08004491 if (mddev->raid_disks != info->raid_disks)
4492 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
NeilBrown36fa3062005-09-09 16:23:45 -07004494 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4495 if (mddev->pers->quiesce == NULL)
4496 return -EINVAL;
4497 if (mddev->recovery || mddev->sync_thread)
4498 return -EBUSY;
4499 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4500 /* add the bitmap */
4501 if (mddev->bitmap)
4502 return -EEXIST;
4503 if (mddev->default_bitmap_offset == 0)
4504 return -EINVAL;
4505 mddev->bitmap_offset = mddev->default_bitmap_offset;
4506 mddev->pers->quiesce(mddev, 1);
4507 rv = bitmap_create(mddev);
4508 if (rv)
4509 bitmap_destroy(mddev);
4510 mddev->pers->quiesce(mddev, 0);
4511 } else {
4512 /* remove the bitmap */
4513 if (!mddev->bitmap)
4514 return -ENOENT;
4515 if (mddev->bitmap->file)
4516 return -EINVAL;
4517 mddev->pers->quiesce(mddev, 1);
4518 bitmap_destroy(mddev);
4519 mddev->pers->quiesce(mddev, 0);
4520 mddev->bitmap_offset = 0;
4521 }
4522 }
NeilBrown850b2b42006-10-03 01:15:46 -07004523 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return rv;
4525}
4526
4527static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4528{
4529 mdk_rdev_t *rdev;
4530
4531 if (mddev->pers == NULL)
4532 return -ENODEV;
4533
4534 rdev = find_rdev(mddev, dev);
4535 if (!rdev)
4536 return -ENODEV;
4537
4538 md_error(mddev, rdev);
4539 return 0;
4540}
4541
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004542static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4543{
4544 mddev_t *mddev = bdev->bd_disk->private_data;
4545
4546 geo->heads = 2;
4547 geo->sectors = 4;
4548 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4549 return 0;
4550}
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552static int md_ioctl(struct inode *inode, struct file *file,
4553 unsigned int cmd, unsigned long arg)
4554{
4555 int err = 0;
4556 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 mddev_t *mddev = NULL;
4558
4559 if (!capable(CAP_SYS_ADMIN))
4560 return -EACCES;
4561
4562 /*
4563 * Commands dealing with the RAID driver but not any
4564 * particular array:
4565 */
4566 switch (cmd)
4567 {
4568 case RAID_VERSION:
4569 err = get_version(argp);
4570 goto done;
4571
4572 case PRINT_RAID_DEBUG:
4573 err = 0;
4574 md_print_devices();
4575 goto done;
4576
4577#ifndef MODULE
4578 case RAID_AUTORUN:
4579 err = 0;
4580 autostart_arrays(arg);
4581 goto done;
4582#endif
4583 default:;
4584 }
4585
4586 /*
4587 * Commands creating/starting a new array:
4588 */
4589
4590 mddev = inode->i_bdev->bd_disk->private_data;
4591
4592 if (!mddev) {
4593 BUG();
4594 goto abort;
4595 }
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 err = mddev_lock(mddev);
4598 if (err) {
4599 printk(KERN_INFO
4600 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4601 err, cmd);
4602 goto abort;
4603 }
4604
4605 switch (cmd)
4606 {
4607 case SET_ARRAY_INFO:
4608 {
4609 mdu_array_info_t info;
4610 if (!arg)
4611 memset(&info, 0, sizeof(info));
4612 else if (copy_from_user(&info, argp, sizeof(info))) {
4613 err = -EFAULT;
4614 goto abort_unlock;
4615 }
4616 if (mddev->pers) {
4617 err = update_array_info(mddev, &info);
4618 if (err) {
4619 printk(KERN_WARNING "md: couldn't update"
4620 " array info. %d\n", err);
4621 goto abort_unlock;
4622 }
4623 goto done_unlock;
4624 }
4625 if (!list_empty(&mddev->disks)) {
4626 printk(KERN_WARNING
4627 "md: array %s already has disks!\n",
4628 mdname(mddev));
4629 err = -EBUSY;
4630 goto abort_unlock;
4631 }
4632 if (mddev->raid_disks) {
4633 printk(KERN_WARNING
4634 "md: array %s already initialised!\n",
4635 mdname(mddev));
4636 err = -EBUSY;
4637 goto abort_unlock;
4638 }
4639 err = set_array_info(mddev, &info);
4640 if (err) {
4641 printk(KERN_WARNING "md: couldn't set"
4642 " array info. %d\n", err);
4643 goto abort_unlock;
4644 }
4645 }
4646 goto done_unlock;
4647
4648 default:;
4649 }
4650
4651 /*
4652 * Commands querying/configuring an existing array:
4653 */
NeilBrown32a76272005-06-21 17:17:14 -07004654 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004655 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004656 if ((!mddev->raid_disks && !mddev->external)
4657 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4658 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4659 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 err = -ENODEV;
4661 goto abort_unlock;
4662 }
4663
4664 /*
4665 * Commands even a read-only array can execute:
4666 */
4667 switch (cmd)
4668 {
4669 case GET_ARRAY_INFO:
4670 err = get_array_info(mddev, argp);
4671 goto done_unlock;
4672
NeilBrown32a76272005-06-21 17:17:14 -07004673 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004674 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004675 goto done_unlock;
4676
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 case GET_DISK_INFO:
4678 err = get_disk_info(mddev, argp);
4679 goto done_unlock;
4680
4681 case RESTART_ARRAY_RW:
4682 err = restart_array(mddev);
4683 goto done_unlock;
4684
4685 case STOP_ARRAY:
4686 err = do_md_stop (mddev, 0);
4687 goto done_unlock;
4688
4689 case STOP_ARRAY_RO:
4690 err = do_md_stop (mddev, 1);
4691 goto done_unlock;
4692
4693 /*
4694 * We have a problem here : there is no easy way to give a CHS
4695 * virtual geometry. We currently pretend that we have a 2 heads
4696 * 4 sectors (with a BIG number of cylinders...). This drives
4697 * dosfs just mad... ;-)
4698 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 }
4700
4701 /*
4702 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004703 * superblock, so we do not allow them on read-only arrays.
4704 * However non-MD ioctls (e.g. get-size) will still come through
4705 * here and hit the 'default' below, so only disallow
4706 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 */
NeilBrownf91de922005-11-08 21:39:36 -08004708 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4709 mddev->ro && mddev->pers) {
4710 if (mddev->ro == 2) {
4711 mddev->ro = 0;
4712 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4713 md_wakeup_thread(mddev->thread);
4714
4715 } else {
4716 err = -EROFS;
4717 goto abort_unlock;
4718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 }
4720
4721 switch (cmd)
4722 {
4723 case ADD_NEW_DISK:
4724 {
4725 mdu_disk_info_t info;
4726 if (copy_from_user(&info, argp, sizeof(info)))
4727 err = -EFAULT;
4728 else
4729 err = add_new_disk(mddev, &info);
4730 goto done_unlock;
4731 }
4732
4733 case HOT_REMOVE_DISK:
4734 err = hot_remove_disk(mddev, new_decode_dev(arg));
4735 goto done_unlock;
4736
4737 case HOT_ADD_DISK:
4738 err = hot_add_disk(mddev, new_decode_dev(arg));
4739 goto done_unlock;
4740
4741 case SET_DISK_FAULTY:
4742 err = set_disk_faulty(mddev, new_decode_dev(arg));
4743 goto done_unlock;
4744
4745 case RUN_ARRAY:
4746 err = do_md_run (mddev);
4747 goto done_unlock;
4748
NeilBrown32a76272005-06-21 17:17:14 -07004749 case SET_BITMAP_FILE:
4750 err = set_bitmap_file(mddev, (int)arg);
4751 goto done_unlock;
4752
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 err = -EINVAL;
4755 goto abort_unlock;
4756 }
4757
4758done_unlock:
4759abort_unlock:
4760 mddev_unlock(mddev);
4761
4762 return err;
4763done:
4764 if (err)
4765 MD_BUG();
4766abort:
4767 return err;
4768}
4769
4770static int md_open(struct inode *inode, struct file *file)
4771{
4772 /*
4773 * Succeed if we can lock the mddev, which confirms that
4774 * it isn't being stopped right now.
4775 */
4776 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4777 int err;
4778
NeilBrownd63a5a72006-12-08 02:36:17 -08004779 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 goto out;
4781
4782 err = 0;
4783 mddev_get(mddev);
4784 mddev_unlock(mddev);
4785
4786 check_disk_change(inode->i_bdev);
4787 out:
4788 return err;
4789}
4790
4791static int md_release(struct inode *inode, struct file * file)
4792{
4793 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4794
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004795 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 mddev_put(mddev);
4797
4798 return 0;
4799}
4800
Linus Torvalds44ce62942007-05-09 18:51:36 -07004801static int md_media_changed(struct gendisk *disk)
4802{
4803 mddev_t *mddev = disk->private_data;
4804
4805 return mddev->changed;
4806}
4807
4808static int md_revalidate(struct gendisk *disk)
4809{
4810 mddev_t *mddev = disk->private_data;
4811
4812 mddev->changed = 0;
4813 return 0;
4814}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815static struct block_device_operations md_fops =
4816{
4817 .owner = THIS_MODULE,
4818 .open = md_open,
4819 .release = md_release,
4820 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004821 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004822 .media_changed = md_media_changed,
4823 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824};
4825
Adrian Bunk75c96f82005-05-05 16:16:09 -07004826static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827{
4828 mdk_thread_t *thread = arg;
4829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 /*
4831 * md_thread is a 'system-thread', it's priority should be very
4832 * high. We avoid resource deadlocks individually in each
4833 * raid personality. (RAID5 does preallocation) We also use RR and
4834 * the very same RT priority as kswapd, thus we will never get
4835 * into a priority inversion deadlock.
4836 *
4837 * we definitely have to have equal or higher priority than
4838 * bdflush, otherwise bdflush will deadlock if there are too
4839 * many dirty RAID5 blocks.
4840 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
NeilBrown6985c432005-10-19 21:23:47 -07004842 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004843 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844
NeilBrown93588e22005-11-15 00:09:12 -08004845 /* We need to wait INTERRUPTIBLE so that
4846 * we don't add to the load-average.
4847 * That means we need to be sure no signals are
4848 * pending
4849 */
4850 if (signal_pending(current))
4851 flush_signals(current);
4852
4853 wait_event_interruptible_timeout
4854 (thread->wqueue,
4855 test_bit(THREAD_WAKEUP, &thread->flags)
4856 || kthread_should_stop(),
4857 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
4859 clear_bit(THREAD_WAKEUP, &thread->flags);
4860
NeilBrown787453c2005-11-08 21:39:43 -08004861 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 return 0;
4865}
4866
4867void md_wakeup_thread(mdk_thread_t *thread)
4868{
4869 if (thread) {
4870 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4871 set_bit(THREAD_WAKEUP, &thread->flags);
4872 wake_up(&thread->wqueue);
4873 }
4874}
4875
4876mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4877 const char *name)
4878{
4879 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
NeilBrown9ffae0c2006-01-06 00:20:32 -08004881 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 if (!thread)
4883 return NULL;
4884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885 init_waitqueue_head(&thread->wqueue);
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 thread->run = run;
4888 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004889 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004890 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004891 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 kfree(thread);
4893 return NULL;
4894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 return thread;
4896}
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898void md_unregister_thread(mdk_thread_t *thread)
4899{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004900 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07004901
4902 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 kfree(thread);
4904}
4905
4906void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4907{
4908 if (!mddev) {
4909 MD_BUG();
4910 return;
4911 }
4912
NeilBrownb2d444d2005-11-08 21:39:31 -08004913 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 return;
NeilBrown32a76272005-06-21 17:17:14 -07004915/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4917 mdname(mddev),
4918 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4919 __builtin_return_address(0),__builtin_return_address(1),
4920 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004921*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004922 if (!mddev->pers)
4923 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 if (!mddev->pers->error_handler)
4925 return;
4926 mddev->pers->error_handler(mddev,rdev);
4927 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4928 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4929 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004930 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931}
4932
4933/* seq_file implementation /proc/mdstat */
4934
4935static void status_unused(struct seq_file *seq)
4936{
4937 int i = 0;
4938 mdk_rdev_t *rdev;
4939 struct list_head *tmp;
4940
4941 seq_printf(seq, "unused devices: ");
4942
NeilBrown73c34432008-02-06 01:39:59 -08004943 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 char b[BDEVNAME_SIZE];
4945 i++;
4946 seq_printf(seq, "%s ",
4947 bdevname(rdev->bdev,b));
4948 }
4949 if (!i)
4950 seq_printf(seq, "<none>");
4951
4952 seq_printf(seq, "\n");
4953}
4954
4955
4956static void status_resync(struct seq_file *seq, mddev_t * mddev)
4957{
NeilBrown4588b422006-03-27 01:18:04 -08004958 sector_t max_blocks, resync, res;
4959 unsigned long dt, db, rt;
4960 int scale;
4961 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
4963 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4964
4965 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4966 max_blocks = mddev->resync_max_sectors >> 1;
4967 else
4968 max_blocks = mddev->size;
4969
4970 /*
4971 * Should not happen.
4972 */
4973 if (!max_blocks) {
4974 MD_BUG();
4975 return;
4976 }
NeilBrown4588b422006-03-27 01:18:04 -08004977 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4978 * in a sector_t, and (max_blocks>>scale) will fit in a
4979 * u32, as those are the requirements for sector_div.
4980 * Thus 'scale' must be at least 10
4981 */
4982 scale = 10;
4983 if (sizeof(sector_t) > sizeof(unsigned long)) {
4984 while ( max_blocks/2 > (1ULL<<(scale+32)))
4985 scale++;
4986 }
4987 res = (resync>>scale)*1000;
4988 sector_div(res, (u32)((max_blocks>>scale)+1));
4989
4990 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 {
NeilBrown4588b422006-03-27 01:18:04 -08004992 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 seq_printf(seq, "[");
4994 for (i = 0; i < x; i++)
4995 seq_printf(seq, "=");
4996 seq_printf(seq, ">");
4997 for (i = 0; i < y; i++)
4998 seq_printf(seq, ".");
4999 seq_printf(seq, "] ");
5000 }
NeilBrown4588b422006-03-27 01:18:04 -08005001 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005002 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5003 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005004 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5005 "check" :
5006 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5007 "resync" : "recovery"))),
5008 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005009 (unsigned long long) resync,
5010 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
5012 /*
5013 * We do not want to overflow, so the order of operands and
5014 * the * 100 / 100 trick are important. We do a +1 to be
5015 * safe against division by zero. We only estimate anyway.
5016 *
5017 * dt: time from mark until now
5018 * db: blocks written from mark until now
5019 * rt: remaining time
5020 */
5021 dt = ((jiffies - mddev->resync_mark) / HZ);
5022 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005023 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5024 - mddev->resync_mark_cnt;
5025 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026
5027 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5028
NeilBrownff4e8d92006-07-10 04:44:16 -07005029 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030}
5031
5032static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5033{
5034 struct list_head *tmp;
5035 loff_t l = *pos;
5036 mddev_t *mddev;
5037
5038 if (l >= 0x10000)
5039 return NULL;
5040 if (!l--)
5041 /* header */
5042 return (void*)1;
5043
5044 spin_lock(&all_mddevs_lock);
5045 list_for_each(tmp,&all_mddevs)
5046 if (!l--) {
5047 mddev = list_entry(tmp, mddev_t, all_mddevs);
5048 mddev_get(mddev);
5049 spin_unlock(&all_mddevs_lock);
5050 return mddev;
5051 }
5052 spin_unlock(&all_mddevs_lock);
5053 if (!l--)
5054 return (void*)2;/* tail */
5055 return NULL;
5056}
5057
5058static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5059{
5060 struct list_head *tmp;
5061 mddev_t *next_mddev, *mddev = v;
5062
5063 ++*pos;
5064 if (v == (void*)2)
5065 return NULL;
5066
5067 spin_lock(&all_mddevs_lock);
5068 if (v == (void*)1)
5069 tmp = all_mddevs.next;
5070 else
5071 tmp = mddev->all_mddevs.next;
5072 if (tmp != &all_mddevs)
5073 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5074 else {
5075 next_mddev = (void*)2;
5076 *pos = 0x10000;
5077 }
5078 spin_unlock(&all_mddevs_lock);
5079
5080 if (v != (void*)1)
5081 mddev_put(mddev);
5082 return next_mddev;
5083
5084}
5085
5086static void md_seq_stop(struct seq_file *seq, void *v)
5087{
5088 mddev_t *mddev = v;
5089
5090 if (mddev && v != (void*)1 && v != (void*)2)
5091 mddev_put(mddev);
5092}
5093
NeilBrownd7603b72006-01-06 00:20:30 -08005094struct mdstat_info {
5095 int event;
5096};
5097
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098static int md_seq_show(struct seq_file *seq, void *v)
5099{
5100 mddev_t *mddev = v;
5101 sector_t size;
5102 struct list_head *tmp2;
5103 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005104 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005105 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
5107 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005108 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 seq_printf(seq, "Personalities : ");
5110 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005111 list_for_each_entry(pers, &pers_list, list)
5112 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
5114 spin_unlock(&pers_lock);
5115 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005116 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 return 0;
5118 }
5119 if (v == (void*)2) {
5120 status_unused(seq);
5121 return 0;
5122 }
5123
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005124 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5128 seq_printf(seq, "%s : %sactive", mdname(mddev),
5129 mddev->pers ? "" : "in");
5130 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005131 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005133 if (mddev->ro==2)
5134 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 seq_printf(seq, " %s", mddev->pers->name);
5136 }
5137
5138 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005139 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 char b[BDEVNAME_SIZE];
5141 seq_printf(seq, " %s[%d]",
5142 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005143 if (test_bit(WriteMostly, &rdev->flags))
5144 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005145 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 seq_printf(seq, "(F)");
5147 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005148 } else if (rdev->raid_disk < 0)
5149 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 size += rdev->size;
5151 }
5152
5153 if (!list_empty(&mddev->disks)) {
5154 if (mddev->pers)
5155 seq_printf(seq, "\n %llu blocks",
5156 (unsigned long long)mddev->array_size);
5157 else
5158 seq_printf(seq, "\n %llu blocks",
5159 (unsigned long long)size);
5160 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005161 if (mddev->persistent) {
5162 if (mddev->major_version != 0 ||
5163 mddev->minor_version != 90) {
5164 seq_printf(seq," super %d.%d",
5165 mddev->major_version,
5166 mddev->minor_version);
5167 }
NeilBrowne6910632008-02-06 01:39:51 -08005168 } else if (mddev->external)
5169 seq_printf(seq, " super external:%s",
5170 mddev->metadata_type);
5171 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005172 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
5174 if (mddev->pers) {
5175 mddev->pers->status (seq, mddev);
5176 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005177 if (mddev->pers->sync_request) {
5178 if (mddev->curr_resync > 2) {
5179 status_resync (seq, mddev);
5180 seq_printf(seq, "\n ");
5181 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5182 seq_printf(seq, "\tresync=DELAYED\n ");
5183 else if (mddev->recovery_cp < MaxSector)
5184 seq_printf(seq, "\tresync=PENDING\n ");
5185 }
NeilBrown32a76272005-06-21 17:17:14 -07005186 } else
5187 seq_printf(seq, "\n ");
5188
5189 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005190 unsigned long chunk_kb;
5191 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005192 spin_lock_irqsave(&bitmap->lock, flags);
5193 chunk_kb = bitmap->chunksize >> 10;
5194 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5195 "%lu%s chunk",
5196 bitmap->pages - bitmap->missing_pages,
5197 bitmap->pages,
5198 (bitmap->pages - bitmap->missing_pages)
5199 << (PAGE_SHIFT - 10),
5200 chunk_kb ? chunk_kb : bitmap->chunksize,
5201 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005202 if (bitmap->file) {
5203 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005204 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005205 }
NeilBrown78d742d2005-06-21 17:17:15 -07005206
NeilBrown32a76272005-06-21 17:17:14 -07005207 seq_printf(seq, "\n");
5208 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 }
5210
5211 seq_printf(seq, "\n");
5212 }
5213 mddev_unlock(mddev);
5214
5215 return 0;
5216}
5217
5218static struct seq_operations md_seq_ops = {
5219 .start = md_seq_start,
5220 .next = md_seq_next,
5221 .stop = md_seq_stop,
5222 .show = md_seq_show,
5223};
5224
5225static int md_seq_open(struct inode *inode, struct file *file)
5226{
5227 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005228 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5229 if (mi == NULL)
5230 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
5232 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005233 if (error)
5234 kfree(mi);
5235 else {
5236 struct seq_file *p = file->private_data;
5237 p->private = mi;
5238 mi->event = atomic_read(&md_event_count);
5239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 return error;
5241}
5242
NeilBrownd7603b72006-01-06 00:20:30 -08005243static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5244{
5245 struct seq_file *m = filp->private_data;
5246 struct mdstat_info *mi = m->private;
5247 int mask;
5248
5249 poll_wait(filp, &md_event_waiters, wait);
5250
5251 /* always allow read */
5252 mask = POLLIN | POLLRDNORM;
5253
5254 if (mi->event != atomic_read(&md_event_count))
5255 mask |= POLLERR | POLLPRI;
5256 return mask;
5257}
5258
Arjan van de Venfa027c22007-02-12 00:55:33 -08005259static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005260 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 .open = md_seq_open,
5262 .read = seq_read,
5263 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005264 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005265 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266};
5267
NeilBrown2604b702006-01-06 00:20:36 -08005268int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005271 list_add_tail(&p->list, &pers_list);
5272 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 spin_unlock(&pers_lock);
5274 return 0;
5275}
5276
NeilBrown2604b702006-01-06 00:20:36 -08005277int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278{
NeilBrown2604b702006-01-06 00:20:36 -08005279 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005281 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 spin_unlock(&pers_lock);
5283 return 0;
5284}
5285
5286static int is_mddev_idle(mddev_t *mddev)
5287{
5288 mdk_rdev_t * rdev;
5289 struct list_head *tmp;
5290 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005291 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292
5293 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005294 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005296 curr_events = disk_stat_read(disk, sectors[0]) +
5297 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005299 /* sync IO will cause sync_io to increase before the disk_stats
5300 * as sync_io is counted when a request starts, and
5301 * disk_stats is counted when it completes.
5302 * So resync activity will cause curr_events to be smaller than
5303 * when there was no such activity.
5304 * non-sync IO will cause disk_stat to increase without
5305 * increasing sync_io so curr_events will (eventually)
5306 * be larger than it was before. Once it becomes
5307 * substantially larger, the test below will cause
5308 * the array to appear non-idle, and resync will slow
5309 * down.
5310 * If there is a lot of outstanding resync activity when
5311 * we set last_event to curr_events, then all that activity
5312 * completing might cause the array to appear non-idle
5313 * and resync will be slowed down even though there might
5314 * not have been non-resync activity. This will only
5315 * happen once though. 'last_events' will soon reflect
5316 * the state where there is little or no outstanding
5317 * resync requests, and further resync activity will
5318 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005319 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005321 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 rdev->last_events = curr_events;
5323 idle = 0;
5324 }
5325 }
5326 return idle;
5327}
5328
5329void md_done_sync(mddev_t *mddev, int blocks, int ok)
5330{
5331 /* another "blocks" (512byte) blocks have been synced */
5332 atomic_sub(blocks, &mddev->recovery_active);
5333 wake_up(&mddev->recovery_wait);
5334 if (!ok) {
5335 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5336 md_wakeup_thread(mddev->thread);
5337 // stop recovery, signal do_sync ....
5338 }
5339}
5340
5341
NeilBrown06d91a52005-06-21 17:17:12 -07005342/* md_write_start(mddev, bi)
5343 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005344 * in superblock) before writing, schedule a superblock update
5345 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005346 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005347void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348{
NeilBrown06d91a52005-06-21 17:17:12 -07005349 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005350 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005351
NeilBrownf91de922005-11-08 21:39:36 -08005352 BUG_ON(mddev->ro == 1);
5353 if (mddev->ro == 2) {
5354 /* need to switch to read/write */
5355 mddev->ro = 0;
5356 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5357 md_wakeup_thread(mddev->thread);
5358 }
NeilBrown06d91a52005-06-21 17:17:12 -07005359 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005360 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005361 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005362 if (mddev->in_sync) {
5363 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005364 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005365 md_wakeup_thread(mddev->thread);
5366 }
NeilBrowna9701a32005-11-08 21:39:34 -08005367 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005368 }
NeilBrown850b2b42006-10-03 01:15:46 -07005369 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370}
5371
5372void md_write_end(mddev_t *mddev)
5373{
5374 if (atomic_dec_and_test(&mddev->writes_pending)) {
5375 if (mddev->safemode == 2)
5376 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005377 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5379 }
5380}
5381
NeilBrown2a2275d2007-01-26 00:57:11 -08005382/* md_allow_write(mddev)
5383 * Calling this ensures that the array is marked 'active' so that writes
5384 * may proceed without blocking. It is important to call this before
5385 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5386 * Must be called with mddev_lock held.
5387 */
5388void md_allow_write(mddev_t *mddev)
5389{
5390 if (!mddev->pers)
5391 return;
5392 if (mddev->ro)
5393 return;
5394
5395 spin_lock_irq(&mddev->write_lock);
5396 if (mddev->in_sync) {
5397 mddev->in_sync = 0;
5398 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5399 if (mddev->safemode_delay &&
5400 mddev->safemode == 0)
5401 mddev->safemode = 1;
5402 spin_unlock_irq(&mddev->write_lock);
5403 md_update_sb(mddev, 0);
5404 } else
5405 spin_unlock_irq(&mddev->write_lock);
5406}
5407EXPORT_SYMBOL_GPL(md_allow_write);
5408
Adrian Bunk75c96f82005-05-05 16:16:09 -07005409static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
5411#define SYNC_MARKS 10
5412#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005413void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414{
5415 mddev_t *mddev2;
5416 unsigned int currspeed = 0,
5417 window;
NeilBrown57afd892005-06-21 17:17:13 -07005418 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 unsigned long mark[SYNC_MARKS];
5420 sector_t mark_cnt[SYNC_MARKS];
5421 int last_mark,m;
5422 struct list_head *tmp;
5423 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005424 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005425 struct list_head *rtmp;
5426 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005427 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428
5429 /* just incase thread restarts... */
5430 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5431 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005432 if (mddev->ro) /* never try to sync a read-only array */
5433 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434
NeilBrown61df9d92006-10-03 01:15:57 -07005435 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5436 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5437 desc = "data-check";
5438 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5439 desc = "requested-resync";
5440 else
5441 desc = "resync";
5442 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5443 desc = "reshape";
5444 else
5445 desc = "recovery";
5446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 /* we overload curr_resync somewhat here.
5448 * 0 == not engaged in resync at all
5449 * 2 == checking that there is no conflict with another sync
5450 * 1 == like 2, but have yielded to allow conflicting resync to
5451 * commense
5452 * other == active in resync - this many blocks
5453 *
5454 * Before starting a resync we must have set curr_resync to
5455 * 2, and then checked that every "conflicting" array has curr_resync
5456 * less than ours. When we find one that is the same or higher
5457 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5458 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5459 * This will mean we have to start checking from the beginning again.
5460 *
5461 */
5462
5463 do {
5464 mddev->curr_resync = 2;
5465
5466 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005467 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005468 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 goto skip;
5470 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005471 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 if (mddev2 == mddev)
5473 continue;
5474 if (mddev2->curr_resync &&
5475 match_mddev_units(mddev,mddev2)) {
5476 DEFINE_WAIT(wq);
5477 if (mddev < mddev2 && mddev->curr_resync == 2) {
5478 /* arbitrarily yield */
5479 mddev->curr_resync = 1;
5480 wake_up(&resync_wait);
5481 }
5482 if (mddev > mddev2 && mddev->curr_resync == 1)
5483 /* no need to wait here, we can wait the next
5484 * time 'round when curr_resync == 2
5485 */
5486 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005487 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5488 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005489 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005490 printk(KERN_INFO "md: delaying %s of %s"
5491 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005493 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 mddev_put(mddev2);
5495 schedule();
5496 finish_wait(&resync_wait, &wq);
5497 goto try_again;
5498 }
5499 finish_wait(&resync_wait, &wq);
5500 }
5501 }
5502 } while (mddev->curr_resync < 2);
5503
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005504 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005505 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005507 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 */
5509 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005510 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005511 /* we don't use the checkpoint if there's a bitmap */
5512 if (!mddev->bitmap &&
5513 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5514 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005515 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5516 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005517 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 /* recovery follows the physical size of devices */
5519 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005520 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005521 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005522 if (rdev->raid_disk >= 0 &&
5523 !test_bit(Faulty, &rdev->flags) &&
5524 !test_bit(In_sync, &rdev->flags) &&
5525 rdev->recovery_offset < j)
5526 j = rdev->recovery_offset;
5527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528
NeilBrown61df9d92006-10-03 01:15:57 -07005529 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5530 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5531 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005532 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005533 "(but not more than %d KB/sec) for %s.\n",
5534 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
5536 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005537
NeilBrown57afd892005-06-21 17:17:13 -07005538 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 for (m = 0; m < SYNC_MARKS; m++) {
5540 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005541 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 }
5543 last_mark = 0;
5544 mddev->resync_mark = mark[last_mark];
5545 mddev->resync_mark_cnt = mark_cnt[last_mark];
5546
5547 /*
5548 * Tune reconstruction:
5549 */
5550 window = 32*(PAGE_SIZE/512);
5551 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5552 window/2,(unsigned long long) max_sectors/2);
5553
5554 atomic_set(&mddev->recovery_active, 0);
5555 init_waitqueue_head(&mddev->recovery_wait);
5556 last_check = 0;
5557
5558 if (j>2) {
5559 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005560 "md: resuming %s of %s from checkpoint.\n",
5561 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 mddev->curr_resync = j;
5563 }
5564
5565 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005566 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
NeilBrown57afd892005-06-21 17:17:13 -07005568 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005569 if (j >= mddev->resync_max) {
5570 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5571 wait_event(mddev->recovery_wait,
5572 mddev->resync_max > j
5573 || kthread_should_stop());
5574 }
5575 if (kthread_should_stop())
5576 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005577 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005578 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005579 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5581 goto out;
5582 }
NeilBrown57afd892005-06-21 17:17:13 -07005583
5584 if (!skipped) { /* actual IO requested */
5585 io_sectors += sectors;
5586 atomic_add(sectors, &mddev->recovery_active);
5587 }
5588
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 j += sectors;
5590 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005591 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005592 if (last_check == 0)
5593 /* this is the earliers that rebuilt will be
5594 * visible in /proc/mdstat
5595 */
5596 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005597
5598 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 continue;
5600
NeilBrown57afd892005-06-21 17:17:13 -07005601 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
5603 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5604 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5605 break;
5606
5607 repeat:
5608 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5609 /* step marks */
5610 int next = (last_mark+1) % SYNC_MARKS;
5611
5612 mddev->resync_mark = mark[next];
5613 mddev->resync_mark_cnt = mark_cnt[next];
5614 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005615 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 last_mark = next;
5617 }
5618
5619
NeilBrownc6207272008-02-06 01:39:52 -08005620 if (kthread_should_stop())
5621 goto interrupted;
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623
5624 /*
5625 * this loop exits only if either when we are slower than
5626 * the 'hard' speed limit, or the system was IO-idle for
5627 * a jiffy.
5628 * the system might be non-idle CPU-wise, but we only care
5629 * about not overloading the IO subsystem. (things like an
5630 * e2fsck being done on the RAID array should execute fast)
5631 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005632 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 cond_resched();
5634
NeilBrown57afd892005-06-21 17:17:13 -07005635 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5636 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
NeilBrown88202a02006-01-06 00:21:36 -08005638 if (currspeed > speed_min(mddev)) {
5639 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005641 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 goto repeat;
5643 }
5644 }
5645 }
NeilBrown61df9d92006-10-03 01:15:57 -07005646 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 /*
5648 * this also signals 'finished resyncing' to md_stop
5649 */
5650 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005651 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
5653 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5654
5655 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005656 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657
5658 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005659 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005660 mddev->curr_resync > 2) {
5661 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5662 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5663 if (mddev->curr_resync >= mddev->recovery_cp) {
5664 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005665 "md: checkpointing %s of %s.\n",
5666 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005667 mddev->recovery_cp = mddev->curr_resync;
5668 }
5669 } else
5670 mddev->recovery_cp = MaxSector;
5671 } else {
5672 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5673 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005674 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005675 if (rdev->raid_disk >= 0 &&
5676 !test_bit(Faulty, &rdev->flags) &&
5677 !test_bit(In_sync, &rdev->flags) &&
5678 rdev->recovery_offset < mddev->curr_resync)
5679 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 }
NeilBrown17571282006-12-10 02:20:52 -08005682 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 skip:
5685 mddev->curr_resync = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005686 mddev->resync_max = MaxSector;
5687 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 wake_up(&resync_wait);
5689 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5690 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005691 return;
5692
5693 interrupted:
5694 /*
5695 * got a signal, exit.
5696 */
5697 printk(KERN_INFO
5698 "md: md_do_sync() got signal ... exiting\n");
5699 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5700 goto out;
5701
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702}
NeilBrown29269552006-03-27 01:18:10 -08005703EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704
5705
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005706static int remove_and_add_spares(mddev_t *mddev)
5707{
5708 mdk_rdev_t *rdev;
5709 struct list_head *rtmp;
5710 int spares = 0;
5711
NeilBrownd089c6a2008-02-06 01:39:59 -08005712 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005713 if (rdev->raid_disk >= 0 &&
NeilBrownc303da62008-02-06 01:39:51 -08005714 !mddev->external &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005715 (test_bit(Faulty, &rdev->flags) ||
5716 ! test_bit(In_sync, &rdev->flags)) &&
5717 atomic_read(&rdev->nr_pending)==0) {
5718 if (mddev->pers->hot_remove_disk(
5719 mddev, rdev->raid_disk)==0) {
5720 char nm[20];
5721 sprintf(nm,"rd%d", rdev->raid_disk);
5722 sysfs_remove_link(&mddev->kobj, nm);
5723 rdev->raid_disk = -1;
5724 }
5725 }
5726
5727 if (mddev->degraded) {
NeilBrownd089c6a2008-02-06 01:39:59 -08005728 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005729 if (rdev->raid_disk < 0
5730 && !test_bit(Faulty, &rdev->flags)) {
5731 rdev->recovery_offset = 0;
5732 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5733 char nm[20];
5734 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005735 if (sysfs_create_link(&mddev->kobj,
5736 &rdev->kobj, nm))
5737 printk(KERN_WARNING
5738 "md: cannot register "
5739 "%s for %s\n",
5740 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005741 spares++;
5742 md_new_event(mddev);
5743 } else
5744 break;
5745 }
5746 }
5747 return spares;
5748}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749/*
5750 * This routine is regularly called by all per-raid-array threads to
5751 * deal with generic issues like resync and super-block update.
5752 * Raid personalities that don't have a thread (linear/raid0) do not
5753 * need this as they never do any recovery or update the superblock.
5754 *
5755 * It does not do any resync itself, but rather "forks" off other threads
5756 * to do that as needed.
5757 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5758 * "->recovery" and create a thread at ->sync_thread.
5759 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5760 * and wakeups up this thread which will reap the thread and finish up.
5761 * This thread also removes any faulty devices (with nr_pending == 0).
5762 *
5763 * The overall approach is:
5764 * 1/ if the superblock needs updating, update it.
5765 * 2/ If a recovery thread is running, don't do anything else.
5766 * 3/ If recovery has finished, clean up, possibly marking spares active.
5767 * 4/ If there are any faulty devices, remove them.
5768 * 5/ If array is degraded, try to add spares devices
5769 * 6/ If array has spares or is not in-sync, start a resync thread.
5770 */
5771void md_check_recovery(mddev_t *mddev)
5772{
5773 mdk_rdev_t *rdev;
5774 struct list_head *rtmp;
5775
5776
NeilBrown5f404022005-06-21 17:17:16 -07005777 if (mddev->bitmap)
5778 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779
5780 if (mddev->ro)
5781 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005782
5783 if (signal_pending(current)) {
5784 if (mddev->pers->sync_request) {
5785 printk(KERN_INFO "md: %s in immediate safe mode\n",
5786 mdname(mddev));
5787 mddev->safemode = 2;
5788 }
5789 flush_signals(current);
5790 }
5791
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08005793 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005795 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5796 (mddev->safemode == 1) ||
5797 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5798 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 ))
5800 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005801
NeilBrowndf5b89b2006-03-27 01:18:20 -08005802 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005803 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005804
NeilBrowna9701a32005-11-08 21:39:34 -08005805 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005806 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5807 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5808 mddev->in_sync = 1;
NeilBrowne6910632008-02-06 01:39:51 -08005809 if (mddev->persistent)
5810 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005811 }
5812 if (mddev->safemode == 1)
5813 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005814 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005815
NeilBrown850b2b42006-10-03 01:15:46 -07005816 if (mddev->flags)
5817 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005818
NeilBrown06d91a52005-06-21 17:17:12 -07005819
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5821 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5822 /* resync/recovery still happening */
5823 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5824 goto unlock;
5825 }
5826 if (mddev->sync_thread) {
5827 /* resync has finished, collect result */
5828 md_unregister_thread(mddev->sync_thread);
5829 mddev->sync_thread = NULL;
5830 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5831 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5832 /* success...*/
5833 /* activate any spares */
5834 mddev->pers->spare_active(mddev);
5835 }
NeilBrown850b2b42006-10-03 01:15:46 -07005836 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005837
5838 /* if array is no-longer degraded, then any saved_raid_disk
5839 * information must be scrapped
5840 */
5841 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08005842 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07005843 rdev->saved_raid_disk = -1;
5844
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 mddev->recovery = 0;
5846 /* flag recovery needed just to double check */
5847 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005848 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 goto unlock;
5850 }
NeilBrown24dd4692005-11-08 21:39:26 -08005851 /* Clear some bits that don't mean anything, but
5852 * might be left set
5853 */
5854 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5855 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5856 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5857 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005859 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5860 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 /* no recovery is running.
5862 * remove any failed drives, then
5863 * add spares if possible.
5864 * Spare are also removed and re-added, to allow
5865 * the personality to fail the re-add.
5866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005868 if (mddev->reshape_position != MaxSector) {
5869 if (mddev->pers->check_reshape(mddev) != 0)
5870 /* Cannot proceed */
5871 goto unlock;
5872 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5873 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005874 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5875 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5876 } else if (mddev->recovery_cp < MaxSector) {
5877 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5878 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5879 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005881
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882 if (mddev->pers->sync_request) {
5883 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005884 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5885 /* We are adding a device or devices to an array
5886 * which has the bitmap stored on all devices.
5887 * So make sure all bitmap pages get written
5888 */
5889 bitmap_write_all(mddev->bitmap);
5890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 mddev->sync_thread = md_register_thread(md_do_sync,
5892 mddev,
5893 "%s_resync");
5894 if (!mddev->sync_thread) {
5895 printk(KERN_ERR "%s: could not start resync"
5896 " thread...\n",
5897 mdname(mddev));
5898 /* leave the spares where they are, it shouldn't hurt */
5899 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005900 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005902 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 }
5904 unlock:
5905 mddev_unlock(mddev);
5906 }
5907}
5908
Adrian Bunk75c96f82005-05-05 16:16:09 -07005909static int md_notify_reboot(struct notifier_block *this,
5910 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911{
5912 struct list_head *tmp;
5913 mddev_t *mddev;
5914
5915 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5916
5917 printk(KERN_INFO "md: stopping all md devices.\n");
5918
NeilBrown29ac4aa2008-02-06 01:39:58 -08005919 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005920 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005922 mddev_unlock(mddev);
5923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 /*
5925 * certain more exotic SCSI devices are known to be
5926 * volatile wrt too early system reboots. While the
5927 * right place to handle this issue is the given
5928 * driver, we do want to have a safe RAID driver ...
5929 */
5930 mdelay(1000*1);
5931 }
5932 return NOTIFY_DONE;
5933}
5934
Adrian Bunk75c96f82005-05-05 16:16:09 -07005935static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 .notifier_call = md_notify_reboot,
5937 .next = NULL,
5938 .priority = INT_MAX, /* before any real devices */
5939};
5940
5941static void md_geninit(void)
5942{
5943 struct proc_dir_entry *p;
5944
5945 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5946
5947 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5948 if (p)
5949 p->proc_fops = &md_seq_fops;
5950}
5951
Adrian Bunk75c96f82005-05-05 16:16:09 -07005952static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 if (register_blkdev(MAJOR_NR, "md"))
5955 return -1;
5956 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5957 unregister_blkdev(MAJOR_NR, "md");
5958 return -1;
5959 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005960 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5961 md_probe, NULL, NULL);
5962 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963 md_probe, NULL, NULL);
5964
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005966 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967
5968 md_geninit();
5969 return (0);
5970}
5971
5972
5973#ifndef MODULE
5974
5975/*
5976 * Searches all registered partitions for autorun RAID arrays
5977 * at boot time.
5978 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005979
5980static LIST_HEAD(all_detected_devices);
5981struct detected_devices_node {
5982 struct list_head list;
5983 dev_t dev;
5984};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985
5986void md_autodetect_dev(dev_t dev)
5987{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005988 struct detected_devices_node *node_detected_dev;
5989
5990 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
5991 if (node_detected_dev) {
5992 node_detected_dev->dev = dev;
5993 list_add_tail(&node_detected_dev->list, &all_detected_devices);
5994 } else {
5995 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
5996 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
5997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998}
5999
6000
6001static void autostart_arrays(int part)
6002{
6003 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006004 struct detected_devices_node *node_detected_dev;
6005 dev_t dev;
6006 int i_scanned, i_passed;
6007
6008 i_scanned = 0;
6009 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
6011 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6012
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006013 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6014 i_scanned++;
6015 node_detected_dev = list_entry(all_detected_devices.next,
6016 struct detected_devices_node, list);
6017 list_del(&node_detected_dev->list);
6018 dev = node_detected_dev->dev;
6019 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006020 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 if (IS_ERR(rdev))
6022 continue;
6023
NeilBrownb2d444d2005-11-08 21:39:31 -08006024 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 MD_BUG();
6026 continue;
6027 }
6028 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006029 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006031
6032 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6033 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
6035 autorun_devices(part);
6036}
6037
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006038#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
6040static __exit void md_exit(void)
6041{
6042 mddev_t *mddev;
6043 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006044
NeilBrowne8703fe2006-10-03 01:15:59 -07006045 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6046 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
6048 unregister_blkdev(MAJOR_NR,"md");
6049 unregister_blkdev(mdp_major, "mdp");
6050 unregister_reboot_notifier(&md_notifier);
6051 unregister_sysctl_table(raid_table_header);
6052 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006053 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 struct gendisk *disk = mddev->gendisk;
6055 if (!disk)
6056 continue;
6057 export_array(mddev);
6058 del_gendisk(disk);
6059 put_disk(disk);
6060 mddev->gendisk = NULL;
6061 mddev_put(mddev);
6062 }
6063}
6064
Dan Williams685784a2007-07-09 11:56:42 -07006065subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066module_exit(md_exit)
6067
NeilBrownf91de922005-11-08 21:39:36 -08006068static int get_ro(char *buffer, struct kernel_param *kp)
6069{
6070 return sprintf(buffer, "%d", start_readonly);
6071}
6072static int set_ro(const char *val, struct kernel_param *kp)
6073{
6074 char *e;
6075 int num = simple_strtoul(val, &e, 10);
6076 if (*val && (*e == '\0' || *e == '\n')) {
6077 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006078 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006079 }
6080 return -EINVAL;
6081}
6082
NeilBrown80ca3a42006-07-10 04:44:18 -07006083module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6084module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006085
NeilBrownf91de922005-11-08 21:39:36 -08006086
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087EXPORT_SYMBOL(register_md_personality);
6088EXPORT_SYMBOL(unregister_md_personality);
6089EXPORT_SYMBOL(md_error);
6090EXPORT_SYMBOL(md_done_sync);
6091EXPORT_SYMBOL(md_write_start);
6092EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093EXPORT_SYMBOL(md_register_thread);
6094EXPORT_SYMBOL(md_unregister_thread);
6095EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096EXPORT_SYMBOL(md_check_recovery);
6097MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006098MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006099MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);