blob: 60d4cad88c208ee7a5c24a288b01d479b9ec76e6 [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 {
661 char *name;
662 struct module *owner;
663 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
664 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
665 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
666};
667
668/*
669 * load_super for 0.90.0
670 */
671static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
672{
673 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
674 mdp_super_t *sb;
675 int ret;
676 sector_t sb_offset;
677
678 /*
679 * Calculate the position of the superblock,
680 * it's at the end of the disk.
681 *
682 * It also happens to be a multiple of 4Kb.
683 */
684 sb_offset = calc_dev_sboffset(rdev->bdev);
685 rdev->sb_offset = sb_offset;
686
NeilBrown0002b272005-09-09 16:23:53 -0700687 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (ret) return ret;
689
690 ret = -EINVAL;
691
692 bdevname(rdev->bdev, b);
693 sb = (mdp_super_t*)page_address(rdev->sb_page);
694
695 if (sb->md_magic != MD_SB_MAGIC) {
696 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
697 b);
698 goto abort;
699 }
700
701 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800702 sb->minor_version < 90 ||
703 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
705 sb->major_version, sb->minor_version,
706 b);
707 goto abort;
708 }
709
710 if (sb->raid_disks <= 0)
711 goto abort;
712
NeilBrown4d167f02007-05-09 02:35:37 -0700713 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
715 b);
716 goto abort;
717 }
718
719 rdev->preferred_minor = sb->md_minor;
720 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700721 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
NeilBrowne11e93f2007-05-09 02:35:36 -0700723 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
724 if (sb->level != 1 && sb->level != 4
725 && sb->level != 5 && sb->level != 6
726 && sb->level != 10) {
727 /* FIXME use a better test */
728 printk(KERN_WARNING
729 "md: bitmaps not supported for this level.\n");
730 goto abort;
731 }
732 }
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (sb->level == LEVEL_MULTIPATH)
735 rdev->desc_nr = -1;
736 else
737 rdev->desc_nr = sb->this_disk.number;
738
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700739 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700741 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 __u64 ev1, ev2;
743 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
744 if (!uuid_equal(refsb, sb)) {
745 printk(KERN_WARNING "md: %s has different UUID to %s\n",
746 b, bdevname(refdev->bdev,b2));
747 goto abort;
748 }
749 if (!sb_equal(refsb, sb)) {
750 printk(KERN_WARNING "md: %s has same UUID"
751 " but different superblock to %s\n",
752 b, bdevname(refdev->bdev, b2));
753 goto abort;
754 }
755 ev1 = md_event(sb);
756 ev2 = md_event(refsb);
757 if (ev1 > ev2)
758 ret = 1;
759 else
760 ret = 0;
761 }
762 rdev->size = calc_dev_size(rdev, sb->chunk_size);
763
NeilBrown2bf071b2006-01-06 00:20:55 -0800764 if (rdev->size < sb->size && sb->level > 1)
765 /* "this cannot possibly happen" ... */
766 ret = -EINVAL;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 abort:
769 return ret;
770}
771
772/*
773 * validate_super for 0.90.0
774 */
775static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
776{
777 mdp_disk_t *desc;
778 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700779 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
NeilBrown41158c72005-06-21 17:17:25 -0700781 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800782 clear_bit(Faulty, &rdev->flags);
783 clear_bit(In_sync, &rdev->flags);
784 clear_bit(WriteMostly, &rdev->flags);
785 clear_bit(BarriersNotsupp, &rdev->flags);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (mddev->raid_disks == 0) {
788 mddev->major_version = 0;
789 mddev->minor_version = sb->minor_version;
790 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800791 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 mddev->chunk_size = sb->chunk_size;
793 mddev->ctime = sb->ctime;
794 mddev->utime = sb->utime;
795 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800796 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 mddev->layout = sb->layout;
798 mddev->raid_disks = sb->raid_disks;
799 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700800 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700801 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700802 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
NeilBrownf6705572006-03-27 01:18:11 -0800804 if (mddev->minor_version >= 91) {
805 mddev->reshape_position = sb->reshape_position;
806 mddev->delta_disks = sb->delta_disks;
807 mddev->new_level = sb->new_level;
808 mddev->new_layout = sb->new_layout;
809 mddev->new_chunk = sb->new_chunk;
810 } else {
811 mddev->reshape_position = MaxSector;
812 mddev->delta_disks = 0;
813 mddev->new_level = mddev->level;
814 mddev->new_layout = mddev->layout;
815 mddev->new_chunk = mddev->chunk_size;
816 }
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 if (sb->state & (1<<MD_SB_CLEAN))
819 mddev->recovery_cp = MaxSector;
820 else {
821 if (sb->events_hi == sb->cp_events_hi &&
822 sb->events_lo == sb->cp_events_lo) {
823 mddev->recovery_cp = sb->recovery_cp;
824 } else
825 mddev->recovery_cp = 0;
826 }
827
828 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
829 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
830 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
831 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
832
833 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700834
835 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700836 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700837 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700838
NeilBrown41158c72005-06-21 17:17:25 -0700839 } else if (mddev->pers == NULL) {
840 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 ++ev1;
842 if (ev1 < mddev->events)
843 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700844 } else if (mddev->bitmap) {
845 /* if adding to array with a bitmap, then we can accept an
846 * older device ... but not too old.
847 */
NeilBrown41158c72005-06-21 17:17:25 -0700848 if (ev1 < mddev->bitmap->events_cleared)
849 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700850 } else {
851 if (ev1 < mddev->events)
852 /* just a hot-add of a new device, leave raid_disk at -1 */
853 return 0;
854 }
NeilBrown41158c72005-06-21 17:17:25 -0700855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 desc = sb->disks + rdev->desc_nr;
858
859 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800860 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700861 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
862 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800863 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 rdev->raid_disk = desc->raid_disk;
865 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700866 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
867 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700868 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800869 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return 0;
871}
872
873/*
874 * sync_super for 0.90.0
875 */
876static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
877{
878 mdp_super_t *sb;
879 struct list_head *tmp;
880 mdk_rdev_t *rdev2;
881 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /* make rdev->sb match mddev data..
885 *
886 * 1/ zero out disks
887 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
888 * 3/ any empty disks < next_spare become removed
889 *
890 * disks[0] gets initialised to REMOVED because
891 * we cannot be sure from other fields if it has
892 * been initialised or not.
893 */
894 int i;
895 int active=0, working=0,failed=0,spare=0,nr_disks=0;
896
NeilBrown61181562005-09-09 16:24:02 -0700897 rdev->sb_size = MD_SB_BYTES;
898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 sb = (mdp_super_t*)page_address(rdev->sb_page);
900
901 memset(sb, 0, sizeof(*sb));
902
903 sb->md_magic = MD_SB_MAGIC;
904 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 sb->patch_version = mddev->patch_version;
906 sb->gvalid_words = 0; /* ignored */
907 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
908 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
909 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
910 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
911
912 sb->ctime = mddev->ctime;
913 sb->level = mddev->level;
914 sb->size = mddev->size;
915 sb->raid_disks = mddev->raid_disks;
916 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800917 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 sb->utime = mddev->utime;
919 sb->state = 0;
920 sb->events_hi = (mddev->events>>32);
921 sb->events_lo = (u32)mddev->events;
922
NeilBrownf6705572006-03-27 01:18:11 -0800923 if (mddev->reshape_position == MaxSector)
924 sb->minor_version = 90;
925 else {
926 sb->minor_version = 91;
927 sb->reshape_position = mddev->reshape_position;
928 sb->new_level = mddev->new_level;
929 sb->delta_disks = mddev->delta_disks;
930 sb->new_layout = mddev->new_layout;
931 sb->new_chunk = mddev->new_chunk;
932 }
933 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (mddev->in_sync)
935 {
936 sb->recovery_cp = mddev->recovery_cp;
937 sb->cp_events_hi = (mddev->events>>32);
938 sb->cp_events_lo = (u32)mddev->events;
939 if (mddev->recovery_cp == MaxSector)
940 sb->state = (1<< MD_SB_CLEAN);
941 } else
942 sb->recovery_cp = 0;
943
944 sb->layout = mddev->layout;
945 sb->chunk_size = mddev->chunk_size;
946
NeilBrowna654b9d82005-06-21 17:17:27 -0700947 if (mddev->bitmap && mddev->bitmap_file == NULL)
948 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800951 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800953 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800954 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
955 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800956 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800958 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800959 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 d = &sb->disks[rdev2->desc_nr];
961 nr_disks++;
962 d->number = rdev2->desc_nr;
963 d->major = MAJOR(rdev2->bdev->bd_dev);
964 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800965 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
966 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 d->raid_disk = rdev2->raid_disk;
968 else
969 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800970 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800972 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 d->state = (1<<MD_DISK_ACTIVE);
974 d->state |= (1<<MD_DISK_SYNC);
975 active++;
976 working++;
977 } else {
978 d->state = 0;
979 spare++;
980 working++;
981 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700982 if (test_bit(WriteMostly, &rdev2->flags))
983 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 /* now set the "removed" and "faulty" bits on any missing devices */
986 for (i=0 ; i < mddev->raid_disks ; i++) {
987 mdp_disk_t *d = &sb->disks[i];
988 if (d->state == 0 && d->number == 0) {
989 d->number = i;
990 d->raid_disk = i;
991 d->state = (1<<MD_DISK_REMOVED);
992 d->state |= (1<<MD_DISK_FAULTY);
993 failed++;
994 }
995 }
996 sb->nr_disks = nr_disks;
997 sb->active_disks = active;
998 sb->working_disks = working;
999 sb->failed_disks = failed;
1000 sb->spare_disks = spare;
1001
1002 sb->this_disk = sb->disks[rdev->desc_nr];
1003 sb->sb_csum = calc_sb_csum(sb);
1004}
1005
1006/*
1007 * version 1 superblock
1008 */
1009
NeilBrown1c05b4b2006-10-21 10:24:08 -07001010static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001012 __le32 disk_csum;
1013 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 unsigned long long newcsum;
1015 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001016 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 int i;
1018
1019 disk_csum = sb->sb_csum;
1020 sb->sb_csum = 0;
1021 newcsum = 0;
1022 for (i=0; size>=4; size -= 4 )
1023 newcsum += le32_to_cpu(*isuper++);
1024
1025 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001026 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1029 sb->sb_csum = disk_csum;
1030 return cpu_to_le32(csum);
1031}
1032
1033static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1034{
1035 struct mdp_superblock_1 *sb;
1036 int ret;
1037 sector_t sb_offset;
1038 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001039 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 /*
1042 * Calculate the position of the superblock.
1043 * It is always aligned to a 4K boundary and
1044 * depeding on minor_version, it can be:
1045 * 0: At least 8K, but less than 12K, from end of device
1046 * 1: At start of device
1047 * 2: 4K from start of device.
1048 */
1049 switch(minor_version) {
1050 case 0:
1051 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1052 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001053 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* convert from sectors to K */
1055 sb_offset /= 2;
1056 break;
1057 case 1:
1058 sb_offset = 0;
1059 break;
1060 case 2:
1061 sb_offset = 4;
1062 break;
1063 default:
1064 return -EINVAL;
1065 }
1066 rdev->sb_offset = sb_offset;
1067
NeilBrown0002b272005-09-09 16:23:53 -07001068 /* superblock is rarely larger than 1K, but it can be larger,
1069 * and it is safe to read 4k, so we do that
1070 */
1071 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (ret) return ret;
1073
1074
1075 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1076
1077 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1078 sb->major_version != cpu_to_le32(1) ||
1079 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1080 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001081 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -EINVAL;
1083
1084 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1085 printk("md: invalid superblock checksum on %s\n",
1086 bdevname(rdev->bdev,b));
1087 return -EINVAL;
1088 }
1089 if (le64_to_cpu(sb->data_size) < 10) {
1090 printk("md: data_size too small on %s\n",
1091 bdevname(rdev->bdev,b));
1092 return -EINVAL;
1093 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001094 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1095 if (sb->level != cpu_to_le32(1) &&
1096 sb->level != cpu_to_le32(4) &&
1097 sb->level != cpu_to_le32(5) &&
1098 sb->level != cpu_to_le32(6) &&
1099 sb->level != cpu_to_le32(10)) {
1100 printk(KERN_WARNING
1101 "md: bitmaps not supported for this level.\n");
1102 return -EINVAL;
1103 }
1104 }
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 rdev->preferred_minor = 0xffff;
1107 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001108 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
NeilBrown0002b272005-09-09 16:23:53 -07001110 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001111 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001112 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001113 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1114
1115 if (minor_version
1116 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1117 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001118
NeilBrown31b65a02006-07-10 04:44:14 -07001119 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1120 rdev->desc_nr = -1;
1121 else
1122 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1123
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001124 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001125 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001126 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 __u64 ev1, ev2;
1128 struct mdp_superblock_1 *refsb =
1129 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1130
1131 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1132 sb->level != refsb->level ||
1133 sb->layout != refsb->layout ||
1134 sb->chunksize != refsb->chunksize) {
1135 printk(KERN_WARNING "md: %s has strangely different"
1136 " superblock to %s\n",
1137 bdevname(rdev->bdev,b),
1138 bdevname(refdev->bdev,b2));
1139 return -EINVAL;
1140 }
1141 ev1 = le64_to_cpu(sb->events);
1142 ev2 = le64_to_cpu(refsb->events);
1143
1144 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001145 ret = 1;
1146 else
1147 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
NeilBrowna1801f82008-03-04 14:29:31 -08001149 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1151 else
1152 rdev->size = rdev->sb_offset;
1153 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1154 return -EINVAL;
1155 rdev->size = le64_to_cpu(sb->data_size)/2;
1156 if (le32_to_cpu(sb->chunksize))
1157 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001158
NeilBrown1c05b4b2006-10-21 10:24:08 -07001159 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001160 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001161 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1165{
1166 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001167 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
NeilBrown41158c72005-06-21 17:17:25 -07001169 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001170 clear_bit(Faulty, &rdev->flags);
1171 clear_bit(In_sync, &rdev->flags);
1172 clear_bit(WriteMostly, &rdev->flags);
1173 clear_bit(BarriersNotsupp, &rdev->flags);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (mddev->raid_disks == 0) {
1176 mddev->major_version = 1;
1177 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001178 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1180 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1181 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1182 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001183 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 mddev->layout = le32_to_cpu(sb->layout);
1185 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1186 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001187 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001188 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001189 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1192 memcpy(mddev->uuid, sb->set_uuid, 16);
1193
1194 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001195
NeilBrown71c08052005-09-09 16:23:51 -07001196 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001197 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001198 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001199
NeilBrownf6705572006-03-27 01:18:11 -08001200 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1201 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1202 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1203 mddev->new_level = le32_to_cpu(sb->new_level);
1204 mddev->new_layout = le32_to_cpu(sb->new_layout);
1205 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1206 } else {
1207 mddev->reshape_position = MaxSector;
1208 mddev->delta_disks = 0;
1209 mddev->new_level = mddev->level;
1210 mddev->new_layout = mddev->layout;
1211 mddev->new_chunk = mddev->chunk_size;
1212 }
1213
NeilBrown41158c72005-06-21 17:17:25 -07001214 } else if (mddev->pers == NULL) {
1215 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ++ev1;
1217 if (ev1 < mddev->events)
1218 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001219 } else if (mddev->bitmap) {
1220 /* If adding to array with a bitmap, then we can accept an
1221 * older device, but not too old.
1222 */
NeilBrown41158c72005-06-21 17:17:25 -07001223 if (ev1 < mddev->bitmap->events_cleared)
1224 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001225 } else {
1226 if (ev1 < mddev->events)
1227 /* just a hot-add of a new device, leave raid_disk at -1 */
1228 return 0;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (mddev->level != LEVEL_MULTIPATH) {
1231 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1233 switch(role) {
1234 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001237 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 break;
1239 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001240 if ((le32_to_cpu(sb->feature_map) &
1241 MD_FEATURE_RECOVERY_OFFSET))
1242 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1243 else
1244 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 rdev->raid_disk = role;
1246 break;
1247 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001248 if (sb->devflags & WriteMostly1)
1249 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001250 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001251 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return 0;
1254}
1255
1256static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1257{
1258 struct mdp_superblock_1 *sb;
1259 struct list_head *tmp;
1260 mdk_rdev_t *rdev2;
1261 int max_dev, i;
1262 /* make rdev->sb match mddev and rdev data. */
1263
1264 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1265
1266 sb->feature_map = 0;
1267 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001268 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 memset(sb->pad1, 0, sizeof(sb->pad1));
1270 memset(sb->pad2, 0, sizeof(sb->pad2));
1271 memset(sb->pad3, 0, sizeof(sb->pad3));
1272
1273 sb->utime = cpu_to_le64((__u64)mddev->utime);
1274 sb->events = cpu_to_le64(mddev->events);
1275 if (mddev->in_sync)
1276 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1277 else
1278 sb->resync_offset = cpu_to_le64(0);
1279
NeilBrown1c05b4b2006-10-21 10:24:08 -07001280 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001281
NeilBrownf0ca3402006-02-02 14:28:04 -08001282 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001283 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001284
NeilBrowna654b9d82005-06-21 17:17:27 -07001285 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1286 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001287 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001288 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001289
1290 if (rdev->raid_disk >= 0 &&
1291 !test_bit(In_sync, &rdev->flags) &&
1292 rdev->recovery_offset > 0) {
1293 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1294 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1295 }
1296
NeilBrownf6705572006-03-27 01:18:11 -08001297 if (mddev->reshape_position != MaxSector) {
1298 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1299 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1300 sb->new_layout = cpu_to_le32(mddev->new_layout);
1301 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1302 sb->new_level = cpu_to_le32(mddev->new_level);
1303 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1304 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001307 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (rdev2->desc_nr+1 > max_dev)
1309 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001310
1311 if (max_dev > le32_to_cpu(sb->max_dev))
1312 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 for (i=0; i<max_dev;i++)
1314 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1315
NeilBrownd089c6a2008-02-06 01:39:59 -08001316 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001318 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001320 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001322 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1323 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 else
1325 sb->dev_roles[i] = cpu_to_le16(0xffff);
1326 }
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 sb->sb_csum = calc_sb_1_csum(sb);
1329}
1330
1331
Adrian Bunk75c96f82005-05-05 16:16:09 -07001332static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 [0] = {
1334 .name = "0.90.0",
1335 .owner = THIS_MODULE,
1336 .load_super = super_90_load,
1337 .validate_super = super_90_validate,
1338 .sync_super = super_90_sync,
1339 },
1340 [1] = {
1341 .name = "md-1",
1342 .owner = THIS_MODULE,
1343 .load_super = super_1_load,
1344 .validate_super = super_1_validate,
1345 .sync_super = super_1_sync,
1346 },
1347};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1350{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001351 struct list_head *tmp, *tmp2;
1352 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
NeilBrownd089c6a2008-02-06 01:39:59 -08001354 rdev_for_each(rdev, tmp, mddev1)
1355 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001356 if (rdev->bdev->bd_contains ==
1357 rdev2->bdev->bd_contains)
1358 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 return 0;
1361}
1362
1363static LIST_HEAD(pending_raid_disks);
1364
1365static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1366{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001367 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001368 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001369 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001370 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 if (rdev->mddev) {
1373 MD_BUG();
1374 return -EINVAL;
1375 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001376
1377 /* prevent duplicates */
1378 if (find_rdev(mddev, rdev->bdev->bd_dev))
1379 return -EEXIST;
1380
NeilBrown2bf071b2006-01-06 00:20:55 -08001381 /* make sure rdev->size exceeds mddev->size */
1382 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001383 if (mddev->pers) {
1384 /* Cannot change size, so fail
1385 * If mddev->level <= 0, then we don't care
1386 * about aligning sizes (e.g. linear)
1387 */
1388 if (mddev->level > 0)
1389 return -ENOSPC;
1390 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001391 mddev->size = rdev->size;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* Verify rdev->desc_nr is unique.
1395 * If it is -1, assign a free number, else
1396 * check number is not in use
1397 */
1398 if (rdev->desc_nr < 0) {
1399 int choice = 0;
1400 if (mddev->pers) choice = mddev->raid_disks;
1401 while (find_rdev_nr(mddev, choice))
1402 choice++;
1403 rdev->desc_nr = choice;
1404 } else {
1405 if (find_rdev_nr(mddev, rdev->desc_nr))
1406 return -EBUSY;
1407 }
NeilBrown19133a42005-11-08 21:39:35 -08001408 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001409 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001410 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001413 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001414
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001415 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001416 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001417
NeilBrownf637b9f2005-11-08 21:39:37 -08001418 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001419 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001420 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001421 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001422 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1423 kobject_del(&rdev->kobj);
1424 goto fail;
1425 }
1426 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001427 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001429
1430 fail:
1431 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1432 b, mdname(mddev));
1433 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
NeilBrown177a99b2008-02-06 01:39:56 -08001436static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001437{
1438 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1439 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001440 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1444{
1445 char b[BDEVNAME_SIZE];
1446 if (!rdev->mddev) {
1447 MD_BUG();
1448 return;
1449 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001450 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 list_del_init(&rdev->same_set);
1452 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1453 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001454 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001455
1456 /* We need to delay this, otherwise we can deadlock when
1457 * writing to 'remove' to "dev/state"
1458 */
NeilBrown177a99b2008-02-06 01:39:56 -08001459 INIT_WORK(&rdev->del_work, md_delayed_delete);
1460 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001461 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
1464/*
1465 * prevent the device from being mounted, repartitioned or
1466 * otherwise reused by a RAID array (or any other kernel
1467 * subsystem), by bd_claiming the device.
1468 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001469static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
1471 int err = 0;
1472 struct block_device *bdev;
1473 char b[BDEVNAME_SIZE];
1474
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001475 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (IS_ERR(bdev)) {
1477 printk(KERN_ERR "md: could not open %s.\n",
1478 __bdevname(dev, b));
1479 return PTR_ERR(bdev);
1480 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001481 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (err) {
1483 printk(KERN_ERR "md: could not bd_claim %s.\n",
1484 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001485 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return err;
1487 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001488 if (!shared)
1489 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 rdev->bdev = bdev;
1491 return err;
1492}
1493
1494static void unlock_rdev(mdk_rdev_t *rdev)
1495{
1496 struct block_device *bdev = rdev->bdev;
1497 rdev->bdev = NULL;
1498 if (!bdev)
1499 MD_BUG();
1500 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001501 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
1504void md_autodetect_dev(dev_t dev);
1505
1506static void export_rdev(mdk_rdev_t * rdev)
1507{
1508 char b[BDEVNAME_SIZE];
1509 printk(KERN_INFO "md: export_rdev(%s)\n",
1510 bdevname(rdev->bdev,b));
1511 if (rdev->mddev)
1512 MD_BUG();
1513 free_disk_sb(rdev);
1514 list_del_init(&rdev->same_set);
1515#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001516 if (test_bit(AutoDetected, &rdev->flags))
1517 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518#endif
1519 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001520 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
1523static void kick_rdev_from_array(mdk_rdev_t * rdev)
1524{
1525 unbind_rdev_from_array(rdev);
1526 export_rdev(rdev);
1527}
1528
1529static void export_array(mddev_t *mddev)
1530{
1531 struct list_head *tmp;
1532 mdk_rdev_t *rdev;
1533
NeilBrownd089c6a2008-02-06 01:39:59 -08001534 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (!rdev->mddev) {
1536 MD_BUG();
1537 continue;
1538 }
1539 kick_rdev_from_array(rdev);
1540 }
1541 if (!list_empty(&mddev->disks))
1542 MD_BUG();
1543 mddev->raid_disks = 0;
1544 mddev->major_version = 0;
1545}
1546
1547static void print_desc(mdp_disk_t *desc)
1548{
1549 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1550 desc->major,desc->minor,desc->raid_disk,desc->state);
1551}
1552
1553static void print_sb(mdp_super_t *sb)
1554{
1555 int i;
1556
1557 printk(KERN_INFO
1558 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1559 sb->major_version, sb->minor_version, sb->patch_version,
1560 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1561 sb->ctime);
1562 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1563 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1564 sb->md_minor, sb->layout, sb->chunk_size);
1565 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1566 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1567 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1568 sb->failed_disks, sb->spare_disks,
1569 sb->sb_csum, (unsigned long)sb->events_lo);
1570
1571 printk(KERN_INFO);
1572 for (i = 0; i < MD_SB_DISKS; i++) {
1573 mdp_disk_t *desc;
1574
1575 desc = sb->disks + i;
1576 if (desc->number || desc->major || desc->minor ||
1577 desc->raid_disk || (desc->state && (desc->state != 4))) {
1578 printk(" D %2d: ", i);
1579 print_desc(desc);
1580 }
1581 }
1582 printk(KERN_INFO "md: THIS: ");
1583 print_desc(&sb->this_disk);
1584
1585}
1586
1587static void print_rdev(mdk_rdev_t *rdev)
1588{
1589 char b[BDEVNAME_SIZE];
1590 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1591 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001592 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1593 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (rdev->sb_loaded) {
1595 printk(KERN_INFO "md: rdev superblock:\n");
1596 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1597 } else
1598 printk(KERN_INFO "md: no rdev superblock!\n");
1599}
1600
Adrian Bunk5e563412006-06-26 00:27:42 -07001601static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 struct list_head *tmp, *tmp2;
1604 mdk_rdev_t *rdev;
1605 mddev_t *mddev;
1606 char b[BDEVNAME_SIZE];
1607
1608 printk("\n");
1609 printk("md: **********************************\n");
1610 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1611 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001612 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
NeilBrown32a76272005-06-21 17:17:14 -07001614 if (mddev->bitmap)
1615 bitmap_print_sb(mddev->bitmap);
1616 else
1617 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001618 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 printk("<%s>", bdevname(rdev->bdev,b));
1620 printk("\n");
1621
NeilBrownd089c6a2008-02-06 01:39:59 -08001622 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 print_rdev(rdev);
1624 }
1625 printk("md: **********************************\n");
1626 printk("\n");
1627}
1628
1629
NeilBrown42543762006-06-26 00:27:57 -07001630static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
NeilBrown42543762006-06-26 00:27:57 -07001632 /* Update each superblock (in-memory image), but
1633 * if we are allowed to, skip spares which already
1634 * have the right event counter, or have one earlier
1635 * (which would mean they aren't being marked as dirty
1636 * with the rest of the array)
1637 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 mdk_rdev_t *rdev;
1639 struct list_head *tmp;
1640
NeilBrownd089c6a2008-02-06 01:39:59 -08001641 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001642 if (rdev->sb_events == mddev->events ||
1643 (nospares &&
1644 rdev->raid_disk < 0 &&
1645 (rdev->sb_events&1)==0 &&
1646 rdev->sb_events+1 == mddev->events)) {
1647 /* Don't update this superblock */
1648 rdev->sb_loaded = 2;
1649 } else {
1650 super_types[mddev->major_version].
1651 sync_super(mddev, rdev);
1652 rdev->sb_loaded = 1;
1653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 }
1655}
1656
NeilBrown850b2b422006-10-03 01:15:46 -07001657static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct list_head *tmp;
1660 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001661 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001662 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
NeilBrown8377bc82008-04-30 00:52:28 -07001664 if (mddev->external)
1665 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001667 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001668
NeilBrown850b2b422006-10-03 01:15:46 -07001669 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1670 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1671 force_change = 1;
1672 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1673 /* just a clean<-> dirty transition, possibly leave spares alone,
1674 * though if events isn't the right even/odd, we will have to do
1675 * spares after all
1676 */
1677 nospares = 1;
1678 if (force_change)
1679 nospares = 0;
1680 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001681 /* If the array is degraded, then skipping spares is both
1682 * dangerous and fairly pointless.
1683 * Dangerous because a device that was removed from the array
1684 * might have a event_count that still looks up-to-date,
1685 * so it can be re-added without a resync.
1686 * Pointless because if there are any spares to skip,
1687 * then a recovery will happen and soon that array won't
1688 * be degraded any more and the spare can go back to sleep then.
1689 */
NeilBrown850b2b422006-10-03 01:15:46 -07001690 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001691
NeilBrown06d91a52005-06-21 17:17:12 -07001692 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001694
1695 /* If this is just a dirty<->clean transition, and the array is clean
1696 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07001697 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001698 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001699 && (mddev->events & 1)
1700 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001701 mddev->events--;
1702 else {
1703 /* otherwise we have to go forward and ... */
1704 mddev->events ++;
1705 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1706 /* .. if the array isn't clean, insist on an odd 'events' */
1707 if ((mddev->events&1)==0) {
1708 mddev->events++;
1709 nospares = 0;
1710 }
1711 } else {
1712 /* otherwise insist on an even 'events' (for clean states) */
1713 if ((mddev->events&1)) {
1714 mddev->events++;
1715 nospares = 0;
1716 }
1717 }
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (!mddev->events) {
1721 /*
1722 * oops, this 64-bit counter should never wrap.
1723 * Either we are in around ~1 trillion A.C., assuming
1724 * 1 reboot per second, or we have a bug:
1725 */
1726 MD_BUG();
1727 mddev->events --;
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 /*
1731 * do not write anything to disk if using
1732 * nonpersistent superblocks
1733 */
NeilBrown06d91a52005-06-21 17:17:12 -07001734 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001735 if (!mddev->external)
1736 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1737
NeilBrowna9701a32005-11-08 21:39:34 -08001738 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001739 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001741 }
NeilBrowne6910632008-02-06 01:39:51 -08001742 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001743 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 dprintk(KERN_INFO
1746 "md: updating %s RAID superblock on device (in sync %d)\n",
1747 mdname(mddev),mddev->in_sync);
1748
NeilBrown4ad13662007-07-17 04:06:13 -07001749 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001750 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 char b[BDEVNAME_SIZE];
1752 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001753 if (rdev->sb_loaded != 1)
1754 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001755 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 dprintk("(skipping faulty ");
1757
1758 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001759 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001760 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001761 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001762 rdev->sb_page);
1763 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1764 bdevname(rdev->bdev,b),
1765 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001766 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 } else
1769 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001770 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 /* only need to write one superblock... */
1772 break;
1773 }
NeilBrowna9701a32005-11-08 21:39:34 -08001774 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07001775 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001776
NeilBrowna9701a32005-11-08 21:39:34 -08001777 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07001778 if (mddev->in_sync != sync_req ||
1779 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001780 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001781 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001782 goto repeat;
1783 }
NeilBrown850b2b422006-10-03 01:15:46 -07001784 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001785 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001786 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
NeilBrownbce74da2006-01-06 00:20:41 -08001790/* words written to sysfs files may, or my not, be \n terminated.
1791 * We want to accept with case. For this we use cmd_match.
1792 */
1793static int cmd_match(const char *cmd, const char *str)
1794{
1795 /* See if cmd, written into a sysfs file, matches
1796 * str. They must either be the same, or cmd can
1797 * have a trailing newline
1798 */
1799 while (*cmd && *str && *cmd == *str) {
1800 cmd++;
1801 str++;
1802 }
1803 if (*cmd == '\n')
1804 cmd++;
1805 if (*str || *cmd)
1806 return 0;
1807 return 1;
1808}
1809
NeilBrown86e6ffd2005-11-08 21:39:24 -08001810struct rdev_sysfs_entry {
1811 struct attribute attr;
1812 ssize_t (*show)(mdk_rdev_t *, char *);
1813 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1814};
1815
1816static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001817state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001818{
1819 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001820 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001821
NeilBrownb2d444d2005-11-08 21:39:31 -08001822 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001823 len+= sprintf(page+len, "%sfaulty",sep);
1824 sep = ",";
1825 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001826 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001827 len += sprintf(page+len, "%sin_sync",sep);
1828 sep = ",";
1829 }
NeilBrownf6556752006-06-26 00:28:01 -07001830 if (test_bit(WriteMostly, &rdev->flags)) {
1831 len += sprintf(page+len, "%swrite_mostly",sep);
1832 sep = ",";
1833 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001834 if (test_bit(Blocked, &rdev->flags)) {
1835 len += sprintf(page+len, "%sblocked", sep);
1836 sep = ",";
1837 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001838 if (!test_bit(Faulty, &rdev->flags) &&
1839 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001840 len += sprintf(page+len, "%sspare", sep);
1841 sep = ",";
1842 }
1843 return len+sprintf(page+len, "\n");
1844}
1845
NeilBrown45dc2de2006-06-26 00:27:58 -07001846static ssize_t
1847state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1848{
1849 /* can write
1850 * faulty - simulates and error
1851 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001852 * writemostly - sets write_mostly
1853 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001854 * blocked - sets the Blocked flag
1855 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001856 */
1857 int err = -EINVAL;
1858 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1859 md_error(rdev->mddev, rdev);
1860 err = 0;
1861 } else if (cmd_match(buf, "remove")) {
1862 if (rdev->raid_disk >= 0)
1863 err = -EBUSY;
1864 else {
1865 mddev_t *mddev = rdev->mddev;
1866 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001867 if (mddev->pers)
1868 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001869 md_new_event(mddev);
1870 err = 0;
1871 }
NeilBrownf6556752006-06-26 00:28:01 -07001872 } else if (cmd_match(buf, "writemostly")) {
1873 set_bit(WriteMostly, &rdev->flags);
1874 err = 0;
1875 } else if (cmd_match(buf, "-writemostly")) {
1876 clear_bit(WriteMostly, &rdev->flags);
1877 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001878 } else if (cmd_match(buf, "blocked")) {
1879 set_bit(Blocked, &rdev->flags);
1880 err = 0;
1881 } else if (cmd_match(buf, "-blocked")) {
1882 clear_bit(Blocked, &rdev->flags);
1883 wake_up(&rdev->blocked_wait);
1884 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1885 md_wakeup_thread(rdev->mddev->thread);
1886
1887 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001888 }
1889 return err ? err : len;
1890}
NeilBrown80ca3a42006-07-10 04:44:18 -07001891static struct rdev_sysfs_entry rdev_state =
1892__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001893
1894static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001895errors_show(mdk_rdev_t *rdev, char *page)
1896{
1897 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1898}
1899
1900static ssize_t
1901errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1902{
1903 char *e;
1904 unsigned long n = simple_strtoul(buf, &e, 10);
1905 if (*buf && (*e == 0 || *e == '\n')) {
1906 atomic_set(&rdev->corrected_errors, n);
1907 return len;
1908 }
1909 return -EINVAL;
1910}
1911static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001912__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001913
NeilBrown014236d2006-01-06 00:20:55 -08001914static ssize_t
1915slot_show(mdk_rdev_t *rdev, char *page)
1916{
1917 if (rdev->raid_disk < 0)
1918 return sprintf(page, "none\n");
1919 else
1920 return sprintf(page, "%d\n", rdev->raid_disk);
1921}
1922
1923static ssize_t
1924slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1925{
1926 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001927 int err;
1928 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001929 int slot = simple_strtoul(buf, &e, 10);
1930 if (strncmp(buf, "none", 4)==0)
1931 slot = -1;
1932 else if (e==buf || (*e && *e!= '\n'))
1933 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10001934 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001935 /* Setting 'slot' on an active array requires also
1936 * updating the 'rd%d' link, and communicating
1937 * with the personality with ->hot_*_disk.
1938 * For now we only support removing
1939 * failed/spare devices. This normally happens automatically,
1940 * but not when the metadata is externally managed.
1941 */
NeilBrownc303da62008-02-06 01:39:51 -08001942 if (rdev->raid_disk == -1)
1943 return -EEXIST;
1944 /* personality does all needed checks */
1945 if (rdev->mddev->pers->hot_add_disk == NULL)
1946 return -EINVAL;
1947 err = rdev->mddev->pers->
1948 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1949 if (err)
1950 return err;
1951 sprintf(nm, "rd%d", rdev->raid_disk);
1952 sysfs_remove_link(&rdev->mddev->kobj, nm);
1953 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1954 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10001955 } else if (rdev->mddev->pers) {
1956 mdk_rdev_t *rdev2;
1957 struct list_head *tmp;
1958 /* Activating a spare .. or possibly reactivating
1959 * if we every get bitmaps working here.
1960 */
1961
1962 if (rdev->raid_disk != -1)
1963 return -EBUSY;
1964
1965 if (rdev->mddev->pers->hot_add_disk == NULL)
1966 return -EINVAL;
1967
1968 rdev_for_each(rdev2, tmp, rdev->mddev)
1969 if (rdev2->raid_disk == slot)
1970 return -EEXIST;
1971
1972 rdev->raid_disk = slot;
1973 if (test_bit(In_sync, &rdev->flags))
1974 rdev->saved_raid_disk = slot;
1975 else
1976 rdev->saved_raid_disk = -1;
1977 err = rdev->mddev->pers->
1978 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10001979 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10001980 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10001981 return err;
1982 }
1983 sprintf(nm, "rd%d", rdev->raid_disk);
1984 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
1985 printk(KERN_WARNING
1986 "md: cannot register "
1987 "%s for %s\n",
1988 nm, mdname(rdev->mddev));
1989
1990 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08001991 } else {
1992 if (slot >= rdev->mddev->raid_disks)
1993 return -ENOSPC;
1994 rdev->raid_disk = slot;
1995 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001996 clear_bit(Faulty, &rdev->flags);
1997 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08001998 set_bit(In_sync, &rdev->flags);
1999 }
NeilBrown014236d2006-01-06 00:20:55 -08002000 return len;
2001}
2002
2003
2004static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002005__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002006
NeilBrown93c8cad2006-01-06 00:20:56 -08002007static ssize_t
2008offset_show(mdk_rdev_t *rdev, char *page)
2009{
Andrew Morton6961ece2006-01-06 00:20:59 -08002010 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002011}
2012
2013static ssize_t
2014offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2015{
2016 char *e;
2017 unsigned long long offset = simple_strtoull(buf, &e, 10);
2018 if (e==buf || (*e && *e != '\n'))
2019 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002020 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002021 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002022 if (rdev->size && rdev->mddev->external)
2023 /* Must set offset before size, so overlap checks
2024 * can be sane */
2025 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002026 rdev->data_offset = offset;
2027 return len;
2028}
2029
2030static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002031__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002032
NeilBrown83303b62006-01-06 00:21:06 -08002033static ssize_t
2034rdev_size_show(mdk_rdev_t *rdev, char *page)
2035{
2036 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2037}
2038
NeilBrownc5d79ad2008-02-06 01:39:54 -08002039static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2040{
2041 /* check if two start/length pairs overlap */
2042 if (s1+l1 <= s2)
2043 return 0;
2044 if (s2+l2 <= s1)
2045 return 0;
2046 return 1;
2047}
2048
NeilBrown83303b62006-01-06 00:21:06 -08002049static ssize_t
2050rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2051{
2052 char *e;
2053 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002054 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002055 mddev_t *my_mddev = rdev->mddev;
2056
NeilBrown83303b62006-01-06 00:21:06 -08002057 if (e==buf || (*e && *e != '\n'))
2058 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002059 if (my_mddev->pers && rdev->raid_disk >= 0)
NeilBrown83303b62006-01-06 00:21:06 -08002060 return -EBUSY;
2061 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002062 if (size > oldsize && rdev->mddev->external) {
2063 /* need to check that all other rdevs with the same ->bdev
2064 * do not overlap. We need to unlock the mddev to avoid
2065 * a deadlock. We have already changed rdev->size, and if
2066 * we have to change it back, we will have the lock again.
2067 */
2068 mddev_t *mddev;
2069 int overlap = 0;
2070 struct list_head *tmp, *tmp2;
2071
NeilBrown27c529b2008-03-04 14:29:33 -08002072 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002073 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002074 mdk_rdev_t *rdev2;
2075
2076 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002077 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002078 if (test_bit(AllReserved, &rdev2->flags) ||
2079 (rdev->bdev == rdev2->bdev &&
2080 rdev != rdev2 &&
2081 overlaps(rdev->data_offset, rdev->size,
2082 rdev2->data_offset, rdev2->size))) {
2083 overlap = 1;
2084 break;
2085 }
2086 mddev_unlock(mddev);
2087 if (overlap) {
2088 mddev_put(mddev);
2089 break;
2090 }
2091 }
NeilBrown27c529b2008-03-04 14:29:33 -08002092 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002093 if (overlap) {
2094 /* Someone else could have slipped in a size
2095 * change here, but doing so is just silly.
2096 * We put oldsize back because we *know* it is
2097 * safe, and trust userspace not to race with
2098 * itself
2099 */
2100 rdev->size = oldsize;
2101 return -EBUSY;
2102 }
2103 }
NeilBrown27c529b2008-03-04 14:29:33 -08002104 if (size < my_mddev->size || my_mddev->size == 0)
2105 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002106 return len;
2107}
2108
2109static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002110__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002111
NeilBrown86e6ffd2005-11-08 21:39:24 -08002112static struct attribute *rdev_default_attrs[] = {
2113 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002114 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002115 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002116 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002117 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002118 NULL,
2119};
2120static ssize_t
2121rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2122{
2123 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2124 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002125 mddev_t *mddev = rdev->mddev;
2126 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002127
2128 if (!entry->show)
2129 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002130
2131 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2132 if (!rv) {
2133 if (rdev->mddev == NULL)
2134 rv = -EBUSY;
2135 else
2136 rv = entry->show(rdev, page);
2137 mddev_unlock(mddev);
2138 }
2139 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002140}
2141
2142static ssize_t
2143rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2144 const char *page, size_t length)
2145{
2146 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2147 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002148 ssize_t rv;
2149 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002150
2151 if (!entry->store)
2152 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002153 if (!capable(CAP_SYS_ADMIN))
2154 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002155 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002156 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002157 if (rdev->mddev == NULL)
2158 rv = -EBUSY;
2159 else
2160 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002161 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002162 }
2163 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002164}
2165
2166static void rdev_free(struct kobject *ko)
2167{
2168 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2169 kfree(rdev);
2170}
2171static struct sysfs_ops rdev_sysfs_ops = {
2172 .show = rdev_attr_show,
2173 .store = rdev_attr_store,
2174};
2175static struct kobj_type rdev_ktype = {
2176 .release = rdev_free,
2177 .sysfs_ops = &rdev_sysfs_ops,
2178 .default_attrs = rdev_default_attrs,
2179};
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181/*
2182 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2183 *
2184 * mark the device faulty if:
2185 *
2186 * - the device is nonexistent (zero size)
2187 * - the device has no valid superblock
2188 *
2189 * a faulty rdev _never_ has rdev->sb set.
2190 */
2191static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2192{
2193 char b[BDEVNAME_SIZE];
2194 int err;
2195 mdk_rdev_t *rdev;
2196 sector_t size;
2197
NeilBrown9ffae0c2006-01-06 00:20:32 -08002198 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (!rdev) {
2200 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2201 return ERR_PTR(-ENOMEM);
2202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 if ((err = alloc_disk_sb(rdev)))
2205 goto abort_free;
2206
NeilBrownc5d79ad2008-02-06 01:39:54 -08002207 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (err)
2209 goto abort_free;
2210
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002211 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002214 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002215 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002216 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002218 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002220 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002221 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2224 if (!size) {
2225 printk(KERN_WARNING
2226 "md: %s has zero or unknown size, marking faulty!\n",
2227 bdevname(rdev->bdev,b));
2228 err = -EINVAL;
2229 goto abort_free;
2230 }
2231
2232 if (super_format >= 0) {
2233 err = super_types[super_format].
2234 load_super(rdev, NULL, super_minor);
2235 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002236 printk(KERN_WARNING
2237 "md: %s does not have a valid v%d.%d "
2238 "superblock, not importing!\n",
2239 bdevname(rdev->bdev,b),
2240 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto abort_free;
2242 }
2243 if (err < 0) {
2244 printk(KERN_WARNING
2245 "md: could not read %s's sb, not importing!\n",
2246 bdevname(rdev->bdev,b));
2247 goto abort_free;
2248 }
2249 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002252 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254 return rdev;
2255
2256abort_free:
2257 if (rdev->sb_page) {
2258 if (rdev->bdev)
2259 unlock_rdev(rdev);
2260 free_disk_sb(rdev);
2261 }
2262 kfree(rdev);
2263 return ERR_PTR(err);
2264}
2265
2266/*
2267 * Check a full RAID array for plausibility
2268 */
2269
2270
NeilBrowna757e642005-04-16 15:26:42 -07002271static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 int i;
2274 struct list_head *tmp;
2275 mdk_rdev_t *rdev, *freshest;
2276 char b[BDEVNAME_SIZE];
2277
2278 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002279 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 switch (super_types[mddev->major_version].
2281 load_super(rdev, freshest, mddev->minor_version)) {
2282 case 1:
2283 freshest = rdev;
2284 break;
2285 case 0:
2286 break;
2287 default:
2288 printk( KERN_ERR \
2289 "md: fatal superblock inconsistency in %s"
2290 " -- removing from array\n",
2291 bdevname(rdev->bdev,b));
2292 kick_rdev_from_array(rdev);
2293 }
2294
2295
2296 super_types[mddev->major_version].
2297 validate_super(mddev, freshest);
2298
2299 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002300 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (rdev != freshest)
2302 if (super_types[mddev->major_version].
2303 validate_super(mddev, rdev)) {
2304 printk(KERN_WARNING "md: kicking non-fresh %s"
2305 " from array!\n",
2306 bdevname(rdev->bdev,b));
2307 kick_rdev_from_array(rdev);
2308 continue;
2309 }
2310 if (mddev->level == LEVEL_MULTIPATH) {
2311 rdev->desc_nr = i++;
2312 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002313 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002314 } else if (rdev->raid_disk >= mddev->raid_disks) {
2315 rdev->raid_disk = -1;
2316 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318 }
2319
2320
2321
2322 if (mddev->recovery_cp != MaxSector &&
2323 mddev->level >= 1)
2324 printk(KERN_ERR "md: %s: raid array is not clean"
2325 " -- starting background reconstruction\n",
2326 mdname(mddev));
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
NeilBrowneae17012005-11-08 21:39:23 -08002330static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002331safe_delay_show(mddev_t *mddev, char *page)
2332{
2333 int msec = (mddev->safemode_delay*1000)/HZ;
2334 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2335}
2336static ssize_t
2337safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2338{
2339 int scale=1;
2340 int dot=0;
2341 int i;
2342 unsigned long msec;
2343 char buf[30];
2344 char *e;
2345 /* remove a period, and count digits after it */
2346 if (len >= sizeof(buf))
2347 return -EINVAL;
2348 strlcpy(buf, cbuf, len);
2349 buf[len] = 0;
2350 for (i=0; i<len; i++) {
2351 if (dot) {
2352 if (isdigit(buf[i])) {
2353 buf[i-1] = buf[i];
2354 scale *= 10;
2355 }
2356 buf[i] = 0;
2357 } else if (buf[i] == '.') {
2358 dot=1;
2359 buf[i] = 0;
2360 }
2361 }
2362 msec = simple_strtoul(buf, &e, 10);
2363 if (e == buf || (*e && *e != '\n'))
2364 return -EINVAL;
2365 msec = (msec * 1000) / scale;
2366 if (msec == 0)
2367 mddev->safemode_delay = 0;
2368 else {
2369 mddev->safemode_delay = (msec*HZ)/1000;
2370 if (mddev->safemode_delay == 0)
2371 mddev->safemode_delay = 1;
2372 }
2373 return len;
2374}
2375static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002376__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002377
2378static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002379level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002380{
NeilBrown2604b702006-01-06 00:20:36 -08002381 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002382 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002383 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002384 else if (mddev->clevel[0])
2385 return sprintf(page, "%s\n", mddev->clevel);
2386 else if (mddev->level != LEVEL_NONE)
2387 return sprintf(page, "%d\n", mddev->level);
2388 else
2389 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002390}
2391
NeilBrownd9d166c2006-01-06 00:20:51 -08002392static ssize_t
2393level_store(mddev_t *mddev, const char *buf, size_t len)
2394{
NeilBrown20a49ff2008-02-06 01:39:57 -08002395 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002396 if (mddev->pers)
2397 return -EBUSY;
2398 if (len == 0)
2399 return 0;
2400 if (len >= sizeof(mddev->clevel))
2401 return -ENOSPC;
2402 strncpy(mddev->clevel, buf, len);
2403 if (mddev->clevel[len-1] == '\n')
2404 len--;
2405 mddev->clevel[len] = 0;
2406 mddev->level = LEVEL_NONE;
2407 return rv;
2408}
2409
2410static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002411__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002412
NeilBrownd4dbd022006-06-26 00:27:59 -07002413
2414static ssize_t
2415layout_show(mddev_t *mddev, char *page)
2416{
2417 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002418 if (mddev->reshape_position != MaxSector &&
2419 mddev->layout != mddev->new_layout)
2420 return sprintf(page, "%d (%d)\n",
2421 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002422 return sprintf(page, "%d\n", mddev->layout);
2423}
2424
2425static ssize_t
2426layout_store(mddev_t *mddev, const char *buf, size_t len)
2427{
2428 char *e;
2429 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002430
2431 if (!*buf || (*e && *e != '\n'))
2432 return -EINVAL;
2433
NeilBrown08a02ec2007-05-09 02:35:38 -07002434 if (mddev->pers)
2435 return -EBUSY;
2436 if (mddev->reshape_position != MaxSector)
2437 mddev->new_layout = n;
2438 else
2439 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002440 return len;
2441}
2442static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002443__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002444
2445
NeilBrowneae17012005-11-08 21:39:23 -08002446static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002447raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002448{
NeilBrownbb636542005-11-08 21:39:45 -08002449 if (mddev->raid_disks == 0)
2450 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002451 if (mddev->reshape_position != MaxSector &&
2452 mddev->delta_disks != 0)
2453 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2454 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002455 return sprintf(page, "%d\n", mddev->raid_disks);
2456}
2457
NeilBrownda943b992006-01-06 00:20:54 -08002458static int update_raid_disks(mddev_t *mddev, int raid_disks);
2459
2460static ssize_t
2461raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2462{
NeilBrownda943b992006-01-06 00:20:54 -08002463 char *e;
2464 int rv = 0;
2465 unsigned long n = simple_strtoul(buf, &e, 10);
2466
2467 if (!*buf || (*e && *e != '\n'))
2468 return -EINVAL;
2469
2470 if (mddev->pers)
2471 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002472 else if (mddev->reshape_position != MaxSector) {
2473 int olddisks = mddev->raid_disks - mddev->delta_disks;
2474 mddev->delta_disks = n - olddisks;
2475 mddev->raid_disks = n;
2476 } else
NeilBrownda943b992006-01-06 00:20:54 -08002477 mddev->raid_disks = n;
2478 return rv ? rv : len;
2479}
2480static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002481__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002482
NeilBrown24dd4692005-11-08 21:39:26 -08002483static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002484chunk_size_show(mddev_t *mddev, char *page)
2485{
NeilBrown08a02ec2007-05-09 02:35:38 -07002486 if (mddev->reshape_position != MaxSector &&
2487 mddev->chunk_size != mddev->new_chunk)
2488 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2489 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002490 return sprintf(page, "%d\n", mddev->chunk_size);
2491}
2492
2493static ssize_t
2494chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2495{
2496 /* can only set chunk_size if array is not yet active */
2497 char *e;
2498 unsigned long n = simple_strtoul(buf, &e, 10);
2499
NeilBrown3b343802006-01-06 00:20:47 -08002500 if (!*buf || (*e && *e != '\n'))
2501 return -EINVAL;
2502
NeilBrown08a02ec2007-05-09 02:35:38 -07002503 if (mddev->pers)
2504 return -EBUSY;
2505 else if (mddev->reshape_position != MaxSector)
2506 mddev->new_chunk = n;
2507 else
2508 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002509 return len;
2510}
2511static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002512__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002513
NeilBrowna94213b2006-06-26 00:28:00 -07002514static ssize_t
2515resync_start_show(mddev_t *mddev, char *page)
2516{
2517 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2518}
2519
2520static ssize_t
2521resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2522{
NeilBrowna94213b2006-06-26 00:28:00 -07002523 char *e;
2524 unsigned long long n = simple_strtoull(buf, &e, 10);
2525
2526 if (mddev->pers)
2527 return -EBUSY;
2528 if (!*buf || (*e && *e != '\n'))
2529 return -EINVAL;
2530
2531 mddev->recovery_cp = n;
2532 return len;
2533}
2534static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002535__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002536
NeilBrown9e653b62006-06-26 00:27:58 -07002537/*
2538 * The array state can be:
2539 *
2540 * clear
2541 * No devices, no size, no level
2542 * Equivalent to STOP_ARRAY ioctl
2543 * inactive
2544 * May have some settings, but array is not active
2545 * all IO results in error
2546 * When written, doesn't tear down array, but just stops it
2547 * suspended (not supported yet)
2548 * All IO requests will block. The array can be reconfigured.
2549 * Writing this, if accepted, will block until array is quiessent
2550 * readonly
2551 * no resync can happen. no superblocks get written.
2552 * write requests fail
2553 * read-auto
2554 * like readonly, but behaves like 'clean' on a write request.
2555 *
2556 * clean - no pending writes, but otherwise active.
2557 * When written to inactive array, starts without resync
2558 * If a write request arrives then
2559 * if metadata is known, mark 'dirty' and switch to 'active'.
2560 * if not known, block and switch to write-pending
2561 * If written to an active array that has pending writes, then fails.
2562 * active
2563 * fully active: IO and resync can be happening.
2564 * When written to inactive array, starts with resync
2565 *
2566 * write-pending
2567 * clean, but writes are blocked waiting for 'active' to be written.
2568 *
2569 * active-idle
2570 * like active, but no writes have been seen for a while (100msec).
2571 *
2572 */
2573enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2574 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002575static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002576 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2577 "write-pending", "active-idle", NULL };
2578
2579static int match_word(const char *word, char **list)
2580{
2581 int n;
2582 for (n=0; list[n]; n++)
2583 if (cmd_match(word, list[n]))
2584 break;
2585 return n;
2586}
2587
2588static ssize_t
2589array_state_show(mddev_t *mddev, char *page)
2590{
2591 enum array_state st = inactive;
2592
2593 if (mddev->pers)
2594 switch(mddev->ro) {
2595 case 1:
2596 st = readonly;
2597 break;
2598 case 2:
2599 st = read_auto;
2600 break;
2601 case 0:
2602 if (mddev->in_sync)
2603 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002604 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2605 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002606 else if (mddev->safemode)
2607 st = active_idle;
2608 else
2609 st = active;
2610 }
2611 else {
2612 if (list_empty(&mddev->disks) &&
2613 mddev->raid_disks == 0 &&
2614 mddev->size == 0)
2615 st = clear;
2616 else
2617 st = inactive;
2618 }
2619 return sprintf(page, "%s\n", array_states[st]);
2620}
2621
2622static int do_md_stop(mddev_t * mddev, int ro);
2623static int do_md_run(mddev_t * mddev);
2624static int restart_array(mddev_t *mddev);
2625
2626static ssize_t
2627array_state_store(mddev_t *mddev, const char *buf, size_t len)
2628{
2629 int err = -EINVAL;
2630 enum array_state st = match_word(buf, array_states);
2631 switch(st) {
2632 case bad_word:
2633 break;
2634 case clear:
2635 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002636 if (atomic_read(&mddev->active) > 1)
2637 return -EBUSY;
2638 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002639 break;
2640 case inactive:
2641 /* stopping an active array */
2642 if (mddev->pers) {
2643 if (atomic_read(&mddev->active) > 1)
2644 return -EBUSY;
2645 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002646 } else
2647 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002648 break;
2649 case suspended:
2650 break; /* not supported yet */
2651 case readonly:
2652 if (mddev->pers)
2653 err = do_md_stop(mddev, 1);
2654 else {
2655 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002656 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002657 err = do_md_run(mddev);
2658 }
2659 break;
2660 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002661 if (mddev->pers) {
NeilBrown648b6292008-04-30 00:52:30 -07002662 if (mddev->ro != 1)
2663 err = do_md_stop(mddev, 1);
2664 else
2665 err = restart_array(mddev);
2666 if (err == 0) {
2667 mddev->ro = 2;
2668 set_disk_ro(mddev->gendisk, 0);
2669 }
NeilBrown9e653b62006-06-26 00:27:58 -07002670 } else {
2671 mddev->ro = 2;
2672 err = do_md_run(mddev);
2673 }
2674 break;
2675 case clean:
2676 if (mddev->pers) {
2677 restart_array(mddev);
2678 spin_lock_irq(&mddev->write_lock);
2679 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002680 if (mddev->in_sync == 0) {
2681 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002682 if (mddev->safemode == 1)
2683 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002684 if (mddev->persistent)
2685 set_bit(MD_CHANGE_CLEAN,
2686 &mddev->flags);
2687 }
2688 err = 0;
2689 } else
2690 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002691 spin_unlock_irq(&mddev->write_lock);
2692 } else {
2693 mddev->ro = 0;
2694 mddev->recovery_cp = MaxSector;
2695 err = do_md_run(mddev);
2696 }
2697 break;
2698 case active:
2699 if (mddev->pers) {
2700 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002701 if (mddev->external)
2702 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002703 wake_up(&mddev->sb_wait);
2704 err = 0;
2705 } else {
2706 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002707 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002708 err = do_md_run(mddev);
2709 }
2710 break;
2711 case write_pending:
2712 case active_idle:
2713 /* these cannot be set */
2714 break;
2715 }
2716 if (err)
2717 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002718 else {
2719 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown9e653b62006-06-26 00:27:58 -07002720 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002721 }
NeilBrown9e653b62006-06-26 00:27:58 -07002722}
NeilBrown80ca3a42006-07-10 04:44:18 -07002723static struct md_sysfs_entry md_array_state =
2724__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002725
NeilBrown6d7ff7382006-01-06 00:21:16 -08002726static ssize_t
2727null_show(mddev_t *mddev, char *page)
2728{
2729 return -EINVAL;
2730}
2731
2732static ssize_t
2733new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2734{
2735 /* buf must be %d:%d\n? giving major and minor numbers */
2736 /* The new device is added to the array.
2737 * If the array has a persistent superblock, we read the
2738 * superblock to initialise info and check validity.
2739 * Otherwise, only checking done is that in bind_rdev_to_array,
2740 * which mainly checks size.
2741 */
2742 char *e;
2743 int major = simple_strtoul(buf, &e, 10);
2744 int minor;
2745 dev_t dev;
2746 mdk_rdev_t *rdev;
2747 int err;
2748
2749 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2750 return -EINVAL;
2751 minor = simple_strtoul(e+1, &e, 10);
2752 if (*e && *e != '\n')
2753 return -EINVAL;
2754 dev = MKDEV(major, minor);
2755 if (major != MAJOR(dev) ||
2756 minor != MINOR(dev))
2757 return -EOVERFLOW;
2758
2759
2760 if (mddev->persistent) {
2761 rdev = md_import_device(dev, mddev->major_version,
2762 mddev->minor_version);
2763 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2764 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2765 mdk_rdev_t, same_set);
2766 err = super_types[mddev->major_version]
2767 .load_super(rdev, rdev0, mddev->minor_version);
2768 if (err < 0)
2769 goto out;
2770 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002771 } else if (mddev->external)
2772 rdev = md_import_device(dev, -2, -1);
2773 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08002774 rdev = md_import_device(dev, -1, -1);
2775
2776 if (IS_ERR(rdev))
2777 return PTR_ERR(rdev);
2778 err = bind_rdev_to_array(rdev, mddev);
2779 out:
2780 if (err)
2781 export_rdev(rdev);
2782 return err ? err : len;
2783}
2784
2785static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002786__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002787
2788static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002789bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2790{
2791 char *end;
2792 unsigned long chunk, end_chunk;
2793
2794 if (!mddev->bitmap)
2795 goto out;
2796 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2797 while (*buf) {
2798 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2799 if (buf == end) break;
2800 if (*end == '-') { /* range */
2801 buf = end + 1;
2802 end_chunk = simple_strtoul(buf, &end, 0);
2803 if (buf == end) break;
2804 }
2805 if (*end && !isspace(*end)) break;
2806 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2807 buf = end;
2808 while (isspace(*buf)) buf++;
2809 }
2810 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2811out:
2812 return len;
2813}
2814
2815static struct md_sysfs_entry md_bitmap =
2816__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2817
2818static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002819size_show(mddev_t *mddev, char *page)
2820{
2821 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2822}
2823
2824static int update_size(mddev_t *mddev, unsigned long size);
2825
2826static ssize_t
2827size_store(mddev_t *mddev, const char *buf, size_t len)
2828{
2829 /* If array is inactive, we can reduce the component size, but
2830 * not increase it (except from 0).
2831 * If array is active, we can try an on-line resize
2832 */
2833 char *e;
2834 int err = 0;
2835 unsigned long long size = simple_strtoull(buf, &e, 10);
2836 if (!*buf || *buf == '\n' ||
2837 (*e && *e != '\n'))
2838 return -EINVAL;
2839
2840 if (mddev->pers) {
2841 err = update_size(mddev, size);
NeilBrown850b2b422006-10-03 01:15:46 -07002842 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002843 } else {
2844 if (mddev->size == 0 ||
2845 mddev->size > size)
2846 mddev->size = size;
2847 else
2848 err = -ENOSPC;
2849 }
2850 return err ? err : len;
2851}
2852
2853static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002854__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002855
NeilBrown8bb93aa2006-01-06 00:20:50 -08002856
2857/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002858 * This is one of
2859 * 'none' for arrays with no metadata (good luck...)
2860 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002861 * or N.M for internally known formats
2862 */
2863static ssize_t
2864metadata_show(mddev_t *mddev, char *page)
2865{
2866 if (mddev->persistent)
2867 return sprintf(page, "%d.%d\n",
2868 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002869 else if (mddev->external)
2870 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002871 else
2872 return sprintf(page, "none\n");
2873}
2874
2875static ssize_t
2876metadata_store(mddev_t *mddev, const char *buf, size_t len)
2877{
2878 int major, minor;
2879 char *e;
2880 if (!list_empty(&mddev->disks))
2881 return -EBUSY;
2882
2883 if (cmd_match(buf, "none")) {
2884 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002885 mddev->external = 0;
2886 mddev->major_version = 0;
2887 mddev->minor_version = 90;
2888 return len;
2889 }
2890 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002891 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002892 if (namelen >= sizeof(mddev->metadata_type))
2893 namelen = sizeof(mddev->metadata_type)-1;
2894 strncpy(mddev->metadata_type, buf+9, namelen);
2895 mddev->metadata_type[namelen] = 0;
2896 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2897 mddev->metadata_type[--namelen] = 0;
2898 mddev->persistent = 0;
2899 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002900 mddev->major_version = 0;
2901 mddev->minor_version = 90;
2902 return len;
2903 }
2904 major = simple_strtoul(buf, &e, 10);
2905 if (e==buf || *e != '.')
2906 return -EINVAL;
2907 buf = e+1;
2908 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002909 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002910 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002911 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002912 return -ENOENT;
2913 mddev->major_version = major;
2914 mddev->minor_version = minor;
2915 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002916 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002917 return len;
2918}
2919
2920static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002921__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002922
NeilBrowna35b0d62006-01-06 00:20:49 -08002923static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002924action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002925{
NeilBrown7eec3142005-11-08 21:39:44 -08002926 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002927 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002928 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002929 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2930 type = "reshape";
2931 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002932 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2933 type = "resync";
2934 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2935 type = "check";
2936 else
2937 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10002938 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002939 type = "recover";
2940 }
2941 return sprintf(page, "%s\n", type);
2942}
2943
2944static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002945action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002946{
NeilBrown7eec3142005-11-08 21:39:44 -08002947 if (!mddev->pers || !mddev->pers->sync_request)
2948 return -EINVAL;
2949
NeilBrownbce74da2006-01-06 00:20:41 -08002950 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002951 if (mddev->sync_thread) {
2952 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2953 md_unregister_thread(mddev->sync_thread);
2954 mddev->sync_thread = NULL;
2955 mddev->recovery = 0;
2956 }
NeilBrown03c902e2006-01-06 00:20:46 -08002957 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2958 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002959 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10002960 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08002961 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10002962 else if (cmd_match(page, "recover")) {
2963 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
2964 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2965 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08002966 int err;
2967 if (mddev->pers->start_reshape == NULL)
2968 return -EINVAL;
2969 err = mddev->pers->start_reshape(mddev);
2970 if (err)
2971 return err;
Neil Browna99ac972008-06-28 08:31:43 +10002972 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08002973 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002974 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002975 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002976 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002977 return -EINVAL;
2978 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2979 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002980 }
NeilBrown03c902e2006-01-06 00:20:46 -08002981 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002982 md_wakeup_thread(mddev->thread);
Neil Brown72a23c22008-06-28 08:31:41 +10002983 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrown24dd4692005-11-08 21:39:26 -08002984 return len;
2985}
2986
NeilBrown9d888832005-11-08 21:39:26 -08002987static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002988mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002989{
2990 return sprintf(page, "%llu\n",
2991 (unsigned long long) mddev->resync_mismatches);
2992}
2993
NeilBrown80ca3a42006-07-10 04:44:18 -07002994static struct md_sysfs_entry md_scan_mode =
2995__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002996
NeilBrown96de1e62005-11-08 21:39:39 -08002997
NeilBrown80ca3a42006-07-10 04:44:18 -07002998static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002999
NeilBrown88202a02006-01-06 00:21:36 -08003000static ssize_t
3001sync_min_show(mddev_t *mddev, char *page)
3002{
3003 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3004 mddev->sync_speed_min ? "local": "system");
3005}
3006
3007static ssize_t
3008sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3009{
3010 int min;
3011 char *e;
3012 if (strncmp(buf, "system", 6)==0) {
3013 mddev->sync_speed_min = 0;
3014 return len;
3015 }
3016 min = simple_strtoul(buf, &e, 10);
3017 if (buf == e || (*e && *e != '\n') || min <= 0)
3018 return -EINVAL;
3019 mddev->sync_speed_min = min;
3020 return len;
3021}
3022
3023static struct md_sysfs_entry md_sync_min =
3024__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3025
3026static ssize_t
3027sync_max_show(mddev_t *mddev, char *page)
3028{
3029 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3030 mddev->sync_speed_max ? "local": "system");
3031}
3032
3033static ssize_t
3034sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3035{
3036 int max;
3037 char *e;
3038 if (strncmp(buf, "system", 6)==0) {
3039 mddev->sync_speed_max = 0;
3040 return len;
3041 }
3042 max = simple_strtoul(buf, &e, 10);
3043 if (buf == e || (*e && *e != '\n') || max <= 0)
3044 return -EINVAL;
3045 mddev->sync_speed_max = max;
3046 return len;
3047}
3048
3049static struct md_sysfs_entry md_sync_max =
3050__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3051
Iustin Popd7f3d292007-10-16 23:30:54 -07003052static ssize_t
3053degraded_show(mddev_t *mddev, char *page)
3054{
3055 return sprintf(page, "%d\n", mddev->degraded);
3056}
3057static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003058
3059static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003060sync_force_parallel_show(mddev_t *mddev, char *page)
3061{
3062 return sprintf(page, "%d\n", mddev->parallel_resync);
3063}
3064
3065static ssize_t
3066sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3067{
3068 long n;
3069
3070 if (strict_strtol(buf, 10, &n))
3071 return -EINVAL;
3072
3073 if (n != 0 && n != 1)
3074 return -EINVAL;
3075
3076 mddev->parallel_resync = n;
3077
3078 if (mddev->sync_thread)
3079 wake_up(&resync_wait);
3080
3081 return len;
3082}
3083
3084/* force parallel resync, even with shared block devices */
3085static struct md_sysfs_entry md_sync_force_parallel =
3086__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3087 sync_force_parallel_show, sync_force_parallel_store);
3088
3089static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003090sync_speed_show(mddev_t *mddev, char *page)
3091{
3092 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07003093 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08003094 dt = ((jiffies - mddev->resync_mark) / HZ);
3095 if (!dt) dt++;
3096 db = resync - (mddev->resync_mark_cnt);
3097 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
3098}
3099
NeilBrown80ca3a42006-07-10 04:44:18 -07003100static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003101
3102static ssize_t
3103sync_completed_show(mddev_t *mddev, char *page)
3104{
3105 unsigned long max_blocks, resync;
3106
3107 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3108 max_blocks = mddev->resync_max_sectors;
3109 else
3110 max_blocks = mddev->size << 1;
3111
3112 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3113 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3114}
3115
NeilBrown80ca3a42006-07-10 04:44:18 -07003116static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003117
NeilBrowne464eaf2006-03-27 01:18:14 -08003118static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003119min_sync_show(mddev_t *mddev, char *page)
3120{
3121 return sprintf(page, "%llu\n",
3122 (unsigned long long)mddev->resync_min);
3123}
3124static ssize_t
3125min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3126{
3127 unsigned long long min;
3128 if (strict_strtoull(buf, 10, &min))
3129 return -EINVAL;
3130 if (min > mddev->resync_max)
3131 return -EINVAL;
3132 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3133 return -EBUSY;
3134
3135 /* Must be a multiple of chunk_size */
3136 if (mddev->chunk_size) {
3137 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3138 return -EINVAL;
3139 }
3140 mddev->resync_min = min;
3141
3142 return len;
3143}
3144
3145static struct md_sysfs_entry md_min_sync =
3146__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3147
3148static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003149max_sync_show(mddev_t *mddev, char *page)
3150{
3151 if (mddev->resync_max == MaxSector)
3152 return sprintf(page, "max\n");
3153 else
3154 return sprintf(page, "%llu\n",
3155 (unsigned long long)mddev->resync_max);
3156}
3157static ssize_t
3158max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3159{
3160 if (strncmp(buf, "max", 3) == 0)
3161 mddev->resync_max = MaxSector;
3162 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003163 unsigned long long max;
3164 if (strict_strtoull(buf, 10, &max))
3165 return -EINVAL;
3166 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003167 return -EINVAL;
3168 if (max < mddev->resync_max &&
3169 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3170 return -EBUSY;
3171
3172 /* Must be a multiple of chunk_size */
3173 if (mddev->chunk_size) {
3174 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3175 return -EINVAL;
3176 }
3177 mddev->resync_max = max;
3178 }
3179 wake_up(&mddev->recovery_wait);
3180 return len;
3181}
3182
3183static struct md_sysfs_entry md_max_sync =
3184__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3185
3186static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003187suspend_lo_show(mddev_t *mddev, char *page)
3188{
3189 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3190}
3191
3192static ssize_t
3193suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3194{
3195 char *e;
3196 unsigned long long new = simple_strtoull(buf, &e, 10);
3197
3198 if (mddev->pers->quiesce == NULL)
3199 return -EINVAL;
3200 if (buf == e || (*e && *e != '\n'))
3201 return -EINVAL;
3202 if (new >= mddev->suspend_hi ||
3203 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3204 mddev->suspend_lo = new;
3205 mddev->pers->quiesce(mddev, 2);
3206 return len;
3207 } else
3208 return -EINVAL;
3209}
3210static struct md_sysfs_entry md_suspend_lo =
3211__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3212
3213
3214static ssize_t
3215suspend_hi_show(mddev_t *mddev, char *page)
3216{
3217 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3218}
3219
3220static ssize_t
3221suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3222{
3223 char *e;
3224 unsigned long long new = simple_strtoull(buf, &e, 10);
3225
3226 if (mddev->pers->quiesce == NULL)
3227 return -EINVAL;
3228 if (buf == e || (*e && *e != '\n'))
3229 return -EINVAL;
3230 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3231 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3232 mddev->suspend_hi = new;
3233 mddev->pers->quiesce(mddev, 1);
3234 mddev->pers->quiesce(mddev, 0);
3235 return len;
3236 } else
3237 return -EINVAL;
3238}
3239static struct md_sysfs_entry md_suspend_hi =
3240__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3241
NeilBrown08a02ec2007-05-09 02:35:38 -07003242static ssize_t
3243reshape_position_show(mddev_t *mddev, char *page)
3244{
3245 if (mddev->reshape_position != MaxSector)
3246 return sprintf(page, "%llu\n",
3247 (unsigned long long)mddev->reshape_position);
3248 strcpy(page, "none\n");
3249 return 5;
3250}
3251
3252static ssize_t
3253reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3254{
3255 char *e;
3256 unsigned long long new = simple_strtoull(buf, &e, 10);
3257 if (mddev->pers)
3258 return -EBUSY;
3259 if (buf == e || (*e && *e != '\n'))
3260 return -EINVAL;
3261 mddev->reshape_position = new;
3262 mddev->delta_disks = 0;
3263 mddev->new_level = mddev->level;
3264 mddev->new_layout = mddev->layout;
3265 mddev->new_chunk = mddev->chunk_size;
3266 return len;
3267}
3268
3269static struct md_sysfs_entry md_reshape_position =
3270__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3271 reshape_position_store);
3272
NeilBrowne464eaf2006-03-27 01:18:14 -08003273
NeilBrowneae17012005-11-08 21:39:23 -08003274static struct attribute *md_default_attrs[] = {
3275 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003276 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003277 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003278 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003279 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003280 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003281 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003282 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003283 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003284 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003285 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003286 NULL,
3287};
3288
3289static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003290 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003291 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003292 &md_sync_min.attr,
3293 &md_sync_max.attr,
3294 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003295 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003296 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003297 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003298 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003299 &md_suspend_lo.attr,
3300 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003301 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003302 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003303 NULL,
3304};
NeilBrown411036f2005-11-08 21:39:40 -08003305static struct attribute_group md_redundancy_group = {
3306 .name = NULL,
3307 .attrs = md_redundancy_attrs,
3308};
3309
NeilBrowneae17012005-11-08 21:39:23 -08003310
3311static ssize_t
3312md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3313{
3314 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3315 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003316 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003317
3318 if (!entry->show)
3319 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003320 rv = mddev_lock(mddev);
3321 if (!rv) {
3322 rv = entry->show(mddev, page);
3323 mddev_unlock(mddev);
3324 }
NeilBrown96de1e62005-11-08 21:39:39 -08003325 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003326}
3327
3328static ssize_t
3329md_attr_store(struct kobject *kobj, struct attribute *attr,
3330 const char *page, size_t length)
3331{
3332 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3333 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003334 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003335
3336 if (!entry->store)
3337 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003338 if (!capable(CAP_SYS_ADMIN))
3339 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003340 rv = mddev_lock(mddev);
3341 if (!rv) {
3342 rv = entry->store(mddev, page, length);
3343 mddev_unlock(mddev);
3344 }
NeilBrown96de1e62005-11-08 21:39:39 -08003345 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003346}
3347
3348static void md_free(struct kobject *ko)
3349{
3350 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3351 kfree(mddev);
3352}
3353
3354static struct sysfs_ops md_sysfs_ops = {
3355 .show = md_attr_show,
3356 .store = md_attr_store,
3357};
3358static struct kobj_type md_ktype = {
3359 .release = md_free,
3360 .sysfs_ops = &md_sysfs_ops,
3361 .default_attrs = md_default_attrs,
3362};
3363
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364int mdp_major = 0;
3365
3366static struct kobject *md_probe(dev_t dev, int *part, void *data)
3367{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003368 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 mddev_t *mddev = mddev_find(dev);
3370 struct gendisk *disk;
3371 int partitioned = (MAJOR(dev) != MD_MAJOR);
3372 int shift = partitioned ? MdpMinorShift : 0;
3373 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003374 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 if (!mddev)
3377 return NULL;
3378
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003379 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003381 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 mddev_put(mddev);
3383 return NULL;
3384 }
3385 disk = alloc_disk(1 << shift);
3386 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003387 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 mddev_put(mddev);
3389 return NULL;
3390 }
3391 disk->major = MAJOR(dev);
3392 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003393 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003395 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 disk->fops = &md_fops;
3398 disk->private_data = mddev;
3399 disk->queue = mddev->queue;
3400 add_disk(disk);
3401 mddev->gendisk = disk;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003402 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003403 "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003404 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003405 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003406 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3407 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003408 else
3409 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return NULL;
3411}
3412
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413static void md_safemode_timeout(unsigned long data)
3414{
3415 mddev_t *mddev = (mddev_t *) data;
3416
Neil Brown0fd62b82008-06-28 08:31:36 +10003417 if (!atomic_read(&mddev->writes_pending)) {
3418 mddev->safemode = 1;
3419 if (mddev->external)
3420 sysfs_notify(&mddev->kobj, NULL, "array_state");
3421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 md_wakeup_thread(mddev->thread);
3423}
3424
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003425static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427static int do_md_run(mddev_t * mddev)
3428{
NeilBrown2604b702006-01-06 00:20:36 -08003429 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 int chunk_size;
3431 struct list_head *tmp;
3432 mdk_rdev_t *rdev;
3433 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003434 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 char b[BDEVNAME_SIZE];
3436
NeilBrowna757e642005-04-16 15:26:42 -07003437 if (list_empty(&mddev->disks))
3438 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
3441 if (mddev->pers)
3442 return -EBUSY;
3443
3444 /*
3445 * Analyze all RAID superblock(s)
3446 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003447 if (!mddev->raid_disks) {
3448 if (!mddev->persistent)
3449 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003450 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
3453 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454
NeilBrown2604b702006-01-06 00:20:36 -08003455 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 if (chunk_size > MAX_CHUNK_SIZE) {
3457 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3458 chunk_size, MAX_CHUNK_SIZE);
3459 return -EINVAL;
3460 }
3461 /*
3462 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3463 */
3464 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003465 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 return -EINVAL;
3467 }
3468 if (chunk_size < PAGE_SIZE) {
3469 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3470 chunk_size, PAGE_SIZE);
3471 return -EINVAL;
3472 }
3473
3474 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003475 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003476 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 continue;
3478 if (rdev->size < chunk_size / 1024) {
3479 printk(KERN_WARNING
3480 "md: Dev %s smaller than chunk_size:"
3481 " %lluk < %dk\n",
3482 bdevname(rdev->bdev,b),
3483 (unsigned long long)rdev->size,
3484 chunk_size / 1024);
3485 return -EINVAL;
3486 }
3487 }
3488 }
3489
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003491 if (mddev->level != LEVEL_NONE)
3492 request_module("md-level-%d", mddev->level);
3493 else if (mddev->clevel[0])
3494 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495#endif
3496
3497 /*
3498 * Drop all container device buffers, from now on
3499 * the only valid external interface is through the md
3500 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003502 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003503 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 continue;
3505 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003506 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003507
3508 /* perform some consistency tests on the device.
3509 * We don't want the data to overlap the metadata,
3510 * Internal Bitmap issues has handled elsewhere.
3511 */
3512 if (rdev->data_offset < rdev->sb_offset) {
3513 if (mddev->size &&
3514 rdev->data_offset + mddev->size*2
3515 > rdev->sb_offset*2) {
3516 printk("md: %s: data overlaps metadata\n",
3517 mdname(mddev));
3518 return -EINVAL;
3519 }
3520 } else {
3521 if (rdev->sb_offset*2 + rdev->sb_size/512
3522 > rdev->data_offset) {
3523 printk("md: %s: metadata overlaps data\n",
3524 mdname(mddev));
3525 return -EINVAL;
3526 }
3527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 }
3529
3530 md_probe(mddev->unit, NULL, NULL);
3531 disk = mddev->gendisk;
3532 if (!disk)
3533 return -ENOMEM;
3534
3535 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003536 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003537 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003539 if (mddev->level != LEVEL_NONE)
3540 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3541 mddev->level);
3542 else
3543 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3544 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 return -EINVAL;
3546 }
NeilBrown2604b702006-01-06 00:20:36 -08003547 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003549 mddev->level = pers->level;
3550 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551
NeilBrownf6705572006-03-27 01:18:11 -08003552 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003553 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003554 /* This personality cannot handle reshaping... */
3555 mddev->pers = NULL;
3556 module_put(pers->owner);
3557 return -EINVAL;
3558 }
3559
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003560 if (pers->sync_request) {
3561 /* Warn if this is a potentially silly
3562 * configuration.
3563 */
3564 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3565 mdk_rdev_t *rdev2;
3566 struct list_head *tmp2;
3567 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003568 rdev_for_each(rdev, tmp, mddev) {
3569 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003570 if (rdev < rdev2 &&
3571 rdev->bdev->bd_contains ==
3572 rdev2->bdev->bd_contains) {
3573 printk(KERN_WARNING
3574 "%s: WARNING: %s appears to be"
3575 " on the same physical disk as"
3576 " %s.\n",
3577 mdname(mddev),
3578 bdevname(rdev->bdev,b),
3579 bdevname(rdev2->bdev,b2));
3580 warned = 1;
3581 }
3582 }
3583 }
3584 if (warned)
3585 printk(KERN_WARNING
3586 "True protection against single-disk"
3587 " failure might be compromised.\n");
3588 }
3589
NeilBrown657390d2005-08-26 18:34:16 -07003590 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003592 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003593 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
NeilBrownf91de922005-11-08 21:39:36 -08003595 if (start_readonly)
3596 mddev->ro = 2; /* read-only, but switch on first write */
3597
NeilBrownb15c2e52006-01-06 00:20:16 -08003598 err = mddev->pers->run(mddev);
3599 if (!err && mddev->pers->sync_request) {
3600 err = bitmap_create(mddev);
3601 if (err) {
3602 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3603 mdname(mddev), err);
3604 mddev->pers->stop(mddev);
3605 }
3606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 if (err) {
3608 printk(KERN_ERR "md: pers->run() failed ...\n");
3609 module_put(mddev->pers->owner);
3610 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003611 bitmap_destroy(mddev);
3612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003614 if (mddev->pers->sync_request) {
3615 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3616 printk(KERN_WARNING
3617 "md: cannot register extra attributes for %s\n",
3618 mdname(mddev));
3619 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003620 mddev->ro = 0;
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 atomic_set(&mddev->writes_pending,0);
3623 mddev->safemode = 0;
3624 mddev->safemode_timer.function = md_safemode_timeout;
3625 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003626 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003628
NeilBrownd089c6a2008-02-06 01:39:59 -08003629 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003630 if (rdev->raid_disk >= 0) {
3631 char nm[20];
3632 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003633 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3634 printk("md: cannot register %s for %s\n",
3635 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637
3638 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3639
NeilBrown850b2b422006-10-03 01:15:46 -07003640 if (mddev->flags)
3641 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
3643 set_capacity(disk, mddev->array_size<<1);
3644
3645 /* If we call blk_queue_make_request here, it will
3646 * re-initialise max_sectors etc which may have been
3647 * refined inside -> run. So just set the bits we need to set.
3648 * Most initialisation happended when we called
3649 * blk_queue_make_request(..., md_fail_request)
3650 * earlier.
3651 */
3652 mddev->queue->queuedata = mddev;
3653 mddev->queue->make_request_fn = mddev->pers->make_request;
3654
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003655 /* If there is a partially-recovered drive we need to
3656 * start recovery here. If we leave it to md_check_recovery,
3657 * it will remove the drives and not do the right thing
3658 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003659 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003660 struct list_head *rtmp;
3661 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003662 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003663 if (rdev->raid_disk >= 0 &&
3664 !test_bit(In_sync, &rdev->flags) &&
3665 !test_bit(Faulty, &rdev->flags))
3666 /* complete an interrupted recovery */
3667 spares++;
3668 if (spares && mddev->pers->sync_request) {
3669 mddev->recovery = 0;
3670 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3671 mddev->sync_thread = md_register_thread(md_do_sync,
3672 mddev,
3673 "%s_resync");
3674 if (!mddev->sync_thread) {
3675 printk(KERN_ERR "%s: could not start resync"
3676 " thread...\n",
3677 mdname(mddev));
3678 /* leave the spares where they are, it shouldn't hurt */
3679 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003680 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003681 }
3682 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003683 md_wakeup_thread(mddev->thread);
3684 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003685
Linus Torvalds44ce62942007-05-09 18:51:36 -07003686 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003687 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003688 sysfs_notify(&mddev->kobj, NULL, "array_state");
Neil Brown72a23c22008-06-28 08:31:41 +10003689 sysfs_notify(&mddev->kobj, NULL, "sync_action");
Neil Browna99ac972008-06-28 08:31:43 +10003690 sysfs_notify(&mddev->kobj, NULL, "degraded");
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003691 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return 0;
3693}
3694
3695static int restart_array(mddev_t *mddev)
3696{
3697 struct gendisk *disk = mddev->gendisk;
3698 int err;
3699
3700 /*
3701 * Complain if it has no devices
3702 */
3703 err = -ENXIO;
3704 if (list_empty(&mddev->disks))
3705 goto out;
3706
3707 if (mddev->pers) {
3708 err = -EBUSY;
3709 if (!mddev->ro)
3710 goto out;
3711
3712 mddev->safemode = 0;
3713 mddev->ro = 0;
3714 set_disk_ro(disk, 0);
3715
3716 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3717 mdname(mddev));
3718 /*
3719 * Kick recovery or resync if necessary
3720 */
3721 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3722 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003723 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 err = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10003725 sysfs_notify(&mddev->kobj, NULL, "array_state");
3726
NeilBrown9e653b62006-06-26 00:27:58 -07003727 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
3730out:
3731 return err;
3732}
3733
NeilBrownacc55e22006-06-26 00:27:47 -07003734/* similar to deny_write_access, but accounts for our holding a reference
3735 * to the file ourselves */
3736static int deny_bitmap_write_access(struct file * file)
3737{
3738 struct inode *inode = file->f_mapping->host;
3739
3740 spin_lock(&inode->i_lock);
3741 if (atomic_read(&inode->i_writecount) > 1) {
3742 spin_unlock(&inode->i_lock);
3743 return -ETXTBSY;
3744 }
3745 atomic_set(&inode->i_writecount, -1);
3746 spin_unlock(&inode->i_lock);
3747
3748 return 0;
3749}
3750
3751static void restore_bitmap_write_access(struct file *file)
3752{
3753 struct inode *inode = file->f_mapping->host;
3754
3755 spin_lock(&inode->i_lock);
3756 atomic_set(&inode->i_writecount, 1);
3757 spin_unlock(&inode->i_lock);
3758}
3759
NeilBrown9e653b62006-06-26 00:27:58 -07003760/* mode:
3761 * 0 - completely stop and dis-assemble array
3762 * 1 - switch to readonly
3763 * 2 - stop but do not disassemble array
3764 */
3765static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766{
3767 int err = 0;
3768 struct gendisk *disk = mddev->gendisk;
3769
3770 if (mddev->pers) {
3771 if (atomic_read(&mddev->active)>2) {
3772 printk("md: %s still in use.\n",mdname(mddev));
3773 return -EBUSY;
3774 }
3775
3776 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003777 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3779 md_unregister_thread(mddev->sync_thread);
3780 mddev->sync_thread = NULL;
3781 }
3782
3783 del_timer_sync(&mddev->safemode_timer);
3784
3785 invalidate_partition(disk, 0);
3786
NeilBrown9e653b62006-06-26 00:27:58 -07003787 switch(mode) {
3788 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003790 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 goto out;
3792 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003793 break;
3794 case 0: /* disassemble */
3795 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003796 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003797 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 if (mddev->ro)
3799 set_disk_ro(disk, 0);
3800 blk_queue_make_request(mddev->queue, md_fail_request);
3801 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003802 mddev->queue->merge_bvec_fn = NULL;
3803 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003804 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003805 if (mddev->pers->sync_request)
3806 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3807
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 module_put(mddev->pers->owner);
3809 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003810 /* tell userspace to handle 'inactive' */
3811 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003812
3813 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003814 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 if (mddev->ro)
3817 mddev->ro = 0;
3818 }
NeilBrown850b2b422006-10-03 01:15:46 -07003819 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 /* mark array as shutdown cleanly */
3821 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07003822 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 }
NeilBrown9e653b62006-06-26 00:27:58 -07003824 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003826 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
NeilBrown32a76272005-06-21 17:17:14 -07003828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 /*
3830 * Free resources if final stop
3831 */
NeilBrown9e653b62006-06-26 00:27:58 -07003832 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003833 mdk_rdev_t *rdev;
3834 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3837
NeilBrown978f9462006-02-02 14:28:05 -08003838 bitmap_destroy(mddev);
3839 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003840 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003841 fput(mddev->bitmap_file);
3842 mddev->bitmap_file = NULL;
3843 }
3844 mddev->bitmap_offset = 0;
3845
NeilBrownd089c6a2008-02-06 01:39:59 -08003846 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003847 if (rdev->raid_disk >= 0) {
3848 char nm[20];
3849 sprintf(nm, "rd%d", rdev->raid_disk);
3850 sysfs_remove_link(&mddev->kobj, nm);
3851 }
3852
NeilBrown177a99b2008-02-06 01:39:56 -08003853 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003854 flush_scheduled_work();
3855
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 export_array(mddev);
3857
3858 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003859 mddev->size = 0;
3860 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003861 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003862 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003863 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003864 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003865 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003866 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003867 mddev->level = LEVEL_NONE;
3868 mddev->clevel[0] = 0;
3869 mddev->flags = 0;
3870 mddev->ro = 0;
3871 mddev->metadata_type[0] = 0;
3872 mddev->chunk_size = 0;
3873 mddev->ctime = mddev->utime = 0;
3874 mddev->layout = 0;
3875 mddev->max_disks = 0;
3876 mddev->events = 0;
3877 mddev->delta_disks = 0;
3878 mddev->new_level = LEVEL_NONE;
3879 mddev->new_layout = 0;
3880 mddev->new_chunk = 0;
3881 mddev->curr_resync = 0;
3882 mddev->resync_mismatches = 0;
3883 mddev->suspend_lo = mddev->suspend_hi = 0;
3884 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3885 mddev->recovery = 0;
3886 mddev->in_sync = 0;
3887 mddev->changed = 0;
3888 mddev->degraded = 0;
3889 mddev->barriers_work = 0;
3890 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003891
NeilBrowna8a55c32006-06-26 00:27:39 -07003892 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3894 mdname(mddev));
3895 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003896 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003897 sysfs_notify(&mddev->kobj, NULL, "array_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898out:
3899 return err;
3900}
3901
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003902#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903static void autorun_array(mddev_t *mddev)
3904{
3905 mdk_rdev_t *rdev;
3906 struct list_head *tmp;
3907 int err;
3908
NeilBrowna757e642005-04-16 15:26:42 -07003909 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
3912 printk(KERN_INFO "md: running: ");
3913
NeilBrownd089c6a2008-02-06 01:39:59 -08003914 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 char b[BDEVNAME_SIZE];
3916 printk("<%s>", bdevname(rdev->bdev,b));
3917 }
3918 printk("\n");
3919
3920 err = do_md_run (mddev);
3921 if (err) {
3922 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3923 do_md_stop (mddev, 0);
3924 }
3925}
3926
3927/*
3928 * lets try to run arrays based on all disks that have arrived
3929 * until now. (those are in pending_raid_disks)
3930 *
3931 * the method: pick the first pending disk, collect all disks with
3932 * the same UUID, remove all from the pending list and put them into
3933 * the 'same_array' list. Then order this list based on superblock
3934 * update time (freshest comes first), kick out 'old' disks and
3935 * compare superblocks. If everything's fine then run it.
3936 *
3937 * If "unit" is allocated, then bump its reference count
3938 */
3939static void autorun_devices(int part)
3940{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 struct list_head *tmp;
3942 mdk_rdev_t *rdev0, *rdev;
3943 mddev_t *mddev;
3944 char b[BDEVNAME_SIZE];
3945
3946 printk(KERN_INFO "md: autorun ...\n");
3947 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003948 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003950 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 rdev0 = list_entry(pending_raid_disks.next,
3952 mdk_rdev_t, same_set);
3953
3954 printk(KERN_INFO "md: considering %s ...\n",
3955 bdevname(rdev0->bdev,b));
3956 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003957 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 if (super_90_load(rdev, rdev0, 0) >= 0) {
3959 printk(KERN_INFO "md: adding %s ...\n",
3960 bdevname(rdev->bdev,b));
3961 list_move(&rdev->same_set, &candidates);
3962 }
3963 /*
3964 * now we have a set of devices, with all of them having
3965 * mostly sane superblocks. It's time to allocate the
3966 * mddev.
3967 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003968 if (part) {
3969 dev = MKDEV(mdp_major,
3970 rdev0->preferred_minor << MdpMinorShift);
3971 unit = MINOR(dev) >> MdpMinorShift;
3972 } else {
3973 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3974 unit = MINOR(dev);
3975 }
3976 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3978 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3979 break;
3980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
3982 md_probe(dev, NULL, NULL);
3983 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10003984 if (!mddev || !mddev->gendisk) {
3985 if (mddev)
3986 mddev_put(mddev);
3987 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 "md: cannot allocate memory for md drive.\n");
3989 break;
3990 }
3991 if (mddev_lock(mddev))
3992 printk(KERN_WARNING "md: %s locked, cannot run\n",
3993 mdname(mddev));
3994 else if (mddev->raid_disks || mddev->major_version
3995 || !list_empty(&mddev->disks)) {
3996 printk(KERN_WARNING
3997 "md: %s already running, cannot run %s\n",
3998 mdname(mddev), bdevname(rdev0->bdev,b));
3999 mddev_unlock(mddev);
4000 } else {
4001 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004002 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08004003 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 list_del_init(&rdev->same_set);
4005 if (bind_rdev_to_array(rdev, mddev))
4006 export_rdev(rdev);
4007 }
4008 autorun_array(mddev);
4009 mddev_unlock(mddev);
4010 }
4011 /* on success, candidates will be empty, on error
4012 * it won't...
4013 */
NeilBrown73c34432008-02-06 01:39:59 -08004014 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 export_rdev(rdev);
4016 mddev_put(mddev);
4017 }
4018 printk(KERN_INFO "md: ... autorun DONE.\n");
4019}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004020#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022static int get_version(void __user * arg)
4023{
4024 mdu_version_t ver;
4025
4026 ver.major = MD_MAJOR_VERSION;
4027 ver.minor = MD_MINOR_VERSION;
4028 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4029
4030 if (copy_to_user(arg, &ver, sizeof(ver)))
4031 return -EFAULT;
4032
4033 return 0;
4034}
4035
4036static int get_array_info(mddev_t * mddev, void __user * arg)
4037{
4038 mdu_array_info_t info;
4039 int nr,working,active,failed,spare;
4040 mdk_rdev_t *rdev;
4041 struct list_head *tmp;
4042
4043 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004044 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004046 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 failed++;
4048 else {
4049 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004050 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 active++;
4052 else
4053 spare++;
4054 }
4055 }
4056
4057 info.major_version = mddev->major_version;
4058 info.minor_version = mddev->minor_version;
4059 info.patch_version = MD_PATCHLEVEL_VERSION;
4060 info.ctime = mddev->ctime;
4061 info.level = mddev->level;
4062 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004063 if (info.size != mddev->size) /* overflow */
4064 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 info.nr_disks = nr;
4066 info.raid_disks = mddev->raid_disks;
4067 info.md_minor = mddev->md_minor;
4068 info.not_persistent= !mddev->persistent;
4069
4070 info.utime = mddev->utime;
4071 info.state = 0;
4072 if (mddev->in_sync)
4073 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004074 if (mddev->bitmap && mddev->bitmap_offset)
4075 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 info.active_disks = active;
4077 info.working_disks = working;
4078 info.failed_disks = failed;
4079 info.spare_disks = spare;
4080
4081 info.layout = mddev->layout;
4082 info.chunk_size = mddev->chunk_size;
4083
4084 if (copy_to_user(arg, &info, sizeof(info)))
4085 return -EFAULT;
4086
4087 return 0;
4088}
4089
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004090static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004091{
4092 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4093 char *ptr, *buf = NULL;
4094 int err = -ENOMEM;
4095
NeilBrown2a2275d2007-01-26 00:57:11 -08004096 md_allow_write(mddev);
4097
NeilBrown32a76272005-06-21 17:17:14 -07004098 file = kmalloc(sizeof(*file), GFP_KERNEL);
4099 if (!file)
4100 goto out;
4101
4102 /* bitmap disabled, zero the first byte and copy out */
4103 if (!mddev->bitmap || !mddev->bitmap->file) {
4104 file->pathname[0] = '\0';
4105 goto copy_out;
4106 }
4107
4108 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4109 if (!buf)
4110 goto out;
4111
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004112 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4113 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004114 goto out;
4115
4116 strcpy(file->pathname, ptr);
4117
4118copy_out:
4119 err = 0;
4120 if (copy_to_user(arg, file, sizeof(*file)))
4121 err = -EFAULT;
4122out:
4123 kfree(buf);
4124 kfree(file);
4125 return err;
4126}
4127
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128static int get_disk_info(mddev_t * mddev, void __user * arg)
4129{
4130 mdu_disk_info_t info;
4131 unsigned int nr;
4132 mdk_rdev_t *rdev;
4133
4134 if (copy_from_user(&info, arg, sizeof(info)))
4135 return -EFAULT;
4136
4137 nr = info.number;
4138
4139 rdev = find_rdev_nr(mddev, nr);
4140 if (rdev) {
4141 info.major = MAJOR(rdev->bdev->bd_dev);
4142 info.minor = MINOR(rdev->bdev->bd_dev);
4143 info.raid_disk = rdev->raid_disk;
4144 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004145 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004147 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 info.state |= (1<<MD_DISK_ACTIVE);
4149 info.state |= (1<<MD_DISK_SYNC);
4150 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004151 if (test_bit(WriteMostly, &rdev->flags))
4152 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 } else {
4154 info.major = info.minor = 0;
4155 info.raid_disk = -1;
4156 info.state = (1<<MD_DISK_REMOVED);
4157 }
4158
4159 if (copy_to_user(arg, &info, sizeof(info)))
4160 return -EFAULT;
4161
4162 return 0;
4163}
4164
4165static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4166{
4167 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4168 mdk_rdev_t *rdev;
4169 dev_t dev = MKDEV(info->major,info->minor);
4170
4171 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4172 return -EOVERFLOW;
4173
4174 if (!mddev->raid_disks) {
4175 int err;
4176 /* expecting a device which has a superblock */
4177 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4178 if (IS_ERR(rdev)) {
4179 printk(KERN_WARNING
4180 "md: md_import_device returned %ld\n",
4181 PTR_ERR(rdev));
4182 return PTR_ERR(rdev);
4183 }
4184 if (!list_empty(&mddev->disks)) {
4185 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4186 mdk_rdev_t, same_set);
4187 int err = super_types[mddev->major_version]
4188 .load_super(rdev, rdev0, mddev->minor_version);
4189 if (err < 0) {
4190 printk(KERN_WARNING
4191 "md: %s has different UUID to %s\n",
4192 bdevname(rdev->bdev,b),
4193 bdevname(rdev0->bdev,b2));
4194 export_rdev(rdev);
4195 return -EINVAL;
4196 }
4197 }
4198 err = bind_rdev_to_array(rdev, mddev);
4199 if (err)
4200 export_rdev(rdev);
4201 return err;
4202 }
4203
4204 /*
4205 * add_new_disk can be used once the array is assembled
4206 * to add "hot spares". They must already have a superblock
4207 * written
4208 */
4209 if (mddev->pers) {
4210 int err;
4211 if (!mddev->pers->hot_add_disk) {
4212 printk(KERN_WARNING
4213 "%s: personality does not support diskops!\n",
4214 mdname(mddev));
4215 return -EINVAL;
4216 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004217 if (mddev->persistent)
4218 rdev = md_import_device(dev, mddev->major_version,
4219 mddev->minor_version);
4220 else
4221 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 if (IS_ERR(rdev)) {
4223 printk(KERN_WARNING
4224 "md: md_import_device returned %ld\n",
4225 PTR_ERR(rdev));
4226 return PTR_ERR(rdev);
4227 }
NeilBrown41158c72005-06-21 17:17:25 -07004228 /* set save_raid_disk if appropriate */
4229 if (!mddev->persistent) {
4230 if (info->state & (1<<MD_DISK_SYNC) &&
4231 info->raid_disk < mddev->raid_disks)
4232 rdev->raid_disk = info->raid_disk;
4233 else
4234 rdev->raid_disk = -1;
4235 } else
4236 super_types[mddev->major_version].
4237 validate_super(mddev, rdev);
4238 rdev->saved_raid_disk = rdev->raid_disk;
4239
NeilBrownb2d444d2005-11-08 21:39:31 -08004240 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004241 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4242 set_bit(WriteMostly, &rdev->flags);
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 rdev->raid_disk = -1;
4245 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004246 if (!err && !mddev->pers->hot_remove_disk) {
4247 /* If there is hot_add_disk but no hot_remove_disk
4248 * then added disks for geometry changes,
4249 * and should be added immediately.
4250 */
4251 super_types[mddev->major_version].
4252 validate_super(mddev, rdev);
4253 err = mddev->pers->hot_add_disk(mddev, rdev);
4254 if (err)
4255 unbind_rdev_from_array(rdev);
4256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 if (err)
4258 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004259
NeilBrown17571282006-12-10 02:20:52 -08004260 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004261 if (mddev->degraded)
4262 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004263 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004264 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return err;
4266 }
4267
4268 /* otherwise, add_new_disk is only allowed
4269 * for major_version==0 superblocks
4270 */
4271 if (mddev->major_version != 0) {
4272 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4273 mdname(mddev));
4274 return -EINVAL;
4275 }
4276
4277 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4278 int err;
4279 rdev = md_import_device (dev, -1, 0);
4280 if (IS_ERR(rdev)) {
4281 printk(KERN_WARNING
4282 "md: error, md_import_device() returned %ld\n",
4283 PTR_ERR(rdev));
4284 return PTR_ERR(rdev);
4285 }
4286 rdev->desc_nr = info->number;
4287 if (info->raid_disk < mddev->raid_disks)
4288 rdev->raid_disk = info->raid_disk;
4289 else
4290 rdev->raid_disk = -1;
4291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004293 if (info->state & (1<<MD_DISK_SYNC))
4294 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004296 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4297 set_bit(WriteMostly, &rdev->flags);
4298
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 if (!mddev->persistent) {
4300 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4301 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4302 } else
4303 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4304 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4305
NeilBrown2bf071b2006-01-06 00:20:55 -08004306 err = bind_rdev_to_array(rdev, mddev);
4307 if (err) {
4308 export_rdev(rdev);
4309 return err;
4310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 }
4312
4313 return 0;
4314}
4315
4316static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4317{
4318 char b[BDEVNAME_SIZE];
4319 mdk_rdev_t *rdev;
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 rdev = find_rdev(mddev, dev);
4322 if (!rdev)
4323 return -ENXIO;
4324
4325 if (rdev->raid_disk >= 0)
4326 goto busy;
4327
4328 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07004329 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004330 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331
4332 return 0;
4333busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004334 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 bdevname(rdev->bdev,b), mdname(mddev));
4336 return -EBUSY;
4337}
4338
4339static int hot_add_disk(mddev_t * mddev, dev_t dev)
4340{
4341 char b[BDEVNAME_SIZE];
4342 int err;
4343 unsigned int size;
4344 mdk_rdev_t *rdev;
4345
4346 if (!mddev->pers)
4347 return -ENODEV;
4348
4349 if (mddev->major_version != 0) {
4350 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4351 " version-0 superblocks.\n",
4352 mdname(mddev));
4353 return -EINVAL;
4354 }
4355 if (!mddev->pers->hot_add_disk) {
4356 printk(KERN_WARNING
4357 "%s: personality does not support diskops!\n",
4358 mdname(mddev));
4359 return -EINVAL;
4360 }
4361
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 -EINVAL;
4368 }
4369
4370 if (mddev->persistent)
4371 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4372 else
4373 rdev->sb_offset =
4374 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4375
4376 size = calc_dev_size(rdev, mddev->chunk_size);
4377 rdev->size = size;
4378
NeilBrownb2d444d2005-11-08 21:39:31 -08004379 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 printk(KERN_WARNING
4381 "md: can not hot-add faulty %s disk to %s!\n",
4382 bdevname(rdev->bdev,b), mdname(mddev));
4383 err = -EINVAL;
4384 goto abort_export;
4385 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004386 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004388 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004389 err = bind_rdev_to_array(rdev, mddev);
4390 if (err)
4391 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
4393 /*
4394 * The rest should better be atomic, we can have disk failures
4395 * noticed in interrupt contexts ...
4396 */
4397
4398 if (rdev->desc_nr == mddev->max_disks) {
4399 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4400 mdname(mddev));
4401 err = -EBUSY;
4402 goto abort_unbind_export;
4403 }
4404
4405 rdev->raid_disk = -1;
4406
NeilBrown850b2b422006-10-03 01:15:46 -07004407 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408
4409 /*
4410 * Kick recovery, maybe this spare has to be added to the
4411 * array immediately.
4412 */
4413 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4414 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004415 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return 0;
4417
4418abort_unbind_export:
4419 unbind_rdev_from_array(rdev);
4420
4421abort_export:
4422 export_rdev(rdev);
4423 return err;
4424}
4425
NeilBrown32a76272005-06-21 17:17:14 -07004426static int set_bitmap_file(mddev_t *mddev, int fd)
4427{
4428 int err;
4429
NeilBrown36fa3062005-09-09 16:23:45 -07004430 if (mddev->pers) {
4431 if (!mddev->pers->quiesce)
4432 return -EBUSY;
4433 if (mddev->recovery || mddev->sync_thread)
4434 return -EBUSY;
4435 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004436 }
4437
NeilBrown36fa3062005-09-09 16:23:45 -07004438
4439 if (fd >= 0) {
4440 if (mddev->bitmap)
4441 return -EEXIST; /* cannot add when bitmap is present */
4442 mddev->bitmap_file = fget(fd);
4443
4444 if (mddev->bitmap_file == NULL) {
4445 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4446 mdname(mddev));
4447 return -EBADF;
4448 }
4449
4450 err = deny_bitmap_write_access(mddev->bitmap_file);
4451 if (err) {
4452 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4453 mdname(mddev));
4454 fput(mddev->bitmap_file);
4455 mddev->bitmap_file = NULL;
4456 return err;
4457 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004458 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004459 } else if (mddev->bitmap == NULL)
4460 return -ENOENT; /* cannot remove what isn't there */
4461 err = 0;
4462 if (mddev->pers) {
4463 mddev->pers->quiesce(mddev, 1);
4464 if (fd >= 0)
4465 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004466 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004467 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004468 fd = -1; /* make sure to put the file */
4469 }
NeilBrown36fa3062005-09-09 16:23:45 -07004470 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004471 }
4472 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004473 if (mddev->bitmap_file) {
4474 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004475 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004476 }
NeilBrown36fa3062005-09-09 16:23:45 -07004477 mddev->bitmap_file = NULL;
4478 }
4479
NeilBrown32a76272005-06-21 17:17:14 -07004480 return err;
4481}
4482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483/*
4484 * set_array_info is used two different ways
4485 * The original usage is when creating a new array.
4486 * In this usage, raid_disks is > 0 and it together with
4487 * level, size, not_persistent,layout,chunksize determine the
4488 * shape of the array.
4489 * This will always create an array with a type-0.90.0 superblock.
4490 * The newer usage is when assembling an array.
4491 * In this case raid_disks will be 0, and the major_version field is
4492 * use to determine which style super-blocks are to be found on the devices.
4493 * The minor and patch _version numbers are also kept incase the
4494 * super_block handler wishes to interpret them.
4495 */
4496static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4497{
4498
4499 if (info->raid_disks == 0) {
4500 /* just setting version number for superblock loading */
4501 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004502 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 super_types[info->major_version].name == NULL) {
4504 /* maybe try to auto-load a module? */
4505 printk(KERN_INFO
4506 "md: superblock version %d not known\n",
4507 info->major_version);
4508 return -EINVAL;
4509 }
4510 mddev->major_version = info->major_version;
4511 mddev->minor_version = info->minor_version;
4512 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004513 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 return 0;
4515 }
4516 mddev->major_version = MD_MAJOR_VERSION;
4517 mddev->minor_version = MD_MINOR_VERSION;
4518 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4519 mddev->ctime = get_seconds();
4520
4521 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004522 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 mddev->size = info->size;
4524 mddev->raid_disks = info->raid_disks;
4525 /* don't set md_minor, it is determined by which /dev/md* was
4526 * openned
4527 */
4528 if (info->state & (1<<MD_SB_CLEAN))
4529 mddev->recovery_cp = MaxSector;
4530 else
4531 mddev->recovery_cp = 0;
4532 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004533 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
4535 mddev->layout = info->layout;
4536 mddev->chunk_size = info->chunk_size;
4537
4538 mddev->max_disks = MD_SB_DISKS;
4539
NeilBrowne6910632008-02-06 01:39:51 -08004540 if (mddev->persistent)
4541 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07004542 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
NeilBrownb2a27032005-11-28 13:44:12 -08004544 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4545 mddev->bitmap_offset = 0;
4546
NeilBrownf6705572006-03-27 01:18:11 -08004547 mddev->reshape_position = MaxSector;
4548
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 /*
4550 * Generate a 128 bit UUID
4551 */
4552 get_random_bytes(mddev->uuid, 16);
4553
NeilBrownf6705572006-03-27 01:18:11 -08004554 mddev->new_level = mddev->level;
4555 mddev->new_chunk = mddev->chunk_size;
4556 mddev->new_layout = mddev->layout;
4557 mddev->delta_disks = 0;
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 return 0;
4560}
4561
NeilBrowna35b0d62006-01-06 00:20:49 -08004562static int update_size(mddev_t *mddev, unsigned long size)
4563{
4564 mdk_rdev_t * rdev;
4565 int rv;
4566 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004567 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004568
4569 if (mddev->pers->resize == NULL)
4570 return -EINVAL;
4571 /* The "size" is the amount of each device that is used.
4572 * This can only make sense for arrays with redundancy.
4573 * linear and raid0 always use whatever space is available
4574 * We can only consider changing the size if no resync
4575 * or reconstruction is happening, and if the new size
4576 * is acceptable. It must fit before the sb_offset or,
4577 * if that is <data_offset, it must fit before the
4578 * size of each device.
4579 * If size is zero, we find the largest size that fits.
4580 */
4581 if (mddev->sync_thread)
4582 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004583 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004584 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004585 avail = rdev->size * 2;
4586
NeilBrowna35b0d62006-01-06 00:20:49 -08004587 if (fit && (size == 0 || size > avail/2))
4588 size = avail/2;
4589 if (avail < ((sector_t)size << 1))
4590 return -ENOSPC;
4591 }
4592 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4593 if (!rv) {
4594 struct block_device *bdev;
4595
4596 bdev = bdget_disk(mddev->gendisk, 0);
4597 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004598 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004599 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004600 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004601 bdput(bdev);
4602 }
4603 }
4604 return rv;
4605}
4606
NeilBrownda943b992006-01-06 00:20:54 -08004607static int update_raid_disks(mddev_t *mddev, int raid_disks)
4608{
4609 int rv;
4610 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004611 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004612 return -EINVAL;
4613 if (raid_disks <= 0 ||
4614 raid_disks >= mddev->max_disks)
4615 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004616 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004617 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004618 mddev->delta_disks = raid_disks - mddev->raid_disks;
4619
4620 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004621 return rv;
4622}
4623
4624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625/*
4626 * update_array_info is used to change the configuration of an
4627 * on-line array.
4628 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4629 * fields in the info are checked against the array.
4630 * Any differences that cannot be handled will cause an error.
4631 * Normally, only one change can be managed at a time.
4632 */
4633static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4634{
4635 int rv = 0;
4636 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004637 int state = 0;
4638
4639 /* calculate expected state,ignoring low bits */
4640 if (mddev->bitmap && mddev->bitmap_offset)
4641 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642
4643 if (mddev->major_version != info->major_version ||
4644 mddev->minor_version != info->minor_version ||
4645/* mddev->patch_version != info->patch_version || */
4646 mddev->ctime != info->ctime ||
4647 mddev->level != info->level ||
4648/* mddev->layout != info->layout || */
4649 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004650 mddev->chunk_size != info->chunk_size ||
4651 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4652 ((state^info->state) & 0xfffffe00)
4653 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 return -EINVAL;
4655 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004656 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (mddev->raid_disks != info->raid_disks) cnt++;
4658 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004659 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 if (cnt == 0) return 0;
4661 if (cnt > 1) return -EINVAL;
4662
4663 if (mddev->layout != info->layout) {
4664 /* Change layout
4665 * we don't need to do anything at the md level, the
4666 * personality will take care of it all.
4667 */
4668 if (mddev->pers->reconfig == NULL)
4669 return -EINVAL;
4670 else
4671 return mddev->pers->reconfig(mddev, info->layout, -1);
4672 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004673 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004674 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
NeilBrownda943b992006-01-06 00:20:54 -08004676 if (mddev->raid_disks != info->raid_disks)
4677 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
NeilBrown36fa3062005-09-09 16:23:45 -07004679 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4680 if (mddev->pers->quiesce == NULL)
4681 return -EINVAL;
4682 if (mddev->recovery || mddev->sync_thread)
4683 return -EBUSY;
4684 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4685 /* add the bitmap */
4686 if (mddev->bitmap)
4687 return -EEXIST;
4688 if (mddev->default_bitmap_offset == 0)
4689 return -EINVAL;
4690 mddev->bitmap_offset = mddev->default_bitmap_offset;
4691 mddev->pers->quiesce(mddev, 1);
4692 rv = bitmap_create(mddev);
4693 if (rv)
4694 bitmap_destroy(mddev);
4695 mddev->pers->quiesce(mddev, 0);
4696 } else {
4697 /* remove the bitmap */
4698 if (!mddev->bitmap)
4699 return -ENOENT;
4700 if (mddev->bitmap->file)
4701 return -EINVAL;
4702 mddev->pers->quiesce(mddev, 1);
4703 bitmap_destroy(mddev);
4704 mddev->pers->quiesce(mddev, 0);
4705 mddev->bitmap_offset = 0;
4706 }
4707 }
NeilBrown850b2b422006-10-03 01:15:46 -07004708 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 return rv;
4710}
4711
4712static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4713{
4714 mdk_rdev_t *rdev;
4715
4716 if (mddev->pers == NULL)
4717 return -ENODEV;
4718
4719 rdev = find_rdev(mddev, dev);
4720 if (!rdev)
4721 return -ENODEV;
4722
4723 md_error(mddev, rdev);
4724 return 0;
4725}
4726
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004727static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4728{
4729 mddev_t *mddev = bdev->bd_disk->private_data;
4730
4731 geo->heads = 2;
4732 geo->sectors = 4;
4733 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4734 return 0;
4735}
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737static int md_ioctl(struct inode *inode, struct file *file,
4738 unsigned int cmd, unsigned long arg)
4739{
4740 int err = 0;
4741 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 mddev_t *mddev = NULL;
4743
4744 if (!capable(CAP_SYS_ADMIN))
4745 return -EACCES;
4746
4747 /*
4748 * Commands dealing with the RAID driver but not any
4749 * particular array:
4750 */
4751 switch (cmd)
4752 {
4753 case RAID_VERSION:
4754 err = get_version(argp);
4755 goto done;
4756
4757 case PRINT_RAID_DEBUG:
4758 err = 0;
4759 md_print_devices();
4760 goto done;
4761
4762#ifndef MODULE
4763 case RAID_AUTORUN:
4764 err = 0;
4765 autostart_arrays(arg);
4766 goto done;
4767#endif
4768 default:;
4769 }
4770
4771 /*
4772 * Commands creating/starting a new array:
4773 */
4774
4775 mddev = inode->i_bdev->bd_disk->private_data;
4776
4777 if (!mddev) {
4778 BUG();
4779 goto abort;
4780 }
4781
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 err = mddev_lock(mddev);
4783 if (err) {
4784 printk(KERN_INFO
4785 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4786 err, cmd);
4787 goto abort;
4788 }
4789
4790 switch (cmd)
4791 {
4792 case SET_ARRAY_INFO:
4793 {
4794 mdu_array_info_t info;
4795 if (!arg)
4796 memset(&info, 0, sizeof(info));
4797 else if (copy_from_user(&info, argp, sizeof(info))) {
4798 err = -EFAULT;
4799 goto abort_unlock;
4800 }
4801 if (mddev->pers) {
4802 err = update_array_info(mddev, &info);
4803 if (err) {
4804 printk(KERN_WARNING "md: couldn't update"
4805 " array info. %d\n", err);
4806 goto abort_unlock;
4807 }
4808 goto done_unlock;
4809 }
4810 if (!list_empty(&mddev->disks)) {
4811 printk(KERN_WARNING
4812 "md: array %s already has disks!\n",
4813 mdname(mddev));
4814 err = -EBUSY;
4815 goto abort_unlock;
4816 }
4817 if (mddev->raid_disks) {
4818 printk(KERN_WARNING
4819 "md: array %s already initialised!\n",
4820 mdname(mddev));
4821 err = -EBUSY;
4822 goto abort_unlock;
4823 }
4824 err = set_array_info(mddev, &info);
4825 if (err) {
4826 printk(KERN_WARNING "md: couldn't set"
4827 " array info. %d\n", err);
4828 goto abort_unlock;
4829 }
4830 }
4831 goto done_unlock;
4832
4833 default:;
4834 }
4835
4836 /*
4837 * Commands querying/configuring an existing array:
4838 */
NeilBrown32a76272005-06-21 17:17:14 -07004839 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004840 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004841 if ((!mddev->raid_disks && !mddev->external)
4842 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4843 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4844 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 err = -ENODEV;
4846 goto abort_unlock;
4847 }
4848
4849 /*
4850 * Commands even a read-only array can execute:
4851 */
4852 switch (cmd)
4853 {
4854 case GET_ARRAY_INFO:
4855 err = get_array_info(mddev, argp);
4856 goto done_unlock;
4857
NeilBrown32a76272005-06-21 17:17:14 -07004858 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004859 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004860 goto done_unlock;
4861
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 case GET_DISK_INFO:
4863 err = get_disk_info(mddev, argp);
4864 goto done_unlock;
4865
4866 case RESTART_ARRAY_RW:
4867 err = restart_array(mddev);
4868 goto done_unlock;
4869
4870 case STOP_ARRAY:
4871 err = do_md_stop (mddev, 0);
4872 goto done_unlock;
4873
4874 case STOP_ARRAY_RO:
4875 err = do_md_stop (mddev, 1);
4876 goto done_unlock;
4877
4878 /*
4879 * We have a problem here : there is no easy way to give a CHS
4880 * virtual geometry. We currently pretend that we have a 2 heads
4881 * 4 sectors (with a BIG number of cylinders...). This drives
4882 * dosfs just mad... ;-)
4883 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 }
4885
4886 /*
4887 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004888 * superblock, so we do not allow them on read-only arrays.
4889 * However non-MD ioctls (e.g. get-size) will still come through
4890 * here and hit the 'default' below, so only disallow
4891 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 */
NeilBrownf91de922005-11-08 21:39:36 -08004893 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4894 mddev->ro && mddev->pers) {
4895 if (mddev->ro == 2) {
4896 mddev->ro = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10004897 sysfs_notify(&mddev->kobj, NULL, "array_state");
4898 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4899 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08004900
4901 } else {
4902 err = -EROFS;
4903 goto abort_unlock;
4904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 }
4906
4907 switch (cmd)
4908 {
4909 case ADD_NEW_DISK:
4910 {
4911 mdu_disk_info_t info;
4912 if (copy_from_user(&info, argp, sizeof(info)))
4913 err = -EFAULT;
4914 else
4915 err = add_new_disk(mddev, &info);
4916 goto done_unlock;
4917 }
4918
4919 case HOT_REMOVE_DISK:
4920 err = hot_remove_disk(mddev, new_decode_dev(arg));
4921 goto done_unlock;
4922
4923 case HOT_ADD_DISK:
4924 err = hot_add_disk(mddev, new_decode_dev(arg));
4925 goto done_unlock;
4926
4927 case SET_DISK_FAULTY:
4928 err = set_disk_faulty(mddev, new_decode_dev(arg));
4929 goto done_unlock;
4930
4931 case RUN_ARRAY:
4932 err = do_md_run (mddev);
4933 goto done_unlock;
4934
NeilBrown32a76272005-06-21 17:17:14 -07004935 case SET_BITMAP_FILE:
4936 err = set_bitmap_file(mddev, (int)arg);
4937 goto done_unlock;
4938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 err = -EINVAL;
4941 goto abort_unlock;
4942 }
4943
4944done_unlock:
4945abort_unlock:
4946 mddev_unlock(mddev);
4947
4948 return err;
4949done:
4950 if (err)
4951 MD_BUG();
4952abort:
4953 return err;
4954}
4955
4956static int md_open(struct inode *inode, struct file *file)
4957{
4958 /*
4959 * Succeed if we can lock the mddev, which confirms that
4960 * it isn't being stopped right now.
4961 */
4962 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4963 int err;
4964
NeilBrownd63a5a72006-12-08 02:36:17 -08004965 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 goto out;
4967
4968 err = 0;
4969 mddev_get(mddev);
4970 mddev_unlock(mddev);
4971
4972 check_disk_change(inode->i_bdev);
4973 out:
4974 return err;
4975}
4976
4977static int md_release(struct inode *inode, struct file * file)
4978{
4979 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4980
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004981 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 mddev_put(mddev);
4983
4984 return 0;
4985}
4986
Linus Torvalds44ce62942007-05-09 18:51:36 -07004987static int md_media_changed(struct gendisk *disk)
4988{
4989 mddev_t *mddev = disk->private_data;
4990
4991 return mddev->changed;
4992}
4993
4994static int md_revalidate(struct gendisk *disk)
4995{
4996 mddev_t *mddev = disk->private_data;
4997
4998 mddev->changed = 0;
4999 return 0;
5000}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001static struct block_device_operations md_fops =
5002{
5003 .owner = THIS_MODULE,
5004 .open = md_open,
5005 .release = md_release,
5006 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005007 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005008 .media_changed = md_media_changed,
5009 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010};
5011
Adrian Bunk75c96f82005-05-05 16:16:09 -07005012static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013{
5014 mdk_thread_t *thread = arg;
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 /*
5017 * md_thread is a 'system-thread', it's priority should be very
5018 * high. We avoid resource deadlocks individually in each
5019 * raid personality. (RAID5 does preallocation) We also use RR and
5020 * the very same RT priority as kswapd, thus we will never get
5021 * into a priority inversion deadlock.
5022 *
5023 * we definitely have to have equal or higher priority than
5024 * bdflush, otherwise bdflush will deadlock if there are too
5025 * many dirty RAID5 blocks.
5026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027
NeilBrown6985c432005-10-19 21:23:47 -07005028 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005029 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
NeilBrown93588e22005-11-15 00:09:12 -08005031 /* We need to wait INTERRUPTIBLE so that
5032 * we don't add to the load-average.
5033 * That means we need to be sure no signals are
5034 * pending
5035 */
5036 if (signal_pending(current))
5037 flush_signals(current);
5038
5039 wait_event_interruptible_timeout
5040 (thread->wqueue,
5041 test_bit(THREAD_WAKEUP, &thread->flags)
5042 || kthread_should_stop(),
5043 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
5045 clear_bit(THREAD_WAKEUP, &thread->flags);
5046
NeilBrown787453c2005-11-08 21:39:43 -08005047 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 return 0;
5051}
5052
5053void md_wakeup_thread(mdk_thread_t *thread)
5054{
5055 if (thread) {
5056 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5057 set_bit(THREAD_WAKEUP, &thread->flags);
5058 wake_up(&thread->wqueue);
5059 }
5060}
5061
5062mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5063 const char *name)
5064{
5065 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066
NeilBrown9ffae0c2006-01-06 00:20:32 -08005067 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 if (!thread)
5069 return NULL;
5070
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 init_waitqueue_head(&thread->wqueue);
5072
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 thread->run = run;
5074 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005075 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005076 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005077 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 kfree(thread);
5079 return NULL;
5080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 return thread;
5082}
5083
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084void md_unregister_thread(mdk_thread_t *thread)
5085{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005086 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005087
5088 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 kfree(thread);
5090}
5091
5092void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5093{
5094 if (!mddev) {
5095 MD_BUG();
5096 return;
5097 }
5098
NeilBrownb2d444d2005-11-08 21:39:31 -08005099 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005101
5102 if (mddev->external)
5103 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5106 mdname(mddev),
5107 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5108 __builtin_return_address(0),__builtin_return_address(1),
5109 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005110*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005111 if (!mddev->pers)
5112 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 if (!mddev->pers->error_handler)
5114 return;
5115 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005116 if (mddev->degraded)
5117 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5119 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5120 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005121 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122}
5123
5124/* seq_file implementation /proc/mdstat */
5125
5126static void status_unused(struct seq_file *seq)
5127{
5128 int i = 0;
5129 mdk_rdev_t *rdev;
5130 struct list_head *tmp;
5131
5132 seq_printf(seq, "unused devices: ");
5133
NeilBrown73c34432008-02-06 01:39:59 -08005134 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 char b[BDEVNAME_SIZE];
5136 i++;
5137 seq_printf(seq, "%s ",
5138 bdevname(rdev->bdev,b));
5139 }
5140 if (!i)
5141 seq_printf(seq, "<none>");
5142
5143 seq_printf(seq, "\n");
5144}
5145
5146
5147static void status_resync(struct seq_file *seq, mddev_t * mddev)
5148{
NeilBrown4588b422006-03-27 01:18:04 -08005149 sector_t max_blocks, resync, res;
5150 unsigned long dt, db, rt;
5151 int scale;
5152 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
5154 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5155
5156 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5157 max_blocks = mddev->resync_max_sectors >> 1;
5158 else
5159 max_blocks = mddev->size;
5160
5161 /*
5162 * Should not happen.
5163 */
5164 if (!max_blocks) {
5165 MD_BUG();
5166 return;
5167 }
NeilBrown4588b422006-03-27 01:18:04 -08005168 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5169 * in a sector_t, and (max_blocks>>scale) will fit in a
5170 * u32, as those are the requirements for sector_div.
5171 * Thus 'scale' must be at least 10
5172 */
5173 scale = 10;
5174 if (sizeof(sector_t) > sizeof(unsigned long)) {
5175 while ( max_blocks/2 > (1ULL<<(scale+32)))
5176 scale++;
5177 }
5178 res = (resync>>scale)*1000;
5179 sector_div(res, (u32)((max_blocks>>scale)+1));
5180
5181 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 {
NeilBrown4588b422006-03-27 01:18:04 -08005183 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 seq_printf(seq, "[");
5185 for (i = 0; i < x; i++)
5186 seq_printf(seq, "=");
5187 seq_printf(seq, ">");
5188 for (i = 0; i < y; i++)
5189 seq_printf(seq, ".");
5190 seq_printf(seq, "] ");
5191 }
NeilBrown4588b422006-03-27 01:18:04 -08005192 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005193 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5194 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005195 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5196 "check" :
5197 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5198 "resync" : "recovery"))),
5199 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005200 (unsigned long long) resync,
5201 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202
5203 /*
5204 * We do not want to overflow, so the order of operands and
5205 * the * 100 / 100 trick are important. We do a +1 to be
5206 * safe against division by zero. We only estimate anyway.
5207 *
5208 * dt: time from mark until now
5209 * db: blocks written from mark until now
5210 * rt: remaining time
5211 */
5212 dt = ((jiffies - mddev->resync_mark) / HZ);
5213 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005214 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5215 - mddev->resync_mark_cnt;
5216 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217
5218 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5219
NeilBrownff4e8d92006-07-10 04:44:16 -07005220 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221}
5222
5223static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5224{
5225 struct list_head *tmp;
5226 loff_t l = *pos;
5227 mddev_t *mddev;
5228
5229 if (l >= 0x10000)
5230 return NULL;
5231 if (!l--)
5232 /* header */
5233 return (void*)1;
5234
5235 spin_lock(&all_mddevs_lock);
5236 list_for_each(tmp,&all_mddevs)
5237 if (!l--) {
5238 mddev = list_entry(tmp, mddev_t, all_mddevs);
5239 mddev_get(mddev);
5240 spin_unlock(&all_mddevs_lock);
5241 return mddev;
5242 }
5243 spin_unlock(&all_mddevs_lock);
5244 if (!l--)
5245 return (void*)2;/* tail */
5246 return NULL;
5247}
5248
5249static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5250{
5251 struct list_head *tmp;
5252 mddev_t *next_mddev, *mddev = v;
5253
5254 ++*pos;
5255 if (v == (void*)2)
5256 return NULL;
5257
5258 spin_lock(&all_mddevs_lock);
5259 if (v == (void*)1)
5260 tmp = all_mddevs.next;
5261 else
5262 tmp = mddev->all_mddevs.next;
5263 if (tmp != &all_mddevs)
5264 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5265 else {
5266 next_mddev = (void*)2;
5267 *pos = 0x10000;
5268 }
5269 spin_unlock(&all_mddevs_lock);
5270
5271 if (v != (void*)1)
5272 mddev_put(mddev);
5273 return next_mddev;
5274
5275}
5276
5277static void md_seq_stop(struct seq_file *seq, void *v)
5278{
5279 mddev_t *mddev = v;
5280
5281 if (mddev && v != (void*)1 && v != (void*)2)
5282 mddev_put(mddev);
5283}
5284
NeilBrownd7603b72006-01-06 00:20:30 -08005285struct mdstat_info {
5286 int event;
5287};
5288
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289static int md_seq_show(struct seq_file *seq, void *v)
5290{
5291 mddev_t *mddev = v;
5292 sector_t size;
5293 struct list_head *tmp2;
5294 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005295 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005296 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297
5298 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005299 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 seq_printf(seq, "Personalities : ");
5301 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005302 list_for_each_entry(pers, &pers_list, list)
5303 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304
5305 spin_unlock(&pers_lock);
5306 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005307 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 return 0;
5309 }
5310 if (v == (void*)2) {
5311 status_unused(seq);
5312 return 0;
5313 }
5314
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005315 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5319 seq_printf(seq, "%s : %sactive", mdname(mddev),
5320 mddev->pers ? "" : "in");
5321 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005322 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005324 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005325 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 seq_printf(seq, " %s", mddev->pers->name);
5327 }
5328
5329 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005330 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 char b[BDEVNAME_SIZE];
5332 seq_printf(seq, " %s[%d]",
5333 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005334 if (test_bit(WriteMostly, &rdev->flags))
5335 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005336 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337 seq_printf(seq, "(F)");
5338 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005339 } else if (rdev->raid_disk < 0)
5340 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 size += rdev->size;
5342 }
5343
5344 if (!list_empty(&mddev->disks)) {
5345 if (mddev->pers)
5346 seq_printf(seq, "\n %llu blocks",
5347 (unsigned long long)mddev->array_size);
5348 else
5349 seq_printf(seq, "\n %llu blocks",
5350 (unsigned long long)size);
5351 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005352 if (mddev->persistent) {
5353 if (mddev->major_version != 0 ||
5354 mddev->minor_version != 90) {
5355 seq_printf(seq," super %d.%d",
5356 mddev->major_version,
5357 mddev->minor_version);
5358 }
NeilBrowne6910632008-02-06 01:39:51 -08005359 } else if (mddev->external)
5360 seq_printf(seq, " super external:%s",
5361 mddev->metadata_type);
5362 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005363 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364
5365 if (mddev->pers) {
5366 mddev->pers->status (seq, mddev);
5367 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005368 if (mddev->pers->sync_request) {
5369 if (mddev->curr_resync > 2) {
5370 status_resync (seq, mddev);
5371 seq_printf(seq, "\n ");
5372 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5373 seq_printf(seq, "\tresync=DELAYED\n ");
5374 else if (mddev->recovery_cp < MaxSector)
5375 seq_printf(seq, "\tresync=PENDING\n ");
5376 }
NeilBrown32a76272005-06-21 17:17:14 -07005377 } else
5378 seq_printf(seq, "\n ");
5379
5380 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005381 unsigned long chunk_kb;
5382 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005383 spin_lock_irqsave(&bitmap->lock, flags);
5384 chunk_kb = bitmap->chunksize >> 10;
5385 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5386 "%lu%s chunk",
5387 bitmap->pages - bitmap->missing_pages,
5388 bitmap->pages,
5389 (bitmap->pages - bitmap->missing_pages)
5390 << (PAGE_SHIFT - 10),
5391 chunk_kb ? chunk_kb : bitmap->chunksize,
5392 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005393 if (bitmap->file) {
5394 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005395 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005396 }
NeilBrown78d742d2005-06-21 17:17:15 -07005397
NeilBrown32a76272005-06-21 17:17:14 -07005398 seq_printf(seq, "\n");
5399 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 }
5401
5402 seq_printf(seq, "\n");
5403 }
5404 mddev_unlock(mddev);
5405
5406 return 0;
5407}
5408
5409static struct seq_operations md_seq_ops = {
5410 .start = md_seq_start,
5411 .next = md_seq_next,
5412 .stop = md_seq_stop,
5413 .show = md_seq_show,
5414};
5415
5416static int md_seq_open(struct inode *inode, struct file *file)
5417{
5418 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005419 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5420 if (mi == NULL)
5421 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
5423 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005424 if (error)
5425 kfree(mi);
5426 else {
5427 struct seq_file *p = file->private_data;
5428 p->private = mi;
5429 mi->event = atomic_read(&md_event_count);
5430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 return error;
5432}
5433
NeilBrownd7603b72006-01-06 00:20:30 -08005434static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5435{
5436 struct seq_file *m = filp->private_data;
5437 struct mdstat_info *mi = m->private;
5438 int mask;
5439
5440 poll_wait(filp, &md_event_waiters, wait);
5441
5442 /* always allow read */
5443 mask = POLLIN | POLLRDNORM;
5444
5445 if (mi->event != atomic_read(&md_event_count))
5446 mask |= POLLERR | POLLPRI;
5447 return mask;
5448}
5449
Arjan van de Venfa027c22007-02-12 00:55:33 -08005450static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005451 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 .open = md_seq_open,
5453 .read = seq_read,
5454 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005455 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005456 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457};
5458
NeilBrown2604b702006-01-06 00:20:36 -08005459int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005462 list_add_tail(&p->list, &pers_list);
5463 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 spin_unlock(&pers_lock);
5465 return 0;
5466}
5467
NeilBrown2604b702006-01-06 00:20:36 -08005468int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469{
NeilBrown2604b702006-01-06 00:20:36 -08005470 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005472 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 spin_unlock(&pers_lock);
5474 return 0;
5475}
5476
5477static int is_mddev_idle(mddev_t *mddev)
5478{
5479 mdk_rdev_t * rdev;
5480 struct list_head *tmp;
5481 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005482 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
5484 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005485 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005487 curr_events = disk_stat_read(disk, sectors[0]) +
5488 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005490 /* sync IO will cause sync_io to increase before the disk_stats
5491 * as sync_io is counted when a request starts, and
5492 * disk_stats is counted when it completes.
5493 * So resync activity will cause curr_events to be smaller than
5494 * when there was no such activity.
5495 * non-sync IO will cause disk_stat to increase without
5496 * increasing sync_io so curr_events will (eventually)
5497 * be larger than it was before. Once it becomes
5498 * substantially larger, the test below will cause
5499 * the array to appear non-idle, and resync will slow
5500 * down.
5501 * If there is a lot of outstanding resync activity when
5502 * we set last_event to curr_events, then all that activity
5503 * completing might cause the array to appear non-idle
5504 * and resync will be slowed down even though there might
5505 * not have been non-resync activity. This will only
5506 * happen once though. 'last_events' will soon reflect
5507 * the state where there is little or no outstanding
5508 * resync requests, and further resync activity will
5509 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005510 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005512 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 rdev->last_events = curr_events;
5514 idle = 0;
5515 }
5516 }
5517 return idle;
5518}
5519
5520void md_done_sync(mddev_t *mddev, int blocks, int ok)
5521{
5522 /* another "blocks" (512byte) blocks have been synced */
5523 atomic_sub(blocks, &mddev->recovery_active);
5524 wake_up(&mddev->recovery_wait);
5525 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005526 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 md_wakeup_thread(mddev->thread);
5528 // stop recovery, signal do_sync ....
5529 }
5530}
5531
5532
NeilBrown06d91a52005-06-21 17:17:12 -07005533/* md_write_start(mddev, bi)
5534 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005535 * in superblock) before writing, schedule a superblock update
5536 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005537 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005538void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539{
Neil Brown0fd62b82008-06-28 08:31:36 +10005540 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005541 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005542 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005543
NeilBrownf91de922005-11-08 21:39:36 -08005544 BUG_ON(mddev->ro == 1);
5545 if (mddev->ro == 2) {
5546 /* need to switch to read/write */
5547 mddev->ro = 0;
5548 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5549 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005550 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005551 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005552 }
NeilBrown06d91a52005-06-21 17:17:12 -07005553 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005554 if (mddev->safemode == 1)
5555 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005556 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005557 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005558 if (mddev->in_sync) {
5559 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005560 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005561 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005562 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005563 }
NeilBrowna9701a32005-11-08 21:39:34 -08005564 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005565 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005566 if (did_change)
5567 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown09a44cc2008-05-23 13:04:36 -07005568 wait_event(mddev->sb_wait,
5569 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5570 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571}
5572
5573void md_write_end(mddev_t *mddev)
5574{
5575 if (atomic_dec_and_test(&mddev->writes_pending)) {
5576 if (mddev->safemode == 2)
5577 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005578 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5580 }
5581}
5582
NeilBrown2a2275d2007-01-26 00:57:11 -08005583/* md_allow_write(mddev)
5584 * Calling this ensures that the array is marked 'active' so that writes
5585 * may proceed without blocking. It is important to call this before
5586 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5587 * Must be called with mddev_lock held.
5588 */
5589void md_allow_write(mddev_t *mddev)
5590{
5591 if (!mddev->pers)
5592 return;
5593 if (mddev->ro)
5594 return;
Neil Brown1a0fd492008-06-28 08:31:27 +10005595 if (!mddev->pers->sync_request)
5596 return;
NeilBrown2a2275d2007-01-26 00:57:11 -08005597
5598 spin_lock_irq(&mddev->write_lock);
5599 if (mddev->in_sync) {
5600 mddev->in_sync = 0;
5601 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5602 if (mddev->safemode_delay &&
5603 mddev->safemode == 0)
5604 mddev->safemode = 1;
5605 spin_unlock_irq(&mddev->write_lock);
5606 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005607
5608 sysfs_notify(&mddev->kobj, NULL, "array_state");
5609 /* wait for the dirty state to be recorded in the metadata */
5610 wait_event(mddev->sb_wait,
5611 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5612 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
NeilBrown2a2275d2007-01-26 00:57:11 -08005613 } else
5614 spin_unlock_irq(&mddev->write_lock);
5615}
5616EXPORT_SYMBOL_GPL(md_allow_write);
5617
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618#define SYNC_MARKS 10
5619#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005620void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
5622 mddev_t *mddev2;
5623 unsigned int currspeed = 0,
5624 window;
NeilBrown57afd892005-06-21 17:17:13 -07005625 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 unsigned long mark[SYNC_MARKS];
5627 sector_t mark_cnt[SYNC_MARKS];
5628 int last_mark,m;
5629 struct list_head *tmp;
5630 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005631 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005632 struct list_head *rtmp;
5633 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005634 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
5636 /* just incase thread restarts... */
5637 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5638 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005639 if (mddev->ro) /* never try to sync a read-only array */
5640 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641
NeilBrown61df9d92006-10-03 01:15:57 -07005642 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5643 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5644 desc = "data-check";
5645 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5646 desc = "requested-resync";
5647 else
5648 desc = "resync";
5649 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5650 desc = "reshape";
5651 else
5652 desc = "recovery";
5653
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 /* we overload curr_resync somewhat here.
5655 * 0 == not engaged in resync at all
5656 * 2 == checking that there is no conflict with another sync
5657 * 1 == like 2, but have yielded to allow conflicting resync to
5658 * commense
5659 * other == active in resync - this many blocks
5660 *
5661 * Before starting a resync we must have set curr_resync to
5662 * 2, and then checked that every "conflicting" array has curr_resync
5663 * less than ours. When we find one that is the same or higher
5664 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5665 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5666 * This will mean we have to start checking from the beginning again.
5667 *
5668 */
5669
5670 do {
5671 mddev->curr_resync = 2;
5672
5673 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005674 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005675 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 goto skip;
5677 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005678 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 if (mddev2 == mddev)
5680 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005681 if (!mddev->parallel_resync
5682 && mddev2->curr_resync
5683 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 DEFINE_WAIT(wq);
5685 if (mddev < mddev2 && mddev->curr_resync == 2) {
5686 /* arbitrarily yield */
5687 mddev->curr_resync = 1;
5688 wake_up(&resync_wait);
5689 }
5690 if (mddev > mddev2 && mddev->curr_resync == 1)
5691 /* no need to wait here, we can wait the next
5692 * time 'round when curr_resync == 2
5693 */
5694 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005695 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5696 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005697 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005698 printk(KERN_INFO "md: delaying %s of %s"
5699 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005701 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 mddev_put(mddev2);
5703 schedule();
5704 finish_wait(&resync_wait, &wq);
5705 goto try_again;
5706 }
5707 finish_wait(&resync_wait, &wq);
5708 }
5709 }
5710 } while (mddev->curr_resync < 2);
5711
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005712 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005713 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005715 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 */
5717 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005718 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005719 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005720 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5721 j = mddev->resync_min;
5722 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005723 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005724
NeilBrownccfcc3c2006-03-27 01:18:09 -08005725 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5726 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005727 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 /* recovery follows the physical size of devices */
5729 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005730 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005731 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005732 if (rdev->raid_disk >= 0 &&
5733 !test_bit(Faulty, &rdev->flags) &&
5734 !test_bit(In_sync, &rdev->flags) &&
5735 rdev->recovery_offset < j)
5736 j = rdev->recovery_offset;
5737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738
NeilBrown61df9d92006-10-03 01:15:57 -07005739 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5740 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5741 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005742 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005743 "(but not more than %d KB/sec) for %s.\n",
5744 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
5746 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005747
NeilBrown57afd892005-06-21 17:17:13 -07005748 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 for (m = 0; m < SYNC_MARKS; m++) {
5750 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005751 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
5753 last_mark = 0;
5754 mddev->resync_mark = mark[last_mark];
5755 mddev->resync_mark_cnt = mark_cnt[last_mark];
5756
5757 /*
5758 * Tune reconstruction:
5759 */
5760 window = 32*(PAGE_SIZE/512);
5761 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5762 window/2,(unsigned long long) max_sectors/2);
5763
5764 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 last_check = 0;
5766
5767 if (j>2) {
5768 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005769 "md: resuming %s of %s from checkpoint.\n",
5770 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 mddev->curr_resync = j;
5772 }
5773
5774 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005775 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776
NeilBrown57afd892005-06-21 17:17:13 -07005777 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005778 if (j >= mddev->resync_max) {
5779 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5780 wait_event(mddev->recovery_wait,
5781 mddev->resync_max > j
5782 || kthread_should_stop());
5783 }
5784 if (kthread_should_stop())
5785 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005786 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005787 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005788 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005789 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 goto out;
5791 }
NeilBrown57afd892005-06-21 17:17:13 -07005792
5793 if (!skipped) { /* actual IO requested */
5794 io_sectors += sectors;
5795 atomic_add(sectors, &mddev->recovery_active);
5796 }
5797
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 j += sectors;
5799 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005800 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005801 if (last_check == 0)
5802 /* this is the earliers that rebuilt will be
5803 * visible in /proc/mdstat
5804 */
5805 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005806
5807 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 continue;
5809
NeilBrown57afd892005-06-21 17:17:13 -07005810 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811
NeilBrowndfc70642008-05-23 13:04:39 -07005812 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813 break;
5814
5815 repeat:
5816 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5817 /* step marks */
5818 int next = (last_mark+1) % SYNC_MARKS;
5819
5820 mddev->resync_mark = mark[next];
5821 mddev->resync_mark_cnt = mark_cnt[next];
5822 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005823 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 last_mark = next;
5825 }
5826
5827
NeilBrownc6207272008-02-06 01:39:52 -08005828 if (kthread_should_stop())
5829 goto interrupted;
5830
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
5832 /*
5833 * this loop exits only if either when we are slower than
5834 * the 'hard' speed limit, or the system was IO-idle for
5835 * a jiffy.
5836 * the system might be non-idle CPU-wise, but we only care
5837 * about not overloading the IO subsystem. (things like an
5838 * e2fsck being done on the RAID array should execute fast)
5839 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005840 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 cond_resched();
5842
NeilBrown57afd892005-06-21 17:17:13 -07005843 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5844 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845
NeilBrown88202a02006-01-06 00:21:36 -08005846 if (currspeed > speed_min(mddev)) {
5847 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005849 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 goto repeat;
5851 }
5852 }
5853 }
NeilBrown61df9d92006-10-03 01:15:57 -07005854 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 /*
5856 * this also signals 'finished resyncing' to md_stop
5857 */
5858 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005859 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860
5861 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5862
5863 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005864 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865
NeilBrowndfc70642008-05-23 13:04:39 -07005866 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005867 mddev->curr_resync > 2) {
5868 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5869 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5870 if (mddev->curr_resync >= mddev->recovery_cp) {
5871 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005872 "md: checkpointing %s of %s.\n",
5873 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005874 mddev->recovery_cp = mddev->curr_resync;
5875 }
5876 } else
5877 mddev->recovery_cp = MaxSector;
5878 } else {
5879 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5880 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005881 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005882 if (rdev->raid_disk >= 0 &&
5883 !test_bit(Faulty, &rdev->flags) &&
5884 !test_bit(In_sync, &rdev->flags) &&
5885 rdev->recovery_offset < mddev->curr_resync)
5886 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 }
NeilBrown17571282006-12-10 02:20:52 -08005889 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 skip:
5892 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005893 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005894 mddev->resync_max = MaxSector;
5895 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 wake_up(&resync_wait);
5897 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5898 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005899 return;
5900
5901 interrupted:
5902 /*
5903 * got a signal, exit.
5904 */
5905 printk(KERN_INFO
5906 "md: md_do_sync() got signal ... exiting\n");
5907 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5908 goto out;
5909
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910}
NeilBrown29269552006-03-27 01:18:10 -08005911EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
5913
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005914static int remove_and_add_spares(mddev_t *mddev)
5915{
5916 mdk_rdev_t *rdev;
5917 struct list_head *rtmp;
5918 int spares = 0;
5919
NeilBrownd089c6a2008-02-06 01:39:59 -08005920 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005921 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005922 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005923 (test_bit(Faulty, &rdev->flags) ||
5924 ! test_bit(In_sync, &rdev->flags)) &&
5925 atomic_read(&rdev->nr_pending)==0) {
5926 if (mddev->pers->hot_remove_disk(
5927 mddev, rdev->raid_disk)==0) {
5928 char nm[20];
5929 sprintf(nm,"rd%d", rdev->raid_disk);
5930 sysfs_remove_link(&mddev->kobj, nm);
5931 rdev->raid_disk = -1;
5932 }
5933 }
5934
5935 if (mddev->degraded) {
NeilBrowndfc70642008-05-23 13:04:39 -07005936 rdev_for_each(rdev, rtmp, mddev) {
5937 if (rdev->raid_disk >= 0 &&
5938 !test_bit(In_sync, &rdev->flags))
5939 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005940 if (rdev->raid_disk < 0
5941 && !test_bit(Faulty, &rdev->flags)) {
5942 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10005943 if (mddev->pers->
5944 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005945 char nm[20];
5946 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005947 if (sysfs_create_link(&mddev->kobj,
5948 &rdev->kobj, nm))
5949 printk(KERN_WARNING
5950 "md: cannot register "
5951 "%s for %s\n",
5952 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005953 spares++;
5954 md_new_event(mddev);
5955 } else
5956 break;
5957 }
NeilBrowndfc70642008-05-23 13:04:39 -07005958 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005959 }
5960 return spares;
5961}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962/*
5963 * This routine is regularly called by all per-raid-array threads to
5964 * deal with generic issues like resync and super-block update.
5965 * Raid personalities that don't have a thread (linear/raid0) do not
5966 * need this as they never do any recovery or update the superblock.
5967 *
5968 * It does not do any resync itself, but rather "forks" off other threads
5969 * to do that as needed.
5970 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5971 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07005972 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973 * and wakeups up this thread which will reap the thread and finish up.
5974 * This thread also removes any faulty devices (with nr_pending == 0).
5975 *
5976 * The overall approach is:
5977 * 1/ if the superblock needs updating, update it.
5978 * 2/ If a recovery thread is running, don't do anything else.
5979 * 3/ If recovery has finished, clean up, possibly marking spares active.
5980 * 4/ If there are any faulty devices, remove them.
5981 * 5/ If array is degraded, try to add spares devices
5982 * 6/ If array has spares or is not in-sync, start a resync thread.
5983 */
5984void md_check_recovery(mddev_t *mddev)
5985{
5986 mdk_rdev_t *rdev;
5987 struct list_head *rtmp;
5988
5989
NeilBrown5f404022005-06-21 17:17:16 -07005990 if (mddev->bitmap)
5991 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992
5993 if (mddev->ro)
5994 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005995
5996 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07005997 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07005998 printk(KERN_INFO "md: %s in immediate safe mode\n",
5999 mdname(mddev));
6000 mddev->safemode = 2;
6001 }
6002 flush_signals(current);
6003 }
6004
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006006 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006008 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006009 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006010 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6011 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 ))
6013 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006014
NeilBrowndf5b89b2006-03-27 01:18:20 -08006015 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006016 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006017
NeilBrown31a59e32008-04-30 00:52:30 -07006018 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006019 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006020 spin_lock_irq(&mddev->write_lock);
6021 if (mddev->safemode &&
6022 !atomic_read(&mddev->writes_pending) &&
6023 !mddev->in_sync &&
6024 mddev->recovery_cp == MaxSector) {
6025 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006026 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006027 if (mddev->persistent)
6028 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6029 }
6030 if (mddev->safemode == 1)
6031 mddev->safemode = 0;
6032 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006033 if (did_change)
6034 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrownfca4d842005-06-21 17:17:11 -07006035 }
NeilBrownfca4d842005-06-21 17:17:11 -07006036
NeilBrown850b2b422006-10-03 01:15:46 -07006037 if (mddev->flags)
6038 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006039
NeilBrown06d91a52005-06-21 17:17:12 -07006040
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6042 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6043 /* resync/recovery still happening */
6044 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6045 goto unlock;
6046 }
6047 if (mddev->sync_thread) {
6048 /* resync has finished, collect result */
6049 md_unregister_thread(mddev->sync_thread);
6050 mddev->sync_thread = NULL;
NeilBrowndfc70642008-05-23 13:04:39 -07006051 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 /* success...*/
6053 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006054 if (mddev->pers->spare_active(mddev))
6055 sysfs_notify(&mddev->kobj, NULL,
6056 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 }
NeilBrown850b2b422006-10-03 01:15:46 -07006058 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006059
6060 /* if array is no-longer degraded, then any saved_raid_disk
6061 * information must be scrapped
6062 */
6063 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006064 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006065 rdev->saved_raid_disk = -1;
6066
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 mddev->recovery = 0;
6068 /* flag recovery needed just to double check */
6069 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006070 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006071 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 goto unlock;
6073 }
Neil Brown72a23c22008-06-28 08:31:41 +10006074 /* Set RUNNING before clearing NEEDED to avoid
6075 * any transients in the value of "sync_action".
6076 */
6077 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6078 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006079 /* Clear some bits that don't mean anything, but
6080 * might be left set
6081 */
NeilBrown24dd4692005-11-08 21:39:26 -08006082 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6083 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006085 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6086 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 /* no recovery is running.
6088 * remove any failed drives, then
6089 * add spares if possible.
6090 * Spare are also removed and re-added, to allow
6091 * the personality to fail the re-add.
6092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006094 if (mddev->reshape_position != MaxSector) {
6095 if (mddev->pers->check_reshape(mddev) != 0)
6096 /* Cannot proceed */
6097 goto unlock;
6098 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006099 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006100 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006101 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6102 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006103 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006104 } else if (mddev->recovery_cp < MaxSector) {
6105 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006106 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006107 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6108 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006110
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006112 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6113 /* We are adding a device or devices to an array
6114 * which has the bitmap stored on all devices.
6115 * So make sure all bitmap pages get written
6116 */
6117 bitmap_write_all(mddev->bitmap);
6118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119 mddev->sync_thread = md_register_thread(md_do_sync,
6120 mddev,
6121 "%s_resync");
6122 if (!mddev->sync_thread) {
6123 printk(KERN_ERR "%s: could not start resync"
6124 " thread...\n",
6125 mdname(mddev));
6126 /* leave the spares where they are, it shouldn't hurt */
6127 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006128 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 md_wakeup_thread(mddev->sync_thread);
Neil Brown72a23c22008-06-28 08:31:41 +10006130 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006131 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 }
6133 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006134 if (!mddev->sync_thread) {
6135 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6136 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6137 &mddev->recovery))
6138 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 mddev_unlock(mddev);
6141 }
6142}
6143
Dan Williams6bfe0b42008-04-30 00:52:32 -07006144void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6145{
6146 sysfs_notify(&rdev->kobj, NULL, "state");
6147 wait_event_timeout(rdev->blocked_wait,
6148 !test_bit(Blocked, &rdev->flags),
6149 msecs_to_jiffies(5000));
6150 rdev_dec_pending(rdev, mddev);
6151}
6152EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6153
Adrian Bunk75c96f82005-05-05 16:16:09 -07006154static int md_notify_reboot(struct notifier_block *this,
6155 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156{
6157 struct list_head *tmp;
6158 mddev_t *mddev;
6159
6160 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6161
6162 printk(KERN_INFO "md: stopping all md devices.\n");
6163
NeilBrown29ac4aa2008-02-06 01:39:58 -08006164 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006165 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10006167 mddev_unlock(mddev);
6168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 /*
6170 * certain more exotic SCSI devices are known to be
6171 * volatile wrt too early system reboots. While the
6172 * right place to handle this issue is the given
6173 * driver, we do want to have a safe RAID driver ...
6174 */
6175 mdelay(1000*1);
6176 }
6177 return NOTIFY_DONE;
6178}
6179
Adrian Bunk75c96f82005-05-05 16:16:09 -07006180static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 .notifier_call = md_notify_reboot,
6182 .next = NULL,
6183 .priority = INT_MAX, /* before any real devices */
6184};
6185
6186static void md_geninit(void)
6187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6189
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006190 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191}
6192
Adrian Bunk75c96f82005-05-05 16:16:09 -07006193static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 if (register_blkdev(MAJOR_NR, "md"))
6196 return -1;
6197 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6198 unregister_blkdev(MAJOR_NR, "md");
6199 return -1;
6200 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006201 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6202 md_probe, NULL, NULL);
6203 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 md_probe, NULL, NULL);
6205
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006207 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208
6209 md_geninit();
6210 return (0);
6211}
6212
6213
6214#ifndef MODULE
6215
6216/*
6217 * Searches all registered partitions for autorun RAID arrays
6218 * at boot time.
6219 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006220
6221static LIST_HEAD(all_detected_devices);
6222struct detected_devices_node {
6223 struct list_head list;
6224 dev_t dev;
6225};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226
6227void md_autodetect_dev(dev_t dev)
6228{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006229 struct detected_devices_node *node_detected_dev;
6230
6231 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6232 if (node_detected_dev) {
6233 node_detected_dev->dev = dev;
6234 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6235 } else {
6236 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6237 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239}
6240
6241
6242static void autostart_arrays(int part)
6243{
6244 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006245 struct detected_devices_node *node_detected_dev;
6246 dev_t dev;
6247 int i_scanned, i_passed;
6248
6249 i_scanned = 0;
6250 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
6252 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6253
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006254 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6255 i_scanned++;
6256 node_detected_dev = list_entry(all_detected_devices.next,
6257 struct detected_devices_node, list);
6258 list_del(&node_detected_dev->list);
6259 dev = node_detected_dev->dev;
6260 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006261 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 if (IS_ERR(rdev))
6263 continue;
6264
NeilBrownb2d444d2005-11-08 21:39:31 -08006265 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 MD_BUG();
6267 continue;
6268 }
NeilBrownd0fae182008-03-04 14:29:31 -08006269 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006271 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006273
6274 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6275 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276
6277 autorun_devices(part);
6278}
6279
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006280#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
6282static __exit void md_exit(void)
6283{
6284 mddev_t *mddev;
6285 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006286
NeilBrowne8703fe2006-10-03 01:15:59 -07006287 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6288 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289
6290 unregister_blkdev(MAJOR_NR,"md");
6291 unregister_blkdev(mdp_major, "mdp");
6292 unregister_reboot_notifier(&md_notifier);
6293 unregister_sysctl_table(raid_table_header);
6294 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006295 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 struct gendisk *disk = mddev->gendisk;
6297 if (!disk)
6298 continue;
6299 export_array(mddev);
6300 del_gendisk(disk);
6301 put_disk(disk);
6302 mddev->gendisk = NULL;
6303 mddev_put(mddev);
6304 }
6305}
6306
Dan Williams685784a2007-07-09 11:56:42 -07006307subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308module_exit(md_exit)
6309
NeilBrownf91de922005-11-08 21:39:36 -08006310static int get_ro(char *buffer, struct kernel_param *kp)
6311{
6312 return sprintf(buffer, "%d", start_readonly);
6313}
6314static int set_ro(const char *val, struct kernel_param *kp)
6315{
6316 char *e;
6317 int num = simple_strtoul(val, &e, 10);
6318 if (*val && (*e == '\0' || *e == '\n')) {
6319 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006320 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006321 }
6322 return -EINVAL;
6323}
6324
NeilBrown80ca3a42006-07-10 04:44:18 -07006325module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6326module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006327
NeilBrownf91de922005-11-08 21:39:36 -08006328
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329EXPORT_SYMBOL(register_md_personality);
6330EXPORT_SYMBOL(unregister_md_personality);
6331EXPORT_SYMBOL(md_error);
6332EXPORT_SYMBOL(md_done_sync);
6333EXPORT_SYMBOL(md_write_start);
6334EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335EXPORT_SYMBOL(md_register_thread);
6336EXPORT_SYMBOL(md_unregister_thread);
6337EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338EXPORT_SYMBOL(md_check_recovery);
6339MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006340MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006341MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);