blob: 827824a9f3e917de4ae1ce38fa6017efbb49602d [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
NeilBrownd0fae182008-03-04 14:29:31 -08001506 if (test_bit(AutoDetected, &rdev->flags))
1507 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508#endif
1509 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001510 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511}
1512
1513static void kick_rdev_from_array(mdk_rdev_t * rdev)
1514{
1515 unbind_rdev_from_array(rdev);
1516 export_rdev(rdev);
1517}
1518
1519static void export_array(mddev_t *mddev)
1520{
1521 struct list_head *tmp;
1522 mdk_rdev_t *rdev;
1523
NeilBrownd089c6a2008-02-06 01:39:59 -08001524 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 if (!rdev->mddev) {
1526 MD_BUG();
1527 continue;
1528 }
1529 kick_rdev_from_array(rdev);
1530 }
1531 if (!list_empty(&mddev->disks))
1532 MD_BUG();
1533 mddev->raid_disks = 0;
1534 mddev->major_version = 0;
1535}
1536
1537static void print_desc(mdp_disk_t *desc)
1538{
1539 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1540 desc->major,desc->minor,desc->raid_disk,desc->state);
1541}
1542
1543static void print_sb(mdp_super_t *sb)
1544{
1545 int i;
1546
1547 printk(KERN_INFO
1548 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1549 sb->major_version, sb->minor_version, sb->patch_version,
1550 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1551 sb->ctime);
1552 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1553 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1554 sb->md_minor, sb->layout, sb->chunk_size);
1555 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1556 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1557 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1558 sb->failed_disks, sb->spare_disks,
1559 sb->sb_csum, (unsigned long)sb->events_lo);
1560
1561 printk(KERN_INFO);
1562 for (i = 0; i < MD_SB_DISKS; i++) {
1563 mdp_disk_t *desc;
1564
1565 desc = sb->disks + i;
1566 if (desc->number || desc->major || desc->minor ||
1567 desc->raid_disk || (desc->state && (desc->state != 4))) {
1568 printk(" D %2d: ", i);
1569 print_desc(desc);
1570 }
1571 }
1572 printk(KERN_INFO "md: THIS: ");
1573 print_desc(&sb->this_disk);
1574
1575}
1576
1577static void print_rdev(mdk_rdev_t *rdev)
1578{
1579 char b[BDEVNAME_SIZE];
1580 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1581 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001582 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1583 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (rdev->sb_loaded) {
1585 printk(KERN_INFO "md: rdev superblock:\n");
1586 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1587 } else
1588 printk(KERN_INFO "md: no rdev superblock!\n");
1589}
1590
Adrian Bunk5e563412006-06-26 00:27:42 -07001591static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 struct list_head *tmp, *tmp2;
1594 mdk_rdev_t *rdev;
1595 mddev_t *mddev;
1596 char b[BDEVNAME_SIZE];
1597
1598 printk("\n");
1599 printk("md: **********************************\n");
1600 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1601 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001602 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
NeilBrown32a76272005-06-21 17:17:14 -07001604 if (mddev->bitmap)
1605 bitmap_print_sb(mddev->bitmap);
1606 else
1607 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001608 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 printk("<%s>", bdevname(rdev->bdev,b));
1610 printk("\n");
1611
NeilBrownd089c6a2008-02-06 01:39:59 -08001612 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 print_rdev(rdev);
1614 }
1615 printk("md: **********************************\n");
1616 printk("\n");
1617}
1618
1619
NeilBrown42543762006-06-26 00:27:57 -07001620static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
NeilBrown42543762006-06-26 00:27:57 -07001622 /* Update each superblock (in-memory image), but
1623 * if we are allowed to, skip spares which already
1624 * have the right event counter, or have one earlier
1625 * (which would mean they aren't being marked as dirty
1626 * with the rest of the array)
1627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 mdk_rdev_t *rdev;
1629 struct list_head *tmp;
1630
NeilBrownd089c6a2008-02-06 01:39:59 -08001631 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001632 if (rdev->sb_events == mddev->events ||
1633 (nospares &&
1634 rdev->raid_disk < 0 &&
1635 (rdev->sb_events&1)==0 &&
1636 rdev->sb_events+1 == mddev->events)) {
1637 /* Don't update this superblock */
1638 rdev->sb_loaded = 2;
1639 } else {
1640 super_types[mddev->major_version].
1641 sync_super(mddev, rdev);
1642 rdev->sb_loaded = 1;
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645}
1646
NeilBrown850b2b42006-10-03 01:15:46 -07001647static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct list_head *tmp;
1650 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001651 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001652 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001655 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001656
NeilBrown850b2b42006-10-03 01:15:46 -07001657 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1658 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1659 force_change = 1;
1660 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1661 /* just a clean<-> dirty transition, possibly leave spares alone,
1662 * though if events isn't the right even/odd, we will have to do
1663 * spares after all
1664 */
1665 nospares = 1;
1666 if (force_change)
1667 nospares = 0;
1668 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001669 /* If the array is degraded, then skipping spares is both
1670 * dangerous and fairly pointless.
1671 * Dangerous because a device that was removed from the array
1672 * might have a event_count that still looks up-to-date,
1673 * so it can be re-added without a resync.
1674 * Pointless because if there are any spares to skip,
1675 * then a recovery will happen and soon that array won't
1676 * be degraded any more and the spare can go back to sleep then.
1677 */
NeilBrown850b2b42006-10-03 01:15:46 -07001678 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001679
NeilBrown06d91a52005-06-21 17:17:12 -07001680 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001682
1683 /* If this is just a dirty<->clean transition, and the array is clean
1684 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001685 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001686 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001687 && (mddev->events & 1)
1688 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001689 mddev->events--;
1690 else {
1691 /* otherwise we have to go forward and ... */
1692 mddev->events ++;
1693 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1694 /* .. if the array isn't clean, insist on an odd 'events' */
1695 if ((mddev->events&1)==0) {
1696 mddev->events++;
1697 nospares = 0;
1698 }
1699 } else {
1700 /* otherwise insist on an even 'events' (for clean states) */
1701 if ((mddev->events&1)) {
1702 mddev->events++;
1703 nospares = 0;
1704 }
1705 }
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 if (!mddev->events) {
1709 /*
1710 * oops, this 64-bit counter should never wrap.
1711 * Either we are in around ~1 trillion A.C., assuming
1712 * 1 reboot per second, or we have a bug:
1713 */
1714 MD_BUG();
1715 mddev->events --;
1716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 /*
1719 * do not write anything to disk if using
1720 * nonpersistent superblocks
1721 */
NeilBrown06d91a52005-06-21 17:17:12 -07001722 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001723 if (!mddev->external)
1724 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1725
NeilBrowna9701a32005-11-08 21:39:34 -08001726 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001727 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001729 }
NeilBrowne6910632008-02-06 01:39:51 -08001730 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001731 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 dprintk(KERN_INFO
1734 "md: updating %s RAID superblock on device (in sync %d)\n",
1735 mdname(mddev),mddev->in_sync);
1736
NeilBrown4ad13662007-07-17 04:06:13 -07001737 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001738 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 char b[BDEVNAME_SIZE];
1740 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001741 if (rdev->sb_loaded != 1)
1742 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001743 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 dprintk("(skipping faulty ");
1745
1746 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001747 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001748 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001749 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001750 rdev->sb_page);
1751 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1752 bdevname(rdev->bdev,b),
1753 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001754 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 } else
1757 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001758 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 /* only need to write one superblock... */
1760 break;
1761 }
NeilBrowna9701a32005-11-08 21:39:34 -08001762 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001763 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001764
NeilBrowna9701a32005-11-08 21:39:34 -08001765 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001766 if (mddev->in_sync != sync_req ||
1767 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001768 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001769 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001770 goto repeat;
1771 }
NeilBrown850b2b42006-10-03 01:15:46 -07001772 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001773 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001774 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
NeilBrownbce74da2006-01-06 00:20:41 -08001778/* words written to sysfs files may, or my not, be \n terminated.
1779 * We want to accept with case. For this we use cmd_match.
1780 */
1781static int cmd_match(const char *cmd, const char *str)
1782{
1783 /* See if cmd, written into a sysfs file, matches
1784 * str. They must either be the same, or cmd can
1785 * have a trailing newline
1786 */
1787 while (*cmd && *str && *cmd == *str) {
1788 cmd++;
1789 str++;
1790 }
1791 if (*cmd == '\n')
1792 cmd++;
1793 if (*str || *cmd)
1794 return 0;
1795 return 1;
1796}
1797
NeilBrown86e6ffd2005-11-08 21:39:24 -08001798struct rdev_sysfs_entry {
1799 struct attribute attr;
1800 ssize_t (*show)(mdk_rdev_t *, char *);
1801 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1802};
1803
1804static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001805state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001806{
1807 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001808 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001809
NeilBrownb2d444d2005-11-08 21:39:31 -08001810 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001811 len+= sprintf(page+len, "%sfaulty",sep);
1812 sep = ",";
1813 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001814 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001815 len += sprintf(page+len, "%sin_sync",sep);
1816 sep = ",";
1817 }
NeilBrownf6556752006-06-26 00:28:01 -07001818 if (test_bit(WriteMostly, &rdev->flags)) {
1819 len += sprintf(page+len, "%swrite_mostly",sep);
1820 sep = ",";
1821 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001822 if (!test_bit(Faulty, &rdev->flags) &&
1823 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001824 len += sprintf(page+len, "%sspare", sep);
1825 sep = ",";
1826 }
1827 return len+sprintf(page+len, "\n");
1828}
1829
NeilBrown45dc2de2006-06-26 00:27:58 -07001830static ssize_t
1831state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1832{
1833 /* can write
1834 * faulty - simulates and error
1835 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001836 * writemostly - sets write_mostly
1837 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001838 */
1839 int err = -EINVAL;
1840 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1841 md_error(rdev->mddev, rdev);
1842 err = 0;
1843 } else if (cmd_match(buf, "remove")) {
1844 if (rdev->raid_disk >= 0)
1845 err = -EBUSY;
1846 else {
1847 mddev_t *mddev = rdev->mddev;
1848 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001849 if (mddev->pers)
1850 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001851 md_new_event(mddev);
1852 err = 0;
1853 }
NeilBrownf6556752006-06-26 00:28:01 -07001854 } else if (cmd_match(buf, "writemostly")) {
1855 set_bit(WriteMostly, &rdev->flags);
1856 err = 0;
1857 } else if (cmd_match(buf, "-writemostly")) {
1858 clear_bit(WriteMostly, &rdev->flags);
1859 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001860 }
1861 return err ? err : len;
1862}
NeilBrown80ca3a42006-07-10 04:44:18 -07001863static struct rdev_sysfs_entry rdev_state =
1864__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001865
1866static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001867super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001868{
1869 if (rdev->sb_loaded && rdev->sb_size) {
1870 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1871 return rdev->sb_size;
1872 } else
1873 return 0;
1874}
NeilBrown96de1e62005-11-08 21:39:39 -08001875static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1876
NeilBrown4dbcdc72006-01-06 00:20:52 -08001877static ssize_t
1878errors_show(mdk_rdev_t *rdev, char *page)
1879{
1880 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1881}
1882
1883static ssize_t
1884errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1885{
1886 char *e;
1887 unsigned long n = simple_strtoul(buf, &e, 10);
1888 if (*buf && (*e == 0 || *e == '\n')) {
1889 atomic_set(&rdev->corrected_errors, n);
1890 return len;
1891 }
1892 return -EINVAL;
1893}
1894static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001895__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001896
NeilBrown014236d2006-01-06 00:20:55 -08001897static ssize_t
1898slot_show(mdk_rdev_t *rdev, char *page)
1899{
1900 if (rdev->raid_disk < 0)
1901 return sprintf(page, "none\n");
1902 else
1903 return sprintf(page, "%d\n", rdev->raid_disk);
1904}
1905
1906static ssize_t
1907slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1908{
1909 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001910 int err;
1911 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001912 int slot = simple_strtoul(buf, &e, 10);
1913 if (strncmp(buf, "none", 4)==0)
1914 slot = -1;
1915 else if (e==buf || (*e && *e!= '\n'))
1916 return -EINVAL;
NeilBrownc303da62008-02-06 01:39:51 -08001917 if (rdev->mddev->pers) {
1918 /* Setting 'slot' on an active array requires also
1919 * updating the 'rd%d' link, and communicating
1920 * with the personality with ->hot_*_disk.
1921 * For now we only support removing
1922 * failed/spare devices. This normally happens automatically,
1923 * but not when the metadata is externally managed.
1924 */
1925 if (slot != -1)
1926 return -EBUSY;
1927 if (rdev->raid_disk == -1)
1928 return -EEXIST;
1929 /* personality does all needed checks */
1930 if (rdev->mddev->pers->hot_add_disk == NULL)
1931 return -EINVAL;
1932 err = rdev->mddev->pers->
1933 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1934 if (err)
1935 return err;
1936 sprintf(nm, "rd%d", rdev->raid_disk);
1937 sysfs_remove_link(&rdev->mddev->kobj, nm);
1938 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1939 md_wakeup_thread(rdev->mddev->thread);
1940 } else {
1941 if (slot >= rdev->mddev->raid_disks)
1942 return -ENOSPC;
1943 rdev->raid_disk = slot;
1944 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001945 clear_bit(Faulty, &rdev->flags);
1946 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08001947 set_bit(In_sync, &rdev->flags);
1948 }
NeilBrown014236d2006-01-06 00:20:55 -08001949 return len;
1950}
1951
1952
1953static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001954__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001955
NeilBrown93c8cad2006-01-06 00:20:56 -08001956static ssize_t
1957offset_show(mdk_rdev_t *rdev, char *page)
1958{
Andrew Morton6961ece2006-01-06 00:20:59 -08001959 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001960}
1961
1962static ssize_t
1963offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1964{
1965 char *e;
1966 unsigned long long offset = simple_strtoull(buf, &e, 10);
1967 if (e==buf || (*e && *e != '\n'))
1968 return -EINVAL;
1969 if (rdev->mddev->pers)
1970 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001971 if (rdev->size && rdev->mddev->external)
1972 /* Must set offset before size, so overlap checks
1973 * can be sane */
1974 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08001975 rdev->data_offset = offset;
1976 return len;
1977}
1978
1979static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001980__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001981
NeilBrown83303b62006-01-06 00:21:06 -08001982static ssize_t
1983rdev_size_show(mdk_rdev_t *rdev, char *page)
1984{
1985 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1986}
1987
NeilBrownc5d79ad2008-02-06 01:39:54 -08001988static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
1989{
1990 /* check if two start/length pairs overlap */
1991 if (s1+l1 <= s2)
1992 return 0;
1993 if (s2+l2 <= s1)
1994 return 0;
1995 return 1;
1996}
1997
NeilBrown83303b62006-01-06 00:21:06 -08001998static ssize_t
1999rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2000{
2001 char *e;
2002 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002003 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002004 mddev_t *my_mddev = rdev->mddev;
2005
NeilBrown83303b62006-01-06 00:21:06 -08002006 if (e==buf || (*e && *e != '\n'))
2007 return -EINVAL;
NeilBrown27c529b2008-03-04 14:29:33 -08002008 if (my_mddev->pers)
NeilBrown83303b62006-01-06 00:21:06 -08002009 return -EBUSY;
2010 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002011 if (size > oldsize && rdev->mddev->external) {
2012 /* need to check that all other rdevs with the same ->bdev
2013 * do not overlap. We need to unlock the mddev to avoid
2014 * a deadlock. We have already changed rdev->size, and if
2015 * we have to change it back, we will have the lock again.
2016 */
2017 mddev_t *mddev;
2018 int overlap = 0;
2019 struct list_head *tmp, *tmp2;
2020
NeilBrown27c529b2008-03-04 14:29:33 -08002021 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002022 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002023 mdk_rdev_t *rdev2;
2024
2025 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002026 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002027 if (test_bit(AllReserved, &rdev2->flags) ||
2028 (rdev->bdev == rdev2->bdev &&
2029 rdev != rdev2 &&
2030 overlaps(rdev->data_offset, rdev->size,
2031 rdev2->data_offset, rdev2->size))) {
2032 overlap = 1;
2033 break;
2034 }
2035 mddev_unlock(mddev);
2036 if (overlap) {
2037 mddev_put(mddev);
2038 break;
2039 }
2040 }
NeilBrown27c529b2008-03-04 14:29:33 -08002041 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002042 if (overlap) {
2043 /* Someone else could have slipped in a size
2044 * change here, but doing so is just silly.
2045 * We put oldsize back because we *know* it is
2046 * safe, and trust userspace not to race with
2047 * itself
2048 */
2049 rdev->size = oldsize;
2050 return -EBUSY;
2051 }
2052 }
NeilBrown27c529b2008-03-04 14:29:33 -08002053 if (size < my_mddev->size || my_mddev->size == 0)
2054 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002055 return len;
2056}
2057
2058static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002059__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002060
NeilBrown86e6ffd2005-11-08 21:39:24 -08002061static struct attribute *rdev_default_attrs[] = {
2062 &rdev_state.attr,
2063 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002064 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002065 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002066 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002067 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002068 NULL,
2069};
2070static ssize_t
2071rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2072{
2073 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2074 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002075 mddev_t *mddev = rdev->mddev;
2076 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002077
2078 if (!entry->show)
2079 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002080
2081 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2082 if (!rv) {
2083 if (rdev->mddev == NULL)
2084 rv = -EBUSY;
2085 else
2086 rv = entry->show(rdev, page);
2087 mddev_unlock(mddev);
2088 }
2089 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002090}
2091
2092static ssize_t
2093rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2094 const char *page, size_t length)
2095{
2096 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2097 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002098 ssize_t rv;
2099 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002100
2101 if (!entry->store)
2102 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002103 if (!capable(CAP_SYS_ADMIN))
2104 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002105 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002106 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002107 if (rdev->mddev == NULL)
2108 rv = -EBUSY;
2109 else
2110 rv = entry->store(rdev, page, length);
NeilBrownca388052008-02-06 01:39:55 -08002111 mddev_unlock(rdev->mddev);
2112 }
2113 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002114}
2115
2116static void rdev_free(struct kobject *ko)
2117{
2118 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2119 kfree(rdev);
2120}
2121static struct sysfs_ops rdev_sysfs_ops = {
2122 .show = rdev_attr_show,
2123 .store = rdev_attr_store,
2124};
2125static struct kobj_type rdev_ktype = {
2126 .release = rdev_free,
2127 .sysfs_ops = &rdev_sysfs_ops,
2128 .default_attrs = rdev_default_attrs,
2129};
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131/*
2132 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2133 *
2134 * mark the device faulty if:
2135 *
2136 * - the device is nonexistent (zero size)
2137 * - the device has no valid superblock
2138 *
2139 * a faulty rdev _never_ has rdev->sb set.
2140 */
2141static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2142{
2143 char b[BDEVNAME_SIZE];
2144 int err;
2145 mdk_rdev_t *rdev;
2146 sector_t size;
2147
NeilBrown9ffae0c2006-01-06 00:20:32 -08002148 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (!rdev) {
2150 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2151 return ERR_PTR(-ENOMEM);
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 if ((err = alloc_disk_sb(rdev)))
2155 goto abort_free;
2156
NeilBrownc5d79ad2008-02-06 01:39:54 -08002157 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (err)
2159 goto abort_free;
2160
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002161 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002164 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002165 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002166 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002168 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002170 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002171 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2174 if (!size) {
2175 printk(KERN_WARNING
2176 "md: %s has zero or unknown size, marking faulty!\n",
2177 bdevname(rdev->bdev,b));
2178 err = -EINVAL;
2179 goto abort_free;
2180 }
2181
2182 if (super_format >= 0) {
2183 err = super_types[super_format].
2184 load_super(rdev, NULL, super_minor);
2185 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002186 printk(KERN_WARNING
2187 "md: %s does not have a valid v%d.%d "
2188 "superblock, not importing!\n",
2189 bdevname(rdev->bdev,b),
2190 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 goto abort_free;
2192 }
2193 if (err < 0) {
2194 printk(KERN_WARNING
2195 "md: could not read %s's sb, not importing!\n",
2196 bdevname(rdev->bdev,b));
2197 goto abort_free;
2198 }
2199 }
2200 INIT_LIST_HEAD(&rdev->same_set);
2201
2202 return rdev;
2203
2204abort_free:
2205 if (rdev->sb_page) {
2206 if (rdev->bdev)
2207 unlock_rdev(rdev);
2208 free_disk_sb(rdev);
2209 }
2210 kfree(rdev);
2211 return ERR_PTR(err);
2212}
2213
2214/*
2215 * Check a full RAID array for plausibility
2216 */
2217
2218
NeilBrowna757e642005-04-16 15:26:42 -07002219static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
2221 int i;
2222 struct list_head *tmp;
2223 mdk_rdev_t *rdev, *freshest;
2224 char b[BDEVNAME_SIZE];
2225
2226 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002227 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 switch (super_types[mddev->major_version].
2229 load_super(rdev, freshest, mddev->minor_version)) {
2230 case 1:
2231 freshest = rdev;
2232 break;
2233 case 0:
2234 break;
2235 default:
2236 printk( KERN_ERR \
2237 "md: fatal superblock inconsistency in %s"
2238 " -- removing from array\n",
2239 bdevname(rdev->bdev,b));
2240 kick_rdev_from_array(rdev);
2241 }
2242
2243
2244 super_types[mddev->major_version].
2245 validate_super(mddev, freshest);
2246
2247 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002248 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (rdev != freshest)
2250 if (super_types[mddev->major_version].
2251 validate_super(mddev, rdev)) {
2252 printk(KERN_WARNING "md: kicking non-fresh %s"
2253 " from array!\n",
2254 bdevname(rdev->bdev,b));
2255 kick_rdev_from_array(rdev);
2256 continue;
2257 }
2258 if (mddev->level == LEVEL_MULTIPATH) {
2259 rdev->desc_nr = i++;
2260 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002261 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002262 } else if (rdev->raid_disk >= mddev->raid_disks) {
2263 rdev->raid_disk = -1;
2264 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
2266 }
2267
2268
2269
2270 if (mddev->recovery_cp != MaxSector &&
2271 mddev->level >= 1)
2272 printk(KERN_ERR "md: %s: raid array is not clean"
2273 " -- starting background reconstruction\n",
2274 mdname(mddev));
2275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
NeilBrowneae17012005-11-08 21:39:23 -08002278static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002279safe_delay_show(mddev_t *mddev, char *page)
2280{
2281 int msec = (mddev->safemode_delay*1000)/HZ;
2282 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2283}
2284static ssize_t
2285safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2286{
2287 int scale=1;
2288 int dot=0;
2289 int i;
2290 unsigned long msec;
2291 char buf[30];
2292 char *e;
2293 /* remove a period, and count digits after it */
2294 if (len >= sizeof(buf))
2295 return -EINVAL;
2296 strlcpy(buf, cbuf, len);
2297 buf[len] = 0;
2298 for (i=0; i<len; i++) {
2299 if (dot) {
2300 if (isdigit(buf[i])) {
2301 buf[i-1] = buf[i];
2302 scale *= 10;
2303 }
2304 buf[i] = 0;
2305 } else if (buf[i] == '.') {
2306 dot=1;
2307 buf[i] = 0;
2308 }
2309 }
2310 msec = simple_strtoul(buf, &e, 10);
2311 if (e == buf || (*e && *e != '\n'))
2312 return -EINVAL;
2313 msec = (msec * 1000) / scale;
2314 if (msec == 0)
2315 mddev->safemode_delay = 0;
2316 else {
2317 mddev->safemode_delay = (msec*HZ)/1000;
2318 if (mddev->safemode_delay == 0)
2319 mddev->safemode_delay = 1;
2320 }
2321 return len;
2322}
2323static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002324__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002325
2326static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002327level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002328{
NeilBrown2604b702006-01-06 00:20:36 -08002329 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002330 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002331 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002332 else if (mddev->clevel[0])
2333 return sprintf(page, "%s\n", mddev->clevel);
2334 else if (mddev->level != LEVEL_NONE)
2335 return sprintf(page, "%d\n", mddev->level);
2336 else
2337 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002338}
2339
NeilBrownd9d166c2006-01-06 00:20:51 -08002340static ssize_t
2341level_store(mddev_t *mddev, const char *buf, size_t len)
2342{
NeilBrown20a49ff2008-02-06 01:39:57 -08002343 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002344 if (mddev->pers)
2345 return -EBUSY;
2346 if (len == 0)
2347 return 0;
2348 if (len >= sizeof(mddev->clevel))
2349 return -ENOSPC;
2350 strncpy(mddev->clevel, buf, len);
2351 if (mddev->clevel[len-1] == '\n')
2352 len--;
2353 mddev->clevel[len] = 0;
2354 mddev->level = LEVEL_NONE;
2355 return rv;
2356}
2357
2358static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002359__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002360
NeilBrownd4dbd022006-06-26 00:27:59 -07002361
2362static ssize_t
2363layout_show(mddev_t *mddev, char *page)
2364{
2365 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002366 if (mddev->reshape_position != MaxSector &&
2367 mddev->layout != mddev->new_layout)
2368 return sprintf(page, "%d (%d)\n",
2369 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002370 return sprintf(page, "%d\n", mddev->layout);
2371}
2372
2373static ssize_t
2374layout_store(mddev_t *mddev, const char *buf, size_t len)
2375{
2376 char *e;
2377 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002378
2379 if (!*buf || (*e && *e != '\n'))
2380 return -EINVAL;
2381
NeilBrown08a02ec2007-05-09 02:35:38 -07002382 if (mddev->pers)
2383 return -EBUSY;
2384 if (mddev->reshape_position != MaxSector)
2385 mddev->new_layout = n;
2386 else
2387 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002388 return len;
2389}
2390static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002391__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002392
2393
NeilBrowneae17012005-11-08 21:39:23 -08002394static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002395raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002396{
NeilBrownbb636542005-11-08 21:39:45 -08002397 if (mddev->raid_disks == 0)
2398 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002399 if (mddev->reshape_position != MaxSector &&
2400 mddev->delta_disks != 0)
2401 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2402 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002403 return sprintf(page, "%d\n", mddev->raid_disks);
2404}
2405
NeilBrownda943b992006-01-06 00:20:54 -08002406static int update_raid_disks(mddev_t *mddev, int raid_disks);
2407
2408static ssize_t
2409raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2410{
NeilBrownda943b992006-01-06 00:20:54 -08002411 char *e;
2412 int rv = 0;
2413 unsigned long n = simple_strtoul(buf, &e, 10);
2414
2415 if (!*buf || (*e && *e != '\n'))
2416 return -EINVAL;
2417
2418 if (mddev->pers)
2419 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002420 else if (mddev->reshape_position != MaxSector) {
2421 int olddisks = mddev->raid_disks - mddev->delta_disks;
2422 mddev->delta_disks = n - olddisks;
2423 mddev->raid_disks = n;
2424 } else
NeilBrownda943b992006-01-06 00:20:54 -08002425 mddev->raid_disks = n;
2426 return rv ? rv : len;
2427}
2428static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002429__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002430
NeilBrown24dd4692005-11-08 21:39:26 -08002431static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002432chunk_size_show(mddev_t *mddev, char *page)
2433{
NeilBrown08a02ec2007-05-09 02:35:38 -07002434 if (mddev->reshape_position != MaxSector &&
2435 mddev->chunk_size != mddev->new_chunk)
2436 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2437 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002438 return sprintf(page, "%d\n", mddev->chunk_size);
2439}
2440
2441static ssize_t
2442chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2443{
2444 /* can only set chunk_size if array is not yet active */
2445 char *e;
2446 unsigned long n = simple_strtoul(buf, &e, 10);
2447
NeilBrown3b343802006-01-06 00:20:47 -08002448 if (!*buf || (*e && *e != '\n'))
2449 return -EINVAL;
2450
NeilBrown08a02ec2007-05-09 02:35:38 -07002451 if (mddev->pers)
2452 return -EBUSY;
2453 else if (mddev->reshape_position != MaxSector)
2454 mddev->new_chunk = n;
2455 else
2456 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002457 return len;
2458}
2459static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002460__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002461
NeilBrowna94213b2006-06-26 00:28:00 -07002462static ssize_t
2463resync_start_show(mddev_t *mddev, char *page)
2464{
2465 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2466}
2467
2468static ssize_t
2469resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2470{
2471 /* can only set chunk_size if array is not yet active */
2472 char *e;
2473 unsigned long long n = simple_strtoull(buf, &e, 10);
2474
2475 if (mddev->pers)
2476 return -EBUSY;
2477 if (!*buf || (*e && *e != '\n'))
2478 return -EINVAL;
2479
2480 mddev->recovery_cp = n;
2481 return len;
2482}
2483static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002484__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002485
NeilBrown9e653b62006-06-26 00:27:58 -07002486/*
2487 * The array state can be:
2488 *
2489 * clear
2490 * No devices, no size, no level
2491 * Equivalent to STOP_ARRAY ioctl
2492 * inactive
2493 * May have some settings, but array is not active
2494 * all IO results in error
2495 * When written, doesn't tear down array, but just stops it
2496 * suspended (not supported yet)
2497 * All IO requests will block. The array can be reconfigured.
2498 * Writing this, if accepted, will block until array is quiessent
2499 * readonly
2500 * no resync can happen. no superblocks get written.
2501 * write requests fail
2502 * read-auto
2503 * like readonly, but behaves like 'clean' on a write request.
2504 *
2505 * clean - no pending writes, but otherwise active.
2506 * When written to inactive array, starts without resync
2507 * If a write request arrives then
2508 * if metadata is known, mark 'dirty' and switch to 'active'.
2509 * if not known, block and switch to write-pending
2510 * If written to an active array that has pending writes, then fails.
2511 * active
2512 * fully active: IO and resync can be happening.
2513 * When written to inactive array, starts with resync
2514 *
2515 * write-pending
2516 * clean, but writes are blocked waiting for 'active' to be written.
2517 *
2518 * active-idle
2519 * like active, but no writes have been seen for a while (100msec).
2520 *
2521 */
2522enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2523 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002524static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002525 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2526 "write-pending", "active-idle", NULL };
2527
2528static int match_word(const char *word, char **list)
2529{
2530 int n;
2531 for (n=0; list[n]; n++)
2532 if (cmd_match(word, list[n]))
2533 break;
2534 return n;
2535}
2536
2537static ssize_t
2538array_state_show(mddev_t *mddev, char *page)
2539{
2540 enum array_state st = inactive;
2541
2542 if (mddev->pers)
2543 switch(mddev->ro) {
2544 case 1:
2545 st = readonly;
2546 break;
2547 case 2:
2548 st = read_auto;
2549 break;
2550 case 0:
2551 if (mddev->in_sync)
2552 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002553 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2554 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002555 else if (mddev->safemode)
2556 st = active_idle;
2557 else
2558 st = active;
2559 }
2560 else {
2561 if (list_empty(&mddev->disks) &&
2562 mddev->raid_disks == 0 &&
2563 mddev->size == 0)
2564 st = clear;
2565 else
2566 st = inactive;
2567 }
2568 return sprintf(page, "%s\n", array_states[st]);
2569}
2570
2571static int do_md_stop(mddev_t * mddev, int ro);
2572static int do_md_run(mddev_t * mddev);
2573static int restart_array(mddev_t *mddev);
2574
2575static ssize_t
2576array_state_store(mddev_t *mddev, const char *buf, size_t len)
2577{
2578 int err = -EINVAL;
2579 enum array_state st = match_word(buf, array_states);
2580 switch(st) {
2581 case bad_word:
2582 break;
2583 case clear:
2584 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002585 if (atomic_read(&mddev->active) > 1)
2586 return -EBUSY;
2587 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002588 break;
2589 case inactive:
2590 /* stopping an active array */
2591 if (mddev->pers) {
2592 if (atomic_read(&mddev->active) > 1)
2593 return -EBUSY;
2594 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002595 } else
2596 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002597 break;
2598 case suspended:
2599 break; /* not supported yet */
2600 case readonly:
2601 if (mddev->pers)
2602 err = do_md_stop(mddev, 1);
2603 else {
2604 mddev->ro = 1;
2605 err = do_md_run(mddev);
2606 }
2607 break;
2608 case read_auto:
2609 /* stopping an active array */
2610 if (mddev->pers) {
2611 err = do_md_stop(mddev, 1);
2612 if (err == 0)
2613 mddev->ro = 2; /* FIXME mark devices writable */
2614 } else {
2615 mddev->ro = 2;
2616 err = do_md_run(mddev);
2617 }
2618 break;
2619 case clean:
2620 if (mddev->pers) {
2621 restart_array(mddev);
2622 spin_lock_irq(&mddev->write_lock);
2623 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002624 if (mddev->in_sync == 0) {
2625 mddev->in_sync = 1;
2626 if (mddev->persistent)
2627 set_bit(MD_CHANGE_CLEAN,
2628 &mddev->flags);
2629 }
2630 err = 0;
2631 } else
2632 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002633 spin_unlock_irq(&mddev->write_lock);
2634 } else {
2635 mddev->ro = 0;
2636 mddev->recovery_cp = MaxSector;
2637 err = do_md_run(mddev);
2638 }
2639 break;
2640 case active:
2641 if (mddev->pers) {
2642 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002643 if (mddev->external)
2644 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002645 wake_up(&mddev->sb_wait);
2646 err = 0;
2647 } else {
2648 mddev->ro = 0;
2649 err = do_md_run(mddev);
2650 }
2651 break;
2652 case write_pending:
2653 case active_idle:
2654 /* these cannot be set */
2655 break;
2656 }
2657 if (err)
2658 return err;
2659 else
2660 return len;
2661}
NeilBrown80ca3a42006-07-10 04:44:18 -07002662static struct md_sysfs_entry md_array_state =
2663__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002664
NeilBrown6d7ff732006-01-06 00:21:16 -08002665static ssize_t
2666null_show(mddev_t *mddev, char *page)
2667{
2668 return -EINVAL;
2669}
2670
2671static ssize_t
2672new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2673{
2674 /* buf must be %d:%d\n? giving major and minor numbers */
2675 /* The new device is added to the array.
2676 * If the array has a persistent superblock, we read the
2677 * superblock to initialise info and check validity.
2678 * Otherwise, only checking done is that in bind_rdev_to_array,
2679 * which mainly checks size.
2680 */
2681 char *e;
2682 int major = simple_strtoul(buf, &e, 10);
2683 int minor;
2684 dev_t dev;
2685 mdk_rdev_t *rdev;
2686 int err;
2687
2688 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2689 return -EINVAL;
2690 minor = simple_strtoul(e+1, &e, 10);
2691 if (*e && *e != '\n')
2692 return -EINVAL;
2693 dev = MKDEV(major, minor);
2694 if (major != MAJOR(dev) ||
2695 minor != MINOR(dev))
2696 return -EOVERFLOW;
2697
2698
2699 if (mddev->persistent) {
2700 rdev = md_import_device(dev, mddev->major_version,
2701 mddev->minor_version);
2702 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2703 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2704 mdk_rdev_t, same_set);
2705 err = super_types[mddev->major_version]
2706 .load_super(rdev, rdev0, mddev->minor_version);
2707 if (err < 0)
2708 goto out;
2709 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002710 } else if (mddev->external)
2711 rdev = md_import_device(dev, -2, -1);
2712 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002713 rdev = md_import_device(dev, -1, -1);
2714
2715 if (IS_ERR(rdev))
2716 return PTR_ERR(rdev);
2717 err = bind_rdev_to_array(rdev, mddev);
2718 out:
2719 if (err)
2720 export_rdev(rdev);
2721 return err ? err : len;
2722}
2723
2724static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002725__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002726
2727static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002728bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2729{
2730 char *end;
2731 unsigned long chunk, end_chunk;
2732
2733 if (!mddev->bitmap)
2734 goto out;
2735 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2736 while (*buf) {
2737 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2738 if (buf == end) break;
2739 if (*end == '-') { /* range */
2740 buf = end + 1;
2741 end_chunk = simple_strtoul(buf, &end, 0);
2742 if (buf == end) break;
2743 }
2744 if (*end && !isspace(*end)) break;
2745 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2746 buf = end;
2747 while (isspace(*buf)) buf++;
2748 }
2749 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2750out:
2751 return len;
2752}
2753
2754static struct md_sysfs_entry md_bitmap =
2755__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2756
2757static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002758size_show(mddev_t *mddev, char *page)
2759{
2760 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2761}
2762
2763static int update_size(mddev_t *mddev, unsigned long size);
2764
2765static ssize_t
2766size_store(mddev_t *mddev, const char *buf, size_t len)
2767{
2768 /* If array is inactive, we can reduce the component size, but
2769 * not increase it (except from 0).
2770 * If array is active, we can try an on-line resize
2771 */
2772 char *e;
2773 int err = 0;
2774 unsigned long long size = simple_strtoull(buf, &e, 10);
2775 if (!*buf || *buf == '\n' ||
2776 (*e && *e != '\n'))
2777 return -EINVAL;
2778
2779 if (mddev->pers) {
2780 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002781 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002782 } else {
2783 if (mddev->size == 0 ||
2784 mddev->size > size)
2785 mddev->size = size;
2786 else
2787 err = -ENOSPC;
2788 }
2789 return err ? err : len;
2790}
2791
2792static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002793__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002794
NeilBrown8bb93aa2006-01-06 00:20:50 -08002795
2796/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002797 * This is one of
2798 * 'none' for arrays with no metadata (good luck...)
2799 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002800 * or N.M for internally known formats
2801 */
2802static ssize_t
2803metadata_show(mddev_t *mddev, char *page)
2804{
2805 if (mddev->persistent)
2806 return sprintf(page, "%d.%d\n",
2807 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002808 else if (mddev->external)
2809 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002810 else
2811 return sprintf(page, "none\n");
2812}
2813
2814static ssize_t
2815metadata_store(mddev_t *mddev, const char *buf, size_t len)
2816{
2817 int major, minor;
2818 char *e;
2819 if (!list_empty(&mddev->disks))
2820 return -EBUSY;
2821
2822 if (cmd_match(buf, "none")) {
2823 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002824 mddev->external = 0;
2825 mddev->major_version = 0;
2826 mddev->minor_version = 90;
2827 return len;
2828 }
2829 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002830 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002831 if (namelen >= sizeof(mddev->metadata_type))
2832 namelen = sizeof(mddev->metadata_type)-1;
2833 strncpy(mddev->metadata_type, buf+9, namelen);
2834 mddev->metadata_type[namelen] = 0;
2835 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2836 mddev->metadata_type[--namelen] = 0;
2837 mddev->persistent = 0;
2838 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002839 mddev->major_version = 0;
2840 mddev->minor_version = 90;
2841 return len;
2842 }
2843 major = simple_strtoul(buf, &e, 10);
2844 if (e==buf || *e != '.')
2845 return -EINVAL;
2846 buf = e+1;
2847 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002848 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002849 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002850 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002851 return -ENOENT;
2852 mddev->major_version = major;
2853 mddev->minor_version = minor;
2854 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002855 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002856 return len;
2857}
2858
2859static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002860__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002861
NeilBrowna35b0d62006-01-06 00:20:49 -08002862static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002863action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002864{
NeilBrown7eec3142005-11-08 21:39:44 -08002865 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002866 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002867 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002868 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2869 type = "reshape";
2870 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002871 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2872 type = "resync";
2873 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2874 type = "check";
2875 else
2876 type = "repair";
2877 } else
2878 type = "recover";
2879 }
2880 return sprintf(page, "%s\n", type);
2881}
2882
2883static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002884action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002885{
NeilBrown7eec3142005-11-08 21:39:44 -08002886 if (!mddev->pers || !mddev->pers->sync_request)
2887 return -EINVAL;
2888
NeilBrownbce74da2006-01-06 00:20:41 -08002889 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002890 if (mddev->sync_thread) {
2891 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2892 md_unregister_thread(mddev->sync_thread);
2893 mddev->sync_thread = NULL;
2894 mddev->recovery = 0;
2895 }
NeilBrown03c902e2006-01-06 00:20:46 -08002896 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2897 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002898 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002899 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002900 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002901 else if (cmd_match(page, "reshape")) {
2902 int err;
2903 if (mddev->pers->start_reshape == NULL)
2904 return -EINVAL;
2905 err = mddev->pers->start_reshape(mddev);
2906 if (err)
2907 return err;
2908 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002909 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002910 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002911 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002912 return -EINVAL;
2913 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2914 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002915 }
NeilBrown03c902e2006-01-06 00:20:46 -08002916 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002917 md_wakeup_thread(mddev->thread);
2918 return len;
2919}
2920
NeilBrown9d888832005-11-08 21:39:26 -08002921static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002922mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002923{
2924 return sprintf(page, "%llu\n",
2925 (unsigned long long) mddev->resync_mismatches);
2926}
2927
NeilBrown80ca3a42006-07-10 04:44:18 -07002928static struct md_sysfs_entry md_scan_mode =
2929__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002930
NeilBrown96de1e62005-11-08 21:39:39 -08002931
NeilBrown80ca3a42006-07-10 04:44:18 -07002932static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002933
NeilBrown88202a02006-01-06 00:21:36 -08002934static ssize_t
2935sync_min_show(mddev_t *mddev, char *page)
2936{
2937 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2938 mddev->sync_speed_min ? "local": "system");
2939}
2940
2941static ssize_t
2942sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2943{
2944 int min;
2945 char *e;
2946 if (strncmp(buf, "system", 6)==0) {
2947 mddev->sync_speed_min = 0;
2948 return len;
2949 }
2950 min = simple_strtoul(buf, &e, 10);
2951 if (buf == e || (*e && *e != '\n') || min <= 0)
2952 return -EINVAL;
2953 mddev->sync_speed_min = min;
2954 return len;
2955}
2956
2957static struct md_sysfs_entry md_sync_min =
2958__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2959
2960static ssize_t
2961sync_max_show(mddev_t *mddev, char *page)
2962{
2963 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2964 mddev->sync_speed_max ? "local": "system");
2965}
2966
2967static ssize_t
2968sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2969{
2970 int max;
2971 char *e;
2972 if (strncmp(buf, "system", 6)==0) {
2973 mddev->sync_speed_max = 0;
2974 return len;
2975 }
2976 max = simple_strtoul(buf, &e, 10);
2977 if (buf == e || (*e && *e != '\n') || max <= 0)
2978 return -EINVAL;
2979 mddev->sync_speed_max = max;
2980 return len;
2981}
2982
2983static struct md_sysfs_entry md_sync_max =
2984__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2985
Iustin Popd7f3d292007-10-16 23:30:54 -07002986static ssize_t
2987degraded_show(mddev_t *mddev, char *page)
2988{
2989 return sprintf(page, "%d\n", mddev->degraded);
2990}
2991static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08002992
2993static ssize_t
2994sync_speed_show(mddev_t *mddev, char *page)
2995{
2996 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002997 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002998 dt = ((jiffies - mddev->resync_mark) / HZ);
2999 if (!dt) dt++;
3000 db = resync - (mddev->resync_mark_cnt);
3001 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
3002}
3003
NeilBrown80ca3a42006-07-10 04:44:18 -07003004static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003005
3006static ssize_t
3007sync_completed_show(mddev_t *mddev, char *page)
3008{
3009 unsigned long max_blocks, resync;
3010
3011 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3012 max_blocks = mddev->resync_max_sectors;
3013 else
3014 max_blocks = mddev->size << 1;
3015
3016 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3017 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3018}
3019
NeilBrown80ca3a42006-07-10 04:44:18 -07003020static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003021
NeilBrowne464eaf2006-03-27 01:18:14 -08003022static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003023max_sync_show(mddev_t *mddev, char *page)
3024{
3025 if (mddev->resync_max == MaxSector)
3026 return sprintf(page, "max\n");
3027 else
3028 return sprintf(page, "%llu\n",
3029 (unsigned long long)mddev->resync_max);
3030}
3031static ssize_t
3032max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3033{
3034 if (strncmp(buf, "max", 3) == 0)
3035 mddev->resync_max = MaxSector;
3036 else {
3037 char *ep;
3038 unsigned long long max = simple_strtoull(buf, &ep, 10);
3039 if (ep == buf || (*ep != 0 && *ep != '\n'))
3040 return -EINVAL;
3041 if (max < mddev->resync_max &&
3042 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3043 return -EBUSY;
3044
3045 /* Must be a multiple of chunk_size */
3046 if (mddev->chunk_size) {
3047 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3048 return -EINVAL;
3049 }
3050 mddev->resync_max = max;
3051 }
3052 wake_up(&mddev->recovery_wait);
3053 return len;
3054}
3055
3056static struct md_sysfs_entry md_max_sync =
3057__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3058
3059static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003060suspend_lo_show(mddev_t *mddev, char *page)
3061{
3062 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3063}
3064
3065static ssize_t
3066suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3067{
3068 char *e;
3069 unsigned long long new = simple_strtoull(buf, &e, 10);
3070
3071 if (mddev->pers->quiesce == NULL)
3072 return -EINVAL;
3073 if (buf == e || (*e && *e != '\n'))
3074 return -EINVAL;
3075 if (new >= mddev->suspend_hi ||
3076 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3077 mddev->suspend_lo = new;
3078 mddev->pers->quiesce(mddev, 2);
3079 return len;
3080 } else
3081 return -EINVAL;
3082}
3083static struct md_sysfs_entry md_suspend_lo =
3084__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3085
3086
3087static ssize_t
3088suspend_hi_show(mddev_t *mddev, char *page)
3089{
3090 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3091}
3092
3093static ssize_t
3094suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3095{
3096 char *e;
3097 unsigned long long new = simple_strtoull(buf, &e, 10);
3098
3099 if (mddev->pers->quiesce == NULL)
3100 return -EINVAL;
3101 if (buf == e || (*e && *e != '\n'))
3102 return -EINVAL;
3103 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3104 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3105 mddev->suspend_hi = new;
3106 mddev->pers->quiesce(mddev, 1);
3107 mddev->pers->quiesce(mddev, 0);
3108 return len;
3109 } else
3110 return -EINVAL;
3111}
3112static struct md_sysfs_entry md_suspend_hi =
3113__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3114
NeilBrown08a02ec2007-05-09 02:35:38 -07003115static ssize_t
3116reshape_position_show(mddev_t *mddev, char *page)
3117{
3118 if (mddev->reshape_position != MaxSector)
3119 return sprintf(page, "%llu\n",
3120 (unsigned long long)mddev->reshape_position);
3121 strcpy(page, "none\n");
3122 return 5;
3123}
3124
3125static ssize_t
3126reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3127{
3128 char *e;
3129 unsigned long long new = simple_strtoull(buf, &e, 10);
3130 if (mddev->pers)
3131 return -EBUSY;
3132 if (buf == e || (*e && *e != '\n'))
3133 return -EINVAL;
3134 mddev->reshape_position = new;
3135 mddev->delta_disks = 0;
3136 mddev->new_level = mddev->level;
3137 mddev->new_layout = mddev->layout;
3138 mddev->new_chunk = mddev->chunk_size;
3139 return len;
3140}
3141
3142static struct md_sysfs_entry md_reshape_position =
3143__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3144 reshape_position_store);
3145
NeilBrowne464eaf2006-03-27 01:18:14 -08003146
NeilBrowneae17012005-11-08 21:39:23 -08003147static struct attribute *md_default_attrs[] = {
3148 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003149 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003150 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003151 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003152 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003153 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003154 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003155 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003156 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003157 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003158 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003159 NULL,
3160};
3161
3162static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003163 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003164 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003165 &md_sync_min.attr,
3166 &md_sync_max.attr,
3167 &md_sync_speed.attr,
3168 &md_sync_completed.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003169 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003170 &md_suspend_lo.attr,
3171 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003172 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003173 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003174 NULL,
3175};
NeilBrown411036f2005-11-08 21:39:40 -08003176static struct attribute_group md_redundancy_group = {
3177 .name = NULL,
3178 .attrs = md_redundancy_attrs,
3179};
3180
NeilBrowneae17012005-11-08 21:39:23 -08003181
3182static ssize_t
3183md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
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->show)
3190 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003191 rv = mddev_lock(mddev);
3192 if (!rv) {
3193 rv = entry->show(mddev, page);
3194 mddev_unlock(mddev);
3195 }
NeilBrown96de1e62005-11-08 21:39:39 -08003196 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003197}
3198
3199static ssize_t
3200md_attr_store(struct kobject *kobj, struct attribute *attr,
3201 const char *page, size_t length)
3202{
3203 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3204 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003205 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003206
3207 if (!entry->store)
3208 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003209 if (!capable(CAP_SYS_ADMIN))
3210 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003211 rv = mddev_lock(mddev);
3212 if (!rv) {
3213 rv = entry->store(mddev, page, length);
3214 mddev_unlock(mddev);
3215 }
NeilBrown96de1e62005-11-08 21:39:39 -08003216 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003217}
3218
3219static void md_free(struct kobject *ko)
3220{
3221 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3222 kfree(mddev);
3223}
3224
3225static struct sysfs_ops md_sysfs_ops = {
3226 .show = md_attr_show,
3227 .store = md_attr_store,
3228};
3229static struct kobj_type md_ktype = {
3230 .release = md_free,
3231 .sysfs_ops = &md_sysfs_ops,
3232 .default_attrs = md_default_attrs,
3233};
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235int mdp_major = 0;
3236
3237static struct kobject *md_probe(dev_t dev, int *part, void *data)
3238{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003239 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 mddev_t *mddev = mddev_find(dev);
3241 struct gendisk *disk;
3242 int partitioned = (MAJOR(dev) != MD_MAJOR);
3243 int shift = partitioned ? MdpMinorShift : 0;
3244 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003245 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246
3247 if (!mddev)
3248 return NULL;
3249
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003250 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003252 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 mddev_put(mddev);
3254 return NULL;
3255 }
3256 disk = alloc_disk(1 << shift);
3257 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003258 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 mddev_put(mddev);
3260 return NULL;
3261 }
3262 disk->major = MAJOR(dev);
3263 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003264 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003266 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 disk->fops = &md_fops;
3269 disk->private_data = mddev;
3270 disk->queue = mddev->queue;
3271 add_disk(disk);
3272 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003273 mutex_unlock(&disks_mutex);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003274 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003275 "%s", "md");
3276 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003277 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3278 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003279 else
3280 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return NULL;
3282}
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284static void md_safemode_timeout(unsigned long data)
3285{
3286 mddev_t *mddev = (mddev_t *) data;
3287
3288 mddev->safemode = 1;
3289 md_wakeup_thread(mddev->thread);
3290}
3291
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003292static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293
3294static int do_md_run(mddev_t * mddev)
3295{
NeilBrown2604b702006-01-06 00:20:36 -08003296 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 int chunk_size;
3298 struct list_head *tmp;
3299 mdk_rdev_t *rdev;
3300 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003301 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 char b[BDEVNAME_SIZE];
3303
NeilBrowna757e642005-04-16 15:26:42 -07003304 if (list_empty(&mddev->disks))
3305 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
3308 if (mddev->pers)
3309 return -EBUSY;
3310
3311 /*
3312 * Analyze all RAID superblock(s)
3313 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003314 if (!mddev->raid_disks) {
3315 if (!mddev->persistent)
3316 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003317 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
3320 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321
NeilBrown2604b702006-01-06 00:20:36 -08003322 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 if (chunk_size > MAX_CHUNK_SIZE) {
3324 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3325 chunk_size, MAX_CHUNK_SIZE);
3326 return -EINVAL;
3327 }
3328 /*
3329 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3330 */
3331 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003332 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 return -EINVAL;
3334 }
3335 if (chunk_size < PAGE_SIZE) {
3336 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3337 chunk_size, PAGE_SIZE);
3338 return -EINVAL;
3339 }
3340
3341 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003342 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003343 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 continue;
3345 if (rdev->size < chunk_size / 1024) {
3346 printk(KERN_WARNING
3347 "md: Dev %s smaller than chunk_size:"
3348 " %lluk < %dk\n",
3349 bdevname(rdev->bdev,b),
3350 (unsigned long long)rdev->size,
3351 chunk_size / 1024);
3352 return -EINVAL;
3353 }
3354 }
3355 }
3356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003358 if (mddev->level != LEVEL_NONE)
3359 request_module("md-level-%d", mddev->level);
3360 else if (mddev->clevel[0])
3361 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362#endif
3363
3364 /*
3365 * Drop all container device buffers, from now on
3366 * the only valid external interface is through the md
3367 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003369 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003370 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 continue;
3372 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003373 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003374
3375 /* perform some consistency tests on the device.
3376 * We don't want the data to overlap the metadata,
3377 * Internal Bitmap issues has handled elsewhere.
3378 */
3379 if (rdev->data_offset < rdev->sb_offset) {
3380 if (mddev->size &&
3381 rdev->data_offset + mddev->size*2
3382 > rdev->sb_offset*2) {
3383 printk("md: %s: data overlaps metadata\n",
3384 mdname(mddev));
3385 return -EINVAL;
3386 }
3387 } else {
3388 if (rdev->sb_offset*2 + rdev->sb_size/512
3389 > rdev->data_offset) {
3390 printk("md: %s: metadata overlaps data\n",
3391 mdname(mddev));
3392 return -EINVAL;
3393 }
3394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396
3397 md_probe(mddev->unit, NULL, NULL);
3398 disk = mddev->gendisk;
3399 if (!disk)
3400 return -ENOMEM;
3401
3402 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003403 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003404 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003406 if (mddev->level != LEVEL_NONE)
3407 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3408 mddev->level);
3409 else
3410 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3411 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 return -EINVAL;
3413 }
NeilBrown2604b702006-01-06 00:20:36 -08003414 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003416 mddev->level = pers->level;
3417 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418
NeilBrownf6705572006-03-27 01:18:11 -08003419 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003420 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003421 /* This personality cannot handle reshaping... */
3422 mddev->pers = NULL;
3423 module_put(pers->owner);
3424 return -EINVAL;
3425 }
3426
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003427 if (pers->sync_request) {
3428 /* Warn if this is a potentially silly
3429 * configuration.
3430 */
3431 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3432 mdk_rdev_t *rdev2;
3433 struct list_head *tmp2;
3434 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003435 rdev_for_each(rdev, tmp, mddev) {
3436 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003437 if (rdev < rdev2 &&
3438 rdev->bdev->bd_contains ==
3439 rdev2->bdev->bd_contains) {
3440 printk(KERN_WARNING
3441 "%s: WARNING: %s appears to be"
3442 " on the same physical disk as"
3443 " %s.\n",
3444 mdname(mddev),
3445 bdevname(rdev->bdev,b),
3446 bdevname(rdev2->bdev,b2));
3447 warned = 1;
3448 }
3449 }
3450 }
3451 if (warned)
3452 printk(KERN_WARNING
3453 "True protection against single-disk"
3454 " failure might be compromised.\n");
3455 }
3456
NeilBrown657390d2005-08-26 18:34:16 -07003457 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003459 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003460 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
NeilBrownf91de922005-11-08 21:39:36 -08003462 if (start_readonly)
3463 mddev->ro = 2; /* read-only, but switch on first write */
3464
NeilBrownb15c2e52006-01-06 00:20:16 -08003465 err = mddev->pers->run(mddev);
3466 if (!err && mddev->pers->sync_request) {
3467 err = bitmap_create(mddev);
3468 if (err) {
3469 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3470 mdname(mddev), err);
3471 mddev->pers->stop(mddev);
3472 }
3473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 if (err) {
3475 printk(KERN_ERR "md: pers->run() failed ...\n");
3476 module_put(mddev->pers->owner);
3477 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003478 bitmap_destroy(mddev);
3479 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003481 if (mddev->pers->sync_request) {
3482 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3483 printk(KERN_WARNING
3484 "md: cannot register extra attributes for %s\n",
3485 mdname(mddev));
3486 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003487 mddev->ro = 0;
3488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 atomic_set(&mddev->writes_pending,0);
3490 mddev->safemode = 0;
3491 mddev->safemode_timer.function = md_safemode_timeout;
3492 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003493 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003495
NeilBrownd089c6a2008-02-06 01:39:59 -08003496 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003497 if (rdev->raid_disk >= 0) {
3498 char nm[20];
3499 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003500 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3501 printk("md: cannot register %s for %s\n",
3502 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3506
NeilBrown850b2b42006-10-03 01:15:46 -07003507 if (mddev->flags)
3508 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 set_capacity(disk, mddev->array_size<<1);
3511
3512 /* If we call blk_queue_make_request here, it will
3513 * re-initialise max_sectors etc which may have been
3514 * refined inside -> run. So just set the bits we need to set.
3515 * Most initialisation happended when we called
3516 * blk_queue_make_request(..., md_fail_request)
3517 * earlier.
3518 */
3519 mddev->queue->queuedata = mddev;
3520 mddev->queue->make_request_fn = mddev->pers->make_request;
3521
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003522 /* If there is a partially-recovered drive we need to
3523 * start recovery here. If we leave it to md_check_recovery,
3524 * it will remove the drives and not do the right thing
3525 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003526 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003527 struct list_head *rtmp;
3528 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003529 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003530 if (rdev->raid_disk >= 0 &&
3531 !test_bit(In_sync, &rdev->flags) &&
3532 !test_bit(Faulty, &rdev->flags))
3533 /* complete an interrupted recovery */
3534 spares++;
3535 if (spares && mddev->pers->sync_request) {
3536 mddev->recovery = 0;
3537 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3538 mddev->sync_thread = md_register_thread(md_do_sync,
3539 mddev,
3540 "%s_resync");
3541 if (!mddev->sync_thread) {
3542 printk(KERN_ERR "%s: could not start resync"
3543 " thread...\n",
3544 mdname(mddev));
3545 /* leave the spares where they are, it shouldn't hurt */
3546 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003547 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003548 }
3549 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003550 md_wakeup_thread(mddev->thread);
3551 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003552
Linus Torvalds44ce62942007-05-09 18:51:36 -07003553 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003554 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003555 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return 0;
3557}
3558
3559static int restart_array(mddev_t *mddev)
3560{
3561 struct gendisk *disk = mddev->gendisk;
3562 int err;
3563
3564 /*
3565 * Complain if it has no devices
3566 */
3567 err = -ENXIO;
3568 if (list_empty(&mddev->disks))
3569 goto out;
3570
3571 if (mddev->pers) {
3572 err = -EBUSY;
3573 if (!mddev->ro)
3574 goto out;
3575
3576 mddev->safemode = 0;
3577 mddev->ro = 0;
3578 set_disk_ro(disk, 0);
3579
3580 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3581 mdname(mddev));
3582 /*
3583 * Kick recovery or resync if necessary
3584 */
3585 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3586 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003587 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003589 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592out:
3593 return err;
3594}
3595
NeilBrownacc55e22006-06-26 00:27:47 -07003596/* similar to deny_write_access, but accounts for our holding a reference
3597 * to the file ourselves */
3598static int deny_bitmap_write_access(struct file * file)
3599{
3600 struct inode *inode = file->f_mapping->host;
3601
3602 spin_lock(&inode->i_lock);
3603 if (atomic_read(&inode->i_writecount) > 1) {
3604 spin_unlock(&inode->i_lock);
3605 return -ETXTBSY;
3606 }
3607 atomic_set(&inode->i_writecount, -1);
3608 spin_unlock(&inode->i_lock);
3609
3610 return 0;
3611}
3612
3613static void restore_bitmap_write_access(struct file *file)
3614{
3615 struct inode *inode = file->f_mapping->host;
3616
3617 spin_lock(&inode->i_lock);
3618 atomic_set(&inode->i_writecount, 1);
3619 spin_unlock(&inode->i_lock);
3620}
3621
NeilBrown9e653b62006-06-26 00:27:58 -07003622/* mode:
3623 * 0 - completely stop and dis-assemble array
3624 * 1 - switch to readonly
3625 * 2 - stop but do not disassemble array
3626 */
3627static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
3629 int err = 0;
3630 struct gendisk *disk = mddev->gendisk;
3631
3632 if (mddev->pers) {
3633 if (atomic_read(&mddev->active)>2) {
3634 printk("md: %s still in use.\n",mdname(mddev));
3635 return -EBUSY;
3636 }
3637
3638 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003639 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3641 md_unregister_thread(mddev->sync_thread);
3642 mddev->sync_thread = NULL;
3643 }
3644
3645 del_timer_sync(&mddev->safemode_timer);
3646
3647 invalidate_partition(disk, 0);
3648
NeilBrown9e653b62006-06-26 00:27:58 -07003649 switch(mode) {
3650 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003652 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 goto out;
3654 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003655 break;
3656 case 0: /* disassemble */
3657 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003658 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003659 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 if (mddev->ro)
3661 set_disk_ro(disk, 0);
3662 blk_queue_make_request(mddev->queue, md_fail_request);
3663 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003664 mddev->queue->merge_bvec_fn = NULL;
3665 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003666 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003667 if (mddev->pers->sync_request)
3668 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 module_put(mddev->pers->owner);
3671 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003672
3673 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003674 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003675
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 if (mddev->ro)
3677 mddev->ro = 0;
3678 }
NeilBrown850b2b42006-10-03 01:15:46 -07003679 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 /* mark array as shutdown cleanly */
3681 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003682 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 }
NeilBrown9e653b62006-06-26 00:27:58 -07003684 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003686 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 }
NeilBrown32a76272005-06-21 17:17:14 -07003688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 /*
3690 * Free resources if final stop
3691 */
NeilBrown9e653b62006-06-26 00:27:58 -07003692 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003693 mdk_rdev_t *rdev;
3694 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3697
NeilBrown978f9462006-02-02 14:28:05 -08003698 bitmap_destroy(mddev);
3699 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003700 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003701 fput(mddev->bitmap_file);
3702 mddev->bitmap_file = NULL;
3703 }
3704 mddev->bitmap_offset = 0;
3705
NeilBrownd089c6a2008-02-06 01:39:59 -08003706 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003707 if (rdev->raid_disk >= 0) {
3708 char nm[20];
3709 sprintf(nm, "rd%d", rdev->raid_disk);
3710 sysfs_remove_link(&mddev->kobj, nm);
3711 }
3712
NeilBrown177a99b2008-02-06 01:39:56 -08003713 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003714 flush_scheduled_work();
3715
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 export_array(mddev);
3717
3718 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003719 mddev->size = 0;
3720 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003721 mddev->recovery_cp = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003722 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003723 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003724 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003725 mddev->persistent = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003726
NeilBrowna8a55c32006-06-26 00:27:39 -07003727 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3729 mdname(mddev));
3730 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003731 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732out:
3733 return err;
3734}
3735
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003736#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737static void autorun_array(mddev_t *mddev)
3738{
3739 mdk_rdev_t *rdev;
3740 struct list_head *tmp;
3741 int err;
3742
NeilBrowna757e642005-04-16 15:26:42 -07003743 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
3746 printk(KERN_INFO "md: running: ");
3747
NeilBrownd089c6a2008-02-06 01:39:59 -08003748 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 char b[BDEVNAME_SIZE];
3750 printk("<%s>", bdevname(rdev->bdev,b));
3751 }
3752 printk("\n");
3753
3754 err = do_md_run (mddev);
3755 if (err) {
3756 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3757 do_md_stop (mddev, 0);
3758 }
3759}
3760
3761/*
3762 * lets try to run arrays based on all disks that have arrived
3763 * until now. (those are in pending_raid_disks)
3764 *
3765 * the method: pick the first pending disk, collect all disks with
3766 * the same UUID, remove all from the pending list and put them into
3767 * the 'same_array' list. Then order this list based on superblock
3768 * update time (freshest comes first), kick out 'old' disks and
3769 * compare superblocks. If everything's fine then run it.
3770 *
3771 * If "unit" is allocated, then bump its reference count
3772 */
3773static void autorun_devices(int part)
3774{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 struct list_head *tmp;
3776 mdk_rdev_t *rdev0, *rdev;
3777 mddev_t *mddev;
3778 char b[BDEVNAME_SIZE];
3779
3780 printk(KERN_INFO "md: autorun ...\n");
3781 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003782 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003784 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 rdev0 = list_entry(pending_raid_disks.next,
3786 mdk_rdev_t, same_set);
3787
3788 printk(KERN_INFO "md: considering %s ...\n",
3789 bdevname(rdev0->bdev,b));
3790 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003791 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 if (super_90_load(rdev, rdev0, 0) >= 0) {
3793 printk(KERN_INFO "md: adding %s ...\n",
3794 bdevname(rdev->bdev,b));
3795 list_move(&rdev->same_set, &candidates);
3796 }
3797 /*
3798 * now we have a set of devices, with all of them having
3799 * mostly sane superblocks. It's time to allocate the
3800 * mddev.
3801 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003802 if (part) {
3803 dev = MKDEV(mdp_major,
3804 rdev0->preferred_minor << MdpMinorShift);
3805 unit = MINOR(dev) >> MdpMinorShift;
3806 } else {
3807 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3808 unit = MINOR(dev);
3809 }
3810 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3812 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3813 break;
3814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
3816 md_probe(dev, NULL, NULL);
3817 mddev = mddev_find(dev);
3818 if (!mddev) {
3819 printk(KERN_ERR
3820 "md: cannot allocate memory for md drive.\n");
3821 break;
3822 }
3823 if (mddev_lock(mddev))
3824 printk(KERN_WARNING "md: %s locked, cannot run\n",
3825 mdname(mddev));
3826 else if (mddev->raid_disks || mddev->major_version
3827 || !list_empty(&mddev->disks)) {
3828 printk(KERN_WARNING
3829 "md: %s already running, cannot run %s\n",
3830 mdname(mddev), bdevname(rdev0->bdev,b));
3831 mddev_unlock(mddev);
3832 } else {
3833 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08003834 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08003835 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 list_del_init(&rdev->same_set);
3837 if (bind_rdev_to_array(rdev, mddev))
3838 export_rdev(rdev);
3839 }
3840 autorun_array(mddev);
3841 mddev_unlock(mddev);
3842 }
3843 /* on success, candidates will be empty, on error
3844 * it won't...
3845 */
NeilBrown73c34432008-02-06 01:39:59 -08003846 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 export_rdev(rdev);
3848 mddev_put(mddev);
3849 }
3850 printk(KERN_INFO "md: ... autorun DONE.\n");
3851}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003852#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854static int get_version(void __user * arg)
3855{
3856 mdu_version_t ver;
3857
3858 ver.major = MD_MAJOR_VERSION;
3859 ver.minor = MD_MINOR_VERSION;
3860 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3861
3862 if (copy_to_user(arg, &ver, sizeof(ver)))
3863 return -EFAULT;
3864
3865 return 0;
3866}
3867
3868static int get_array_info(mddev_t * mddev, void __user * arg)
3869{
3870 mdu_array_info_t info;
3871 int nr,working,active,failed,spare;
3872 mdk_rdev_t *rdev;
3873 struct list_head *tmp;
3874
3875 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003876 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003878 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 failed++;
3880 else {
3881 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003882 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 active++;
3884 else
3885 spare++;
3886 }
3887 }
3888
3889 info.major_version = mddev->major_version;
3890 info.minor_version = mddev->minor_version;
3891 info.patch_version = MD_PATCHLEVEL_VERSION;
3892 info.ctime = mddev->ctime;
3893 info.level = mddev->level;
3894 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003895 if (info.size != mddev->size) /* overflow */
3896 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 info.nr_disks = nr;
3898 info.raid_disks = mddev->raid_disks;
3899 info.md_minor = mddev->md_minor;
3900 info.not_persistent= !mddev->persistent;
3901
3902 info.utime = mddev->utime;
3903 info.state = 0;
3904 if (mddev->in_sync)
3905 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003906 if (mddev->bitmap && mddev->bitmap_offset)
3907 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 info.active_disks = active;
3909 info.working_disks = working;
3910 info.failed_disks = failed;
3911 info.spare_disks = spare;
3912
3913 info.layout = mddev->layout;
3914 info.chunk_size = mddev->chunk_size;
3915
3916 if (copy_to_user(arg, &info, sizeof(info)))
3917 return -EFAULT;
3918
3919 return 0;
3920}
3921
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003922static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003923{
3924 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3925 char *ptr, *buf = NULL;
3926 int err = -ENOMEM;
3927
NeilBrown2a2275d2007-01-26 00:57:11 -08003928 md_allow_write(mddev);
3929
NeilBrown32a76272005-06-21 17:17:14 -07003930 file = kmalloc(sizeof(*file), GFP_KERNEL);
3931 if (!file)
3932 goto out;
3933
3934 /* bitmap disabled, zero the first byte and copy out */
3935 if (!mddev->bitmap || !mddev->bitmap->file) {
3936 file->pathname[0] = '\0';
3937 goto copy_out;
3938 }
3939
3940 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3941 if (!buf)
3942 goto out;
3943
3944 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3945 if (!ptr)
3946 goto out;
3947
3948 strcpy(file->pathname, ptr);
3949
3950copy_out:
3951 err = 0;
3952 if (copy_to_user(arg, file, sizeof(*file)))
3953 err = -EFAULT;
3954out:
3955 kfree(buf);
3956 kfree(file);
3957 return err;
3958}
3959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960static int get_disk_info(mddev_t * mddev, void __user * arg)
3961{
3962 mdu_disk_info_t info;
3963 unsigned int nr;
3964 mdk_rdev_t *rdev;
3965
3966 if (copy_from_user(&info, arg, sizeof(info)))
3967 return -EFAULT;
3968
3969 nr = info.number;
3970
3971 rdev = find_rdev_nr(mddev, nr);
3972 if (rdev) {
3973 info.major = MAJOR(rdev->bdev->bd_dev);
3974 info.minor = MINOR(rdev->bdev->bd_dev);
3975 info.raid_disk = rdev->raid_disk;
3976 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003977 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003979 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 info.state |= (1<<MD_DISK_ACTIVE);
3981 info.state |= (1<<MD_DISK_SYNC);
3982 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003983 if (test_bit(WriteMostly, &rdev->flags))
3984 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 } else {
3986 info.major = info.minor = 0;
3987 info.raid_disk = -1;
3988 info.state = (1<<MD_DISK_REMOVED);
3989 }
3990
3991 if (copy_to_user(arg, &info, sizeof(info)))
3992 return -EFAULT;
3993
3994 return 0;
3995}
3996
3997static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3998{
3999 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4000 mdk_rdev_t *rdev;
4001 dev_t dev = MKDEV(info->major,info->minor);
4002
4003 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4004 return -EOVERFLOW;
4005
4006 if (!mddev->raid_disks) {
4007 int err;
4008 /* expecting a device which has a superblock */
4009 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4010 if (IS_ERR(rdev)) {
4011 printk(KERN_WARNING
4012 "md: md_import_device returned %ld\n",
4013 PTR_ERR(rdev));
4014 return PTR_ERR(rdev);
4015 }
4016 if (!list_empty(&mddev->disks)) {
4017 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4018 mdk_rdev_t, same_set);
4019 int err = super_types[mddev->major_version]
4020 .load_super(rdev, rdev0, mddev->minor_version);
4021 if (err < 0) {
4022 printk(KERN_WARNING
4023 "md: %s has different UUID to %s\n",
4024 bdevname(rdev->bdev,b),
4025 bdevname(rdev0->bdev,b2));
4026 export_rdev(rdev);
4027 return -EINVAL;
4028 }
4029 }
4030 err = bind_rdev_to_array(rdev, mddev);
4031 if (err)
4032 export_rdev(rdev);
4033 return err;
4034 }
4035
4036 /*
4037 * add_new_disk can be used once the array is assembled
4038 * to add "hot spares". They must already have a superblock
4039 * written
4040 */
4041 if (mddev->pers) {
4042 int err;
4043 if (!mddev->pers->hot_add_disk) {
4044 printk(KERN_WARNING
4045 "%s: personality does not support diskops!\n",
4046 mdname(mddev));
4047 return -EINVAL;
4048 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004049 if (mddev->persistent)
4050 rdev = md_import_device(dev, mddev->major_version,
4051 mddev->minor_version);
4052 else
4053 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 if (IS_ERR(rdev)) {
4055 printk(KERN_WARNING
4056 "md: md_import_device returned %ld\n",
4057 PTR_ERR(rdev));
4058 return PTR_ERR(rdev);
4059 }
NeilBrown41158c72005-06-21 17:17:25 -07004060 /* set save_raid_disk if appropriate */
4061 if (!mddev->persistent) {
4062 if (info->state & (1<<MD_DISK_SYNC) &&
4063 info->raid_disk < mddev->raid_disks)
4064 rdev->raid_disk = info->raid_disk;
4065 else
4066 rdev->raid_disk = -1;
4067 } else
4068 super_types[mddev->major_version].
4069 validate_super(mddev, rdev);
4070 rdev->saved_raid_disk = rdev->raid_disk;
4071
NeilBrownb2d444d2005-11-08 21:39:31 -08004072 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004073 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4074 set_bit(WriteMostly, &rdev->flags);
4075
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 rdev->raid_disk = -1;
4077 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004078 if (!err && !mddev->pers->hot_remove_disk) {
4079 /* If there is hot_add_disk but no hot_remove_disk
4080 * then added disks for geometry changes,
4081 * and should be added immediately.
4082 */
4083 super_types[mddev->major_version].
4084 validate_super(mddev, rdev);
4085 err = mddev->pers->hot_add_disk(mddev, rdev);
4086 if (err)
4087 unbind_rdev_from_array(rdev);
4088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 if (err)
4090 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004091
NeilBrown17571282006-12-10 02:20:52 -08004092 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07004093 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004094 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 return err;
4096 }
4097
4098 /* otherwise, add_new_disk is only allowed
4099 * for major_version==0 superblocks
4100 */
4101 if (mddev->major_version != 0) {
4102 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4103 mdname(mddev));
4104 return -EINVAL;
4105 }
4106
4107 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4108 int err;
4109 rdev = md_import_device (dev, -1, 0);
4110 if (IS_ERR(rdev)) {
4111 printk(KERN_WARNING
4112 "md: error, md_import_device() returned %ld\n",
4113 PTR_ERR(rdev));
4114 return PTR_ERR(rdev);
4115 }
4116 rdev->desc_nr = info->number;
4117 if (info->raid_disk < mddev->raid_disks)
4118 rdev->raid_disk = info->raid_disk;
4119 else
4120 rdev->raid_disk = -1;
4121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004123 if (info->state & (1<<MD_DISK_SYNC))
4124 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004126 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4127 set_bit(WriteMostly, &rdev->flags);
4128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 if (!mddev->persistent) {
4130 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4131 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4132 } else
4133 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4134 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4135
NeilBrown2bf071b2006-01-06 00:20:55 -08004136 err = bind_rdev_to_array(rdev, mddev);
4137 if (err) {
4138 export_rdev(rdev);
4139 return err;
4140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 }
4142
4143 return 0;
4144}
4145
4146static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4147{
4148 char b[BDEVNAME_SIZE];
4149 mdk_rdev_t *rdev;
4150
4151 if (!mddev->pers)
4152 return -ENODEV;
4153
4154 rdev = find_rdev(mddev, dev);
4155 if (!rdev)
4156 return -ENXIO;
4157
4158 if (rdev->raid_disk >= 0)
4159 goto busy;
4160
4161 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004162 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004163 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
4165 return 0;
4166busy:
4167 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
4168 bdevname(rdev->bdev,b), mdname(mddev));
4169 return -EBUSY;
4170}
4171
4172static int hot_add_disk(mddev_t * mddev, dev_t dev)
4173{
4174 char b[BDEVNAME_SIZE];
4175 int err;
4176 unsigned int size;
4177 mdk_rdev_t *rdev;
4178
4179 if (!mddev->pers)
4180 return -ENODEV;
4181
4182 if (mddev->major_version != 0) {
4183 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4184 " version-0 superblocks.\n",
4185 mdname(mddev));
4186 return -EINVAL;
4187 }
4188 if (!mddev->pers->hot_add_disk) {
4189 printk(KERN_WARNING
4190 "%s: personality does not support diskops!\n",
4191 mdname(mddev));
4192 return -EINVAL;
4193 }
4194
4195 rdev = md_import_device (dev, -1, 0);
4196 if (IS_ERR(rdev)) {
4197 printk(KERN_WARNING
4198 "md: error, md_import_device() returned %ld\n",
4199 PTR_ERR(rdev));
4200 return -EINVAL;
4201 }
4202
4203 if (mddev->persistent)
4204 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4205 else
4206 rdev->sb_offset =
4207 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4208
4209 size = calc_dev_size(rdev, mddev->chunk_size);
4210 rdev->size = size;
4211
NeilBrownb2d444d2005-11-08 21:39:31 -08004212 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 printk(KERN_WARNING
4214 "md: can not hot-add faulty %s disk to %s!\n",
4215 bdevname(rdev->bdev,b), mdname(mddev));
4216 err = -EINVAL;
4217 goto abort_export;
4218 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004219 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004221 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004222 err = bind_rdev_to_array(rdev, mddev);
4223 if (err)
4224 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
4226 /*
4227 * The rest should better be atomic, we can have disk failures
4228 * noticed in interrupt contexts ...
4229 */
4230
4231 if (rdev->desc_nr == mddev->max_disks) {
4232 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4233 mdname(mddev));
4234 err = -EBUSY;
4235 goto abort_unbind_export;
4236 }
4237
4238 rdev->raid_disk = -1;
4239
NeilBrown850b2b42006-10-03 01:15:46 -07004240 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
4242 /*
4243 * Kick recovery, maybe this spare has to be added to the
4244 * array immediately.
4245 */
4246 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4247 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004248 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 return 0;
4250
4251abort_unbind_export:
4252 unbind_rdev_from_array(rdev);
4253
4254abort_export:
4255 export_rdev(rdev);
4256 return err;
4257}
4258
NeilBrown32a76272005-06-21 17:17:14 -07004259static int set_bitmap_file(mddev_t *mddev, int fd)
4260{
4261 int err;
4262
NeilBrown36fa3062005-09-09 16:23:45 -07004263 if (mddev->pers) {
4264 if (!mddev->pers->quiesce)
4265 return -EBUSY;
4266 if (mddev->recovery || mddev->sync_thread)
4267 return -EBUSY;
4268 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004269 }
4270
NeilBrown36fa3062005-09-09 16:23:45 -07004271
4272 if (fd >= 0) {
4273 if (mddev->bitmap)
4274 return -EEXIST; /* cannot add when bitmap is present */
4275 mddev->bitmap_file = fget(fd);
4276
4277 if (mddev->bitmap_file == NULL) {
4278 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4279 mdname(mddev));
4280 return -EBADF;
4281 }
4282
4283 err = deny_bitmap_write_access(mddev->bitmap_file);
4284 if (err) {
4285 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4286 mdname(mddev));
4287 fput(mddev->bitmap_file);
4288 mddev->bitmap_file = NULL;
4289 return err;
4290 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004291 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004292 } else if (mddev->bitmap == NULL)
4293 return -ENOENT; /* cannot remove what isn't there */
4294 err = 0;
4295 if (mddev->pers) {
4296 mddev->pers->quiesce(mddev, 1);
4297 if (fd >= 0)
4298 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004299 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004300 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004301 fd = -1; /* make sure to put the file */
4302 }
NeilBrown36fa3062005-09-09 16:23:45 -07004303 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004304 }
4305 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004306 if (mddev->bitmap_file) {
4307 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004308 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004309 }
NeilBrown36fa3062005-09-09 16:23:45 -07004310 mddev->bitmap_file = NULL;
4311 }
4312
NeilBrown32a76272005-06-21 17:17:14 -07004313 return err;
4314}
4315
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316/*
4317 * set_array_info is used two different ways
4318 * The original usage is when creating a new array.
4319 * In this usage, raid_disks is > 0 and it together with
4320 * level, size, not_persistent,layout,chunksize determine the
4321 * shape of the array.
4322 * This will always create an array with a type-0.90.0 superblock.
4323 * The newer usage is when assembling an array.
4324 * In this case raid_disks will be 0, and the major_version field is
4325 * use to determine which style super-blocks are to be found on the devices.
4326 * The minor and patch _version numbers are also kept incase the
4327 * super_block handler wishes to interpret them.
4328 */
4329static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4330{
4331
4332 if (info->raid_disks == 0) {
4333 /* just setting version number for superblock loading */
4334 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004335 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 super_types[info->major_version].name == NULL) {
4337 /* maybe try to auto-load a module? */
4338 printk(KERN_INFO
4339 "md: superblock version %d not known\n",
4340 info->major_version);
4341 return -EINVAL;
4342 }
4343 mddev->major_version = info->major_version;
4344 mddev->minor_version = info->minor_version;
4345 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004346 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 return 0;
4348 }
4349 mddev->major_version = MD_MAJOR_VERSION;
4350 mddev->minor_version = MD_MINOR_VERSION;
4351 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4352 mddev->ctime = get_seconds();
4353
4354 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004355 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 mddev->size = info->size;
4357 mddev->raid_disks = info->raid_disks;
4358 /* don't set md_minor, it is determined by which /dev/md* was
4359 * openned
4360 */
4361 if (info->state & (1<<MD_SB_CLEAN))
4362 mddev->recovery_cp = MaxSector;
4363 else
4364 mddev->recovery_cp = 0;
4365 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004366 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367
4368 mddev->layout = info->layout;
4369 mddev->chunk_size = info->chunk_size;
4370
4371 mddev->max_disks = MD_SB_DISKS;
4372
NeilBrowne6910632008-02-06 01:39:51 -08004373 if (mddev->persistent)
4374 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004375 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376
NeilBrownb2a27032005-11-28 13:44:12 -08004377 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4378 mddev->bitmap_offset = 0;
4379
NeilBrownf6705572006-03-27 01:18:11 -08004380 mddev->reshape_position = MaxSector;
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 /*
4383 * Generate a 128 bit UUID
4384 */
4385 get_random_bytes(mddev->uuid, 16);
4386
NeilBrownf6705572006-03-27 01:18:11 -08004387 mddev->new_level = mddev->level;
4388 mddev->new_chunk = mddev->chunk_size;
4389 mddev->new_layout = mddev->layout;
4390 mddev->delta_disks = 0;
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 return 0;
4393}
4394
NeilBrowna35b0d62006-01-06 00:20:49 -08004395static int update_size(mddev_t *mddev, unsigned long size)
4396{
4397 mdk_rdev_t * rdev;
4398 int rv;
4399 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004400 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004401
4402 if (mddev->pers->resize == NULL)
4403 return -EINVAL;
4404 /* The "size" is the amount of each device that is used.
4405 * This can only make sense for arrays with redundancy.
4406 * linear and raid0 always use whatever space is available
4407 * We can only consider changing the size if no resync
4408 * or reconstruction is happening, and if the new size
4409 * is acceptable. It must fit before the sb_offset or,
4410 * if that is <data_offset, it must fit before the
4411 * size of each device.
4412 * If size is zero, we find the largest size that fits.
4413 */
4414 if (mddev->sync_thread)
4415 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004416 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004417 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004418 avail = rdev->size * 2;
4419
NeilBrowna35b0d62006-01-06 00:20:49 -08004420 if (fit && (size == 0 || size > avail/2))
4421 size = avail/2;
4422 if (avail < ((sector_t)size << 1))
4423 return -ENOSPC;
4424 }
4425 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4426 if (!rv) {
4427 struct block_device *bdev;
4428
4429 bdev = bdget_disk(mddev->gendisk, 0);
4430 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004431 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004432 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004433 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004434 bdput(bdev);
4435 }
4436 }
4437 return rv;
4438}
4439
NeilBrownda943b992006-01-06 00:20:54 -08004440static int update_raid_disks(mddev_t *mddev, int raid_disks)
4441{
4442 int rv;
4443 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004444 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004445 return -EINVAL;
4446 if (raid_disks <= 0 ||
4447 raid_disks >= mddev->max_disks)
4448 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004449 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004450 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004451 mddev->delta_disks = raid_disks - mddev->raid_disks;
4452
4453 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004454 return rv;
4455}
4456
4457
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458/*
4459 * update_array_info is used to change the configuration of an
4460 * on-line array.
4461 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4462 * fields in the info are checked against the array.
4463 * Any differences that cannot be handled will cause an error.
4464 * Normally, only one change can be managed at a time.
4465 */
4466static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4467{
4468 int rv = 0;
4469 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004470 int state = 0;
4471
4472 /* calculate expected state,ignoring low bits */
4473 if (mddev->bitmap && mddev->bitmap_offset)
4474 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 if (mddev->major_version != info->major_version ||
4477 mddev->minor_version != info->minor_version ||
4478/* mddev->patch_version != info->patch_version || */
4479 mddev->ctime != info->ctime ||
4480 mddev->level != info->level ||
4481/* mddev->layout != info->layout || */
4482 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004483 mddev->chunk_size != info->chunk_size ||
4484 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4485 ((state^info->state) & 0xfffffe00)
4486 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 return -EINVAL;
4488 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004489 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 if (mddev->raid_disks != info->raid_disks) cnt++;
4491 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004492 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 if (cnt == 0) return 0;
4494 if (cnt > 1) return -EINVAL;
4495
4496 if (mddev->layout != info->layout) {
4497 /* Change layout
4498 * we don't need to do anything at the md level, the
4499 * personality will take care of it all.
4500 */
4501 if (mddev->pers->reconfig == NULL)
4502 return -EINVAL;
4503 else
4504 return mddev->pers->reconfig(mddev, info->layout, -1);
4505 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004506 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004507 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
NeilBrownda943b992006-01-06 00:20:54 -08004509 if (mddev->raid_disks != info->raid_disks)
4510 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
NeilBrown36fa3062005-09-09 16:23:45 -07004512 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4513 if (mddev->pers->quiesce == NULL)
4514 return -EINVAL;
4515 if (mddev->recovery || mddev->sync_thread)
4516 return -EBUSY;
4517 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4518 /* add the bitmap */
4519 if (mddev->bitmap)
4520 return -EEXIST;
4521 if (mddev->default_bitmap_offset == 0)
4522 return -EINVAL;
4523 mddev->bitmap_offset = mddev->default_bitmap_offset;
4524 mddev->pers->quiesce(mddev, 1);
4525 rv = bitmap_create(mddev);
4526 if (rv)
4527 bitmap_destroy(mddev);
4528 mddev->pers->quiesce(mddev, 0);
4529 } else {
4530 /* remove the bitmap */
4531 if (!mddev->bitmap)
4532 return -ENOENT;
4533 if (mddev->bitmap->file)
4534 return -EINVAL;
4535 mddev->pers->quiesce(mddev, 1);
4536 bitmap_destroy(mddev);
4537 mddev->pers->quiesce(mddev, 0);
4538 mddev->bitmap_offset = 0;
4539 }
4540 }
NeilBrown850b2b42006-10-03 01:15:46 -07004541 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 return rv;
4543}
4544
4545static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4546{
4547 mdk_rdev_t *rdev;
4548
4549 if (mddev->pers == NULL)
4550 return -ENODEV;
4551
4552 rdev = find_rdev(mddev, dev);
4553 if (!rdev)
4554 return -ENODEV;
4555
4556 md_error(mddev, rdev);
4557 return 0;
4558}
4559
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004560static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4561{
4562 mddev_t *mddev = bdev->bd_disk->private_data;
4563
4564 geo->heads = 2;
4565 geo->sectors = 4;
4566 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4567 return 0;
4568}
4569
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570static int md_ioctl(struct inode *inode, struct file *file,
4571 unsigned int cmd, unsigned long arg)
4572{
4573 int err = 0;
4574 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 mddev_t *mddev = NULL;
4576
4577 if (!capable(CAP_SYS_ADMIN))
4578 return -EACCES;
4579
4580 /*
4581 * Commands dealing with the RAID driver but not any
4582 * particular array:
4583 */
4584 switch (cmd)
4585 {
4586 case RAID_VERSION:
4587 err = get_version(argp);
4588 goto done;
4589
4590 case PRINT_RAID_DEBUG:
4591 err = 0;
4592 md_print_devices();
4593 goto done;
4594
4595#ifndef MODULE
4596 case RAID_AUTORUN:
4597 err = 0;
4598 autostart_arrays(arg);
4599 goto done;
4600#endif
4601 default:;
4602 }
4603
4604 /*
4605 * Commands creating/starting a new array:
4606 */
4607
4608 mddev = inode->i_bdev->bd_disk->private_data;
4609
4610 if (!mddev) {
4611 BUG();
4612 goto abort;
4613 }
4614
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 err = mddev_lock(mddev);
4616 if (err) {
4617 printk(KERN_INFO
4618 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4619 err, cmd);
4620 goto abort;
4621 }
4622
4623 switch (cmd)
4624 {
4625 case SET_ARRAY_INFO:
4626 {
4627 mdu_array_info_t info;
4628 if (!arg)
4629 memset(&info, 0, sizeof(info));
4630 else if (copy_from_user(&info, argp, sizeof(info))) {
4631 err = -EFAULT;
4632 goto abort_unlock;
4633 }
4634 if (mddev->pers) {
4635 err = update_array_info(mddev, &info);
4636 if (err) {
4637 printk(KERN_WARNING "md: couldn't update"
4638 " array info. %d\n", err);
4639 goto abort_unlock;
4640 }
4641 goto done_unlock;
4642 }
4643 if (!list_empty(&mddev->disks)) {
4644 printk(KERN_WARNING
4645 "md: array %s already has disks!\n",
4646 mdname(mddev));
4647 err = -EBUSY;
4648 goto abort_unlock;
4649 }
4650 if (mddev->raid_disks) {
4651 printk(KERN_WARNING
4652 "md: array %s already initialised!\n",
4653 mdname(mddev));
4654 err = -EBUSY;
4655 goto abort_unlock;
4656 }
4657 err = set_array_info(mddev, &info);
4658 if (err) {
4659 printk(KERN_WARNING "md: couldn't set"
4660 " array info. %d\n", err);
4661 goto abort_unlock;
4662 }
4663 }
4664 goto done_unlock;
4665
4666 default:;
4667 }
4668
4669 /*
4670 * Commands querying/configuring an existing array:
4671 */
NeilBrown32a76272005-06-21 17:17:14 -07004672 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004673 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004674 if ((!mddev->raid_disks && !mddev->external)
4675 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4676 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4677 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 err = -ENODEV;
4679 goto abort_unlock;
4680 }
4681
4682 /*
4683 * Commands even a read-only array can execute:
4684 */
4685 switch (cmd)
4686 {
4687 case GET_ARRAY_INFO:
4688 err = get_array_info(mddev, argp);
4689 goto done_unlock;
4690
NeilBrown32a76272005-06-21 17:17:14 -07004691 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004692 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004693 goto done_unlock;
4694
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 case GET_DISK_INFO:
4696 err = get_disk_info(mddev, argp);
4697 goto done_unlock;
4698
4699 case RESTART_ARRAY_RW:
4700 err = restart_array(mddev);
4701 goto done_unlock;
4702
4703 case STOP_ARRAY:
4704 err = do_md_stop (mddev, 0);
4705 goto done_unlock;
4706
4707 case STOP_ARRAY_RO:
4708 err = do_md_stop (mddev, 1);
4709 goto done_unlock;
4710
4711 /*
4712 * We have a problem here : there is no easy way to give a CHS
4713 * virtual geometry. We currently pretend that we have a 2 heads
4714 * 4 sectors (with a BIG number of cylinders...). This drives
4715 * dosfs just mad... ;-)
4716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 }
4718
4719 /*
4720 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004721 * superblock, so we do not allow them on read-only arrays.
4722 * However non-MD ioctls (e.g. get-size) will still come through
4723 * here and hit the 'default' below, so only disallow
4724 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 */
NeilBrownf91de922005-11-08 21:39:36 -08004726 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4727 mddev->ro && mddev->pers) {
4728 if (mddev->ro == 2) {
4729 mddev->ro = 0;
4730 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4731 md_wakeup_thread(mddev->thread);
4732
4733 } else {
4734 err = -EROFS;
4735 goto abort_unlock;
4736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 }
4738
4739 switch (cmd)
4740 {
4741 case ADD_NEW_DISK:
4742 {
4743 mdu_disk_info_t info;
4744 if (copy_from_user(&info, argp, sizeof(info)))
4745 err = -EFAULT;
4746 else
4747 err = add_new_disk(mddev, &info);
4748 goto done_unlock;
4749 }
4750
4751 case HOT_REMOVE_DISK:
4752 err = hot_remove_disk(mddev, new_decode_dev(arg));
4753 goto done_unlock;
4754
4755 case HOT_ADD_DISK:
4756 err = hot_add_disk(mddev, new_decode_dev(arg));
4757 goto done_unlock;
4758
4759 case SET_DISK_FAULTY:
4760 err = set_disk_faulty(mddev, new_decode_dev(arg));
4761 goto done_unlock;
4762
4763 case RUN_ARRAY:
4764 err = do_md_run (mddev);
4765 goto done_unlock;
4766
NeilBrown32a76272005-06-21 17:17:14 -07004767 case SET_BITMAP_FILE:
4768 err = set_bitmap_file(mddev, (int)arg);
4769 goto done_unlock;
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 err = -EINVAL;
4773 goto abort_unlock;
4774 }
4775
4776done_unlock:
4777abort_unlock:
4778 mddev_unlock(mddev);
4779
4780 return err;
4781done:
4782 if (err)
4783 MD_BUG();
4784abort:
4785 return err;
4786}
4787
4788static int md_open(struct inode *inode, struct file *file)
4789{
4790 /*
4791 * Succeed if we can lock the mddev, which confirms that
4792 * it isn't being stopped right now.
4793 */
4794 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4795 int err;
4796
NeilBrownd63a5a72006-12-08 02:36:17 -08004797 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 goto out;
4799
4800 err = 0;
4801 mddev_get(mddev);
4802 mddev_unlock(mddev);
4803
4804 check_disk_change(inode->i_bdev);
4805 out:
4806 return err;
4807}
4808
4809static int md_release(struct inode *inode, struct file * file)
4810{
4811 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4812
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004813 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 mddev_put(mddev);
4815
4816 return 0;
4817}
4818
Linus Torvalds44ce62942007-05-09 18:51:36 -07004819static int md_media_changed(struct gendisk *disk)
4820{
4821 mddev_t *mddev = disk->private_data;
4822
4823 return mddev->changed;
4824}
4825
4826static int md_revalidate(struct gendisk *disk)
4827{
4828 mddev_t *mddev = disk->private_data;
4829
4830 mddev->changed = 0;
4831 return 0;
4832}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833static struct block_device_operations md_fops =
4834{
4835 .owner = THIS_MODULE,
4836 .open = md_open,
4837 .release = md_release,
4838 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004839 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004840 .media_changed = md_media_changed,
4841 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842};
4843
Adrian Bunk75c96f82005-05-05 16:16:09 -07004844static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845{
4846 mdk_thread_t *thread = arg;
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 /*
4849 * md_thread is a 'system-thread', it's priority should be very
4850 * high. We avoid resource deadlocks individually in each
4851 * raid personality. (RAID5 does preallocation) We also use RR and
4852 * the very same RT priority as kswapd, thus we will never get
4853 * into a priority inversion deadlock.
4854 *
4855 * we definitely have to have equal or higher priority than
4856 * bdflush, otherwise bdflush will deadlock if there are too
4857 * many dirty RAID5 blocks.
4858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
NeilBrown6985c432005-10-19 21:23:47 -07004860 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004861 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862
NeilBrown93588e22005-11-15 00:09:12 -08004863 /* We need to wait INTERRUPTIBLE so that
4864 * we don't add to the load-average.
4865 * That means we need to be sure no signals are
4866 * pending
4867 */
4868 if (signal_pending(current))
4869 flush_signals(current);
4870
4871 wait_event_interruptible_timeout
4872 (thread->wqueue,
4873 test_bit(THREAD_WAKEUP, &thread->flags)
4874 || kthread_should_stop(),
4875 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
4877 clear_bit(THREAD_WAKEUP, &thread->flags);
4878
NeilBrown787453c2005-11-08 21:39:43 -08004879 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 return 0;
4883}
4884
4885void md_wakeup_thread(mdk_thread_t *thread)
4886{
4887 if (thread) {
4888 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4889 set_bit(THREAD_WAKEUP, &thread->flags);
4890 wake_up(&thread->wqueue);
4891 }
4892}
4893
4894mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4895 const char *name)
4896{
4897 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
NeilBrown9ffae0c2006-01-06 00:20:32 -08004899 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 if (!thread)
4901 return NULL;
4902
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 init_waitqueue_head(&thread->wqueue);
4904
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 thread->run = run;
4906 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004907 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004908 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004909 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 kfree(thread);
4911 return NULL;
4912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 return thread;
4914}
4915
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916void md_unregister_thread(mdk_thread_t *thread)
4917{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004918 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07004919
4920 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 kfree(thread);
4922}
4923
4924void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4925{
4926 if (!mddev) {
4927 MD_BUG();
4928 return;
4929 }
4930
NeilBrownb2d444d2005-11-08 21:39:31 -08004931 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 return;
NeilBrown32a76272005-06-21 17:17:14 -07004933/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4935 mdname(mddev),
4936 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4937 __builtin_return_address(0),__builtin_return_address(1),
4938 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004939*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004940 if (!mddev->pers)
4941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 if (!mddev->pers->error_handler)
4943 return;
4944 mddev->pers->error_handler(mddev,rdev);
4945 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4946 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4947 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004948 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949}
4950
4951/* seq_file implementation /proc/mdstat */
4952
4953static void status_unused(struct seq_file *seq)
4954{
4955 int i = 0;
4956 mdk_rdev_t *rdev;
4957 struct list_head *tmp;
4958
4959 seq_printf(seq, "unused devices: ");
4960
NeilBrown73c34432008-02-06 01:39:59 -08004961 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 char b[BDEVNAME_SIZE];
4963 i++;
4964 seq_printf(seq, "%s ",
4965 bdevname(rdev->bdev,b));
4966 }
4967 if (!i)
4968 seq_printf(seq, "<none>");
4969
4970 seq_printf(seq, "\n");
4971}
4972
4973
4974static void status_resync(struct seq_file *seq, mddev_t * mddev)
4975{
NeilBrown4588b422006-03-27 01:18:04 -08004976 sector_t max_blocks, resync, res;
4977 unsigned long dt, db, rt;
4978 int scale;
4979 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980
4981 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4982
4983 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4984 max_blocks = mddev->resync_max_sectors >> 1;
4985 else
4986 max_blocks = mddev->size;
4987
4988 /*
4989 * Should not happen.
4990 */
4991 if (!max_blocks) {
4992 MD_BUG();
4993 return;
4994 }
NeilBrown4588b422006-03-27 01:18:04 -08004995 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4996 * in a sector_t, and (max_blocks>>scale) will fit in a
4997 * u32, as those are the requirements for sector_div.
4998 * Thus 'scale' must be at least 10
4999 */
5000 scale = 10;
5001 if (sizeof(sector_t) > sizeof(unsigned long)) {
5002 while ( max_blocks/2 > (1ULL<<(scale+32)))
5003 scale++;
5004 }
5005 res = (resync>>scale)*1000;
5006 sector_div(res, (u32)((max_blocks>>scale)+1));
5007
5008 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 {
NeilBrown4588b422006-03-27 01:18:04 -08005010 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 seq_printf(seq, "[");
5012 for (i = 0; i < x; i++)
5013 seq_printf(seq, "=");
5014 seq_printf(seq, ">");
5015 for (i = 0; i < y; i++)
5016 seq_printf(seq, ".");
5017 seq_printf(seq, "] ");
5018 }
NeilBrown4588b422006-03-27 01:18:04 -08005019 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005020 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5021 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005022 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5023 "check" :
5024 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5025 "resync" : "recovery"))),
5026 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005027 (unsigned long long) resync,
5028 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 /*
5031 * We do not want to overflow, so the order of operands and
5032 * the * 100 / 100 trick are important. We do a +1 to be
5033 * safe against division by zero. We only estimate anyway.
5034 *
5035 * dt: time from mark until now
5036 * db: blocks written from mark until now
5037 * rt: remaining time
5038 */
5039 dt = ((jiffies - mddev->resync_mark) / HZ);
5040 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005041 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5042 - mddev->resync_mark_cnt;
5043 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
5045 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5046
NeilBrownff4e8d92006-07-10 04:44:16 -07005047 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048}
5049
5050static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5051{
5052 struct list_head *tmp;
5053 loff_t l = *pos;
5054 mddev_t *mddev;
5055
5056 if (l >= 0x10000)
5057 return NULL;
5058 if (!l--)
5059 /* header */
5060 return (void*)1;
5061
5062 spin_lock(&all_mddevs_lock);
5063 list_for_each(tmp,&all_mddevs)
5064 if (!l--) {
5065 mddev = list_entry(tmp, mddev_t, all_mddevs);
5066 mddev_get(mddev);
5067 spin_unlock(&all_mddevs_lock);
5068 return mddev;
5069 }
5070 spin_unlock(&all_mddevs_lock);
5071 if (!l--)
5072 return (void*)2;/* tail */
5073 return NULL;
5074}
5075
5076static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5077{
5078 struct list_head *tmp;
5079 mddev_t *next_mddev, *mddev = v;
5080
5081 ++*pos;
5082 if (v == (void*)2)
5083 return NULL;
5084
5085 spin_lock(&all_mddevs_lock);
5086 if (v == (void*)1)
5087 tmp = all_mddevs.next;
5088 else
5089 tmp = mddev->all_mddevs.next;
5090 if (tmp != &all_mddevs)
5091 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5092 else {
5093 next_mddev = (void*)2;
5094 *pos = 0x10000;
5095 }
5096 spin_unlock(&all_mddevs_lock);
5097
5098 if (v != (void*)1)
5099 mddev_put(mddev);
5100 return next_mddev;
5101
5102}
5103
5104static void md_seq_stop(struct seq_file *seq, void *v)
5105{
5106 mddev_t *mddev = v;
5107
5108 if (mddev && v != (void*)1 && v != (void*)2)
5109 mddev_put(mddev);
5110}
5111
NeilBrownd7603b72006-01-06 00:20:30 -08005112struct mdstat_info {
5113 int event;
5114};
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116static int md_seq_show(struct seq_file *seq, void *v)
5117{
5118 mddev_t *mddev = v;
5119 sector_t size;
5120 struct list_head *tmp2;
5121 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005122 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005123 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124
5125 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005126 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 seq_printf(seq, "Personalities : ");
5128 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005129 list_for_each_entry(pers, &pers_list, list)
5130 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131
5132 spin_unlock(&pers_lock);
5133 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005134 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 return 0;
5136 }
5137 if (v == (void*)2) {
5138 status_unused(seq);
5139 return 0;
5140 }
5141
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005142 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005144
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5146 seq_printf(seq, "%s : %sactive", mdname(mddev),
5147 mddev->pers ? "" : "in");
5148 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005149 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005151 if (mddev->ro==2)
5152 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 seq_printf(seq, " %s", mddev->pers->name);
5154 }
5155
5156 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005157 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 char b[BDEVNAME_SIZE];
5159 seq_printf(seq, " %s[%d]",
5160 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005161 if (test_bit(WriteMostly, &rdev->flags))
5162 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005163 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 seq_printf(seq, "(F)");
5165 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005166 } else if (rdev->raid_disk < 0)
5167 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 size += rdev->size;
5169 }
5170
5171 if (!list_empty(&mddev->disks)) {
5172 if (mddev->pers)
5173 seq_printf(seq, "\n %llu blocks",
5174 (unsigned long long)mddev->array_size);
5175 else
5176 seq_printf(seq, "\n %llu blocks",
5177 (unsigned long long)size);
5178 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005179 if (mddev->persistent) {
5180 if (mddev->major_version != 0 ||
5181 mddev->minor_version != 90) {
5182 seq_printf(seq," super %d.%d",
5183 mddev->major_version,
5184 mddev->minor_version);
5185 }
NeilBrowne6910632008-02-06 01:39:51 -08005186 } else if (mddev->external)
5187 seq_printf(seq, " super external:%s",
5188 mddev->metadata_type);
5189 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005190 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
5192 if (mddev->pers) {
5193 mddev->pers->status (seq, mddev);
5194 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005195 if (mddev->pers->sync_request) {
5196 if (mddev->curr_resync > 2) {
5197 status_resync (seq, mddev);
5198 seq_printf(seq, "\n ");
5199 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5200 seq_printf(seq, "\tresync=DELAYED\n ");
5201 else if (mddev->recovery_cp < MaxSector)
5202 seq_printf(seq, "\tresync=PENDING\n ");
5203 }
NeilBrown32a76272005-06-21 17:17:14 -07005204 } else
5205 seq_printf(seq, "\n ");
5206
5207 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005208 unsigned long chunk_kb;
5209 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005210 spin_lock_irqsave(&bitmap->lock, flags);
5211 chunk_kb = bitmap->chunksize >> 10;
5212 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5213 "%lu%s chunk",
5214 bitmap->pages - bitmap->missing_pages,
5215 bitmap->pages,
5216 (bitmap->pages - bitmap->missing_pages)
5217 << (PAGE_SHIFT - 10),
5218 chunk_kb ? chunk_kb : bitmap->chunksize,
5219 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005220 if (bitmap->file) {
5221 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005222 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005223 }
NeilBrown78d742d2005-06-21 17:17:15 -07005224
NeilBrown32a76272005-06-21 17:17:14 -07005225 seq_printf(seq, "\n");
5226 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 }
5228
5229 seq_printf(seq, "\n");
5230 }
5231 mddev_unlock(mddev);
5232
5233 return 0;
5234}
5235
5236static struct seq_operations md_seq_ops = {
5237 .start = md_seq_start,
5238 .next = md_seq_next,
5239 .stop = md_seq_stop,
5240 .show = md_seq_show,
5241};
5242
5243static int md_seq_open(struct inode *inode, struct file *file)
5244{
5245 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005246 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5247 if (mi == NULL)
5248 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249
5250 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005251 if (error)
5252 kfree(mi);
5253 else {
5254 struct seq_file *p = file->private_data;
5255 p->private = mi;
5256 mi->event = atomic_read(&md_event_count);
5257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 return error;
5259}
5260
NeilBrownd7603b72006-01-06 00:20:30 -08005261static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5262{
5263 struct seq_file *m = filp->private_data;
5264 struct mdstat_info *mi = m->private;
5265 int mask;
5266
5267 poll_wait(filp, &md_event_waiters, wait);
5268
5269 /* always allow read */
5270 mask = POLLIN | POLLRDNORM;
5271
5272 if (mi->event != atomic_read(&md_event_count))
5273 mask |= POLLERR | POLLPRI;
5274 return mask;
5275}
5276
Arjan van de Venfa027c22007-02-12 00:55:33 -08005277static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005278 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 .open = md_seq_open,
5280 .read = seq_read,
5281 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005282 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005283 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284};
5285
NeilBrown2604b702006-01-06 00:20:36 -08005286int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005289 list_add_tail(&p->list, &pers_list);
5290 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 spin_unlock(&pers_lock);
5292 return 0;
5293}
5294
NeilBrown2604b702006-01-06 00:20:36 -08005295int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296{
NeilBrown2604b702006-01-06 00:20:36 -08005297 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005299 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 spin_unlock(&pers_lock);
5301 return 0;
5302}
5303
5304static int is_mddev_idle(mddev_t *mddev)
5305{
5306 mdk_rdev_t * rdev;
5307 struct list_head *tmp;
5308 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005309 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
5311 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005312 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005314 curr_events = disk_stat_read(disk, sectors[0]) +
5315 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005317 /* sync IO will cause sync_io to increase before the disk_stats
5318 * as sync_io is counted when a request starts, and
5319 * disk_stats is counted when it completes.
5320 * So resync activity will cause curr_events to be smaller than
5321 * when there was no such activity.
5322 * non-sync IO will cause disk_stat to increase without
5323 * increasing sync_io so curr_events will (eventually)
5324 * be larger than it was before. Once it becomes
5325 * substantially larger, the test below will cause
5326 * the array to appear non-idle, and resync will slow
5327 * down.
5328 * If there is a lot of outstanding resync activity when
5329 * we set last_event to curr_events, then all that activity
5330 * completing might cause the array to appear non-idle
5331 * and resync will be slowed down even though there might
5332 * not have been non-resync activity. This will only
5333 * happen once though. 'last_events' will soon reflect
5334 * the state where there is little or no outstanding
5335 * resync requests, and further resync activity will
5336 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005337 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005339 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 rdev->last_events = curr_events;
5341 idle = 0;
5342 }
5343 }
5344 return idle;
5345}
5346
5347void md_done_sync(mddev_t *mddev, int blocks, int ok)
5348{
5349 /* another "blocks" (512byte) blocks have been synced */
5350 atomic_sub(blocks, &mddev->recovery_active);
5351 wake_up(&mddev->recovery_wait);
5352 if (!ok) {
5353 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5354 md_wakeup_thread(mddev->thread);
5355 // stop recovery, signal do_sync ....
5356 }
5357}
5358
5359
NeilBrown06d91a52005-06-21 17:17:12 -07005360/* md_write_start(mddev, bi)
5361 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005362 * in superblock) before writing, schedule a superblock update
5363 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005364 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005365void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366{
NeilBrown06d91a52005-06-21 17:17:12 -07005367 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005368 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005369
NeilBrownf91de922005-11-08 21:39:36 -08005370 BUG_ON(mddev->ro == 1);
5371 if (mddev->ro == 2) {
5372 /* need to switch to read/write */
5373 mddev->ro = 0;
5374 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5375 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005376 md_wakeup_thread(mddev->sync_thread);
NeilBrownf91de922005-11-08 21:39:36 -08005377 }
NeilBrown06d91a52005-06-21 17:17:12 -07005378 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005379 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005380 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005381 if (mddev->in_sync) {
5382 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005383 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005384 md_wakeup_thread(mddev->thread);
5385 }
NeilBrowna9701a32005-11-08 21:39:34 -08005386 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005387 }
NeilBrown850b2b42006-10-03 01:15:46 -07005388 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389}
5390
5391void md_write_end(mddev_t *mddev)
5392{
5393 if (atomic_dec_and_test(&mddev->writes_pending)) {
5394 if (mddev->safemode == 2)
5395 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005396 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5398 }
5399}
5400
NeilBrown2a2275d2007-01-26 00:57:11 -08005401/* md_allow_write(mddev)
5402 * Calling this ensures that the array is marked 'active' so that writes
5403 * may proceed without blocking. It is important to call this before
5404 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5405 * Must be called with mddev_lock held.
5406 */
5407void md_allow_write(mddev_t *mddev)
5408{
5409 if (!mddev->pers)
5410 return;
5411 if (mddev->ro)
5412 return;
5413
5414 spin_lock_irq(&mddev->write_lock);
5415 if (mddev->in_sync) {
5416 mddev->in_sync = 0;
5417 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5418 if (mddev->safemode_delay &&
5419 mddev->safemode == 0)
5420 mddev->safemode = 1;
5421 spin_unlock_irq(&mddev->write_lock);
5422 md_update_sb(mddev, 0);
5423 } else
5424 spin_unlock_irq(&mddev->write_lock);
5425}
5426EXPORT_SYMBOL_GPL(md_allow_write);
5427
Adrian Bunk75c96f82005-05-05 16:16:09 -07005428static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429
5430#define SYNC_MARKS 10
5431#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005432void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433{
5434 mddev_t *mddev2;
5435 unsigned int currspeed = 0,
5436 window;
NeilBrown57afd892005-06-21 17:17:13 -07005437 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 unsigned long mark[SYNC_MARKS];
5439 sector_t mark_cnt[SYNC_MARKS];
5440 int last_mark,m;
5441 struct list_head *tmp;
5442 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005443 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005444 struct list_head *rtmp;
5445 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005446 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447
5448 /* just incase thread restarts... */
5449 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5450 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005451 if (mddev->ro) /* never try to sync a read-only array */
5452 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453
NeilBrown61df9d92006-10-03 01:15:57 -07005454 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5455 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5456 desc = "data-check";
5457 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5458 desc = "requested-resync";
5459 else
5460 desc = "resync";
5461 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5462 desc = "reshape";
5463 else
5464 desc = "recovery";
5465
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 /* we overload curr_resync somewhat here.
5467 * 0 == not engaged in resync at all
5468 * 2 == checking that there is no conflict with another sync
5469 * 1 == like 2, but have yielded to allow conflicting resync to
5470 * commense
5471 * other == active in resync - this many blocks
5472 *
5473 * Before starting a resync we must have set curr_resync to
5474 * 2, and then checked that every "conflicting" array has curr_resync
5475 * less than ours. When we find one that is the same or higher
5476 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5477 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5478 * This will mean we have to start checking from the beginning again.
5479 *
5480 */
5481
5482 do {
5483 mddev->curr_resync = 2;
5484
5485 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005486 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005487 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 goto skip;
5489 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005490 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 if (mddev2 == mddev)
5492 continue;
5493 if (mddev2->curr_resync &&
5494 match_mddev_units(mddev,mddev2)) {
5495 DEFINE_WAIT(wq);
5496 if (mddev < mddev2 && mddev->curr_resync == 2) {
5497 /* arbitrarily yield */
5498 mddev->curr_resync = 1;
5499 wake_up(&resync_wait);
5500 }
5501 if (mddev > mddev2 && mddev->curr_resync == 1)
5502 /* no need to wait here, we can wait the next
5503 * time 'round when curr_resync == 2
5504 */
5505 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005506 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5507 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005508 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005509 printk(KERN_INFO "md: delaying %s of %s"
5510 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005512 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 mddev_put(mddev2);
5514 schedule();
5515 finish_wait(&resync_wait, &wq);
5516 goto try_again;
5517 }
5518 finish_wait(&resync_wait, &wq);
5519 }
5520 }
5521 } while (mddev->curr_resync < 2);
5522
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005523 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005524 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005526 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 */
5528 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005529 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005530 /* we don't use the checkpoint if there's a bitmap */
5531 if (!mddev->bitmap &&
5532 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5533 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005534 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5535 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005536 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 /* recovery follows the physical size of devices */
5538 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005539 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005540 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005541 if (rdev->raid_disk >= 0 &&
5542 !test_bit(Faulty, &rdev->flags) &&
5543 !test_bit(In_sync, &rdev->flags) &&
5544 rdev->recovery_offset < j)
5545 j = rdev->recovery_offset;
5546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547
NeilBrown61df9d92006-10-03 01:15:57 -07005548 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5549 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5550 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005551 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005552 "(but not more than %d KB/sec) for %s.\n",
5553 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
5555 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005556
NeilBrown57afd892005-06-21 17:17:13 -07005557 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 for (m = 0; m < SYNC_MARKS; m++) {
5559 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005560 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 }
5562 last_mark = 0;
5563 mddev->resync_mark = mark[last_mark];
5564 mddev->resync_mark_cnt = mark_cnt[last_mark];
5565
5566 /*
5567 * Tune reconstruction:
5568 */
5569 window = 32*(PAGE_SIZE/512);
5570 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5571 window/2,(unsigned long long) max_sectors/2);
5572
5573 atomic_set(&mddev->recovery_active, 0);
5574 init_waitqueue_head(&mddev->recovery_wait);
5575 last_check = 0;
5576
5577 if (j>2) {
5578 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005579 "md: resuming %s of %s from checkpoint.\n",
5580 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 mddev->curr_resync = j;
5582 }
5583
5584 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005585 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586
NeilBrown57afd892005-06-21 17:17:13 -07005587 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005588 if (j >= mddev->resync_max) {
5589 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5590 wait_event(mddev->recovery_wait,
5591 mddev->resync_max > j
5592 || kthread_should_stop());
5593 }
5594 if (kthread_should_stop())
5595 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005596 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005597 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005598 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5600 goto out;
5601 }
NeilBrown57afd892005-06-21 17:17:13 -07005602
5603 if (!skipped) { /* actual IO requested */
5604 io_sectors += sectors;
5605 atomic_add(sectors, &mddev->recovery_active);
5606 }
5607
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 j += sectors;
5609 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005610 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005611 if (last_check == 0)
5612 /* this is the earliers that rebuilt will be
5613 * visible in /proc/mdstat
5614 */
5615 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005616
5617 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 continue;
5619
NeilBrown57afd892005-06-21 17:17:13 -07005620 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621
5622 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5623 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5624 break;
5625
5626 repeat:
5627 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5628 /* step marks */
5629 int next = (last_mark+1) % SYNC_MARKS;
5630
5631 mddev->resync_mark = mark[next];
5632 mddev->resync_mark_cnt = mark_cnt[next];
5633 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005634 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 last_mark = next;
5636 }
5637
5638
NeilBrownc6207272008-02-06 01:39:52 -08005639 if (kthread_should_stop())
5640 goto interrupted;
5641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
5643 /*
5644 * this loop exits only if either when we are slower than
5645 * the 'hard' speed limit, or the system was IO-idle for
5646 * a jiffy.
5647 * the system might be non-idle CPU-wise, but we only care
5648 * about not overloading the IO subsystem. (things like an
5649 * e2fsck being done on the RAID array should execute fast)
5650 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005651 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 cond_resched();
5653
NeilBrown57afd892005-06-21 17:17:13 -07005654 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5655 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656
NeilBrown88202a02006-01-06 00:21:36 -08005657 if (currspeed > speed_min(mddev)) {
5658 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005660 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 goto repeat;
5662 }
5663 }
5664 }
NeilBrown61df9d92006-10-03 01:15:57 -07005665 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 /*
5667 * this also signals 'finished resyncing' to md_stop
5668 */
5669 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005670 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
5672 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5673
5674 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005675 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676
5677 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005678 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005679 mddev->curr_resync > 2) {
5680 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5681 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5682 if (mddev->curr_resync >= mddev->recovery_cp) {
5683 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005684 "md: checkpointing %s of %s.\n",
5685 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005686 mddev->recovery_cp = mddev->curr_resync;
5687 }
5688 } else
5689 mddev->recovery_cp = MaxSector;
5690 } else {
5691 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5692 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005693 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005694 if (rdev->raid_disk >= 0 &&
5695 !test_bit(Faulty, &rdev->flags) &&
5696 !test_bit(In_sync, &rdev->flags) &&
5697 rdev->recovery_offset < mddev->curr_resync)
5698 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 }
NeilBrown17571282006-12-10 02:20:52 -08005701 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 skip:
5704 mddev->curr_resync = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005705 mddev->resync_max = MaxSector;
5706 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 wake_up(&resync_wait);
5708 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5709 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005710 return;
5711
5712 interrupted:
5713 /*
5714 * got a signal, exit.
5715 */
5716 printk(KERN_INFO
5717 "md: md_do_sync() got signal ... exiting\n");
5718 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5719 goto out;
5720
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721}
NeilBrown29269552006-03-27 01:18:10 -08005722EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723
5724
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005725static int remove_and_add_spares(mddev_t *mddev)
5726{
5727 mdk_rdev_t *rdev;
5728 struct list_head *rtmp;
5729 int spares = 0;
5730
NeilBrownd089c6a2008-02-06 01:39:59 -08005731 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005732 if (rdev->raid_disk >= 0 &&
NeilBrownc303da62008-02-06 01:39:51 -08005733 !mddev->external &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005734 (test_bit(Faulty, &rdev->flags) ||
5735 ! test_bit(In_sync, &rdev->flags)) &&
5736 atomic_read(&rdev->nr_pending)==0) {
5737 if (mddev->pers->hot_remove_disk(
5738 mddev, rdev->raid_disk)==0) {
5739 char nm[20];
5740 sprintf(nm,"rd%d", rdev->raid_disk);
5741 sysfs_remove_link(&mddev->kobj, nm);
5742 rdev->raid_disk = -1;
5743 }
5744 }
5745
5746 if (mddev->degraded) {
NeilBrownd089c6a2008-02-06 01:39:59 -08005747 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005748 if (rdev->raid_disk < 0
5749 && !test_bit(Faulty, &rdev->flags)) {
5750 rdev->recovery_offset = 0;
5751 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5752 char nm[20];
5753 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005754 if (sysfs_create_link(&mddev->kobj,
5755 &rdev->kobj, nm))
5756 printk(KERN_WARNING
5757 "md: cannot register "
5758 "%s for %s\n",
5759 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005760 spares++;
5761 md_new_event(mddev);
5762 } else
5763 break;
5764 }
5765 }
5766 return spares;
5767}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768/*
5769 * This routine is regularly called by all per-raid-array threads to
5770 * deal with generic issues like resync and super-block update.
5771 * Raid personalities that don't have a thread (linear/raid0) do not
5772 * need this as they never do any recovery or update the superblock.
5773 *
5774 * It does not do any resync itself, but rather "forks" off other threads
5775 * to do that as needed.
5776 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5777 * "->recovery" and create a thread at ->sync_thread.
5778 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5779 * and wakeups up this thread which will reap the thread and finish up.
5780 * This thread also removes any faulty devices (with nr_pending == 0).
5781 *
5782 * The overall approach is:
5783 * 1/ if the superblock needs updating, update it.
5784 * 2/ If a recovery thread is running, don't do anything else.
5785 * 3/ If recovery has finished, clean up, possibly marking spares active.
5786 * 4/ If there are any faulty devices, remove them.
5787 * 5/ If array is degraded, try to add spares devices
5788 * 6/ If array has spares or is not in-sync, start a resync thread.
5789 */
5790void md_check_recovery(mddev_t *mddev)
5791{
5792 mdk_rdev_t *rdev;
5793 struct list_head *rtmp;
5794
5795
NeilBrown5f404022005-06-21 17:17:16 -07005796 if (mddev->bitmap)
5797 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
5799 if (mddev->ro)
5800 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005801
5802 if (signal_pending(current)) {
5803 if (mddev->pers->sync_request) {
5804 printk(KERN_INFO "md: %s in immediate safe mode\n",
5805 mdname(mddev));
5806 mddev->safemode = 2;
5807 }
5808 flush_signals(current);
5809 }
5810
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08005812 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005814 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5815 (mddev->safemode == 1) ||
5816 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5817 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005818 ))
5819 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005820
NeilBrowndf5b89b2006-03-27 01:18:20 -08005821 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005822 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005823
NeilBrowna9701a32005-11-08 21:39:34 -08005824 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005825 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5826 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5827 mddev->in_sync = 1;
NeilBrowne6910632008-02-06 01:39:51 -08005828 if (mddev->persistent)
5829 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005830 }
5831 if (mddev->safemode == 1)
5832 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005833 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005834
NeilBrown850b2b42006-10-03 01:15:46 -07005835 if (mddev->flags)
5836 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005837
NeilBrown06d91a52005-06-21 17:17:12 -07005838
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5840 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5841 /* resync/recovery still happening */
5842 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5843 goto unlock;
5844 }
5845 if (mddev->sync_thread) {
5846 /* resync has finished, collect result */
5847 md_unregister_thread(mddev->sync_thread);
5848 mddev->sync_thread = NULL;
5849 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5850 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5851 /* success...*/
5852 /* activate any spares */
5853 mddev->pers->spare_active(mddev);
5854 }
NeilBrown850b2b42006-10-03 01:15:46 -07005855 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005856
5857 /* if array is no-longer degraded, then any saved_raid_disk
5858 * information must be scrapped
5859 */
5860 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08005861 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07005862 rdev->saved_raid_disk = -1;
5863
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 mddev->recovery = 0;
5865 /* flag recovery needed just to double check */
5866 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005867 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 goto unlock;
5869 }
NeilBrown24dd4692005-11-08 21:39:26 -08005870 /* Clear some bits that don't mean anything, but
5871 * might be left set
5872 */
5873 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5874 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5875 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5876 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005878 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5879 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 /* no recovery is running.
5881 * remove any failed drives, then
5882 * add spares if possible.
5883 * Spare are also removed and re-added, to allow
5884 * the personality to fail the re-add.
5885 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005887 if (mddev->reshape_position != MaxSector) {
5888 if (mddev->pers->check_reshape(mddev) != 0)
5889 /* Cannot proceed */
5890 goto unlock;
5891 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5892 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005893 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5894 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5895 } else if (mddev->recovery_cp < MaxSector) {
5896 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5897 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5898 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005900
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 if (mddev->pers->sync_request) {
5902 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005903 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5904 /* We are adding a device or devices to an array
5905 * which has the bitmap stored on all devices.
5906 * So make sure all bitmap pages get written
5907 */
5908 bitmap_write_all(mddev->bitmap);
5909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 mddev->sync_thread = md_register_thread(md_do_sync,
5911 mddev,
5912 "%s_resync");
5913 if (!mddev->sync_thread) {
5914 printk(KERN_ERR "%s: could not start resync"
5915 " thread...\n",
5916 mdname(mddev));
5917 /* leave the spares where they are, it shouldn't hurt */
5918 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005919 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005921 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 }
5923 unlock:
5924 mddev_unlock(mddev);
5925 }
5926}
5927
Adrian Bunk75c96f82005-05-05 16:16:09 -07005928static int md_notify_reboot(struct notifier_block *this,
5929 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930{
5931 struct list_head *tmp;
5932 mddev_t *mddev;
5933
5934 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5935
5936 printk(KERN_INFO "md: stopping all md devices.\n");
5937
NeilBrown29ac4aa2008-02-06 01:39:58 -08005938 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005939 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005941 mddev_unlock(mddev);
5942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 /*
5944 * certain more exotic SCSI devices are known to be
5945 * volatile wrt too early system reboots. While the
5946 * right place to handle this issue is the given
5947 * driver, we do want to have a safe RAID driver ...
5948 */
5949 mdelay(1000*1);
5950 }
5951 return NOTIFY_DONE;
5952}
5953
Adrian Bunk75c96f82005-05-05 16:16:09 -07005954static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 .notifier_call = md_notify_reboot,
5956 .next = NULL,
5957 .priority = INT_MAX, /* before any real devices */
5958};
5959
5960static void md_geninit(void)
5961{
5962 struct proc_dir_entry *p;
5963
5964 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5965
5966 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5967 if (p)
5968 p->proc_fops = &md_seq_fops;
5969}
5970
Adrian Bunk75c96f82005-05-05 16:16:09 -07005971static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 if (register_blkdev(MAJOR_NR, "md"))
5974 return -1;
5975 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5976 unregister_blkdev(MAJOR_NR, "md");
5977 return -1;
5978 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005979 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5980 md_probe, NULL, NULL);
5981 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 md_probe, NULL, NULL);
5983
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005985 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
5987 md_geninit();
5988 return (0);
5989}
5990
5991
5992#ifndef MODULE
5993
5994/*
5995 * Searches all registered partitions for autorun RAID arrays
5996 * at boot time.
5997 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005998
5999static LIST_HEAD(all_detected_devices);
6000struct detected_devices_node {
6001 struct list_head list;
6002 dev_t dev;
6003};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004
6005void md_autodetect_dev(dev_t dev)
6006{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006007 struct detected_devices_node *node_detected_dev;
6008
6009 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6010 if (node_detected_dev) {
6011 node_detected_dev->dev = dev;
6012 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6013 } else {
6014 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6015 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017}
6018
6019
6020static void autostart_arrays(int part)
6021{
6022 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006023 struct detected_devices_node *node_detected_dev;
6024 dev_t dev;
6025 int i_scanned, i_passed;
6026
6027 i_scanned = 0;
6028 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029
6030 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6031
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006032 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6033 i_scanned++;
6034 node_detected_dev = list_entry(all_detected_devices.next,
6035 struct detected_devices_node, list);
6036 list_del(&node_detected_dev->list);
6037 dev = node_detected_dev->dev;
6038 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006039 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 if (IS_ERR(rdev))
6041 continue;
6042
NeilBrownb2d444d2005-11-08 21:39:31 -08006043 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 MD_BUG();
6045 continue;
6046 }
NeilBrownd0fae182008-03-04 14:29:31 -08006047 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006049 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006051
6052 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6053 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054
6055 autorun_devices(part);
6056}
6057
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006058#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059
6060static __exit void md_exit(void)
6061{
6062 mddev_t *mddev;
6063 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006064
NeilBrowne8703fe2006-10-03 01:15:59 -07006065 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6066 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067
6068 unregister_blkdev(MAJOR_NR,"md");
6069 unregister_blkdev(mdp_major, "mdp");
6070 unregister_reboot_notifier(&md_notifier);
6071 unregister_sysctl_table(raid_table_header);
6072 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006073 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 struct gendisk *disk = mddev->gendisk;
6075 if (!disk)
6076 continue;
6077 export_array(mddev);
6078 del_gendisk(disk);
6079 put_disk(disk);
6080 mddev->gendisk = NULL;
6081 mddev_put(mddev);
6082 }
6083}
6084
Dan Williams685784a2007-07-09 11:56:42 -07006085subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086module_exit(md_exit)
6087
NeilBrownf91de922005-11-08 21:39:36 -08006088static int get_ro(char *buffer, struct kernel_param *kp)
6089{
6090 return sprintf(buffer, "%d", start_readonly);
6091}
6092static int set_ro(const char *val, struct kernel_param *kp)
6093{
6094 char *e;
6095 int num = simple_strtoul(val, &e, 10);
6096 if (*val && (*e == '\0' || *e == '\n')) {
6097 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006098 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006099 }
6100 return -EINVAL;
6101}
6102
NeilBrown80ca3a42006-07-10 04:44:18 -07006103module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6104module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006105
NeilBrownf91de922005-11-08 21:39:36 -08006106
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107EXPORT_SYMBOL(register_md_personality);
6108EXPORT_SYMBOL(unregister_md_personality);
6109EXPORT_SYMBOL(md_error);
6110EXPORT_SYMBOL(md_done_sync);
6111EXPORT_SYMBOL(md_write_start);
6112EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113EXPORT_SYMBOL(md_register_thread);
6114EXPORT_SYMBOL(md_unregister_thread);
6115EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116EXPORT_SYMBOL(md_check_recovery);
6117MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006118MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006119MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);