blob: 80bcba9951c3bc3a900523c70aa2973830a4a3b8 [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
Bernd Schubert90b08712008-05-23 13:04:38 -070077static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
78
Adrian Bunk5e563412006-06-26 00:27:42 -070079#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070085 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
89 *
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080091 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93
94static int sysctl_speed_limit_min = 1000;
95static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080096static inline int speed_min(mddev_t *mddev)
97{
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
100}
101
102static inline int speed_max(mddev_t *mddev)
103{
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static struct ctl_table_header *raid_table_header;
109
110static ctl_table raid_table[] = {
111 {
112 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
113 .procname = "speed_limit_min",
114 .data = &sysctl_speed_limit_min,
115 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700116 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .proc_handler = &proc_dointvec,
118 },
119 {
120 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .proc_handler = &proc_dointvec,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_dir_table[] = {
131 {
132 .ctl_name = DEV_RAID,
133 .procname = "raid",
134 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700135 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .child = raid_table,
137 },
138 { .ctl_name = 0 }
139};
140
141static ctl_table raid_root_table[] = {
142 {
143 .ctl_name = CTL_DEV,
144 .procname = "dev",
145 .maxlen = 0,
146 .mode = 0555,
147 .child = raid_dir_table,
148 },
149 { .ctl_name = 0 }
150};
151
152static struct block_device_operations md_fops;
153
NeilBrownf91de922005-11-08 21:39:36 -0800154static int start_readonly;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
NeilBrownd7603b72006-01-06 00:20:30 -0800157 * We have a system wide 'event count' that is incremented
158 * on any 'interesting' event, and readers of /proc/mdstat
159 * can use 'poll' or 'select' to find out when the event
160 * count increases.
161 *
162 * Events are:
163 * start array, stop array, error, add device, remove device,
164 * start build, activate spare
165 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800166static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800167static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800168void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800169{
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
172}
NeilBrown29269552006-03-27 01:18:10 -0800173EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800174
NeilBrownc331eb02006-05-30 21:27:13 -0700175/* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
177 */
Adrian Bunk05381952006-06-26 00:28:01 -0700178static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700179{
180 atomic_inc(&md_event_count);
181 wake_up(&md_event_waiters);
182}
183
NeilBrownd7603b72006-01-06 00:20:30 -0800184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
187 */
188static LIST_HEAD(all_mddevs);
189static DEFINE_SPINLOCK(all_mddevs_lock);
190
191
192/*
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
198 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800199#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
203 mddev = NULL;}); \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
211 tmp = tmp->next;}) \
212 )
213
214
Jens Axboe165125e2007-07-24 09:28:11 +0200215static int md_fail_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
NeilBrown6712ecf2007-09-27 12:47:43 +0200217 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219}
220
221static inline mddev_t *mddev_get(mddev_t *mddev)
222{
223 atomic_inc(&mddev->active);
224 return mddev;
225}
226
227static void mddev_put(mddev_t *mddev)
228{
229 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
230 return;
231 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
232 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800233 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500234 blk_cleanup_queue(mddev->queue);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800235 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800236 } else
237 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static mddev_t * mddev_find(dev_t unit)
241{
242 mddev_t *mddev, *new = NULL;
243
244 retry:
245 spin_lock(&all_mddevs_lock);
246 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
247 if (mddev->unit == unit) {
248 mddev_get(mddev);
249 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700250 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return mddev;
252 }
253
254 if (new) {
255 list_add(&new->all_mddevs, &all_mddevs);
256 spin_unlock(&all_mddevs_lock);
257 return new;
258 }
259 spin_unlock(&all_mddevs_lock);
260
NeilBrown9ffae0c2006-01-06 00:20:32 -0800261 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!new)
263 return NULL;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 new->unit = unit;
266 if (MAJOR(unit) == MD_MAJOR)
267 new->md_minor = MINOR(unit);
268 else
269 new->md_minor = MINOR(unit) >> MdpMinorShift;
270
NeilBrowndf5b89b2006-03-27 01:18:20 -0800271 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 INIT_LIST_HEAD(&new->disks);
273 INIT_LIST_HEAD(&new->all_mddevs);
274 init_timer(&new->safemode_timer);
275 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700276 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700277 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700278 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700279 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000280 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800281 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700282 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 new->queue = blk_alloc_queue(GFP_KERNEL);
285 if (!new->queue) {
286 kfree(new);
287 return NULL;
288 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200289 /* Can be unlocked because the queue is new: no concurrency */
290 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 blk_queue_make_request(new->queue, md_fail_request);
293
294 goto retry;
295}
296
297static inline int mddev_lock(mddev_t * mddev)
298{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800299 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static inline int mddev_trylock(mddev_t * mddev)
303{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800304 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307static inline void mddev_unlock(mddev_t * mddev)
308{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800309 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
NeilBrown005eca52005-08-22 13:11:08 -0700311 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
NeilBrown2989ddb2006-01-06 00:20:43 -0800314static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 mdk_rdev_t * rdev;
317 struct list_head *tmp;
318
NeilBrownd089c6a2008-02-06 01:39:59 -0800319 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (rdev->desc_nr == nr)
321 return rdev;
322 }
323 return NULL;
324}
325
326static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
327{
328 struct list_head *tmp;
329 mdk_rdev_t *rdev;
330
NeilBrownd089c6a2008-02-06 01:39:59 -0800331 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (rdev->bdev->bd_dev == dev)
333 return rdev;
334 }
335 return NULL;
336}
337
NeilBrownd9d166c2006-01-06 00:20:51 -0800338static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800339{
340 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800341 list_for_each_entry(pers, &pers_list, list) {
342 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800343 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800344 if (strcmp(pers->name, clevel)==0)
345 return pers;
346 }
NeilBrown2604b702006-01-06 00:20:36 -0800347 return NULL;
348}
349
Jesper Juhl77933d72005-07-27 11:46:09 -0700350static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
353 return MD_NEW_SIZE_BLOCKS(size);
354}
355
356static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
357{
358 sector_t size;
359
360 size = rdev->sb_offset;
361
362 if (chunk_size)
363 size &= ~((sector_t)chunk_size/1024 - 1);
364 return size;
365}
366
367static int alloc_disk_sb(mdk_rdev_t * rdev)
368{
369 if (rdev->sb_page)
370 MD_BUG();
371
372 rdev->sb_page = alloc_page(GFP_KERNEL);
373 if (!rdev->sb_page) {
374 printk(KERN_ALERT "md: out of memory.\n");
375 return -EINVAL;
376 }
377
378 return 0;
379}
380
381static void free_disk_sb(mdk_rdev_t * rdev)
382{
383 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800384 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 rdev->sb_loaded = 0;
386 rdev->sb_page = NULL;
387 rdev->sb_offset = 0;
388 rdev->size = 0;
389 }
390}
391
392
NeilBrown6712ecf2007-09-27 12:47:43 +0200393static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700394{
395 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800396 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700397
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700398 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
399 printk("md: super_written gets error=%d, uptodate=%d\n",
400 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
401 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800402 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700403 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700404
NeilBrowna9701a32005-11-08 21:39:34 -0800405 if (atomic_dec_and_test(&mddev->pending_writes))
406 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700407 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700408}
409
NeilBrown6712ecf2007-09-27 12:47:43 +0200410static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800411{
412 struct bio *bio2 = bio->bi_private;
413 mdk_rdev_t *rdev = bio2->bi_private;
414 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800415
416 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
417 error == -EOPNOTSUPP) {
418 unsigned long flags;
419 /* barriers don't appear to be supported :-( */
420 set_bit(BarriersNotsupp, &rdev->flags);
421 mddev->barriers_work = 0;
422 spin_lock_irqsave(&mddev->write_lock, flags);
423 bio2->bi_next = mddev->biolist;
424 mddev->biolist = bio2;
425 spin_unlock_irqrestore(&mddev->write_lock, flags);
426 wake_up(&mddev->sb_wait);
427 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200428 } else {
429 bio_put(bio2);
430 bio->bi_private = rdev;
431 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800432 }
NeilBrowna9701a32005-11-08 21:39:34 -0800433}
434
NeilBrown7bfa19f2005-06-21 17:17:28 -0700435void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
436 sector_t sector, int size, struct page *page)
437{
438 /* write first size bytes of page to sector of rdev
439 * Increment mddev->pending_writes before returning
440 * and decrement it on completion, waking up sb_wait
441 * if zero is reached.
442 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800443 *
444 * As we might need to resubmit the request if BIO_RW_BARRIER
445 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700446 */
447 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800448 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700449
450 bio->bi_bdev = rdev->bdev;
451 bio->bi_sector = sector;
452 bio_add_page(bio, page, size, 0);
453 bio->bi_private = rdev;
454 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800455 bio->bi_rw = rw;
456
NeilBrown7bfa19f2005-06-21 17:17:28 -0700457 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800458 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
459 struct bio *rbio;
460 rw |= (1<<BIO_RW_BARRIER);
461 rbio = bio_clone(bio, GFP_NOIO);
462 rbio->bi_private = bio;
463 rbio->bi_end_io = super_written_barrier;
464 submit_bio(rw, rbio);
465 } else
466 submit_bio(rw, bio);
467}
468
469void md_super_wait(mddev_t *mddev)
470{
471 /* wait for all superblock writes that were scheduled to complete.
472 * if any had to be retried (due to BARRIER problems), retry them
473 */
474 DEFINE_WAIT(wq);
475 for(;;) {
476 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
477 if (atomic_read(&mddev->pending_writes)==0)
478 break;
479 while (mddev->biolist) {
480 struct bio *bio;
481 spin_lock_irq(&mddev->write_lock);
482 bio = mddev->biolist;
483 mddev->biolist = bio->bi_next ;
484 bio->bi_next = NULL;
485 spin_unlock_irq(&mddev->write_lock);
486 submit_bio(bio->bi_rw, bio);
487 }
488 schedule();
489 }
490 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700491}
492
NeilBrown6712ecf2007-09-27 12:47:43 +0200493static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
NeilBrowna654b9d82005-06-21 17:17:27 -0700498int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct page *page, int rw)
500{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700501 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct completion event;
503 int ret;
504
505 rw |= (1 << BIO_RW_SYNC);
506
507 bio->bi_bdev = bdev;
508 bio->bi_sector = sector;
509 bio_add_page(bio, page, size, 0);
510 init_completion(&event);
511 bio->bi_private = &event;
512 bio->bi_end_io = bi_complete;
513 submit_bio(rw, bio);
514 wait_for_completion(&event);
515
516 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
517 bio_put(bio);
518 return ret;
519}
NeilBrowna8745db2006-01-06 00:20:34 -0800520EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
NeilBrown0002b272005-09-09 16:23:53 -0700522static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 char b[BDEVNAME_SIZE];
525 if (!rdev->sb_page) {
526 MD_BUG();
527 return -EINVAL;
528 }
529 if (rdev->sb_loaded)
530 return 0;
531
532
NeilBrown0002b272005-09-09 16:23:53 -0700533 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto fail;
535 rdev->sb_loaded = 1;
536 return 0;
537
538fail:
539 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
540 bdevname(rdev->bdev,b));
541 return -EINVAL;
542}
543
544static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
545{
546 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
547 (sb1->set_uuid1 == sb2->set_uuid1) &&
548 (sb1->set_uuid2 == sb2->set_uuid2) &&
549 (sb1->set_uuid3 == sb2->set_uuid3))
550
551 return 1;
552
553 return 0;
554}
555
556
557static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
558{
559 int ret;
560 mdp_super_t *tmp1, *tmp2;
561
562 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
563 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
564
565 if (!tmp1 || !tmp2) {
566 ret = 0;
567 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
568 goto abort;
569 }
570
571 *tmp1 = *sb1;
572 *tmp2 = *sb2;
573
574 /*
575 * nr_disks is not constant
576 */
577 tmp1->nr_disks = 0;
578 tmp2->nr_disks = 0;
579
580 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
581 ret = 0;
582 else
583 ret = 1;
584
585abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700586 kfree(tmp1);
587 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 return ret;
589}
590
NeilBrown4d167f02007-05-09 02:35:37 -0700591
592static u32 md_csum_fold(u32 csum)
593{
594 csum = (csum & 0xffff) + (csum >> 16);
595 return (csum & 0xffff) + (csum >> 16);
596}
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598static unsigned int calc_sb_csum(mdp_super_t * sb)
599{
NeilBrown4d167f02007-05-09 02:35:37 -0700600 u64 newcsum = 0;
601 u32 *sb32 = (u32*)sb;
602 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unsigned int disk_csum, csum;
604
605 disk_csum = sb->sb_csum;
606 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700607
608 for (i = 0; i < MD_SB_BYTES/4 ; i++)
609 newcsum += sb32[i];
610 csum = (newcsum & 0xffffffff) + (newcsum>>32);
611
612
613#ifdef CONFIG_ALPHA
614 /* This used to use csum_partial, which was wrong for several
615 * reasons including that different results are returned on
616 * different architectures. It isn't critical that we get exactly
617 * the same return value as before (we always csum_fold before
618 * testing, and that removes any differences). However as we
619 * know that csum_partial always returned a 16bit value on
620 * alphas, do a fold to maximise conformity to previous behaviour.
621 */
622 sb->sb_csum = md_csum_fold(disk_csum);
623#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700625#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return csum;
627}
628
629
630/*
631 * Handle superblock details.
632 * We want to be able to handle multiple superblock formats
633 * so we have a common interface to them all, and an array of
634 * different handlers.
635 * We rely on user-space to write the initial superblock, and support
636 * reading and updating of superblocks.
637 * Interface methods are:
638 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
639 * loads and validates a superblock on dev.
640 * if refdev != NULL, compare superblocks on both devices
641 * Return:
642 * 0 - dev has a superblock that is compatible with refdev
643 * 1 - dev has a superblock that is compatible and newer than refdev
644 * so dev should be used as the refdev in future
645 * -EINVAL superblock incompatible or invalid
646 * -othererror e.g. -EIO
647 *
648 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
649 * Verify that dev is acceptable into mddev.
650 * The first time, mddev->raid_disks will be 0, and data from
651 * dev should be merged in. Subsequent calls check that dev
652 * is new enough. Return 0 or -EINVAL
653 *
654 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
655 * Update the superblock for rdev with data in mddev
656 * This does not write to disc.
657 *
658 */
659
660struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000661 char *name;
662 struct module *owner;
663 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
664 int minor_version);
665 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
666 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
667 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
668 unsigned long long size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669};
670
671/*
672 * load_super for 0.90.0
673 */
674static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
675{
676 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
677 mdp_super_t *sb;
678 int ret;
679 sector_t sb_offset;
680
681 /*
682 * Calculate the position of the superblock,
683 * it's at the end of the disk.
684 *
685 * It also happens to be a multiple of 4Kb.
686 */
687 sb_offset = calc_dev_sboffset(rdev->bdev);
688 rdev->sb_offset = sb_offset;
689
NeilBrown0002b272005-09-09 16:23:53 -0700690 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (ret) return ret;
692
693 ret = -EINVAL;
694
695 bdevname(rdev->bdev, b);
696 sb = (mdp_super_t*)page_address(rdev->sb_page);
697
698 if (sb->md_magic != MD_SB_MAGIC) {
699 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
700 b);
701 goto abort;
702 }
703
704 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800705 sb->minor_version < 90 ||
706 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
708 sb->major_version, sb->minor_version,
709 b);
710 goto abort;
711 }
712
713 if (sb->raid_disks <= 0)
714 goto abort;
715
NeilBrown4d167f02007-05-09 02:35:37 -0700716 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
718 b);
719 goto abort;
720 }
721
722 rdev->preferred_minor = sb->md_minor;
723 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700724 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
NeilBrowne11e93f2007-05-09 02:35:36 -0700726 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
727 if (sb->level != 1 && sb->level != 4
728 && sb->level != 5 && sb->level != 6
729 && sb->level != 10) {
730 /* FIXME use a better test */
731 printk(KERN_WARNING
732 "md: bitmaps not supported for this level.\n");
733 goto abort;
734 }
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (sb->level == LEVEL_MULTIPATH)
738 rdev->desc_nr = -1;
739 else
740 rdev->desc_nr = sb->this_disk.number;
741
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700742 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700744 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 __u64 ev1, ev2;
746 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
747 if (!uuid_equal(refsb, sb)) {
748 printk(KERN_WARNING "md: %s has different UUID to %s\n",
749 b, bdevname(refdev->bdev,b2));
750 goto abort;
751 }
752 if (!sb_equal(refsb, sb)) {
753 printk(KERN_WARNING "md: %s has same UUID"
754 " but different superblock to %s\n",
755 b, bdevname(refdev->bdev, b2));
756 goto abort;
757 }
758 ev1 = md_event(sb);
759 ev2 = md_event(refsb);
760 if (ev1 > ev2)
761 ret = 1;
762 else
763 ret = 0;
764 }
765 rdev->size = calc_dev_size(rdev, sb->chunk_size);
766
NeilBrown2bf071b2006-01-06 00:20:55 -0800767 if (rdev->size < sb->size && sb->level > 1)
768 /* "this cannot possibly happen" ... */
769 ret = -EINVAL;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 abort:
772 return ret;
773}
774
775/*
776 * validate_super for 0.90.0
777 */
778static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
779{
780 mdp_disk_t *desc;
781 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700782 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
NeilBrown41158c72005-06-21 17:17:25 -0700784 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800785 clear_bit(Faulty, &rdev->flags);
786 clear_bit(In_sync, &rdev->flags);
787 clear_bit(WriteMostly, &rdev->flags);
788 clear_bit(BarriersNotsupp, &rdev->flags);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (mddev->raid_disks == 0) {
791 mddev->major_version = 0;
792 mddev->minor_version = sb->minor_version;
793 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800794 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 mddev->chunk_size = sb->chunk_size;
796 mddev->ctime = sb->ctime;
797 mddev->utime = sb->utime;
798 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800799 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 mddev->layout = sb->layout;
801 mddev->raid_disks = sb->raid_disks;
802 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700803 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700804 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700805 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
NeilBrownf6705572006-03-27 01:18:11 -0800807 if (mddev->minor_version >= 91) {
808 mddev->reshape_position = sb->reshape_position;
809 mddev->delta_disks = sb->delta_disks;
810 mddev->new_level = sb->new_level;
811 mddev->new_layout = sb->new_layout;
812 mddev->new_chunk = sb->new_chunk;
813 } else {
814 mddev->reshape_position = MaxSector;
815 mddev->delta_disks = 0;
816 mddev->new_level = mddev->level;
817 mddev->new_layout = mddev->layout;
818 mddev->new_chunk = mddev->chunk_size;
819 }
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (sb->state & (1<<MD_SB_CLEAN))
822 mddev->recovery_cp = MaxSector;
823 else {
824 if (sb->events_hi == sb->cp_events_hi &&
825 sb->events_lo == sb->cp_events_lo) {
826 mddev->recovery_cp = sb->recovery_cp;
827 } else
828 mddev->recovery_cp = 0;
829 }
830
831 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
832 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
833 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
834 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
835
836 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700837
838 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700839 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700840 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700841
NeilBrown41158c72005-06-21 17:17:25 -0700842 } else if (mddev->pers == NULL) {
843 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 ++ev1;
845 if (ev1 < mddev->events)
846 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700847 } else if (mddev->bitmap) {
848 /* if adding to array with a bitmap, then we can accept an
849 * older device ... but not too old.
850 */
NeilBrown41158c72005-06-21 17:17:25 -0700851 if (ev1 < mddev->bitmap->events_cleared)
852 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700853 } else {
854 if (ev1 < mddev->events)
855 /* just a hot-add of a new device, leave raid_disk at -1 */
856 return 0;
857 }
NeilBrown41158c72005-06-21 17:17:25 -0700858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 desc = sb->disks + rdev->desc_nr;
861
862 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800863 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700864 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
865 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800866 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 rdev->raid_disk = desc->raid_disk;
868 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700869 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
870 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700871 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800872 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return 0;
874}
875
876/*
877 * sync_super for 0.90.0
878 */
879static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
880{
881 mdp_super_t *sb;
882 struct list_head *tmp;
883 mdk_rdev_t *rdev2;
884 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* make rdev->sb match mddev data..
888 *
889 * 1/ zero out disks
890 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
891 * 3/ any empty disks < next_spare become removed
892 *
893 * disks[0] gets initialised to REMOVED because
894 * we cannot be sure from other fields if it has
895 * been initialised or not.
896 */
897 int i;
898 int active=0, working=0,failed=0,spare=0,nr_disks=0;
899
NeilBrown61181562005-09-09 16:24:02 -0700900 rdev->sb_size = MD_SB_BYTES;
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 sb = (mdp_super_t*)page_address(rdev->sb_page);
903
904 memset(sb, 0, sizeof(*sb));
905
906 sb->md_magic = MD_SB_MAGIC;
907 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 sb->patch_version = mddev->patch_version;
909 sb->gvalid_words = 0; /* ignored */
910 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
911 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
912 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
913 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
914
915 sb->ctime = mddev->ctime;
916 sb->level = mddev->level;
917 sb->size = mddev->size;
918 sb->raid_disks = mddev->raid_disks;
919 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800920 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sb->utime = mddev->utime;
922 sb->state = 0;
923 sb->events_hi = (mddev->events>>32);
924 sb->events_lo = (u32)mddev->events;
925
NeilBrownf6705572006-03-27 01:18:11 -0800926 if (mddev->reshape_position == MaxSector)
927 sb->minor_version = 90;
928 else {
929 sb->minor_version = 91;
930 sb->reshape_position = mddev->reshape_position;
931 sb->new_level = mddev->new_level;
932 sb->delta_disks = mddev->delta_disks;
933 sb->new_layout = mddev->new_layout;
934 sb->new_chunk = mddev->new_chunk;
935 }
936 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (mddev->in_sync)
938 {
939 sb->recovery_cp = mddev->recovery_cp;
940 sb->cp_events_hi = (mddev->events>>32);
941 sb->cp_events_lo = (u32)mddev->events;
942 if (mddev->recovery_cp == MaxSector)
943 sb->state = (1<< MD_SB_CLEAN);
944 } else
945 sb->recovery_cp = 0;
946
947 sb->layout = mddev->layout;
948 sb->chunk_size = mddev->chunk_size;
949
NeilBrowna654b9d82005-06-21 17:17:27 -0700950 if (mddev->bitmap && mddev->bitmap_file == NULL)
951 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800954 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800956 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800957 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
958 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800959 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800961 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800962 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 d = &sb->disks[rdev2->desc_nr];
964 nr_disks++;
965 d->number = rdev2->desc_nr;
966 d->major = MAJOR(rdev2->bdev->bd_dev);
967 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800968 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
969 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 d->raid_disk = rdev2->raid_disk;
971 else
972 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800973 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800975 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 d->state = (1<<MD_DISK_ACTIVE);
977 d->state |= (1<<MD_DISK_SYNC);
978 active++;
979 working++;
980 } else {
981 d->state = 0;
982 spare++;
983 working++;
984 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700985 if (test_bit(WriteMostly, &rdev2->flags))
986 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* now set the "removed" and "faulty" bits on any missing devices */
989 for (i=0 ; i < mddev->raid_disks ; i++) {
990 mdp_disk_t *d = &sb->disks[i];
991 if (d->state == 0 && d->number == 0) {
992 d->number = i;
993 d->raid_disk = i;
994 d->state = (1<<MD_DISK_REMOVED);
995 d->state |= (1<<MD_DISK_FAULTY);
996 failed++;
997 }
998 }
999 sb->nr_disks = nr_disks;
1000 sb->active_disks = active;
1001 sb->working_disks = working;
1002 sb->failed_disks = failed;
1003 sb->spare_disks = spare;
1004
1005 sb->this_disk = sb->disks[rdev->desc_nr];
1006 sb->sb_csum = calc_sb_csum(sb);
1007}
1008
1009/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001010 * rdev_size_change for 0.90.0
1011 */
1012static unsigned long long
1013super_90_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1014{
1015 if (size && size < rdev->mddev->size)
1016 return 0; /* component must fit device */
1017 size *= 2; /* convert to sectors */
1018 if (rdev->mddev->bitmap_offset)
1019 return 0; /* can't move bitmap */
1020 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
1021 if (!size || size > rdev->sb_offset*2)
1022 size = rdev->sb_offset*2;
1023 md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size,
1024 rdev->sb_page);
1025 md_super_wait(rdev->mddev);
1026 return size/2; /* kB for sysfs */
1027}
1028
1029
1030/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * version 1 superblock
1032 */
1033
NeilBrown1c05b4b2006-10-21 10:24:08 -07001034static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001036 __le32 disk_csum;
1037 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 unsigned long long newcsum;
1039 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001040 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 int i;
1042
1043 disk_csum = sb->sb_csum;
1044 sb->sb_csum = 0;
1045 newcsum = 0;
1046 for (i=0; size>=4; size -= 4 )
1047 newcsum += le32_to_cpu(*isuper++);
1048
1049 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001050 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1053 sb->sb_csum = disk_csum;
1054 return cpu_to_le32(csum);
1055}
1056
1057static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1058{
1059 struct mdp_superblock_1 *sb;
1060 int ret;
1061 sector_t sb_offset;
1062 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001063 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 /*
1066 * Calculate the position of the superblock.
1067 * It is always aligned to a 4K boundary and
1068 * depeding on minor_version, it can be:
1069 * 0: At least 8K, but less than 12K, from end of device
1070 * 1: At start of device
1071 * 2: 4K from start of device.
1072 */
1073 switch(minor_version) {
1074 case 0:
1075 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1076 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001077 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* convert from sectors to K */
1079 sb_offset /= 2;
1080 break;
1081 case 1:
1082 sb_offset = 0;
1083 break;
1084 case 2:
1085 sb_offset = 4;
1086 break;
1087 default:
1088 return -EINVAL;
1089 }
1090 rdev->sb_offset = sb_offset;
1091
NeilBrown0002b272005-09-09 16:23:53 -07001092 /* superblock is rarely larger than 1K, but it can be larger,
1093 * and it is safe to read 4k, so we do that
1094 */
1095 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (ret) return ret;
1097
1098
1099 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1100
1101 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1102 sb->major_version != cpu_to_le32(1) ||
1103 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1104 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001105 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return -EINVAL;
1107
1108 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1109 printk("md: invalid superblock checksum on %s\n",
1110 bdevname(rdev->bdev,b));
1111 return -EINVAL;
1112 }
1113 if (le64_to_cpu(sb->data_size) < 10) {
1114 printk("md: data_size too small on %s\n",
1115 bdevname(rdev->bdev,b));
1116 return -EINVAL;
1117 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001118 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1119 if (sb->level != cpu_to_le32(1) &&
1120 sb->level != cpu_to_le32(4) &&
1121 sb->level != cpu_to_le32(5) &&
1122 sb->level != cpu_to_le32(6) &&
1123 sb->level != cpu_to_le32(10)) {
1124 printk(KERN_WARNING
1125 "md: bitmaps not supported for this level.\n");
1126 return -EINVAL;
1127 }
1128 }
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 rdev->preferred_minor = 0xffff;
1131 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001132 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
NeilBrown0002b272005-09-09 16:23:53 -07001134 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001135 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001136 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001137 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1138
1139 if (minor_version
1140 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1141 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001142
NeilBrown31b65a02006-07-10 04:44:14 -07001143 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1144 rdev->desc_nr = -1;
1145 else
1146 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1147
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001148 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001149 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001150 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 __u64 ev1, ev2;
1152 struct mdp_superblock_1 *refsb =
1153 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1154
1155 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1156 sb->level != refsb->level ||
1157 sb->layout != refsb->layout ||
1158 sb->chunksize != refsb->chunksize) {
1159 printk(KERN_WARNING "md: %s has strangely different"
1160 " superblock to %s\n",
1161 bdevname(rdev->bdev,b),
1162 bdevname(refdev->bdev,b2));
1163 return -EINVAL;
1164 }
1165 ev1 = le64_to_cpu(sb->events);
1166 ev2 = le64_to_cpu(refsb->events);
1167
1168 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001169 ret = 1;
1170 else
1171 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
NeilBrowna1801f82008-03-04 14:29:31 -08001173 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1175 else
1176 rdev->size = rdev->sb_offset;
1177 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1178 return -EINVAL;
1179 rdev->size = le64_to_cpu(sb->data_size)/2;
1180 if (le32_to_cpu(sb->chunksize))
1181 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001182
NeilBrown1c05b4b2006-10-21 10:24:08 -07001183 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001184 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001185 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186}
1187
1188static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1189{
1190 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001191 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
NeilBrown41158c72005-06-21 17:17:25 -07001193 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001194 clear_bit(Faulty, &rdev->flags);
1195 clear_bit(In_sync, &rdev->flags);
1196 clear_bit(WriteMostly, &rdev->flags);
1197 clear_bit(BarriersNotsupp, &rdev->flags);
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (mddev->raid_disks == 0) {
1200 mddev->major_version = 1;
1201 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001202 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1204 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1205 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1206 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001207 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 mddev->layout = le32_to_cpu(sb->layout);
1209 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1210 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001211 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001212 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001213 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1216 memcpy(mddev->uuid, sb->set_uuid, 16);
1217
1218 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001219
NeilBrown71c08052005-09-09 16:23:51 -07001220 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001221 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001222 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001223
NeilBrownf6705572006-03-27 01:18:11 -08001224 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1225 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1226 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1227 mddev->new_level = le32_to_cpu(sb->new_level);
1228 mddev->new_layout = le32_to_cpu(sb->new_layout);
1229 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1230 } else {
1231 mddev->reshape_position = MaxSector;
1232 mddev->delta_disks = 0;
1233 mddev->new_level = mddev->level;
1234 mddev->new_layout = mddev->layout;
1235 mddev->new_chunk = mddev->chunk_size;
1236 }
1237
NeilBrown41158c72005-06-21 17:17:25 -07001238 } else if (mddev->pers == NULL) {
1239 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 ++ev1;
1241 if (ev1 < mddev->events)
1242 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001243 } else if (mddev->bitmap) {
1244 /* If adding to array with a bitmap, then we can accept an
1245 * older device, but not too old.
1246 */
NeilBrown41158c72005-06-21 17:17:25 -07001247 if (ev1 < mddev->bitmap->events_cleared)
1248 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001249 } else {
1250 if (ev1 < mddev->events)
1251 /* just a hot-add of a new device, leave raid_disk at -1 */
1252 return 0;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (mddev->level != LEVEL_MULTIPATH) {
1255 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1257 switch(role) {
1258 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 break;
1260 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001261 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 break;
1263 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001264 if ((le32_to_cpu(sb->feature_map) &
1265 MD_FEATURE_RECOVERY_OFFSET))
1266 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1267 else
1268 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 rdev->raid_disk = role;
1270 break;
1271 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001272 if (sb->devflags & WriteMostly1)
1273 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001274 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001275 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
1278}
1279
1280static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1281{
1282 struct mdp_superblock_1 *sb;
1283 struct list_head *tmp;
1284 mdk_rdev_t *rdev2;
1285 int max_dev, i;
1286 /* make rdev->sb match mddev and rdev data. */
1287
1288 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1289
1290 sb->feature_map = 0;
1291 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001292 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 memset(sb->pad1, 0, sizeof(sb->pad1));
1294 memset(sb->pad2, 0, sizeof(sb->pad2));
1295 memset(sb->pad3, 0, sizeof(sb->pad3));
1296
1297 sb->utime = cpu_to_le64((__u64)mddev->utime);
1298 sb->events = cpu_to_le64(mddev->events);
1299 if (mddev->in_sync)
1300 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1301 else
1302 sb->resync_offset = cpu_to_le64(0);
1303
NeilBrown1c05b4b2006-10-21 10:24:08 -07001304 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001305
NeilBrownf0ca3402006-02-02 14:28:04 -08001306 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001307 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001308
NeilBrowna654b9d82005-06-21 17:17:27 -07001309 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1310 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001311 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001312 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001313
1314 if (rdev->raid_disk >= 0 &&
1315 !test_bit(In_sync, &rdev->flags) &&
1316 rdev->recovery_offset > 0) {
1317 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1318 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1319 }
1320
NeilBrownf6705572006-03-27 01:18:11 -08001321 if (mddev->reshape_position != MaxSector) {
1322 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1323 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1324 sb->new_layout = cpu_to_le32(mddev->new_layout);
1325 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1326 sb->new_level = cpu_to_le32(mddev->new_level);
1327 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1328 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001331 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (rdev2->desc_nr+1 > max_dev)
1333 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001334
1335 if (max_dev > le32_to_cpu(sb->max_dev))
1336 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 for (i=0; i<max_dev;i++)
1338 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1339
NeilBrownd089c6a2008-02-06 01:39:59 -08001340 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001342 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001344 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001346 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1347 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 else
1349 sb->dev_roles[i] = cpu_to_le16(0xffff);
1350 }
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 sb->sb_csum = calc_sb_1_csum(sb);
1353}
1354
Chris Webb0cd17fe2008-06-28 08:31:46 +10001355static unsigned long long
1356super_1_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1357{
1358 struct mdp_superblock_1 *sb;
1359 unsigned long long max_size;
1360 if (size && size < rdev->mddev->size)
1361 return 0; /* component must fit device */
1362 size *= 2; /* convert to sectors */
1363 if (rdev->sb_offset < rdev->data_offset/2) {
1364 /* minor versions 1 and 2; superblock before data */
1365 max_size = (rdev->bdev->bd_inode->i_size >> 9);
1366 max_size -= rdev->data_offset;
1367 if (!size || size > max_size)
1368 size = max_size;
1369 } else if (rdev->mddev->bitmap_offset) {
1370 /* minor version 0 with bitmap we can't move */
1371 return 0;
1372 } else {
1373 /* minor version 0; superblock after data */
1374 sector_t sb_offset;
1375 sb_offset = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1376 sb_offset &= ~(sector_t)(4*2 - 1);
1377 max_size = rdev->size*2 + sb_offset - rdev->sb_offset*2;
1378 if (!size || size > max_size)
1379 size = max_size;
1380 rdev->sb_offset = sb_offset/2;
1381 }
1382 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1383 sb->data_size = cpu_to_le64(size);
1384 sb->super_offset = rdev->sb_offset*2;
1385 sb->sb_csum = calc_sb_1_csum(sb);
1386 md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size,
1387 rdev->sb_page);
1388 md_super_wait(rdev->mddev);
1389 return size/2; /* kB for sysfs */
1390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Adrian Bunk75c96f82005-05-05 16:16:09 -07001392static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 [0] = {
1394 .name = "0.90.0",
1395 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001396 .load_super = super_90_load,
1397 .validate_super = super_90_validate,
1398 .sync_super = super_90_sync,
1399 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 },
1401 [1] = {
1402 .name = "md-1",
1403 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001404 .load_super = super_1_load,
1405 .validate_super = super_1_validate,
1406 .sync_super = super_1_sync,
1407 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 },
1409};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1412{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001413 struct list_head *tmp, *tmp2;
1414 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
NeilBrownd089c6a2008-02-06 01:39:59 -08001416 rdev_for_each(rdev, tmp, mddev1)
1417 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001418 if (rdev->bdev->bd_contains ==
1419 rdev2->bdev->bd_contains)
1420 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 return 0;
1423}
1424
1425static LIST_HEAD(pending_raid_disks);
1426
1427static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1428{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001429 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001430 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001431 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001432 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (rdev->mddev) {
1435 MD_BUG();
1436 return -EINVAL;
1437 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001438
1439 /* prevent duplicates */
1440 if (find_rdev(mddev, rdev->bdev->bd_dev))
1441 return -EEXIST;
1442
NeilBrown2bf071b2006-01-06 00:20:55 -08001443 /* make sure rdev->size exceeds mddev->size */
1444 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001445 if (mddev->pers) {
1446 /* Cannot change size, so fail
1447 * If mddev->level <= 0, then we don't care
1448 * about aligning sizes (e.g. linear)
1449 */
1450 if (mddev->level > 0)
1451 return -ENOSPC;
1452 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001453 mddev->size = rdev->size;
1454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 /* Verify rdev->desc_nr is unique.
1457 * If it is -1, assign a free number, else
1458 * check number is not in use
1459 */
1460 if (rdev->desc_nr < 0) {
1461 int choice = 0;
1462 if (mddev->pers) choice = mddev->raid_disks;
1463 while (find_rdev_nr(mddev, choice))
1464 choice++;
1465 rdev->desc_nr = choice;
1466 } else {
1467 if (find_rdev_nr(mddev, rdev->desc_nr))
1468 return -EBUSY;
1469 }
NeilBrown19133a42005-11-08 21:39:35 -08001470 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001471 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001472 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001475 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001476
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001477 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001478 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001479
NeilBrownf637b9f2005-11-08 21:39:37 -08001480 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001481 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001482 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001483 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001484 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1485 kobject_del(&rdev->kobj);
1486 goto fail;
1487 }
1488 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001489 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001491
1492 fail:
1493 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1494 b, mdname(mddev));
1495 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
NeilBrown177a99b2008-02-06 01:39:56 -08001498static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001499{
1500 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1501 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001502 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1506{
1507 char b[BDEVNAME_SIZE];
1508 if (!rdev->mddev) {
1509 MD_BUG();
1510 return;
1511 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001512 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 list_del_init(&rdev->same_set);
1514 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1515 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001516 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001517
1518 /* We need to delay this, otherwise we can deadlock when
1519 * writing to 'remove' to "dev/state"
1520 */
NeilBrown177a99b2008-02-06 01:39:56 -08001521 INIT_WORK(&rdev->del_work, md_delayed_delete);
1522 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001523 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/*
1527 * prevent the device from being mounted, repartitioned or
1528 * otherwise reused by a RAID array (or any other kernel
1529 * subsystem), by bd_claiming the device.
1530 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001531static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 int err = 0;
1534 struct block_device *bdev;
1535 char b[BDEVNAME_SIZE];
1536
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001537 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (IS_ERR(bdev)) {
1539 printk(KERN_ERR "md: could not open %s.\n",
1540 __bdevname(dev, b));
1541 return PTR_ERR(bdev);
1542 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001543 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (err) {
1545 printk(KERN_ERR "md: could not bd_claim %s.\n",
1546 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001547 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 return err;
1549 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001550 if (!shared)
1551 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 rdev->bdev = bdev;
1553 return err;
1554}
1555
1556static void unlock_rdev(mdk_rdev_t *rdev)
1557{
1558 struct block_device *bdev = rdev->bdev;
1559 rdev->bdev = NULL;
1560 if (!bdev)
1561 MD_BUG();
1562 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001563 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566void md_autodetect_dev(dev_t dev);
1567
1568static void export_rdev(mdk_rdev_t * rdev)
1569{
1570 char b[BDEVNAME_SIZE];
1571 printk(KERN_INFO "md: export_rdev(%s)\n",
1572 bdevname(rdev->bdev,b));
1573 if (rdev->mddev)
1574 MD_BUG();
1575 free_disk_sb(rdev);
1576 list_del_init(&rdev->same_set);
1577#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001578 if (test_bit(AutoDetected, &rdev->flags))
1579 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580#endif
1581 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001582 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585static void kick_rdev_from_array(mdk_rdev_t * rdev)
1586{
1587 unbind_rdev_from_array(rdev);
1588 export_rdev(rdev);
1589}
1590
1591static void export_array(mddev_t *mddev)
1592{
1593 struct list_head *tmp;
1594 mdk_rdev_t *rdev;
1595
NeilBrownd089c6a2008-02-06 01:39:59 -08001596 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (!rdev->mddev) {
1598 MD_BUG();
1599 continue;
1600 }
1601 kick_rdev_from_array(rdev);
1602 }
1603 if (!list_empty(&mddev->disks))
1604 MD_BUG();
1605 mddev->raid_disks = 0;
1606 mddev->major_version = 0;
1607}
1608
1609static void print_desc(mdp_disk_t *desc)
1610{
1611 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1612 desc->major,desc->minor,desc->raid_disk,desc->state);
1613}
1614
1615static void print_sb(mdp_super_t *sb)
1616{
1617 int i;
1618
1619 printk(KERN_INFO
1620 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1621 sb->major_version, sb->minor_version, sb->patch_version,
1622 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1623 sb->ctime);
1624 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1625 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1626 sb->md_minor, sb->layout, sb->chunk_size);
1627 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1628 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1629 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1630 sb->failed_disks, sb->spare_disks,
1631 sb->sb_csum, (unsigned long)sb->events_lo);
1632
1633 printk(KERN_INFO);
1634 for (i = 0; i < MD_SB_DISKS; i++) {
1635 mdp_disk_t *desc;
1636
1637 desc = sb->disks + i;
1638 if (desc->number || desc->major || desc->minor ||
1639 desc->raid_disk || (desc->state && (desc->state != 4))) {
1640 printk(" D %2d: ", i);
1641 print_desc(desc);
1642 }
1643 }
1644 printk(KERN_INFO "md: THIS: ");
1645 print_desc(&sb->this_disk);
1646
1647}
1648
1649static void print_rdev(mdk_rdev_t *rdev)
1650{
1651 char b[BDEVNAME_SIZE];
1652 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1653 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001654 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1655 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (rdev->sb_loaded) {
1657 printk(KERN_INFO "md: rdev superblock:\n");
1658 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1659 } else
1660 printk(KERN_INFO "md: no rdev superblock!\n");
1661}
1662
Adrian Bunk5e563412006-06-26 00:27:42 -07001663static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 struct list_head *tmp, *tmp2;
1666 mdk_rdev_t *rdev;
1667 mddev_t *mddev;
1668 char b[BDEVNAME_SIZE];
1669
1670 printk("\n");
1671 printk("md: **********************************\n");
1672 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1673 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001674 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
NeilBrown32a76272005-06-21 17:17:14 -07001676 if (mddev->bitmap)
1677 bitmap_print_sb(mddev->bitmap);
1678 else
1679 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001680 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 printk("<%s>", bdevname(rdev->bdev,b));
1682 printk("\n");
1683
NeilBrownd089c6a2008-02-06 01:39:59 -08001684 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 print_rdev(rdev);
1686 }
1687 printk("md: **********************************\n");
1688 printk("\n");
1689}
1690
1691
NeilBrown42543762006-06-26 00:27:57 -07001692static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693{
NeilBrown42543762006-06-26 00:27:57 -07001694 /* Update each superblock (in-memory image), but
1695 * if we are allowed to, skip spares which already
1696 * have the right event counter, or have one earlier
1697 * (which would mean they aren't being marked as dirty
1698 * with the rest of the array)
1699 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 mdk_rdev_t *rdev;
1701 struct list_head *tmp;
1702
NeilBrownd089c6a2008-02-06 01:39:59 -08001703 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001704 if (rdev->sb_events == mddev->events ||
1705 (nospares &&
1706 rdev->raid_disk < 0 &&
1707 (rdev->sb_events&1)==0 &&
1708 rdev->sb_events+1 == mddev->events)) {
1709 /* Don't update this superblock */
1710 rdev->sb_loaded = 2;
1711 } else {
1712 super_types[mddev->major_version].
1713 sync_super(mddev, rdev);
1714 rdev->sb_loaded = 1;
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717}
1718
NeilBrown850b2b42006-10-03 01:15:46 -07001719static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 struct list_head *tmp;
1722 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001723 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001724 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
NeilBrown8377bc82008-04-30 00:52:28 -07001726 if (mddev->external)
1727 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001729 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001730
NeilBrown850b2b42006-10-03 01:15:46 -07001731 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1732 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1733 force_change = 1;
1734 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1735 /* just a clean<-> dirty transition, possibly leave spares alone,
1736 * though if events isn't the right even/odd, we will have to do
1737 * spares after all
1738 */
1739 nospares = 1;
1740 if (force_change)
1741 nospares = 0;
1742 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001743 /* If the array is degraded, then skipping spares is both
1744 * dangerous and fairly pointless.
1745 * Dangerous because a device that was removed from the array
1746 * might have a event_count that still looks up-to-date,
1747 * so it can be re-added without a resync.
1748 * Pointless because if there are any spares to skip,
1749 * then a recovery will happen and soon that array won't
1750 * be degraded any more and the spare can go back to sleep then.
1751 */
NeilBrown850b2b42006-10-03 01:15:46 -07001752 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001753
NeilBrown06d91a52005-06-21 17:17:12 -07001754 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001756
1757 /* If this is just a dirty<->clean transition, and the array is clean
1758 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001759 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001760 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001761 && (mddev->events & 1)
1762 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001763 mddev->events--;
1764 else {
1765 /* otherwise we have to go forward and ... */
1766 mddev->events ++;
1767 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1768 /* .. if the array isn't clean, insist on an odd 'events' */
1769 if ((mddev->events&1)==0) {
1770 mddev->events++;
1771 nospares = 0;
1772 }
1773 } else {
1774 /* otherwise insist on an even 'events' (for clean states) */
1775 if ((mddev->events&1)) {
1776 mddev->events++;
1777 nospares = 0;
1778 }
1779 }
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 if (!mddev->events) {
1783 /*
1784 * oops, this 64-bit counter should never wrap.
1785 * Either we are in around ~1 trillion A.C., assuming
1786 * 1 reboot per second, or we have a bug:
1787 */
1788 MD_BUG();
1789 mddev->events --;
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 /*
1793 * do not write anything to disk if using
1794 * nonpersistent superblocks
1795 */
NeilBrown06d91a52005-06-21 17:17:12 -07001796 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001797 if (!mddev->external)
1798 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1799
NeilBrowna9701a32005-11-08 21:39:34 -08001800 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001801 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001803 }
NeilBrowne6910632008-02-06 01:39:51 -08001804 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001805 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 dprintk(KERN_INFO
1808 "md: updating %s RAID superblock on device (in sync %d)\n",
1809 mdname(mddev),mddev->in_sync);
1810
NeilBrown4ad13662007-07-17 04:06:13 -07001811 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001812 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 char b[BDEVNAME_SIZE];
1814 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001815 if (rdev->sb_loaded != 1)
1816 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001817 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 dprintk("(skipping faulty ");
1819
1820 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001821 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001822 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001823 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001824 rdev->sb_page);
1825 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1826 bdevname(rdev->bdev,b),
1827 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001828 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 } else
1831 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001832 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* only need to write one superblock... */
1834 break;
1835 }
NeilBrowna9701a32005-11-08 21:39:34 -08001836 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001837 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001838
NeilBrowna9701a32005-11-08 21:39:34 -08001839 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001840 if (mddev->in_sync != sync_req ||
1841 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001842 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001843 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001844 goto repeat;
1845 }
NeilBrown850b2b42006-10-03 01:15:46 -07001846 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001847 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001848 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
NeilBrownbce74da2006-01-06 00:20:41 -08001852/* words written to sysfs files may, or my not, be \n terminated.
1853 * We want to accept with case. For this we use cmd_match.
1854 */
1855static int cmd_match(const char *cmd, const char *str)
1856{
1857 /* See if cmd, written into a sysfs file, matches
1858 * str. They must either be the same, or cmd can
1859 * have a trailing newline
1860 */
1861 while (*cmd && *str && *cmd == *str) {
1862 cmd++;
1863 str++;
1864 }
1865 if (*cmd == '\n')
1866 cmd++;
1867 if (*str || *cmd)
1868 return 0;
1869 return 1;
1870}
1871
NeilBrown86e6ffd2005-11-08 21:39:24 -08001872struct rdev_sysfs_entry {
1873 struct attribute attr;
1874 ssize_t (*show)(mdk_rdev_t *, char *);
1875 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1876};
1877
1878static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001879state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001880{
1881 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001882 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001883
NeilBrownb2d444d2005-11-08 21:39:31 -08001884 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001885 len+= sprintf(page+len, "%sfaulty",sep);
1886 sep = ",";
1887 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001888 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001889 len += sprintf(page+len, "%sin_sync",sep);
1890 sep = ",";
1891 }
NeilBrownf6556752006-06-26 00:28:01 -07001892 if (test_bit(WriteMostly, &rdev->flags)) {
1893 len += sprintf(page+len, "%swrite_mostly",sep);
1894 sep = ",";
1895 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001896 if (test_bit(Blocked, &rdev->flags)) {
1897 len += sprintf(page+len, "%sblocked", sep);
1898 sep = ",";
1899 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001900 if (!test_bit(Faulty, &rdev->flags) &&
1901 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001902 len += sprintf(page+len, "%sspare", sep);
1903 sep = ",";
1904 }
1905 return len+sprintf(page+len, "\n");
1906}
1907
NeilBrown45dc2de2006-06-26 00:27:58 -07001908static ssize_t
1909state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1910{
1911 /* can write
1912 * faulty - simulates and error
1913 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001914 * writemostly - sets write_mostly
1915 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001916 * blocked - sets the Blocked flag
1917 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001918 */
1919 int err = -EINVAL;
1920 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1921 md_error(rdev->mddev, rdev);
1922 err = 0;
1923 } else if (cmd_match(buf, "remove")) {
1924 if (rdev->raid_disk >= 0)
1925 err = -EBUSY;
1926 else {
1927 mddev_t *mddev = rdev->mddev;
1928 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001929 if (mddev->pers)
1930 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001931 md_new_event(mddev);
1932 err = 0;
1933 }
NeilBrownf6556752006-06-26 00:28:01 -07001934 } else if (cmd_match(buf, "writemostly")) {
1935 set_bit(WriteMostly, &rdev->flags);
1936 err = 0;
1937 } else if (cmd_match(buf, "-writemostly")) {
1938 clear_bit(WriteMostly, &rdev->flags);
1939 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001940 } else if (cmd_match(buf, "blocked")) {
1941 set_bit(Blocked, &rdev->flags);
1942 err = 0;
1943 } else if (cmd_match(buf, "-blocked")) {
1944 clear_bit(Blocked, &rdev->flags);
1945 wake_up(&rdev->blocked_wait);
1946 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1947 md_wakeup_thread(rdev->mddev->thread);
1948
1949 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001950 }
Neil Brown52664732008-06-28 08:31:44 +10001951 if (!err)
1952 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrown45dc2de2006-06-26 00:27:58 -07001953 return err ? err : len;
1954}
NeilBrown80ca3a42006-07-10 04:44:18 -07001955static struct rdev_sysfs_entry rdev_state =
1956__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001957
1958static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001959errors_show(mdk_rdev_t *rdev, char *page)
1960{
1961 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1962}
1963
1964static ssize_t
1965errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1966{
1967 char *e;
1968 unsigned long n = simple_strtoul(buf, &e, 10);
1969 if (*buf && (*e == 0 || *e == '\n')) {
1970 atomic_set(&rdev->corrected_errors, n);
1971 return len;
1972 }
1973 return -EINVAL;
1974}
1975static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001976__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001977
NeilBrown014236d2006-01-06 00:20:55 -08001978static ssize_t
1979slot_show(mdk_rdev_t *rdev, char *page)
1980{
1981 if (rdev->raid_disk < 0)
1982 return sprintf(page, "none\n");
1983 else
1984 return sprintf(page, "%d\n", rdev->raid_disk);
1985}
1986
1987static ssize_t
1988slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1989{
1990 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001991 int err;
1992 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001993 int slot = simple_strtoul(buf, &e, 10);
1994 if (strncmp(buf, "none", 4)==0)
1995 slot = -1;
1996 else if (e==buf || (*e && *e!= '\n'))
1997 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10001998 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001999 /* Setting 'slot' on an active array requires also
2000 * updating the 'rd%d' link, and communicating
2001 * with the personality with ->hot_*_disk.
2002 * For now we only support removing
2003 * failed/spare devices. This normally happens automatically,
2004 * but not when the metadata is externally managed.
2005 */
NeilBrownc303da62008-02-06 01:39:51 -08002006 if (rdev->raid_disk == -1)
2007 return -EEXIST;
2008 /* personality does all needed checks */
2009 if (rdev->mddev->pers->hot_add_disk == NULL)
2010 return -EINVAL;
2011 err = rdev->mddev->pers->
2012 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2013 if (err)
2014 return err;
2015 sprintf(nm, "rd%d", rdev->raid_disk);
2016 sysfs_remove_link(&rdev->mddev->kobj, nm);
2017 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2018 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002019 } else if (rdev->mddev->pers) {
2020 mdk_rdev_t *rdev2;
2021 struct list_head *tmp;
2022 /* Activating a spare .. or possibly reactivating
2023 * if we every get bitmaps working here.
2024 */
2025
2026 if (rdev->raid_disk != -1)
2027 return -EBUSY;
2028
2029 if (rdev->mddev->pers->hot_add_disk == NULL)
2030 return -EINVAL;
2031
2032 rdev_for_each(rdev2, tmp, rdev->mddev)
2033 if (rdev2->raid_disk == slot)
2034 return -EEXIST;
2035
2036 rdev->raid_disk = slot;
2037 if (test_bit(In_sync, &rdev->flags))
2038 rdev->saved_raid_disk = slot;
2039 else
2040 rdev->saved_raid_disk = -1;
2041 err = rdev->mddev->pers->
2042 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002043 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002044 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002045 return err;
Neil Brown52664732008-06-28 08:31:44 +10002046 } else
2047 sysfs_notify(&rdev->kobj, NULL, "state");
Neil Brown6c2fce22008-06-28 08:31:31 +10002048 sprintf(nm, "rd%d", rdev->raid_disk);
2049 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2050 printk(KERN_WARNING
2051 "md: cannot register "
2052 "%s for %s\n",
2053 nm, mdname(rdev->mddev));
2054
2055 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002056 } else {
2057 if (slot >= rdev->mddev->raid_disks)
2058 return -ENOSPC;
2059 rdev->raid_disk = slot;
2060 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002061 clear_bit(Faulty, &rdev->flags);
2062 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002063 set_bit(In_sync, &rdev->flags);
Neil Brown52664732008-06-28 08:31:44 +10002064 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc303da62008-02-06 01:39:51 -08002065 }
NeilBrown014236d2006-01-06 00:20:55 -08002066 return len;
2067}
2068
2069
2070static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002071__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002072
NeilBrown93c8cad2006-01-06 00:20:56 -08002073static ssize_t
2074offset_show(mdk_rdev_t *rdev, char *page)
2075{
Andrew Morton6961ece2006-01-06 00:20:59 -08002076 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002077}
2078
2079static ssize_t
2080offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2081{
2082 char *e;
2083 unsigned long long offset = simple_strtoull(buf, &e, 10);
2084 if (e==buf || (*e && *e != '\n'))
2085 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002086 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002087 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002088 if (rdev->size && rdev->mddev->external)
2089 /* Must set offset before size, so overlap checks
2090 * can be sane */
2091 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002092 rdev->data_offset = offset;
2093 return len;
2094}
2095
2096static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002097__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002098
NeilBrown83303b62006-01-06 00:21:06 -08002099static ssize_t
2100rdev_size_show(mdk_rdev_t *rdev, char *page)
2101{
2102 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2103}
2104
NeilBrownc5d79ad2008-02-06 01:39:54 -08002105static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2106{
2107 /* check if two start/length pairs overlap */
2108 if (s1+l1 <= s2)
2109 return 0;
2110 if (s2+l2 <= s1)
2111 return 0;
2112 return 1;
2113}
2114
NeilBrown83303b62006-01-06 00:21:06 -08002115static ssize_t
2116rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2117{
2118 char *e;
2119 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002120 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002121 mddev_t *my_mddev = rdev->mddev;
2122
NeilBrown83303b62006-01-06 00:21:06 -08002123 if (e==buf || (*e && *e != '\n'))
2124 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002125 if (my_mddev->pers && rdev->raid_disk >= 0) {
2126 if (rdev->mddev->persistent) {
2127 size = super_types[rdev->mddev->major_version].
2128 rdev_size_change(rdev, size);
2129 if (!size)
2130 return -EBUSY;
2131 } else if (!size) {
2132 size = (rdev->bdev->bd_inode->i_size >> 10);
2133 size -= rdev->data_offset/2;
2134 }
2135 if (size < rdev->mddev->size)
2136 return -EINVAL; /* component must fit device */
2137 }
2138
NeilBrown83303b62006-01-06 00:21:06 -08002139 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002140 if (size > oldsize && rdev->mddev->external) {
2141 /* need to check that all other rdevs with the same ->bdev
2142 * do not overlap. We need to unlock the mddev to avoid
2143 * a deadlock. We have already changed rdev->size, and if
2144 * we have to change it back, we will have the lock again.
2145 */
2146 mddev_t *mddev;
2147 int overlap = 0;
2148 struct list_head *tmp, *tmp2;
2149
NeilBrown27c529b2008-03-04 14:29:33 -08002150 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002151 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002152 mdk_rdev_t *rdev2;
2153
2154 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002155 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002156 if (test_bit(AllReserved, &rdev2->flags) ||
2157 (rdev->bdev == rdev2->bdev &&
2158 rdev != rdev2 &&
2159 overlaps(rdev->data_offset, rdev->size,
2160 rdev2->data_offset, rdev2->size))) {
2161 overlap = 1;
2162 break;
2163 }
2164 mddev_unlock(mddev);
2165 if (overlap) {
2166 mddev_put(mddev);
2167 break;
2168 }
2169 }
NeilBrown27c529b2008-03-04 14:29:33 -08002170 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002171 if (overlap) {
2172 /* Someone else could have slipped in a size
2173 * change here, but doing so is just silly.
2174 * We put oldsize back because we *know* it is
2175 * safe, and trust userspace not to race with
2176 * itself
2177 */
2178 rdev->size = oldsize;
2179 return -EBUSY;
2180 }
2181 }
NeilBrown27c529b2008-03-04 14:29:33 -08002182 if (size < my_mddev->size || my_mddev->size == 0)
2183 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002184 return len;
2185}
2186
2187static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002188__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002189
NeilBrown86e6ffd2005-11-08 21:39:24 -08002190static struct attribute *rdev_default_attrs[] = {
2191 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002192 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002193 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002194 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002195 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002196 NULL,
2197};
2198static ssize_t
2199rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2200{
2201 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2202 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002203 mddev_t *mddev = rdev->mddev;
2204 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002205
2206 if (!entry->show)
2207 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002208
2209 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2210 if (!rv) {
2211 if (rdev->mddev == NULL)
2212 rv = -EBUSY;
2213 else
2214 rv = entry->show(rdev, page);
2215 mddev_unlock(mddev);
2216 }
2217 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002218}
2219
2220static ssize_t
2221rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2222 const char *page, size_t length)
2223{
2224 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2225 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002226 ssize_t rv;
2227 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002228
2229 if (!entry->store)
2230 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002231 if (!capable(CAP_SYS_ADMIN))
2232 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002233 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002234 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002235 if (rdev->mddev == NULL)
2236 rv = -EBUSY;
2237 else
2238 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002239 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002240 }
2241 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002242}
2243
2244static void rdev_free(struct kobject *ko)
2245{
2246 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2247 kfree(rdev);
2248}
2249static struct sysfs_ops rdev_sysfs_ops = {
2250 .show = rdev_attr_show,
2251 .store = rdev_attr_store,
2252};
2253static struct kobj_type rdev_ktype = {
2254 .release = rdev_free,
2255 .sysfs_ops = &rdev_sysfs_ops,
2256 .default_attrs = rdev_default_attrs,
2257};
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259/*
2260 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2261 *
2262 * mark the device faulty if:
2263 *
2264 * - the device is nonexistent (zero size)
2265 * - the device has no valid superblock
2266 *
2267 * a faulty rdev _never_ has rdev->sb set.
2268 */
2269static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2270{
2271 char b[BDEVNAME_SIZE];
2272 int err;
2273 mdk_rdev_t *rdev;
2274 sector_t size;
2275
NeilBrown9ffae0c2006-01-06 00:20:32 -08002276 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (!rdev) {
2278 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2279 return ERR_PTR(-ENOMEM);
2280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 if ((err = alloc_disk_sb(rdev)))
2283 goto abort_free;
2284
NeilBrownc5d79ad2008-02-06 01:39:54 -08002285 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (err)
2287 goto abort_free;
2288
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002289 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002292 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002293 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002294 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002296 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002298 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002299 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2302 if (!size) {
2303 printk(KERN_WARNING
2304 "md: %s has zero or unknown size, marking faulty!\n",
2305 bdevname(rdev->bdev,b));
2306 err = -EINVAL;
2307 goto abort_free;
2308 }
2309
2310 if (super_format >= 0) {
2311 err = super_types[super_format].
2312 load_super(rdev, NULL, super_minor);
2313 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002314 printk(KERN_WARNING
2315 "md: %s does not have a valid v%d.%d "
2316 "superblock, not importing!\n",
2317 bdevname(rdev->bdev,b),
2318 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 goto abort_free;
2320 }
2321 if (err < 0) {
2322 printk(KERN_WARNING
2323 "md: could not read %s's sb, not importing!\n",
2324 bdevname(rdev->bdev,b));
2325 goto abort_free;
2326 }
2327 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002330 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 return rdev;
2333
2334abort_free:
2335 if (rdev->sb_page) {
2336 if (rdev->bdev)
2337 unlock_rdev(rdev);
2338 free_disk_sb(rdev);
2339 }
2340 kfree(rdev);
2341 return ERR_PTR(err);
2342}
2343
2344/*
2345 * Check a full RAID array for plausibility
2346 */
2347
2348
NeilBrowna757e642005-04-16 15:26:42 -07002349static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
2351 int i;
2352 struct list_head *tmp;
2353 mdk_rdev_t *rdev, *freshest;
2354 char b[BDEVNAME_SIZE];
2355
2356 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002357 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 switch (super_types[mddev->major_version].
2359 load_super(rdev, freshest, mddev->minor_version)) {
2360 case 1:
2361 freshest = rdev;
2362 break;
2363 case 0:
2364 break;
2365 default:
2366 printk( KERN_ERR \
2367 "md: fatal superblock inconsistency in %s"
2368 " -- removing from array\n",
2369 bdevname(rdev->bdev,b));
2370 kick_rdev_from_array(rdev);
2371 }
2372
2373
2374 super_types[mddev->major_version].
2375 validate_super(mddev, freshest);
2376
2377 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002378 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (rdev != freshest)
2380 if (super_types[mddev->major_version].
2381 validate_super(mddev, rdev)) {
2382 printk(KERN_WARNING "md: kicking non-fresh %s"
2383 " from array!\n",
2384 bdevname(rdev->bdev,b));
2385 kick_rdev_from_array(rdev);
2386 continue;
2387 }
2388 if (mddev->level == LEVEL_MULTIPATH) {
2389 rdev->desc_nr = i++;
2390 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002391 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002392 } else if (rdev->raid_disk >= mddev->raid_disks) {
2393 rdev->raid_disk = -1;
2394 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 }
2396 }
2397
2398
2399
2400 if (mddev->recovery_cp != MaxSector &&
2401 mddev->level >= 1)
2402 printk(KERN_ERR "md: %s: raid array is not clean"
2403 " -- starting background reconstruction\n",
2404 mdname(mddev));
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406}
2407
NeilBrowneae17012005-11-08 21:39:23 -08002408static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002409safe_delay_show(mddev_t *mddev, char *page)
2410{
2411 int msec = (mddev->safemode_delay*1000)/HZ;
2412 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2413}
2414static ssize_t
2415safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2416{
2417 int scale=1;
2418 int dot=0;
2419 int i;
2420 unsigned long msec;
2421 char buf[30];
2422 char *e;
2423 /* remove a period, and count digits after it */
2424 if (len >= sizeof(buf))
2425 return -EINVAL;
2426 strlcpy(buf, cbuf, len);
2427 buf[len] = 0;
2428 for (i=0; i<len; i++) {
2429 if (dot) {
2430 if (isdigit(buf[i])) {
2431 buf[i-1] = buf[i];
2432 scale *= 10;
2433 }
2434 buf[i] = 0;
2435 } else if (buf[i] == '.') {
2436 dot=1;
2437 buf[i] = 0;
2438 }
2439 }
2440 msec = simple_strtoul(buf, &e, 10);
2441 if (e == buf || (*e && *e != '\n'))
2442 return -EINVAL;
2443 msec = (msec * 1000) / scale;
2444 if (msec == 0)
2445 mddev->safemode_delay = 0;
2446 else {
2447 mddev->safemode_delay = (msec*HZ)/1000;
2448 if (mddev->safemode_delay == 0)
2449 mddev->safemode_delay = 1;
2450 }
2451 return len;
2452}
2453static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002454__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002455
2456static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002457level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002458{
NeilBrown2604b702006-01-06 00:20:36 -08002459 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002460 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002461 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002462 else if (mddev->clevel[0])
2463 return sprintf(page, "%s\n", mddev->clevel);
2464 else if (mddev->level != LEVEL_NONE)
2465 return sprintf(page, "%d\n", mddev->level);
2466 else
2467 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002468}
2469
NeilBrownd9d166c2006-01-06 00:20:51 -08002470static ssize_t
2471level_store(mddev_t *mddev, const char *buf, size_t len)
2472{
NeilBrown20a49ff2008-02-06 01:39:57 -08002473 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002474 if (mddev->pers)
2475 return -EBUSY;
2476 if (len == 0)
2477 return 0;
2478 if (len >= sizeof(mddev->clevel))
2479 return -ENOSPC;
2480 strncpy(mddev->clevel, buf, len);
2481 if (mddev->clevel[len-1] == '\n')
2482 len--;
2483 mddev->clevel[len] = 0;
2484 mddev->level = LEVEL_NONE;
2485 return rv;
2486}
2487
2488static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002489__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002490
NeilBrownd4dbd022006-06-26 00:27:59 -07002491
2492static ssize_t
2493layout_show(mddev_t *mddev, char *page)
2494{
2495 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002496 if (mddev->reshape_position != MaxSector &&
2497 mddev->layout != mddev->new_layout)
2498 return sprintf(page, "%d (%d)\n",
2499 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002500 return sprintf(page, "%d\n", mddev->layout);
2501}
2502
2503static ssize_t
2504layout_store(mddev_t *mddev, const char *buf, size_t len)
2505{
2506 char *e;
2507 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002508
2509 if (!*buf || (*e && *e != '\n'))
2510 return -EINVAL;
2511
NeilBrown08a02ec2007-05-09 02:35:38 -07002512 if (mddev->pers)
2513 return -EBUSY;
2514 if (mddev->reshape_position != MaxSector)
2515 mddev->new_layout = n;
2516 else
2517 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002518 return len;
2519}
2520static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002521__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002522
2523
NeilBrowneae17012005-11-08 21:39:23 -08002524static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002525raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002526{
NeilBrownbb636542005-11-08 21:39:45 -08002527 if (mddev->raid_disks == 0)
2528 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002529 if (mddev->reshape_position != MaxSector &&
2530 mddev->delta_disks != 0)
2531 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2532 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002533 return sprintf(page, "%d\n", mddev->raid_disks);
2534}
2535
NeilBrownda943b992006-01-06 00:20:54 -08002536static int update_raid_disks(mddev_t *mddev, int raid_disks);
2537
2538static ssize_t
2539raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2540{
NeilBrownda943b992006-01-06 00:20:54 -08002541 char *e;
2542 int rv = 0;
2543 unsigned long n = simple_strtoul(buf, &e, 10);
2544
2545 if (!*buf || (*e && *e != '\n'))
2546 return -EINVAL;
2547
2548 if (mddev->pers)
2549 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002550 else if (mddev->reshape_position != MaxSector) {
2551 int olddisks = mddev->raid_disks - mddev->delta_disks;
2552 mddev->delta_disks = n - olddisks;
2553 mddev->raid_disks = n;
2554 } else
NeilBrownda943b992006-01-06 00:20:54 -08002555 mddev->raid_disks = n;
2556 return rv ? rv : len;
2557}
2558static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002559__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002560
NeilBrown24dd4692005-11-08 21:39:26 -08002561static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002562chunk_size_show(mddev_t *mddev, char *page)
2563{
NeilBrown08a02ec2007-05-09 02:35:38 -07002564 if (mddev->reshape_position != MaxSector &&
2565 mddev->chunk_size != mddev->new_chunk)
2566 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2567 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002568 return sprintf(page, "%d\n", mddev->chunk_size);
2569}
2570
2571static ssize_t
2572chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2573{
2574 /* can only set chunk_size if array is not yet active */
2575 char *e;
2576 unsigned long n = simple_strtoul(buf, &e, 10);
2577
NeilBrown3b343802006-01-06 00:20:47 -08002578 if (!*buf || (*e && *e != '\n'))
2579 return -EINVAL;
2580
NeilBrown08a02ec2007-05-09 02:35:38 -07002581 if (mddev->pers)
2582 return -EBUSY;
2583 else if (mddev->reshape_position != MaxSector)
2584 mddev->new_chunk = n;
2585 else
2586 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002587 return len;
2588}
2589static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002590__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002591
NeilBrowna94213b2006-06-26 00:28:00 -07002592static ssize_t
2593resync_start_show(mddev_t *mddev, char *page)
2594{
2595 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2596}
2597
2598static ssize_t
2599resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2600{
NeilBrowna94213b2006-06-26 00:28:00 -07002601 char *e;
2602 unsigned long long n = simple_strtoull(buf, &e, 10);
2603
2604 if (mddev->pers)
2605 return -EBUSY;
2606 if (!*buf || (*e && *e != '\n'))
2607 return -EINVAL;
2608
2609 mddev->recovery_cp = n;
2610 return len;
2611}
2612static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002613__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002614
NeilBrown9e653b62006-06-26 00:27:58 -07002615/*
2616 * The array state can be:
2617 *
2618 * clear
2619 * No devices, no size, no level
2620 * Equivalent to STOP_ARRAY ioctl
2621 * inactive
2622 * May have some settings, but array is not active
2623 * all IO results in error
2624 * When written, doesn't tear down array, but just stops it
2625 * suspended (not supported yet)
2626 * All IO requests will block. The array can be reconfigured.
2627 * Writing this, if accepted, will block until array is quiessent
2628 * readonly
2629 * no resync can happen. no superblocks get written.
2630 * write requests fail
2631 * read-auto
2632 * like readonly, but behaves like 'clean' on a write request.
2633 *
2634 * clean - no pending writes, but otherwise active.
2635 * When written to inactive array, starts without resync
2636 * If a write request arrives then
2637 * if metadata is known, mark 'dirty' and switch to 'active'.
2638 * if not known, block and switch to write-pending
2639 * If written to an active array that has pending writes, then fails.
2640 * active
2641 * fully active: IO and resync can be happening.
2642 * When written to inactive array, starts with resync
2643 *
2644 * write-pending
2645 * clean, but writes are blocked waiting for 'active' to be written.
2646 *
2647 * active-idle
2648 * like active, but no writes have been seen for a while (100msec).
2649 *
2650 */
2651enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2652 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002653static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002654 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2655 "write-pending", "active-idle", NULL };
2656
2657static int match_word(const char *word, char **list)
2658{
2659 int n;
2660 for (n=0; list[n]; n++)
2661 if (cmd_match(word, list[n]))
2662 break;
2663 return n;
2664}
2665
2666static ssize_t
2667array_state_show(mddev_t *mddev, char *page)
2668{
2669 enum array_state st = inactive;
2670
2671 if (mddev->pers)
2672 switch(mddev->ro) {
2673 case 1:
2674 st = readonly;
2675 break;
2676 case 2:
2677 st = read_auto;
2678 break;
2679 case 0:
2680 if (mddev->in_sync)
2681 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002682 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2683 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002684 else if (mddev->safemode)
2685 st = active_idle;
2686 else
2687 st = active;
2688 }
2689 else {
2690 if (list_empty(&mddev->disks) &&
2691 mddev->raid_disks == 0 &&
2692 mddev->size == 0)
2693 st = clear;
2694 else
2695 st = inactive;
2696 }
2697 return sprintf(page, "%s\n", array_states[st]);
2698}
2699
2700static int do_md_stop(mddev_t * mddev, int ro);
2701static int do_md_run(mddev_t * mddev);
2702static int restart_array(mddev_t *mddev);
2703
2704static ssize_t
2705array_state_store(mddev_t *mddev, const char *buf, size_t len)
2706{
2707 int err = -EINVAL;
2708 enum array_state st = match_word(buf, array_states);
2709 switch(st) {
2710 case bad_word:
2711 break;
2712 case clear:
2713 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002714 if (atomic_read(&mddev->active) > 1)
2715 return -EBUSY;
2716 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002717 break;
2718 case inactive:
2719 /* stopping an active array */
2720 if (mddev->pers) {
2721 if (atomic_read(&mddev->active) > 1)
2722 return -EBUSY;
2723 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002724 } else
2725 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002726 break;
2727 case suspended:
2728 break; /* not supported yet */
2729 case readonly:
2730 if (mddev->pers)
2731 err = do_md_stop(mddev, 1);
2732 else {
2733 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002734 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002735 err = do_md_run(mddev);
2736 }
2737 break;
2738 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002739 if (mddev->pers) {
NeilBrown648b6292008-04-30 00:52:30 -07002740 if (mddev->ro != 1)
2741 err = do_md_stop(mddev, 1);
2742 else
2743 err = restart_array(mddev);
2744 if (err == 0) {
2745 mddev->ro = 2;
2746 set_disk_ro(mddev->gendisk, 0);
2747 }
NeilBrown9e653b62006-06-26 00:27:58 -07002748 } else {
2749 mddev->ro = 2;
2750 err = do_md_run(mddev);
2751 }
2752 break;
2753 case clean:
2754 if (mddev->pers) {
2755 restart_array(mddev);
2756 spin_lock_irq(&mddev->write_lock);
2757 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002758 if (mddev->in_sync == 0) {
2759 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002760 if (mddev->safemode == 1)
2761 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002762 if (mddev->persistent)
2763 set_bit(MD_CHANGE_CLEAN,
2764 &mddev->flags);
2765 }
2766 err = 0;
2767 } else
2768 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002769 spin_unlock_irq(&mddev->write_lock);
2770 } else {
2771 mddev->ro = 0;
2772 mddev->recovery_cp = MaxSector;
2773 err = do_md_run(mddev);
2774 }
2775 break;
2776 case active:
2777 if (mddev->pers) {
2778 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002779 if (mddev->external)
2780 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002781 wake_up(&mddev->sb_wait);
2782 err = 0;
2783 } else {
2784 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002785 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002786 err = do_md_run(mddev);
2787 }
2788 break;
2789 case write_pending:
2790 case active_idle:
2791 /* these cannot be set */
2792 break;
2793 }
2794 if (err)
2795 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002796 else {
2797 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown9e653b62006-06-26 00:27:58 -07002798 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002799 }
NeilBrown9e653b62006-06-26 00:27:58 -07002800}
NeilBrown80ca3a42006-07-10 04:44:18 -07002801static struct md_sysfs_entry md_array_state =
2802__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002803
NeilBrown6d7ff7382006-01-06 00:21:16 -08002804static ssize_t
2805null_show(mddev_t *mddev, char *page)
2806{
2807 return -EINVAL;
2808}
2809
2810static ssize_t
2811new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2812{
2813 /* buf must be %d:%d\n? giving major and minor numbers */
2814 /* The new device is added to the array.
2815 * If the array has a persistent superblock, we read the
2816 * superblock to initialise info and check validity.
2817 * Otherwise, only checking done is that in bind_rdev_to_array,
2818 * which mainly checks size.
2819 */
2820 char *e;
2821 int major = simple_strtoul(buf, &e, 10);
2822 int minor;
2823 dev_t dev;
2824 mdk_rdev_t *rdev;
2825 int err;
2826
2827 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2828 return -EINVAL;
2829 minor = simple_strtoul(e+1, &e, 10);
2830 if (*e && *e != '\n')
2831 return -EINVAL;
2832 dev = MKDEV(major, minor);
2833 if (major != MAJOR(dev) ||
2834 minor != MINOR(dev))
2835 return -EOVERFLOW;
2836
2837
2838 if (mddev->persistent) {
2839 rdev = md_import_device(dev, mddev->major_version,
2840 mddev->minor_version);
2841 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2842 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2843 mdk_rdev_t, same_set);
2844 err = super_types[mddev->major_version]
2845 .load_super(rdev, rdev0, mddev->minor_version);
2846 if (err < 0)
2847 goto out;
2848 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002849 } else if (mddev->external)
2850 rdev = md_import_device(dev, -2, -1);
2851 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08002852 rdev = md_import_device(dev, -1, -1);
2853
2854 if (IS_ERR(rdev))
2855 return PTR_ERR(rdev);
2856 err = bind_rdev_to_array(rdev, mddev);
2857 out:
2858 if (err)
2859 export_rdev(rdev);
2860 return err ? err : len;
2861}
2862
2863static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002864__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002865
2866static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002867bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2868{
2869 char *end;
2870 unsigned long chunk, end_chunk;
2871
2872 if (!mddev->bitmap)
2873 goto out;
2874 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2875 while (*buf) {
2876 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2877 if (buf == end) break;
2878 if (*end == '-') { /* range */
2879 buf = end + 1;
2880 end_chunk = simple_strtoul(buf, &end, 0);
2881 if (buf == end) break;
2882 }
2883 if (*end && !isspace(*end)) break;
2884 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2885 buf = end;
2886 while (isspace(*buf)) buf++;
2887 }
2888 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2889out:
2890 return len;
2891}
2892
2893static struct md_sysfs_entry md_bitmap =
2894__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2895
2896static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002897size_show(mddev_t *mddev, char *page)
2898{
2899 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2900}
2901
2902static int update_size(mddev_t *mddev, unsigned long size);
2903
2904static ssize_t
2905size_store(mddev_t *mddev, const char *buf, size_t len)
2906{
2907 /* If array is inactive, we can reduce the component size, but
2908 * not increase it (except from 0).
2909 * If array is active, we can try an on-line resize
2910 */
2911 char *e;
2912 int err = 0;
2913 unsigned long long size = simple_strtoull(buf, &e, 10);
2914 if (!*buf || *buf == '\n' ||
2915 (*e && *e != '\n'))
2916 return -EINVAL;
2917
2918 if (mddev->pers) {
2919 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002920 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002921 } else {
2922 if (mddev->size == 0 ||
2923 mddev->size > size)
2924 mddev->size = size;
2925 else
2926 err = -ENOSPC;
2927 }
2928 return err ? err : len;
2929}
2930
2931static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002932__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002933
NeilBrown8bb93aa2006-01-06 00:20:50 -08002934
2935/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002936 * This is one of
2937 * 'none' for arrays with no metadata (good luck...)
2938 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002939 * or N.M for internally known formats
2940 */
2941static ssize_t
2942metadata_show(mddev_t *mddev, char *page)
2943{
2944 if (mddev->persistent)
2945 return sprintf(page, "%d.%d\n",
2946 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002947 else if (mddev->external)
2948 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002949 else
2950 return sprintf(page, "none\n");
2951}
2952
2953static ssize_t
2954metadata_store(mddev_t *mddev, const char *buf, size_t len)
2955{
2956 int major, minor;
2957 char *e;
2958 if (!list_empty(&mddev->disks))
2959 return -EBUSY;
2960
2961 if (cmd_match(buf, "none")) {
2962 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002963 mddev->external = 0;
2964 mddev->major_version = 0;
2965 mddev->minor_version = 90;
2966 return len;
2967 }
2968 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002969 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002970 if (namelen >= sizeof(mddev->metadata_type))
2971 namelen = sizeof(mddev->metadata_type)-1;
2972 strncpy(mddev->metadata_type, buf+9, namelen);
2973 mddev->metadata_type[namelen] = 0;
2974 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2975 mddev->metadata_type[--namelen] = 0;
2976 mddev->persistent = 0;
2977 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002978 mddev->major_version = 0;
2979 mddev->minor_version = 90;
2980 return len;
2981 }
2982 major = simple_strtoul(buf, &e, 10);
2983 if (e==buf || *e != '.')
2984 return -EINVAL;
2985 buf = e+1;
2986 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002987 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002988 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002989 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002990 return -ENOENT;
2991 mddev->major_version = major;
2992 mddev->minor_version = minor;
2993 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002994 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002995 return len;
2996}
2997
2998static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002999__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003000
NeilBrowna35b0d62006-01-06 00:20:49 -08003001static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003002action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003003{
NeilBrown7eec3142005-11-08 21:39:44 -08003004 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08003005 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003006 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003007 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3008 type = "reshape";
3009 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003010 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3011 type = "resync";
3012 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3013 type = "check";
3014 else
3015 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003016 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003017 type = "recover";
3018 }
3019 return sprintf(page, "%s\n", type);
3020}
3021
3022static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003023action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003024{
NeilBrown7eec3142005-11-08 21:39:44 -08003025 if (!mddev->pers || !mddev->pers->sync_request)
3026 return -EINVAL;
3027
NeilBrownbce74da2006-01-06 00:20:41 -08003028 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003029 if (mddev->sync_thread) {
3030 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3031 md_unregister_thread(mddev->sync_thread);
3032 mddev->sync_thread = NULL;
3033 mddev->recovery = 0;
3034 }
NeilBrown03c902e2006-01-06 00:20:46 -08003035 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3036 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003037 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003038 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003039 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003040 else if (cmd_match(page, "recover")) {
3041 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3042 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3043 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003044 int err;
3045 if (mddev->pers->start_reshape == NULL)
3046 return -EINVAL;
3047 err = mddev->pers->start_reshape(mddev);
3048 if (err)
3049 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003050 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003051 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003052 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003053 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003054 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003055 return -EINVAL;
3056 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3057 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003058 }
NeilBrown03c902e2006-01-06 00:20:46 -08003059 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003060 md_wakeup_thread(mddev->thread);
Neil Brown72a23c22008-06-28 08:31:41 +10003061 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrown24dd4692005-11-08 21:39:26 -08003062 return len;
3063}
3064
NeilBrown9d888832005-11-08 21:39:26 -08003065static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003066mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003067{
3068 return sprintf(page, "%llu\n",
3069 (unsigned long long) mddev->resync_mismatches);
3070}
3071
NeilBrown80ca3a42006-07-10 04:44:18 -07003072static struct md_sysfs_entry md_scan_mode =
3073__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003074
NeilBrown96de1e62005-11-08 21:39:39 -08003075
NeilBrown80ca3a42006-07-10 04:44:18 -07003076static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003077
NeilBrown88202a02006-01-06 00:21:36 -08003078static ssize_t
3079sync_min_show(mddev_t *mddev, char *page)
3080{
3081 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3082 mddev->sync_speed_min ? "local": "system");
3083}
3084
3085static ssize_t
3086sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3087{
3088 int min;
3089 char *e;
3090 if (strncmp(buf, "system", 6)==0) {
3091 mddev->sync_speed_min = 0;
3092 return len;
3093 }
3094 min = simple_strtoul(buf, &e, 10);
3095 if (buf == e || (*e && *e != '\n') || min <= 0)
3096 return -EINVAL;
3097 mddev->sync_speed_min = min;
3098 return len;
3099}
3100
3101static struct md_sysfs_entry md_sync_min =
3102__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3103
3104static ssize_t
3105sync_max_show(mddev_t *mddev, char *page)
3106{
3107 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3108 mddev->sync_speed_max ? "local": "system");
3109}
3110
3111static ssize_t
3112sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3113{
3114 int max;
3115 char *e;
3116 if (strncmp(buf, "system", 6)==0) {
3117 mddev->sync_speed_max = 0;
3118 return len;
3119 }
3120 max = simple_strtoul(buf, &e, 10);
3121 if (buf == e || (*e && *e != '\n') || max <= 0)
3122 return -EINVAL;
3123 mddev->sync_speed_max = max;
3124 return len;
3125}
3126
3127static struct md_sysfs_entry md_sync_max =
3128__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3129
Iustin Popd7f3d292007-10-16 23:30:54 -07003130static ssize_t
3131degraded_show(mddev_t *mddev, char *page)
3132{
3133 return sprintf(page, "%d\n", mddev->degraded);
3134}
3135static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003136
3137static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003138sync_force_parallel_show(mddev_t *mddev, char *page)
3139{
3140 return sprintf(page, "%d\n", mddev->parallel_resync);
3141}
3142
3143static ssize_t
3144sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3145{
3146 long n;
3147
3148 if (strict_strtol(buf, 10, &n))
3149 return -EINVAL;
3150
3151 if (n != 0 && n != 1)
3152 return -EINVAL;
3153
3154 mddev->parallel_resync = n;
3155
3156 if (mddev->sync_thread)
3157 wake_up(&resync_wait);
3158
3159 return len;
3160}
3161
3162/* force parallel resync, even with shared block devices */
3163static struct md_sysfs_entry md_sync_force_parallel =
3164__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3165 sync_force_parallel_show, sync_force_parallel_store);
3166
3167static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003168sync_speed_show(mddev_t *mddev, char *page)
3169{
3170 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07003171 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08003172 dt = ((jiffies - mddev->resync_mark) / HZ);
3173 if (!dt) dt++;
3174 db = resync - (mddev->resync_mark_cnt);
3175 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
3176}
3177
NeilBrown80ca3a42006-07-10 04:44:18 -07003178static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003179
3180static ssize_t
3181sync_completed_show(mddev_t *mddev, char *page)
3182{
3183 unsigned long max_blocks, resync;
3184
3185 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3186 max_blocks = mddev->resync_max_sectors;
3187 else
3188 max_blocks = mddev->size << 1;
3189
3190 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3191 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3192}
3193
NeilBrown80ca3a42006-07-10 04:44:18 -07003194static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003195
NeilBrowne464eaf2006-03-27 01:18:14 -08003196static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003197min_sync_show(mddev_t *mddev, char *page)
3198{
3199 return sprintf(page, "%llu\n",
3200 (unsigned long long)mddev->resync_min);
3201}
3202static ssize_t
3203min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3204{
3205 unsigned long long min;
3206 if (strict_strtoull(buf, 10, &min))
3207 return -EINVAL;
3208 if (min > mddev->resync_max)
3209 return -EINVAL;
3210 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3211 return -EBUSY;
3212
3213 /* Must be a multiple of chunk_size */
3214 if (mddev->chunk_size) {
3215 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3216 return -EINVAL;
3217 }
3218 mddev->resync_min = min;
3219
3220 return len;
3221}
3222
3223static struct md_sysfs_entry md_min_sync =
3224__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3225
3226static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003227max_sync_show(mddev_t *mddev, char *page)
3228{
3229 if (mddev->resync_max == MaxSector)
3230 return sprintf(page, "max\n");
3231 else
3232 return sprintf(page, "%llu\n",
3233 (unsigned long long)mddev->resync_max);
3234}
3235static ssize_t
3236max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3237{
3238 if (strncmp(buf, "max", 3) == 0)
3239 mddev->resync_max = MaxSector;
3240 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003241 unsigned long long max;
3242 if (strict_strtoull(buf, 10, &max))
3243 return -EINVAL;
3244 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003245 return -EINVAL;
3246 if (max < mddev->resync_max &&
3247 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3248 return -EBUSY;
3249
3250 /* Must be a multiple of chunk_size */
3251 if (mddev->chunk_size) {
3252 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3253 return -EINVAL;
3254 }
3255 mddev->resync_max = max;
3256 }
3257 wake_up(&mddev->recovery_wait);
3258 return len;
3259}
3260
3261static struct md_sysfs_entry md_max_sync =
3262__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3263
3264static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003265suspend_lo_show(mddev_t *mddev, char *page)
3266{
3267 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3268}
3269
3270static ssize_t
3271suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3272{
3273 char *e;
3274 unsigned long long new = simple_strtoull(buf, &e, 10);
3275
3276 if (mddev->pers->quiesce == NULL)
3277 return -EINVAL;
3278 if (buf == e || (*e && *e != '\n'))
3279 return -EINVAL;
3280 if (new >= mddev->suspend_hi ||
3281 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3282 mddev->suspend_lo = new;
3283 mddev->pers->quiesce(mddev, 2);
3284 return len;
3285 } else
3286 return -EINVAL;
3287}
3288static struct md_sysfs_entry md_suspend_lo =
3289__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3290
3291
3292static ssize_t
3293suspend_hi_show(mddev_t *mddev, char *page)
3294{
3295 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3296}
3297
3298static ssize_t
3299suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3300{
3301 char *e;
3302 unsigned long long new = simple_strtoull(buf, &e, 10);
3303
3304 if (mddev->pers->quiesce == NULL)
3305 return -EINVAL;
3306 if (buf == e || (*e && *e != '\n'))
3307 return -EINVAL;
3308 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3309 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3310 mddev->suspend_hi = new;
3311 mddev->pers->quiesce(mddev, 1);
3312 mddev->pers->quiesce(mddev, 0);
3313 return len;
3314 } else
3315 return -EINVAL;
3316}
3317static struct md_sysfs_entry md_suspend_hi =
3318__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3319
NeilBrown08a02ec2007-05-09 02:35:38 -07003320static ssize_t
3321reshape_position_show(mddev_t *mddev, char *page)
3322{
3323 if (mddev->reshape_position != MaxSector)
3324 return sprintf(page, "%llu\n",
3325 (unsigned long long)mddev->reshape_position);
3326 strcpy(page, "none\n");
3327 return 5;
3328}
3329
3330static ssize_t
3331reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3332{
3333 char *e;
3334 unsigned long long new = simple_strtoull(buf, &e, 10);
3335 if (mddev->pers)
3336 return -EBUSY;
3337 if (buf == e || (*e && *e != '\n'))
3338 return -EINVAL;
3339 mddev->reshape_position = new;
3340 mddev->delta_disks = 0;
3341 mddev->new_level = mddev->level;
3342 mddev->new_layout = mddev->layout;
3343 mddev->new_chunk = mddev->chunk_size;
3344 return len;
3345}
3346
3347static struct md_sysfs_entry md_reshape_position =
3348__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3349 reshape_position_store);
3350
NeilBrowne464eaf2006-03-27 01:18:14 -08003351
NeilBrowneae17012005-11-08 21:39:23 -08003352static struct attribute *md_default_attrs[] = {
3353 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003354 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003355 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003356 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003357 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003358 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003359 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003360 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003361 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003362 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003363 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003364 NULL,
3365};
3366
3367static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003368 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003369 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003370 &md_sync_min.attr,
3371 &md_sync_max.attr,
3372 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003373 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003374 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003375 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003376 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003377 &md_suspend_lo.attr,
3378 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003379 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003380 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003381 NULL,
3382};
NeilBrown411036f2005-11-08 21:39:40 -08003383static struct attribute_group md_redundancy_group = {
3384 .name = NULL,
3385 .attrs = md_redundancy_attrs,
3386};
3387
NeilBrowneae17012005-11-08 21:39:23 -08003388
3389static ssize_t
3390md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3391{
3392 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3393 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003394 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003395
3396 if (!entry->show)
3397 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003398 rv = mddev_lock(mddev);
3399 if (!rv) {
3400 rv = entry->show(mddev, page);
3401 mddev_unlock(mddev);
3402 }
NeilBrown96de1e62005-11-08 21:39:39 -08003403 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003404}
3405
3406static ssize_t
3407md_attr_store(struct kobject *kobj, struct attribute *attr,
3408 const char *page, size_t length)
3409{
3410 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3411 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003412 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003413
3414 if (!entry->store)
3415 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003416 if (!capable(CAP_SYS_ADMIN))
3417 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003418 rv = mddev_lock(mddev);
3419 if (!rv) {
3420 rv = entry->store(mddev, page, length);
3421 mddev_unlock(mddev);
3422 }
NeilBrown96de1e62005-11-08 21:39:39 -08003423 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003424}
3425
3426static void md_free(struct kobject *ko)
3427{
3428 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3429 kfree(mddev);
3430}
3431
3432static struct sysfs_ops md_sysfs_ops = {
3433 .show = md_attr_show,
3434 .store = md_attr_store,
3435};
3436static struct kobj_type md_ktype = {
3437 .release = md_free,
3438 .sysfs_ops = &md_sysfs_ops,
3439 .default_attrs = md_default_attrs,
3440};
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442int mdp_major = 0;
3443
3444static struct kobject *md_probe(dev_t dev, int *part, void *data)
3445{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003446 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 mddev_t *mddev = mddev_find(dev);
3448 struct gendisk *disk;
3449 int partitioned = (MAJOR(dev) != MD_MAJOR);
3450 int shift = partitioned ? MdpMinorShift : 0;
3451 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003452 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
3454 if (!mddev)
3455 return NULL;
3456
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003457 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003459 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 mddev_put(mddev);
3461 return NULL;
3462 }
3463 disk = alloc_disk(1 << shift);
3464 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003465 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 mddev_put(mddev);
3467 return NULL;
3468 }
3469 disk->major = MAJOR(dev);
3470 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003471 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003473 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 disk->fops = &md_fops;
3476 disk->private_data = mddev;
3477 disk->queue = mddev->queue;
3478 add_disk(disk);
3479 mddev->gendisk = disk;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003480 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003481 "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003482 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003483 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003484 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3485 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003486 else
3487 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 return NULL;
3489}
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491static void md_safemode_timeout(unsigned long data)
3492{
3493 mddev_t *mddev = (mddev_t *) data;
3494
Neil Brown0fd62b82008-06-28 08:31:36 +10003495 if (!atomic_read(&mddev->writes_pending)) {
3496 mddev->safemode = 1;
3497 if (mddev->external)
3498 sysfs_notify(&mddev->kobj, NULL, "array_state");
3499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 md_wakeup_thread(mddev->thread);
3501}
3502
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003503static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505static int do_md_run(mddev_t * mddev)
3506{
NeilBrown2604b702006-01-06 00:20:36 -08003507 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 int chunk_size;
3509 struct list_head *tmp;
3510 mdk_rdev_t *rdev;
3511 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003512 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 char b[BDEVNAME_SIZE];
3514
NeilBrowna757e642005-04-16 15:26:42 -07003515 if (list_empty(&mddev->disks))
3516 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
3519 if (mddev->pers)
3520 return -EBUSY;
3521
3522 /*
3523 * Analyze all RAID superblock(s)
3524 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003525 if (!mddev->raid_disks) {
3526 if (!mddev->persistent)
3527 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003528 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530
3531 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
NeilBrown2604b702006-01-06 00:20:36 -08003533 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 if (chunk_size > MAX_CHUNK_SIZE) {
3535 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3536 chunk_size, MAX_CHUNK_SIZE);
3537 return -EINVAL;
3538 }
3539 /*
3540 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3541 */
3542 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003543 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return -EINVAL;
3545 }
3546 if (chunk_size < PAGE_SIZE) {
3547 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3548 chunk_size, PAGE_SIZE);
3549 return -EINVAL;
3550 }
3551
3552 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003553 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003554 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 continue;
3556 if (rdev->size < chunk_size / 1024) {
3557 printk(KERN_WARNING
3558 "md: Dev %s smaller than chunk_size:"
3559 " %lluk < %dk\n",
3560 bdevname(rdev->bdev,b),
3561 (unsigned long long)rdev->size,
3562 chunk_size / 1024);
3563 return -EINVAL;
3564 }
3565 }
3566 }
3567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003569 if (mddev->level != LEVEL_NONE)
3570 request_module("md-level-%d", mddev->level);
3571 else if (mddev->clevel[0])
3572 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573#endif
3574
3575 /*
3576 * Drop all container device buffers, from now on
3577 * the only valid external interface is through the md
3578 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003580 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003581 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 continue;
3583 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003584 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003585
3586 /* perform some consistency tests on the device.
3587 * We don't want the data to overlap the metadata,
3588 * Internal Bitmap issues has handled elsewhere.
3589 */
3590 if (rdev->data_offset < rdev->sb_offset) {
3591 if (mddev->size &&
3592 rdev->data_offset + mddev->size*2
3593 > rdev->sb_offset*2) {
3594 printk("md: %s: data overlaps metadata\n",
3595 mdname(mddev));
3596 return -EINVAL;
3597 }
3598 } else {
3599 if (rdev->sb_offset*2 + rdev->sb_size/512
3600 > rdev->data_offset) {
3601 printk("md: %s: metadata overlaps data\n",
3602 mdname(mddev));
3603 return -EINVAL;
3604 }
3605 }
Neil Brown52664732008-06-28 08:31:44 +10003606 sysfs_notify(&rdev->kobj, NULL, "state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 }
3608
3609 md_probe(mddev->unit, NULL, NULL);
3610 disk = mddev->gendisk;
3611 if (!disk)
3612 return -ENOMEM;
3613
3614 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003615 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003616 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003618 if (mddev->level != LEVEL_NONE)
3619 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3620 mddev->level);
3621 else
3622 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3623 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 return -EINVAL;
3625 }
NeilBrown2604b702006-01-06 00:20:36 -08003626 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003628 mddev->level = pers->level;
3629 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
NeilBrownf6705572006-03-27 01:18:11 -08003631 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003632 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003633 /* This personality cannot handle reshaping... */
3634 mddev->pers = NULL;
3635 module_put(pers->owner);
3636 return -EINVAL;
3637 }
3638
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003639 if (pers->sync_request) {
3640 /* Warn if this is a potentially silly
3641 * configuration.
3642 */
3643 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3644 mdk_rdev_t *rdev2;
3645 struct list_head *tmp2;
3646 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003647 rdev_for_each(rdev, tmp, mddev) {
3648 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003649 if (rdev < rdev2 &&
3650 rdev->bdev->bd_contains ==
3651 rdev2->bdev->bd_contains) {
3652 printk(KERN_WARNING
3653 "%s: WARNING: %s appears to be"
3654 " on the same physical disk as"
3655 " %s.\n",
3656 mdname(mddev),
3657 bdevname(rdev->bdev,b),
3658 bdevname(rdev2->bdev,b2));
3659 warned = 1;
3660 }
3661 }
3662 }
3663 if (warned)
3664 printk(KERN_WARNING
3665 "True protection against single-disk"
3666 " failure might be compromised.\n");
3667 }
3668
NeilBrown657390d2005-08-26 18:34:16 -07003669 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003671 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003672 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
NeilBrownf91de922005-11-08 21:39:36 -08003674 if (start_readonly)
3675 mddev->ro = 2; /* read-only, but switch on first write */
3676
NeilBrownb15c2e52006-01-06 00:20:16 -08003677 err = mddev->pers->run(mddev);
3678 if (!err && mddev->pers->sync_request) {
3679 err = bitmap_create(mddev);
3680 if (err) {
3681 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3682 mdname(mddev), err);
3683 mddev->pers->stop(mddev);
3684 }
3685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 if (err) {
3687 printk(KERN_ERR "md: pers->run() failed ...\n");
3688 module_put(mddev->pers->owner);
3689 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003690 bitmap_destroy(mddev);
3691 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003693 if (mddev->pers->sync_request) {
3694 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3695 printk(KERN_WARNING
3696 "md: cannot register extra attributes for %s\n",
3697 mdname(mddev));
3698 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003699 mddev->ro = 0;
3700
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 atomic_set(&mddev->writes_pending,0);
3702 mddev->safemode = 0;
3703 mddev->safemode_timer.function = md_safemode_timeout;
3704 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003705 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003707
NeilBrownd089c6a2008-02-06 01:39:59 -08003708 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003709 if (rdev->raid_disk >= 0) {
3710 char nm[20];
3711 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003712 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3713 printk("md: cannot register %s for %s\n",
3714 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3718
NeilBrown850b2b42006-10-03 01:15:46 -07003719 if (mddev->flags)
3720 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 set_capacity(disk, mddev->array_size<<1);
3723
3724 /* If we call blk_queue_make_request here, it will
3725 * re-initialise max_sectors etc which may have been
3726 * refined inside -> run. So just set the bits we need to set.
3727 * Most initialisation happended when we called
3728 * blk_queue_make_request(..., md_fail_request)
3729 * earlier.
3730 */
3731 mddev->queue->queuedata = mddev;
3732 mddev->queue->make_request_fn = mddev->pers->make_request;
3733
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003734 /* If there is a partially-recovered drive we need to
3735 * start recovery here. If we leave it to md_check_recovery,
3736 * it will remove the drives and not do the right thing
3737 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003738 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003739 struct list_head *rtmp;
3740 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003741 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003742 if (rdev->raid_disk >= 0 &&
3743 !test_bit(In_sync, &rdev->flags) &&
3744 !test_bit(Faulty, &rdev->flags))
3745 /* complete an interrupted recovery */
3746 spares++;
3747 if (spares && mddev->pers->sync_request) {
3748 mddev->recovery = 0;
3749 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3750 mddev->sync_thread = md_register_thread(md_do_sync,
3751 mddev,
3752 "%s_resync");
3753 if (!mddev->sync_thread) {
3754 printk(KERN_ERR "%s: could not start resync"
3755 " thread...\n",
3756 mdname(mddev));
3757 /* leave the spares where they are, it shouldn't hurt */
3758 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003759 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003760 }
3761 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003762 md_wakeup_thread(mddev->thread);
3763 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003764
Linus Torvalds44ce62942007-05-09 18:51:36 -07003765 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003766 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003767 sysfs_notify(&mddev->kobj, NULL, "array_state");
Neil Brown72a23c22008-06-28 08:31:41 +10003768 sysfs_notify(&mddev->kobj, NULL, "sync_action");
Neil Browna99ac972008-06-28 08:31:43 +10003769 sysfs_notify(&mddev->kobj, NULL, "degraded");
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003770 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return 0;
3772}
3773
3774static int restart_array(mddev_t *mddev)
3775{
3776 struct gendisk *disk = mddev->gendisk;
3777 int err;
3778
3779 /*
3780 * Complain if it has no devices
3781 */
3782 err = -ENXIO;
3783 if (list_empty(&mddev->disks))
3784 goto out;
3785
3786 if (mddev->pers) {
3787 err = -EBUSY;
3788 if (!mddev->ro)
3789 goto out;
3790
3791 mddev->safemode = 0;
3792 mddev->ro = 0;
3793 set_disk_ro(disk, 0);
3794
3795 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3796 mdname(mddev));
3797 /*
3798 * Kick recovery or resync if necessary
3799 */
3800 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3801 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003802 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 err = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10003804 sysfs_notify(&mddev->kobj, NULL, "array_state");
3805
NeilBrown9e653b62006-06-26 00:27:58 -07003806 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
3809out:
3810 return err;
3811}
3812
NeilBrownacc55e22006-06-26 00:27:47 -07003813/* similar to deny_write_access, but accounts for our holding a reference
3814 * to the file ourselves */
3815static int deny_bitmap_write_access(struct file * file)
3816{
3817 struct inode *inode = file->f_mapping->host;
3818
3819 spin_lock(&inode->i_lock);
3820 if (atomic_read(&inode->i_writecount) > 1) {
3821 spin_unlock(&inode->i_lock);
3822 return -ETXTBSY;
3823 }
3824 atomic_set(&inode->i_writecount, -1);
3825 spin_unlock(&inode->i_lock);
3826
3827 return 0;
3828}
3829
3830static void restore_bitmap_write_access(struct file *file)
3831{
3832 struct inode *inode = file->f_mapping->host;
3833
3834 spin_lock(&inode->i_lock);
3835 atomic_set(&inode->i_writecount, 1);
3836 spin_unlock(&inode->i_lock);
3837}
3838
NeilBrown9e653b62006-06-26 00:27:58 -07003839/* mode:
3840 * 0 - completely stop and dis-assemble array
3841 * 1 - switch to readonly
3842 * 2 - stop but do not disassemble array
3843 */
3844static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845{
3846 int err = 0;
3847 struct gendisk *disk = mddev->gendisk;
3848
3849 if (mddev->pers) {
3850 if (atomic_read(&mddev->active)>2) {
3851 printk("md: %s still in use.\n",mdname(mddev));
3852 return -EBUSY;
3853 }
3854
3855 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003856 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3858 md_unregister_thread(mddev->sync_thread);
3859 mddev->sync_thread = NULL;
3860 }
3861
3862 del_timer_sync(&mddev->safemode_timer);
3863
3864 invalidate_partition(disk, 0);
3865
NeilBrown9e653b62006-06-26 00:27:58 -07003866 switch(mode) {
3867 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003869 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 goto out;
3871 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003872 break;
3873 case 0: /* disassemble */
3874 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003875 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003876 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 if (mddev->ro)
3878 set_disk_ro(disk, 0);
3879 blk_queue_make_request(mddev->queue, md_fail_request);
3880 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003881 mddev->queue->merge_bvec_fn = NULL;
3882 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003883 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003884 if (mddev->pers->sync_request)
3885 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3886
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 module_put(mddev->pers->owner);
3888 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003889 /* tell userspace to handle 'inactive' */
3890 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003891
3892 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003893 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 if (mddev->ro)
3896 mddev->ro = 0;
3897 }
NeilBrown850b2b42006-10-03 01:15:46 -07003898 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 /* mark array as shutdown cleanly */
3900 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003901 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
NeilBrown9e653b62006-06-26 00:27:58 -07003903 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003905 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 }
NeilBrown32a76272005-06-21 17:17:14 -07003907
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 /*
3909 * Free resources if final stop
3910 */
NeilBrown9e653b62006-06-26 00:27:58 -07003911 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003912 mdk_rdev_t *rdev;
3913 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003914
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3916
NeilBrown978f9462006-02-02 14:28:05 -08003917 bitmap_destroy(mddev);
3918 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003919 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003920 fput(mddev->bitmap_file);
3921 mddev->bitmap_file = NULL;
3922 }
3923 mddev->bitmap_offset = 0;
3924
NeilBrownd089c6a2008-02-06 01:39:59 -08003925 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003926 if (rdev->raid_disk >= 0) {
3927 char nm[20];
3928 sprintf(nm, "rd%d", rdev->raid_disk);
3929 sysfs_remove_link(&mddev->kobj, nm);
3930 }
3931
NeilBrown177a99b2008-02-06 01:39:56 -08003932 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003933 flush_scheduled_work();
3934
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 export_array(mddev);
3936
3937 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003938 mddev->size = 0;
3939 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003940 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003941 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003942 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003943 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003944 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003945 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003946 mddev->level = LEVEL_NONE;
3947 mddev->clevel[0] = 0;
3948 mddev->flags = 0;
3949 mddev->ro = 0;
3950 mddev->metadata_type[0] = 0;
3951 mddev->chunk_size = 0;
3952 mddev->ctime = mddev->utime = 0;
3953 mddev->layout = 0;
3954 mddev->max_disks = 0;
3955 mddev->events = 0;
3956 mddev->delta_disks = 0;
3957 mddev->new_level = LEVEL_NONE;
3958 mddev->new_layout = 0;
3959 mddev->new_chunk = 0;
3960 mddev->curr_resync = 0;
3961 mddev->resync_mismatches = 0;
3962 mddev->suspend_lo = mddev->suspend_hi = 0;
3963 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3964 mddev->recovery = 0;
3965 mddev->in_sync = 0;
3966 mddev->changed = 0;
3967 mddev->degraded = 0;
3968 mddev->barriers_work = 0;
3969 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003970
NeilBrowna8a55c32006-06-26 00:27:39 -07003971 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3973 mdname(mddev));
3974 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003975 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003976 sysfs_notify(&mddev->kobj, NULL, "array_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977out:
3978 return err;
3979}
3980
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003981#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982static void autorun_array(mddev_t *mddev)
3983{
3984 mdk_rdev_t *rdev;
3985 struct list_head *tmp;
3986 int err;
3987
NeilBrowna757e642005-04-16 15:26:42 -07003988 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
3991 printk(KERN_INFO "md: running: ");
3992
NeilBrownd089c6a2008-02-06 01:39:59 -08003993 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 char b[BDEVNAME_SIZE];
3995 printk("<%s>", bdevname(rdev->bdev,b));
3996 }
3997 printk("\n");
3998
3999 err = do_md_run (mddev);
4000 if (err) {
4001 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
4002 do_md_stop (mddev, 0);
4003 }
4004}
4005
4006/*
4007 * lets try to run arrays based on all disks that have arrived
4008 * until now. (those are in pending_raid_disks)
4009 *
4010 * the method: pick the first pending disk, collect all disks with
4011 * the same UUID, remove all from the pending list and put them into
4012 * the 'same_array' list. Then order this list based on superblock
4013 * update time (freshest comes first), kick out 'old' disks and
4014 * compare superblocks. If everything's fine then run it.
4015 *
4016 * If "unit" is allocated, then bump its reference count
4017 */
4018static void autorun_devices(int part)
4019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 struct list_head *tmp;
4021 mdk_rdev_t *rdev0, *rdev;
4022 mddev_t *mddev;
4023 char b[BDEVNAME_SIZE];
4024
4025 printk(KERN_INFO "md: autorun ...\n");
4026 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004027 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004029 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 rdev0 = list_entry(pending_raid_disks.next,
4031 mdk_rdev_t, same_set);
4032
4033 printk(KERN_INFO "md: considering %s ...\n",
4034 bdevname(rdev0->bdev,b));
4035 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08004036 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 if (super_90_load(rdev, rdev0, 0) >= 0) {
4038 printk(KERN_INFO "md: adding %s ...\n",
4039 bdevname(rdev->bdev,b));
4040 list_move(&rdev->same_set, &candidates);
4041 }
4042 /*
4043 * now we have a set of devices, with all of them having
4044 * mostly sane superblocks. It's time to allocate the
4045 * mddev.
4046 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004047 if (part) {
4048 dev = MKDEV(mdp_major,
4049 rdev0->preferred_minor << MdpMinorShift);
4050 unit = MINOR(dev) >> MdpMinorShift;
4051 } else {
4052 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4053 unit = MINOR(dev);
4054 }
4055 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4057 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4058 break;
4059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 md_probe(dev, NULL, NULL);
4062 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004063 if (!mddev || !mddev->gendisk) {
4064 if (mddev)
4065 mddev_put(mddev);
4066 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 "md: cannot allocate memory for md drive.\n");
4068 break;
4069 }
4070 if (mddev_lock(mddev))
4071 printk(KERN_WARNING "md: %s locked, cannot run\n",
4072 mdname(mddev));
4073 else if (mddev->raid_disks || mddev->major_version
4074 || !list_empty(&mddev->disks)) {
4075 printk(KERN_WARNING
4076 "md: %s already running, cannot run %s\n",
4077 mdname(mddev), bdevname(rdev0->bdev,b));
4078 mddev_unlock(mddev);
4079 } else {
4080 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004081 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08004082 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 list_del_init(&rdev->same_set);
4084 if (bind_rdev_to_array(rdev, mddev))
4085 export_rdev(rdev);
4086 }
4087 autorun_array(mddev);
4088 mddev_unlock(mddev);
4089 }
4090 /* on success, candidates will be empty, on error
4091 * it won't...
4092 */
NeilBrown73c34432008-02-06 01:39:59 -08004093 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 export_rdev(rdev);
4095 mddev_put(mddev);
4096 }
4097 printk(KERN_INFO "md: ... autorun DONE.\n");
4098}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004099#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101static int get_version(void __user * arg)
4102{
4103 mdu_version_t ver;
4104
4105 ver.major = MD_MAJOR_VERSION;
4106 ver.minor = MD_MINOR_VERSION;
4107 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4108
4109 if (copy_to_user(arg, &ver, sizeof(ver)))
4110 return -EFAULT;
4111
4112 return 0;
4113}
4114
4115static int get_array_info(mddev_t * mddev, void __user * arg)
4116{
4117 mdu_array_info_t info;
4118 int nr,working,active,failed,spare;
4119 mdk_rdev_t *rdev;
4120 struct list_head *tmp;
4121
4122 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004123 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004125 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 failed++;
4127 else {
4128 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004129 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 active++;
4131 else
4132 spare++;
4133 }
4134 }
4135
4136 info.major_version = mddev->major_version;
4137 info.minor_version = mddev->minor_version;
4138 info.patch_version = MD_PATCHLEVEL_VERSION;
4139 info.ctime = mddev->ctime;
4140 info.level = mddev->level;
4141 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004142 if (info.size != mddev->size) /* overflow */
4143 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 info.nr_disks = nr;
4145 info.raid_disks = mddev->raid_disks;
4146 info.md_minor = mddev->md_minor;
4147 info.not_persistent= !mddev->persistent;
4148
4149 info.utime = mddev->utime;
4150 info.state = 0;
4151 if (mddev->in_sync)
4152 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004153 if (mddev->bitmap && mddev->bitmap_offset)
4154 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 info.active_disks = active;
4156 info.working_disks = working;
4157 info.failed_disks = failed;
4158 info.spare_disks = spare;
4159
4160 info.layout = mddev->layout;
4161 info.chunk_size = mddev->chunk_size;
4162
4163 if (copy_to_user(arg, &info, sizeof(info)))
4164 return -EFAULT;
4165
4166 return 0;
4167}
4168
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004169static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004170{
4171 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4172 char *ptr, *buf = NULL;
4173 int err = -ENOMEM;
4174
Dan Williamsb5470dc2008-06-27 21:44:04 -07004175 if (md_allow_write(mddev))
4176 file = kmalloc(sizeof(*file), GFP_NOIO);
4177 else
4178 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004179
NeilBrown32a76272005-06-21 17:17:14 -07004180 if (!file)
4181 goto out;
4182
4183 /* bitmap disabled, zero the first byte and copy out */
4184 if (!mddev->bitmap || !mddev->bitmap->file) {
4185 file->pathname[0] = '\0';
4186 goto copy_out;
4187 }
4188
4189 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4190 if (!buf)
4191 goto out;
4192
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004193 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4194 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004195 goto out;
4196
4197 strcpy(file->pathname, ptr);
4198
4199copy_out:
4200 err = 0;
4201 if (copy_to_user(arg, file, sizeof(*file)))
4202 err = -EFAULT;
4203out:
4204 kfree(buf);
4205 kfree(file);
4206 return err;
4207}
4208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209static int get_disk_info(mddev_t * mddev, void __user * arg)
4210{
4211 mdu_disk_info_t info;
4212 unsigned int nr;
4213 mdk_rdev_t *rdev;
4214
4215 if (copy_from_user(&info, arg, sizeof(info)))
4216 return -EFAULT;
4217
4218 nr = info.number;
4219
4220 rdev = find_rdev_nr(mddev, nr);
4221 if (rdev) {
4222 info.major = MAJOR(rdev->bdev->bd_dev);
4223 info.minor = MINOR(rdev->bdev->bd_dev);
4224 info.raid_disk = rdev->raid_disk;
4225 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004226 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004228 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 info.state |= (1<<MD_DISK_ACTIVE);
4230 info.state |= (1<<MD_DISK_SYNC);
4231 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004232 if (test_bit(WriteMostly, &rdev->flags))
4233 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 } else {
4235 info.major = info.minor = 0;
4236 info.raid_disk = -1;
4237 info.state = (1<<MD_DISK_REMOVED);
4238 }
4239
4240 if (copy_to_user(arg, &info, sizeof(info)))
4241 return -EFAULT;
4242
4243 return 0;
4244}
4245
4246static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4247{
4248 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4249 mdk_rdev_t *rdev;
4250 dev_t dev = MKDEV(info->major,info->minor);
4251
4252 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4253 return -EOVERFLOW;
4254
4255 if (!mddev->raid_disks) {
4256 int err;
4257 /* expecting a device which has a superblock */
4258 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4259 if (IS_ERR(rdev)) {
4260 printk(KERN_WARNING
4261 "md: md_import_device returned %ld\n",
4262 PTR_ERR(rdev));
4263 return PTR_ERR(rdev);
4264 }
4265 if (!list_empty(&mddev->disks)) {
4266 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4267 mdk_rdev_t, same_set);
4268 int err = super_types[mddev->major_version]
4269 .load_super(rdev, rdev0, mddev->minor_version);
4270 if (err < 0) {
4271 printk(KERN_WARNING
4272 "md: %s has different UUID to %s\n",
4273 bdevname(rdev->bdev,b),
4274 bdevname(rdev0->bdev,b2));
4275 export_rdev(rdev);
4276 return -EINVAL;
4277 }
4278 }
4279 err = bind_rdev_to_array(rdev, mddev);
4280 if (err)
4281 export_rdev(rdev);
4282 return err;
4283 }
4284
4285 /*
4286 * add_new_disk can be used once the array is assembled
4287 * to add "hot spares". They must already have a superblock
4288 * written
4289 */
4290 if (mddev->pers) {
4291 int err;
4292 if (!mddev->pers->hot_add_disk) {
4293 printk(KERN_WARNING
4294 "%s: personality does not support diskops!\n",
4295 mdname(mddev));
4296 return -EINVAL;
4297 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004298 if (mddev->persistent)
4299 rdev = md_import_device(dev, mddev->major_version,
4300 mddev->minor_version);
4301 else
4302 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 if (IS_ERR(rdev)) {
4304 printk(KERN_WARNING
4305 "md: md_import_device returned %ld\n",
4306 PTR_ERR(rdev));
4307 return PTR_ERR(rdev);
4308 }
NeilBrown41158c72005-06-21 17:17:25 -07004309 /* set save_raid_disk if appropriate */
4310 if (!mddev->persistent) {
4311 if (info->state & (1<<MD_DISK_SYNC) &&
4312 info->raid_disk < mddev->raid_disks)
4313 rdev->raid_disk = info->raid_disk;
4314 else
4315 rdev->raid_disk = -1;
4316 } else
4317 super_types[mddev->major_version].
4318 validate_super(mddev, rdev);
4319 rdev->saved_raid_disk = rdev->raid_disk;
4320
NeilBrownb2d444d2005-11-08 21:39:31 -08004321 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004322 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4323 set_bit(WriteMostly, &rdev->flags);
4324
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 rdev->raid_disk = -1;
4326 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004327 if (!err && !mddev->pers->hot_remove_disk) {
4328 /* If there is hot_add_disk but no hot_remove_disk
4329 * then added disks for geometry changes,
4330 * and should be added immediately.
4331 */
4332 super_types[mddev->major_version].
4333 validate_super(mddev, rdev);
4334 err = mddev->pers->hot_add_disk(mddev, rdev);
4335 if (err)
4336 unbind_rdev_from_array(rdev);
4337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 if (err)
4339 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004340 else
4341 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc3617772005-06-21 17:17:10 -07004342
NeilBrown17571282006-12-10 02:20:52 -08004343 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004344 if (mddev->degraded)
4345 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004346 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004347 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 return err;
4349 }
4350
4351 /* otherwise, add_new_disk is only allowed
4352 * for major_version==0 superblocks
4353 */
4354 if (mddev->major_version != 0) {
4355 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4356 mdname(mddev));
4357 return -EINVAL;
4358 }
4359
4360 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4361 int err;
4362 rdev = md_import_device (dev, -1, 0);
4363 if (IS_ERR(rdev)) {
4364 printk(KERN_WARNING
4365 "md: error, md_import_device() returned %ld\n",
4366 PTR_ERR(rdev));
4367 return PTR_ERR(rdev);
4368 }
4369 rdev->desc_nr = info->number;
4370 if (info->raid_disk < mddev->raid_disks)
4371 rdev->raid_disk = info->raid_disk;
4372 else
4373 rdev->raid_disk = -1;
4374
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004376 if (info->state & (1<<MD_DISK_SYNC))
4377 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004379 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4380 set_bit(WriteMostly, &rdev->flags);
4381
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 if (!mddev->persistent) {
4383 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4384 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4385 } else
4386 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4387 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4388
NeilBrown2bf071b2006-01-06 00:20:55 -08004389 err = bind_rdev_to_array(rdev, mddev);
4390 if (err) {
4391 export_rdev(rdev);
4392 return err;
4393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 }
4395
4396 return 0;
4397}
4398
4399static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4400{
4401 char b[BDEVNAME_SIZE];
4402 mdk_rdev_t *rdev;
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 rdev = find_rdev(mddev, dev);
4405 if (!rdev)
4406 return -ENXIO;
4407
4408 if (rdev->raid_disk >= 0)
4409 goto busy;
4410
4411 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004412 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004413 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414
4415 return 0;
4416busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004417 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 bdevname(rdev->bdev,b), mdname(mddev));
4419 return -EBUSY;
4420}
4421
4422static int hot_add_disk(mddev_t * mddev, dev_t dev)
4423{
4424 char b[BDEVNAME_SIZE];
4425 int err;
4426 unsigned int size;
4427 mdk_rdev_t *rdev;
4428
4429 if (!mddev->pers)
4430 return -ENODEV;
4431
4432 if (mddev->major_version != 0) {
4433 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4434 " version-0 superblocks.\n",
4435 mdname(mddev));
4436 return -EINVAL;
4437 }
4438 if (!mddev->pers->hot_add_disk) {
4439 printk(KERN_WARNING
4440 "%s: personality does not support diskops!\n",
4441 mdname(mddev));
4442 return -EINVAL;
4443 }
4444
4445 rdev = md_import_device (dev, -1, 0);
4446 if (IS_ERR(rdev)) {
4447 printk(KERN_WARNING
4448 "md: error, md_import_device() returned %ld\n",
4449 PTR_ERR(rdev));
4450 return -EINVAL;
4451 }
4452
4453 if (mddev->persistent)
4454 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4455 else
4456 rdev->sb_offset =
4457 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4458
4459 size = calc_dev_size(rdev, mddev->chunk_size);
4460 rdev->size = size;
4461
NeilBrownb2d444d2005-11-08 21:39:31 -08004462 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 printk(KERN_WARNING
4464 "md: can not hot-add faulty %s disk to %s!\n",
4465 bdevname(rdev->bdev,b), mdname(mddev));
4466 err = -EINVAL;
4467 goto abort_export;
4468 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004469 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004471 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004472 err = bind_rdev_to_array(rdev, mddev);
4473 if (err)
4474 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
4476 /*
4477 * The rest should better be atomic, we can have disk failures
4478 * noticed in interrupt contexts ...
4479 */
4480
4481 if (rdev->desc_nr == mddev->max_disks) {
4482 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4483 mdname(mddev));
4484 err = -EBUSY;
4485 goto abort_unbind_export;
4486 }
4487
4488 rdev->raid_disk = -1;
4489
NeilBrown850b2b42006-10-03 01:15:46 -07004490 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 /*
4493 * Kick recovery, maybe this spare has to be added to the
4494 * array immediately.
4495 */
4496 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4497 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004498 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 return 0;
4500
4501abort_unbind_export:
4502 unbind_rdev_from_array(rdev);
4503
4504abort_export:
4505 export_rdev(rdev);
4506 return err;
4507}
4508
NeilBrown32a76272005-06-21 17:17:14 -07004509static int set_bitmap_file(mddev_t *mddev, int fd)
4510{
4511 int err;
4512
NeilBrown36fa3062005-09-09 16:23:45 -07004513 if (mddev->pers) {
4514 if (!mddev->pers->quiesce)
4515 return -EBUSY;
4516 if (mddev->recovery || mddev->sync_thread)
4517 return -EBUSY;
4518 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004519 }
4520
NeilBrown36fa3062005-09-09 16:23:45 -07004521
4522 if (fd >= 0) {
4523 if (mddev->bitmap)
4524 return -EEXIST; /* cannot add when bitmap is present */
4525 mddev->bitmap_file = fget(fd);
4526
4527 if (mddev->bitmap_file == NULL) {
4528 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4529 mdname(mddev));
4530 return -EBADF;
4531 }
4532
4533 err = deny_bitmap_write_access(mddev->bitmap_file);
4534 if (err) {
4535 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4536 mdname(mddev));
4537 fput(mddev->bitmap_file);
4538 mddev->bitmap_file = NULL;
4539 return err;
4540 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004541 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004542 } else if (mddev->bitmap == NULL)
4543 return -ENOENT; /* cannot remove what isn't there */
4544 err = 0;
4545 if (mddev->pers) {
4546 mddev->pers->quiesce(mddev, 1);
4547 if (fd >= 0)
4548 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004549 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004550 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004551 fd = -1; /* make sure to put the file */
4552 }
NeilBrown36fa3062005-09-09 16:23:45 -07004553 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004554 }
4555 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004556 if (mddev->bitmap_file) {
4557 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004558 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004559 }
NeilBrown36fa3062005-09-09 16:23:45 -07004560 mddev->bitmap_file = NULL;
4561 }
4562
NeilBrown32a76272005-06-21 17:17:14 -07004563 return err;
4564}
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566/*
4567 * set_array_info is used two different ways
4568 * The original usage is when creating a new array.
4569 * In this usage, raid_disks is > 0 and it together with
4570 * level, size, not_persistent,layout,chunksize determine the
4571 * shape of the array.
4572 * This will always create an array with a type-0.90.0 superblock.
4573 * The newer usage is when assembling an array.
4574 * In this case raid_disks will be 0, and the major_version field is
4575 * use to determine which style super-blocks are to be found on the devices.
4576 * The minor and patch _version numbers are also kept incase the
4577 * super_block handler wishes to interpret them.
4578 */
4579static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4580{
4581
4582 if (info->raid_disks == 0) {
4583 /* just setting version number for superblock loading */
4584 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004585 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 super_types[info->major_version].name == NULL) {
4587 /* maybe try to auto-load a module? */
4588 printk(KERN_INFO
4589 "md: superblock version %d not known\n",
4590 info->major_version);
4591 return -EINVAL;
4592 }
4593 mddev->major_version = info->major_version;
4594 mddev->minor_version = info->minor_version;
4595 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004596 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 return 0;
4598 }
4599 mddev->major_version = MD_MAJOR_VERSION;
4600 mddev->minor_version = MD_MINOR_VERSION;
4601 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4602 mddev->ctime = get_seconds();
4603
4604 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004605 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 mddev->size = info->size;
4607 mddev->raid_disks = info->raid_disks;
4608 /* don't set md_minor, it is determined by which /dev/md* was
4609 * openned
4610 */
4611 if (info->state & (1<<MD_SB_CLEAN))
4612 mddev->recovery_cp = MaxSector;
4613 else
4614 mddev->recovery_cp = 0;
4615 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004616 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
4618 mddev->layout = info->layout;
4619 mddev->chunk_size = info->chunk_size;
4620
4621 mddev->max_disks = MD_SB_DISKS;
4622
NeilBrowne6910632008-02-06 01:39:51 -08004623 if (mddev->persistent)
4624 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004625 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626
NeilBrownb2a27032005-11-28 13:44:12 -08004627 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4628 mddev->bitmap_offset = 0;
4629
NeilBrownf6705572006-03-27 01:18:11 -08004630 mddev->reshape_position = MaxSector;
4631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 /*
4633 * Generate a 128 bit UUID
4634 */
4635 get_random_bytes(mddev->uuid, 16);
4636
NeilBrownf6705572006-03-27 01:18:11 -08004637 mddev->new_level = mddev->level;
4638 mddev->new_chunk = mddev->chunk_size;
4639 mddev->new_layout = mddev->layout;
4640 mddev->delta_disks = 0;
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 return 0;
4643}
4644
NeilBrowna35b0d62006-01-06 00:20:49 -08004645static int update_size(mddev_t *mddev, unsigned long size)
4646{
4647 mdk_rdev_t * rdev;
4648 int rv;
4649 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004650 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004651
4652 if (mddev->pers->resize == NULL)
4653 return -EINVAL;
4654 /* The "size" is the amount of each device that is used.
4655 * This can only make sense for arrays with redundancy.
4656 * linear and raid0 always use whatever space is available
4657 * We can only consider changing the size if no resync
4658 * or reconstruction is happening, and if the new size
4659 * is acceptable. It must fit before the sb_offset or,
4660 * if that is <data_offset, it must fit before the
4661 * size of each device.
4662 * If size is zero, we find the largest size that fits.
4663 */
4664 if (mddev->sync_thread)
4665 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004666 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004667 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004668 avail = rdev->size * 2;
4669
NeilBrowna35b0d62006-01-06 00:20:49 -08004670 if (fit && (size == 0 || size > avail/2))
4671 size = avail/2;
4672 if (avail < ((sector_t)size << 1))
4673 return -ENOSPC;
4674 }
4675 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4676 if (!rv) {
4677 struct block_device *bdev;
4678
4679 bdev = bdget_disk(mddev->gendisk, 0);
4680 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004681 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004682 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004683 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004684 bdput(bdev);
4685 }
4686 }
4687 return rv;
4688}
4689
NeilBrownda943b992006-01-06 00:20:54 -08004690static int update_raid_disks(mddev_t *mddev, int raid_disks)
4691{
4692 int rv;
4693 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004694 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004695 return -EINVAL;
4696 if (raid_disks <= 0 ||
4697 raid_disks >= mddev->max_disks)
4698 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004699 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004700 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004701 mddev->delta_disks = raid_disks - mddev->raid_disks;
4702
4703 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004704 return rv;
4705}
4706
4707
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708/*
4709 * update_array_info is used to change the configuration of an
4710 * on-line array.
4711 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4712 * fields in the info are checked against the array.
4713 * Any differences that cannot be handled will cause an error.
4714 * Normally, only one change can be managed at a time.
4715 */
4716static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4717{
4718 int rv = 0;
4719 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004720 int state = 0;
4721
4722 /* calculate expected state,ignoring low bits */
4723 if (mddev->bitmap && mddev->bitmap_offset)
4724 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
4726 if (mddev->major_version != info->major_version ||
4727 mddev->minor_version != info->minor_version ||
4728/* mddev->patch_version != info->patch_version || */
4729 mddev->ctime != info->ctime ||
4730 mddev->level != info->level ||
4731/* mddev->layout != info->layout || */
4732 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004733 mddev->chunk_size != info->chunk_size ||
4734 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4735 ((state^info->state) & 0xfffffe00)
4736 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 return -EINVAL;
4738 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004739 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 if (mddev->raid_disks != info->raid_disks) cnt++;
4741 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004742 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 if (cnt == 0) return 0;
4744 if (cnt > 1) return -EINVAL;
4745
4746 if (mddev->layout != info->layout) {
4747 /* Change layout
4748 * we don't need to do anything at the md level, the
4749 * personality will take care of it all.
4750 */
4751 if (mddev->pers->reconfig == NULL)
4752 return -EINVAL;
4753 else
4754 return mddev->pers->reconfig(mddev, info->layout, -1);
4755 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004756 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004757 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
NeilBrownda943b992006-01-06 00:20:54 -08004759 if (mddev->raid_disks != info->raid_disks)
4760 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761
NeilBrown36fa3062005-09-09 16:23:45 -07004762 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4763 if (mddev->pers->quiesce == NULL)
4764 return -EINVAL;
4765 if (mddev->recovery || mddev->sync_thread)
4766 return -EBUSY;
4767 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4768 /* add the bitmap */
4769 if (mddev->bitmap)
4770 return -EEXIST;
4771 if (mddev->default_bitmap_offset == 0)
4772 return -EINVAL;
4773 mddev->bitmap_offset = mddev->default_bitmap_offset;
4774 mddev->pers->quiesce(mddev, 1);
4775 rv = bitmap_create(mddev);
4776 if (rv)
4777 bitmap_destroy(mddev);
4778 mddev->pers->quiesce(mddev, 0);
4779 } else {
4780 /* remove the bitmap */
4781 if (!mddev->bitmap)
4782 return -ENOENT;
4783 if (mddev->bitmap->file)
4784 return -EINVAL;
4785 mddev->pers->quiesce(mddev, 1);
4786 bitmap_destroy(mddev);
4787 mddev->pers->quiesce(mddev, 0);
4788 mddev->bitmap_offset = 0;
4789 }
4790 }
NeilBrown850b2b42006-10-03 01:15:46 -07004791 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 return rv;
4793}
4794
4795static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4796{
4797 mdk_rdev_t *rdev;
4798
4799 if (mddev->pers == NULL)
4800 return -ENODEV;
4801
4802 rdev = find_rdev(mddev, dev);
4803 if (!rdev)
4804 return -ENODEV;
4805
4806 md_error(mddev, rdev);
4807 return 0;
4808}
4809
Andre Noll2f9618c2008-04-25 18:57:58 +02004810/*
4811 * We have a problem here : there is no easy way to give a CHS
4812 * virtual geometry. We currently pretend that we have a 2 heads
4813 * 4 sectors (with a BIG number of cylinders...). This drives
4814 * dosfs just mad... ;-)
4815 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004816static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4817{
4818 mddev_t *mddev = bdev->bd_disk->private_data;
4819
4820 geo->heads = 2;
4821 geo->sectors = 4;
4822 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4823 return 0;
4824}
4825
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826static int md_ioctl(struct inode *inode, struct file *file,
4827 unsigned int cmd, unsigned long arg)
4828{
4829 int err = 0;
4830 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 mddev_t *mddev = NULL;
4832
4833 if (!capable(CAP_SYS_ADMIN))
4834 return -EACCES;
4835
4836 /*
4837 * Commands dealing with the RAID driver but not any
4838 * particular array:
4839 */
4840 switch (cmd)
4841 {
4842 case RAID_VERSION:
4843 err = get_version(argp);
4844 goto done;
4845
4846 case PRINT_RAID_DEBUG:
4847 err = 0;
4848 md_print_devices();
4849 goto done;
4850
4851#ifndef MODULE
4852 case RAID_AUTORUN:
4853 err = 0;
4854 autostart_arrays(arg);
4855 goto done;
4856#endif
4857 default:;
4858 }
4859
4860 /*
4861 * Commands creating/starting a new array:
4862 */
4863
4864 mddev = inode->i_bdev->bd_disk->private_data;
4865
4866 if (!mddev) {
4867 BUG();
4868 goto abort;
4869 }
4870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 err = mddev_lock(mddev);
4872 if (err) {
4873 printk(KERN_INFO
4874 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4875 err, cmd);
4876 goto abort;
4877 }
4878
4879 switch (cmd)
4880 {
4881 case SET_ARRAY_INFO:
4882 {
4883 mdu_array_info_t info;
4884 if (!arg)
4885 memset(&info, 0, sizeof(info));
4886 else if (copy_from_user(&info, argp, sizeof(info))) {
4887 err = -EFAULT;
4888 goto abort_unlock;
4889 }
4890 if (mddev->pers) {
4891 err = update_array_info(mddev, &info);
4892 if (err) {
4893 printk(KERN_WARNING "md: couldn't update"
4894 " array info. %d\n", err);
4895 goto abort_unlock;
4896 }
4897 goto done_unlock;
4898 }
4899 if (!list_empty(&mddev->disks)) {
4900 printk(KERN_WARNING
4901 "md: array %s already has disks!\n",
4902 mdname(mddev));
4903 err = -EBUSY;
4904 goto abort_unlock;
4905 }
4906 if (mddev->raid_disks) {
4907 printk(KERN_WARNING
4908 "md: array %s already initialised!\n",
4909 mdname(mddev));
4910 err = -EBUSY;
4911 goto abort_unlock;
4912 }
4913 err = set_array_info(mddev, &info);
4914 if (err) {
4915 printk(KERN_WARNING "md: couldn't set"
4916 " array info. %d\n", err);
4917 goto abort_unlock;
4918 }
4919 }
4920 goto done_unlock;
4921
4922 default:;
4923 }
4924
4925 /*
4926 * Commands querying/configuring an existing array:
4927 */
NeilBrown32a76272005-06-21 17:17:14 -07004928 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004929 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004930 if ((!mddev->raid_disks && !mddev->external)
4931 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4932 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4933 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 err = -ENODEV;
4935 goto abort_unlock;
4936 }
4937
4938 /*
4939 * Commands even a read-only array can execute:
4940 */
4941 switch (cmd)
4942 {
4943 case GET_ARRAY_INFO:
4944 err = get_array_info(mddev, argp);
4945 goto done_unlock;
4946
NeilBrown32a76272005-06-21 17:17:14 -07004947 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004948 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004949 goto done_unlock;
4950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 case GET_DISK_INFO:
4952 err = get_disk_info(mddev, argp);
4953 goto done_unlock;
4954
4955 case RESTART_ARRAY_RW:
4956 err = restart_array(mddev);
4957 goto done_unlock;
4958
4959 case STOP_ARRAY:
4960 err = do_md_stop (mddev, 0);
4961 goto done_unlock;
4962
4963 case STOP_ARRAY_RO:
4964 err = do_md_stop (mddev, 1);
4965 goto done_unlock;
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 }
4968
4969 /*
4970 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004971 * superblock, so we do not allow them on read-only arrays.
4972 * However non-MD ioctls (e.g. get-size) will still come through
4973 * here and hit the 'default' below, so only disallow
4974 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 */
Andre Nollbb57fc62008-04-25 19:06:35 +02004976 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004977 if (mddev->ro == 2) {
4978 mddev->ro = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10004979 sysfs_notify(&mddev->kobj, NULL, "array_state");
4980 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4981 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08004982 } else {
4983 err = -EROFS;
4984 goto abort_unlock;
4985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 }
4987
4988 switch (cmd)
4989 {
4990 case ADD_NEW_DISK:
4991 {
4992 mdu_disk_info_t info;
4993 if (copy_from_user(&info, argp, sizeof(info)))
4994 err = -EFAULT;
4995 else
4996 err = add_new_disk(mddev, &info);
4997 goto done_unlock;
4998 }
4999
5000 case HOT_REMOVE_DISK:
5001 err = hot_remove_disk(mddev, new_decode_dev(arg));
5002 goto done_unlock;
5003
5004 case HOT_ADD_DISK:
5005 err = hot_add_disk(mddev, new_decode_dev(arg));
5006 goto done_unlock;
5007
5008 case SET_DISK_FAULTY:
5009 err = set_disk_faulty(mddev, new_decode_dev(arg));
5010 goto done_unlock;
5011
5012 case RUN_ARRAY:
5013 err = do_md_run (mddev);
5014 goto done_unlock;
5015
NeilBrown32a76272005-06-21 17:17:14 -07005016 case SET_BITMAP_FILE:
5017 err = set_bitmap_file(mddev, (int)arg);
5018 goto done_unlock;
5019
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 err = -EINVAL;
5022 goto abort_unlock;
5023 }
5024
5025done_unlock:
5026abort_unlock:
5027 mddev_unlock(mddev);
5028
5029 return err;
5030done:
5031 if (err)
5032 MD_BUG();
5033abort:
5034 return err;
5035}
5036
5037static int md_open(struct inode *inode, struct file *file)
5038{
5039 /*
5040 * Succeed if we can lock the mddev, which confirms that
5041 * it isn't being stopped right now.
5042 */
5043 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5044 int err;
5045
NeilBrownd63a5a72006-12-08 02:36:17 -08005046 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 goto out;
5048
5049 err = 0;
5050 mddev_get(mddev);
5051 mddev_unlock(mddev);
5052
5053 check_disk_change(inode->i_bdev);
5054 out:
5055 return err;
5056}
5057
5058static int md_release(struct inode *inode, struct file * file)
5059{
5060 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5061
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005062 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 mddev_put(mddev);
5064
5065 return 0;
5066}
5067
Linus Torvalds44ce62942007-05-09 18:51:36 -07005068static int md_media_changed(struct gendisk *disk)
5069{
5070 mddev_t *mddev = disk->private_data;
5071
5072 return mddev->changed;
5073}
5074
5075static int md_revalidate(struct gendisk *disk)
5076{
5077 mddev_t *mddev = disk->private_data;
5078
5079 mddev->changed = 0;
5080 return 0;
5081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082static struct block_device_operations md_fops =
5083{
5084 .owner = THIS_MODULE,
5085 .open = md_open,
5086 .release = md_release,
5087 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005088 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005089 .media_changed = md_media_changed,
5090 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091};
5092
Adrian Bunk75c96f82005-05-05 16:16:09 -07005093static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
5095 mdk_thread_t *thread = arg;
5096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 /*
5098 * md_thread is a 'system-thread', it's priority should be very
5099 * high. We avoid resource deadlocks individually in each
5100 * raid personality. (RAID5 does preallocation) We also use RR and
5101 * the very same RT priority as kswapd, thus we will never get
5102 * into a priority inversion deadlock.
5103 *
5104 * we definitely have to have equal or higher priority than
5105 * bdflush, otherwise bdflush will deadlock if there are too
5106 * many dirty RAID5 blocks.
5107 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
NeilBrown6985c432005-10-19 21:23:47 -07005109 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005110 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
NeilBrown93588e22005-11-15 00:09:12 -08005112 /* We need to wait INTERRUPTIBLE so that
5113 * we don't add to the load-average.
5114 * That means we need to be sure no signals are
5115 * pending
5116 */
5117 if (signal_pending(current))
5118 flush_signals(current);
5119
5120 wait_event_interruptible_timeout
5121 (thread->wqueue,
5122 test_bit(THREAD_WAKEUP, &thread->flags)
5123 || kthread_should_stop(),
5124 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
5126 clear_bit(THREAD_WAKEUP, &thread->flags);
5127
NeilBrown787453c2005-11-08 21:39:43 -08005128 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return 0;
5132}
5133
5134void md_wakeup_thread(mdk_thread_t *thread)
5135{
5136 if (thread) {
5137 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5138 set_bit(THREAD_WAKEUP, &thread->flags);
5139 wake_up(&thread->wqueue);
5140 }
5141}
5142
5143mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5144 const char *name)
5145{
5146 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147
NeilBrown9ffae0c2006-01-06 00:20:32 -08005148 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 if (!thread)
5150 return NULL;
5151
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 init_waitqueue_head(&thread->wqueue);
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 thread->run = run;
5155 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005156 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005157 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005158 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 kfree(thread);
5160 return NULL;
5161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return thread;
5163}
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165void md_unregister_thread(mdk_thread_t *thread)
5166{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005167 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005168
5169 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 kfree(thread);
5171}
5172
5173void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5174{
5175 if (!mddev) {
5176 MD_BUG();
5177 return;
5178 }
5179
NeilBrownb2d444d2005-11-08 21:39:31 -08005180 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005182
5183 if (mddev->external)
5184 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5187 mdname(mddev),
5188 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5189 __builtin_return_address(0),__builtin_return_address(1),
5190 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005191*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005192 if (!mddev->pers)
5193 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 if (!mddev->pers->error_handler)
5195 return;
5196 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005197 if (mddev->degraded)
5198 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005199 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5201 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5202 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005203 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204}
5205
5206/* seq_file implementation /proc/mdstat */
5207
5208static void status_unused(struct seq_file *seq)
5209{
5210 int i = 0;
5211 mdk_rdev_t *rdev;
5212 struct list_head *tmp;
5213
5214 seq_printf(seq, "unused devices: ");
5215
NeilBrown73c34432008-02-06 01:39:59 -08005216 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 char b[BDEVNAME_SIZE];
5218 i++;
5219 seq_printf(seq, "%s ",
5220 bdevname(rdev->bdev,b));
5221 }
5222 if (!i)
5223 seq_printf(seq, "<none>");
5224
5225 seq_printf(seq, "\n");
5226}
5227
5228
5229static void status_resync(struct seq_file *seq, mddev_t * mddev)
5230{
NeilBrown4588b422006-03-27 01:18:04 -08005231 sector_t max_blocks, resync, res;
5232 unsigned long dt, db, rt;
5233 int scale;
5234 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
5236 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5237
5238 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5239 max_blocks = mddev->resync_max_sectors >> 1;
5240 else
5241 max_blocks = mddev->size;
5242
5243 /*
5244 * Should not happen.
5245 */
5246 if (!max_blocks) {
5247 MD_BUG();
5248 return;
5249 }
NeilBrown4588b422006-03-27 01:18:04 -08005250 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5251 * in a sector_t, and (max_blocks>>scale) will fit in a
5252 * u32, as those are the requirements for sector_div.
5253 * Thus 'scale' must be at least 10
5254 */
5255 scale = 10;
5256 if (sizeof(sector_t) > sizeof(unsigned long)) {
5257 while ( max_blocks/2 > (1ULL<<(scale+32)))
5258 scale++;
5259 }
5260 res = (resync>>scale)*1000;
5261 sector_div(res, (u32)((max_blocks>>scale)+1));
5262
5263 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005264 {
NeilBrown4588b422006-03-27 01:18:04 -08005265 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 seq_printf(seq, "[");
5267 for (i = 0; i < x; i++)
5268 seq_printf(seq, "=");
5269 seq_printf(seq, ">");
5270 for (i = 0; i < y; i++)
5271 seq_printf(seq, ".");
5272 seq_printf(seq, "] ");
5273 }
NeilBrown4588b422006-03-27 01:18:04 -08005274 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005275 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5276 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005277 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5278 "check" :
5279 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5280 "resync" : "recovery"))),
5281 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005282 (unsigned long long) resync,
5283 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284
5285 /*
5286 * We do not want to overflow, so the order of operands and
5287 * the * 100 / 100 trick are important. We do a +1 to be
5288 * safe against division by zero. We only estimate anyway.
5289 *
5290 * dt: time from mark until now
5291 * db: blocks written from mark until now
5292 * rt: remaining time
5293 */
5294 dt = ((jiffies - mddev->resync_mark) / HZ);
5295 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005296 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5297 - mddev->resync_mark_cnt;
5298 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299
5300 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5301
NeilBrownff4e8d92006-07-10 04:44:16 -07005302 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303}
5304
5305static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5306{
5307 struct list_head *tmp;
5308 loff_t l = *pos;
5309 mddev_t *mddev;
5310
5311 if (l >= 0x10000)
5312 return NULL;
5313 if (!l--)
5314 /* header */
5315 return (void*)1;
5316
5317 spin_lock(&all_mddevs_lock);
5318 list_for_each(tmp,&all_mddevs)
5319 if (!l--) {
5320 mddev = list_entry(tmp, mddev_t, all_mddevs);
5321 mddev_get(mddev);
5322 spin_unlock(&all_mddevs_lock);
5323 return mddev;
5324 }
5325 spin_unlock(&all_mddevs_lock);
5326 if (!l--)
5327 return (void*)2;/* tail */
5328 return NULL;
5329}
5330
5331static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5332{
5333 struct list_head *tmp;
5334 mddev_t *next_mddev, *mddev = v;
5335
5336 ++*pos;
5337 if (v == (void*)2)
5338 return NULL;
5339
5340 spin_lock(&all_mddevs_lock);
5341 if (v == (void*)1)
5342 tmp = all_mddevs.next;
5343 else
5344 tmp = mddev->all_mddevs.next;
5345 if (tmp != &all_mddevs)
5346 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5347 else {
5348 next_mddev = (void*)2;
5349 *pos = 0x10000;
5350 }
5351 spin_unlock(&all_mddevs_lock);
5352
5353 if (v != (void*)1)
5354 mddev_put(mddev);
5355 return next_mddev;
5356
5357}
5358
5359static void md_seq_stop(struct seq_file *seq, void *v)
5360{
5361 mddev_t *mddev = v;
5362
5363 if (mddev && v != (void*)1 && v != (void*)2)
5364 mddev_put(mddev);
5365}
5366
NeilBrownd7603b72006-01-06 00:20:30 -08005367struct mdstat_info {
5368 int event;
5369};
5370
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371static int md_seq_show(struct seq_file *seq, void *v)
5372{
5373 mddev_t *mddev = v;
5374 sector_t size;
5375 struct list_head *tmp2;
5376 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005377 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005378 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
5380 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005381 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 seq_printf(seq, "Personalities : ");
5383 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005384 list_for_each_entry(pers, &pers_list, list)
5385 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
5387 spin_unlock(&pers_lock);
5388 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005389 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 return 0;
5391 }
5392 if (v == (void*)2) {
5393 status_unused(seq);
5394 return 0;
5395 }
5396
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005397 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5401 seq_printf(seq, "%s : %sactive", mdname(mddev),
5402 mddev->pers ? "" : "in");
5403 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005404 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005406 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005407 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 seq_printf(seq, " %s", mddev->pers->name);
5409 }
5410
5411 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005412 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 char b[BDEVNAME_SIZE];
5414 seq_printf(seq, " %s[%d]",
5415 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005416 if (test_bit(WriteMostly, &rdev->flags))
5417 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005418 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 seq_printf(seq, "(F)");
5420 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005421 } else if (rdev->raid_disk < 0)
5422 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 size += rdev->size;
5424 }
5425
5426 if (!list_empty(&mddev->disks)) {
5427 if (mddev->pers)
5428 seq_printf(seq, "\n %llu blocks",
5429 (unsigned long long)mddev->array_size);
5430 else
5431 seq_printf(seq, "\n %llu blocks",
5432 (unsigned long long)size);
5433 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005434 if (mddev->persistent) {
5435 if (mddev->major_version != 0 ||
5436 mddev->minor_version != 90) {
5437 seq_printf(seq," super %d.%d",
5438 mddev->major_version,
5439 mddev->minor_version);
5440 }
NeilBrowne6910632008-02-06 01:39:51 -08005441 } else if (mddev->external)
5442 seq_printf(seq, " super external:%s",
5443 mddev->metadata_type);
5444 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005445 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
5447 if (mddev->pers) {
5448 mddev->pers->status (seq, mddev);
5449 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005450 if (mddev->pers->sync_request) {
5451 if (mddev->curr_resync > 2) {
5452 status_resync (seq, mddev);
5453 seq_printf(seq, "\n ");
5454 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5455 seq_printf(seq, "\tresync=DELAYED\n ");
5456 else if (mddev->recovery_cp < MaxSector)
5457 seq_printf(seq, "\tresync=PENDING\n ");
5458 }
NeilBrown32a76272005-06-21 17:17:14 -07005459 } else
5460 seq_printf(seq, "\n ");
5461
5462 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005463 unsigned long chunk_kb;
5464 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005465 spin_lock_irqsave(&bitmap->lock, flags);
5466 chunk_kb = bitmap->chunksize >> 10;
5467 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5468 "%lu%s chunk",
5469 bitmap->pages - bitmap->missing_pages,
5470 bitmap->pages,
5471 (bitmap->pages - bitmap->missing_pages)
5472 << (PAGE_SHIFT - 10),
5473 chunk_kb ? chunk_kb : bitmap->chunksize,
5474 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005475 if (bitmap->file) {
5476 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005477 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005478 }
NeilBrown78d742d2005-06-21 17:17:15 -07005479
NeilBrown32a76272005-06-21 17:17:14 -07005480 seq_printf(seq, "\n");
5481 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 }
5483
5484 seq_printf(seq, "\n");
5485 }
5486 mddev_unlock(mddev);
5487
5488 return 0;
5489}
5490
5491static struct seq_operations md_seq_ops = {
5492 .start = md_seq_start,
5493 .next = md_seq_next,
5494 .stop = md_seq_stop,
5495 .show = md_seq_show,
5496};
5497
5498static int md_seq_open(struct inode *inode, struct file *file)
5499{
5500 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005501 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5502 if (mi == NULL)
5503 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504
5505 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005506 if (error)
5507 kfree(mi);
5508 else {
5509 struct seq_file *p = file->private_data;
5510 p->private = mi;
5511 mi->event = atomic_read(&md_event_count);
5512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 return error;
5514}
5515
NeilBrownd7603b72006-01-06 00:20:30 -08005516static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5517{
5518 struct seq_file *m = filp->private_data;
5519 struct mdstat_info *mi = m->private;
5520 int mask;
5521
5522 poll_wait(filp, &md_event_waiters, wait);
5523
5524 /* always allow read */
5525 mask = POLLIN | POLLRDNORM;
5526
5527 if (mi->event != atomic_read(&md_event_count))
5528 mask |= POLLERR | POLLPRI;
5529 return mask;
5530}
5531
Arjan van de Venfa027c22007-02-12 00:55:33 -08005532static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005533 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 .open = md_seq_open,
5535 .read = seq_read,
5536 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005537 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005538 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539};
5540
NeilBrown2604b702006-01-06 00:20:36 -08005541int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005544 list_add_tail(&p->list, &pers_list);
5545 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 spin_unlock(&pers_lock);
5547 return 0;
5548}
5549
NeilBrown2604b702006-01-06 00:20:36 -08005550int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551{
NeilBrown2604b702006-01-06 00:20:36 -08005552 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005554 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 spin_unlock(&pers_lock);
5556 return 0;
5557}
5558
5559static int is_mddev_idle(mddev_t *mddev)
5560{
5561 mdk_rdev_t * rdev;
5562 struct list_head *tmp;
5563 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005564 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
5566 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005567 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005569 curr_events = disk_stat_read(disk, sectors[0]) +
5570 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005572 /* sync IO will cause sync_io to increase before the disk_stats
5573 * as sync_io is counted when a request starts, and
5574 * disk_stats is counted when it completes.
5575 * So resync activity will cause curr_events to be smaller than
5576 * when there was no such activity.
5577 * non-sync IO will cause disk_stat to increase without
5578 * increasing sync_io so curr_events will (eventually)
5579 * be larger than it was before. Once it becomes
5580 * substantially larger, the test below will cause
5581 * the array to appear non-idle, and resync will slow
5582 * down.
5583 * If there is a lot of outstanding resync activity when
5584 * we set last_event to curr_events, then all that activity
5585 * completing might cause the array to appear non-idle
5586 * and resync will be slowed down even though there might
5587 * not have been non-resync activity. This will only
5588 * happen once though. 'last_events' will soon reflect
5589 * the state where there is little or no outstanding
5590 * resync requests, and further resync activity will
5591 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005592 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005594 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 rdev->last_events = curr_events;
5596 idle = 0;
5597 }
5598 }
5599 return idle;
5600}
5601
5602void md_done_sync(mddev_t *mddev, int blocks, int ok)
5603{
5604 /* another "blocks" (512byte) blocks have been synced */
5605 atomic_sub(blocks, &mddev->recovery_active);
5606 wake_up(&mddev->recovery_wait);
5607 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005608 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 md_wakeup_thread(mddev->thread);
5610 // stop recovery, signal do_sync ....
5611 }
5612}
5613
5614
NeilBrown06d91a52005-06-21 17:17:12 -07005615/* md_write_start(mddev, bi)
5616 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005617 * in superblock) before writing, schedule a superblock update
5618 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005619 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005620void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
Neil Brown0fd62b82008-06-28 08:31:36 +10005622 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005623 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005624 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005625
NeilBrownf91de922005-11-08 21:39:36 -08005626 BUG_ON(mddev->ro == 1);
5627 if (mddev->ro == 2) {
5628 /* need to switch to read/write */
5629 mddev->ro = 0;
5630 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5631 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005632 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005633 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005634 }
NeilBrown06d91a52005-06-21 17:17:12 -07005635 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005636 if (mddev->safemode == 1)
5637 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005638 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005639 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005640 if (mddev->in_sync) {
5641 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005642 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005643 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005644 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005645 }
NeilBrowna9701a32005-11-08 21:39:34 -08005646 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005647 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005648 if (did_change)
5649 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown09a44cc2008-05-23 13:04:36 -07005650 wait_event(mddev->sb_wait,
5651 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5652 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653}
5654
5655void md_write_end(mddev_t *mddev)
5656{
5657 if (atomic_dec_and_test(&mddev->writes_pending)) {
5658 if (mddev->safemode == 2)
5659 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005660 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5662 }
5663}
5664
NeilBrown2a2275d2007-01-26 00:57:11 -08005665/* md_allow_write(mddev)
5666 * Calling this ensures that the array is marked 'active' so that writes
5667 * may proceed without blocking. It is important to call this before
5668 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5669 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005670 *
5671 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5672 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005673 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005674int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005675{
5676 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005677 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005678 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005679 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005680 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005681 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005682
5683 spin_lock_irq(&mddev->write_lock);
5684 if (mddev->in_sync) {
5685 mddev->in_sync = 0;
5686 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5687 if (mddev->safemode_delay &&
5688 mddev->safemode == 0)
5689 mddev->safemode = 1;
5690 spin_unlock_irq(&mddev->write_lock);
5691 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005692 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown2a2275d2007-01-26 00:57:11 -08005693 } else
5694 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005695
5696 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5697 return -EAGAIN;
5698 else
5699 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005700}
5701EXPORT_SYMBOL_GPL(md_allow_write);
5702
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703#define SYNC_MARKS 10
5704#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005705void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706{
5707 mddev_t *mddev2;
5708 unsigned int currspeed = 0,
5709 window;
NeilBrown57afd892005-06-21 17:17:13 -07005710 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 unsigned long mark[SYNC_MARKS];
5712 sector_t mark_cnt[SYNC_MARKS];
5713 int last_mark,m;
5714 struct list_head *tmp;
5715 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005716 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005717 struct list_head *rtmp;
5718 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005719 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720
5721 /* just incase thread restarts... */
5722 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5723 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005724 if (mddev->ro) /* never try to sync a read-only array */
5725 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726
NeilBrown61df9d92006-10-03 01:15:57 -07005727 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5728 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5729 desc = "data-check";
5730 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5731 desc = "requested-resync";
5732 else
5733 desc = "resync";
5734 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5735 desc = "reshape";
5736 else
5737 desc = "recovery";
5738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 /* we overload curr_resync somewhat here.
5740 * 0 == not engaged in resync at all
5741 * 2 == checking that there is no conflict with another sync
5742 * 1 == like 2, but have yielded to allow conflicting resync to
5743 * commense
5744 * other == active in resync - this many blocks
5745 *
5746 * Before starting a resync we must have set curr_resync to
5747 * 2, and then checked that every "conflicting" array has curr_resync
5748 * less than ours. When we find one that is the same or higher
5749 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5750 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5751 * This will mean we have to start checking from the beginning again.
5752 *
5753 */
5754
5755 do {
5756 mddev->curr_resync = 2;
5757
5758 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005759 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005760 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 goto skip;
5762 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005763 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 if (mddev2 == mddev)
5765 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005766 if (!mddev->parallel_resync
5767 && mddev2->curr_resync
5768 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 DEFINE_WAIT(wq);
5770 if (mddev < mddev2 && mddev->curr_resync == 2) {
5771 /* arbitrarily yield */
5772 mddev->curr_resync = 1;
5773 wake_up(&resync_wait);
5774 }
5775 if (mddev > mddev2 && mddev->curr_resync == 1)
5776 /* no need to wait here, we can wait the next
5777 * time 'round when curr_resync == 2
5778 */
5779 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005780 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5781 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005782 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005783 printk(KERN_INFO "md: delaying %s of %s"
5784 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005786 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 mddev_put(mddev2);
5788 schedule();
5789 finish_wait(&resync_wait, &wq);
5790 goto try_again;
5791 }
5792 finish_wait(&resync_wait, &wq);
5793 }
5794 }
5795 } while (mddev->curr_resync < 2);
5796
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005797 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005798 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005800 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 */
5802 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005803 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005804 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005805 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5806 j = mddev->resync_min;
5807 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005808 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005809
NeilBrownccfcc3c2006-03-27 01:18:09 -08005810 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5811 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005812 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 /* recovery follows the physical size of devices */
5814 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005815 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005816 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005817 if (rdev->raid_disk >= 0 &&
5818 !test_bit(Faulty, &rdev->flags) &&
5819 !test_bit(In_sync, &rdev->flags) &&
5820 rdev->recovery_offset < j)
5821 j = rdev->recovery_offset;
5822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
NeilBrown61df9d92006-10-03 01:15:57 -07005824 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5825 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5826 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005827 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005828 "(but not more than %d KB/sec) for %s.\n",
5829 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
5831 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005832
NeilBrown57afd892005-06-21 17:17:13 -07005833 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005834 for (m = 0; m < SYNC_MARKS; m++) {
5835 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005836 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 }
5838 last_mark = 0;
5839 mddev->resync_mark = mark[last_mark];
5840 mddev->resync_mark_cnt = mark_cnt[last_mark];
5841
5842 /*
5843 * Tune reconstruction:
5844 */
5845 window = 32*(PAGE_SIZE/512);
5846 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5847 window/2,(unsigned long long) max_sectors/2);
5848
5849 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 last_check = 0;
5851
5852 if (j>2) {
5853 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005854 "md: resuming %s of %s from checkpoint.\n",
5855 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 mddev->curr_resync = j;
5857 }
5858
5859 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005860 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
NeilBrown57afd892005-06-21 17:17:13 -07005862 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005863 if (j >= mddev->resync_max) {
5864 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5865 wait_event(mddev->recovery_wait,
5866 mddev->resync_max > j
5867 || kthread_should_stop());
5868 }
5869 if (kthread_should_stop())
5870 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005871 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005872 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005873 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005874 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 goto out;
5876 }
NeilBrown57afd892005-06-21 17:17:13 -07005877
5878 if (!skipped) { /* actual IO requested */
5879 io_sectors += sectors;
5880 atomic_add(sectors, &mddev->recovery_active);
5881 }
5882
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883 j += sectors;
5884 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005885 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005886 if (last_check == 0)
5887 /* this is the earliers that rebuilt will be
5888 * visible in /proc/mdstat
5889 */
5890 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005891
5892 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 continue;
5894
NeilBrown57afd892005-06-21 17:17:13 -07005895 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
NeilBrowndfc70642008-05-23 13:04:39 -07005897 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 break;
5899
5900 repeat:
5901 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5902 /* step marks */
5903 int next = (last_mark+1) % SYNC_MARKS;
5904
5905 mddev->resync_mark = mark[next];
5906 mddev->resync_mark_cnt = mark_cnt[next];
5907 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005908 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 last_mark = next;
5910 }
5911
5912
NeilBrownc6207272008-02-06 01:39:52 -08005913 if (kthread_should_stop())
5914 goto interrupted;
5915
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
5917 /*
5918 * this loop exits only if either when we are slower than
5919 * the 'hard' speed limit, or the system was IO-idle for
5920 * a jiffy.
5921 * the system might be non-idle CPU-wise, but we only care
5922 * about not overloading the IO subsystem. (things like an
5923 * e2fsck being done on the RAID array should execute fast)
5924 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005925 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 cond_resched();
5927
NeilBrown57afd892005-06-21 17:17:13 -07005928 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5929 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930
NeilBrown88202a02006-01-06 00:21:36 -08005931 if (currspeed > speed_min(mddev)) {
5932 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005934 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 goto repeat;
5936 }
5937 }
5938 }
NeilBrown61df9d92006-10-03 01:15:57 -07005939 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940 /*
5941 * this also signals 'finished resyncing' to md_stop
5942 */
5943 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005944 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945
5946 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5947
5948 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005949 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
NeilBrowndfc70642008-05-23 13:04:39 -07005951 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005952 mddev->curr_resync > 2) {
5953 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5954 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5955 if (mddev->curr_resync >= mddev->recovery_cp) {
5956 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005957 "md: checkpointing %s of %s.\n",
5958 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005959 mddev->recovery_cp = mddev->curr_resync;
5960 }
5961 } else
5962 mddev->recovery_cp = MaxSector;
5963 } else {
5964 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5965 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005966 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005967 if (rdev->raid_disk >= 0 &&
5968 !test_bit(Faulty, &rdev->flags) &&
5969 !test_bit(In_sync, &rdev->flags) &&
5970 rdev->recovery_offset < mddev->curr_resync)
5971 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 }
NeilBrown17571282006-12-10 02:20:52 -08005974 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 skip:
5977 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005978 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005979 mddev->resync_max = MaxSector;
5980 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981 wake_up(&resync_wait);
5982 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5983 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005984 return;
5985
5986 interrupted:
5987 /*
5988 * got a signal, exit.
5989 */
5990 printk(KERN_INFO
5991 "md: md_do_sync() got signal ... exiting\n");
5992 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5993 goto out;
5994
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995}
NeilBrown29269552006-03-27 01:18:10 -08005996EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997
5998
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005999static int remove_and_add_spares(mddev_t *mddev)
6000{
6001 mdk_rdev_t *rdev;
6002 struct list_head *rtmp;
6003 int spares = 0;
6004
NeilBrownd089c6a2008-02-06 01:39:59 -08006005 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006006 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006007 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006008 (test_bit(Faulty, &rdev->flags) ||
6009 ! test_bit(In_sync, &rdev->flags)) &&
6010 atomic_read(&rdev->nr_pending)==0) {
6011 if (mddev->pers->hot_remove_disk(
6012 mddev, rdev->raid_disk)==0) {
6013 char nm[20];
6014 sprintf(nm,"rd%d", rdev->raid_disk);
6015 sysfs_remove_link(&mddev->kobj, nm);
6016 rdev->raid_disk = -1;
6017 }
6018 }
6019
6020 if (mddev->degraded) {
NeilBrowndfc70642008-05-23 13:04:39 -07006021 rdev_for_each(rdev, rtmp, mddev) {
6022 if (rdev->raid_disk >= 0 &&
6023 !test_bit(In_sync, &rdev->flags))
6024 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006025 if (rdev->raid_disk < 0
6026 && !test_bit(Faulty, &rdev->flags)) {
6027 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006028 if (mddev->pers->
6029 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006030 char nm[20];
6031 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006032 if (sysfs_create_link(&mddev->kobj,
6033 &rdev->kobj, nm))
6034 printk(KERN_WARNING
6035 "md: cannot register "
6036 "%s for %s\n",
6037 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006038 spares++;
6039 md_new_event(mddev);
6040 } else
6041 break;
6042 }
NeilBrowndfc70642008-05-23 13:04:39 -07006043 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006044 }
6045 return spares;
6046}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047/*
6048 * This routine is regularly called by all per-raid-array threads to
6049 * deal with generic issues like resync and super-block update.
6050 * Raid personalities that don't have a thread (linear/raid0) do not
6051 * need this as they never do any recovery or update the superblock.
6052 *
6053 * It does not do any resync itself, but rather "forks" off other threads
6054 * to do that as needed.
6055 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6056 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006057 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 * and wakeups up this thread which will reap the thread and finish up.
6059 * This thread also removes any faulty devices (with nr_pending == 0).
6060 *
6061 * The overall approach is:
6062 * 1/ if the superblock needs updating, update it.
6063 * 2/ If a recovery thread is running, don't do anything else.
6064 * 3/ If recovery has finished, clean up, possibly marking spares active.
6065 * 4/ If there are any faulty devices, remove them.
6066 * 5/ If array is degraded, try to add spares devices
6067 * 6/ If array has spares or is not in-sync, start a resync thread.
6068 */
6069void md_check_recovery(mddev_t *mddev)
6070{
6071 mdk_rdev_t *rdev;
6072 struct list_head *rtmp;
6073
6074
NeilBrown5f404022005-06-21 17:17:16 -07006075 if (mddev->bitmap)
6076 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
6078 if (mddev->ro)
6079 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006080
6081 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006082 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006083 printk(KERN_INFO "md: %s in immediate safe mode\n",
6084 mdname(mddev));
6085 mddev->safemode = 2;
6086 }
6087 flush_signals(current);
6088 }
6089
Linus Torvalds1da177e2005-04-16 15:20:36 -07006090 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006091 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006093 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006094 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006095 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6096 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 ))
6098 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006099
NeilBrowndf5b89b2006-03-27 01:18:20 -08006100 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006101 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006102
NeilBrown31a59e32008-04-30 00:52:30 -07006103 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006104 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006105 spin_lock_irq(&mddev->write_lock);
6106 if (mddev->safemode &&
6107 !atomic_read(&mddev->writes_pending) &&
6108 !mddev->in_sync &&
6109 mddev->recovery_cp == MaxSector) {
6110 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006111 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006112 if (mddev->persistent)
6113 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6114 }
6115 if (mddev->safemode == 1)
6116 mddev->safemode = 0;
6117 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006118 if (did_change)
6119 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrownfca4d842005-06-21 17:17:11 -07006120 }
NeilBrownfca4d842005-06-21 17:17:11 -07006121
NeilBrown850b2b42006-10-03 01:15:46 -07006122 if (mddev->flags)
6123 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006124
Neil Brown52664732008-06-28 08:31:44 +10006125 rdev_for_each(rdev, rtmp, mddev)
6126 if (test_and_clear_bit(StateChanged, &rdev->flags))
6127 sysfs_notify(&rdev->kobj, NULL, "state");
6128
NeilBrown06d91a52005-06-21 17:17:12 -07006129
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6131 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6132 /* resync/recovery still happening */
6133 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6134 goto unlock;
6135 }
6136 if (mddev->sync_thread) {
6137 /* resync has finished, collect result */
6138 md_unregister_thread(mddev->sync_thread);
6139 mddev->sync_thread = NULL;
NeilBrowndfc70642008-05-23 13:04:39 -07006140 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141 /* success...*/
6142 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006143 if (mddev->pers->spare_active(mddev))
6144 sysfs_notify(&mddev->kobj, NULL,
6145 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 }
NeilBrown850b2b42006-10-03 01:15:46 -07006147 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006148
6149 /* if array is no-longer degraded, then any saved_raid_disk
6150 * information must be scrapped
6151 */
6152 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006153 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006154 rdev->saved_raid_disk = -1;
6155
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156 mddev->recovery = 0;
6157 /* flag recovery needed just to double check */
6158 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006159 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006160 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161 goto unlock;
6162 }
Neil Brown72a23c22008-06-28 08:31:41 +10006163 /* Set RUNNING before clearing NEEDED to avoid
6164 * any transients in the value of "sync_action".
6165 */
6166 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6167 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006168 /* Clear some bits that don't mean anything, but
6169 * might be left set
6170 */
NeilBrown24dd4692005-11-08 21:39:26 -08006171 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6172 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006174 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6175 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 /* no recovery is running.
6177 * remove any failed drives, then
6178 * add spares if possible.
6179 * Spare are also removed and re-added, to allow
6180 * the personality to fail the re-add.
6181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006183 if (mddev->reshape_position != MaxSector) {
6184 if (mddev->pers->check_reshape(mddev) != 0)
6185 /* Cannot proceed */
6186 goto unlock;
6187 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006188 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006189 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006190 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6191 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006192 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006193 } else if (mddev->recovery_cp < MaxSector) {
6194 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006195 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006196 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6197 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006199
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006201 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6202 /* We are adding a device or devices to an array
6203 * which has the bitmap stored on all devices.
6204 * So make sure all bitmap pages get written
6205 */
6206 bitmap_write_all(mddev->bitmap);
6207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 mddev->sync_thread = md_register_thread(md_do_sync,
6209 mddev,
6210 "%s_resync");
6211 if (!mddev->sync_thread) {
6212 printk(KERN_ERR "%s: could not start resync"
6213 " thread...\n",
6214 mdname(mddev));
6215 /* leave the spares where they are, it shouldn't hurt */
6216 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006217 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 md_wakeup_thread(mddev->sync_thread);
Neil Brown72a23c22008-06-28 08:31:41 +10006219 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006220 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221 }
6222 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006223 if (!mddev->sync_thread) {
6224 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6225 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6226 &mddev->recovery))
6227 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 mddev_unlock(mddev);
6230 }
6231}
6232
Dan Williams6bfe0b42008-04-30 00:52:32 -07006233void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6234{
6235 sysfs_notify(&rdev->kobj, NULL, "state");
6236 wait_event_timeout(rdev->blocked_wait,
6237 !test_bit(Blocked, &rdev->flags),
6238 msecs_to_jiffies(5000));
6239 rdev_dec_pending(rdev, mddev);
6240}
6241EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6242
Adrian Bunk75c96f82005-05-05 16:16:09 -07006243static int md_notify_reboot(struct notifier_block *this,
6244 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245{
6246 struct list_head *tmp;
6247 mddev_t *mddev;
6248
6249 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6250
6251 printk(KERN_INFO "md: stopping all md devices.\n");
6252
NeilBrown29ac4aa2008-02-06 01:39:58 -08006253 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006254 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10006256 mddev_unlock(mddev);
6257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 /*
6259 * certain more exotic SCSI devices are known to be
6260 * volatile wrt too early system reboots. While the
6261 * right place to handle this issue is the given
6262 * driver, we do want to have a safe RAID driver ...
6263 */
6264 mdelay(1000*1);
6265 }
6266 return NOTIFY_DONE;
6267}
6268
Adrian Bunk75c96f82005-05-05 16:16:09 -07006269static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 .notifier_call = md_notify_reboot,
6271 .next = NULL,
6272 .priority = INT_MAX, /* before any real devices */
6273};
6274
6275static void md_geninit(void)
6276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6278
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006279 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
Adrian Bunk75c96f82005-05-05 16:16:09 -07006282static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 if (register_blkdev(MAJOR_NR, "md"))
6285 return -1;
6286 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6287 unregister_blkdev(MAJOR_NR, "md");
6288 return -1;
6289 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006290 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6291 md_probe, NULL, NULL);
6292 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 md_probe, NULL, NULL);
6294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006296 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297
6298 md_geninit();
6299 return (0);
6300}
6301
6302
6303#ifndef MODULE
6304
6305/*
6306 * Searches all registered partitions for autorun RAID arrays
6307 * at boot time.
6308 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006309
6310static LIST_HEAD(all_detected_devices);
6311struct detected_devices_node {
6312 struct list_head list;
6313 dev_t dev;
6314};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
6316void md_autodetect_dev(dev_t dev)
6317{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006318 struct detected_devices_node *node_detected_dev;
6319
6320 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6321 if (node_detected_dev) {
6322 node_detected_dev->dev = dev;
6323 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6324 } else {
6325 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6326 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328}
6329
6330
6331static void autostart_arrays(int part)
6332{
6333 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006334 struct detected_devices_node *node_detected_dev;
6335 dev_t dev;
6336 int i_scanned, i_passed;
6337
6338 i_scanned = 0;
6339 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
6341 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6342
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006343 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6344 i_scanned++;
6345 node_detected_dev = list_entry(all_detected_devices.next,
6346 struct detected_devices_node, list);
6347 list_del(&node_detected_dev->list);
6348 dev = node_detected_dev->dev;
6349 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006350 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 if (IS_ERR(rdev))
6352 continue;
6353
NeilBrownb2d444d2005-11-08 21:39:31 -08006354 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 MD_BUG();
6356 continue;
6357 }
NeilBrownd0fae182008-03-04 14:29:31 -08006358 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006360 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006362
6363 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6364 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
6366 autorun_devices(part);
6367}
6368
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006369#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370
6371static __exit void md_exit(void)
6372{
6373 mddev_t *mddev;
6374 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006375
NeilBrowne8703fe2006-10-03 01:15:59 -07006376 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6377 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
6379 unregister_blkdev(MAJOR_NR,"md");
6380 unregister_blkdev(mdp_major, "mdp");
6381 unregister_reboot_notifier(&md_notifier);
6382 unregister_sysctl_table(raid_table_header);
6383 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006384 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 struct gendisk *disk = mddev->gendisk;
6386 if (!disk)
6387 continue;
6388 export_array(mddev);
6389 del_gendisk(disk);
6390 put_disk(disk);
6391 mddev->gendisk = NULL;
6392 mddev_put(mddev);
6393 }
6394}
6395
Dan Williams685784a2007-07-09 11:56:42 -07006396subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397module_exit(md_exit)
6398
NeilBrownf91de922005-11-08 21:39:36 -08006399static int get_ro(char *buffer, struct kernel_param *kp)
6400{
6401 return sprintf(buffer, "%d", start_readonly);
6402}
6403static int set_ro(const char *val, struct kernel_param *kp)
6404{
6405 char *e;
6406 int num = simple_strtoul(val, &e, 10);
6407 if (*val && (*e == '\0' || *e == '\n')) {
6408 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006409 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006410 }
6411 return -EINVAL;
6412}
6413
NeilBrown80ca3a42006-07-10 04:44:18 -07006414module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6415module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006416
NeilBrownf91de922005-11-08 21:39:36 -08006417
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418EXPORT_SYMBOL(register_md_personality);
6419EXPORT_SYMBOL(unregister_md_personality);
6420EXPORT_SYMBOL(md_error);
6421EXPORT_SYMBOL(md_done_sync);
6422EXPORT_SYMBOL(md_write_start);
6423EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006424EXPORT_SYMBOL(md_register_thread);
6425EXPORT_SYMBOL(md_unregister_thread);
6426EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427EXPORT_SYMBOL(md_check_recovery);
6428MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006429MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006430MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);