blob: 7a44c81ae3cd2ec5f47b3e18b9184640d8fa6aeb [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 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001373 /* make sure rdev->size exceeds mddev->size */
1374 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001375 if (mddev->pers) {
1376 /* Cannot change size, so fail
1377 * If mddev->level <= 0, then we don't care
1378 * about aligning sizes (e.g. linear)
1379 */
1380 if (mddev->level > 0)
1381 return -ENOSPC;
1382 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001383 mddev->size = rdev->size;
1384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /* Verify rdev->desc_nr is unique.
1387 * If it is -1, assign a free number, else
1388 * check number is not in use
1389 */
1390 if (rdev->desc_nr < 0) {
1391 int choice = 0;
1392 if (mddev->pers) choice = mddev->raid_disks;
1393 while (find_rdev_nr(mddev, choice))
1394 choice++;
1395 rdev->desc_nr = choice;
1396 } else {
1397 if (find_rdev_nr(mddev, rdev->desc_nr))
1398 return -EBUSY;
1399 }
NeilBrown19133a42005-11-08 21:39:35 -08001400 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001401 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001402 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001405 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001406
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001407 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001408 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001409
NeilBrownf637b9f2005-11-08 21:39:37 -08001410 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001411 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001412 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001413 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001414 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1415 kobject_del(&rdev->kobj);
1416 goto fail;
1417 }
1418 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001419 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001421
1422 fail:
1423 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1424 b, mdname(mddev));
1425 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
NeilBrown177a99b2008-02-06 01:39:56 -08001428static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001429{
1430 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1431 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001432 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1436{
1437 char b[BDEVNAME_SIZE];
1438 if (!rdev->mddev) {
1439 MD_BUG();
1440 return;
1441 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001442 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 list_del_init(&rdev->same_set);
1444 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1445 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001446 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001447
1448 /* We need to delay this, otherwise we can deadlock when
1449 * writing to 'remove' to "dev/state"
1450 */
NeilBrown177a99b2008-02-06 01:39:56 -08001451 INIT_WORK(&rdev->del_work, md_delayed_delete);
1452 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001453 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
1456/*
1457 * prevent the device from being mounted, repartitioned or
1458 * otherwise reused by a RAID array (or any other kernel
1459 * subsystem), by bd_claiming the device.
1460 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001461static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 int err = 0;
1464 struct block_device *bdev;
1465 char b[BDEVNAME_SIZE];
1466
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001467 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (IS_ERR(bdev)) {
1469 printk(KERN_ERR "md: could not open %s.\n",
1470 __bdevname(dev, b));
1471 return PTR_ERR(bdev);
1472 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001473 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (err) {
1475 printk(KERN_ERR "md: could not bd_claim %s.\n",
1476 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001477 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return err;
1479 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001480 if (!shared)
1481 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 rdev->bdev = bdev;
1483 return err;
1484}
1485
1486static void unlock_rdev(mdk_rdev_t *rdev)
1487{
1488 struct block_device *bdev = rdev->bdev;
1489 rdev->bdev = NULL;
1490 if (!bdev)
1491 MD_BUG();
1492 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001493 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496void md_autodetect_dev(dev_t dev);
1497
1498static void export_rdev(mdk_rdev_t * rdev)
1499{
1500 char b[BDEVNAME_SIZE];
1501 printk(KERN_INFO "md: export_rdev(%s)\n",
1502 bdevname(rdev->bdev,b));
1503 if (rdev->mddev)
1504 MD_BUG();
1505 free_disk_sb(rdev);
1506 list_del_init(&rdev->same_set);
1507#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001508 if (test_bit(AutoDetected, &rdev->flags))
1509 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510#endif
1511 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001512 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515static void kick_rdev_from_array(mdk_rdev_t * rdev)
1516{
1517 unbind_rdev_from_array(rdev);
1518 export_rdev(rdev);
1519}
1520
1521static void export_array(mddev_t *mddev)
1522{
1523 struct list_head *tmp;
1524 mdk_rdev_t *rdev;
1525
NeilBrownd089c6a2008-02-06 01:39:59 -08001526 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (!rdev->mddev) {
1528 MD_BUG();
1529 continue;
1530 }
1531 kick_rdev_from_array(rdev);
1532 }
1533 if (!list_empty(&mddev->disks))
1534 MD_BUG();
1535 mddev->raid_disks = 0;
1536 mddev->major_version = 0;
1537}
1538
1539static void print_desc(mdp_disk_t *desc)
1540{
1541 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1542 desc->major,desc->minor,desc->raid_disk,desc->state);
1543}
1544
1545static void print_sb(mdp_super_t *sb)
1546{
1547 int i;
1548
1549 printk(KERN_INFO
1550 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1551 sb->major_version, sb->minor_version, sb->patch_version,
1552 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1553 sb->ctime);
1554 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1555 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1556 sb->md_minor, sb->layout, sb->chunk_size);
1557 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1558 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1559 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1560 sb->failed_disks, sb->spare_disks,
1561 sb->sb_csum, (unsigned long)sb->events_lo);
1562
1563 printk(KERN_INFO);
1564 for (i = 0; i < MD_SB_DISKS; i++) {
1565 mdp_disk_t *desc;
1566
1567 desc = sb->disks + i;
1568 if (desc->number || desc->major || desc->minor ||
1569 desc->raid_disk || (desc->state && (desc->state != 4))) {
1570 printk(" D %2d: ", i);
1571 print_desc(desc);
1572 }
1573 }
1574 printk(KERN_INFO "md: THIS: ");
1575 print_desc(&sb->this_disk);
1576
1577}
1578
1579static void print_rdev(mdk_rdev_t *rdev)
1580{
1581 char b[BDEVNAME_SIZE];
1582 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1583 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001584 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1585 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (rdev->sb_loaded) {
1587 printk(KERN_INFO "md: rdev superblock:\n");
1588 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1589 } else
1590 printk(KERN_INFO "md: no rdev superblock!\n");
1591}
1592
Adrian Bunk5e563412006-06-26 00:27:42 -07001593static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 struct list_head *tmp, *tmp2;
1596 mdk_rdev_t *rdev;
1597 mddev_t *mddev;
1598 char b[BDEVNAME_SIZE];
1599
1600 printk("\n");
1601 printk("md: **********************************\n");
1602 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1603 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001604 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
NeilBrown32a76272005-06-21 17:17:14 -07001606 if (mddev->bitmap)
1607 bitmap_print_sb(mddev->bitmap);
1608 else
1609 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001610 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 printk("<%s>", bdevname(rdev->bdev,b));
1612 printk("\n");
1613
NeilBrownd089c6a2008-02-06 01:39:59 -08001614 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 print_rdev(rdev);
1616 }
1617 printk("md: **********************************\n");
1618 printk("\n");
1619}
1620
1621
NeilBrown42543762006-06-26 00:27:57 -07001622static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
NeilBrown42543762006-06-26 00:27:57 -07001624 /* Update each superblock (in-memory image), but
1625 * if we are allowed to, skip spares which already
1626 * have the right event counter, or have one earlier
1627 * (which would mean they aren't being marked as dirty
1628 * with the rest of the array)
1629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 mdk_rdev_t *rdev;
1631 struct list_head *tmp;
1632
NeilBrownd089c6a2008-02-06 01:39:59 -08001633 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001634 if (rdev->sb_events == mddev->events ||
1635 (nospares &&
1636 rdev->raid_disk < 0 &&
1637 (rdev->sb_events&1)==0 &&
1638 rdev->sb_events+1 == mddev->events)) {
1639 /* Don't update this superblock */
1640 rdev->sb_loaded = 2;
1641 } else {
1642 super_types[mddev->major_version].
1643 sync_super(mddev, rdev);
1644 rdev->sb_loaded = 1;
1645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647}
1648
NeilBrown850b2b42006-10-03 01:15:46 -07001649static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 struct list_head *tmp;
1652 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001653 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001654 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
NeilBrown8377bc82008-04-30 00:52:28 -07001656 if (mddev->external)
1657 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001659 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001660
NeilBrown850b2b42006-10-03 01:15:46 -07001661 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1662 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1663 force_change = 1;
1664 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1665 /* just a clean<-> dirty transition, possibly leave spares alone,
1666 * though if events isn't the right even/odd, we will have to do
1667 * spares after all
1668 */
1669 nospares = 1;
1670 if (force_change)
1671 nospares = 0;
1672 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001673 /* If the array is degraded, then skipping spares is both
1674 * dangerous and fairly pointless.
1675 * Dangerous because a device that was removed from the array
1676 * might have a event_count that still looks up-to-date,
1677 * so it can be re-added without a resync.
1678 * Pointless because if there are any spares to skip,
1679 * then a recovery will happen and soon that array won't
1680 * be degraded any more and the spare can go back to sleep then.
1681 */
NeilBrown850b2b42006-10-03 01:15:46 -07001682 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001683
NeilBrown06d91a52005-06-21 17:17:12 -07001684 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001686
1687 /* If this is just a dirty<->clean transition, and the array is clean
1688 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001689 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001690 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001691 && (mddev->events & 1)
1692 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001693 mddev->events--;
1694 else {
1695 /* otherwise we have to go forward and ... */
1696 mddev->events ++;
1697 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1698 /* .. if the array isn't clean, insist on an odd 'events' */
1699 if ((mddev->events&1)==0) {
1700 mddev->events++;
1701 nospares = 0;
1702 }
1703 } else {
1704 /* otherwise insist on an even 'events' (for clean states) */
1705 if ((mddev->events&1)) {
1706 mddev->events++;
1707 nospares = 0;
1708 }
1709 }
1710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 if (!mddev->events) {
1713 /*
1714 * oops, this 64-bit counter should never wrap.
1715 * Either we are in around ~1 trillion A.C., assuming
1716 * 1 reboot per second, or we have a bug:
1717 */
1718 MD_BUG();
1719 mddev->events --;
1720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 /*
1723 * do not write anything to disk if using
1724 * nonpersistent superblocks
1725 */
NeilBrown06d91a52005-06-21 17:17:12 -07001726 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001727 if (!mddev->external)
1728 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1729
NeilBrowna9701a32005-11-08 21:39:34 -08001730 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001731 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001733 }
NeilBrowne6910632008-02-06 01:39:51 -08001734 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001735 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 dprintk(KERN_INFO
1738 "md: updating %s RAID superblock on device (in sync %d)\n",
1739 mdname(mddev),mddev->in_sync);
1740
NeilBrown4ad13662007-07-17 04:06:13 -07001741 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001742 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 char b[BDEVNAME_SIZE];
1744 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001745 if (rdev->sb_loaded != 1)
1746 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001747 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 dprintk("(skipping faulty ");
1749
1750 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001751 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001752 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001753 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001754 rdev->sb_page);
1755 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1756 bdevname(rdev->bdev,b),
1757 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001758 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 } else
1761 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001762 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 /* only need to write one superblock... */
1764 break;
1765 }
NeilBrowna9701a32005-11-08 21:39:34 -08001766 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001767 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001768
NeilBrowna9701a32005-11-08 21:39:34 -08001769 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001770 if (mddev->in_sync != sync_req ||
1771 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001772 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001773 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001774 goto repeat;
1775 }
NeilBrown850b2b42006-10-03 01:15:46 -07001776 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001777 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001778 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
NeilBrownbce74da2006-01-06 00:20:41 -08001782/* words written to sysfs files may, or my not, be \n terminated.
1783 * We want to accept with case. For this we use cmd_match.
1784 */
1785static int cmd_match(const char *cmd, const char *str)
1786{
1787 /* See if cmd, written into a sysfs file, matches
1788 * str. They must either be the same, or cmd can
1789 * have a trailing newline
1790 */
1791 while (*cmd && *str && *cmd == *str) {
1792 cmd++;
1793 str++;
1794 }
1795 if (*cmd == '\n')
1796 cmd++;
1797 if (*str || *cmd)
1798 return 0;
1799 return 1;
1800}
1801
NeilBrown86e6ffd2005-11-08 21:39:24 -08001802struct rdev_sysfs_entry {
1803 struct attribute attr;
1804 ssize_t (*show)(mdk_rdev_t *, char *);
1805 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1806};
1807
1808static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001809state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001810{
1811 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001812 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001813
NeilBrownb2d444d2005-11-08 21:39:31 -08001814 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001815 len+= sprintf(page+len, "%sfaulty",sep);
1816 sep = ",";
1817 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001818 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001819 len += sprintf(page+len, "%sin_sync",sep);
1820 sep = ",";
1821 }
NeilBrownf6556752006-06-26 00:28:01 -07001822 if (test_bit(WriteMostly, &rdev->flags)) {
1823 len += sprintf(page+len, "%swrite_mostly",sep);
1824 sep = ",";
1825 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001826 if (!test_bit(Faulty, &rdev->flags) &&
1827 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001828 len += sprintf(page+len, "%sspare", sep);
1829 sep = ",";
1830 }
1831 return len+sprintf(page+len, "\n");
1832}
1833
NeilBrown45dc2de2006-06-26 00:27:58 -07001834static ssize_t
1835state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1836{
1837 /* can write
1838 * faulty - simulates and error
1839 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001840 * writemostly - sets write_mostly
1841 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001842 */
1843 int err = -EINVAL;
1844 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1845 md_error(rdev->mddev, rdev);
1846 err = 0;
1847 } else if (cmd_match(buf, "remove")) {
1848 if (rdev->raid_disk >= 0)
1849 err = -EBUSY;
1850 else {
1851 mddev_t *mddev = rdev->mddev;
1852 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001853 if (mddev->pers)
1854 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001855 md_new_event(mddev);
1856 err = 0;
1857 }
NeilBrownf6556752006-06-26 00:28:01 -07001858 } else if (cmd_match(buf, "writemostly")) {
1859 set_bit(WriteMostly, &rdev->flags);
1860 err = 0;
1861 } else if (cmd_match(buf, "-writemostly")) {
1862 clear_bit(WriteMostly, &rdev->flags);
1863 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001864 }
1865 return err ? err : len;
1866}
NeilBrown80ca3a42006-07-10 04:44:18 -07001867static struct rdev_sysfs_entry rdev_state =
1868__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001869
1870static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001871errors_show(mdk_rdev_t *rdev, char *page)
1872{
1873 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1874}
1875
1876static ssize_t
1877errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1878{
1879 char *e;
1880 unsigned long n = simple_strtoul(buf, &e, 10);
1881 if (*buf && (*e == 0 || *e == '\n')) {
1882 atomic_set(&rdev->corrected_errors, n);
1883 return len;
1884 }
1885 return -EINVAL;
1886}
1887static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001888__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001889
NeilBrown014236d2006-01-06 00:20:55 -08001890static ssize_t
1891slot_show(mdk_rdev_t *rdev, char *page)
1892{
1893 if (rdev->raid_disk < 0)
1894 return sprintf(page, "none\n");
1895 else
1896 return sprintf(page, "%d\n", rdev->raid_disk);
1897}
1898
1899static ssize_t
1900slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1901{
1902 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001903 int err;
1904 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001905 int slot = simple_strtoul(buf, &e, 10);
1906 if (strncmp(buf, "none", 4)==0)
1907 slot = -1;
1908 else if (e==buf || (*e && *e!= '\n'))
1909 return -EINVAL;
NeilBrownc303da62008-02-06 01:39:51 -08001910 if (rdev->mddev->pers) {
1911 /* Setting 'slot' on an active array requires also
1912 * updating the 'rd%d' link, and communicating
1913 * with the personality with ->hot_*_disk.
1914 * For now we only support removing
1915 * failed/spare devices. This normally happens automatically,
1916 * but not when the metadata is externally managed.
1917 */
1918 if (slot != -1)
1919 return -EBUSY;
1920 if (rdev->raid_disk == -1)
1921 return -EEXIST;
1922 /* personality does all needed checks */
1923 if (rdev->mddev->pers->hot_add_disk == NULL)
1924 return -EINVAL;
1925 err = rdev->mddev->pers->
1926 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1927 if (err)
1928 return err;
1929 sprintf(nm, "rd%d", rdev->raid_disk);
1930 sysfs_remove_link(&rdev->mddev->kobj, nm);
1931 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1932 md_wakeup_thread(rdev->mddev->thread);
1933 } else {
1934 if (slot >= rdev->mddev->raid_disks)
1935 return -ENOSPC;
1936 rdev->raid_disk = slot;
1937 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001938 clear_bit(Faulty, &rdev->flags);
1939 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08001940 set_bit(In_sync, &rdev->flags);
1941 }
NeilBrown014236d2006-01-06 00:20:55 -08001942 return len;
1943}
1944
1945
1946static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001947__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001948
NeilBrown93c8cad2006-01-06 00:20:56 -08001949static ssize_t
1950offset_show(mdk_rdev_t *rdev, char *page)
1951{
Andrew Morton6961ece2006-01-06 00:20:59 -08001952 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001953}
1954
1955static ssize_t
1956offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1957{
1958 char *e;
1959 unsigned long long offset = simple_strtoull(buf, &e, 10);
1960 if (e==buf || (*e && *e != '\n'))
1961 return -EINVAL;
1962 if (rdev->mddev->pers)
1963 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001964 if (rdev->size && rdev->mddev->external)
1965 /* Must set offset before size, so overlap checks
1966 * can be sane */
1967 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08001968 rdev->data_offset = offset;
1969 return len;
1970}
1971
1972static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001973__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001974
NeilBrown83303b62006-01-06 00:21:06 -08001975static ssize_t
1976rdev_size_show(mdk_rdev_t *rdev, char *page)
1977{
1978 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1979}
1980
NeilBrownc5d79ad2008-02-06 01:39:54 -08001981static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
1982{
1983 /* check if two start/length pairs overlap */
1984 if (s1+l1 <= s2)
1985 return 0;
1986 if (s2+l2 <= s1)
1987 return 0;
1988 return 1;
1989}
1990
NeilBrown83303b62006-01-06 00:21:06 -08001991static ssize_t
1992rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1993{
1994 char *e;
1995 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001996 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08001997 mddev_t *my_mddev = rdev->mddev;
1998
NeilBrown83303b62006-01-06 00:21:06 -08001999 if (e==buf || (*e && *e != '\n'))
2000 return -EINVAL;
NeilBrown27c529b2008-03-04 14:29:33 -08002001 if (my_mddev->pers)
NeilBrown83303b62006-01-06 00:21:06 -08002002 return -EBUSY;
2003 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002004 if (size > oldsize && rdev->mddev->external) {
2005 /* need to check that all other rdevs with the same ->bdev
2006 * do not overlap. We need to unlock the mddev to avoid
2007 * a deadlock. We have already changed rdev->size, and if
2008 * we have to change it back, we will have the lock again.
2009 */
2010 mddev_t *mddev;
2011 int overlap = 0;
2012 struct list_head *tmp, *tmp2;
2013
NeilBrown27c529b2008-03-04 14:29:33 -08002014 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002015 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002016 mdk_rdev_t *rdev2;
2017
2018 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002019 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002020 if (test_bit(AllReserved, &rdev2->flags) ||
2021 (rdev->bdev == rdev2->bdev &&
2022 rdev != rdev2 &&
2023 overlaps(rdev->data_offset, rdev->size,
2024 rdev2->data_offset, rdev2->size))) {
2025 overlap = 1;
2026 break;
2027 }
2028 mddev_unlock(mddev);
2029 if (overlap) {
2030 mddev_put(mddev);
2031 break;
2032 }
2033 }
NeilBrown27c529b2008-03-04 14:29:33 -08002034 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002035 if (overlap) {
2036 /* Someone else could have slipped in a size
2037 * change here, but doing so is just silly.
2038 * We put oldsize back because we *know* it is
2039 * safe, and trust userspace not to race with
2040 * itself
2041 */
2042 rdev->size = oldsize;
2043 return -EBUSY;
2044 }
2045 }
NeilBrown27c529b2008-03-04 14:29:33 -08002046 if (size < my_mddev->size || my_mddev->size == 0)
2047 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002048 return len;
2049}
2050
2051static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002052__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002053
NeilBrown86e6ffd2005-11-08 21:39:24 -08002054static struct attribute *rdev_default_attrs[] = {
2055 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002056 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002057 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002058 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002059 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002060 NULL,
2061};
2062static ssize_t
2063rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2064{
2065 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2066 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002067 mddev_t *mddev = rdev->mddev;
2068 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002069
2070 if (!entry->show)
2071 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002072
2073 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2074 if (!rv) {
2075 if (rdev->mddev == NULL)
2076 rv = -EBUSY;
2077 else
2078 rv = entry->show(rdev, page);
2079 mddev_unlock(mddev);
2080 }
2081 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002082}
2083
2084static ssize_t
2085rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2086 const char *page, size_t length)
2087{
2088 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2089 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002090 ssize_t rv;
2091 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002092
2093 if (!entry->store)
2094 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002095 if (!capable(CAP_SYS_ADMIN))
2096 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002097 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002098 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002099 if (rdev->mddev == NULL)
2100 rv = -EBUSY;
2101 else
2102 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002103 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002104 }
2105 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002106}
2107
2108static void rdev_free(struct kobject *ko)
2109{
2110 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2111 kfree(rdev);
2112}
2113static struct sysfs_ops rdev_sysfs_ops = {
2114 .show = rdev_attr_show,
2115 .store = rdev_attr_store,
2116};
2117static struct kobj_type rdev_ktype = {
2118 .release = rdev_free,
2119 .sysfs_ops = &rdev_sysfs_ops,
2120 .default_attrs = rdev_default_attrs,
2121};
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123/*
2124 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2125 *
2126 * mark the device faulty if:
2127 *
2128 * - the device is nonexistent (zero size)
2129 * - the device has no valid superblock
2130 *
2131 * a faulty rdev _never_ has rdev->sb set.
2132 */
2133static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2134{
2135 char b[BDEVNAME_SIZE];
2136 int err;
2137 mdk_rdev_t *rdev;
2138 sector_t size;
2139
NeilBrown9ffae0c2006-01-06 00:20:32 -08002140 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (!rdev) {
2142 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2143 return ERR_PTR(-ENOMEM);
2144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 if ((err = alloc_disk_sb(rdev)))
2147 goto abort_free;
2148
NeilBrownc5d79ad2008-02-06 01:39:54 -08002149 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (err)
2151 goto abort_free;
2152
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002153 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002156 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002157 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002158 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002160 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002162 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002163 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2166 if (!size) {
2167 printk(KERN_WARNING
2168 "md: %s has zero or unknown size, marking faulty!\n",
2169 bdevname(rdev->bdev,b));
2170 err = -EINVAL;
2171 goto abort_free;
2172 }
2173
2174 if (super_format >= 0) {
2175 err = super_types[super_format].
2176 load_super(rdev, NULL, super_minor);
2177 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002178 printk(KERN_WARNING
2179 "md: %s does not have a valid v%d.%d "
2180 "superblock, not importing!\n",
2181 bdevname(rdev->bdev,b),
2182 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 goto abort_free;
2184 }
2185 if (err < 0) {
2186 printk(KERN_WARNING
2187 "md: could not read %s's sb, not importing!\n",
2188 bdevname(rdev->bdev,b));
2189 goto abort_free;
2190 }
2191 }
2192 INIT_LIST_HEAD(&rdev->same_set);
2193
2194 return rdev;
2195
2196abort_free:
2197 if (rdev->sb_page) {
2198 if (rdev->bdev)
2199 unlock_rdev(rdev);
2200 free_disk_sb(rdev);
2201 }
2202 kfree(rdev);
2203 return ERR_PTR(err);
2204}
2205
2206/*
2207 * Check a full RAID array for plausibility
2208 */
2209
2210
NeilBrowna757e642005-04-16 15:26:42 -07002211static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 int i;
2214 struct list_head *tmp;
2215 mdk_rdev_t *rdev, *freshest;
2216 char b[BDEVNAME_SIZE];
2217
2218 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002219 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 switch (super_types[mddev->major_version].
2221 load_super(rdev, freshest, mddev->minor_version)) {
2222 case 1:
2223 freshest = rdev;
2224 break;
2225 case 0:
2226 break;
2227 default:
2228 printk( KERN_ERR \
2229 "md: fatal superblock inconsistency in %s"
2230 " -- removing from array\n",
2231 bdevname(rdev->bdev,b));
2232 kick_rdev_from_array(rdev);
2233 }
2234
2235
2236 super_types[mddev->major_version].
2237 validate_super(mddev, freshest);
2238
2239 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002240 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (rdev != freshest)
2242 if (super_types[mddev->major_version].
2243 validate_super(mddev, rdev)) {
2244 printk(KERN_WARNING "md: kicking non-fresh %s"
2245 " from array!\n",
2246 bdevname(rdev->bdev,b));
2247 kick_rdev_from_array(rdev);
2248 continue;
2249 }
2250 if (mddev->level == LEVEL_MULTIPATH) {
2251 rdev->desc_nr = i++;
2252 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002253 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002254 } else if (rdev->raid_disk >= mddev->raid_disks) {
2255 rdev->raid_disk = -1;
2256 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 }
2258 }
2259
2260
2261
2262 if (mddev->recovery_cp != MaxSector &&
2263 mddev->level >= 1)
2264 printk(KERN_ERR "md: %s: raid array is not clean"
2265 " -- starting background reconstruction\n",
2266 mdname(mddev));
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
NeilBrowneae17012005-11-08 21:39:23 -08002270static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002271safe_delay_show(mddev_t *mddev, char *page)
2272{
2273 int msec = (mddev->safemode_delay*1000)/HZ;
2274 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2275}
2276static ssize_t
2277safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2278{
2279 int scale=1;
2280 int dot=0;
2281 int i;
2282 unsigned long msec;
2283 char buf[30];
2284 char *e;
2285 /* remove a period, and count digits after it */
2286 if (len >= sizeof(buf))
2287 return -EINVAL;
2288 strlcpy(buf, cbuf, len);
2289 buf[len] = 0;
2290 for (i=0; i<len; i++) {
2291 if (dot) {
2292 if (isdigit(buf[i])) {
2293 buf[i-1] = buf[i];
2294 scale *= 10;
2295 }
2296 buf[i] = 0;
2297 } else if (buf[i] == '.') {
2298 dot=1;
2299 buf[i] = 0;
2300 }
2301 }
2302 msec = simple_strtoul(buf, &e, 10);
2303 if (e == buf || (*e && *e != '\n'))
2304 return -EINVAL;
2305 msec = (msec * 1000) / scale;
2306 if (msec == 0)
2307 mddev->safemode_delay = 0;
2308 else {
2309 mddev->safemode_delay = (msec*HZ)/1000;
2310 if (mddev->safemode_delay == 0)
2311 mddev->safemode_delay = 1;
2312 }
2313 return len;
2314}
2315static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002316__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002317
2318static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002319level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002320{
NeilBrown2604b702006-01-06 00:20:36 -08002321 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002322 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002323 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002324 else if (mddev->clevel[0])
2325 return sprintf(page, "%s\n", mddev->clevel);
2326 else if (mddev->level != LEVEL_NONE)
2327 return sprintf(page, "%d\n", mddev->level);
2328 else
2329 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002330}
2331
NeilBrownd9d166c2006-01-06 00:20:51 -08002332static ssize_t
2333level_store(mddev_t *mddev, const char *buf, size_t len)
2334{
NeilBrown20a49ff2008-02-06 01:39:57 -08002335 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002336 if (mddev->pers)
2337 return -EBUSY;
2338 if (len == 0)
2339 return 0;
2340 if (len >= sizeof(mddev->clevel))
2341 return -ENOSPC;
2342 strncpy(mddev->clevel, buf, len);
2343 if (mddev->clevel[len-1] == '\n')
2344 len--;
2345 mddev->clevel[len] = 0;
2346 mddev->level = LEVEL_NONE;
2347 return rv;
2348}
2349
2350static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002351__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002352
NeilBrownd4dbd022006-06-26 00:27:59 -07002353
2354static ssize_t
2355layout_show(mddev_t *mddev, char *page)
2356{
2357 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002358 if (mddev->reshape_position != MaxSector &&
2359 mddev->layout != mddev->new_layout)
2360 return sprintf(page, "%d (%d)\n",
2361 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002362 return sprintf(page, "%d\n", mddev->layout);
2363}
2364
2365static ssize_t
2366layout_store(mddev_t *mddev, const char *buf, size_t len)
2367{
2368 char *e;
2369 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002370
2371 if (!*buf || (*e && *e != '\n'))
2372 return -EINVAL;
2373
NeilBrown08a02ec2007-05-09 02:35:38 -07002374 if (mddev->pers)
2375 return -EBUSY;
2376 if (mddev->reshape_position != MaxSector)
2377 mddev->new_layout = n;
2378 else
2379 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002380 return len;
2381}
2382static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002383__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002384
2385
NeilBrowneae17012005-11-08 21:39:23 -08002386static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002387raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002388{
NeilBrownbb636542005-11-08 21:39:45 -08002389 if (mddev->raid_disks == 0)
2390 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002391 if (mddev->reshape_position != MaxSector &&
2392 mddev->delta_disks != 0)
2393 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2394 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002395 return sprintf(page, "%d\n", mddev->raid_disks);
2396}
2397
NeilBrownda943b992006-01-06 00:20:54 -08002398static int update_raid_disks(mddev_t *mddev, int raid_disks);
2399
2400static ssize_t
2401raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2402{
NeilBrownda943b992006-01-06 00:20:54 -08002403 char *e;
2404 int rv = 0;
2405 unsigned long n = simple_strtoul(buf, &e, 10);
2406
2407 if (!*buf || (*e && *e != '\n'))
2408 return -EINVAL;
2409
2410 if (mddev->pers)
2411 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002412 else if (mddev->reshape_position != MaxSector) {
2413 int olddisks = mddev->raid_disks - mddev->delta_disks;
2414 mddev->delta_disks = n - olddisks;
2415 mddev->raid_disks = n;
2416 } else
NeilBrownda943b992006-01-06 00:20:54 -08002417 mddev->raid_disks = n;
2418 return rv ? rv : len;
2419}
2420static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002421__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002422
NeilBrown24dd4692005-11-08 21:39:26 -08002423static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002424chunk_size_show(mddev_t *mddev, char *page)
2425{
NeilBrown08a02ec2007-05-09 02:35:38 -07002426 if (mddev->reshape_position != MaxSector &&
2427 mddev->chunk_size != mddev->new_chunk)
2428 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2429 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002430 return sprintf(page, "%d\n", mddev->chunk_size);
2431}
2432
2433static ssize_t
2434chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2435{
2436 /* can only set chunk_size if array is not yet active */
2437 char *e;
2438 unsigned long n = simple_strtoul(buf, &e, 10);
2439
NeilBrown3b343802006-01-06 00:20:47 -08002440 if (!*buf || (*e && *e != '\n'))
2441 return -EINVAL;
2442
NeilBrown08a02ec2007-05-09 02:35:38 -07002443 if (mddev->pers)
2444 return -EBUSY;
2445 else if (mddev->reshape_position != MaxSector)
2446 mddev->new_chunk = n;
2447 else
2448 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002449 return len;
2450}
2451static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002452__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002453
NeilBrowna94213b2006-06-26 00:28:00 -07002454static ssize_t
2455resync_start_show(mddev_t *mddev, char *page)
2456{
2457 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2458}
2459
2460static ssize_t
2461resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2462{
2463 /* can only set chunk_size if array is not yet active */
2464 char *e;
2465 unsigned long long n = simple_strtoull(buf, &e, 10);
2466
2467 if (mddev->pers)
2468 return -EBUSY;
2469 if (!*buf || (*e && *e != '\n'))
2470 return -EINVAL;
2471
2472 mddev->recovery_cp = n;
2473 return len;
2474}
2475static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002476__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002477
NeilBrown9e653b62006-06-26 00:27:58 -07002478/*
2479 * The array state can be:
2480 *
2481 * clear
2482 * No devices, no size, no level
2483 * Equivalent to STOP_ARRAY ioctl
2484 * inactive
2485 * May have some settings, but array is not active
2486 * all IO results in error
2487 * When written, doesn't tear down array, but just stops it
2488 * suspended (not supported yet)
2489 * All IO requests will block. The array can be reconfigured.
2490 * Writing this, if accepted, will block until array is quiessent
2491 * readonly
2492 * no resync can happen. no superblocks get written.
2493 * write requests fail
2494 * read-auto
2495 * like readonly, but behaves like 'clean' on a write request.
2496 *
2497 * clean - no pending writes, but otherwise active.
2498 * When written to inactive array, starts without resync
2499 * If a write request arrives then
2500 * if metadata is known, mark 'dirty' and switch to 'active'.
2501 * if not known, block and switch to write-pending
2502 * If written to an active array that has pending writes, then fails.
2503 * active
2504 * fully active: IO and resync can be happening.
2505 * When written to inactive array, starts with resync
2506 *
2507 * write-pending
2508 * clean, but writes are blocked waiting for 'active' to be written.
2509 *
2510 * active-idle
2511 * like active, but no writes have been seen for a while (100msec).
2512 *
2513 */
2514enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2515 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002516static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002517 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2518 "write-pending", "active-idle", NULL };
2519
2520static int match_word(const char *word, char **list)
2521{
2522 int n;
2523 for (n=0; list[n]; n++)
2524 if (cmd_match(word, list[n]))
2525 break;
2526 return n;
2527}
2528
2529static ssize_t
2530array_state_show(mddev_t *mddev, char *page)
2531{
2532 enum array_state st = inactive;
2533
2534 if (mddev->pers)
2535 switch(mddev->ro) {
2536 case 1:
2537 st = readonly;
2538 break;
2539 case 2:
2540 st = read_auto;
2541 break;
2542 case 0:
2543 if (mddev->in_sync)
2544 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002545 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2546 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002547 else if (mddev->safemode)
2548 st = active_idle;
2549 else
2550 st = active;
2551 }
2552 else {
2553 if (list_empty(&mddev->disks) &&
2554 mddev->raid_disks == 0 &&
2555 mddev->size == 0)
2556 st = clear;
2557 else
2558 st = inactive;
2559 }
2560 return sprintf(page, "%s\n", array_states[st]);
2561}
2562
2563static int do_md_stop(mddev_t * mddev, int ro);
2564static int do_md_run(mddev_t * mddev);
2565static int restart_array(mddev_t *mddev);
2566
2567static ssize_t
2568array_state_store(mddev_t *mddev, const char *buf, size_t len)
2569{
2570 int err = -EINVAL;
2571 enum array_state st = match_word(buf, array_states);
2572 switch(st) {
2573 case bad_word:
2574 break;
2575 case clear:
2576 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002577 if (atomic_read(&mddev->active) > 1)
2578 return -EBUSY;
2579 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002580 break;
2581 case inactive:
2582 /* stopping an active array */
2583 if (mddev->pers) {
2584 if (atomic_read(&mddev->active) > 1)
2585 return -EBUSY;
2586 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002587 } else
2588 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002589 break;
2590 case suspended:
2591 break; /* not supported yet */
2592 case readonly:
2593 if (mddev->pers)
2594 err = do_md_stop(mddev, 1);
2595 else {
2596 mddev->ro = 1;
2597 err = do_md_run(mddev);
2598 }
2599 break;
2600 case read_auto:
2601 /* stopping an active array */
2602 if (mddev->pers) {
2603 err = do_md_stop(mddev, 1);
2604 if (err == 0)
2605 mddev->ro = 2; /* FIXME mark devices writable */
2606 } else {
2607 mddev->ro = 2;
2608 err = do_md_run(mddev);
2609 }
2610 break;
2611 case clean:
2612 if (mddev->pers) {
2613 restart_array(mddev);
2614 spin_lock_irq(&mddev->write_lock);
2615 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002616 if (mddev->in_sync == 0) {
2617 mddev->in_sync = 1;
2618 if (mddev->persistent)
2619 set_bit(MD_CHANGE_CLEAN,
2620 &mddev->flags);
2621 }
2622 err = 0;
2623 } else
2624 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002625 spin_unlock_irq(&mddev->write_lock);
2626 } else {
2627 mddev->ro = 0;
2628 mddev->recovery_cp = MaxSector;
2629 err = do_md_run(mddev);
2630 }
2631 break;
2632 case active:
2633 if (mddev->pers) {
2634 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002635 if (mddev->external)
2636 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002637 wake_up(&mddev->sb_wait);
2638 err = 0;
2639 } else {
2640 mddev->ro = 0;
2641 err = do_md_run(mddev);
2642 }
2643 break;
2644 case write_pending:
2645 case active_idle:
2646 /* these cannot be set */
2647 break;
2648 }
2649 if (err)
2650 return err;
2651 else
2652 return len;
2653}
NeilBrown80ca3a42006-07-10 04:44:18 -07002654static struct md_sysfs_entry md_array_state =
2655__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002656
NeilBrown6d7ff732006-01-06 00:21:16 -08002657static ssize_t
2658null_show(mddev_t *mddev, char *page)
2659{
2660 return -EINVAL;
2661}
2662
2663static ssize_t
2664new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2665{
2666 /* buf must be %d:%d\n? giving major and minor numbers */
2667 /* The new device is added to the array.
2668 * If the array has a persistent superblock, we read the
2669 * superblock to initialise info and check validity.
2670 * Otherwise, only checking done is that in bind_rdev_to_array,
2671 * which mainly checks size.
2672 */
2673 char *e;
2674 int major = simple_strtoul(buf, &e, 10);
2675 int minor;
2676 dev_t dev;
2677 mdk_rdev_t *rdev;
2678 int err;
2679
2680 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2681 return -EINVAL;
2682 minor = simple_strtoul(e+1, &e, 10);
2683 if (*e && *e != '\n')
2684 return -EINVAL;
2685 dev = MKDEV(major, minor);
2686 if (major != MAJOR(dev) ||
2687 minor != MINOR(dev))
2688 return -EOVERFLOW;
2689
2690
2691 if (mddev->persistent) {
2692 rdev = md_import_device(dev, mddev->major_version,
2693 mddev->minor_version);
2694 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2695 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2696 mdk_rdev_t, same_set);
2697 err = super_types[mddev->major_version]
2698 .load_super(rdev, rdev0, mddev->minor_version);
2699 if (err < 0)
2700 goto out;
2701 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002702 } else if (mddev->external)
2703 rdev = md_import_device(dev, -2, -1);
2704 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002705 rdev = md_import_device(dev, -1, -1);
2706
2707 if (IS_ERR(rdev))
2708 return PTR_ERR(rdev);
2709 err = bind_rdev_to_array(rdev, mddev);
2710 out:
2711 if (err)
2712 export_rdev(rdev);
2713 return err ? err : len;
2714}
2715
2716static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002717__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002718
2719static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002720bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2721{
2722 char *end;
2723 unsigned long chunk, end_chunk;
2724
2725 if (!mddev->bitmap)
2726 goto out;
2727 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2728 while (*buf) {
2729 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2730 if (buf == end) break;
2731 if (*end == '-') { /* range */
2732 buf = end + 1;
2733 end_chunk = simple_strtoul(buf, &end, 0);
2734 if (buf == end) break;
2735 }
2736 if (*end && !isspace(*end)) break;
2737 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2738 buf = end;
2739 while (isspace(*buf)) buf++;
2740 }
2741 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2742out:
2743 return len;
2744}
2745
2746static struct md_sysfs_entry md_bitmap =
2747__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2748
2749static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002750size_show(mddev_t *mddev, char *page)
2751{
2752 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2753}
2754
2755static int update_size(mddev_t *mddev, unsigned long size);
2756
2757static ssize_t
2758size_store(mddev_t *mddev, const char *buf, size_t len)
2759{
2760 /* If array is inactive, we can reduce the component size, but
2761 * not increase it (except from 0).
2762 * If array is active, we can try an on-line resize
2763 */
2764 char *e;
2765 int err = 0;
2766 unsigned long long size = simple_strtoull(buf, &e, 10);
2767 if (!*buf || *buf == '\n' ||
2768 (*e && *e != '\n'))
2769 return -EINVAL;
2770
2771 if (mddev->pers) {
2772 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002773 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002774 } else {
2775 if (mddev->size == 0 ||
2776 mddev->size > size)
2777 mddev->size = size;
2778 else
2779 err = -ENOSPC;
2780 }
2781 return err ? err : len;
2782}
2783
2784static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002785__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002786
NeilBrown8bb93aa2006-01-06 00:20:50 -08002787
2788/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002789 * This is one of
2790 * 'none' for arrays with no metadata (good luck...)
2791 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002792 * or N.M for internally known formats
2793 */
2794static ssize_t
2795metadata_show(mddev_t *mddev, char *page)
2796{
2797 if (mddev->persistent)
2798 return sprintf(page, "%d.%d\n",
2799 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002800 else if (mddev->external)
2801 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002802 else
2803 return sprintf(page, "none\n");
2804}
2805
2806static ssize_t
2807metadata_store(mddev_t *mddev, const char *buf, size_t len)
2808{
2809 int major, minor;
2810 char *e;
2811 if (!list_empty(&mddev->disks))
2812 return -EBUSY;
2813
2814 if (cmd_match(buf, "none")) {
2815 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002816 mddev->external = 0;
2817 mddev->major_version = 0;
2818 mddev->minor_version = 90;
2819 return len;
2820 }
2821 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002822 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002823 if (namelen >= sizeof(mddev->metadata_type))
2824 namelen = sizeof(mddev->metadata_type)-1;
2825 strncpy(mddev->metadata_type, buf+9, namelen);
2826 mddev->metadata_type[namelen] = 0;
2827 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2828 mddev->metadata_type[--namelen] = 0;
2829 mddev->persistent = 0;
2830 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002831 mddev->major_version = 0;
2832 mddev->minor_version = 90;
2833 return len;
2834 }
2835 major = simple_strtoul(buf, &e, 10);
2836 if (e==buf || *e != '.')
2837 return -EINVAL;
2838 buf = e+1;
2839 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002840 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002841 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002842 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002843 return -ENOENT;
2844 mddev->major_version = major;
2845 mddev->minor_version = minor;
2846 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002847 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002848 return len;
2849}
2850
2851static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002852__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002853
NeilBrowna35b0d62006-01-06 00:20:49 -08002854static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002855action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002856{
NeilBrown7eec3142005-11-08 21:39:44 -08002857 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002858 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002859 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002860 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2861 type = "reshape";
2862 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002863 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2864 type = "resync";
2865 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2866 type = "check";
2867 else
2868 type = "repair";
2869 } else
2870 type = "recover";
2871 }
2872 return sprintf(page, "%s\n", type);
2873}
2874
2875static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002876action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002877{
NeilBrown7eec3142005-11-08 21:39:44 -08002878 if (!mddev->pers || !mddev->pers->sync_request)
2879 return -EINVAL;
2880
NeilBrownbce74da2006-01-06 00:20:41 -08002881 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002882 if (mddev->sync_thread) {
2883 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2884 md_unregister_thread(mddev->sync_thread);
2885 mddev->sync_thread = NULL;
2886 mddev->recovery = 0;
2887 }
NeilBrown03c902e2006-01-06 00:20:46 -08002888 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2889 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002890 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002891 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002892 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002893 else if (cmd_match(page, "reshape")) {
2894 int err;
2895 if (mddev->pers->start_reshape == NULL)
2896 return -EINVAL;
2897 err = mddev->pers->start_reshape(mddev);
2898 if (err)
2899 return err;
2900 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002901 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002902 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002903 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002904 return -EINVAL;
2905 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2906 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002907 }
NeilBrown03c902e2006-01-06 00:20:46 -08002908 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002909 md_wakeup_thread(mddev->thread);
2910 return len;
2911}
2912
NeilBrown9d888832005-11-08 21:39:26 -08002913static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002914mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002915{
2916 return sprintf(page, "%llu\n",
2917 (unsigned long long) mddev->resync_mismatches);
2918}
2919
NeilBrown80ca3a42006-07-10 04:44:18 -07002920static struct md_sysfs_entry md_scan_mode =
2921__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002922
NeilBrown96de1e62005-11-08 21:39:39 -08002923
NeilBrown80ca3a42006-07-10 04:44:18 -07002924static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002925
NeilBrown88202a02006-01-06 00:21:36 -08002926static ssize_t
2927sync_min_show(mddev_t *mddev, char *page)
2928{
2929 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2930 mddev->sync_speed_min ? "local": "system");
2931}
2932
2933static ssize_t
2934sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2935{
2936 int min;
2937 char *e;
2938 if (strncmp(buf, "system", 6)==0) {
2939 mddev->sync_speed_min = 0;
2940 return len;
2941 }
2942 min = simple_strtoul(buf, &e, 10);
2943 if (buf == e || (*e && *e != '\n') || min <= 0)
2944 return -EINVAL;
2945 mddev->sync_speed_min = min;
2946 return len;
2947}
2948
2949static struct md_sysfs_entry md_sync_min =
2950__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2951
2952static ssize_t
2953sync_max_show(mddev_t *mddev, char *page)
2954{
2955 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2956 mddev->sync_speed_max ? "local": "system");
2957}
2958
2959static ssize_t
2960sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2961{
2962 int max;
2963 char *e;
2964 if (strncmp(buf, "system", 6)==0) {
2965 mddev->sync_speed_max = 0;
2966 return len;
2967 }
2968 max = simple_strtoul(buf, &e, 10);
2969 if (buf == e || (*e && *e != '\n') || max <= 0)
2970 return -EINVAL;
2971 mddev->sync_speed_max = max;
2972 return len;
2973}
2974
2975static struct md_sysfs_entry md_sync_max =
2976__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2977
Iustin Popd7f3d292007-10-16 23:30:54 -07002978static ssize_t
2979degraded_show(mddev_t *mddev, char *page)
2980{
2981 return sprintf(page, "%d\n", mddev->degraded);
2982}
2983static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08002984
2985static ssize_t
2986sync_speed_show(mddev_t *mddev, char *page)
2987{
2988 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002989 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002990 dt = ((jiffies - mddev->resync_mark) / HZ);
2991 if (!dt) dt++;
2992 db = resync - (mddev->resync_mark_cnt);
2993 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2994}
2995
NeilBrown80ca3a42006-07-10 04:44:18 -07002996static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002997
2998static ssize_t
2999sync_completed_show(mddev_t *mddev, char *page)
3000{
3001 unsigned long max_blocks, resync;
3002
3003 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3004 max_blocks = mddev->resync_max_sectors;
3005 else
3006 max_blocks = mddev->size << 1;
3007
3008 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3009 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3010}
3011
NeilBrown80ca3a42006-07-10 04:44:18 -07003012static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003013
NeilBrowne464eaf2006-03-27 01:18:14 -08003014static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003015max_sync_show(mddev_t *mddev, char *page)
3016{
3017 if (mddev->resync_max == MaxSector)
3018 return sprintf(page, "max\n");
3019 else
3020 return sprintf(page, "%llu\n",
3021 (unsigned long long)mddev->resync_max);
3022}
3023static ssize_t
3024max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3025{
3026 if (strncmp(buf, "max", 3) == 0)
3027 mddev->resync_max = MaxSector;
3028 else {
3029 char *ep;
3030 unsigned long long max = simple_strtoull(buf, &ep, 10);
3031 if (ep == buf || (*ep != 0 && *ep != '\n'))
3032 return -EINVAL;
3033 if (max < mddev->resync_max &&
3034 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3035 return -EBUSY;
3036
3037 /* Must be a multiple of chunk_size */
3038 if (mddev->chunk_size) {
3039 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3040 return -EINVAL;
3041 }
3042 mddev->resync_max = max;
3043 }
3044 wake_up(&mddev->recovery_wait);
3045 return len;
3046}
3047
3048static struct md_sysfs_entry md_max_sync =
3049__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3050
3051static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003052suspend_lo_show(mddev_t *mddev, char *page)
3053{
3054 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3055}
3056
3057static ssize_t
3058suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3059{
3060 char *e;
3061 unsigned long long new = simple_strtoull(buf, &e, 10);
3062
3063 if (mddev->pers->quiesce == NULL)
3064 return -EINVAL;
3065 if (buf == e || (*e && *e != '\n'))
3066 return -EINVAL;
3067 if (new >= mddev->suspend_hi ||
3068 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3069 mddev->suspend_lo = new;
3070 mddev->pers->quiesce(mddev, 2);
3071 return len;
3072 } else
3073 return -EINVAL;
3074}
3075static struct md_sysfs_entry md_suspend_lo =
3076__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3077
3078
3079static ssize_t
3080suspend_hi_show(mddev_t *mddev, char *page)
3081{
3082 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3083}
3084
3085static ssize_t
3086suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3087{
3088 char *e;
3089 unsigned long long new = simple_strtoull(buf, &e, 10);
3090
3091 if (mddev->pers->quiesce == NULL)
3092 return -EINVAL;
3093 if (buf == e || (*e && *e != '\n'))
3094 return -EINVAL;
3095 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3096 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3097 mddev->suspend_hi = new;
3098 mddev->pers->quiesce(mddev, 1);
3099 mddev->pers->quiesce(mddev, 0);
3100 return len;
3101 } else
3102 return -EINVAL;
3103}
3104static struct md_sysfs_entry md_suspend_hi =
3105__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3106
NeilBrown08a02ec2007-05-09 02:35:38 -07003107static ssize_t
3108reshape_position_show(mddev_t *mddev, char *page)
3109{
3110 if (mddev->reshape_position != MaxSector)
3111 return sprintf(page, "%llu\n",
3112 (unsigned long long)mddev->reshape_position);
3113 strcpy(page, "none\n");
3114 return 5;
3115}
3116
3117static ssize_t
3118reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3119{
3120 char *e;
3121 unsigned long long new = simple_strtoull(buf, &e, 10);
3122 if (mddev->pers)
3123 return -EBUSY;
3124 if (buf == e || (*e && *e != '\n'))
3125 return -EINVAL;
3126 mddev->reshape_position = new;
3127 mddev->delta_disks = 0;
3128 mddev->new_level = mddev->level;
3129 mddev->new_layout = mddev->layout;
3130 mddev->new_chunk = mddev->chunk_size;
3131 return len;
3132}
3133
3134static struct md_sysfs_entry md_reshape_position =
3135__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3136 reshape_position_store);
3137
NeilBrowne464eaf2006-03-27 01:18:14 -08003138
NeilBrowneae17012005-11-08 21:39:23 -08003139static struct attribute *md_default_attrs[] = {
3140 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003141 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003142 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003143 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003144 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003145 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003146 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003147 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003148 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003149 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003150 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003151 NULL,
3152};
3153
3154static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003155 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003156 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003157 &md_sync_min.attr,
3158 &md_sync_max.attr,
3159 &md_sync_speed.attr,
3160 &md_sync_completed.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003161 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003162 &md_suspend_lo.attr,
3163 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003164 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003165 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003166 NULL,
3167};
NeilBrown411036f2005-11-08 21:39:40 -08003168static struct attribute_group md_redundancy_group = {
3169 .name = NULL,
3170 .attrs = md_redundancy_attrs,
3171};
3172
NeilBrowneae17012005-11-08 21:39:23 -08003173
3174static ssize_t
3175md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3176{
3177 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3178 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003179 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003180
3181 if (!entry->show)
3182 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003183 rv = mddev_lock(mddev);
3184 if (!rv) {
3185 rv = entry->show(mddev, page);
3186 mddev_unlock(mddev);
3187 }
NeilBrown96de1e62005-11-08 21:39:39 -08003188 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003189}
3190
3191static ssize_t
3192md_attr_store(struct kobject *kobj, struct attribute *attr,
3193 const char *page, size_t length)
3194{
3195 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3196 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003197 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003198
3199 if (!entry->store)
3200 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003201 if (!capable(CAP_SYS_ADMIN))
3202 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003203 rv = mddev_lock(mddev);
3204 if (!rv) {
3205 rv = entry->store(mddev, page, length);
3206 mddev_unlock(mddev);
3207 }
NeilBrown96de1e62005-11-08 21:39:39 -08003208 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003209}
3210
3211static void md_free(struct kobject *ko)
3212{
3213 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3214 kfree(mddev);
3215}
3216
3217static struct sysfs_ops md_sysfs_ops = {
3218 .show = md_attr_show,
3219 .store = md_attr_store,
3220};
3221static struct kobj_type md_ktype = {
3222 .release = md_free,
3223 .sysfs_ops = &md_sysfs_ops,
3224 .default_attrs = md_default_attrs,
3225};
3226
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227int mdp_major = 0;
3228
3229static struct kobject *md_probe(dev_t dev, int *part, void *data)
3230{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003231 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 mddev_t *mddev = mddev_find(dev);
3233 struct gendisk *disk;
3234 int partitioned = (MAJOR(dev) != MD_MAJOR);
3235 int shift = partitioned ? MdpMinorShift : 0;
3236 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003237 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239 if (!mddev)
3240 return NULL;
3241
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003242 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003244 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 mddev_put(mddev);
3246 return NULL;
3247 }
3248 disk = alloc_disk(1 << shift);
3249 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003250 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 mddev_put(mddev);
3252 return NULL;
3253 }
3254 disk->major = MAJOR(dev);
3255 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003256 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003258 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 disk->fops = &md_fops;
3261 disk->private_data = mddev;
3262 disk->queue = mddev->queue;
3263 add_disk(disk);
3264 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003265 mutex_unlock(&disks_mutex);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003266 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003267 "%s", "md");
3268 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003269 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3270 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003271 else
3272 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 return NULL;
3274}
3275
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276static void md_safemode_timeout(unsigned long data)
3277{
3278 mddev_t *mddev = (mddev_t *) data;
3279
3280 mddev->safemode = 1;
3281 md_wakeup_thread(mddev->thread);
3282}
3283
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003284static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286static int do_md_run(mddev_t * mddev)
3287{
NeilBrown2604b702006-01-06 00:20:36 -08003288 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 int chunk_size;
3290 struct list_head *tmp;
3291 mdk_rdev_t *rdev;
3292 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003293 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 char b[BDEVNAME_SIZE];
3295
NeilBrowna757e642005-04-16 15:26:42 -07003296 if (list_empty(&mddev->disks))
3297 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
3300 if (mddev->pers)
3301 return -EBUSY;
3302
3303 /*
3304 * Analyze all RAID superblock(s)
3305 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003306 if (!mddev->raid_disks) {
3307 if (!mddev->persistent)
3308 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003309 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
3312 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
NeilBrown2604b702006-01-06 00:20:36 -08003314 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 if (chunk_size > MAX_CHUNK_SIZE) {
3316 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3317 chunk_size, MAX_CHUNK_SIZE);
3318 return -EINVAL;
3319 }
3320 /*
3321 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3322 */
3323 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003324 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return -EINVAL;
3326 }
3327 if (chunk_size < PAGE_SIZE) {
3328 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3329 chunk_size, PAGE_SIZE);
3330 return -EINVAL;
3331 }
3332
3333 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003334 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003335 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 continue;
3337 if (rdev->size < chunk_size / 1024) {
3338 printk(KERN_WARNING
3339 "md: Dev %s smaller than chunk_size:"
3340 " %lluk < %dk\n",
3341 bdevname(rdev->bdev,b),
3342 (unsigned long long)rdev->size,
3343 chunk_size / 1024);
3344 return -EINVAL;
3345 }
3346 }
3347 }
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003350 if (mddev->level != LEVEL_NONE)
3351 request_module("md-level-%d", mddev->level);
3352 else if (mddev->clevel[0])
3353 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354#endif
3355
3356 /*
3357 * Drop all container device buffers, from now on
3358 * the only valid external interface is through the md
3359 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003361 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003362 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 continue;
3364 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003365 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003366
3367 /* perform some consistency tests on the device.
3368 * We don't want the data to overlap the metadata,
3369 * Internal Bitmap issues has handled elsewhere.
3370 */
3371 if (rdev->data_offset < rdev->sb_offset) {
3372 if (mddev->size &&
3373 rdev->data_offset + mddev->size*2
3374 > rdev->sb_offset*2) {
3375 printk("md: %s: data overlaps metadata\n",
3376 mdname(mddev));
3377 return -EINVAL;
3378 }
3379 } else {
3380 if (rdev->sb_offset*2 + rdev->sb_size/512
3381 > rdev->data_offset) {
3382 printk("md: %s: metadata overlaps data\n",
3383 mdname(mddev));
3384 return -EINVAL;
3385 }
3386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 }
3388
3389 md_probe(mddev->unit, NULL, NULL);
3390 disk = mddev->gendisk;
3391 if (!disk)
3392 return -ENOMEM;
3393
3394 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003395 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003396 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003398 if (mddev->level != LEVEL_NONE)
3399 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3400 mddev->level);
3401 else
3402 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3403 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return -EINVAL;
3405 }
NeilBrown2604b702006-01-06 00:20:36 -08003406 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003408 mddev->level = pers->level;
3409 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
NeilBrownf6705572006-03-27 01:18:11 -08003411 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003412 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003413 /* This personality cannot handle reshaping... */
3414 mddev->pers = NULL;
3415 module_put(pers->owner);
3416 return -EINVAL;
3417 }
3418
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003419 if (pers->sync_request) {
3420 /* Warn if this is a potentially silly
3421 * configuration.
3422 */
3423 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3424 mdk_rdev_t *rdev2;
3425 struct list_head *tmp2;
3426 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003427 rdev_for_each(rdev, tmp, mddev) {
3428 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003429 if (rdev < rdev2 &&
3430 rdev->bdev->bd_contains ==
3431 rdev2->bdev->bd_contains) {
3432 printk(KERN_WARNING
3433 "%s: WARNING: %s appears to be"
3434 " on the same physical disk as"
3435 " %s.\n",
3436 mdname(mddev),
3437 bdevname(rdev->bdev,b),
3438 bdevname(rdev2->bdev,b2));
3439 warned = 1;
3440 }
3441 }
3442 }
3443 if (warned)
3444 printk(KERN_WARNING
3445 "True protection against single-disk"
3446 " failure might be compromised.\n");
3447 }
3448
NeilBrown657390d2005-08-26 18:34:16 -07003449 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003451 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003452 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
NeilBrownf91de922005-11-08 21:39:36 -08003454 if (start_readonly)
3455 mddev->ro = 2; /* read-only, but switch on first write */
3456
NeilBrownb15c2e52006-01-06 00:20:16 -08003457 err = mddev->pers->run(mddev);
3458 if (!err && mddev->pers->sync_request) {
3459 err = bitmap_create(mddev);
3460 if (err) {
3461 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3462 mdname(mddev), err);
3463 mddev->pers->stop(mddev);
3464 }
3465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 if (err) {
3467 printk(KERN_ERR "md: pers->run() failed ...\n");
3468 module_put(mddev->pers->owner);
3469 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003470 bitmap_destroy(mddev);
3471 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003473 if (mddev->pers->sync_request) {
3474 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3475 printk(KERN_WARNING
3476 "md: cannot register extra attributes for %s\n",
3477 mdname(mddev));
3478 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003479 mddev->ro = 0;
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 atomic_set(&mddev->writes_pending,0);
3482 mddev->safemode = 0;
3483 mddev->safemode_timer.function = md_safemode_timeout;
3484 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003485 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003487
NeilBrownd089c6a2008-02-06 01:39:59 -08003488 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003489 if (rdev->raid_disk >= 0) {
3490 char nm[20];
3491 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003492 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3493 printk("md: cannot register %s for %s\n",
3494 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
3497 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3498
NeilBrown850b2b42006-10-03 01:15:46 -07003499 if (mddev->flags)
3500 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
3502 set_capacity(disk, mddev->array_size<<1);
3503
3504 /* If we call blk_queue_make_request here, it will
3505 * re-initialise max_sectors etc which may have been
3506 * refined inside -> run. So just set the bits we need to set.
3507 * Most initialisation happended when we called
3508 * blk_queue_make_request(..., md_fail_request)
3509 * earlier.
3510 */
3511 mddev->queue->queuedata = mddev;
3512 mddev->queue->make_request_fn = mddev->pers->make_request;
3513
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003514 /* If there is a partially-recovered drive we need to
3515 * start recovery here. If we leave it to md_check_recovery,
3516 * it will remove the drives and not do the right thing
3517 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003518 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003519 struct list_head *rtmp;
3520 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003521 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003522 if (rdev->raid_disk >= 0 &&
3523 !test_bit(In_sync, &rdev->flags) &&
3524 !test_bit(Faulty, &rdev->flags))
3525 /* complete an interrupted recovery */
3526 spares++;
3527 if (spares && mddev->pers->sync_request) {
3528 mddev->recovery = 0;
3529 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3530 mddev->sync_thread = md_register_thread(md_do_sync,
3531 mddev,
3532 "%s_resync");
3533 if (!mddev->sync_thread) {
3534 printk(KERN_ERR "%s: could not start resync"
3535 " thread...\n",
3536 mdname(mddev));
3537 /* leave the spares where they are, it shouldn't hurt */
3538 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003539 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003540 }
3541 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003542 md_wakeup_thread(mddev->thread);
3543 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003544
Linus Torvalds44ce62942007-05-09 18:51:36 -07003545 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003546 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003547 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 return 0;
3549}
3550
3551static int restart_array(mddev_t *mddev)
3552{
3553 struct gendisk *disk = mddev->gendisk;
3554 int err;
3555
3556 /*
3557 * Complain if it has no devices
3558 */
3559 err = -ENXIO;
3560 if (list_empty(&mddev->disks))
3561 goto out;
3562
3563 if (mddev->pers) {
3564 err = -EBUSY;
3565 if (!mddev->ro)
3566 goto out;
3567
3568 mddev->safemode = 0;
3569 mddev->ro = 0;
3570 set_disk_ro(disk, 0);
3571
3572 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3573 mdname(mddev));
3574 /*
3575 * Kick recovery or resync if necessary
3576 */
3577 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3578 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003579 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003581 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
3584out:
3585 return err;
3586}
3587
NeilBrownacc55e22006-06-26 00:27:47 -07003588/* similar to deny_write_access, but accounts for our holding a reference
3589 * to the file ourselves */
3590static int deny_bitmap_write_access(struct file * file)
3591{
3592 struct inode *inode = file->f_mapping->host;
3593
3594 spin_lock(&inode->i_lock);
3595 if (atomic_read(&inode->i_writecount) > 1) {
3596 spin_unlock(&inode->i_lock);
3597 return -ETXTBSY;
3598 }
3599 atomic_set(&inode->i_writecount, -1);
3600 spin_unlock(&inode->i_lock);
3601
3602 return 0;
3603}
3604
3605static void restore_bitmap_write_access(struct file *file)
3606{
3607 struct inode *inode = file->f_mapping->host;
3608
3609 spin_lock(&inode->i_lock);
3610 atomic_set(&inode->i_writecount, 1);
3611 spin_unlock(&inode->i_lock);
3612}
3613
NeilBrown9e653b62006-06-26 00:27:58 -07003614/* mode:
3615 * 0 - completely stop and dis-assemble array
3616 * 1 - switch to readonly
3617 * 2 - stop but do not disassemble array
3618 */
3619static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620{
3621 int err = 0;
3622 struct gendisk *disk = mddev->gendisk;
3623
3624 if (mddev->pers) {
3625 if (atomic_read(&mddev->active)>2) {
3626 printk("md: %s still in use.\n",mdname(mddev));
3627 return -EBUSY;
3628 }
3629
3630 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003631 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3633 md_unregister_thread(mddev->sync_thread);
3634 mddev->sync_thread = NULL;
3635 }
3636
3637 del_timer_sync(&mddev->safemode_timer);
3638
3639 invalidate_partition(disk, 0);
3640
NeilBrown9e653b62006-06-26 00:27:58 -07003641 switch(mode) {
3642 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003644 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 goto out;
3646 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003647 break;
3648 case 0: /* disassemble */
3649 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003650 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003651 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 if (mddev->ro)
3653 set_disk_ro(disk, 0);
3654 blk_queue_make_request(mddev->queue, md_fail_request);
3655 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003656 mddev->queue->merge_bvec_fn = NULL;
3657 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003658 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003659 if (mddev->pers->sync_request)
3660 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3661
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 module_put(mddev->pers->owner);
3663 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003664
3665 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003666 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 if (mddev->ro)
3669 mddev->ro = 0;
3670 }
NeilBrown850b2b42006-10-03 01:15:46 -07003671 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 /* mark array as shutdown cleanly */
3673 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003674 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
NeilBrown9e653b62006-06-26 00:27:58 -07003676 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003678 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 }
NeilBrown32a76272005-06-21 17:17:14 -07003680
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 /*
3682 * Free resources if final stop
3683 */
NeilBrown9e653b62006-06-26 00:27:58 -07003684 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003685 mdk_rdev_t *rdev;
3686 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003687
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3689
NeilBrown978f9462006-02-02 14:28:05 -08003690 bitmap_destroy(mddev);
3691 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003692 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003693 fput(mddev->bitmap_file);
3694 mddev->bitmap_file = NULL;
3695 }
3696 mddev->bitmap_offset = 0;
3697
NeilBrownd089c6a2008-02-06 01:39:59 -08003698 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003699 if (rdev->raid_disk >= 0) {
3700 char nm[20];
3701 sprintf(nm, "rd%d", rdev->raid_disk);
3702 sysfs_remove_link(&mddev->kobj, nm);
3703 }
3704
NeilBrown177a99b2008-02-06 01:39:56 -08003705 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003706 flush_scheduled_work();
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 export_array(mddev);
3709
3710 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003711 mddev->size = 0;
3712 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003713 mddev->recovery_cp = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003714 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003715 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003716 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003717 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003718 mddev->level = LEVEL_NONE;
3719 mddev->clevel[0] = 0;
3720 mddev->flags = 0;
3721 mddev->ro = 0;
3722 mddev->metadata_type[0] = 0;
3723 mddev->chunk_size = 0;
3724 mddev->ctime = mddev->utime = 0;
3725 mddev->layout = 0;
3726 mddev->max_disks = 0;
3727 mddev->events = 0;
3728 mddev->delta_disks = 0;
3729 mddev->new_level = LEVEL_NONE;
3730 mddev->new_layout = 0;
3731 mddev->new_chunk = 0;
3732 mddev->curr_resync = 0;
3733 mddev->resync_mismatches = 0;
3734 mddev->suspend_lo = mddev->suspend_hi = 0;
3735 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3736 mddev->recovery = 0;
3737 mddev->in_sync = 0;
3738 mddev->changed = 0;
3739 mddev->degraded = 0;
3740 mddev->barriers_work = 0;
3741 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003742
NeilBrowna8a55c32006-06-26 00:27:39 -07003743 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3745 mdname(mddev));
3746 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003747 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748out:
3749 return err;
3750}
3751
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003752#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753static void autorun_array(mddev_t *mddev)
3754{
3755 mdk_rdev_t *rdev;
3756 struct list_head *tmp;
3757 int err;
3758
NeilBrowna757e642005-04-16 15:26:42 -07003759 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761
3762 printk(KERN_INFO "md: running: ");
3763
NeilBrownd089c6a2008-02-06 01:39:59 -08003764 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 char b[BDEVNAME_SIZE];
3766 printk("<%s>", bdevname(rdev->bdev,b));
3767 }
3768 printk("\n");
3769
3770 err = do_md_run (mddev);
3771 if (err) {
3772 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3773 do_md_stop (mddev, 0);
3774 }
3775}
3776
3777/*
3778 * lets try to run arrays based on all disks that have arrived
3779 * until now. (those are in pending_raid_disks)
3780 *
3781 * the method: pick the first pending disk, collect all disks with
3782 * the same UUID, remove all from the pending list and put them into
3783 * the 'same_array' list. Then order this list based on superblock
3784 * update time (freshest comes first), kick out 'old' disks and
3785 * compare superblocks. If everything's fine then run it.
3786 *
3787 * If "unit" is allocated, then bump its reference count
3788 */
3789static void autorun_devices(int part)
3790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 struct list_head *tmp;
3792 mdk_rdev_t *rdev0, *rdev;
3793 mddev_t *mddev;
3794 char b[BDEVNAME_SIZE];
3795
3796 printk(KERN_INFO "md: autorun ...\n");
3797 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003798 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003800 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 rdev0 = list_entry(pending_raid_disks.next,
3802 mdk_rdev_t, same_set);
3803
3804 printk(KERN_INFO "md: considering %s ...\n",
3805 bdevname(rdev0->bdev,b));
3806 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003807 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 if (super_90_load(rdev, rdev0, 0) >= 0) {
3809 printk(KERN_INFO "md: adding %s ...\n",
3810 bdevname(rdev->bdev,b));
3811 list_move(&rdev->same_set, &candidates);
3812 }
3813 /*
3814 * now we have a set of devices, with all of them having
3815 * mostly sane superblocks. It's time to allocate the
3816 * mddev.
3817 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003818 if (part) {
3819 dev = MKDEV(mdp_major,
3820 rdev0->preferred_minor << MdpMinorShift);
3821 unit = MINOR(dev) >> MdpMinorShift;
3822 } else {
3823 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3824 unit = MINOR(dev);
3825 }
3826 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3828 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3829 break;
3830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 md_probe(dev, NULL, NULL);
3833 mddev = mddev_find(dev);
3834 if (!mddev) {
3835 printk(KERN_ERR
3836 "md: cannot allocate memory for md drive.\n");
3837 break;
3838 }
3839 if (mddev_lock(mddev))
3840 printk(KERN_WARNING "md: %s locked, cannot run\n",
3841 mdname(mddev));
3842 else if (mddev->raid_disks || mddev->major_version
3843 || !list_empty(&mddev->disks)) {
3844 printk(KERN_WARNING
3845 "md: %s already running, cannot run %s\n",
3846 mdname(mddev), bdevname(rdev0->bdev,b));
3847 mddev_unlock(mddev);
3848 } else {
3849 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08003850 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08003851 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 list_del_init(&rdev->same_set);
3853 if (bind_rdev_to_array(rdev, mddev))
3854 export_rdev(rdev);
3855 }
3856 autorun_array(mddev);
3857 mddev_unlock(mddev);
3858 }
3859 /* on success, candidates will be empty, on error
3860 * it won't...
3861 */
NeilBrown73c34432008-02-06 01:39:59 -08003862 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 export_rdev(rdev);
3864 mddev_put(mddev);
3865 }
3866 printk(KERN_INFO "md: ... autorun DONE.\n");
3867}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003868#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870static int get_version(void __user * arg)
3871{
3872 mdu_version_t ver;
3873
3874 ver.major = MD_MAJOR_VERSION;
3875 ver.minor = MD_MINOR_VERSION;
3876 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3877
3878 if (copy_to_user(arg, &ver, sizeof(ver)))
3879 return -EFAULT;
3880
3881 return 0;
3882}
3883
3884static int get_array_info(mddev_t * mddev, void __user * arg)
3885{
3886 mdu_array_info_t info;
3887 int nr,working,active,failed,spare;
3888 mdk_rdev_t *rdev;
3889 struct list_head *tmp;
3890
3891 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003892 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003894 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 failed++;
3896 else {
3897 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003898 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 active++;
3900 else
3901 spare++;
3902 }
3903 }
3904
3905 info.major_version = mddev->major_version;
3906 info.minor_version = mddev->minor_version;
3907 info.patch_version = MD_PATCHLEVEL_VERSION;
3908 info.ctime = mddev->ctime;
3909 info.level = mddev->level;
3910 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003911 if (info.size != mddev->size) /* overflow */
3912 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 info.nr_disks = nr;
3914 info.raid_disks = mddev->raid_disks;
3915 info.md_minor = mddev->md_minor;
3916 info.not_persistent= !mddev->persistent;
3917
3918 info.utime = mddev->utime;
3919 info.state = 0;
3920 if (mddev->in_sync)
3921 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003922 if (mddev->bitmap && mddev->bitmap_offset)
3923 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924 info.active_disks = active;
3925 info.working_disks = working;
3926 info.failed_disks = failed;
3927 info.spare_disks = spare;
3928
3929 info.layout = mddev->layout;
3930 info.chunk_size = mddev->chunk_size;
3931
3932 if (copy_to_user(arg, &info, sizeof(info)))
3933 return -EFAULT;
3934
3935 return 0;
3936}
3937
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003938static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003939{
3940 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3941 char *ptr, *buf = NULL;
3942 int err = -ENOMEM;
3943
NeilBrown2a2275d2007-01-26 00:57:11 -08003944 md_allow_write(mddev);
3945
NeilBrown32a76272005-06-21 17:17:14 -07003946 file = kmalloc(sizeof(*file), GFP_KERNEL);
3947 if (!file)
3948 goto out;
3949
3950 /* bitmap disabled, zero the first byte and copy out */
3951 if (!mddev->bitmap || !mddev->bitmap->file) {
3952 file->pathname[0] = '\0';
3953 goto copy_out;
3954 }
3955
3956 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3957 if (!buf)
3958 goto out;
3959
3960 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3961 if (!ptr)
3962 goto out;
3963
3964 strcpy(file->pathname, ptr);
3965
3966copy_out:
3967 err = 0;
3968 if (copy_to_user(arg, file, sizeof(*file)))
3969 err = -EFAULT;
3970out:
3971 kfree(buf);
3972 kfree(file);
3973 return err;
3974}
3975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976static int get_disk_info(mddev_t * mddev, void __user * arg)
3977{
3978 mdu_disk_info_t info;
3979 unsigned int nr;
3980 mdk_rdev_t *rdev;
3981
3982 if (copy_from_user(&info, arg, sizeof(info)))
3983 return -EFAULT;
3984
3985 nr = info.number;
3986
3987 rdev = find_rdev_nr(mddev, nr);
3988 if (rdev) {
3989 info.major = MAJOR(rdev->bdev->bd_dev);
3990 info.minor = MINOR(rdev->bdev->bd_dev);
3991 info.raid_disk = rdev->raid_disk;
3992 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003993 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003995 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 info.state |= (1<<MD_DISK_ACTIVE);
3997 info.state |= (1<<MD_DISK_SYNC);
3998 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003999 if (test_bit(WriteMostly, &rdev->flags))
4000 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 } else {
4002 info.major = info.minor = 0;
4003 info.raid_disk = -1;
4004 info.state = (1<<MD_DISK_REMOVED);
4005 }
4006
4007 if (copy_to_user(arg, &info, sizeof(info)))
4008 return -EFAULT;
4009
4010 return 0;
4011}
4012
4013static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4014{
4015 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4016 mdk_rdev_t *rdev;
4017 dev_t dev = MKDEV(info->major,info->minor);
4018
4019 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4020 return -EOVERFLOW;
4021
4022 if (!mddev->raid_disks) {
4023 int err;
4024 /* expecting a device which has a superblock */
4025 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4026 if (IS_ERR(rdev)) {
4027 printk(KERN_WARNING
4028 "md: md_import_device returned %ld\n",
4029 PTR_ERR(rdev));
4030 return PTR_ERR(rdev);
4031 }
4032 if (!list_empty(&mddev->disks)) {
4033 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4034 mdk_rdev_t, same_set);
4035 int err = super_types[mddev->major_version]
4036 .load_super(rdev, rdev0, mddev->minor_version);
4037 if (err < 0) {
4038 printk(KERN_WARNING
4039 "md: %s has different UUID to %s\n",
4040 bdevname(rdev->bdev,b),
4041 bdevname(rdev0->bdev,b2));
4042 export_rdev(rdev);
4043 return -EINVAL;
4044 }
4045 }
4046 err = bind_rdev_to_array(rdev, mddev);
4047 if (err)
4048 export_rdev(rdev);
4049 return err;
4050 }
4051
4052 /*
4053 * add_new_disk can be used once the array is assembled
4054 * to add "hot spares". They must already have a superblock
4055 * written
4056 */
4057 if (mddev->pers) {
4058 int err;
4059 if (!mddev->pers->hot_add_disk) {
4060 printk(KERN_WARNING
4061 "%s: personality does not support diskops!\n",
4062 mdname(mddev));
4063 return -EINVAL;
4064 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004065 if (mddev->persistent)
4066 rdev = md_import_device(dev, mddev->major_version,
4067 mddev->minor_version);
4068 else
4069 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 if (IS_ERR(rdev)) {
4071 printk(KERN_WARNING
4072 "md: md_import_device returned %ld\n",
4073 PTR_ERR(rdev));
4074 return PTR_ERR(rdev);
4075 }
NeilBrown41158c72005-06-21 17:17:25 -07004076 /* set save_raid_disk if appropriate */
4077 if (!mddev->persistent) {
4078 if (info->state & (1<<MD_DISK_SYNC) &&
4079 info->raid_disk < mddev->raid_disks)
4080 rdev->raid_disk = info->raid_disk;
4081 else
4082 rdev->raid_disk = -1;
4083 } else
4084 super_types[mddev->major_version].
4085 validate_super(mddev, rdev);
4086 rdev->saved_raid_disk = rdev->raid_disk;
4087
NeilBrownb2d444d2005-11-08 21:39:31 -08004088 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004089 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4090 set_bit(WriteMostly, &rdev->flags);
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 rdev->raid_disk = -1;
4093 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004094 if (!err && !mddev->pers->hot_remove_disk) {
4095 /* If there is hot_add_disk but no hot_remove_disk
4096 * then added disks for geometry changes,
4097 * and should be added immediately.
4098 */
4099 super_types[mddev->major_version].
4100 validate_super(mddev, rdev);
4101 err = mddev->pers->hot_add_disk(mddev, rdev);
4102 if (err)
4103 unbind_rdev_from_array(rdev);
4104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 if (err)
4106 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004107
NeilBrown17571282006-12-10 02:20:52 -08004108 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07004109 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004110 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 return err;
4112 }
4113
4114 /* otherwise, add_new_disk is only allowed
4115 * for major_version==0 superblocks
4116 */
4117 if (mddev->major_version != 0) {
4118 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4119 mdname(mddev));
4120 return -EINVAL;
4121 }
4122
4123 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4124 int err;
4125 rdev = md_import_device (dev, -1, 0);
4126 if (IS_ERR(rdev)) {
4127 printk(KERN_WARNING
4128 "md: error, md_import_device() returned %ld\n",
4129 PTR_ERR(rdev));
4130 return PTR_ERR(rdev);
4131 }
4132 rdev->desc_nr = info->number;
4133 if (info->raid_disk < mddev->raid_disks)
4134 rdev->raid_disk = info->raid_disk;
4135 else
4136 rdev->raid_disk = -1;
4137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004139 if (info->state & (1<<MD_DISK_SYNC))
4140 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004142 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4143 set_bit(WriteMostly, &rdev->flags);
4144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 if (!mddev->persistent) {
4146 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4147 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4148 } else
4149 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4150 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4151
NeilBrown2bf071b2006-01-06 00:20:55 -08004152 err = bind_rdev_to_array(rdev, mddev);
4153 if (err) {
4154 export_rdev(rdev);
4155 return err;
4156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 }
4158
4159 return 0;
4160}
4161
4162static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4163{
4164 char b[BDEVNAME_SIZE];
4165 mdk_rdev_t *rdev;
4166
4167 if (!mddev->pers)
4168 return -ENODEV;
4169
4170 rdev = find_rdev(mddev, dev);
4171 if (!rdev)
4172 return -ENXIO;
4173
4174 if (rdev->raid_disk >= 0)
4175 goto busy;
4176
4177 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004178 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004179 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181 return 0;
4182busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004183 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 bdevname(rdev->bdev,b), mdname(mddev));
4185 return -EBUSY;
4186}
4187
4188static int hot_add_disk(mddev_t * mddev, dev_t dev)
4189{
4190 char b[BDEVNAME_SIZE];
4191 int err;
4192 unsigned int size;
4193 mdk_rdev_t *rdev;
4194
4195 if (!mddev->pers)
4196 return -ENODEV;
4197
4198 if (mddev->major_version != 0) {
4199 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4200 " version-0 superblocks.\n",
4201 mdname(mddev));
4202 return -EINVAL;
4203 }
4204 if (!mddev->pers->hot_add_disk) {
4205 printk(KERN_WARNING
4206 "%s: personality does not support diskops!\n",
4207 mdname(mddev));
4208 return -EINVAL;
4209 }
4210
4211 rdev = md_import_device (dev, -1, 0);
4212 if (IS_ERR(rdev)) {
4213 printk(KERN_WARNING
4214 "md: error, md_import_device() returned %ld\n",
4215 PTR_ERR(rdev));
4216 return -EINVAL;
4217 }
4218
4219 if (mddev->persistent)
4220 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4221 else
4222 rdev->sb_offset =
4223 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4224
4225 size = calc_dev_size(rdev, mddev->chunk_size);
4226 rdev->size = size;
4227
NeilBrownb2d444d2005-11-08 21:39:31 -08004228 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 printk(KERN_WARNING
4230 "md: can not hot-add faulty %s disk to %s!\n",
4231 bdevname(rdev->bdev,b), mdname(mddev));
4232 err = -EINVAL;
4233 goto abort_export;
4234 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004235 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004237 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004238 err = bind_rdev_to_array(rdev, mddev);
4239 if (err)
4240 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
4242 /*
4243 * The rest should better be atomic, we can have disk failures
4244 * noticed in interrupt contexts ...
4245 */
4246
4247 if (rdev->desc_nr == mddev->max_disks) {
4248 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4249 mdname(mddev));
4250 err = -EBUSY;
4251 goto abort_unbind_export;
4252 }
4253
4254 rdev->raid_disk = -1;
4255
NeilBrown850b2b42006-10-03 01:15:46 -07004256 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 /*
4259 * Kick recovery, maybe this spare has to be added to the
4260 * array immediately.
4261 */
4262 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4263 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004264 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return 0;
4266
4267abort_unbind_export:
4268 unbind_rdev_from_array(rdev);
4269
4270abort_export:
4271 export_rdev(rdev);
4272 return err;
4273}
4274
NeilBrown32a76272005-06-21 17:17:14 -07004275static int set_bitmap_file(mddev_t *mddev, int fd)
4276{
4277 int err;
4278
NeilBrown36fa3062005-09-09 16:23:45 -07004279 if (mddev->pers) {
4280 if (!mddev->pers->quiesce)
4281 return -EBUSY;
4282 if (mddev->recovery || mddev->sync_thread)
4283 return -EBUSY;
4284 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004285 }
4286
NeilBrown36fa3062005-09-09 16:23:45 -07004287
4288 if (fd >= 0) {
4289 if (mddev->bitmap)
4290 return -EEXIST; /* cannot add when bitmap is present */
4291 mddev->bitmap_file = fget(fd);
4292
4293 if (mddev->bitmap_file == NULL) {
4294 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4295 mdname(mddev));
4296 return -EBADF;
4297 }
4298
4299 err = deny_bitmap_write_access(mddev->bitmap_file);
4300 if (err) {
4301 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4302 mdname(mddev));
4303 fput(mddev->bitmap_file);
4304 mddev->bitmap_file = NULL;
4305 return err;
4306 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004307 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004308 } else if (mddev->bitmap == NULL)
4309 return -ENOENT; /* cannot remove what isn't there */
4310 err = 0;
4311 if (mddev->pers) {
4312 mddev->pers->quiesce(mddev, 1);
4313 if (fd >= 0)
4314 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004315 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004316 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004317 fd = -1; /* make sure to put the file */
4318 }
NeilBrown36fa3062005-09-09 16:23:45 -07004319 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004320 }
4321 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004322 if (mddev->bitmap_file) {
4323 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004324 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004325 }
NeilBrown36fa3062005-09-09 16:23:45 -07004326 mddev->bitmap_file = NULL;
4327 }
4328
NeilBrown32a76272005-06-21 17:17:14 -07004329 return err;
4330}
4331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332/*
4333 * set_array_info is used two different ways
4334 * The original usage is when creating a new array.
4335 * In this usage, raid_disks is > 0 and it together with
4336 * level, size, not_persistent,layout,chunksize determine the
4337 * shape of the array.
4338 * This will always create an array with a type-0.90.0 superblock.
4339 * The newer usage is when assembling an array.
4340 * In this case raid_disks will be 0, and the major_version field is
4341 * use to determine which style super-blocks are to be found on the devices.
4342 * The minor and patch _version numbers are also kept incase the
4343 * super_block handler wishes to interpret them.
4344 */
4345static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4346{
4347
4348 if (info->raid_disks == 0) {
4349 /* just setting version number for superblock loading */
4350 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004351 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 super_types[info->major_version].name == NULL) {
4353 /* maybe try to auto-load a module? */
4354 printk(KERN_INFO
4355 "md: superblock version %d not known\n",
4356 info->major_version);
4357 return -EINVAL;
4358 }
4359 mddev->major_version = info->major_version;
4360 mddev->minor_version = info->minor_version;
4361 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004362 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 return 0;
4364 }
4365 mddev->major_version = MD_MAJOR_VERSION;
4366 mddev->minor_version = MD_MINOR_VERSION;
4367 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4368 mddev->ctime = get_seconds();
4369
4370 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004371 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 mddev->size = info->size;
4373 mddev->raid_disks = info->raid_disks;
4374 /* don't set md_minor, it is determined by which /dev/md* was
4375 * openned
4376 */
4377 if (info->state & (1<<MD_SB_CLEAN))
4378 mddev->recovery_cp = MaxSector;
4379 else
4380 mddev->recovery_cp = 0;
4381 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004382 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
4384 mddev->layout = info->layout;
4385 mddev->chunk_size = info->chunk_size;
4386
4387 mddev->max_disks = MD_SB_DISKS;
4388
NeilBrowne6910632008-02-06 01:39:51 -08004389 if (mddev->persistent)
4390 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004391 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
NeilBrownb2a27032005-11-28 13:44:12 -08004393 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4394 mddev->bitmap_offset = 0;
4395
NeilBrownf6705572006-03-27 01:18:11 -08004396 mddev->reshape_position = MaxSector;
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 /*
4399 * Generate a 128 bit UUID
4400 */
4401 get_random_bytes(mddev->uuid, 16);
4402
NeilBrownf6705572006-03-27 01:18:11 -08004403 mddev->new_level = mddev->level;
4404 mddev->new_chunk = mddev->chunk_size;
4405 mddev->new_layout = mddev->layout;
4406 mddev->delta_disks = 0;
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 return 0;
4409}
4410
NeilBrowna35b0d62006-01-06 00:20:49 -08004411static int update_size(mddev_t *mddev, unsigned long size)
4412{
4413 mdk_rdev_t * rdev;
4414 int rv;
4415 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004416 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004417
4418 if (mddev->pers->resize == NULL)
4419 return -EINVAL;
4420 /* The "size" is the amount of each device that is used.
4421 * This can only make sense for arrays with redundancy.
4422 * linear and raid0 always use whatever space is available
4423 * We can only consider changing the size if no resync
4424 * or reconstruction is happening, and if the new size
4425 * is acceptable. It must fit before the sb_offset or,
4426 * if that is <data_offset, it must fit before the
4427 * size of each device.
4428 * If size is zero, we find the largest size that fits.
4429 */
4430 if (mddev->sync_thread)
4431 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004432 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004433 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004434 avail = rdev->size * 2;
4435
NeilBrowna35b0d62006-01-06 00:20:49 -08004436 if (fit && (size == 0 || size > avail/2))
4437 size = avail/2;
4438 if (avail < ((sector_t)size << 1))
4439 return -ENOSPC;
4440 }
4441 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4442 if (!rv) {
4443 struct block_device *bdev;
4444
4445 bdev = bdget_disk(mddev->gendisk, 0);
4446 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004447 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004448 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004449 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004450 bdput(bdev);
4451 }
4452 }
4453 return rv;
4454}
4455
NeilBrownda943b992006-01-06 00:20:54 -08004456static int update_raid_disks(mddev_t *mddev, int raid_disks)
4457{
4458 int rv;
4459 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004460 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004461 return -EINVAL;
4462 if (raid_disks <= 0 ||
4463 raid_disks >= mddev->max_disks)
4464 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004465 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004466 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004467 mddev->delta_disks = raid_disks - mddev->raid_disks;
4468
4469 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004470 return rv;
4471}
4472
4473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474/*
4475 * update_array_info is used to change the configuration of an
4476 * on-line array.
4477 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4478 * fields in the info are checked against the array.
4479 * Any differences that cannot be handled will cause an error.
4480 * Normally, only one change can be managed at a time.
4481 */
4482static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4483{
4484 int rv = 0;
4485 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004486 int state = 0;
4487
4488 /* calculate expected state,ignoring low bits */
4489 if (mddev->bitmap && mddev->bitmap_offset)
4490 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 if (mddev->major_version != info->major_version ||
4493 mddev->minor_version != info->minor_version ||
4494/* mddev->patch_version != info->patch_version || */
4495 mddev->ctime != info->ctime ||
4496 mddev->level != info->level ||
4497/* mddev->layout != info->layout || */
4498 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004499 mddev->chunk_size != info->chunk_size ||
4500 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4501 ((state^info->state) & 0xfffffe00)
4502 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 return -EINVAL;
4504 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004505 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 if (mddev->raid_disks != info->raid_disks) cnt++;
4507 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004508 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 if (cnt == 0) return 0;
4510 if (cnt > 1) return -EINVAL;
4511
4512 if (mddev->layout != info->layout) {
4513 /* Change layout
4514 * we don't need to do anything at the md level, the
4515 * personality will take care of it all.
4516 */
4517 if (mddev->pers->reconfig == NULL)
4518 return -EINVAL;
4519 else
4520 return mddev->pers->reconfig(mddev, info->layout, -1);
4521 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004522 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004523 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
NeilBrownda943b992006-01-06 00:20:54 -08004525 if (mddev->raid_disks != info->raid_disks)
4526 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
NeilBrown36fa3062005-09-09 16:23:45 -07004528 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4529 if (mddev->pers->quiesce == NULL)
4530 return -EINVAL;
4531 if (mddev->recovery || mddev->sync_thread)
4532 return -EBUSY;
4533 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4534 /* add the bitmap */
4535 if (mddev->bitmap)
4536 return -EEXIST;
4537 if (mddev->default_bitmap_offset == 0)
4538 return -EINVAL;
4539 mddev->bitmap_offset = mddev->default_bitmap_offset;
4540 mddev->pers->quiesce(mddev, 1);
4541 rv = bitmap_create(mddev);
4542 if (rv)
4543 bitmap_destroy(mddev);
4544 mddev->pers->quiesce(mddev, 0);
4545 } else {
4546 /* remove the bitmap */
4547 if (!mddev->bitmap)
4548 return -ENOENT;
4549 if (mddev->bitmap->file)
4550 return -EINVAL;
4551 mddev->pers->quiesce(mddev, 1);
4552 bitmap_destroy(mddev);
4553 mddev->pers->quiesce(mddev, 0);
4554 mddev->bitmap_offset = 0;
4555 }
4556 }
NeilBrown850b2b42006-10-03 01:15:46 -07004557 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 return rv;
4559}
4560
4561static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4562{
4563 mdk_rdev_t *rdev;
4564
4565 if (mddev->pers == NULL)
4566 return -ENODEV;
4567
4568 rdev = find_rdev(mddev, dev);
4569 if (!rdev)
4570 return -ENODEV;
4571
4572 md_error(mddev, rdev);
4573 return 0;
4574}
4575
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004576static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4577{
4578 mddev_t *mddev = bdev->bd_disk->private_data;
4579
4580 geo->heads = 2;
4581 geo->sectors = 4;
4582 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4583 return 0;
4584}
4585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586static int md_ioctl(struct inode *inode, struct file *file,
4587 unsigned int cmd, unsigned long arg)
4588{
4589 int err = 0;
4590 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 mddev_t *mddev = NULL;
4592
4593 if (!capable(CAP_SYS_ADMIN))
4594 return -EACCES;
4595
4596 /*
4597 * Commands dealing with the RAID driver but not any
4598 * particular array:
4599 */
4600 switch (cmd)
4601 {
4602 case RAID_VERSION:
4603 err = get_version(argp);
4604 goto done;
4605
4606 case PRINT_RAID_DEBUG:
4607 err = 0;
4608 md_print_devices();
4609 goto done;
4610
4611#ifndef MODULE
4612 case RAID_AUTORUN:
4613 err = 0;
4614 autostart_arrays(arg);
4615 goto done;
4616#endif
4617 default:;
4618 }
4619
4620 /*
4621 * Commands creating/starting a new array:
4622 */
4623
4624 mddev = inode->i_bdev->bd_disk->private_data;
4625
4626 if (!mddev) {
4627 BUG();
4628 goto abort;
4629 }
4630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 err = mddev_lock(mddev);
4632 if (err) {
4633 printk(KERN_INFO
4634 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4635 err, cmd);
4636 goto abort;
4637 }
4638
4639 switch (cmd)
4640 {
4641 case SET_ARRAY_INFO:
4642 {
4643 mdu_array_info_t info;
4644 if (!arg)
4645 memset(&info, 0, sizeof(info));
4646 else if (copy_from_user(&info, argp, sizeof(info))) {
4647 err = -EFAULT;
4648 goto abort_unlock;
4649 }
4650 if (mddev->pers) {
4651 err = update_array_info(mddev, &info);
4652 if (err) {
4653 printk(KERN_WARNING "md: couldn't update"
4654 " array info. %d\n", err);
4655 goto abort_unlock;
4656 }
4657 goto done_unlock;
4658 }
4659 if (!list_empty(&mddev->disks)) {
4660 printk(KERN_WARNING
4661 "md: array %s already has disks!\n",
4662 mdname(mddev));
4663 err = -EBUSY;
4664 goto abort_unlock;
4665 }
4666 if (mddev->raid_disks) {
4667 printk(KERN_WARNING
4668 "md: array %s already initialised!\n",
4669 mdname(mddev));
4670 err = -EBUSY;
4671 goto abort_unlock;
4672 }
4673 err = set_array_info(mddev, &info);
4674 if (err) {
4675 printk(KERN_WARNING "md: couldn't set"
4676 " array info. %d\n", err);
4677 goto abort_unlock;
4678 }
4679 }
4680 goto done_unlock;
4681
4682 default:;
4683 }
4684
4685 /*
4686 * Commands querying/configuring an existing array:
4687 */
NeilBrown32a76272005-06-21 17:17:14 -07004688 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004689 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004690 if ((!mddev->raid_disks && !mddev->external)
4691 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4692 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4693 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 err = -ENODEV;
4695 goto abort_unlock;
4696 }
4697
4698 /*
4699 * Commands even a read-only array can execute:
4700 */
4701 switch (cmd)
4702 {
4703 case GET_ARRAY_INFO:
4704 err = get_array_info(mddev, argp);
4705 goto done_unlock;
4706
NeilBrown32a76272005-06-21 17:17:14 -07004707 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004708 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004709 goto done_unlock;
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 case GET_DISK_INFO:
4712 err = get_disk_info(mddev, argp);
4713 goto done_unlock;
4714
4715 case RESTART_ARRAY_RW:
4716 err = restart_array(mddev);
4717 goto done_unlock;
4718
4719 case STOP_ARRAY:
4720 err = do_md_stop (mddev, 0);
4721 goto done_unlock;
4722
4723 case STOP_ARRAY_RO:
4724 err = do_md_stop (mddev, 1);
4725 goto done_unlock;
4726
4727 /*
4728 * We have a problem here : there is no easy way to give a CHS
4729 * virtual geometry. We currently pretend that we have a 2 heads
4730 * 4 sectors (with a BIG number of cylinders...). This drives
4731 * dosfs just mad... ;-)
4732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 }
4734
4735 /*
4736 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004737 * superblock, so we do not allow them on read-only arrays.
4738 * However non-MD ioctls (e.g. get-size) will still come through
4739 * here and hit the 'default' below, so only disallow
4740 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 */
NeilBrownf91de922005-11-08 21:39:36 -08004742 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4743 mddev->ro && mddev->pers) {
4744 if (mddev->ro == 2) {
4745 mddev->ro = 0;
4746 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4747 md_wakeup_thread(mddev->thread);
4748
4749 } else {
4750 err = -EROFS;
4751 goto abort_unlock;
4752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 }
4754
4755 switch (cmd)
4756 {
4757 case ADD_NEW_DISK:
4758 {
4759 mdu_disk_info_t info;
4760 if (copy_from_user(&info, argp, sizeof(info)))
4761 err = -EFAULT;
4762 else
4763 err = add_new_disk(mddev, &info);
4764 goto done_unlock;
4765 }
4766
4767 case HOT_REMOVE_DISK:
4768 err = hot_remove_disk(mddev, new_decode_dev(arg));
4769 goto done_unlock;
4770
4771 case HOT_ADD_DISK:
4772 err = hot_add_disk(mddev, new_decode_dev(arg));
4773 goto done_unlock;
4774
4775 case SET_DISK_FAULTY:
4776 err = set_disk_faulty(mddev, new_decode_dev(arg));
4777 goto done_unlock;
4778
4779 case RUN_ARRAY:
4780 err = do_md_run (mddev);
4781 goto done_unlock;
4782
NeilBrown32a76272005-06-21 17:17:14 -07004783 case SET_BITMAP_FILE:
4784 err = set_bitmap_file(mddev, (int)arg);
4785 goto done_unlock;
4786
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 err = -EINVAL;
4789 goto abort_unlock;
4790 }
4791
4792done_unlock:
4793abort_unlock:
4794 mddev_unlock(mddev);
4795
4796 return err;
4797done:
4798 if (err)
4799 MD_BUG();
4800abort:
4801 return err;
4802}
4803
4804static int md_open(struct inode *inode, struct file *file)
4805{
4806 /*
4807 * Succeed if we can lock the mddev, which confirms that
4808 * it isn't being stopped right now.
4809 */
4810 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4811 int err;
4812
NeilBrownd63a5a72006-12-08 02:36:17 -08004813 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 goto out;
4815
4816 err = 0;
4817 mddev_get(mddev);
4818 mddev_unlock(mddev);
4819
4820 check_disk_change(inode->i_bdev);
4821 out:
4822 return err;
4823}
4824
4825static int md_release(struct inode *inode, struct file * file)
4826{
4827 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4828
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004829 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 mddev_put(mddev);
4831
4832 return 0;
4833}
4834
Linus Torvalds44ce62942007-05-09 18:51:36 -07004835static int md_media_changed(struct gendisk *disk)
4836{
4837 mddev_t *mddev = disk->private_data;
4838
4839 return mddev->changed;
4840}
4841
4842static int md_revalidate(struct gendisk *disk)
4843{
4844 mddev_t *mddev = disk->private_data;
4845
4846 mddev->changed = 0;
4847 return 0;
4848}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849static struct block_device_operations md_fops =
4850{
4851 .owner = THIS_MODULE,
4852 .open = md_open,
4853 .release = md_release,
4854 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004855 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004856 .media_changed = md_media_changed,
4857 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858};
4859
Adrian Bunk75c96f82005-05-05 16:16:09 -07004860static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861{
4862 mdk_thread_t *thread = arg;
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 /*
4865 * md_thread is a 'system-thread', it's priority should be very
4866 * high. We avoid resource deadlocks individually in each
4867 * raid personality. (RAID5 does preallocation) We also use RR and
4868 * the very same RT priority as kswapd, thus we will never get
4869 * into a priority inversion deadlock.
4870 *
4871 * we definitely have to have equal or higher priority than
4872 * bdflush, otherwise bdflush will deadlock if there are too
4873 * many dirty RAID5 blocks.
4874 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875
NeilBrown6985c432005-10-19 21:23:47 -07004876 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004877 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878
NeilBrown93588e22005-11-15 00:09:12 -08004879 /* We need to wait INTERRUPTIBLE so that
4880 * we don't add to the load-average.
4881 * That means we need to be sure no signals are
4882 * pending
4883 */
4884 if (signal_pending(current))
4885 flush_signals(current);
4886
4887 wait_event_interruptible_timeout
4888 (thread->wqueue,
4889 test_bit(THREAD_WAKEUP, &thread->flags)
4890 || kthread_should_stop(),
4891 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
4893 clear_bit(THREAD_WAKEUP, &thread->flags);
4894
NeilBrown787453c2005-11-08 21:39:43 -08004895 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 return 0;
4899}
4900
4901void md_wakeup_thread(mdk_thread_t *thread)
4902{
4903 if (thread) {
4904 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4905 set_bit(THREAD_WAKEUP, &thread->flags);
4906 wake_up(&thread->wqueue);
4907 }
4908}
4909
4910mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4911 const char *name)
4912{
4913 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914
NeilBrown9ffae0c2006-01-06 00:20:32 -08004915 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 if (!thread)
4917 return NULL;
4918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 init_waitqueue_head(&thread->wqueue);
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 thread->run = run;
4922 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004923 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004924 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004925 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 kfree(thread);
4927 return NULL;
4928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 return thread;
4930}
4931
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932void md_unregister_thread(mdk_thread_t *thread)
4933{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004934 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07004935
4936 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 kfree(thread);
4938}
4939
4940void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4941{
4942 if (!mddev) {
4943 MD_BUG();
4944 return;
4945 }
4946
NeilBrownb2d444d2005-11-08 21:39:31 -08004947 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 return;
NeilBrown32a76272005-06-21 17:17:14 -07004949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4951 mdname(mddev),
4952 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4953 __builtin_return_address(0),__builtin_return_address(1),
4954 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004955*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004956 if (!mddev->pers)
4957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 if (!mddev->pers->error_handler)
4959 return;
4960 mddev->pers->error_handler(mddev,rdev);
4961 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4962 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4963 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004964 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965}
4966
4967/* seq_file implementation /proc/mdstat */
4968
4969static void status_unused(struct seq_file *seq)
4970{
4971 int i = 0;
4972 mdk_rdev_t *rdev;
4973 struct list_head *tmp;
4974
4975 seq_printf(seq, "unused devices: ");
4976
NeilBrown73c34432008-02-06 01:39:59 -08004977 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 char b[BDEVNAME_SIZE];
4979 i++;
4980 seq_printf(seq, "%s ",
4981 bdevname(rdev->bdev,b));
4982 }
4983 if (!i)
4984 seq_printf(seq, "<none>");
4985
4986 seq_printf(seq, "\n");
4987}
4988
4989
4990static void status_resync(struct seq_file *seq, mddev_t * mddev)
4991{
NeilBrown4588b422006-03-27 01:18:04 -08004992 sector_t max_blocks, resync, res;
4993 unsigned long dt, db, rt;
4994 int scale;
4995 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
4997 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4998
4999 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5000 max_blocks = mddev->resync_max_sectors >> 1;
5001 else
5002 max_blocks = mddev->size;
5003
5004 /*
5005 * Should not happen.
5006 */
5007 if (!max_blocks) {
5008 MD_BUG();
5009 return;
5010 }
NeilBrown4588b422006-03-27 01:18:04 -08005011 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5012 * in a sector_t, and (max_blocks>>scale) will fit in a
5013 * u32, as those are the requirements for sector_div.
5014 * Thus 'scale' must be at least 10
5015 */
5016 scale = 10;
5017 if (sizeof(sector_t) > sizeof(unsigned long)) {
5018 while ( max_blocks/2 > (1ULL<<(scale+32)))
5019 scale++;
5020 }
5021 res = (resync>>scale)*1000;
5022 sector_div(res, (u32)((max_blocks>>scale)+1));
5023
5024 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 {
NeilBrown4588b422006-03-27 01:18:04 -08005026 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 seq_printf(seq, "[");
5028 for (i = 0; i < x; i++)
5029 seq_printf(seq, "=");
5030 seq_printf(seq, ">");
5031 for (i = 0; i < y; i++)
5032 seq_printf(seq, ".");
5033 seq_printf(seq, "] ");
5034 }
NeilBrown4588b422006-03-27 01:18:04 -08005035 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005036 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5037 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005038 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5039 "check" :
5040 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5041 "resync" : "recovery"))),
5042 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005043 (unsigned long long) resync,
5044 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
5046 /*
5047 * We do not want to overflow, so the order of operands and
5048 * the * 100 / 100 trick are important. We do a +1 to be
5049 * safe against division by zero. We only estimate anyway.
5050 *
5051 * dt: time from mark until now
5052 * db: blocks written from mark until now
5053 * rt: remaining time
5054 */
5055 dt = ((jiffies - mddev->resync_mark) / HZ);
5056 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005057 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5058 - mddev->resync_mark_cnt;
5059 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060
5061 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5062
NeilBrownff4e8d92006-07-10 04:44:16 -07005063 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
5066static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5067{
5068 struct list_head *tmp;
5069 loff_t l = *pos;
5070 mddev_t *mddev;
5071
5072 if (l >= 0x10000)
5073 return NULL;
5074 if (!l--)
5075 /* header */
5076 return (void*)1;
5077
5078 spin_lock(&all_mddevs_lock);
5079 list_for_each(tmp,&all_mddevs)
5080 if (!l--) {
5081 mddev = list_entry(tmp, mddev_t, all_mddevs);
5082 mddev_get(mddev);
5083 spin_unlock(&all_mddevs_lock);
5084 return mddev;
5085 }
5086 spin_unlock(&all_mddevs_lock);
5087 if (!l--)
5088 return (void*)2;/* tail */
5089 return NULL;
5090}
5091
5092static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5093{
5094 struct list_head *tmp;
5095 mddev_t *next_mddev, *mddev = v;
5096
5097 ++*pos;
5098 if (v == (void*)2)
5099 return NULL;
5100
5101 spin_lock(&all_mddevs_lock);
5102 if (v == (void*)1)
5103 tmp = all_mddevs.next;
5104 else
5105 tmp = mddev->all_mddevs.next;
5106 if (tmp != &all_mddevs)
5107 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5108 else {
5109 next_mddev = (void*)2;
5110 *pos = 0x10000;
5111 }
5112 spin_unlock(&all_mddevs_lock);
5113
5114 if (v != (void*)1)
5115 mddev_put(mddev);
5116 return next_mddev;
5117
5118}
5119
5120static void md_seq_stop(struct seq_file *seq, void *v)
5121{
5122 mddev_t *mddev = v;
5123
5124 if (mddev && v != (void*)1 && v != (void*)2)
5125 mddev_put(mddev);
5126}
5127
NeilBrownd7603b72006-01-06 00:20:30 -08005128struct mdstat_info {
5129 int event;
5130};
5131
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132static int md_seq_show(struct seq_file *seq, void *v)
5133{
5134 mddev_t *mddev = v;
5135 sector_t size;
5136 struct list_head *tmp2;
5137 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005138 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005139 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140
5141 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005142 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 seq_printf(seq, "Personalities : ");
5144 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005145 list_for_each_entry(pers, &pers_list, list)
5146 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
5148 spin_unlock(&pers_lock);
5149 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005150 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 return 0;
5152 }
5153 if (v == (void*)2) {
5154 status_unused(seq);
5155 return 0;
5156 }
5157
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005158 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5162 seq_printf(seq, "%s : %sactive", mdname(mddev),
5163 mddev->pers ? "" : "in");
5164 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005165 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005167 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005168 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 seq_printf(seq, " %s", mddev->pers->name);
5170 }
5171
5172 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005173 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 char b[BDEVNAME_SIZE];
5175 seq_printf(seq, " %s[%d]",
5176 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005177 if (test_bit(WriteMostly, &rdev->flags))
5178 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005179 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 seq_printf(seq, "(F)");
5181 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005182 } else if (rdev->raid_disk < 0)
5183 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 size += rdev->size;
5185 }
5186
5187 if (!list_empty(&mddev->disks)) {
5188 if (mddev->pers)
5189 seq_printf(seq, "\n %llu blocks",
5190 (unsigned long long)mddev->array_size);
5191 else
5192 seq_printf(seq, "\n %llu blocks",
5193 (unsigned long long)size);
5194 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005195 if (mddev->persistent) {
5196 if (mddev->major_version != 0 ||
5197 mddev->minor_version != 90) {
5198 seq_printf(seq," super %d.%d",
5199 mddev->major_version,
5200 mddev->minor_version);
5201 }
NeilBrowne6910632008-02-06 01:39:51 -08005202 } else if (mddev->external)
5203 seq_printf(seq, " super external:%s",
5204 mddev->metadata_type);
5205 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005206 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208 if (mddev->pers) {
5209 mddev->pers->status (seq, mddev);
5210 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005211 if (mddev->pers->sync_request) {
5212 if (mddev->curr_resync > 2) {
5213 status_resync (seq, mddev);
5214 seq_printf(seq, "\n ");
5215 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5216 seq_printf(seq, "\tresync=DELAYED\n ");
5217 else if (mddev->recovery_cp < MaxSector)
5218 seq_printf(seq, "\tresync=PENDING\n ");
5219 }
NeilBrown32a76272005-06-21 17:17:14 -07005220 } else
5221 seq_printf(seq, "\n ");
5222
5223 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005224 unsigned long chunk_kb;
5225 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005226 spin_lock_irqsave(&bitmap->lock, flags);
5227 chunk_kb = bitmap->chunksize >> 10;
5228 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5229 "%lu%s chunk",
5230 bitmap->pages - bitmap->missing_pages,
5231 bitmap->pages,
5232 (bitmap->pages - bitmap->missing_pages)
5233 << (PAGE_SHIFT - 10),
5234 chunk_kb ? chunk_kb : bitmap->chunksize,
5235 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005236 if (bitmap->file) {
5237 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005238 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005239 }
NeilBrown78d742d2005-06-21 17:17:15 -07005240
NeilBrown32a76272005-06-21 17:17:14 -07005241 seq_printf(seq, "\n");
5242 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 }
5244
5245 seq_printf(seq, "\n");
5246 }
5247 mddev_unlock(mddev);
5248
5249 return 0;
5250}
5251
5252static struct seq_operations md_seq_ops = {
5253 .start = md_seq_start,
5254 .next = md_seq_next,
5255 .stop = md_seq_stop,
5256 .show = md_seq_show,
5257};
5258
5259static int md_seq_open(struct inode *inode, struct file *file)
5260{
5261 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005262 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5263 if (mi == NULL)
5264 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
5266 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005267 if (error)
5268 kfree(mi);
5269 else {
5270 struct seq_file *p = file->private_data;
5271 p->private = mi;
5272 mi->event = atomic_read(&md_event_count);
5273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 return error;
5275}
5276
NeilBrownd7603b72006-01-06 00:20:30 -08005277static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5278{
5279 struct seq_file *m = filp->private_data;
5280 struct mdstat_info *mi = m->private;
5281 int mask;
5282
5283 poll_wait(filp, &md_event_waiters, wait);
5284
5285 /* always allow read */
5286 mask = POLLIN | POLLRDNORM;
5287
5288 if (mi->event != atomic_read(&md_event_count))
5289 mask |= POLLERR | POLLPRI;
5290 return mask;
5291}
5292
Arjan van de Venfa027c22007-02-12 00:55:33 -08005293static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005294 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 .open = md_seq_open,
5296 .read = seq_read,
5297 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005298 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005299 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300};
5301
NeilBrown2604b702006-01-06 00:20:36 -08005302int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005305 list_add_tail(&p->list, &pers_list);
5306 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 spin_unlock(&pers_lock);
5308 return 0;
5309}
5310
NeilBrown2604b702006-01-06 00:20:36 -08005311int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312{
NeilBrown2604b702006-01-06 00:20:36 -08005313 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005315 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 spin_unlock(&pers_lock);
5317 return 0;
5318}
5319
5320static int is_mddev_idle(mddev_t *mddev)
5321{
5322 mdk_rdev_t * rdev;
5323 struct list_head *tmp;
5324 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005325 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005328 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005330 curr_events = disk_stat_read(disk, sectors[0]) +
5331 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005333 /* sync IO will cause sync_io to increase before the disk_stats
5334 * as sync_io is counted when a request starts, and
5335 * disk_stats is counted when it completes.
5336 * So resync activity will cause curr_events to be smaller than
5337 * when there was no such activity.
5338 * non-sync IO will cause disk_stat to increase without
5339 * increasing sync_io so curr_events will (eventually)
5340 * be larger than it was before. Once it becomes
5341 * substantially larger, the test below will cause
5342 * the array to appear non-idle, and resync will slow
5343 * down.
5344 * If there is a lot of outstanding resync activity when
5345 * we set last_event to curr_events, then all that activity
5346 * completing might cause the array to appear non-idle
5347 * and resync will be slowed down even though there might
5348 * not have been non-resync activity. This will only
5349 * happen once though. 'last_events' will soon reflect
5350 * the state where there is little or no outstanding
5351 * resync requests, and further resync activity will
5352 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005353 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005355 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 rdev->last_events = curr_events;
5357 idle = 0;
5358 }
5359 }
5360 return idle;
5361}
5362
5363void md_done_sync(mddev_t *mddev, int blocks, int ok)
5364{
5365 /* another "blocks" (512byte) blocks have been synced */
5366 atomic_sub(blocks, &mddev->recovery_active);
5367 wake_up(&mddev->recovery_wait);
5368 if (!ok) {
5369 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5370 md_wakeup_thread(mddev->thread);
5371 // stop recovery, signal do_sync ....
5372 }
5373}
5374
5375
NeilBrown06d91a52005-06-21 17:17:12 -07005376/* md_write_start(mddev, bi)
5377 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005378 * in superblock) before writing, schedule a superblock update
5379 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005380 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005381void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382{
NeilBrown06d91a52005-06-21 17:17:12 -07005383 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005384 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005385
NeilBrownf91de922005-11-08 21:39:36 -08005386 BUG_ON(mddev->ro == 1);
5387 if (mddev->ro == 2) {
5388 /* need to switch to read/write */
5389 mddev->ro = 0;
5390 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5391 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005392 md_wakeup_thread(mddev->sync_thread);
NeilBrownf91de922005-11-08 21:39:36 -08005393 }
NeilBrown06d91a52005-06-21 17:17:12 -07005394 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005395 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005396 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005397 if (mddev->in_sync) {
5398 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005399 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005400 md_wakeup_thread(mddev->thread);
5401 }
NeilBrowna9701a32005-11-08 21:39:34 -08005402 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005403 }
NeilBrown850b2b42006-10-03 01:15:46 -07005404 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405}
5406
5407void md_write_end(mddev_t *mddev)
5408{
5409 if (atomic_dec_and_test(&mddev->writes_pending)) {
5410 if (mddev->safemode == 2)
5411 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005412 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5414 }
5415}
5416
NeilBrown2a2275d2007-01-26 00:57:11 -08005417/* md_allow_write(mddev)
5418 * Calling this ensures that the array is marked 'active' so that writes
5419 * may proceed without blocking. It is important to call this before
5420 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5421 * Must be called with mddev_lock held.
5422 */
5423void md_allow_write(mddev_t *mddev)
5424{
5425 if (!mddev->pers)
5426 return;
5427 if (mddev->ro)
5428 return;
5429
5430 spin_lock_irq(&mddev->write_lock);
5431 if (mddev->in_sync) {
5432 mddev->in_sync = 0;
5433 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5434 if (mddev->safemode_delay &&
5435 mddev->safemode == 0)
5436 mddev->safemode = 1;
5437 spin_unlock_irq(&mddev->write_lock);
5438 md_update_sb(mddev, 0);
5439 } else
5440 spin_unlock_irq(&mddev->write_lock);
5441}
5442EXPORT_SYMBOL_GPL(md_allow_write);
5443
Adrian Bunk75c96f82005-05-05 16:16:09 -07005444static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445
5446#define SYNC_MARKS 10
5447#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005448void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449{
5450 mddev_t *mddev2;
5451 unsigned int currspeed = 0,
5452 window;
NeilBrown57afd892005-06-21 17:17:13 -07005453 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 unsigned long mark[SYNC_MARKS];
5455 sector_t mark_cnt[SYNC_MARKS];
5456 int last_mark,m;
5457 struct list_head *tmp;
5458 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005459 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005460 struct list_head *rtmp;
5461 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005462 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463
5464 /* just incase thread restarts... */
5465 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5466 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005467 if (mddev->ro) /* never try to sync a read-only array */
5468 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
NeilBrown61df9d92006-10-03 01:15:57 -07005470 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5471 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5472 desc = "data-check";
5473 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5474 desc = "requested-resync";
5475 else
5476 desc = "resync";
5477 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5478 desc = "reshape";
5479 else
5480 desc = "recovery";
5481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 /* we overload curr_resync somewhat here.
5483 * 0 == not engaged in resync at all
5484 * 2 == checking that there is no conflict with another sync
5485 * 1 == like 2, but have yielded to allow conflicting resync to
5486 * commense
5487 * other == active in resync - this many blocks
5488 *
5489 * Before starting a resync we must have set curr_resync to
5490 * 2, and then checked that every "conflicting" array has curr_resync
5491 * less than ours. When we find one that is the same or higher
5492 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5493 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5494 * This will mean we have to start checking from the beginning again.
5495 *
5496 */
5497
5498 do {
5499 mddev->curr_resync = 2;
5500
5501 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005502 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005503 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 goto skip;
5505 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005506 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 if (mddev2 == mddev)
5508 continue;
5509 if (mddev2->curr_resync &&
5510 match_mddev_units(mddev,mddev2)) {
5511 DEFINE_WAIT(wq);
5512 if (mddev < mddev2 && mddev->curr_resync == 2) {
5513 /* arbitrarily yield */
5514 mddev->curr_resync = 1;
5515 wake_up(&resync_wait);
5516 }
5517 if (mddev > mddev2 && mddev->curr_resync == 1)
5518 /* no need to wait here, we can wait the next
5519 * time 'round when curr_resync == 2
5520 */
5521 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005522 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5523 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005524 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005525 printk(KERN_INFO "md: delaying %s of %s"
5526 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005528 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 mddev_put(mddev2);
5530 schedule();
5531 finish_wait(&resync_wait, &wq);
5532 goto try_again;
5533 }
5534 finish_wait(&resync_wait, &wq);
5535 }
5536 }
5537 } while (mddev->curr_resync < 2);
5538
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005539 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005540 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005542 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 */
5544 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005545 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005546 /* we don't use the checkpoint if there's a bitmap */
5547 if (!mddev->bitmap &&
5548 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5549 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005550 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5551 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005552 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 /* recovery follows the physical size of devices */
5554 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005555 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005556 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005557 if (rdev->raid_disk >= 0 &&
5558 !test_bit(Faulty, &rdev->flags) &&
5559 !test_bit(In_sync, &rdev->flags) &&
5560 rdev->recovery_offset < j)
5561 j = rdev->recovery_offset;
5562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563
NeilBrown61df9d92006-10-03 01:15:57 -07005564 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5565 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5566 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005567 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005568 "(but not more than %d KB/sec) for %s.\n",
5569 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
5571 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005572
NeilBrown57afd892005-06-21 17:17:13 -07005573 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 for (m = 0; m < SYNC_MARKS; m++) {
5575 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005576 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 }
5578 last_mark = 0;
5579 mddev->resync_mark = mark[last_mark];
5580 mddev->resync_mark_cnt = mark_cnt[last_mark];
5581
5582 /*
5583 * Tune reconstruction:
5584 */
5585 window = 32*(PAGE_SIZE/512);
5586 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5587 window/2,(unsigned long long) max_sectors/2);
5588
5589 atomic_set(&mddev->recovery_active, 0);
5590 init_waitqueue_head(&mddev->recovery_wait);
5591 last_check = 0;
5592
5593 if (j>2) {
5594 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005595 "md: resuming %s of %s from checkpoint.\n",
5596 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 mddev->curr_resync = j;
5598 }
5599
5600 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005601 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
NeilBrown57afd892005-06-21 17:17:13 -07005603 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005604 if (j >= mddev->resync_max) {
5605 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5606 wait_event(mddev->recovery_wait,
5607 mddev->resync_max > j
5608 || kthread_should_stop());
5609 }
5610 if (kthread_should_stop())
5611 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005612 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005613 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005614 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5616 goto out;
5617 }
NeilBrown57afd892005-06-21 17:17:13 -07005618
5619 if (!skipped) { /* actual IO requested */
5620 io_sectors += sectors;
5621 atomic_add(sectors, &mddev->recovery_active);
5622 }
5623
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 j += sectors;
5625 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005626 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005627 if (last_check == 0)
5628 /* this is the earliers that rebuilt will be
5629 * visible in /proc/mdstat
5630 */
5631 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005632
5633 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 continue;
5635
NeilBrown57afd892005-06-21 17:17:13 -07005636 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637
5638 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5639 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5640 break;
5641
5642 repeat:
5643 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5644 /* step marks */
5645 int next = (last_mark+1) % SYNC_MARKS;
5646
5647 mddev->resync_mark = mark[next];
5648 mddev->resync_mark_cnt = mark_cnt[next];
5649 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005650 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 last_mark = next;
5652 }
5653
5654
NeilBrownc6207272008-02-06 01:39:52 -08005655 if (kthread_should_stop())
5656 goto interrupted;
5657
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658
5659 /*
5660 * this loop exits only if either when we are slower than
5661 * the 'hard' speed limit, or the system was IO-idle for
5662 * a jiffy.
5663 * the system might be non-idle CPU-wise, but we only care
5664 * about not overloading the IO subsystem. (things like an
5665 * e2fsck being done on the RAID array should execute fast)
5666 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005667 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 cond_resched();
5669
NeilBrown57afd892005-06-21 17:17:13 -07005670 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5671 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
NeilBrown88202a02006-01-06 00:21:36 -08005673 if (currspeed > speed_min(mddev)) {
5674 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005676 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 goto repeat;
5678 }
5679 }
5680 }
NeilBrown61df9d92006-10-03 01:15:57 -07005681 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 /*
5683 * this also signals 'finished resyncing' to md_stop
5684 */
5685 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005686 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687
5688 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5689
5690 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005691 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005694 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005695 mddev->curr_resync > 2) {
5696 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5697 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5698 if (mddev->curr_resync >= mddev->recovery_cp) {
5699 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005700 "md: checkpointing %s of %s.\n",
5701 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005702 mddev->recovery_cp = mddev->curr_resync;
5703 }
5704 } else
5705 mddev->recovery_cp = MaxSector;
5706 } else {
5707 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5708 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005709 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005710 if (rdev->raid_disk >= 0 &&
5711 !test_bit(Faulty, &rdev->flags) &&
5712 !test_bit(In_sync, &rdev->flags) &&
5713 rdev->recovery_offset < mddev->curr_resync)
5714 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 }
NeilBrown17571282006-12-10 02:20:52 -08005717 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 skip:
5720 mddev->curr_resync = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005721 mddev->resync_max = MaxSector;
5722 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 wake_up(&resync_wait);
5724 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5725 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005726 return;
5727
5728 interrupted:
5729 /*
5730 * got a signal, exit.
5731 */
5732 printk(KERN_INFO
5733 "md: md_do_sync() got signal ... exiting\n");
5734 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5735 goto out;
5736
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737}
NeilBrown29269552006-03-27 01:18:10 -08005738EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739
5740
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005741static int remove_and_add_spares(mddev_t *mddev)
5742{
5743 mdk_rdev_t *rdev;
5744 struct list_head *rtmp;
5745 int spares = 0;
5746
NeilBrownd089c6a2008-02-06 01:39:59 -08005747 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005748 if (rdev->raid_disk >= 0 &&
NeilBrownc303da62008-02-06 01:39:51 -08005749 !mddev->external &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005750 (test_bit(Faulty, &rdev->flags) ||
5751 ! test_bit(In_sync, &rdev->flags)) &&
5752 atomic_read(&rdev->nr_pending)==0) {
5753 if (mddev->pers->hot_remove_disk(
5754 mddev, rdev->raid_disk)==0) {
5755 char nm[20];
5756 sprintf(nm,"rd%d", rdev->raid_disk);
5757 sysfs_remove_link(&mddev->kobj, nm);
5758 rdev->raid_disk = -1;
5759 }
5760 }
5761
5762 if (mddev->degraded) {
NeilBrownd089c6a2008-02-06 01:39:59 -08005763 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005764 if (rdev->raid_disk < 0
5765 && !test_bit(Faulty, &rdev->flags)) {
5766 rdev->recovery_offset = 0;
5767 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5768 char nm[20];
5769 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005770 if (sysfs_create_link(&mddev->kobj,
5771 &rdev->kobj, nm))
5772 printk(KERN_WARNING
5773 "md: cannot register "
5774 "%s for %s\n",
5775 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005776 spares++;
5777 md_new_event(mddev);
5778 } else
5779 break;
5780 }
5781 }
5782 return spares;
5783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784/*
5785 * This routine is regularly called by all per-raid-array threads to
5786 * deal with generic issues like resync and super-block update.
5787 * Raid personalities that don't have a thread (linear/raid0) do not
5788 * need this as they never do any recovery or update the superblock.
5789 *
5790 * It does not do any resync itself, but rather "forks" off other threads
5791 * to do that as needed.
5792 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5793 * "->recovery" and create a thread at ->sync_thread.
5794 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5795 * and wakeups up this thread which will reap the thread and finish up.
5796 * This thread also removes any faulty devices (with nr_pending == 0).
5797 *
5798 * The overall approach is:
5799 * 1/ if the superblock needs updating, update it.
5800 * 2/ If a recovery thread is running, don't do anything else.
5801 * 3/ If recovery has finished, clean up, possibly marking spares active.
5802 * 4/ If there are any faulty devices, remove them.
5803 * 5/ If array is degraded, try to add spares devices
5804 * 6/ If array has spares or is not in-sync, start a resync thread.
5805 */
5806void md_check_recovery(mddev_t *mddev)
5807{
5808 mdk_rdev_t *rdev;
5809 struct list_head *rtmp;
5810
5811
NeilBrown5f404022005-06-21 17:17:16 -07005812 if (mddev->bitmap)
5813 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
5815 if (mddev->ro)
5816 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005817
5818 if (signal_pending(current)) {
5819 if (mddev->pers->sync_request) {
5820 printk(KERN_INFO "md: %s in immediate safe mode\n",
5821 mdname(mddev));
5822 mddev->safemode = 2;
5823 }
5824 flush_signals(current);
5825 }
5826
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08005828 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005830 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5831 (mddev->safemode == 1) ||
5832 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5833 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 ))
5835 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005836
NeilBrowndf5b89b2006-03-27 01:18:20 -08005837 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005838 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005839
NeilBrowna9701a32005-11-08 21:39:34 -08005840 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005841 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5842 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5843 mddev->in_sync = 1;
NeilBrowne6910632008-02-06 01:39:51 -08005844 if (mddev->persistent)
5845 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005846 }
5847 if (mddev->safemode == 1)
5848 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005849 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005850
NeilBrown850b2b42006-10-03 01:15:46 -07005851 if (mddev->flags)
5852 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005853
NeilBrown06d91a52005-06-21 17:17:12 -07005854
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5856 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5857 /* resync/recovery still happening */
5858 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5859 goto unlock;
5860 }
5861 if (mddev->sync_thread) {
5862 /* resync has finished, collect result */
5863 md_unregister_thread(mddev->sync_thread);
5864 mddev->sync_thread = NULL;
5865 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5866 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5867 /* success...*/
5868 /* activate any spares */
5869 mddev->pers->spare_active(mddev);
5870 }
NeilBrown850b2b42006-10-03 01:15:46 -07005871 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005872
5873 /* if array is no-longer degraded, then any saved_raid_disk
5874 * information must be scrapped
5875 */
5876 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08005877 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07005878 rdev->saved_raid_disk = -1;
5879
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 mddev->recovery = 0;
5881 /* flag recovery needed just to double check */
5882 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005883 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 goto unlock;
5885 }
NeilBrown24dd4692005-11-08 21:39:26 -08005886 /* Clear some bits that don't mean anything, but
5887 * might be left set
5888 */
5889 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5890 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5891 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5892 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005894 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5895 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 /* no recovery is running.
5897 * remove any failed drives, then
5898 * add spares if possible.
5899 * Spare are also removed and re-added, to allow
5900 * the personality to fail the re-add.
5901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005903 if (mddev->reshape_position != MaxSector) {
5904 if (mddev->pers->check_reshape(mddev) != 0)
5905 /* Cannot proceed */
5906 goto unlock;
5907 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5908 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005909 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5910 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5911 } else if (mddev->recovery_cp < MaxSector) {
5912 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5913 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5914 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005916
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 if (mddev->pers->sync_request) {
5918 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005919 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5920 /* We are adding a device or devices to an array
5921 * which has the bitmap stored on all devices.
5922 * So make sure all bitmap pages get written
5923 */
5924 bitmap_write_all(mddev->bitmap);
5925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 mddev->sync_thread = md_register_thread(md_do_sync,
5927 mddev,
5928 "%s_resync");
5929 if (!mddev->sync_thread) {
5930 printk(KERN_ERR "%s: could not start resync"
5931 " thread...\n",
5932 mdname(mddev));
5933 /* leave the spares where they are, it shouldn't hurt */
5934 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005935 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005937 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 }
5939 unlock:
5940 mddev_unlock(mddev);
5941 }
5942}
5943
Adrian Bunk75c96f82005-05-05 16:16:09 -07005944static int md_notify_reboot(struct notifier_block *this,
5945 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946{
5947 struct list_head *tmp;
5948 mddev_t *mddev;
5949
5950 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5951
5952 printk(KERN_INFO "md: stopping all md devices.\n");
5953
NeilBrown29ac4aa2008-02-06 01:39:58 -08005954 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005955 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005957 mddev_unlock(mddev);
5958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 /*
5960 * certain more exotic SCSI devices are known to be
5961 * volatile wrt too early system reboots. While the
5962 * right place to handle this issue is the given
5963 * driver, we do want to have a safe RAID driver ...
5964 */
5965 mdelay(1000*1);
5966 }
5967 return NOTIFY_DONE;
5968}
5969
Adrian Bunk75c96f82005-05-05 16:16:09 -07005970static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 .notifier_call = md_notify_reboot,
5972 .next = NULL,
5973 .priority = INT_MAX, /* before any real devices */
5974};
5975
5976static void md_geninit(void)
5977{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5979
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07005980 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981}
5982
Adrian Bunk75c96f82005-05-05 16:16:09 -07005983static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 if (register_blkdev(MAJOR_NR, "md"))
5986 return -1;
5987 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5988 unregister_blkdev(MAJOR_NR, "md");
5989 return -1;
5990 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005991 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5992 md_probe, NULL, NULL);
5993 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 md_probe, NULL, NULL);
5995
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005997 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
5999 md_geninit();
6000 return (0);
6001}
6002
6003
6004#ifndef MODULE
6005
6006/*
6007 * Searches all registered partitions for autorun RAID arrays
6008 * at boot time.
6009 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006010
6011static LIST_HEAD(all_detected_devices);
6012struct detected_devices_node {
6013 struct list_head list;
6014 dev_t dev;
6015};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
6017void md_autodetect_dev(dev_t dev)
6018{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006019 struct detected_devices_node *node_detected_dev;
6020
6021 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6022 if (node_detected_dev) {
6023 node_detected_dev->dev = dev;
6024 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6025 } else {
6026 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6027 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029}
6030
6031
6032static void autostart_arrays(int part)
6033{
6034 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006035 struct detected_devices_node *node_detected_dev;
6036 dev_t dev;
6037 int i_scanned, i_passed;
6038
6039 i_scanned = 0;
6040 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041
6042 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6043
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006044 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6045 i_scanned++;
6046 node_detected_dev = list_entry(all_detected_devices.next,
6047 struct detected_devices_node, list);
6048 list_del(&node_detected_dev->list);
6049 dev = node_detected_dev->dev;
6050 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006051 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 if (IS_ERR(rdev))
6053 continue;
6054
NeilBrownb2d444d2005-11-08 21:39:31 -08006055 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 MD_BUG();
6057 continue;
6058 }
NeilBrownd0fae182008-03-04 14:29:31 -08006059 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006061 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006063
6064 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6065 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066
6067 autorun_devices(part);
6068}
6069
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006070#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071
6072static __exit void md_exit(void)
6073{
6074 mddev_t *mddev;
6075 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006076
NeilBrowne8703fe2006-10-03 01:15:59 -07006077 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6078 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079
6080 unregister_blkdev(MAJOR_NR,"md");
6081 unregister_blkdev(mdp_major, "mdp");
6082 unregister_reboot_notifier(&md_notifier);
6083 unregister_sysctl_table(raid_table_header);
6084 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006085 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 struct gendisk *disk = mddev->gendisk;
6087 if (!disk)
6088 continue;
6089 export_array(mddev);
6090 del_gendisk(disk);
6091 put_disk(disk);
6092 mddev->gendisk = NULL;
6093 mddev_put(mddev);
6094 }
6095}
6096
Dan Williams685784a2007-07-09 11:56:42 -07006097subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006098module_exit(md_exit)
6099
NeilBrownf91de922005-11-08 21:39:36 -08006100static int get_ro(char *buffer, struct kernel_param *kp)
6101{
6102 return sprintf(buffer, "%d", start_readonly);
6103}
6104static int set_ro(const char *val, struct kernel_param *kp)
6105{
6106 char *e;
6107 int num = simple_strtoul(val, &e, 10);
6108 if (*val && (*e == '\0' || *e == '\n')) {
6109 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006110 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006111 }
6112 return -EINVAL;
6113}
6114
NeilBrown80ca3a42006-07-10 04:44:18 -07006115module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6116module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006117
NeilBrownf91de922005-11-08 21:39:36 -08006118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119EXPORT_SYMBOL(register_md_personality);
6120EXPORT_SYMBOL(unregister_md_personality);
6121EXPORT_SYMBOL(md_error);
6122EXPORT_SYMBOL(md_done_sync);
6123EXPORT_SYMBOL(md_write_start);
6124EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006125EXPORT_SYMBOL(md_register_thread);
6126EXPORT_SYMBOL(md_unregister_thread);
6127EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128EXPORT_SYMBOL(md_check_recovery);
6129MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006130MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006131MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);