blob: 83eb78b00137e98f6624bf72247d8a20bed1f870 [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;
NeilBrownd897dbf2008-04-30 00:52:29 -0700279 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 new->queue = blk_alloc_queue(GFP_KERNEL);
282 if (!new->queue) {
283 kfree(new);
284 return NULL;
285 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200286 /* Can be unlocked because the queue is new: no concurrency */
287 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 blk_queue_make_request(new->queue, md_fail_request);
290
291 goto retry;
292}
293
294static inline int mddev_lock(mddev_t * mddev)
295{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800296 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static inline int mddev_trylock(mddev_t * mddev)
300{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800301 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static inline void mddev_unlock(mddev_t * mddev)
305{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800306 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
NeilBrown005eca52005-08-22 13:11:08 -0700308 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
NeilBrown2989ddb2006-01-06 00:20:43 -0800311static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 mdk_rdev_t * rdev;
314 struct list_head *tmp;
315
NeilBrownd089c6a2008-02-06 01:39:59 -0800316 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (rdev->desc_nr == nr)
318 return rdev;
319 }
320 return NULL;
321}
322
323static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
324{
325 struct list_head *tmp;
326 mdk_rdev_t *rdev;
327
NeilBrownd089c6a2008-02-06 01:39:59 -0800328 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (rdev->bdev->bd_dev == dev)
330 return rdev;
331 }
332 return NULL;
333}
334
NeilBrownd9d166c2006-01-06 00:20:51 -0800335static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800336{
337 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800338 list_for_each_entry(pers, &pers_list, list) {
339 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800340 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800341 if (strcmp(pers->name, clevel)==0)
342 return pers;
343 }
NeilBrown2604b702006-01-06 00:20:36 -0800344 return NULL;
345}
346
Jesper Juhl77933d72005-07-27 11:46:09 -0700347static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
350 return MD_NEW_SIZE_BLOCKS(size);
351}
352
353static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
354{
355 sector_t size;
356
357 size = rdev->sb_offset;
358
359 if (chunk_size)
360 size &= ~((sector_t)chunk_size/1024 - 1);
361 return size;
362}
363
364static int alloc_disk_sb(mdk_rdev_t * rdev)
365{
366 if (rdev->sb_page)
367 MD_BUG();
368
369 rdev->sb_page = alloc_page(GFP_KERNEL);
370 if (!rdev->sb_page) {
371 printk(KERN_ALERT "md: out of memory.\n");
372 return -EINVAL;
373 }
374
375 return 0;
376}
377
378static void free_disk_sb(mdk_rdev_t * rdev)
379{
380 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800381 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 rdev->sb_loaded = 0;
383 rdev->sb_page = NULL;
384 rdev->sb_offset = 0;
385 rdev->size = 0;
386 }
387}
388
389
NeilBrown6712ecf2007-09-27 12:47:43 +0200390static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700391{
392 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800393 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700394
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700395 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
396 printk("md: super_written gets error=%d, uptodate=%d\n",
397 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
398 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800399 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700400 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700401
NeilBrowna9701a32005-11-08 21:39:34 -0800402 if (atomic_dec_and_test(&mddev->pending_writes))
403 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700404 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700405}
406
NeilBrown6712ecf2007-09-27 12:47:43 +0200407static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800408{
409 struct bio *bio2 = bio->bi_private;
410 mdk_rdev_t *rdev = bio2->bi_private;
411 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800412
413 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
414 error == -EOPNOTSUPP) {
415 unsigned long flags;
416 /* barriers don't appear to be supported :-( */
417 set_bit(BarriersNotsupp, &rdev->flags);
418 mddev->barriers_work = 0;
419 spin_lock_irqsave(&mddev->write_lock, flags);
420 bio2->bi_next = mddev->biolist;
421 mddev->biolist = bio2;
422 spin_unlock_irqrestore(&mddev->write_lock, flags);
423 wake_up(&mddev->sb_wait);
424 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200425 } else {
426 bio_put(bio2);
427 bio->bi_private = rdev;
428 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800429 }
NeilBrowna9701a32005-11-08 21:39:34 -0800430}
431
NeilBrown7bfa19f2005-06-21 17:17:28 -0700432void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
433 sector_t sector, int size, struct page *page)
434{
435 /* write first size bytes of page to sector of rdev
436 * Increment mddev->pending_writes before returning
437 * and decrement it on completion, waking up sb_wait
438 * if zero is reached.
439 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800440 *
441 * As we might need to resubmit the request if BIO_RW_BARRIER
442 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700443 */
444 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800445 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700446
447 bio->bi_bdev = rdev->bdev;
448 bio->bi_sector = sector;
449 bio_add_page(bio, page, size, 0);
450 bio->bi_private = rdev;
451 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800452 bio->bi_rw = rw;
453
NeilBrown7bfa19f2005-06-21 17:17:28 -0700454 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800455 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
456 struct bio *rbio;
457 rw |= (1<<BIO_RW_BARRIER);
458 rbio = bio_clone(bio, GFP_NOIO);
459 rbio->bi_private = bio;
460 rbio->bi_end_io = super_written_barrier;
461 submit_bio(rw, rbio);
462 } else
463 submit_bio(rw, bio);
464}
465
466void md_super_wait(mddev_t *mddev)
467{
468 /* wait for all superblock writes that were scheduled to complete.
469 * if any had to be retried (due to BARRIER problems), retry them
470 */
471 DEFINE_WAIT(wq);
472 for(;;) {
473 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
474 if (atomic_read(&mddev->pending_writes)==0)
475 break;
476 while (mddev->biolist) {
477 struct bio *bio;
478 spin_lock_irq(&mddev->write_lock);
479 bio = mddev->biolist;
480 mddev->biolist = bio->bi_next ;
481 bio->bi_next = NULL;
482 spin_unlock_irq(&mddev->write_lock);
483 submit_bio(bio->bi_rw, bio);
484 }
485 schedule();
486 }
487 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700488}
489
NeilBrown6712ecf2007-09-27 12:47:43 +0200490static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
NeilBrowna654b9d82005-06-21 17:17:27 -0700495int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct page *page, int rw)
497{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700498 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct completion event;
500 int ret;
501
502 rw |= (1 << BIO_RW_SYNC);
503
504 bio->bi_bdev = bdev;
505 bio->bi_sector = sector;
506 bio_add_page(bio, page, size, 0);
507 init_completion(&event);
508 bio->bi_private = &event;
509 bio->bi_end_io = bi_complete;
510 submit_bio(rw, bio);
511 wait_for_completion(&event);
512
513 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
514 bio_put(bio);
515 return ret;
516}
NeilBrowna8745db2006-01-06 00:20:34 -0800517EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
NeilBrown0002b272005-09-09 16:23:53 -0700519static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 char b[BDEVNAME_SIZE];
522 if (!rdev->sb_page) {
523 MD_BUG();
524 return -EINVAL;
525 }
526 if (rdev->sb_loaded)
527 return 0;
528
529
NeilBrown0002b272005-09-09 16:23:53 -0700530 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto fail;
532 rdev->sb_loaded = 1;
533 return 0;
534
535fail:
536 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
537 bdevname(rdev->bdev,b));
538 return -EINVAL;
539}
540
541static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
542{
543 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
544 (sb1->set_uuid1 == sb2->set_uuid1) &&
545 (sb1->set_uuid2 == sb2->set_uuid2) &&
546 (sb1->set_uuid3 == sb2->set_uuid3))
547
548 return 1;
549
550 return 0;
551}
552
553
554static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
555{
556 int ret;
557 mdp_super_t *tmp1, *tmp2;
558
559 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
560 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
561
562 if (!tmp1 || !tmp2) {
563 ret = 0;
564 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
565 goto abort;
566 }
567
568 *tmp1 = *sb1;
569 *tmp2 = *sb2;
570
571 /*
572 * nr_disks is not constant
573 */
574 tmp1->nr_disks = 0;
575 tmp2->nr_disks = 0;
576
577 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
578 ret = 0;
579 else
580 ret = 1;
581
582abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700583 kfree(tmp1);
584 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return ret;
586}
587
NeilBrown4d167f02007-05-09 02:35:37 -0700588
589static u32 md_csum_fold(u32 csum)
590{
591 csum = (csum & 0xffff) + (csum >> 16);
592 return (csum & 0xffff) + (csum >> 16);
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595static unsigned int calc_sb_csum(mdp_super_t * sb)
596{
NeilBrown4d167f02007-05-09 02:35:37 -0700597 u64 newcsum = 0;
598 u32 *sb32 = (u32*)sb;
599 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 unsigned int disk_csum, csum;
601
602 disk_csum = sb->sb_csum;
603 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700604
605 for (i = 0; i < MD_SB_BYTES/4 ; i++)
606 newcsum += sb32[i];
607 csum = (newcsum & 0xffffffff) + (newcsum>>32);
608
609
610#ifdef CONFIG_ALPHA
611 /* This used to use csum_partial, which was wrong for several
612 * reasons including that different results are returned on
613 * different architectures. It isn't critical that we get exactly
614 * the same return value as before (we always csum_fold before
615 * testing, and that removes any differences). However as we
616 * know that csum_partial always returned a 16bit value on
617 * alphas, do a fold to maximise conformity to previous behaviour.
618 */
619 sb->sb_csum = md_csum_fold(disk_csum);
620#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700622#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return csum;
624}
625
626
627/*
628 * Handle superblock details.
629 * We want to be able to handle multiple superblock formats
630 * so we have a common interface to them all, and an array of
631 * different handlers.
632 * We rely on user-space to write the initial superblock, and support
633 * reading and updating of superblocks.
634 * Interface methods are:
635 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
636 * loads and validates a superblock on dev.
637 * if refdev != NULL, compare superblocks on both devices
638 * Return:
639 * 0 - dev has a superblock that is compatible with refdev
640 * 1 - dev has a superblock that is compatible and newer than refdev
641 * so dev should be used as the refdev in future
642 * -EINVAL superblock incompatible or invalid
643 * -othererror e.g. -EIO
644 *
645 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
646 * Verify that dev is acceptable into mddev.
647 * The first time, mddev->raid_disks will be 0, and data from
648 * dev should be merged in. Subsequent calls check that dev
649 * is new enough. Return 0 or -EINVAL
650 *
651 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
652 * Update the superblock for rdev with data in mddev
653 * This does not write to disc.
654 *
655 */
656
657struct super_type {
658 char *name;
659 struct module *owner;
660 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
661 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
662 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
663};
664
665/*
666 * load_super for 0.90.0
667 */
668static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
669{
670 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
671 mdp_super_t *sb;
672 int ret;
673 sector_t sb_offset;
674
675 /*
676 * Calculate the position of the superblock,
677 * it's at the end of the disk.
678 *
679 * It also happens to be a multiple of 4Kb.
680 */
681 sb_offset = calc_dev_sboffset(rdev->bdev);
682 rdev->sb_offset = sb_offset;
683
NeilBrown0002b272005-09-09 16:23:53 -0700684 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (ret) return ret;
686
687 ret = -EINVAL;
688
689 bdevname(rdev->bdev, b);
690 sb = (mdp_super_t*)page_address(rdev->sb_page);
691
692 if (sb->md_magic != MD_SB_MAGIC) {
693 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
694 b);
695 goto abort;
696 }
697
698 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800699 sb->minor_version < 90 ||
700 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
702 sb->major_version, sb->minor_version,
703 b);
704 goto abort;
705 }
706
707 if (sb->raid_disks <= 0)
708 goto abort;
709
NeilBrown4d167f02007-05-09 02:35:37 -0700710 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
712 b);
713 goto abort;
714 }
715
716 rdev->preferred_minor = sb->md_minor;
717 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700718 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
NeilBrowne11e93f2007-05-09 02:35:36 -0700720 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
721 if (sb->level != 1 && sb->level != 4
722 && sb->level != 5 && sb->level != 6
723 && sb->level != 10) {
724 /* FIXME use a better test */
725 printk(KERN_WARNING
726 "md: bitmaps not supported for this level.\n");
727 goto abort;
728 }
729 }
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (sb->level == LEVEL_MULTIPATH)
732 rdev->desc_nr = -1;
733 else
734 rdev->desc_nr = sb->this_disk.number;
735
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700736 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700738 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 __u64 ev1, ev2;
740 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
741 if (!uuid_equal(refsb, sb)) {
742 printk(KERN_WARNING "md: %s has different UUID to %s\n",
743 b, bdevname(refdev->bdev,b2));
744 goto abort;
745 }
746 if (!sb_equal(refsb, sb)) {
747 printk(KERN_WARNING "md: %s has same UUID"
748 " but different superblock to %s\n",
749 b, bdevname(refdev->bdev, b2));
750 goto abort;
751 }
752 ev1 = md_event(sb);
753 ev2 = md_event(refsb);
754 if (ev1 > ev2)
755 ret = 1;
756 else
757 ret = 0;
758 }
759 rdev->size = calc_dev_size(rdev, sb->chunk_size);
760
NeilBrown2bf071b2006-01-06 00:20:55 -0800761 if (rdev->size < sb->size && sb->level > 1)
762 /* "this cannot possibly happen" ... */
763 ret = -EINVAL;
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 abort:
766 return ret;
767}
768
769/*
770 * validate_super for 0.90.0
771 */
772static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
773{
774 mdp_disk_t *desc;
775 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700776 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
NeilBrown41158c72005-06-21 17:17:25 -0700778 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800779 clear_bit(Faulty, &rdev->flags);
780 clear_bit(In_sync, &rdev->flags);
781 clear_bit(WriteMostly, &rdev->flags);
782 clear_bit(BarriersNotsupp, &rdev->flags);
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (mddev->raid_disks == 0) {
785 mddev->major_version = 0;
786 mddev->minor_version = sb->minor_version;
787 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800788 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 mddev->chunk_size = sb->chunk_size;
790 mddev->ctime = sb->ctime;
791 mddev->utime = sb->utime;
792 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800793 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 mddev->layout = sb->layout;
795 mddev->raid_disks = sb->raid_disks;
796 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700797 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700798 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700799 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
NeilBrownf6705572006-03-27 01:18:11 -0800801 if (mddev->minor_version >= 91) {
802 mddev->reshape_position = sb->reshape_position;
803 mddev->delta_disks = sb->delta_disks;
804 mddev->new_level = sb->new_level;
805 mddev->new_layout = sb->new_layout;
806 mddev->new_chunk = sb->new_chunk;
807 } else {
808 mddev->reshape_position = MaxSector;
809 mddev->delta_disks = 0;
810 mddev->new_level = mddev->level;
811 mddev->new_layout = mddev->layout;
812 mddev->new_chunk = mddev->chunk_size;
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (sb->state & (1<<MD_SB_CLEAN))
816 mddev->recovery_cp = MaxSector;
817 else {
818 if (sb->events_hi == sb->cp_events_hi &&
819 sb->events_lo == sb->cp_events_lo) {
820 mddev->recovery_cp = sb->recovery_cp;
821 } else
822 mddev->recovery_cp = 0;
823 }
824
825 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
826 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
827 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
828 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
829
830 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700831
832 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700833 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700834 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700835
NeilBrown41158c72005-06-21 17:17:25 -0700836 } else if (mddev->pers == NULL) {
837 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 ++ev1;
839 if (ev1 < mddev->events)
840 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700841 } else if (mddev->bitmap) {
842 /* if adding to array with a bitmap, then we can accept an
843 * older device ... but not too old.
844 */
NeilBrown41158c72005-06-21 17:17:25 -0700845 if (ev1 < mddev->bitmap->events_cleared)
846 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700847 } else {
848 if (ev1 < mddev->events)
849 /* just a hot-add of a new device, leave raid_disk at -1 */
850 return 0;
851 }
NeilBrown41158c72005-06-21 17:17:25 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 desc = sb->disks + rdev->desc_nr;
855
856 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800857 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700858 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
859 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800860 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 rdev->raid_disk = desc->raid_disk;
862 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700863 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
864 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700865 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800866 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return 0;
868}
869
870/*
871 * sync_super for 0.90.0
872 */
873static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
874{
875 mdp_super_t *sb;
876 struct list_head *tmp;
877 mdk_rdev_t *rdev2;
878 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* make rdev->sb match mddev data..
882 *
883 * 1/ zero out disks
884 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
885 * 3/ any empty disks < next_spare become removed
886 *
887 * disks[0] gets initialised to REMOVED because
888 * we cannot be sure from other fields if it has
889 * been initialised or not.
890 */
891 int i;
892 int active=0, working=0,failed=0,spare=0,nr_disks=0;
893
NeilBrown61181562005-09-09 16:24:02 -0700894 rdev->sb_size = MD_SB_BYTES;
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 sb = (mdp_super_t*)page_address(rdev->sb_page);
897
898 memset(sb, 0, sizeof(*sb));
899
900 sb->md_magic = MD_SB_MAGIC;
901 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 sb->patch_version = mddev->patch_version;
903 sb->gvalid_words = 0; /* ignored */
904 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
905 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
906 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
907 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
908
909 sb->ctime = mddev->ctime;
910 sb->level = mddev->level;
911 sb->size = mddev->size;
912 sb->raid_disks = mddev->raid_disks;
913 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800914 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 sb->utime = mddev->utime;
916 sb->state = 0;
917 sb->events_hi = (mddev->events>>32);
918 sb->events_lo = (u32)mddev->events;
919
NeilBrownf6705572006-03-27 01:18:11 -0800920 if (mddev->reshape_position == MaxSector)
921 sb->minor_version = 90;
922 else {
923 sb->minor_version = 91;
924 sb->reshape_position = mddev->reshape_position;
925 sb->new_level = mddev->new_level;
926 sb->delta_disks = mddev->delta_disks;
927 sb->new_layout = mddev->new_layout;
928 sb->new_chunk = mddev->new_chunk;
929 }
930 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (mddev->in_sync)
932 {
933 sb->recovery_cp = mddev->recovery_cp;
934 sb->cp_events_hi = (mddev->events>>32);
935 sb->cp_events_lo = (u32)mddev->events;
936 if (mddev->recovery_cp == MaxSector)
937 sb->state = (1<< MD_SB_CLEAN);
938 } else
939 sb->recovery_cp = 0;
940
941 sb->layout = mddev->layout;
942 sb->chunk_size = mddev->chunk_size;
943
NeilBrowna654b9d82005-06-21 17:17:27 -0700944 if (mddev->bitmap && mddev->bitmap_file == NULL)
945 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800948 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800950 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800951 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
952 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800953 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800955 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800956 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 d = &sb->disks[rdev2->desc_nr];
958 nr_disks++;
959 d->number = rdev2->desc_nr;
960 d->major = MAJOR(rdev2->bdev->bd_dev);
961 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800962 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
963 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 d->raid_disk = rdev2->raid_disk;
965 else
966 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800967 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800969 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 d->state = (1<<MD_DISK_ACTIVE);
971 d->state |= (1<<MD_DISK_SYNC);
972 active++;
973 working++;
974 } else {
975 d->state = 0;
976 spare++;
977 working++;
978 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700979 if (test_bit(WriteMostly, &rdev2->flags))
980 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 /* now set the "removed" and "faulty" bits on any missing devices */
983 for (i=0 ; i < mddev->raid_disks ; i++) {
984 mdp_disk_t *d = &sb->disks[i];
985 if (d->state == 0 && d->number == 0) {
986 d->number = i;
987 d->raid_disk = i;
988 d->state = (1<<MD_DISK_REMOVED);
989 d->state |= (1<<MD_DISK_FAULTY);
990 failed++;
991 }
992 }
993 sb->nr_disks = nr_disks;
994 sb->active_disks = active;
995 sb->working_disks = working;
996 sb->failed_disks = failed;
997 sb->spare_disks = spare;
998
999 sb->this_disk = sb->disks[rdev->desc_nr];
1000 sb->sb_csum = calc_sb_csum(sb);
1001}
1002
1003/*
1004 * version 1 superblock
1005 */
1006
NeilBrown1c05b4b2006-10-21 10:24:08 -07001007static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001009 __le32 disk_csum;
1010 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 unsigned long long newcsum;
1012 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001013 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 int i;
1015
1016 disk_csum = sb->sb_csum;
1017 sb->sb_csum = 0;
1018 newcsum = 0;
1019 for (i=0; size>=4; size -= 4 )
1020 newcsum += le32_to_cpu(*isuper++);
1021
1022 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001023 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1026 sb->sb_csum = disk_csum;
1027 return cpu_to_le32(csum);
1028}
1029
1030static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1031{
1032 struct mdp_superblock_1 *sb;
1033 int ret;
1034 sector_t sb_offset;
1035 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001036 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /*
1039 * Calculate the position of the superblock.
1040 * It is always aligned to a 4K boundary and
1041 * depeding on minor_version, it can be:
1042 * 0: At least 8K, but less than 12K, from end of device
1043 * 1: At start of device
1044 * 2: 4K from start of device.
1045 */
1046 switch(minor_version) {
1047 case 0:
1048 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1049 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001050 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 /* convert from sectors to K */
1052 sb_offset /= 2;
1053 break;
1054 case 1:
1055 sb_offset = 0;
1056 break;
1057 case 2:
1058 sb_offset = 4;
1059 break;
1060 default:
1061 return -EINVAL;
1062 }
1063 rdev->sb_offset = sb_offset;
1064
NeilBrown0002b272005-09-09 16:23:53 -07001065 /* superblock is rarely larger than 1K, but it can be larger,
1066 * and it is safe to read 4k, so we do that
1067 */
1068 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 if (ret) return ret;
1070
1071
1072 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1073
1074 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1075 sb->major_version != cpu_to_le32(1) ||
1076 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1077 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001078 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return -EINVAL;
1080
1081 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1082 printk("md: invalid superblock checksum on %s\n",
1083 bdevname(rdev->bdev,b));
1084 return -EINVAL;
1085 }
1086 if (le64_to_cpu(sb->data_size) < 10) {
1087 printk("md: data_size too small on %s\n",
1088 bdevname(rdev->bdev,b));
1089 return -EINVAL;
1090 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001091 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1092 if (sb->level != cpu_to_le32(1) &&
1093 sb->level != cpu_to_le32(4) &&
1094 sb->level != cpu_to_le32(5) &&
1095 sb->level != cpu_to_le32(6) &&
1096 sb->level != cpu_to_le32(10)) {
1097 printk(KERN_WARNING
1098 "md: bitmaps not supported for this level.\n");
1099 return -EINVAL;
1100 }
1101 }
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 rdev->preferred_minor = 0xffff;
1104 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001105 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
NeilBrown0002b272005-09-09 16:23:53 -07001107 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001108 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001109 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001110 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1111
1112 if (minor_version
1113 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1114 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001115
NeilBrown31b65a02006-07-10 04:44:14 -07001116 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1117 rdev->desc_nr = -1;
1118 else
1119 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1120
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001121 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001122 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001123 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 __u64 ev1, ev2;
1125 struct mdp_superblock_1 *refsb =
1126 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1127
1128 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1129 sb->level != refsb->level ||
1130 sb->layout != refsb->layout ||
1131 sb->chunksize != refsb->chunksize) {
1132 printk(KERN_WARNING "md: %s has strangely different"
1133 " superblock to %s\n",
1134 bdevname(rdev->bdev,b),
1135 bdevname(refdev->bdev,b2));
1136 return -EINVAL;
1137 }
1138 ev1 = le64_to_cpu(sb->events);
1139 ev2 = le64_to_cpu(refsb->events);
1140
1141 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001142 ret = 1;
1143 else
1144 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
NeilBrowna1801f82008-03-04 14:29:31 -08001146 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1148 else
1149 rdev->size = rdev->sb_offset;
1150 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1151 return -EINVAL;
1152 rdev->size = le64_to_cpu(sb->data_size)/2;
1153 if (le32_to_cpu(sb->chunksize))
1154 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001155
NeilBrown1c05b4b2006-10-21 10:24:08 -07001156 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001157 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001158 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159}
1160
1161static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1162{
1163 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001164 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
NeilBrown41158c72005-06-21 17:17:25 -07001166 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001167 clear_bit(Faulty, &rdev->flags);
1168 clear_bit(In_sync, &rdev->flags);
1169 clear_bit(WriteMostly, &rdev->flags);
1170 clear_bit(BarriersNotsupp, &rdev->flags);
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (mddev->raid_disks == 0) {
1173 mddev->major_version = 1;
1174 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001175 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1177 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1178 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1179 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001180 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 mddev->layout = le32_to_cpu(sb->layout);
1182 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1183 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001184 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001185 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001186 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1189 memcpy(mddev->uuid, sb->set_uuid, 16);
1190
1191 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001192
NeilBrown71c08052005-09-09 16:23:51 -07001193 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001194 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001195 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001196
NeilBrownf6705572006-03-27 01:18:11 -08001197 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1198 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1199 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1200 mddev->new_level = le32_to_cpu(sb->new_level);
1201 mddev->new_layout = le32_to_cpu(sb->new_layout);
1202 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1203 } else {
1204 mddev->reshape_position = MaxSector;
1205 mddev->delta_disks = 0;
1206 mddev->new_level = mddev->level;
1207 mddev->new_layout = mddev->layout;
1208 mddev->new_chunk = mddev->chunk_size;
1209 }
1210
NeilBrown41158c72005-06-21 17:17:25 -07001211 } else if (mddev->pers == NULL) {
1212 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 ++ev1;
1214 if (ev1 < mddev->events)
1215 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001216 } else if (mddev->bitmap) {
1217 /* If adding to array with a bitmap, then we can accept an
1218 * older device, but not too old.
1219 */
NeilBrown41158c72005-06-21 17:17:25 -07001220 if (ev1 < mddev->bitmap->events_cleared)
1221 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001222 } else {
1223 if (ev1 < mddev->events)
1224 /* just a hot-add of a new device, leave raid_disk at -1 */
1225 return 0;
1226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (mddev->level != LEVEL_MULTIPATH) {
1228 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1230 switch(role) {
1231 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 break;
1233 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001234 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001237 if ((le32_to_cpu(sb->feature_map) &
1238 MD_FEATURE_RECOVERY_OFFSET))
1239 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1240 else
1241 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 rdev->raid_disk = role;
1243 break;
1244 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001245 if (sb->devflags & WriteMostly1)
1246 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001247 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001248 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return 0;
1251}
1252
1253static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1254{
1255 struct mdp_superblock_1 *sb;
1256 struct list_head *tmp;
1257 mdk_rdev_t *rdev2;
1258 int max_dev, i;
1259 /* make rdev->sb match mddev and rdev data. */
1260
1261 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1262
1263 sb->feature_map = 0;
1264 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001265 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 memset(sb->pad1, 0, sizeof(sb->pad1));
1267 memset(sb->pad2, 0, sizeof(sb->pad2));
1268 memset(sb->pad3, 0, sizeof(sb->pad3));
1269
1270 sb->utime = cpu_to_le64((__u64)mddev->utime);
1271 sb->events = cpu_to_le64(mddev->events);
1272 if (mddev->in_sync)
1273 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1274 else
1275 sb->resync_offset = cpu_to_le64(0);
1276
NeilBrown1c05b4b2006-10-21 10:24:08 -07001277 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001278
NeilBrownf0ca3402006-02-02 14:28:04 -08001279 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001280 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001281
NeilBrowna654b9d82005-06-21 17:17:27 -07001282 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1283 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001284 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001285 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001286
1287 if (rdev->raid_disk >= 0 &&
1288 !test_bit(In_sync, &rdev->flags) &&
1289 rdev->recovery_offset > 0) {
1290 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1291 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1292 }
1293
NeilBrownf6705572006-03-27 01:18:11 -08001294 if (mddev->reshape_position != MaxSector) {
1295 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1296 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1297 sb->new_layout = cpu_to_le32(mddev->new_layout);
1298 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1299 sb->new_level = cpu_to_le32(mddev->new_level);
1300 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1301 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001304 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (rdev2->desc_nr+1 > max_dev)
1306 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001307
1308 if (max_dev > le32_to_cpu(sb->max_dev))
1309 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 for (i=0; i<max_dev;i++)
1311 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1312
NeilBrownd089c6a2008-02-06 01:39:59 -08001313 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001315 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001317 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001319 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1320 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 else
1322 sb->dev_roles[i] = cpu_to_le16(0xffff);
1323 }
1324
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 sb->sb_csum = calc_sb_1_csum(sb);
1326}
1327
1328
Adrian Bunk75c96f82005-05-05 16:16:09 -07001329static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 [0] = {
1331 .name = "0.90.0",
1332 .owner = THIS_MODULE,
1333 .load_super = super_90_load,
1334 .validate_super = super_90_validate,
1335 .sync_super = super_90_sync,
1336 },
1337 [1] = {
1338 .name = "md-1",
1339 .owner = THIS_MODULE,
1340 .load_super = super_1_load,
1341 .validate_super = super_1_validate,
1342 .sync_super = super_1_sync,
1343 },
1344};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1347{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001348 struct list_head *tmp, *tmp2;
1349 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
NeilBrownd089c6a2008-02-06 01:39:59 -08001351 rdev_for_each(rdev, tmp, mddev1)
1352 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001353 if (rdev->bdev->bd_contains ==
1354 rdev2->bdev->bd_contains)
1355 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 return 0;
1358}
1359
1360static LIST_HEAD(pending_raid_disks);
1361
1362static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1363{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001364 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001365 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001366 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001367 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 if (rdev->mddev) {
1370 MD_BUG();
1371 return -EINVAL;
1372 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001373
1374 /* prevent duplicates */
1375 if (find_rdev(mddev, rdev->bdev->bd_dev))
1376 return -EEXIST;
1377
NeilBrown2bf071b2006-01-06 00:20:55 -08001378 /* make sure rdev->size exceeds mddev->size */
1379 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001380 if (mddev->pers) {
1381 /* Cannot change size, so fail
1382 * If mddev->level <= 0, then we don't care
1383 * about aligning sizes (e.g. linear)
1384 */
1385 if (mddev->level > 0)
1386 return -ENOSPC;
1387 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001388 mddev->size = rdev->size;
1389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
1391 /* Verify rdev->desc_nr is unique.
1392 * If it is -1, assign a free number, else
1393 * check number is not in use
1394 */
1395 if (rdev->desc_nr < 0) {
1396 int choice = 0;
1397 if (mddev->pers) choice = mddev->raid_disks;
1398 while (find_rdev_nr(mddev, choice))
1399 choice++;
1400 rdev->desc_nr = choice;
1401 } else {
1402 if (find_rdev_nr(mddev, rdev->desc_nr))
1403 return -EBUSY;
1404 }
NeilBrown19133a42005-11-08 21:39:35 -08001405 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001406 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001407 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001410 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001411
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001412 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001413 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001414
NeilBrownf637b9f2005-11-08 21:39:37 -08001415 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001416 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001417 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001418 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001419 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1420 kobject_del(&rdev->kobj);
1421 goto fail;
1422 }
1423 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001424 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001426
1427 fail:
1428 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1429 b, mdname(mddev));
1430 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
NeilBrown177a99b2008-02-06 01:39:56 -08001433static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001434{
1435 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1436 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001437 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001438}
1439
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1441{
1442 char b[BDEVNAME_SIZE];
1443 if (!rdev->mddev) {
1444 MD_BUG();
1445 return;
1446 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001447 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 list_del_init(&rdev->same_set);
1449 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1450 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001451 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001452
1453 /* We need to delay this, otherwise we can deadlock when
1454 * writing to 'remove' to "dev/state"
1455 */
NeilBrown177a99b2008-02-06 01:39:56 -08001456 INIT_WORK(&rdev->del_work, md_delayed_delete);
1457 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001458 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/*
1462 * prevent the device from being mounted, repartitioned or
1463 * otherwise reused by a RAID array (or any other kernel
1464 * subsystem), by bd_claiming the device.
1465 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001466static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
1468 int err = 0;
1469 struct block_device *bdev;
1470 char b[BDEVNAME_SIZE];
1471
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001472 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (IS_ERR(bdev)) {
1474 printk(KERN_ERR "md: could not open %s.\n",
1475 __bdevname(dev, b));
1476 return PTR_ERR(bdev);
1477 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001478 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 if (err) {
1480 printk(KERN_ERR "md: could not bd_claim %s.\n",
1481 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001482 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 return err;
1484 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001485 if (!shared)
1486 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 rdev->bdev = bdev;
1488 return err;
1489}
1490
1491static void unlock_rdev(mdk_rdev_t *rdev)
1492{
1493 struct block_device *bdev = rdev->bdev;
1494 rdev->bdev = NULL;
1495 if (!bdev)
1496 MD_BUG();
1497 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001498 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501void md_autodetect_dev(dev_t dev);
1502
1503static void export_rdev(mdk_rdev_t * rdev)
1504{
1505 char b[BDEVNAME_SIZE];
1506 printk(KERN_INFO "md: export_rdev(%s)\n",
1507 bdevname(rdev->bdev,b));
1508 if (rdev->mddev)
1509 MD_BUG();
1510 free_disk_sb(rdev);
1511 list_del_init(&rdev->same_set);
1512#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001513 if (test_bit(AutoDetected, &rdev->flags))
1514 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515#endif
1516 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001517 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
1520static void kick_rdev_from_array(mdk_rdev_t * rdev)
1521{
1522 unbind_rdev_from_array(rdev);
1523 export_rdev(rdev);
1524}
1525
1526static void export_array(mddev_t *mddev)
1527{
1528 struct list_head *tmp;
1529 mdk_rdev_t *rdev;
1530
NeilBrownd089c6a2008-02-06 01:39:59 -08001531 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (!rdev->mddev) {
1533 MD_BUG();
1534 continue;
1535 }
1536 kick_rdev_from_array(rdev);
1537 }
1538 if (!list_empty(&mddev->disks))
1539 MD_BUG();
1540 mddev->raid_disks = 0;
1541 mddev->major_version = 0;
1542}
1543
1544static void print_desc(mdp_disk_t *desc)
1545{
1546 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1547 desc->major,desc->minor,desc->raid_disk,desc->state);
1548}
1549
1550static void print_sb(mdp_super_t *sb)
1551{
1552 int i;
1553
1554 printk(KERN_INFO
1555 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1556 sb->major_version, sb->minor_version, sb->patch_version,
1557 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1558 sb->ctime);
1559 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1560 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1561 sb->md_minor, sb->layout, sb->chunk_size);
1562 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1563 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1564 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1565 sb->failed_disks, sb->spare_disks,
1566 sb->sb_csum, (unsigned long)sb->events_lo);
1567
1568 printk(KERN_INFO);
1569 for (i = 0; i < MD_SB_DISKS; i++) {
1570 mdp_disk_t *desc;
1571
1572 desc = sb->disks + i;
1573 if (desc->number || desc->major || desc->minor ||
1574 desc->raid_disk || (desc->state && (desc->state != 4))) {
1575 printk(" D %2d: ", i);
1576 print_desc(desc);
1577 }
1578 }
1579 printk(KERN_INFO "md: THIS: ");
1580 print_desc(&sb->this_disk);
1581
1582}
1583
1584static void print_rdev(mdk_rdev_t *rdev)
1585{
1586 char b[BDEVNAME_SIZE];
1587 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1588 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001589 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1590 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (rdev->sb_loaded) {
1592 printk(KERN_INFO "md: rdev superblock:\n");
1593 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1594 } else
1595 printk(KERN_INFO "md: no rdev superblock!\n");
1596}
1597
Adrian Bunk5e563412006-06-26 00:27:42 -07001598static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 struct list_head *tmp, *tmp2;
1601 mdk_rdev_t *rdev;
1602 mddev_t *mddev;
1603 char b[BDEVNAME_SIZE];
1604
1605 printk("\n");
1606 printk("md: **********************************\n");
1607 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1608 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001609 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
NeilBrown32a76272005-06-21 17:17:14 -07001611 if (mddev->bitmap)
1612 bitmap_print_sb(mddev->bitmap);
1613 else
1614 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001615 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 printk("<%s>", bdevname(rdev->bdev,b));
1617 printk("\n");
1618
NeilBrownd089c6a2008-02-06 01:39:59 -08001619 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 print_rdev(rdev);
1621 }
1622 printk("md: **********************************\n");
1623 printk("\n");
1624}
1625
1626
NeilBrown42543762006-06-26 00:27:57 -07001627static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
NeilBrown42543762006-06-26 00:27:57 -07001629 /* Update each superblock (in-memory image), but
1630 * if we are allowed to, skip spares which already
1631 * have the right event counter, or have one earlier
1632 * (which would mean they aren't being marked as dirty
1633 * with the rest of the array)
1634 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 mdk_rdev_t *rdev;
1636 struct list_head *tmp;
1637
NeilBrownd089c6a2008-02-06 01:39:59 -08001638 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001639 if (rdev->sb_events == mddev->events ||
1640 (nospares &&
1641 rdev->raid_disk < 0 &&
1642 (rdev->sb_events&1)==0 &&
1643 rdev->sb_events+1 == mddev->events)) {
1644 /* Don't update this superblock */
1645 rdev->sb_loaded = 2;
1646 } else {
1647 super_types[mddev->major_version].
1648 sync_super(mddev, rdev);
1649 rdev->sb_loaded = 1;
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652}
1653
NeilBrown850b2b42006-10-03 01:15:46 -07001654static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 struct list_head *tmp;
1657 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001658 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001659 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
NeilBrown8377bc82008-04-30 00:52:28 -07001661 if (mddev->external)
1662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001664 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001665
NeilBrown850b2b42006-10-03 01:15:46 -07001666 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1667 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1668 force_change = 1;
1669 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1670 /* just a clean<-> dirty transition, possibly leave spares alone,
1671 * though if events isn't the right even/odd, we will have to do
1672 * spares after all
1673 */
1674 nospares = 1;
1675 if (force_change)
1676 nospares = 0;
1677 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001678 /* If the array is degraded, then skipping spares is both
1679 * dangerous and fairly pointless.
1680 * Dangerous because a device that was removed from the array
1681 * might have a event_count that still looks up-to-date,
1682 * so it can be re-added without a resync.
1683 * Pointless because if there are any spares to skip,
1684 * then a recovery will happen and soon that array won't
1685 * be degraded any more and the spare can go back to sleep then.
1686 */
NeilBrown850b2b42006-10-03 01:15:46 -07001687 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001688
NeilBrown06d91a52005-06-21 17:17:12 -07001689 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001691
1692 /* If this is just a dirty<->clean transition, and the array is clean
1693 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001694 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001695 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001696 && (mddev->events & 1)
1697 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001698 mddev->events--;
1699 else {
1700 /* otherwise we have to go forward and ... */
1701 mddev->events ++;
1702 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1703 /* .. if the array isn't clean, insist on an odd 'events' */
1704 if ((mddev->events&1)==0) {
1705 mddev->events++;
1706 nospares = 0;
1707 }
1708 } else {
1709 /* otherwise insist on an even 'events' (for clean states) */
1710 if ((mddev->events&1)) {
1711 mddev->events++;
1712 nospares = 0;
1713 }
1714 }
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 if (!mddev->events) {
1718 /*
1719 * oops, this 64-bit counter should never wrap.
1720 * Either we are in around ~1 trillion A.C., assuming
1721 * 1 reboot per second, or we have a bug:
1722 */
1723 MD_BUG();
1724 mddev->events --;
1725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 /*
1728 * do not write anything to disk if using
1729 * nonpersistent superblocks
1730 */
NeilBrown06d91a52005-06-21 17:17:12 -07001731 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001732 if (!mddev->external)
1733 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1734
NeilBrowna9701a32005-11-08 21:39:34 -08001735 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001736 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001738 }
NeilBrowne6910632008-02-06 01:39:51 -08001739 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001740 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 dprintk(KERN_INFO
1743 "md: updating %s RAID superblock on device (in sync %d)\n",
1744 mdname(mddev),mddev->in_sync);
1745
NeilBrown4ad13662007-07-17 04:06:13 -07001746 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001747 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 char b[BDEVNAME_SIZE];
1749 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001750 if (rdev->sb_loaded != 1)
1751 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001752 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 dprintk("(skipping faulty ");
1754
1755 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001756 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001757 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001758 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001759 rdev->sb_page);
1760 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1761 bdevname(rdev->bdev,b),
1762 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001763 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 } else
1766 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001767 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 /* only need to write one superblock... */
1769 break;
1770 }
NeilBrowna9701a32005-11-08 21:39:34 -08001771 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001772 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001773
NeilBrowna9701a32005-11-08 21:39:34 -08001774 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001775 if (mddev->in_sync != sync_req ||
1776 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001777 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001778 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001779 goto repeat;
1780 }
NeilBrown850b2b42006-10-03 01:15:46 -07001781 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001782 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001783 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
NeilBrownbce74da2006-01-06 00:20:41 -08001787/* words written to sysfs files may, or my not, be \n terminated.
1788 * We want to accept with case. For this we use cmd_match.
1789 */
1790static int cmd_match(const char *cmd, const char *str)
1791{
1792 /* See if cmd, written into a sysfs file, matches
1793 * str. They must either be the same, or cmd can
1794 * have a trailing newline
1795 */
1796 while (*cmd && *str && *cmd == *str) {
1797 cmd++;
1798 str++;
1799 }
1800 if (*cmd == '\n')
1801 cmd++;
1802 if (*str || *cmd)
1803 return 0;
1804 return 1;
1805}
1806
NeilBrown86e6ffd2005-11-08 21:39:24 -08001807struct rdev_sysfs_entry {
1808 struct attribute attr;
1809 ssize_t (*show)(mdk_rdev_t *, char *);
1810 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1811};
1812
1813static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001814state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001815{
1816 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001817 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001818
NeilBrownb2d444d2005-11-08 21:39:31 -08001819 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001820 len+= sprintf(page+len, "%sfaulty",sep);
1821 sep = ",";
1822 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001823 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001824 len += sprintf(page+len, "%sin_sync",sep);
1825 sep = ",";
1826 }
NeilBrownf6556752006-06-26 00:28:01 -07001827 if (test_bit(WriteMostly, &rdev->flags)) {
1828 len += sprintf(page+len, "%swrite_mostly",sep);
1829 sep = ",";
1830 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001831 if (test_bit(Blocked, &rdev->flags)) {
1832 len += sprintf(page+len, "%sblocked", sep);
1833 sep = ",";
1834 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001835 if (!test_bit(Faulty, &rdev->flags) &&
1836 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001837 len += sprintf(page+len, "%sspare", sep);
1838 sep = ",";
1839 }
1840 return len+sprintf(page+len, "\n");
1841}
1842
NeilBrown45dc2de2006-06-26 00:27:58 -07001843static ssize_t
1844state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1845{
1846 /* can write
1847 * faulty - simulates and error
1848 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001849 * writemostly - sets write_mostly
1850 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001851 * blocked - sets the Blocked flag
1852 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001853 */
1854 int err = -EINVAL;
1855 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1856 md_error(rdev->mddev, rdev);
1857 err = 0;
1858 } else if (cmd_match(buf, "remove")) {
1859 if (rdev->raid_disk >= 0)
1860 err = -EBUSY;
1861 else {
1862 mddev_t *mddev = rdev->mddev;
1863 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001864 if (mddev->pers)
1865 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001866 md_new_event(mddev);
1867 err = 0;
1868 }
NeilBrownf6556752006-06-26 00:28:01 -07001869 } else if (cmd_match(buf, "writemostly")) {
1870 set_bit(WriteMostly, &rdev->flags);
1871 err = 0;
1872 } else if (cmd_match(buf, "-writemostly")) {
1873 clear_bit(WriteMostly, &rdev->flags);
1874 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001875 } else if (cmd_match(buf, "blocked")) {
1876 set_bit(Blocked, &rdev->flags);
1877 err = 0;
1878 } else if (cmd_match(buf, "-blocked")) {
1879 clear_bit(Blocked, &rdev->flags);
1880 wake_up(&rdev->blocked_wait);
1881 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1882 md_wakeup_thread(rdev->mddev->thread);
1883
1884 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001885 }
1886 return err ? err : len;
1887}
NeilBrown80ca3a42006-07-10 04:44:18 -07001888static struct rdev_sysfs_entry rdev_state =
1889__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001890
1891static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001892errors_show(mdk_rdev_t *rdev, char *page)
1893{
1894 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1895}
1896
1897static ssize_t
1898errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1899{
1900 char *e;
1901 unsigned long n = simple_strtoul(buf, &e, 10);
1902 if (*buf && (*e == 0 || *e == '\n')) {
1903 atomic_set(&rdev->corrected_errors, n);
1904 return len;
1905 }
1906 return -EINVAL;
1907}
1908static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001909__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001910
NeilBrown014236d2006-01-06 00:20:55 -08001911static ssize_t
1912slot_show(mdk_rdev_t *rdev, char *page)
1913{
1914 if (rdev->raid_disk < 0)
1915 return sprintf(page, "none\n");
1916 else
1917 return sprintf(page, "%d\n", rdev->raid_disk);
1918}
1919
1920static ssize_t
1921slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1922{
1923 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001924 int err;
1925 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001926 int slot = simple_strtoul(buf, &e, 10);
1927 if (strncmp(buf, "none", 4)==0)
1928 slot = -1;
1929 else if (e==buf || (*e && *e!= '\n'))
1930 return -EINVAL;
NeilBrownc303da62008-02-06 01:39:51 -08001931 if (rdev->mddev->pers) {
1932 /* Setting 'slot' on an active array requires also
1933 * updating the 'rd%d' link, and communicating
1934 * with the personality with ->hot_*_disk.
1935 * For now we only support removing
1936 * failed/spare devices. This normally happens automatically,
1937 * but not when the metadata is externally managed.
1938 */
1939 if (slot != -1)
1940 return -EBUSY;
1941 if (rdev->raid_disk == -1)
1942 return -EEXIST;
1943 /* personality does all needed checks */
1944 if (rdev->mddev->pers->hot_add_disk == NULL)
1945 return -EINVAL;
1946 err = rdev->mddev->pers->
1947 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1948 if (err)
1949 return err;
1950 sprintf(nm, "rd%d", rdev->raid_disk);
1951 sysfs_remove_link(&rdev->mddev->kobj, nm);
1952 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1953 md_wakeup_thread(rdev->mddev->thread);
1954 } else {
1955 if (slot >= rdev->mddev->raid_disks)
1956 return -ENOSPC;
1957 rdev->raid_disk = slot;
1958 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001959 clear_bit(Faulty, &rdev->flags);
1960 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08001961 set_bit(In_sync, &rdev->flags);
1962 }
NeilBrown014236d2006-01-06 00:20:55 -08001963 return len;
1964}
1965
1966
1967static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001968__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001969
NeilBrown93c8cad2006-01-06 00:20:56 -08001970static ssize_t
1971offset_show(mdk_rdev_t *rdev, char *page)
1972{
Andrew Morton6961ece2006-01-06 00:20:59 -08001973 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001974}
1975
1976static ssize_t
1977offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1978{
1979 char *e;
1980 unsigned long long offset = simple_strtoull(buf, &e, 10);
1981 if (e==buf || (*e && *e != '\n'))
1982 return -EINVAL;
1983 if (rdev->mddev->pers)
1984 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001985 if (rdev->size && rdev->mddev->external)
1986 /* Must set offset before size, so overlap checks
1987 * can be sane */
1988 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08001989 rdev->data_offset = offset;
1990 return len;
1991}
1992
1993static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001994__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001995
NeilBrown83303b62006-01-06 00:21:06 -08001996static ssize_t
1997rdev_size_show(mdk_rdev_t *rdev, char *page)
1998{
1999 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2000}
2001
NeilBrownc5d79ad2008-02-06 01:39:54 -08002002static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2003{
2004 /* check if two start/length pairs overlap */
2005 if (s1+l1 <= s2)
2006 return 0;
2007 if (s2+l2 <= s1)
2008 return 0;
2009 return 1;
2010}
2011
NeilBrown83303b62006-01-06 00:21:06 -08002012static ssize_t
2013rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2014{
2015 char *e;
2016 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002017 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002018 mddev_t *my_mddev = rdev->mddev;
2019
NeilBrown83303b62006-01-06 00:21:06 -08002020 if (e==buf || (*e && *e != '\n'))
2021 return -EINVAL;
NeilBrown27c529b2008-03-04 14:29:33 -08002022 if (my_mddev->pers)
NeilBrown83303b62006-01-06 00:21:06 -08002023 return -EBUSY;
2024 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002025 if (size > oldsize && rdev->mddev->external) {
2026 /* need to check that all other rdevs with the same ->bdev
2027 * do not overlap. We need to unlock the mddev to avoid
2028 * a deadlock. We have already changed rdev->size, and if
2029 * we have to change it back, we will have the lock again.
2030 */
2031 mddev_t *mddev;
2032 int overlap = 0;
2033 struct list_head *tmp, *tmp2;
2034
NeilBrown27c529b2008-03-04 14:29:33 -08002035 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002036 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002037 mdk_rdev_t *rdev2;
2038
2039 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002040 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002041 if (test_bit(AllReserved, &rdev2->flags) ||
2042 (rdev->bdev == rdev2->bdev &&
2043 rdev != rdev2 &&
2044 overlaps(rdev->data_offset, rdev->size,
2045 rdev2->data_offset, rdev2->size))) {
2046 overlap = 1;
2047 break;
2048 }
2049 mddev_unlock(mddev);
2050 if (overlap) {
2051 mddev_put(mddev);
2052 break;
2053 }
2054 }
NeilBrown27c529b2008-03-04 14:29:33 -08002055 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002056 if (overlap) {
2057 /* Someone else could have slipped in a size
2058 * change here, but doing so is just silly.
2059 * We put oldsize back because we *know* it is
2060 * safe, and trust userspace not to race with
2061 * itself
2062 */
2063 rdev->size = oldsize;
2064 return -EBUSY;
2065 }
2066 }
NeilBrown27c529b2008-03-04 14:29:33 -08002067 if (size < my_mddev->size || my_mddev->size == 0)
2068 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002069 return len;
2070}
2071
2072static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002073__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002074
NeilBrown86e6ffd2005-11-08 21:39:24 -08002075static struct attribute *rdev_default_attrs[] = {
2076 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002077 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002078 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002079 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002080 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002081 NULL,
2082};
2083static ssize_t
2084rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2085{
2086 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2087 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002088 mddev_t *mddev = rdev->mddev;
2089 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002090
2091 if (!entry->show)
2092 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002093
2094 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2095 if (!rv) {
2096 if (rdev->mddev == NULL)
2097 rv = -EBUSY;
2098 else
2099 rv = entry->show(rdev, page);
2100 mddev_unlock(mddev);
2101 }
2102 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002103}
2104
2105static ssize_t
2106rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2107 const char *page, size_t length)
2108{
2109 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2110 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002111 ssize_t rv;
2112 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002113
2114 if (!entry->store)
2115 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002116 if (!capable(CAP_SYS_ADMIN))
2117 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002118 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002119 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002120 if (rdev->mddev == NULL)
2121 rv = -EBUSY;
2122 else
2123 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002124 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002125 }
2126 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002127}
2128
2129static void rdev_free(struct kobject *ko)
2130{
2131 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2132 kfree(rdev);
2133}
2134static struct sysfs_ops rdev_sysfs_ops = {
2135 .show = rdev_attr_show,
2136 .store = rdev_attr_store,
2137};
2138static struct kobj_type rdev_ktype = {
2139 .release = rdev_free,
2140 .sysfs_ops = &rdev_sysfs_ops,
2141 .default_attrs = rdev_default_attrs,
2142};
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144/*
2145 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2146 *
2147 * mark the device faulty if:
2148 *
2149 * - the device is nonexistent (zero size)
2150 * - the device has no valid superblock
2151 *
2152 * a faulty rdev _never_ has rdev->sb set.
2153 */
2154static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2155{
2156 char b[BDEVNAME_SIZE];
2157 int err;
2158 mdk_rdev_t *rdev;
2159 sector_t size;
2160
NeilBrown9ffae0c2006-01-06 00:20:32 -08002161 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (!rdev) {
2163 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2164 return ERR_PTR(-ENOMEM);
2165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 if ((err = alloc_disk_sb(rdev)))
2168 goto abort_free;
2169
NeilBrownc5d79ad2008-02-06 01:39:54 -08002170 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (err)
2172 goto abort_free;
2173
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002174 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002177 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002178 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002179 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002181 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002183 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002184 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2187 if (!size) {
2188 printk(KERN_WARNING
2189 "md: %s has zero or unknown size, marking faulty!\n",
2190 bdevname(rdev->bdev,b));
2191 err = -EINVAL;
2192 goto abort_free;
2193 }
2194
2195 if (super_format >= 0) {
2196 err = super_types[super_format].
2197 load_super(rdev, NULL, super_minor);
2198 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002199 printk(KERN_WARNING
2200 "md: %s does not have a valid v%d.%d "
2201 "superblock, not importing!\n",
2202 bdevname(rdev->bdev,b),
2203 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto abort_free;
2205 }
2206 if (err < 0) {
2207 printk(KERN_WARNING
2208 "md: could not read %s's sb, not importing!\n",
2209 bdevname(rdev->bdev,b));
2210 goto abort_free;
2211 }
2212 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002215 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 return rdev;
2218
2219abort_free:
2220 if (rdev->sb_page) {
2221 if (rdev->bdev)
2222 unlock_rdev(rdev);
2223 free_disk_sb(rdev);
2224 }
2225 kfree(rdev);
2226 return ERR_PTR(err);
2227}
2228
2229/*
2230 * Check a full RAID array for plausibility
2231 */
2232
2233
NeilBrowna757e642005-04-16 15:26:42 -07002234static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 int i;
2237 struct list_head *tmp;
2238 mdk_rdev_t *rdev, *freshest;
2239 char b[BDEVNAME_SIZE];
2240
2241 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002242 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 switch (super_types[mddev->major_version].
2244 load_super(rdev, freshest, mddev->minor_version)) {
2245 case 1:
2246 freshest = rdev;
2247 break;
2248 case 0:
2249 break;
2250 default:
2251 printk( KERN_ERR \
2252 "md: fatal superblock inconsistency in %s"
2253 " -- removing from array\n",
2254 bdevname(rdev->bdev,b));
2255 kick_rdev_from_array(rdev);
2256 }
2257
2258
2259 super_types[mddev->major_version].
2260 validate_super(mddev, freshest);
2261
2262 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002263 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (rdev != freshest)
2265 if (super_types[mddev->major_version].
2266 validate_super(mddev, rdev)) {
2267 printk(KERN_WARNING "md: kicking non-fresh %s"
2268 " from array!\n",
2269 bdevname(rdev->bdev,b));
2270 kick_rdev_from_array(rdev);
2271 continue;
2272 }
2273 if (mddev->level == LEVEL_MULTIPATH) {
2274 rdev->desc_nr = i++;
2275 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002276 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002277 } else if (rdev->raid_disk >= mddev->raid_disks) {
2278 rdev->raid_disk = -1;
2279 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281 }
2282
2283
2284
2285 if (mddev->recovery_cp != MaxSector &&
2286 mddev->level >= 1)
2287 printk(KERN_ERR "md: %s: raid array is not clean"
2288 " -- starting background reconstruction\n",
2289 mdname(mddev));
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
NeilBrowneae17012005-11-08 21:39:23 -08002293static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002294safe_delay_show(mddev_t *mddev, char *page)
2295{
2296 int msec = (mddev->safemode_delay*1000)/HZ;
2297 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2298}
2299static ssize_t
2300safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2301{
2302 int scale=1;
2303 int dot=0;
2304 int i;
2305 unsigned long msec;
2306 char buf[30];
2307 char *e;
2308 /* remove a period, and count digits after it */
2309 if (len >= sizeof(buf))
2310 return -EINVAL;
2311 strlcpy(buf, cbuf, len);
2312 buf[len] = 0;
2313 for (i=0; i<len; i++) {
2314 if (dot) {
2315 if (isdigit(buf[i])) {
2316 buf[i-1] = buf[i];
2317 scale *= 10;
2318 }
2319 buf[i] = 0;
2320 } else if (buf[i] == '.') {
2321 dot=1;
2322 buf[i] = 0;
2323 }
2324 }
2325 msec = simple_strtoul(buf, &e, 10);
2326 if (e == buf || (*e && *e != '\n'))
2327 return -EINVAL;
2328 msec = (msec * 1000) / scale;
2329 if (msec == 0)
2330 mddev->safemode_delay = 0;
2331 else {
2332 mddev->safemode_delay = (msec*HZ)/1000;
2333 if (mddev->safemode_delay == 0)
2334 mddev->safemode_delay = 1;
2335 }
2336 return len;
2337}
2338static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002339__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002340
2341static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002342level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002343{
NeilBrown2604b702006-01-06 00:20:36 -08002344 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002345 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002346 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002347 else if (mddev->clevel[0])
2348 return sprintf(page, "%s\n", mddev->clevel);
2349 else if (mddev->level != LEVEL_NONE)
2350 return sprintf(page, "%d\n", mddev->level);
2351 else
2352 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002353}
2354
NeilBrownd9d166c2006-01-06 00:20:51 -08002355static ssize_t
2356level_store(mddev_t *mddev, const char *buf, size_t len)
2357{
NeilBrown20a49ff2008-02-06 01:39:57 -08002358 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002359 if (mddev->pers)
2360 return -EBUSY;
2361 if (len == 0)
2362 return 0;
2363 if (len >= sizeof(mddev->clevel))
2364 return -ENOSPC;
2365 strncpy(mddev->clevel, buf, len);
2366 if (mddev->clevel[len-1] == '\n')
2367 len--;
2368 mddev->clevel[len] = 0;
2369 mddev->level = LEVEL_NONE;
2370 return rv;
2371}
2372
2373static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002374__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002375
NeilBrownd4dbd022006-06-26 00:27:59 -07002376
2377static ssize_t
2378layout_show(mddev_t *mddev, char *page)
2379{
2380 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002381 if (mddev->reshape_position != MaxSector &&
2382 mddev->layout != mddev->new_layout)
2383 return sprintf(page, "%d (%d)\n",
2384 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002385 return sprintf(page, "%d\n", mddev->layout);
2386}
2387
2388static ssize_t
2389layout_store(mddev_t *mddev, const char *buf, size_t len)
2390{
2391 char *e;
2392 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002393
2394 if (!*buf || (*e && *e != '\n'))
2395 return -EINVAL;
2396
NeilBrown08a02ec2007-05-09 02:35:38 -07002397 if (mddev->pers)
2398 return -EBUSY;
2399 if (mddev->reshape_position != MaxSector)
2400 mddev->new_layout = n;
2401 else
2402 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002403 return len;
2404}
2405static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002406__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002407
2408
NeilBrowneae17012005-11-08 21:39:23 -08002409static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002410raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002411{
NeilBrownbb636542005-11-08 21:39:45 -08002412 if (mddev->raid_disks == 0)
2413 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002414 if (mddev->reshape_position != MaxSector &&
2415 mddev->delta_disks != 0)
2416 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2417 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002418 return sprintf(page, "%d\n", mddev->raid_disks);
2419}
2420
NeilBrownda943b992006-01-06 00:20:54 -08002421static int update_raid_disks(mddev_t *mddev, int raid_disks);
2422
2423static ssize_t
2424raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2425{
NeilBrownda943b992006-01-06 00:20:54 -08002426 char *e;
2427 int rv = 0;
2428 unsigned long n = simple_strtoul(buf, &e, 10);
2429
2430 if (!*buf || (*e && *e != '\n'))
2431 return -EINVAL;
2432
2433 if (mddev->pers)
2434 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002435 else if (mddev->reshape_position != MaxSector) {
2436 int olddisks = mddev->raid_disks - mddev->delta_disks;
2437 mddev->delta_disks = n - olddisks;
2438 mddev->raid_disks = n;
2439 } else
NeilBrownda943b992006-01-06 00:20:54 -08002440 mddev->raid_disks = n;
2441 return rv ? rv : len;
2442}
2443static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002444__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002445
NeilBrown24dd4692005-11-08 21:39:26 -08002446static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002447chunk_size_show(mddev_t *mddev, char *page)
2448{
NeilBrown08a02ec2007-05-09 02:35:38 -07002449 if (mddev->reshape_position != MaxSector &&
2450 mddev->chunk_size != mddev->new_chunk)
2451 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2452 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002453 return sprintf(page, "%d\n", mddev->chunk_size);
2454}
2455
2456static ssize_t
2457chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2458{
2459 /* can only set chunk_size if array is not yet active */
2460 char *e;
2461 unsigned long n = simple_strtoul(buf, &e, 10);
2462
NeilBrown3b343802006-01-06 00:20:47 -08002463 if (!*buf || (*e && *e != '\n'))
2464 return -EINVAL;
2465
NeilBrown08a02ec2007-05-09 02:35:38 -07002466 if (mddev->pers)
2467 return -EBUSY;
2468 else if (mddev->reshape_position != MaxSector)
2469 mddev->new_chunk = n;
2470 else
2471 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002472 return len;
2473}
2474static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002475__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002476
NeilBrowna94213b2006-06-26 00:28:00 -07002477static ssize_t
2478resync_start_show(mddev_t *mddev, char *page)
2479{
2480 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2481}
2482
2483static ssize_t
2484resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2485{
NeilBrowna94213b2006-06-26 00:28:00 -07002486 char *e;
2487 unsigned long long n = simple_strtoull(buf, &e, 10);
2488
2489 if (mddev->pers)
2490 return -EBUSY;
2491 if (!*buf || (*e && *e != '\n'))
2492 return -EINVAL;
2493
2494 mddev->recovery_cp = n;
2495 return len;
2496}
2497static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002498__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002499
NeilBrown9e653b62006-06-26 00:27:58 -07002500/*
2501 * The array state can be:
2502 *
2503 * clear
2504 * No devices, no size, no level
2505 * Equivalent to STOP_ARRAY ioctl
2506 * inactive
2507 * May have some settings, but array is not active
2508 * all IO results in error
2509 * When written, doesn't tear down array, but just stops it
2510 * suspended (not supported yet)
2511 * All IO requests will block. The array can be reconfigured.
2512 * Writing this, if accepted, will block until array is quiessent
2513 * readonly
2514 * no resync can happen. no superblocks get written.
2515 * write requests fail
2516 * read-auto
2517 * like readonly, but behaves like 'clean' on a write request.
2518 *
2519 * clean - no pending writes, but otherwise active.
2520 * When written to inactive array, starts without resync
2521 * If a write request arrives then
2522 * if metadata is known, mark 'dirty' and switch to 'active'.
2523 * if not known, block and switch to write-pending
2524 * If written to an active array that has pending writes, then fails.
2525 * active
2526 * fully active: IO and resync can be happening.
2527 * When written to inactive array, starts with resync
2528 *
2529 * write-pending
2530 * clean, but writes are blocked waiting for 'active' to be written.
2531 *
2532 * active-idle
2533 * like active, but no writes have been seen for a while (100msec).
2534 *
2535 */
2536enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2537 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002538static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002539 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2540 "write-pending", "active-idle", NULL };
2541
2542static int match_word(const char *word, char **list)
2543{
2544 int n;
2545 for (n=0; list[n]; n++)
2546 if (cmd_match(word, list[n]))
2547 break;
2548 return n;
2549}
2550
2551static ssize_t
2552array_state_show(mddev_t *mddev, char *page)
2553{
2554 enum array_state st = inactive;
2555
2556 if (mddev->pers)
2557 switch(mddev->ro) {
2558 case 1:
2559 st = readonly;
2560 break;
2561 case 2:
2562 st = read_auto;
2563 break;
2564 case 0:
2565 if (mddev->in_sync)
2566 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002567 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2568 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002569 else if (mddev->safemode)
2570 st = active_idle;
2571 else
2572 st = active;
2573 }
2574 else {
2575 if (list_empty(&mddev->disks) &&
2576 mddev->raid_disks == 0 &&
2577 mddev->size == 0)
2578 st = clear;
2579 else
2580 st = inactive;
2581 }
2582 return sprintf(page, "%s\n", array_states[st]);
2583}
2584
2585static int do_md_stop(mddev_t * mddev, int ro);
2586static int do_md_run(mddev_t * mddev);
2587static int restart_array(mddev_t *mddev);
2588
2589static ssize_t
2590array_state_store(mddev_t *mddev, const char *buf, size_t len)
2591{
2592 int err = -EINVAL;
2593 enum array_state st = match_word(buf, array_states);
2594 switch(st) {
2595 case bad_word:
2596 break;
2597 case clear:
2598 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002599 if (atomic_read(&mddev->active) > 1)
2600 return -EBUSY;
2601 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002602 break;
2603 case inactive:
2604 /* stopping an active array */
2605 if (mddev->pers) {
2606 if (atomic_read(&mddev->active) > 1)
2607 return -EBUSY;
2608 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002609 } else
2610 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002611 break;
2612 case suspended:
2613 break; /* not supported yet */
2614 case readonly:
2615 if (mddev->pers)
2616 err = do_md_stop(mddev, 1);
2617 else {
2618 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002619 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002620 err = do_md_run(mddev);
2621 }
2622 break;
2623 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002624 if (mddev->pers) {
NeilBrown648b6292008-04-30 00:52:30 -07002625 if (mddev->ro != 1)
2626 err = do_md_stop(mddev, 1);
2627 else
2628 err = restart_array(mddev);
2629 if (err == 0) {
2630 mddev->ro = 2;
2631 set_disk_ro(mddev->gendisk, 0);
2632 }
NeilBrown9e653b62006-06-26 00:27:58 -07002633 } else {
2634 mddev->ro = 2;
2635 err = do_md_run(mddev);
2636 }
2637 break;
2638 case clean:
2639 if (mddev->pers) {
2640 restart_array(mddev);
2641 spin_lock_irq(&mddev->write_lock);
2642 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002643 if (mddev->in_sync == 0) {
2644 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002645 if (mddev->safemode == 1)
2646 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002647 if (mddev->persistent)
2648 set_bit(MD_CHANGE_CLEAN,
2649 &mddev->flags);
2650 }
2651 err = 0;
2652 } else
2653 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002654 spin_unlock_irq(&mddev->write_lock);
2655 } else {
2656 mddev->ro = 0;
2657 mddev->recovery_cp = MaxSector;
2658 err = do_md_run(mddev);
2659 }
2660 break;
2661 case active:
2662 if (mddev->pers) {
2663 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002664 if (mddev->external)
2665 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002666 wake_up(&mddev->sb_wait);
2667 err = 0;
2668 } else {
2669 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002670 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002671 err = do_md_run(mddev);
2672 }
2673 break;
2674 case write_pending:
2675 case active_idle:
2676 /* these cannot be set */
2677 break;
2678 }
2679 if (err)
2680 return err;
2681 else
2682 return len;
2683}
NeilBrown80ca3a42006-07-10 04:44:18 -07002684static struct md_sysfs_entry md_array_state =
2685__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002686
NeilBrown6d7ff732006-01-06 00:21:16 -08002687static ssize_t
2688null_show(mddev_t *mddev, char *page)
2689{
2690 return -EINVAL;
2691}
2692
2693static ssize_t
2694new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2695{
2696 /* buf must be %d:%d\n? giving major and minor numbers */
2697 /* The new device is added to the array.
2698 * If the array has a persistent superblock, we read the
2699 * superblock to initialise info and check validity.
2700 * Otherwise, only checking done is that in bind_rdev_to_array,
2701 * which mainly checks size.
2702 */
2703 char *e;
2704 int major = simple_strtoul(buf, &e, 10);
2705 int minor;
2706 dev_t dev;
2707 mdk_rdev_t *rdev;
2708 int err;
2709
2710 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2711 return -EINVAL;
2712 minor = simple_strtoul(e+1, &e, 10);
2713 if (*e && *e != '\n')
2714 return -EINVAL;
2715 dev = MKDEV(major, minor);
2716 if (major != MAJOR(dev) ||
2717 minor != MINOR(dev))
2718 return -EOVERFLOW;
2719
2720
2721 if (mddev->persistent) {
2722 rdev = md_import_device(dev, mddev->major_version,
2723 mddev->minor_version);
2724 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2725 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2726 mdk_rdev_t, same_set);
2727 err = super_types[mddev->major_version]
2728 .load_super(rdev, rdev0, mddev->minor_version);
2729 if (err < 0)
2730 goto out;
2731 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002732 } else if (mddev->external)
2733 rdev = md_import_device(dev, -2, -1);
2734 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002735 rdev = md_import_device(dev, -1, -1);
2736
2737 if (IS_ERR(rdev))
2738 return PTR_ERR(rdev);
2739 err = bind_rdev_to_array(rdev, mddev);
2740 out:
2741 if (err)
2742 export_rdev(rdev);
2743 return err ? err : len;
2744}
2745
2746static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002747__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002748
2749static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002750bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2751{
2752 char *end;
2753 unsigned long chunk, end_chunk;
2754
2755 if (!mddev->bitmap)
2756 goto out;
2757 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2758 while (*buf) {
2759 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2760 if (buf == end) break;
2761 if (*end == '-') { /* range */
2762 buf = end + 1;
2763 end_chunk = simple_strtoul(buf, &end, 0);
2764 if (buf == end) break;
2765 }
2766 if (*end && !isspace(*end)) break;
2767 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2768 buf = end;
2769 while (isspace(*buf)) buf++;
2770 }
2771 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2772out:
2773 return len;
2774}
2775
2776static struct md_sysfs_entry md_bitmap =
2777__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2778
2779static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002780size_show(mddev_t *mddev, char *page)
2781{
2782 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2783}
2784
2785static int update_size(mddev_t *mddev, unsigned long size);
2786
2787static ssize_t
2788size_store(mddev_t *mddev, const char *buf, size_t len)
2789{
2790 /* If array is inactive, we can reduce the component size, but
2791 * not increase it (except from 0).
2792 * If array is active, we can try an on-line resize
2793 */
2794 char *e;
2795 int err = 0;
2796 unsigned long long size = simple_strtoull(buf, &e, 10);
2797 if (!*buf || *buf == '\n' ||
2798 (*e && *e != '\n'))
2799 return -EINVAL;
2800
2801 if (mddev->pers) {
2802 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002803 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002804 } else {
2805 if (mddev->size == 0 ||
2806 mddev->size > size)
2807 mddev->size = size;
2808 else
2809 err = -ENOSPC;
2810 }
2811 return err ? err : len;
2812}
2813
2814static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002815__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002816
NeilBrown8bb93aa2006-01-06 00:20:50 -08002817
2818/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002819 * This is one of
2820 * 'none' for arrays with no metadata (good luck...)
2821 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002822 * or N.M for internally known formats
2823 */
2824static ssize_t
2825metadata_show(mddev_t *mddev, char *page)
2826{
2827 if (mddev->persistent)
2828 return sprintf(page, "%d.%d\n",
2829 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002830 else if (mddev->external)
2831 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002832 else
2833 return sprintf(page, "none\n");
2834}
2835
2836static ssize_t
2837metadata_store(mddev_t *mddev, const char *buf, size_t len)
2838{
2839 int major, minor;
2840 char *e;
2841 if (!list_empty(&mddev->disks))
2842 return -EBUSY;
2843
2844 if (cmd_match(buf, "none")) {
2845 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002846 mddev->external = 0;
2847 mddev->major_version = 0;
2848 mddev->minor_version = 90;
2849 return len;
2850 }
2851 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002852 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002853 if (namelen >= sizeof(mddev->metadata_type))
2854 namelen = sizeof(mddev->metadata_type)-1;
2855 strncpy(mddev->metadata_type, buf+9, namelen);
2856 mddev->metadata_type[namelen] = 0;
2857 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2858 mddev->metadata_type[--namelen] = 0;
2859 mddev->persistent = 0;
2860 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002861 mddev->major_version = 0;
2862 mddev->minor_version = 90;
2863 return len;
2864 }
2865 major = simple_strtoul(buf, &e, 10);
2866 if (e==buf || *e != '.')
2867 return -EINVAL;
2868 buf = e+1;
2869 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002870 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002871 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002872 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002873 return -ENOENT;
2874 mddev->major_version = major;
2875 mddev->minor_version = minor;
2876 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002877 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002878 return len;
2879}
2880
2881static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002882__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002883
NeilBrowna35b0d62006-01-06 00:20:49 -08002884static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002885action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002886{
NeilBrown7eec3142005-11-08 21:39:44 -08002887 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002888 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002889 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002890 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2891 type = "reshape";
2892 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002893 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2894 type = "resync";
2895 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2896 type = "check";
2897 else
2898 type = "repair";
2899 } else
2900 type = "recover";
2901 }
2902 return sprintf(page, "%s\n", type);
2903}
2904
2905static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002906action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002907{
NeilBrown7eec3142005-11-08 21:39:44 -08002908 if (!mddev->pers || !mddev->pers->sync_request)
2909 return -EINVAL;
2910
NeilBrownbce74da2006-01-06 00:20:41 -08002911 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002912 if (mddev->sync_thread) {
2913 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2914 md_unregister_thread(mddev->sync_thread);
2915 mddev->sync_thread = NULL;
2916 mddev->recovery = 0;
2917 }
NeilBrown03c902e2006-01-06 00:20:46 -08002918 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2919 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002920 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002921 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002922 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002923 else if (cmd_match(page, "reshape")) {
2924 int err;
2925 if (mddev->pers->start_reshape == NULL)
2926 return -EINVAL;
2927 err = mddev->pers->start_reshape(mddev);
2928 if (err)
2929 return err;
2930 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002931 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002932 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002933 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002934 return -EINVAL;
2935 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2936 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002937 }
NeilBrown03c902e2006-01-06 00:20:46 -08002938 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002939 md_wakeup_thread(mddev->thread);
2940 return len;
2941}
2942
NeilBrown9d888832005-11-08 21:39:26 -08002943static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002944mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002945{
2946 return sprintf(page, "%llu\n",
2947 (unsigned long long) mddev->resync_mismatches);
2948}
2949
NeilBrown80ca3a42006-07-10 04:44:18 -07002950static struct md_sysfs_entry md_scan_mode =
2951__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002952
NeilBrown96de1e62005-11-08 21:39:39 -08002953
NeilBrown80ca3a42006-07-10 04:44:18 -07002954static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002955
NeilBrown88202a02006-01-06 00:21:36 -08002956static ssize_t
2957sync_min_show(mddev_t *mddev, char *page)
2958{
2959 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2960 mddev->sync_speed_min ? "local": "system");
2961}
2962
2963static ssize_t
2964sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2965{
2966 int min;
2967 char *e;
2968 if (strncmp(buf, "system", 6)==0) {
2969 mddev->sync_speed_min = 0;
2970 return len;
2971 }
2972 min = simple_strtoul(buf, &e, 10);
2973 if (buf == e || (*e && *e != '\n') || min <= 0)
2974 return -EINVAL;
2975 mddev->sync_speed_min = min;
2976 return len;
2977}
2978
2979static struct md_sysfs_entry md_sync_min =
2980__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2981
2982static ssize_t
2983sync_max_show(mddev_t *mddev, char *page)
2984{
2985 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2986 mddev->sync_speed_max ? "local": "system");
2987}
2988
2989static ssize_t
2990sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2991{
2992 int max;
2993 char *e;
2994 if (strncmp(buf, "system", 6)==0) {
2995 mddev->sync_speed_max = 0;
2996 return len;
2997 }
2998 max = simple_strtoul(buf, &e, 10);
2999 if (buf == e || (*e && *e != '\n') || max <= 0)
3000 return -EINVAL;
3001 mddev->sync_speed_max = max;
3002 return len;
3003}
3004
3005static struct md_sysfs_entry md_sync_max =
3006__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3007
Iustin Popd7f3d292007-10-16 23:30:54 -07003008static ssize_t
3009degraded_show(mddev_t *mddev, char *page)
3010{
3011 return sprintf(page, "%d\n", mddev->degraded);
3012}
3013static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003014
3015static ssize_t
3016sync_speed_show(mddev_t *mddev, char *page)
3017{
3018 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07003019 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08003020 dt = ((jiffies - mddev->resync_mark) / HZ);
3021 if (!dt) dt++;
3022 db = resync - (mddev->resync_mark_cnt);
3023 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
3024}
3025
NeilBrown80ca3a42006-07-10 04:44:18 -07003026static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003027
3028static ssize_t
3029sync_completed_show(mddev_t *mddev, char *page)
3030{
3031 unsigned long max_blocks, resync;
3032
3033 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3034 max_blocks = mddev->resync_max_sectors;
3035 else
3036 max_blocks = mddev->size << 1;
3037
3038 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3039 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3040}
3041
NeilBrown80ca3a42006-07-10 04:44:18 -07003042static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003043
NeilBrowne464eaf2006-03-27 01:18:14 -08003044static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003045max_sync_show(mddev_t *mddev, char *page)
3046{
3047 if (mddev->resync_max == MaxSector)
3048 return sprintf(page, "max\n");
3049 else
3050 return sprintf(page, "%llu\n",
3051 (unsigned long long)mddev->resync_max);
3052}
3053static ssize_t
3054max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3055{
3056 if (strncmp(buf, "max", 3) == 0)
3057 mddev->resync_max = MaxSector;
3058 else {
3059 char *ep;
3060 unsigned long long max = simple_strtoull(buf, &ep, 10);
3061 if (ep == buf || (*ep != 0 && *ep != '\n'))
3062 return -EINVAL;
3063 if (max < mddev->resync_max &&
3064 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3065 return -EBUSY;
3066
3067 /* Must be a multiple of chunk_size */
3068 if (mddev->chunk_size) {
3069 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3070 return -EINVAL;
3071 }
3072 mddev->resync_max = max;
3073 }
3074 wake_up(&mddev->recovery_wait);
3075 return len;
3076}
3077
3078static struct md_sysfs_entry md_max_sync =
3079__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3080
3081static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003082suspend_lo_show(mddev_t *mddev, char *page)
3083{
3084 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3085}
3086
3087static ssize_t
3088suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3089{
3090 char *e;
3091 unsigned long long new = simple_strtoull(buf, &e, 10);
3092
3093 if (mddev->pers->quiesce == NULL)
3094 return -EINVAL;
3095 if (buf == e || (*e && *e != '\n'))
3096 return -EINVAL;
3097 if (new >= mddev->suspend_hi ||
3098 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3099 mddev->suspend_lo = new;
3100 mddev->pers->quiesce(mddev, 2);
3101 return len;
3102 } else
3103 return -EINVAL;
3104}
3105static struct md_sysfs_entry md_suspend_lo =
3106__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3107
3108
3109static ssize_t
3110suspend_hi_show(mddev_t *mddev, char *page)
3111{
3112 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3113}
3114
3115static ssize_t
3116suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3117{
3118 char *e;
3119 unsigned long long new = simple_strtoull(buf, &e, 10);
3120
3121 if (mddev->pers->quiesce == NULL)
3122 return -EINVAL;
3123 if (buf == e || (*e && *e != '\n'))
3124 return -EINVAL;
3125 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3126 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3127 mddev->suspend_hi = new;
3128 mddev->pers->quiesce(mddev, 1);
3129 mddev->pers->quiesce(mddev, 0);
3130 return len;
3131 } else
3132 return -EINVAL;
3133}
3134static struct md_sysfs_entry md_suspend_hi =
3135__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3136
NeilBrown08a02ec2007-05-09 02:35:38 -07003137static ssize_t
3138reshape_position_show(mddev_t *mddev, char *page)
3139{
3140 if (mddev->reshape_position != MaxSector)
3141 return sprintf(page, "%llu\n",
3142 (unsigned long long)mddev->reshape_position);
3143 strcpy(page, "none\n");
3144 return 5;
3145}
3146
3147static ssize_t
3148reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3149{
3150 char *e;
3151 unsigned long long new = simple_strtoull(buf, &e, 10);
3152 if (mddev->pers)
3153 return -EBUSY;
3154 if (buf == e || (*e && *e != '\n'))
3155 return -EINVAL;
3156 mddev->reshape_position = new;
3157 mddev->delta_disks = 0;
3158 mddev->new_level = mddev->level;
3159 mddev->new_layout = mddev->layout;
3160 mddev->new_chunk = mddev->chunk_size;
3161 return len;
3162}
3163
3164static struct md_sysfs_entry md_reshape_position =
3165__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3166 reshape_position_store);
3167
NeilBrowne464eaf2006-03-27 01:18:14 -08003168
NeilBrowneae17012005-11-08 21:39:23 -08003169static struct attribute *md_default_attrs[] = {
3170 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003171 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003172 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003173 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003174 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003175 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003176 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003177 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003178 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003179 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003180 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003181 NULL,
3182};
3183
3184static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003185 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003186 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003187 &md_sync_min.attr,
3188 &md_sync_max.attr,
3189 &md_sync_speed.attr,
3190 &md_sync_completed.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003191 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003192 &md_suspend_lo.attr,
3193 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003194 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003195 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003196 NULL,
3197};
NeilBrown411036f2005-11-08 21:39:40 -08003198static struct attribute_group md_redundancy_group = {
3199 .name = NULL,
3200 .attrs = md_redundancy_attrs,
3201};
3202
NeilBrowneae17012005-11-08 21:39:23 -08003203
3204static ssize_t
3205md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3206{
3207 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3208 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003209 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003210
3211 if (!entry->show)
3212 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003213 rv = mddev_lock(mddev);
3214 if (!rv) {
3215 rv = entry->show(mddev, page);
3216 mddev_unlock(mddev);
3217 }
NeilBrown96de1e62005-11-08 21:39:39 -08003218 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003219}
3220
3221static ssize_t
3222md_attr_store(struct kobject *kobj, struct attribute *attr,
3223 const char *page, size_t length)
3224{
3225 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3226 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003227 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003228
3229 if (!entry->store)
3230 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003231 if (!capable(CAP_SYS_ADMIN))
3232 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003233 rv = mddev_lock(mddev);
3234 if (!rv) {
3235 rv = entry->store(mddev, page, length);
3236 mddev_unlock(mddev);
3237 }
NeilBrown96de1e62005-11-08 21:39:39 -08003238 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003239}
3240
3241static void md_free(struct kobject *ko)
3242{
3243 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3244 kfree(mddev);
3245}
3246
3247static struct sysfs_ops md_sysfs_ops = {
3248 .show = md_attr_show,
3249 .store = md_attr_store,
3250};
3251static struct kobj_type md_ktype = {
3252 .release = md_free,
3253 .sysfs_ops = &md_sysfs_ops,
3254 .default_attrs = md_default_attrs,
3255};
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257int mdp_major = 0;
3258
3259static struct kobject *md_probe(dev_t dev, int *part, void *data)
3260{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003261 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 mddev_t *mddev = mddev_find(dev);
3263 struct gendisk *disk;
3264 int partitioned = (MAJOR(dev) != MD_MAJOR);
3265 int shift = partitioned ? MdpMinorShift : 0;
3266 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003267 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
3269 if (!mddev)
3270 return NULL;
3271
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003272 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003274 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 mddev_put(mddev);
3276 return NULL;
3277 }
3278 disk = alloc_disk(1 << shift);
3279 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003280 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 mddev_put(mddev);
3282 return NULL;
3283 }
3284 disk->major = MAJOR(dev);
3285 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003286 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003288 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 disk->fops = &md_fops;
3291 disk->private_data = mddev;
3292 disk->queue = mddev->queue;
3293 add_disk(disk);
3294 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003295 mutex_unlock(&disks_mutex);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003296 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003297 "%s", "md");
3298 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003299 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3300 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003301 else
3302 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return NULL;
3304}
3305
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306static void md_safemode_timeout(unsigned long data)
3307{
3308 mddev_t *mddev = (mddev_t *) data;
3309
3310 mddev->safemode = 1;
3311 md_wakeup_thread(mddev->thread);
3312}
3313
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003314static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316static int do_md_run(mddev_t * mddev)
3317{
NeilBrown2604b702006-01-06 00:20:36 -08003318 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 int chunk_size;
3320 struct list_head *tmp;
3321 mdk_rdev_t *rdev;
3322 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003323 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 char b[BDEVNAME_SIZE];
3325
NeilBrowna757e642005-04-16 15:26:42 -07003326 if (list_empty(&mddev->disks))
3327 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 if (mddev->pers)
3331 return -EBUSY;
3332
3333 /*
3334 * Analyze all RAID superblock(s)
3335 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003336 if (!mddev->raid_disks) {
3337 if (!mddev->persistent)
3338 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003339 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
NeilBrown2604b702006-01-06 00:20:36 -08003344 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (chunk_size > MAX_CHUNK_SIZE) {
3346 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3347 chunk_size, MAX_CHUNK_SIZE);
3348 return -EINVAL;
3349 }
3350 /*
3351 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3352 */
3353 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003354 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return -EINVAL;
3356 }
3357 if (chunk_size < PAGE_SIZE) {
3358 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3359 chunk_size, PAGE_SIZE);
3360 return -EINVAL;
3361 }
3362
3363 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003364 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003365 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 continue;
3367 if (rdev->size < chunk_size / 1024) {
3368 printk(KERN_WARNING
3369 "md: Dev %s smaller than chunk_size:"
3370 " %lluk < %dk\n",
3371 bdevname(rdev->bdev,b),
3372 (unsigned long long)rdev->size,
3373 chunk_size / 1024);
3374 return -EINVAL;
3375 }
3376 }
3377 }
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003380 if (mddev->level != LEVEL_NONE)
3381 request_module("md-level-%d", mddev->level);
3382 else if (mddev->clevel[0])
3383 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384#endif
3385
3386 /*
3387 * Drop all container device buffers, from now on
3388 * the only valid external interface is through the md
3389 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003391 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003392 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 continue;
3394 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003395 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003396
3397 /* perform some consistency tests on the device.
3398 * We don't want the data to overlap the metadata,
3399 * Internal Bitmap issues has handled elsewhere.
3400 */
3401 if (rdev->data_offset < rdev->sb_offset) {
3402 if (mddev->size &&
3403 rdev->data_offset + mddev->size*2
3404 > rdev->sb_offset*2) {
3405 printk("md: %s: data overlaps metadata\n",
3406 mdname(mddev));
3407 return -EINVAL;
3408 }
3409 } else {
3410 if (rdev->sb_offset*2 + rdev->sb_size/512
3411 > rdev->data_offset) {
3412 printk("md: %s: metadata overlaps data\n",
3413 mdname(mddev));
3414 return -EINVAL;
3415 }
3416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
3418
3419 md_probe(mddev->unit, NULL, NULL);
3420 disk = mddev->gendisk;
3421 if (!disk)
3422 return -ENOMEM;
3423
3424 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003425 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003426 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003428 if (mddev->level != LEVEL_NONE)
3429 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3430 mddev->level);
3431 else
3432 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3433 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 return -EINVAL;
3435 }
NeilBrown2604b702006-01-06 00:20:36 -08003436 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003438 mddev->level = pers->level;
3439 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
NeilBrownf6705572006-03-27 01:18:11 -08003441 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003442 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003443 /* This personality cannot handle reshaping... */
3444 mddev->pers = NULL;
3445 module_put(pers->owner);
3446 return -EINVAL;
3447 }
3448
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003449 if (pers->sync_request) {
3450 /* Warn if this is a potentially silly
3451 * configuration.
3452 */
3453 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3454 mdk_rdev_t *rdev2;
3455 struct list_head *tmp2;
3456 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003457 rdev_for_each(rdev, tmp, mddev) {
3458 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003459 if (rdev < rdev2 &&
3460 rdev->bdev->bd_contains ==
3461 rdev2->bdev->bd_contains) {
3462 printk(KERN_WARNING
3463 "%s: WARNING: %s appears to be"
3464 " on the same physical disk as"
3465 " %s.\n",
3466 mdname(mddev),
3467 bdevname(rdev->bdev,b),
3468 bdevname(rdev2->bdev,b2));
3469 warned = 1;
3470 }
3471 }
3472 }
3473 if (warned)
3474 printk(KERN_WARNING
3475 "True protection against single-disk"
3476 " failure might be compromised.\n");
3477 }
3478
NeilBrown657390d2005-08-26 18:34:16 -07003479 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003481 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003482 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
NeilBrownf91de922005-11-08 21:39:36 -08003484 if (start_readonly)
3485 mddev->ro = 2; /* read-only, but switch on first write */
3486
NeilBrownb15c2e52006-01-06 00:20:16 -08003487 err = mddev->pers->run(mddev);
3488 if (!err && mddev->pers->sync_request) {
3489 err = bitmap_create(mddev);
3490 if (err) {
3491 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3492 mdname(mddev), err);
3493 mddev->pers->stop(mddev);
3494 }
3495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (err) {
3497 printk(KERN_ERR "md: pers->run() failed ...\n");
3498 module_put(mddev->pers->owner);
3499 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003500 bitmap_destroy(mddev);
3501 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003503 if (mddev->pers->sync_request) {
3504 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3505 printk(KERN_WARNING
3506 "md: cannot register extra attributes for %s\n",
3507 mdname(mddev));
3508 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003509 mddev->ro = 0;
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 atomic_set(&mddev->writes_pending,0);
3512 mddev->safemode = 0;
3513 mddev->safemode_timer.function = md_safemode_timeout;
3514 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003515 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003517
NeilBrownd089c6a2008-02-06 01:39:59 -08003518 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003519 if (rdev->raid_disk >= 0) {
3520 char nm[20];
3521 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003522 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3523 printk("md: cannot register %s for %s\n",
3524 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3528
NeilBrown850b2b42006-10-03 01:15:46 -07003529 if (mddev->flags)
3530 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 set_capacity(disk, mddev->array_size<<1);
3533
3534 /* If we call blk_queue_make_request here, it will
3535 * re-initialise max_sectors etc which may have been
3536 * refined inside -> run. So just set the bits we need to set.
3537 * Most initialisation happended when we called
3538 * blk_queue_make_request(..., md_fail_request)
3539 * earlier.
3540 */
3541 mddev->queue->queuedata = mddev;
3542 mddev->queue->make_request_fn = mddev->pers->make_request;
3543
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003544 /* If there is a partially-recovered drive we need to
3545 * start recovery here. If we leave it to md_check_recovery,
3546 * it will remove the drives and not do the right thing
3547 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003548 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003549 struct list_head *rtmp;
3550 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003551 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003552 if (rdev->raid_disk >= 0 &&
3553 !test_bit(In_sync, &rdev->flags) &&
3554 !test_bit(Faulty, &rdev->flags))
3555 /* complete an interrupted recovery */
3556 spares++;
3557 if (spares && mddev->pers->sync_request) {
3558 mddev->recovery = 0;
3559 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3560 mddev->sync_thread = md_register_thread(md_do_sync,
3561 mddev,
3562 "%s_resync");
3563 if (!mddev->sync_thread) {
3564 printk(KERN_ERR "%s: could not start resync"
3565 " thread...\n",
3566 mdname(mddev));
3567 /* leave the spares where they are, it shouldn't hurt */
3568 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003569 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003570 }
3571 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003572 md_wakeup_thread(mddev->thread);
3573 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003574
Linus Torvalds44ce62942007-05-09 18:51:36 -07003575 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003576 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003577 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 return 0;
3579}
3580
3581static int restart_array(mddev_t *mddev)
3582{
3583 struct gendisk *disk = mddev->gendisk;
3584 int err;
3585
3586 /*
3587 * Complain if it has no devices
3588 */
3589 err = -ENXIO;
3590 if (list_empty(&mddev->disks))
3591 goto out;
3592
3593 if (mddev->pers) {
3594 err = -EBUSY;
3595 if (!mddev->ro)
3596 goto out;
3597
3598 mddev->safemode = 0;
3599 mddev->ro = 0;
3600 set_disk_ro(disk, 0);
3601
3602 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3603 mdname(mddev));
3604 /*
3605 * Kick recovery or resync if necessary
3606 */
3607 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3608 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003609 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003611 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
3614out:
3615 return err;
3616}
3617
NeilBrownacc55e22006-06-26 00:27:47 -07003618/* similar to deny_write_access, but accounts for our holding a reference
3619 * to the file ourselves */
3620static int deny_bitmap_write_access(struct file * file)
3621{
3622 struct inode *inode = file->f_mapping->host;
3623
3624 spin_lock(&inode->i_lock);
3625 if (atomic_read(&inode->i_writecount) > 1) {
3626 spin_unlock(&inode->i_lock);
3627 return -ETXTBSY;
3628 }
3629 atomic_set(&inode->i_writecount, -1);
3630 spin_unlock(&inode->i_lock);
3631
3632 return 0;
3633}
3634
3635static void restore_bitmap_write_access(struct file *file)
3636{
3637 struct inode *inode = file->f_mapping->host;
3638
3639 spin_lock(&inode->i_lock);
3640 atomic_set(&inode->i_writecount, 1);
3641 spin_unlock(&inode->i_lock);
3642}
3643
NeilBrown9e653b62006-06-26 00:27:58 -07003644/* mode:
3645 * 0 - completely stop and dis-assemble array
3646 * 1 - switch to readonly
3647 * 2 - stop but do not disassemble array
3648 */
3649static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
3651 int err = 0;
3652 struct gendisk *disk = mddev->gendisk;
3653
3654 if (mddev->pers) {
3655 if (atomic_read(&mddev->active)>2) {
3656 printk("md: %s still in use.\n",mdname(mddev));
3657 return -EBUSY;
3658 }
3659
3660 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003661 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3663 md_unregister_thread(mddev->sync_thread);
3664 mddev->sync_thread = NULL;
3665 }
3666
3667 del_timer_sync(&mddev->safemode_timer);
3668
3669 invalidate_partition(disk, 0);
3670
NeilBrown9e653b62006-06-26 00:27:58 -07003671 switch(mode) {
3672 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003674 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 goto out;
3676 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003677 break;
3678 case 0: /* disassemble */
3679 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003680 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003681 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 if (mddev->ro)
3683 set_disk_ro(disk, 0);
3684 blk_queue_make_request(mddev->queue, md_fail_request);
3685 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003686 mddev->queue->merge_bvec_fn = NULL;
3687 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003688 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003689 if (mddev->pers->sync_request)
3690 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3691
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 module_put(mddev->pers->owner);
3693 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003694
3695 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003696 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 if (mddev->ro)
3699 mddev->ro = 0;
3700 }
NeilBrown850b2b42006-10-03 01:15:46 -07003701 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 /* mark array as shutdown cleanly */
3703 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003704 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
NeilBrown9e653b62006-06-26 00:27:58 -07003706 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003708 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 }
NeilBrown32a76272005-06-21 17:17:14 -07003710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 /*
3712 * Free resources if final stop
3713 */
NeilBrown9e653b62006-06-26 00:27:58 -07003714 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003715 mdk_rdev_t *rdev;
3716 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3719
NeilBrown978f9462006-02-02 14:28:05 -08003720 bitmap_destroy(mddev);
3721 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003722 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003723 fput(mddev->bitmap_file);
3724 mddev->bitmap_file = NULL;
3725 }
3726 mddev->bitmap_offset = 0;
3727
NeilBrownd089c6a2008-02-06 01:39:59 -08003728 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003729 if (rdev->raid_disk >= 0) {
3730 char nm[20];
3731 sprintf(nm, "rd%d", rdev->raid_disk);
3732 sysfs_remove_link(&mddev->kobj, nm);
3733 }
3734
NeilBrown177a99b2008-02-06 01:39:56 -08003735 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003736 flush_scheduled_work();
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 export_array(mddev);
3739
3740 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003741 mddev->size = 0;
3742 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003743 mddev->recovery_cp = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003744 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003745 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003746 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003747 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003748 mddev->level = LEVEL_NONE;
3749 mddev->clevel[0] = 0;
3750 mddev->flags = 0;
3751 mddev->ro = 0;
3752 mddev->metadata_type[0] = 0;
3753 mddev->chunk_size = 0;
3754 mddev->ctime = mddev->utime = 0;
3755 mddev->layout = 0;
3756 mddev->max_disks = 0;
3757 mddev->events = 0;
3758 mddev->delta_disks = 0;
3759 mddev->new_level = LEVEL_NONE;
3760 mddev->new_layout = 0;
3761 mddev->new_chunk = 0;
3762 mddev->curr_resync = 0;
3763 mddev->resync_mismatches = 0;
3764 mddev->suspend_lo = mddev->suspend_hi = 0;
3765 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3766 mddev->recovery = 0;
3767 mddev->in_sync = 0;
3768 mddev->changed = 0;
3769 mddev->degraded = 0;
3770 mddev->barriers_work = 0;
3771 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003772
NeilBrowna8a55c32006-06-26 00:27:39 -07003773 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3775 mdname(mddev));
3776 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003777 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778out:
3779 return err;
3780}
3781
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003782#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783static void autorun_array(mddev_t *mddev)
3784{
3785 mdk_rdev_t *rdev;
3786 struct list_head *tmp;
3787 int err;
3788
NeilBrowna757e642005-04-16 15:26:42 -07003789 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 printk(KERN_INFO "md: running: ");
3793
NeilBrownd089c6a2008-02-06 01:39:59 -08003794 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 char b[BDEVNAME_SIZE];
3796 printk("<%s>", bdevname(rdev->bdev,b));
3797 }
3798 printk("\n");
3799
3800 err = do_md_run (mddev);
3801 if (err) {
3802 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3803 do_md_stop (mddev, 0);
3804 }
3805}
3806
3807/*
3808 * lets try to run arrays based on all disks that have arrived
3809 * until now. (those are in pending_raid_disks)
3810 *
3811 * the method: pick the first pending disk, collect all disks with
3812 * the same UUID, remove all from the pending list and put them into
3813 * the 'same_array' list. Then order this list based on superblock
3814 * update time (freshest comes first), kick out 'old' disks and
3815 * compare superblocks. If everything's fine then run it.
3816 *
3817 * If "unit" is allocated, then bump its reference count
3818 */
3819static void autorun_devices(int part)
3820{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 struct list_head *tmp;
3822 mdk_rdev_t *rdev0, *rdev;
3823 mddev_t *mddev;
3824 char b[BDEVNAME_SIZE];
3825
3826 printk(KERN_INFO "md: autorun ...\n");
3827 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003828 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003830 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 rdev0 = list_entry(pending_raid_disks.next,
3832 mdk_rdev_t, same_set);
3833
3834 printk(KERN_INFO "md: considering %s ...\n",
3835 bdevname(rdev0->bdev,b));
3836 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003837 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 if (super_90_load(rdev, rdev0, 0) >= 0) {
3839 printk(KERN_INFO "md: adding %s ...\n",
3840 bdevname(rdev->bdev,b));
3841 list_move(&rdev->same_set, &candidates);
3842 }
3843 /*
3844 * now we have a set of devices, with all of them having
3845 * mostly sane superblocks. It's time to allocate the
3846 * mddev.
3847 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003848 if (part) {
3849 dev = MKDEV(mdp_major,
3850 rdev0->preferred_minor << MdpMinorShift);
3851 unit = MINOR(dev) >> MdpMinorShift;
3852 } else {
3853 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3854 unit = MINOR(dev);
3855 }
3856 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3858 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3859 break;
3860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
3862 md_probe(dev, NULL, NULL);
3863 mddev = mddev_find(dev);
3864 if (!mddev) {
3865 printk(KERN_ERR
3866 "md: cannot allocate memory for md drive.\n");
3867 break;
3868 }
3869 if (mddev_lock(mddev))
3870 printk(KERN_WARNING "md: %s locked, cannot run\n",
3871 mdname(mddev));
3872 else if (mddev->raid_disks || mddev->major_version
3873 || !list_empty(&mddev->disks)) {
3874 printk(KERN_WARNING
3875 "md: %s already running, cannot run %s\n",
3876 mdname(mddev), bdevname(rdev0->bdev,b));
3877 mddev_unlock(mddev);
3878 } else {
3879 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08003880 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08003881 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 list_del_init(&rdev->same_set);
3883 if (bind_rdev_to_array(rdev, mddev))
3884 export_rdev(rdev);
3885 }
3886 autorun_array(mddev);
3887 mddev_unlock(mddev);
3888 }
3889 /* on success, candidates will be empty, on error
3890 * it won't...
3891 */
NeilBrown73c34432008-02-06 01:39:59 -08003892 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 export_rdev(rdev);
3894 mddev_put(mddev);
3895 }
3896 printk(KERN_INFO "md: ... autorun DONE.\n");
3897}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003898#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900static int get_version(void __user * arg)
3901{
3902 mdu_version_t ver;
3903
3904 ver.major = MD_MAJOR_VERSION;
3905 ver.minor = MD_MINOR_VERSION;
3906 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3907
3908 if (copy_to_user(arg, &ver, sizeof(ver)))
3909 return -EFAULT;
3910
3911 return 0;
3912}
3913
3914static int get_array_info(mddev_t * mddev, void __user * arg)
3915{
3916 mdu_array_info_t info;
3917 int nr,working,active,failed,spare;
3918 mdk_rdev_t *rdev;
3919 struct list_head *tmp;
3920
3921 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003922 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003924 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 failed++;
3926 else {
3927 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003928 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 active++;
3930 else
3931 spare++;
3932 }
3933 }
3934
3935 info.major_version = mddev->major_version;
3936 info.minor_version = mddev->minor_version;
3937 info.patch_version = MD_PATCHLEVEL_VERSION;
3938 info.ctime = mddev->ctime;
3939 info.level = mddev->level;
3940 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003941 if (info.size != mddev->size) /* overflow */
3942 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 info.nr_disks = nr;
3944 info.raid_disks = mddev->raid_disks;
3945 info.md_minor = mddev->md_minor;
3946 info.not_persistent= !mddev->persistent;
3947
3948 info.utime = mddev->utime;
3949 info.state = 0;
3950 if (mddev->in_sync)
3951 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003952 if (mddev->bitmap && mddev->bitmap_offset)
3953 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 info.active_disks = active;
3955 info.working_disks = working;
3956 info.failed_disks = failed;
3957 info.spare_disks = spare;
3958
3959 info.layout = mddev->layout;
3960 info.chunk_size = mddev->chunk_size;
3961
3962 if (copy_to_user(arg, &info, sizeof(info)))
3963 return -EFAULT;
3964
3965 return 0;
3966}
3967
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003968static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003969{
3970 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3971 char *ptr, *buf = NULL;
3972 int err = -ENOMEM;
3973
NeilBrown2a2275d2007-01-26 00:57:11 -08003974 md_allow_write(mddev);
3975
NeilBrown32a76272005-06-21 17:17:14 -07003976 file = kmalloc(sizeof(*file), GFP_KERNEL);
3977 if (!file)
3978 goto out;
3979
3980 /* bitmap disabled, zero the first byte and copy out */
3981 if (!mddev->bitmap || !mddev->bitmap->file) {
3982 file->pathname[0] = '\0';
3983 goto copy_out;
3984 }
3985
3986 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3987 if (!buf)
3988 goto out;
3989
3990 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3991 if (!ptr)
3992 goto out;
3993
3994 strcpy(file->pathname, ptr);
3995
3996copy_out:
3997 err = 0;
3998 if (copy_to_user(arg, file, sizeof(*file)))
3999 err = -EFAULT;
4000out:
4001 kfree(buf);
4002 kfree(file);
4003 return err;
4004}
4005
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006static int get_disk_info(mddev_t * mddev, void __user * arg)
4007{
4008 mdu_disk_info_t info;
4009 unsigned int nr;
4010 mdk_rdev_t *rdev;
4011
4012 if (copy_from_user(&info, arg, sizeof(info)))
4013 return -EFAULT;
4014
4015 nr = info.number;
4016
4017 rdev = find_rdev_nr(mddev, nr);
4018 if (rdev) {
4019 info.major = MAJOR(rdev->bdev->bd_dev);
4020 info.minor = MINOR(rdev->bdev->bd_dev);
4021 info.raid_disk = rdev->raid_disk;
4022 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004023 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004025 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 info.state |= (1<<MD_DISK_ACTIVE);
4027 info.state |= (1<<MD_DISK_SYNC);
4028 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004029 if (test_bit(WriteMostly, &rdev->flags))
4030 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 } else {
4032 info.major = info.minor = 0;
4033 info.raid_disk = -1;
4034 info.state = (1<<MD_DISK_REMOVED);
4035 }
4036
4037 if (copy_to_user(arg, &info, sizeof(info)))
4038 return -EFAULT;
4039
4040 return 0;
4041}
4042
4043static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4044{
4045 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4046 mdk_rdev_t *rdev;
4047 dev_t dev = MKDEV(info->major,info->minor);
4048
4049 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4050 return -EOVERFLOW;
4051
4052 if (!mddev->raid_disks) {
4053 int err;
4054 /* expecting a device which has a superblock */
4055 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4056 if (IS_ERR(rdev)) {
4057 printk(KERN_WARNING
4058 "md: md_import_device returned %ld\n",
4059 PTR_ERR(rdev));
4060 return PTR_ERR(rdev);
4061 }
4062 if (!list_empty(&mddev->disks)) {
4063 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4064 mdk_rdev_t, same_set);
4065 int err = super_types[mddev->major_version]
4066 .load_super(rdev, rdev0, mddev->minor_version);
4067 if (err < 0) {
4068 printk(KERN_WARNING
4069 "md: %s has different UUID to %s\n",
4070 bdevname(rdev->bdev,b),
4071 bdevname(rdev0->bdev,b2));
4072 export_rdev(rdev);
4073 return -EINVAL;
4074 }
4075 }
4076 err = bind_rdev_to_array(rdev, mddev);
4077 if (err)
4078 export_rdev(rdev);
4079 return err;
4080 }
4081
4082 /*
4083 * add_new_disk can be used once the array is assembled
4084 * to add "hot spares". They must already have a superblock
4085 * written
4086 */
4087 if (mddev->pers) {
4088 int err;
4089 if (!mddev->pers->hot_add_disk) {
4090 printk(KERN_WARNING
4091 "%s: personality does not support diskops!\n",
4092 mdname(mddev));
4093 return -EINVAL;
4094 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004095 if (mddev->persistent)
4096 rdev = md_import_device(dev, mddev->major_version,
4097 mddev->minor_version);
4098 else
4099 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (IS_ERR(rdev)) {
4101 printk(KERN_WARNING
4102 "md: md_import_device returned %ld\n",
4103 PTR_ERR(rdev));
4104 return PTR_ERR(rdev);
4105 }
NeilBrown41158c72005-06-21 17:17:25 -07004106 /* set save_raid_disk if appropriate */
4107 if (!mddev->persistent) {
4108 if (info->state & (1<<MD_DISK_SYNC) &&
4109 info->raid_disk < mddev->raid_disks)
4110 rdev->raid_disk = info->raid_disk;
4111 else
4112 rdev->raid_disk = -1;
4113 } else
4114 super_types[mddev->major_version].
4115 validate_super(mddev, rdev);
4116 rdev->saved_raid_disk = rdev->raid_disk;
4117
NeilBrownb2d444d2005-11-08 21:39:31 -08004118 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004119 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4120 set_bit(WriteMostly, &rdev->flags);
4121
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 rdev->raid_disk = -1;
4123 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004124 if (!err && !mddev->pers->hot_remove_disk) {
4125 /* If there is hot_add_disk but no hot_remove_disk
4126 * then added disks for geometry changes,
4127 * and should be added immediately.
4128 */
4129 super_types[mddev->major_version].
4130 validate_super(mddev, rdev);
4131 err = mddev->pers->hot_add_disk(mddev, rdev);
4132 if (err)
4133 unbind_rdev_from_array(rdev);
4134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 if (err)
4136 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004137
NeilBrown17571282006-12-10 02:20:52 -08004138 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07004139 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004140 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return err;
4142 }
4143
4144 /* otherwise, add_new_disk is only allowed
4145 * for major_version==0 superblocks
4146 */
4147 if (mddev->major_version != 0) {
4148 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4149 mdname(mddev));
4150 return -EINVAL;
4151 }
4152
4153 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4154 int err;
4155 rdev = md_import_device (dev, -1, 0);
4156 if (IS_ERR(rdev)) {
4157 printk(KERN_WARNING
4158 "md: error, md_import_device() returned %ld\n",
4159 PTR_ERR(rdev));
4160 return PTR_ERR(rdev);
4161 }
4162 rdev->desc_nr = info->number;
4163 if (info->raid_disk < mddev->raid_disks)
4164 rdev->raid_disk = info->raid_disk;
4165 else
4166 rdev->raid_disk = -1;
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004169 if (info->state & (1<<MD_DISK_SYNC))
4170 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004172 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4173 set_bit(WriteMostly, &rdev->flags);
4174
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 if (!mddev->persistent) {
4176 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4177 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4178 } else
4179 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4180 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4181
NeilBrown2bf071b2006-01-06 00:20:55 -08004182 err = bind_rdev_to_array(rdev, mddev);
4183 if (err) {
4184 export_rdev(rdev);
4185 return err;
4186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 }
4188
4189 return 0;
4190}
4191
4192static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4193{
4194 char b[BDEVNAME_SIZE];
4195 mdk_rdev_t *rdev;
4196
4197 if (!mddev->pers)
4198 return -ENODEV;
4199
4200 rdev = find_rdev(mddev, dev);
4201 if (!rdev)
4202 return -ENXIO;
4203
4204 if (rdev->raid_disk >= 0)
4205 goto busy;
4206
4207 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004208 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004209 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
4211 return 0;
4212busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004213 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 bdevname(rdev->bdev,b), mdname(mddev));
4215 return -EBUSY;
4216}
4217
4218static int hot_add_disk(mddev_t * mddev, dev_t dev)
4219{
4220 char b[BDEVNAME_SIZE];
4221 int err;
4222 unsigned int size;
4223 mdk_rdev_t *rdev;
4224
4225 if (!mddev->pers)
4226 return -ENODEV;
4227
4228 if (mddev->major_version != 0) {
4229 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4230 " version-0 superblocks.\n",
4231 mdname(mddev));
4232 return -EINVAL;
4233 }
4234 if (!mddev->pers->hot_add_disk) {
4235 printk(KERN_WARNING
4236 "%s: personality does not support diskops!\n",
4237 mdname(mddev));
4238 return -EINVAL;
4239 }
4240
4241 rdev = md_import_device (dev, -1, 0);
4242 if (IS_ERR(rdev)) {
4243 printk(KERN_WARNING
4244 "md: error, md_import_device() returned %ld\n",
4245 PTR_ERR(rdev));
4246 return -EINVAL;
4247 }
4248
4249 if (mddev->persistent)
4250 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4251 else
4252 rdev->sb_offset =
4253 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4254
4255 size = calc_dev_size(rdev, mddev->chunk_size);
4256 rdev->size = size;
4257
NeilBrownb2d444d2005-11-08 21:39:31 -08004258 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 printk(KERN_WARNING
4260 "md: can not hot-add faulty %s disk to %s!\n",
4261 bdevname(rdev->bdev,b), mdname(mddev));
4262 err = -EINVAL;
4263 goto abort_export;
4264 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004265 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004267 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004268 err = bind_rdev_to_array(rdev, mddev);
4269 if (err)
4270 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 /*
4273 * The rest should better be atomic, we can have disk failures
4274 * noticed in interrupt contexts ...
4275 */
4276
4277 if (rdev->desc_nr == mddev->max_disks) {
4278 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4279 mdname(mddev));
4280 err = -EBUSY;
4281 goto abort_unbind_export;
4282 }
4283
4284 rdev->raid_disk = -1;
4285
NeilBrown850b2b42006-10-03 01:15:46 -07004286 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
4288 /*
4289 * Kick recovery, maybe this spare has to be added to the
4290 * array immediately.
4291 */
4292 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4293 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004294 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return 0;
4296
4297abort_unbind_export:
4298 unbind_rdev_from_array(rdev);
4299
4300abort_export:
4301 export_rdev(rdev);
4302 return err;
4303}
4304
NeilBrown32a76272005-06-21 17:17:14 -07004305static int set_bitmap_file(mddev_t *mddev, int fd)
4306{
4307 int err;
4308
NeilBrown36fa3062005-09-09 16:23:45 -07004309 if (mddev->pers) {
4310 if (!mddev->pers->quiesce)
4311 return -EBUSY;
4312 if (mddev->recovery || mddev->sync_thread)
4313 return -EBUSY;
4314 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004315 }
4316
NeilBrown36fa3062005-09-09 16:23:45 -07004317
4318 if (fd >= 0) {
4319 if (mddev->bitmap)
4320 return -EEXIST; /* cannot add when bitmap is present */
4321 mddev->bitmap_file = fget(fd);
4322
4323 if (mddev->bitmap_file == NULL) {
4324 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4325 mdname(mddev));
4326 return -EBADF;
4327 }
4328
4329 err = deny_bitmap_write_access(mddev->bitmap_file);
4330 if (err) {
4331 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4332 mdname(mddev));
4333 fput(mddev->bitmap_file);
4334 mddev->bitmap_file = NULL;
4335 return err;
4336 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004337 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004338 } else if (mddev->bitmap == NULL)
4339 return -ENOENT; /* cannot remove what isn't there */
4340 err = 0;
4341 if (mddev->pers) {
4342 mddev->pers->quiesce(mddev, 1);
4343 if (fd >= 0)
4344 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004345 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004346 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004347 fd = -1; /* make sure to put the file */
4348 }
NeilBrown36fa3062005-09-09 16:23:45 -07004349 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004350 }
4351 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004352 if (mddev->bitmap_file) {
4353 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004354 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004355 }
NeilBrown36fa3062005-09-09 16:23:45 -07004356 mddev->bitmap_file = NULL;
4357 }
4358
NeilBrown32a76272005-06-21 17:17:14 -07004359 return err;
4360}
4361
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362/*
4363 * set_array_info is used two different ways
4364 * The original usage is when creating a new array.
4365 * In this usage, raid_disks is > 0 and it together with
4366 * level, size, not_persistent,layout,chunksize determine the
4367 * shape of the array.
4368 * This will always create an array with a type-0.90.0 superblock.
4369 * The newer usage is when assembling an array.
4370 * In this case raid_disks will be 0, and the major_version field is
4371 * use to determine which style super-blocks are to be found on the devices.
4372 * The minor and patch _version numbers are also kept incase the
4373 * super_block handler wishes to interpret them.
4374 */
4375static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4376{
4377
4378 if (info->raid_disks == 0) {
4379 /* just setting version number for superblock loading */
4380 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004381 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 super_types[info->major_version].name == NULL) {
4383 /* maybe try to auto-load a module? */
4384 printk(KERN_INFO
4385 "md: superblock version %d not known\n",
4386 info->major_version);
4387 return -EINVAL;
4388 }
4389 mddev->major_version = info->major_version;
4390 mddev->minor_version = info->minor_version;
4391 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004392 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return 0;
4394 }
4395 mddev->major_version = MD_MAJOR_VERSION;
4396 mddev->minor_version = MD_MINOR_VERSION;
4397 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4398 mddev->ctime = get_seconds();
4399
4400 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004401 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 mddev->size = info->size;
4403 mddev->raid_disks = info->raid_disks;
4404 /* don't set md_minor, it is determined by which /dev/md* was
4405 * openned
4406 */
4407 if (info->state & (1<<MD_SB_CLEAN))
4408 mddev->recovery_cp = MaxSector;
4409 else
4410 mddev->recovery_cp = 0;
4411 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004412 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413
4414 mddev->layout = info->layout;
4415 mddev->chunk_size = info->chunk_size;
4416
4417 mddev->max_disks = MD_SB_DISKS;
4418
NeilBrowne6910632008-02-06 01:39:51 -08004419 if (mddev->persistent)
4420 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004421 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
NeilBrownb2a27032005-11-28 13:44:12 -08004423 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4424 mddev->bitmap_offset = 0;
4425
NeilBrownf6705572006-03-27 01:18:11 -08004426 mddev->reshape_position = MaxSector;
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 /*
4429 * Generate a 128 bit UUID
4430 */
4431 get_random_bytes(mddev->uuid, 16);
4432
NeilBrownf6705572006-03-27 01:18:11 -08004433 mddev->new_level = mddev->level;
4434 mddev->new_chunk = mddev->chunk_size;
4435 mddev->new_layout = mddev->layout;
4436 mddev->delta_disks = 0;
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 return 0;
4439}
4440
NeilBrowna35b0d62006-01-06 00:20:49 -08004441static int update_size(mddev_t *mddev, unsigned long size)
4442{
4443 mdk_rdev_t * rdev;
4444 int rv;
4445 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004446 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004447
4448 if (mddev->pers->resize == NULL)
4449 return -EINVAL;
4450 /* The "size" is the amount of each device that is used.
4451 * This can only make sense for arrays with redundancy.
4452 * linear and raid0 always use whatever space is available
4453 * We can only consider changing the size if no resync
4454 * or reconstruction is happening, and if the new size
4455 * is acceptable. It must fit before the sb_offset or,
4456 * if that is <data_offset, it must fit before the
4457 * size of each device.
4458 * If size is zero, we find the largest size that fits.
4459 */
4460 if (mddev->sync_thread)
4461 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004462 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004463 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004464 avail = rdev->size * 2;
4465
NeilBrowna35b0d62006-01-06 00:20:49 -08004466 if (fit && (size == 0 || size > avail/2))
4467 size = avail/2;
4468 if (avail < ((sector_t)size << 1))
4469 return -ENOSPC;
4470 }
4471 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4472 if (!rv) {
4473 struct block_device *bdev;
4474
4475 bdev = bdget_disk(mddev->gendisk, 0);
4476 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004477 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004478 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004479 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004480 bdput(bdev);
4481 }
4482 }
4483 return rv;
4484}
4485
NeilBrownda943b992006-01-06 00:20:54 -08004486static int update_raid_disks(mddev_t *mddev, int raid_disks)
4487{
4488 int rv;
4489 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004490 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004491 return -EINVAL;
4492 if (raid_disks <= 0 ||
4493 raid_disks >= mddev->max_disks)
4494 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004495 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004496 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004497 mddev->delta_disks = raid_disks - mddev->raid_disks;
4498
4499 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004500 return rv;
4501}
4502
4503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504/*
4505 * update_array_info is used to change the configuration of an
4506 * on-line array.
4507 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4508 * fields in the info are checked against the array.
4509 * Any differences that cannot be handled will cause an error.
4510 * Normally, only one change can be managed at a time.
4511 */
4512static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4513{
4514 int rv = 0;
4515 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004516 int state = 0;
4517
4518 /* calculate expected state,ignoring low bits */
4519 if (mddev->bitmap && mddev->bitmap_offset)
4520 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 if (mddev->major_version != info->major_version ||
4523 mddev->minor_version != info->minor_version ||
4524/* mddev->patch_version != info->patch_version || */
4525 mddev->ctime != info->ctime ||
4526 mddev->level != info->level ||
4527/* mddev->layout != info->layout || */
4528 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004529 mddev->chunk_size != info->chunk_size ||
4530 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4531 ((state^info->state) & 0xfffffe00)
4532 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 return -EINVAL;
4534 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004535 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 if (mddev->raid_disks != info->raid_disks) cnt++;
4537 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004538 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 if (cnt == 0) return 0;
4540 if (cnt > 1) return -EINVAL;
4541
4542 if (mddev->layout != info->layout) {
4543 /* Change layout
4544 * we don't need to do anything at the md level, the
4545 * personality will take care of it all.
4546 */
4547 if (mddev->pers->reconfig == NULL)
4548 return -EINVAL;
4549 else
4550 return mddev->pers->reconfig(mddev, info->layout, -1);
4551 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004552 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004553 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
NeilBrownda943b992006-01-06 00:20:54 -08004555 if (mddev->raid_disks != info->raid_disks)
4556 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
NeilBrown36fa3062005-09-09 16:23:45 -07004558 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4559 if (mddev->pers->quiesce == NULL)
4560 return -EINVAL;
4561 if (mddev->recovery || mddev->sync_thread)
4562 return -EBUSY;
4563 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4564 /* add the bitmap */
4565 if (mddev->bitmap)
4566 return -EEXIST;
4567 if (mddev->default_bitmap_offset == 0)
4568 return -EINVAL;
4569 mddev->bitmap_offset = mddev->default_bitmap_offset;
4570 mddev->pers->quiesce(mddev, 1);
4571 rv = bitmap_create(mddev);
4572 if (rv)
4573 bitmap_destroy(mddev);
4574 mddev->pers->quiesce(mddev, 0);
4575 } else {
4576 /* remove the bitmap */
4577 if (!mddev->bitmap)
4578 return -ENOENT;
4579 if (mddev->bitmap->file)
4580 return -EINVAL;
4581 mddev->pers->quiesce(mddev, 1);
4582 bitmap_destroy(mddev);
4583 mddev->pers->quiesce(mddev, 0);
4584 mddev->bitmap_offset = 0;
4585 }
4586 }
NeilBrown850b2b42006-10-03 01:15:46 -07004587 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 return rv;
4589}
4590
4591static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4592{
4593 mdk_rdev_t *rdev;
4594
4595 if (mddev->pers == NULL)
4596 return -ENODEV;
4597
4598 rdev = find_rdev(mddev, dev);
4599 if (!rdev)
4600 return -ENODEV;
4601
4602 md_error(mddev, rdev);
4603 return 0;
4604}
4605
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004606static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4607{
4608 mddev_t *mddev = bdev->bd_disk->private_data;
4609
4610 geo->heads = 2;
4611 geo->sectors = 4;
4612 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4613 return 0;
4614}
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616static int md_ioctl(struct inode *inode, struct file *file,
4617 unsigned int cmd, unsigned long arg)
4618{
4619 int err = 0;
4620 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 mddev_t *mddev = NULL;
4622
4623 if (!capable(CAP_SYS_ADMIN))
4624 return -EACCES;
4625
4626 /*
4627 * Commands dealing with the RAID driver but not any
4628 * particular array:
4629 */
4630 switch (cmd)
4631 {
4632 case RAID_VERSION:
4633 err = get_version(argp);
4634 goto done;
4635
4636 case PRINT_RAID_DEBUG:
4637 err = 0;
4638 md_print_devices();
4639 goto done;
4640
4641#ifndef MODULE
4642 case RAID_AUTORUN:
4643 err = 0;
4644 autostart_arrays(arg);
4645 goto done;
4646#endif
4647 default:;
4648 }
4649
4650 /*
4651 * Commands creating/starting a new array:
4652 */
4653
4654 mddev = inode->i_bdev->bd_disk->private_data;
4655
4656 if (!mddev) {
4657 BUG();
4658 goto abort;
4659 }
4660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 err = mddev_lock(mddev);
4662 if (err) {
4663 printk(KERN_INFO
4664 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4665 err, cmd);
4666 goto abort;
4667 }
4668
4669 switch (cmd)
4670 {
4671 case SET_ARRAY_INFO:
4672 {
4673 mdu_array_info_t info;
4674 if (!arg)
4675 memset(&info, 0, sizeof(info));
4676 else if (copy_from_user(&info, argp, sizeof(info))) {
4677 err = -EFAULT;
4678 goto abort_unlock;
4679 }
4680 if (mddev->pers) {
4681 err = update_array_info(mddev, &info);
4682 if (err) {
4683 printk(KERN_WARNING "md: couldn't update"
4684 " array info. %d\n", err);
4685 goto abort_unlock;
4686 }
4687 goto done_unlock;
4688 }
4689 if (!list_empty(&mddev->disks)) {
4690 printk(KERN_WARNING
4691 "md: array %s already has disks!\n",
4692 mdname(mddev));
4693 err = -EBUSY;
4694 goto abort_unlock;
4695 }
4696 if (mddev->raid_disks) {
4697 printk(KERN_WARNING
4698 "md: array %s already initialised!\n",
4699 mdname(mddev));
4700 err = -EBUSY;
4701 goto abort_unlock;
4702 }
4703 err = set_array_info(mddev, &info);
4704 if (err) {
4705 printk(KERN_WARNING "md: couldn't set"
4706 " array info. %d\n", err);
4707 goto abort_unlock;
4708 }
4709 }
4710 goto done_unlock;
4711
4712 default:;
4713 }
4714
4715 /*
4716 * Commands querying/configuring an existing array:
4717 */
NeilBrown32a76272005-06-21 17:17:14 -07004718 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004719 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004720 if ((!mddev->raid_disks && !mddev->external)
4721 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4722 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4723 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 err = -ENODEV;
4725 goto abort_unlock;
4726 }
4727
4728 /*
4729 * Commands even a read-only array can execute:
4730 */
4731 switch (cmd)
4732 {
4733 case GET_ARRAY_INFO:
4734 err = get_array_info(mddev, argp);
4735 goto done_unlock;
4736
NeilBrown32a76272005-06-21 17:17:14 -07004737 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004738 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004739 goto done_unlock;
4740
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 case GET_DISK_INFO:
4742 err = get_disk_info(mddev, argp);
4743 goto done_unlock;
4744
4745 case RESTART_ARRAY_RW:
4746 err = restart_array(mddev);
4747 goto done_unlock;
4748
4749 case STOP_ARRAY:
4750 err = do_md_stop (mddev, 0);
4751 goto done_unlock;
4752
4753 case STOP_ARRAY_RO:
4754 err = do_md_stop (mddev, 1);
4755 goto done_unlock;
4756
4757 /*
4758 * We have a problem here : there is no easy way to give a CHS
4759 * virtual geometry. We currently pretend that we have a 2 heads
4760 * 4 sectors (with a BIG number of cylinders...). This drives
4761 * dosfs just mad... ;-)
4762 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 }
4764
4765 /*
4766 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004767 * superblock, so we do not allow them on read-only arrays.
4768 * However non-MD ioctls (e.g. get-size) will still come through
4769 * here and hit the 'default' below, so only disallow
4770 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 */
NeilBrownf91de922005-11-08 21:39:36 -08004772 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4773 mddev->ro && mddev->pers) {
4774 if (mddev->ro == 2) {
4775 mddev->ro = 0;
4776 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4777 md_wakeup_thread(mddev->thread);
4778
4779 } else {
4780 err = -EROFS;
4781 goto abort_unlock;
4782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 }
4784
4785 switch (cmd)
4786 {
4787 case ADD_NEW_DISK:
4788 {
4789 mdu_disk_info_t info;
4790 if (copy_from_user(&info, argp, sizeof(info)))
4791 err = -EFAULT;
4792 else
4793 err = add_new_disk(mddev, &info);
4794 goto done_unlock;
4795 }
4796
4797 case HOT_REMOVE_DISK:
4798 err = hot_remove_disk(mddev, new_decode_dev(arg));
4799 goto done_unlock;
4800
4801 case HOT_ADD_DISK:
4802 err = hot_add_disk(mddev, new_decode_dev(arg));
4803 goto done_unlock;
4804
4805 case SET_DISK_FAULTY:
4806 err = set_disk_faulty(mddev, new_decode_dev(arg));
4807 goto done_unlock;
4808
4809 case RUN_ARRAY:
4810 err = do_md_run (mddev);
4811 goto done_unlock;
4812
NeilBrown32a76272005-06-21 17:17:14 -07004813 case SET_BITMAP_FILE:
4814 err = set_bitmap_file(mddev, (int)arg);
4815 goto done_unlock;
4816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 err = -EINVAL;
4819 goto abort_unlock;
4820 }
4821
4822done_unlock:
4823abort_unlock:
4824 mddev_unlock(mddev);
4825
4826 return err;
4827done:
4828 if (err)
4829 MD_BUG();
4830abort:
4831 return err;
4832}
4833
4834static int md_open(struct inode *inode, struct file *file)
4835{
4836 /*
4837 * Succeed if we can lock the mddev, which confirms that
4838 * it isn't being stopped right now.
4839 */
4840 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4841 int err;
4842
NeilBrownd63a5a72006-12-08 02:36:17 -08004843 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 goto out;
4845
4846 err = 0;
4847 mddev_get(mddev);
4848 mddev_unlock(mddev);
4849
4850 check_disk_change(inode->i_bdev);
4851 out:
4852 return err;
4853}
4854
4855static int md_release(struct inode *inode, struct file * file)
4856{
4857 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4858
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004859 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 mddev_put(mddev);
4861
4862 return 0;
4863}
4864
Linus Torvalds44ce62942007-05-09 18:51:36 -07004865static int md_media_changed(struct gendisk *disk)
4866{
4867 mddev_t *mddev = disk->private_data;
4868
4869 return mddev->changed;
4870}
4871
4872static int md_revalidate(struct gendisk *disk)
4873{
4874 mddev_t *mddev = disk->private_data;
4875
4876 mddev->changed = 0;
4877 return 0;
4878}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879static struct block_device_operations md_fops =
4880{
4881 .owner = THIS_MODULE,
4882 .open = md_open,
4883 .release = md_release,
4884 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004885 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004886 .media_changed = md_media_changed,
4887 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888};
4889
Adrian Bunk75c96f82005-05-05 16:16:09 -07004890static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891{
4892 mdk_thread_t *thread = arg;
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 /*
4895 * md_thread is a 'system-thread', it's priority should be very
4896 * high. We avoid resource deadlocks individually in each
4897 * raid personality. (RAID5 does preallocation) We also use RR and
4898 * the very same RT priority as kswapd, thus we will never get
4899 * into a priority inversion deadlock.
4900 *
4901 * we definitely have to have equal or higher priority than
4902 * bdflush, otherwise bdflush will deadlock if there are too
4903 * many dirty RAID5 blocks.
4904 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
NeilBrown6985c432005-10-19 21:23:47 -07004906 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004907 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908
NeilBrown93588e22005-11-15 00:09:12 -08004909 /* We need to wait INTERRUPTIBLE so that
4910 * we don't add to the load-average.
4911 * That means we need to be sure no signals are
4912 * pending
4913 */
4914 if (signal_pending(current))
4915 flush_signals(current);
4916
4917 wait_event_interruptible_timeout
4918 (thread->wqueue,
4919 test_bit(THREAD_WAKEUP, &thread->flags)
4920 || kthread_should_stop(),
4921 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922
4923 clear_bit(THREAD_WAKEUP, &thread->flags);
4924
NeilBrown787453c2005-11-08 21:39:43 -08004925 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 return 0;
4929}
4930
4931void md_wakeup_thread(mdk_thread_t *thread)
4932{
4933 if (thread) {
4934 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4935 set_bit(THREAD_WAKEUP, &thread->flags);
4936 wake_up(&thread->wqueue);
4937 }
4938}
4939
4940mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4941 const char *name)
4942{
4943 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
NeilBrown9ffae0c2006-01-06 00:20:32 -08004945 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 if (!thread)
4947 return NULL;
4948
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 init_waitqueue_head(&thread->wqueue);
4950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 thread->run = run;
4952 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004953 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004954 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004955 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 kfree(thread);
4957 return NULL;
4958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 return thread;
4960}
4961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962void md_unregister_thread(mdk_thread_t *thread)
4963{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004964 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07004965
4966 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 kfree(thread);
4968}
4969
4970void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4971{
4972 if (!mddev) {
4973 MD_BUG();
4974 return;
4975 }
4976
NeilBrownb2d444d2005-11-08 21:39:31 -08004977 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07004979
4980 if (mddev->external)
4981 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07004982/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4984 mdname(mddev),
4985 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4986 __builtin_return_address(0),__builtin_return_address(1),
4987 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004988*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004989 if (!mddev->pers)
4990 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 if (!mddev->pers->error_handler)
4992 return;
4993 mddev->pers->error_handler(mddev,rdev);
4994 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4995 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4996 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004997 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998}
4999
5000/* seq_file implementation /proc/mdstat */
5001
5002static void status_unused(struct seq_file *seq)
5003{
5004 int i = 0;
5005 mdk_rdev_t *rdev;
5006 struct list_head *tmp;
5007
5008 seq_printf(seq, "unused devices: ");
5009
NeilBrown73c34432008-02-06 01:39:59 -08005010 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 char b[BDEVNAME_SIZE];
5012 i++;
5013 seq_printf(seq, "%s ",
5014 bdevname(rdev->bdev,b));
5015 }
5016 if (!i)
5017 seq_printf(seq, "<none>");
5018
5019 seq_printf(seq, "\n");
5020}
5021
5022
5023static void status_resync(struct seq_file *seq, mddev_t * mddev)
5024{
NeilBrown4588b422006-03-27 01:18:04 -08005025 sector_t max_blocks, resync, res;
5026 unsigned long dt, db, rt;
5027 int scale;
5028 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029
5030 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5031
5032 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5033 max_blocks = mddev->resync_max_sectors >> 1;
5034 else
5035 max_blocks = mddev->size;
5036
5037 /*
5038 * Should not happen.
5039 */
5040 if (!max_blocks) {
5041 MD_BUG();
5042 return;
5043 }
NeilBrown4588b422006-03-27 01:18:04 -08005044 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5045 * in a sector_t, and (max_blocks>>scale) will fit in a
5046 * u32, as those are the requirements for sector_div.
5047 * Thus 'scale' must be at least 10
5048 */
5049 scale = 10;
5050 if (sizeof(sector_t) > sizeof(unsigned long)) {
5051 while ( max_blocks/2 > (1ULL<<(scale+32)))
5052 scale++;
5053 }
5054 res = (resync>>scale)*1000;
5055 sector_div(res, (u32)((max_blocks>>scale)+1));
5056
5057 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 {
NeilBrown4588b422006-03-27 01:18:04 -08005059 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 seq_printf(seq, "[");
5061 for (i = 0; i < x; i++)
5062 seq_printf(seq, "=");
5063 seq_printf(seq, ">");
5064 for (i = 0; i < y; i++)
5065 seq_printf(seq, ".");
5066 seq_printf(seq, "] ");
5067 }
NeilBrown4588b422006-03-27 01:18:04 -08005068 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005069 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5070 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005071 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5072 "check" :
5073 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5074 "resync" : "recovery"))),
5075 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005076 (unsigned long long) resync,
5077 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078
5079 /*
5080 * We do not want to overflow, so the order of operands and
5081 * the * 100 / 100 trick are important. We do a +1 to be
5082 * safe against division by zero. We only estimate anyway.
5083 *
5084 * dt: time from mark until now
5085 * db: blocks written from mark until now
5086 * rt: remaining time
5087 */
5088 dt = ((jiffies - mddev->resync_mark) / HZ);
5089 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005090 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5091 - mddev->resync_mark_cnt;
5092 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
5094 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5095
NeilBrownff4e8d92006-07-10 04:44:16 -07005096 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097}
5098
5099static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5100{
5101 struct list_head *tmp;
5102 loff_t l = *pos;
5103 mddev_t *mddev;
5104
5105 if (l >= 0x10000)
5106 return NULL;
5107 if (!l--)
5108 /* header */
5109 return (void*)1;
5110
5111 spin_lock(&all_mddevs_lock);
5112 list_for_each(tmp,&all_mddevs)
5113 if (!l--) {
5114 mddev = list_entry(tmp, mddev_t, all_mddevs);
5115 mddev_get(mddev);
5116 spin_unlock(&all_mddevs_lock);
5117 return mddev;
5118 }
5119 spin_unlock(&all_mddevs_lock);
5120 if (!l--)
5121 return (void*)2;/* tail */
5122 return NULL;
5123}
5124
5125static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5126{
5127 struct list_head *tmp;
5128 mddev_t *next_mddev, *mddev = v;
5129
5130 ++*pos;
5131 if (v == (void*)2)
5132 return NULL;
5133
5134 spin_lock(&all_mddevs_lock);
5135 if (v == (void*)1)
5136 tmp = all_mddevs.next;
5137 else
5138 tmp = mddev->all_mddevs.next;
5139 if (tmp != &all_mddevs)
5140 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5141 else {
5142 next_mddev = (void*)2;
5143 *pos = 0x10000;
5144 }
5145 spin_unlock(&all_mddevs_lock);
5146
5147 if (v != (void*)1)
5148 mddev_put(mddev);
5149 return next_mddev;
5150
5151}
5152
5153static void md_seq_stop(struct seq_file *seq, void *v)
5154{
5155 mddev_t *mddev = v;
5156
5157 if (mddev && v != (void*)1 && v != (void*)2)
5158 mddev_put(mddev);
5159}
5160
NeilBrownd7603b72006-01-06 00:20:30 -08005161struct mdstat_info {
5162 int event;
5163};
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165static int md_seq_show(struct seq_file *seq, void *v)
5166{
5167 mddev_t *mddev = v;
5168 sector_t size;
5169 struct list_head *tmp2;
5170 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005171 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005172 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173
5174 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005175 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 seq_printf(seq, "Personalities : ");
5177 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005178 list_for_each_entry(pers, &pers_list, list)
5179 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
5181 spin_unlock(&pers_lock);
5182 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005183 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 return 0;
5185 }
5186 if (v == (void*)2) {
5187 status_unused(seq);
5188 return 0;
5189 }
5190
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005191 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5195 seq_printf(seq, "%s : %sactive", mdname(mddev),
5196 mddev->pers ? "" : "in");
5197 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005198 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005200 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005201 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 seq_printf(seq, " %s", mddev->pers->name);
5203 }
5204
5205 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005206 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 char b[BDEVNAME_SIZE];
5208 seq_printf(seq, " %s[%d]",
5209 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005210 if (test_bit(WriteMostly, &rdev->flags))
5211 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005212 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 seq_printf(seq, "(F)");
5214 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005215 } else if (rdev->raid_disk < 0)
5216 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 size += rdev->size;
5218 }
5219
5220 if (!list_empty(&mddev->disks)) {
5221 if (mddev->pers)
5222 seq_printf(seq, "\n %llu blocks",
5223 (unsigned long long)mddev->array_size);
5224 else
5225 seq_printf(seq, "\n %llu blocks",
5226 (unsigned long long)size);
5227 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005228 if (mddev->persistent) {
5229 if (mddev->major_version != 0 ||
5230 mddev->minor_version != 90) {
5231 seq_printf(seq," super %d.%d",
5232 mddev->major_version,
5233 mddev->minor_version);
5234 }
NeilBrowne6910632008-02-06 01:39:51 -08005235 } else if (mddev->external)
5236 seq_printf(seq, " super external:%s",
5237 mddev->metadata_type);
5238 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005239 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240
5241 if (mddev->pers) {
5242 mddev->pers->status (seq, mddev);
5243 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005244 if (mddev->pers->sync_request) {
5245 if (mddev->curr_resync > 2) {
5246 status_resync (seq, mddev);
5247 seq_printf(seq, "\n ");
5248 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5249 seq_printf(seq, "\tresync=DELAYED\n ");
5250 else if (mddev->recovery_cp < MaxSector)
5251 seq_printf(seq, "\tresync=PENDING\n ");
5252 }
NeilBrown32a76272005-06-21 17:17:14 -07005253 } else
5254 seq_printf(seq, "\n ");
5255
5256 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005257 unsigned long chunk_kb;
5258 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005259 spin_lock_irqsave(&bitmap->lock, flags);
5260 chunk_kb = bitmap->chunksize >> 10;
5261 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5262 "%lu%s chunk",
5263 bitmap->pages - bitmap->missing_pages,
5264 bitmap->pages,
5265 (bitmap->pages - bitmap->missing_pages)
5266 << (PAGE_SHIFT - 10),
5267 chunk_kb ? chunk_kb : bitmap->chunksize,
5268 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005269 if (bitmap->file) {
5270 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005271 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005272 }
NeilBrown78d742d2005-06-21 17:17:15 -07005273
NeilBrown32a76272005-06-21 17:17:14 -07005274 seq_printf(seq, "\n");
5275 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 }
5277
5278 seq_printf(seq, "\n");
5279 }
5280 mddev_unlock(mddev);
5281
5282 return 0;
5283}
5284
5285static struct seq_operations md_seq_ops = {
5286 .start = md_seq_start,
5287 .next = md_seq_next,
5288 .stop = md_seq_stop,
5289 .show = md_seq_show,
5290};
5291
5292static int md_seq_open(struct inode *inode, struct file *file)
5293{
5294 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005295 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5296 if (mi == NULL)
5297 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
5299 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005300 if (error)
5301 kfree(mi);
5302 else {
5303 struct seq_file *p = file->private_data;
5304 p->private = mi;
5305 mi->event = atomic_read(&md_event_count);
5306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 return error;
5308}
5309
NeilBrownd7603b72006-01-06 00:20:30 -08005310static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5311{
5312 struct seq_file *m = filp->private_data;
5313 struct mdstat_info *mi = m->private;
5314 int mask;
5315
5316 poll_wait(filp, &md_event_waiters, wait);
5317
5318 /* always allow read */
5319 mask = POLLIN | POLLRDNORM;
5320
5321 if (mi->event != atomic_read(&md_event_count))
5322 mask |= POLLERR | POLLPRI;
5323 return mask;
5324}
5325
Arjan van de Venfa027c22007-02-12 00:55:33 -08005326static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005327 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 .open = md_seq_open,
5329 .read = seq_read,
5330 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005331 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005332 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333};
5334
NeilBrown2604b702006-01-06 00:20:36 -08005335int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005338 list_add_tail(&p->list, &pers_list);
5339 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 spin_unlock(&pers_lock);
5341 return 0;
5342}
5343
NeilBrown2604b702006-01-06 00:20:36 -08005344int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345{
NeilBrown2604b702006-01-06 00:20:36 -08005346 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005348 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 spin_unlock(&pers_lock);
5350 return 0;
5351}
5352
5353static int is_mddev_idle(mddev_t *mddev)
5354{
5355 mdk_rdev_t * rdev;
5356 struct list_head *tmp;
5357 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005358 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359
5360 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005361 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005363 curr_events = disk_stat_read(disk, sectors[0]) +
5364 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005366 /* sync IO will cause sync_io to increase before the disk_stats
5367 * as sync_io is counted when a request starts, and
5368 * disk_stats is counted when it completes.
5369 * So resync activity will cause curr_events to be smaller than
5370 * when there was no such activity.
5371 * non-sync IO will cause disk_stat to increase without
5372 * increasing sync_io so curr_events will (eventually)
5373 * be larger than it was before. Once it becomes
5374 * substantially larger, the test below will cause
5375 * the array to appear non-idle, and resync will slow
5376 * down.
5377 * If there is a lot of outstanding resync activity when
5378 * we set last_event to curr_events, then all that activity
5379 * completing might cause the array to appear non-idle
5380 * and resync will be slowed down even though there might
5381 * not have been non-resync activity. This will only
5382 * happen once though. 'last_events' will soon reflect
5383 * the state where there is little or no outstanding
5384 * resync requests, and further resync activity will
5385 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005388 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 rdev->last_events = curr_events;
5390 idle = 0;
5391 }
5392 }
5393 return idle;
5394}
5395
5396void md_done_sync(mddev_t *mddev, int blocks, int ok)
5397{
5398 /* another "blocks" (512byte) blocks have been synced */
5399 atomic_sub(blocks, &mddev->recovery_active);
5400 wake_up(&mddev->recovery_wait);
5401 if (!ok) {
5402 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5403 md_wakeup_thread(mddev->thread);
5404 // stop recovery, signal do_sync ....
5405 }
5406}
5407
5408
NeilBrown06d91a52005-06-21 17:17:12 -07005409/* md_write_start(mddev, bi)
5410 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005411 * in superblock) before writing, schedule a superblock update
5412 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005413 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005414void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415{
NeilBrown06d91a52005-06-21 17:17:12 -07005416 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005417 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005418
NeilBrownf91de922005-11-08 21:39:36 -08005419 BUG_ON(mddev->ro == 1);
5420 if (mddev->ro == 2) {
5421 /* need to switch to read/write */
5422 mddev->ro = 0;
5423 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5424 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005425 md_wakeup_thread(mddev->sync_thread);
NeilBrownf91de922005-11-08 21:39:36 -08005426 }
NeilBrown06d91a52005-06-21 17:17:12 -07005427 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005428 if (mddev->safemode == 1)
5429 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005430 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005431 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005432 if (mddev->in_sync) {
5433 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005434 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005435 md_wakeup_thread(mddev->thread);
5436 }
NeilBrowna9701a32005-11-08 21:39:34 -08005437 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005438 }
NeilBrown850b2b42006-10-03 01:15:46 -07005439 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440}
5441
5442void md_write_end(mddev_t *mddev)
5443{
5444 if (atomic_dec_and_test(&mddev->writes_pending)) {
5445 if (mddev->safemode == 2)
5446 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005447 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5449 }
5450}
5451
NeilBrown2a2275d2007-01-26 00:57:11 -08005452/* md_allow_write(mddev)
5453 * Calling this ensures that the array is marked 'active' so that writes
5454 * may proceed without blocking. It is important to call this before
5455 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5456 * Must be called with mddev_lock held.
5457 */
5458void md_allow_write(mddev_t *mddev)
5459{
5460 if (!mddev->pers)
5461 return;
5462 if (mddev->ro)
5463 return;
5464
5465 spin_lock_irq(&mddev->write_lock);
5466 if (mddev->in_sync) {
5467 mddev->in_sync = 0;
5468 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5469 if (mddev->safemode_delay &&
5470 mddev->safemode == 0)
5471 mddev->safemode = 1;
5472 spin_unlock_irq(&mddev->write_lock);
5473 md_update_sb(mddev, 0);
5474 } else
5475 spin_unlock_irq(&mddev->write_lock);
5476}
5477EXPORT_SYMBOL_GPL(md_allow_write);
5478
Adrian Bunk75c96f82005-05-05 16:16:09 -07005479static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480
5481#define SYNC_MARKS 10
5482#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005483void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484{
5485 mddev_t *mddev2;
5486 unsigned int currspeed = 0,
5487 window;
NeilBrown57afd892005-06-21 17:17:13 -07005488 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 unsigned long mark[SYNC_MARKS];
5490 sector_t mark_cnt[SYNC_MARKS];
5491 int last_mark,m;
5492 struct list_head *tmp;
5493 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005494 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005495 struct list_head *rtmp;
5496 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005497 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498
5499 /* just incase thread restarts... */
5500 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5501 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005502 if (mddev->ro) /* never try to sync a read-only array */
5503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
NeilBrown61df9d92006-10-03 01:15:57 -07005505 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5506 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5507 desc = "data-check";
5508 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5509 desc = "requested-resync";
5510 else
5511 desc = "resync";
5512 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5513 desc = "reshape";
5514 else
5515 desc = "recovery";
5516
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 /* we overload curr_resync somewhat here.
5518 * 0 == not engaged in resync at all
5519 * 2 == checking that there is no conflict with another sync
5520 * 1 == like 2, but have yielded to allow conflicting resync to
5521 * commense
5522 * other == active in resync - this many blocks
5523 *
5524 * Before starting a resync we must have set curr_resync to
5525 * 2, and then checked that every "conflicting" array has curr_resync
5526 * less than ours. When we find one that is the same or higher
5527 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5528 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5529 * This will mean we have to start checking from the beginning again.
5530 *
5531 */
5532
5533 do {
5534 mddev->curr_resync = 2;
5535
5536 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005537 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005538 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 goto skip;
5540 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005541 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 if (mddev2 == mddev)
5543 continue;
5544 if (mddev2->curr_resync &&
5545 match_mddev_units(mddev,mddev2)) {
5546 DEFINE_WAIT(wq);
5547 if (mddev < mddev2 && mddev->curr_resync == 2) {
5548 /* arbitrarily yield */
5549 mddev->curr_resync = 1;
5550 wake_up(&resync_wait);
5551 }
5552 if (mddev > mddev2 && mddev->curr_resync == 1)
5553 /* no need to wait here, we can wait the next
5554 * time 'round when curr_resync == 2
5555 */
5556 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005557 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5558 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005559 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005560 printk(KERN_INFO "md: delaying %s of %s"
5561 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005563 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 mddev_put(mddev2);
5565 schedule();
5566 finish_wait(&resync_wait, &wq);
5567 goto try_again;
5568 }
5569 finish_wait(&resync_wait, &wq);
5570 }
5571 }
5572 } while (mddev->curr_resync < 2);
5573
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005574 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005575 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005577 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 */
5579 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005580 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005581 /* we don't use the checkpoint if there's a bitmap */
5582 if (!mddev->bitmap &&
5583 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5584 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005585 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5586 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005587 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 /* recovery follows the physical size of devices */
5589 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005590 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005591 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005592 if (rdev->raid_disk >= 0 &&
5593 !test_bit(Faulty, &rdev->flags) &&
5594 !test_bit(In_sync, &rdev->flags) &&
5595 rdev->recovery_offset < j)
5596 j = rdev->recovery_offset;
5597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598
NeilBrown61df9d92006-10-03 01:15:57 -07005599 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5600 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5601 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005602 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005603 "(but not more than %d KB/sec) for %s.\n",
5604 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605
5606 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005607
NeilBrown57afd892005-06-21 17:17:13 -07005608 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 for (m = 0; m < SYNC_MARKS; m++) {
5610 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005611 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 }
5613 last_mark = 0;
5614 mddev->resync_mark = mark[last_mark];
5615 mddev->resync_mark_cnt = mark_cnt[last_mark];
5616
5617 /*
5618 * Tune reconstruction:
5619 */
5620 window = 32*(PAGE_SIZE/512);
5621 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5622 window/2,(unsigned long long) max_sectors/2);
5623
5624 atomic_set(&mddev->recovery_active, 0);
5625 init_waitqueue_head(&mddev->recovery_wait);
5626 last_check = 0;
5627
5628 if (j>2) {
5629 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005630 "md: resuming %s of %s from checkpoint.\n",
5631 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 mddev->curr_resync = j;
5633 }
5634
5635 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005636 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
NeilBrown57afd892005-06-21 17:17:13 -07005638 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005639 if (j >= mddev->resync_max) {
5640 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5641 wait_event(mddev->recovery_wait,
5642 mddev->resync_max > j
5643 || kthread_should_stop());
5644 }
5645 if (kthread_should_stop())
5646 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005647 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005648 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005649 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5651 goto out;
5652 }
NeilBrown57afd892005-06-21 17:17:13 -07005653
5654 if (!skipped) { /* actual IO requested */
5655 io_sectors += sectors;
5656 atomic_add(sectors, &mddev->recovery_active);
5657 }
5658
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 j += sectors;
5660 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005661 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005662 if (last_check == 0)
5663 /* this is the earliers that rebuilt will be
5664 * visible in /proc/mdstat
5665 */
5666 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005667
5668 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 continue;
5670
NeilBrown57afd892005-06-21 17:17:13 -07005671 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
5673 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5674 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5675 break;
5676
5677 repeat:
5678 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5679 /* step marks */
5680 int next = (last_mark+1) % SYNC_MARKS;
5681
5682 mddev->resync_mark = mark[next];
5683 mddev->resync_mark_cnt = mark_cnt[next];
5684 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005685 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 last_mark = next;
5687 }
5688
5689
NeilBrownc6207272008-02-06 01:39:52 -08005690 if (kthread_should_stop())
5691 goto interrupted;
5692
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
5694 /*
5695 * this loop exits only if either when we are slower than
5696 * the 'hard' speed limit, or the system was IO-idle for
5697 * a jiffy.
5698 * the system might be non-idle CPU-wise, but we only care
5699 * about not overloading the IO subsystem. (things like an
5700 * e2fsck being done on the RAID array should execute fast)
5701 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005702 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 cond_resched();
5704
NeilBrown57afd892005-06-21 17:17:13 -07005705 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5706 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707
NeilBrown88202a02006-01-06 00:21:36 -08005708 if (currspeed > speed_min(mddev)) {
5709 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005711 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 goto repeat;
5713 }
5714 }
5715 }
NeilBrown61df9d92006-10-03 01:15:57 -07005716 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 /*
5718 * this also signals 'finished resyncing' to md_stop
5719 */
5720 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005721 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5724
5725 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005726 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727
5728 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005729 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005730 mddev->curr_resync > 2) {
5731 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5732 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5733 if (mddev->curr_resync >= mddev->recovery_cp) {
5734 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005735 "md: checkpointing %s of %s.\n",
5736 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005737 mddev->recovery_cp = mddev->curr_resync;
5738 }
5739 } else
5740 mddev->recovery_cp = MaxSector;
5741 } else {
5742 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5743 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005744 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005745 if (rdev->raid_disk >= 0 &&
5746 !test_bit(Faulty, &rdev->flags) &&
5747 !test_bit(In_sync, &rdev->flags) &&
5748 rdev->recovery_offset < mddev->curr_resync)
5749 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 }
NeilBrown17571282006-12-10 02:20:52 -08005752 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 skip:
5755 mddev->curr_resync = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005756 mddev->resync_max = MaxSector;
5757 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 wake_up(&resync_wait);
5759 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5760 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005761 return;
5762
5763 interrupted:
5764 /*
5765 * got a signal, exit.
5766 */
5767 printk(KERN_INFO
5768 "md: md_do_sync() got signal ... exiting\n");
5769 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5770 goto out;
5771
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772}
NeilBrown29269552006-03-27 01:18:10 -08005773EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774
5775
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005776static int remove_and_add_spares(mddev_t *mddev)
5777{
5778 mdk_rdev_t *rdev;
5779 struct list_head *rtmp;
5780 int spares = 0;
5781
NeilBrownd089c6a2008-02-06 01:39:59 -08005782 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005783 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005784 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005785 (test_bit(Faulty, &rdev->flags) ||
5786 ! test_bit(In_sync, &rdev->flags)) &&
5787 atomic_read(&rdev->nr_pending)==0) {
5788 if (mddev->pers->hot_remove_disk(
5789 mddev, rdev->raid_disk)==0) {
5790 char nm[20];
5791 sprintf(nm,"rd%d", rdev->raid_disk);
5792 sysfs_remove_link(&mddev->kobj, nm);
5793 rdev->raid_disk = -1;
5794 }
5795 }
5796
5797 if (mddev->degraded) {
NeilBrownd089c6a2008-02-06 01:39:59 -08005798 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005799 if (rdev->raid_disk < 0
5800 && !test_bit(Faulty, &rdev->flags)) {
5801 rdev->recovery_offset = 0;
5802 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5803 char nm[20];
5804 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005805 if (sysfs_create_link(&mddev->kobj,
5806 &rdev->kobj, nm))
5807 printk(KERN_WARNING
5808 "md: cannot register "
5809 "%s for %s\n",
5810 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005811 spares++;
5812 md_new_event(mddev);
5813 } else
5814 break;
5815 }
5816 }
5817 return spares;
5818}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819/*
5820 * This routine is regularly called by all per-raid-array threads to
5821 * deal with generic issues like resync and super-block update.
5822 * Raid personalities that don't have a thread (linear/raid0) do not
5823 * need this as they never do any recovery or update the superblock.
5824 *
5825 * It does not do any resync itself, but rather "forks" off other threads
5826 * to do that as needed.
5827 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5828 * "->recovery" and create a thread at ->sync_thread.
5829 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5830 * and wakeups up this thread which will reap the thread and finish up.
5831 * This thread also removes any faulty devices (with nr_pending == 0).
5832 *
5833 * The overall approach is:
5834 * 1/ if the superblock needs updating, update it.
5835 * 2/ If a recovery thread is running, don't do anything else.
5836 * 3/ If recovery has finished, clean up, possibly marking spares active.
5837 * 4/ If there are any faulty devices, remove them.
5838 * 5/ If array is degraded, try to add spares devices
5839 * 6/ If array has spares or is not in-sync, start a resync thread.
5840 */
5841void md_check_recovery(mddev_t *mddev)
5842{
5843 mdk_rdev_t *rdev;
5844 struct list_head *rtmp;
5845
5846
NeilBrown5f404022005-06-21 17:17:16 -07005847 if (mddev->bitmap)
5848 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849
5850 if (mddev->ro)
5851 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005852
5853 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07005854 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07005855 printk(KERN_INFO "md: %s in immediate safe mode\n",
5856 mdname(mddev));
5857 mddev->safemode = 2;
5858 }
5859 flush_signals(current);
5860 }
5861
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08005863 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005865 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07005866 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005867 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5868 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 ))
5870 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005871
NeilBrowndf5b89b2006-03-27 01:18:20 -08005872 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005873 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005874
NeilBrown31a59e32008-04-30 00:52:30 -07005875 if (!mddev->external) {
5876 spin_lock_irq(&mddev->write_lock);
5877 if (mddev->safemode &&
5878 !atomic_read(&mddev->writes_pending) &&
5879 !mddev->in_sync &&
5880 mddev->recovery_cp == MaxSector) {
5881 mddev->in_sync = 1;
5882 if (mddev->persistent)
5883 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5884 }
5885 if (mddev->safemode == 1)
5886 mddev->safemode = 0;
5887 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005888 }
NeilBrownfca4d842005-06-21 17:17:11 -07005889
NeilBrown850b2b42006-10-03 01:15:46 -07005890 if (mddev->flags)
5891 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005892
NeilBrown06d91a52005-06-21 17:17:12 -07005893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5895 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5896 /* resync/recovery still happening */
5897 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5898 goto unlock;
5899 }
5900 if (mddev->sync_thread) {
5901 /* resync has finished, collect result */
5902 md_unregister_thread(mddev->sync_thread);
5903 mddev->sync_thread = NULL;
5904 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5905 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5906 /* success...*/
5907 /* activate any spares */
5908 mddev->pers->spare_active(mddev);
5909 }
NeilBrown850b2b42006-10-03 01:15:46 -07005910 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005911
5912 /* if array is no-longer degraded, then any saved_raid_disk
5913 * information must be scrapped
5914 */
5915 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08005916 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07005917 rdev->saved_raid_disk = -1;
5918
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 mddev->recovery = 0;
5920 /* flag recovery needed just to double check */
5921 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005922 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 goto unlock;
5924 }
NeilBrown24dd4692005-11-08 21:39:26 -08005925 /* Clear some bits that don't mean anything, but
5926 * might be left set
5927 */
5928 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5929 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5930 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5931 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005933 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5934 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 /* no recovery is running.
5936 * remove any failed drives, then
5937 * add spares if possible.
5938 * Spare are also removed and re-added, to allow
5939 * the personality to fail the re-add.
5940 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005942 if (mddev->reshape_position != MaxSector) {
5943 if (mddev->pers->check_reshape(mddev) != 0)
5944 /* Cannot proceed */
5945 goto unlock;
5946 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5947 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005948 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5949 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5950 } else if (mddev->recovery_cp < MaxSector) {
5951 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5952 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5953 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 if (mddev->pers->sync_request) {
5957 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005958 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5959 /* We are adding a device or devices to an array
5960 * which has the bitmap stored on all devices.
5961 * So make sure all bitmap pages get written
5962 */
5963 bitmap_write_all(mddev->bitmap);
5964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 mddev->sync_thread = md_register_thread(md_do_sync,
5966 mddev,
5967 "%s_resync");
5968 if (!mddev->sync_thread) {
5969 printk(KERN_ERR "%s: could not start resync"
5970 " thread...\n",
5971 mdname(mddev));
5972 /* leave the spares where they are, it shouldn't hurt */
5973 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005974 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005976 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 }
5978 unlock:
5979 mddev_unlock(mddev);
5980 }
5981}
5982
Dan Williams6bfe0b42008-04-30 00:52:32 -07005983void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
5984{
5985 sysfs_notify(&rdev->kobj, NULL, "state");
5986 wait_event_timeout(rdev->blocked_wait,
5987 !test_bit(Blocked, &rdev->flags),
5988 msecs_to_jiffies(5000));
5989 rdev_dec_pending(rdev, mddev);
5990}
5991EXPORT_SYMBOL(md_wait_for_blocked_rdev);
5992
Adrian Bunk75c96f82005-05-05 16:16:09 -07005993static int md_notify_reboot(struct notifier_block *this,
5994 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995{
5996 struct list_head *tmp;
5997 mddev_t *mddev;
5998
5999 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6000
6001 printk(KERN_INFO "md: stopping all md devices.\n");
6002
NeilBrown29ac4aa2008-02-06 01:39:58 -08006003 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006004 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10006006 mddev_unlock(mddev);
6007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 /*
6009 * certain more exotic SCSI devices are known to be
6010 * volatile wrt too early system reboots. While the
6011 * right place to handle this issue is the given
6012 * driver, we do want to have a safe RAID driver ...
6013 */
6014 mdelay(1000*1);
6015 }
6016 return NOTIFY_DONE;
6017}
6018
Adrian Bunk75c96f82005-05-05 16:16:09 -07006019static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020 .notifier_call = md_notify_reboot,
6021 .next = NULL,
6022 .priority = INT_MAX, /* before any real devices */
6023};
6024
6025static void md_geninit(void)
6026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6028
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006029 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030}
6031
Adrian Bunk75c96f82005-05-05 16:16:09 -07006032static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 if (register_blkdev(MAJOR_NR, "md"))
6035 return -1;
6036 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6037 unregister_blkdev(MAJOR_NR, "md");
6038 return -1;
6039 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006040 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6041 md_probe, NULL, NULL);
6042 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043 md_probe, NULL, NULL);
6044
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006046 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047
6048 md_geninit();
6049 return (0);
6050}
6051
6052
6053#ifndef MODULE
6054
6055/*
6056 * Searches all registered partitions for autorun RAID arrays
6057 * at boot time.
6058 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006059
6060static LIST_HEAD(all_detected_devices);
6061struct detected_devices_node {
6062 struct list_head list;
6063 dev_t dev;
6064};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
6066void md_autodetect_dev(dev_t dev)
6067{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006068 struct detected_devices_node *node_detected_dev;
6069
6070 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6071 if (node_detected_dev) {
6072 node_detected_dev->dev = dev;
6073 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6074 } else {
6075 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6076 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078}
6079
6080
6081static void autostart_arrays(int part)
6082{
6083 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006084 struct detected_devices_node *node_detected_dev;
6085 dev_t dev;
6086 int i_scanned, i_passed;
6087
6088 i_scanned = 0;
6089 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090
6091 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6092
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006093 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6094 i_scanned++;
6095 node_detected_dev = list_entry(all_detected_devices.next,
6096 struct detected_devices_node, list);
6097 list_del(&node_detected_dev->list);
6098 dev = node_detected_dev->dev;
6099 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006100 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 if (IS_ERR(rdev))
6102 continue;
6103
NeilBrownb2d444d2005-11-08 21:39:31 -08006104 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 MD_BUG();
6106 continue;
6107 }
NeilBrownd0fae182008-03-04 14:29:31 -08006108 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006110 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006112
6113 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6114 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115
6116 autorun_devices(part);
6117}
6118
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006119#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
6121static __exit void md_exit(void)
6122{
6123 mddev_t *mddev;
6124 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006125
NeilBrowne8703fe2006-10-03 01:15:59 -07006126 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6127 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128
6129 unregister_blkdev(MAJOR_NR,"md");
6130 unregister_blkdev(mdp_major, "mdp");
6131 unregister_reboot_notifier(&md_notifier);
6132 unregister_sysctl_table(raid_table_header);
6133 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006134 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006135 struct gendisk *disk = mddev->gendisk;
6136 if (!disk)
6137 continue;
6138 export_array(mddev);
6139 del_gendisk(disk);
6140 put_disk(disk);
6141 mddev->gendisk = NULL;
6142 mddev_put(mddev);
6143 }
6144}
6145
Dan Williams685784a2007-07-09 11:56:42 -07006146subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147module_exit(md_exit)
6148
NeilBrownf91de922005-11-08 21:39:36 -08006149static int get_ro(char *buffer, struct kernel_param *kp)
6150{
6151 return sprintf(buffer, "%d", start_readonly);
6152}
6153static int set_ro(const char *val, struct kernel_param *kp)
6154{
6155 char *e;
6156 int num = simple_strtoul(val, &e, 10);
6157 if (*val && (*e == '\0' || *e == '\n')) {
6158 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006159 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006160 }
6161 return -EINVAL;
6162}
6163
NeilBrown80ca3a42006-07-10 04:44:18 -07006164module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6165module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006166
NeilBrownf91de922005-11-08 21:39:36 -08006167
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168EXPORT_SYMBOL(register_md_personality);
6169EXPORT_SYMBOL(unregister_md_personality);
6170EXPORT_SYMBOL(md_error);
6171EXPORT_SYMBOL(md_done_sync);
6172EXPORT_SYMBOL(md_write_start);
6173EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174EXPORT_SYMBOL(md_register_thread);
6175EXPORT_SYMBOL(md_unregister_thread);
6176EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177EXPORT_SYMBOL(md_check_recovery);
6178MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006179MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006180MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);