blob: 7c9a87b02e7784259d751ed9a57ce02f4a7409a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
35#include <linux/module.h>
Ahmed S. Darwish50511da2007-05-09 02:35:34 -070036#include <linux/kernel.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070037#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/linkage.h>
39#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070040#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080043#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080044#include <linux/mutex.h>
NeilBrown16f17b32006-06-26 00:27:37 -070045#include <linux/ctype.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <linux/init.h>
49
NeilBrown32a76272005-06-21 17:17:14 -070050#include <linux/file.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef CONFIG_KMOD
53#include <linux/kmod.h>
54#endif
55
56#include <asm/unaligned.h>
57
58#define MAJOR_NR MD_MAJOR
59#define MD_DRIVER
60
61/* 63 partitions with the alternate major number (mdp) */
62#define MdpMinorShift 6
63
64#define DEBUG 0
65#define dprintk(x...) ((void)(DEBUG && printk(x)))
66
67
68#ifndef MODULE
69static void autostart_arrays (int part);
70#endif
71
NeilBrown2604b702006-01-06 00:20:36 -080072static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static DEFINE_SPINLOCK(pers_lock);
74
Adrian Bunk5e563412006-06-26 00:27:42 -070075static void md_print_devices(void);
76
77#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070083 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
86 * idle IO detection.
87 *
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080089 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91
92static int sysctl_speed_limit_min = 1000;
93static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080094static inline int speed_min(mddev_t *mddev)
95{
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
98}
99
100static inline int speed_max(mddev_t *mddev)
101{
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static struct ctl_table_header *raid_table_header;
107
108static ctl_table raid_table[] = {
109 {
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
111 .procname = "speed_limit_min",
112 .data = &sysctl_speed_limit_min,
113 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700114 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .proc_handler = &proc_dointvec,
116 },
117 {
118 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .proc_handler = &proc_dointvec,
124 },
125 { .ctl_name = 0 }
126};
127
128static ctl_table raid_dir_table[] = {
129 {
130 .ctl_name = DEV_RAID,
131 .procname = "raid",
132 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700133 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .child = raid_table,
135 },
136 { .ctl_name = 0 }
137};
138
139static ctl_table raid_root_table[] = {
140 {
141 .ctl_name = CTL_DEV,
142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
147 { .ctl_name = 0 }
148};
149
150static struct block_device_operations md_fops;
151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
NeilBrownd7603b72006-01-06 00:20:30 -0800155 * We have a system wide 'event count' that is incremented
156 * on any 'interesting' event, and readers of /proc/mdstat
157 * can use 'poll' or 'select' to find out when the event
158 * count increases.
159 *
160 * Events are:
161 * start array, stop array, error, add device, remove device,
162 * start build, activate spare
163 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800164static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800165static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800166void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800167{
168 atomic_inc(&md_event_count);
169 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100170 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800171}
NeilBrown29269552006-03-27 01:18:10 -0800172EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800173
NeilBrownc331eb02006-05-30 21:27:13 -0700174/* Alternate version that can be called from interrupts
175 * when calling sysfs_notify isn't needed.
176 */
Adrian Bunk05381952006-06-26 00:28:01 -0700177static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700178{
179 atomic_inc(&md_event_count);
180 wake_up(&md_event_waiters);
181}
182
NeilBrownd7603b72006-01-06 00:20:30 -0800183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Enables to iterate over all existing md arrays
185 * all_mddevs_lock protects this list.
186 */
187static LIST_HEAD(all_mddevs);
188static DEFINE_SPINLOCK(all_mddevs_lock);
189
190
191/*
192 * iterates through all used mddevs in the system.
193 * We take care to grab the all_mddevs_lock whenever navigating
194 * the list, and to always hold a refcount when unlocked.
195 * Any code which breaks out of this loop while own
196 * a reference to the current mddev and must mddev_put it.
197 */
198#define ITERATE_MDDEV(mddev,tmp) \
199 \
200 for (({ spin_lock(&all_mddevs_lock); \
201 tmp = all_mddevs.next; \
202 mddev = NULL;}); \
203 ({ if (tmp != &all_mddevs) \
204 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
205 spin_unlock(&all_mddevs_lock); \
206 if (mddev) mddev_put(mddev); \
207 mddev = list_entry(tmp, mddev_t, all_mddevs); \
208 tmp != &all_mddevs;}); \
209 ({ spin_lock(&all_mddevs_lock); \
210 tmp = tmp->next;}) \
211 )
212
213
Jens Axboe165125e2007-07-24 09:28:11 +0200214static int md_fail_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
NeilBrown6712ecf2007-09-27 12:47:43 +0200216 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return 0;
218}
219
220static inline mddev_t *mddev_get(mddev_t *mddev)
221{
222 atomic_inc(&mddev->active);
223 return mddev;
224}
225
226static void mddev_put(mddev_t *mddev)
227{
228 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
229 return;
230 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
231 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800232 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500233 blk_cleanup_queue(mddev->queue);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800234 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800235 } else
236 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static mddev_t * mddev_find(dev_t unit)
240{
241 mddev_t *mddev, *new = NULL;
242
243 retry:
244 spin_lock(&all_mddevs_lock);
245 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
246 if (mddev->unit == unit) {
247 mddev_get(mddev);
248 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700249 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return mddev;
251 }
252
253 if (new) {
254 list_add(&new->all_mddevs, &all_mddevs);
255 spin_unlock(&all_mddevs_lock);
256 return new;
257 }
258 spin_unlock(&all_mddevs_lock);
259
NeilBrown9ffae0c2006-01-06 00:20:32 -0800260 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!new)
262 return NULL;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 new->unit = unit;
265 if (MAJOR(unit) == MD_MAJOR)
266 new->md_minor = MINOR(unit);
267 else
268 new->md_minor = MINOR(unit) >> MdpMinorShift;
269
NeilBrowndf5b89b2006-03-27 01:18:20 -0800270 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 INIT_LIST_HEAD(&new->disks);
272 INIT_LIST_HEAD(&new->all_mddevs);
273 init_timer(&new->safemode_timer);
274 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700275 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700276 init_waitqueue_head(&new->sb_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700277 new->reshape_position = MaxSector;
NeilBrownc6207272008-02-06 01:39:52 -0800278 new->resync_max = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 new->queue = blk_alloc_queue(GFP_KERNEL);
281 if (!new->queue) {
282 kfree(new);
283 return NULL;
284 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800285 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 blk_queue_make_request(new->queue, md_fail_request);
288
289 goto retry;
290}
291
292static inline int mddev_lock(mddev_t * mddev)
293{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800294 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static inline int mddev_trylock(mddev_t * mddev)
298{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800299 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static inline void mddev_unlock(mddev_t * mddev)
303{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800304 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
NeilBrown005eca52005-08-22 13:11:08 -0700306 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
NeilBrown2989ddb2006-01-06 00:20:43 -0800309static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 mdk_rdev_t * rdev;
312 struct list_head *tmp;
313
314 ITERATE_RDEV(mddev,rdev,tmp) {
315 if (rdev->desc_nr == nr)
316 return rdev;
317 }
318 return NULL;
319}
320
321static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
322{
323 struct list_head *tmp;
324 mdk_rdev_t *rdev;
325
326 ITERATE_RDEV(mddev,rdev,tmp) {
327 if (rdev->bdev->bd_dev == dev)
328 return rdev;
329 }
330 return NULL;
331}
332
NeilBrownd9d166c2006-01-06 00:20:51 -0800333static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800334{
335 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800336 list_for_each_entry(pers, &pers_list, list) {
337 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800338 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800339 if (strcmp(pers->name, clevel)==0)
340 return pers;
341 }
NeilBrown2604b702006-01-06 00:20:36 -0800342 return NULL;
343}
344
Jesper Juhl77933d72005-07-27 11:46:09 -0700345static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
348 return MD_NEW_SIZE_BLOCKS(size);
349}
350
351static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
352{
353 sector_t size;
354
355 size = rdev->sb_offset;
356
357 if (chunk_size)
358 size &= ~((sector_t)chunk_size/1024 - 1);
359 return size;
360}
361
362static int alloc_disk_sb(mdk_rdev_t * rdev)
363{
364 if (rdev->sb_page)
365 MD_BUG();
366
367 rdev->sb_page = alloc_page(GFP_KERNEL);
368 if (!rdev->sb_page) {
369 printk(KERN_ALERT "md: out of memory.\n");
370 return -EINVAL;
371 }
372
373 return 0;
374}
375
376static void free_disk_sb(mdk_rdev_t * rdev)
377{
378 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800379 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 rdev->sb_loaded = 0;
381 rdev->sb_page = NULL;
382 rdev->sb_offset = 0;
383 rdev->size = 0;
384 }
385}
386
387
NeilBrown6712ecf2007-09-27 12:47:43 +0200388static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700389{
390 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800391 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700392
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700393 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
394 printk("md: super_written gets error=%d, uptodate=%d\n",
395 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
396 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800397 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700398 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700399
NeilBrowna9701a32005-11-08 21:39:34 -0800400 if (atomic_dec_and_test(&mddev->pending_writes))
401 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700402 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700403}
404
NeilBrown6712ecf2007-09-27 12:47:43 +0200405static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800406{
407 struct bio *bio2 = bio->bi_private;
408 mdk_rdev_t *rdev = bio2->bi_private;
409 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800410
411 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
412 error == -EOPNOTSUPP) {
413 unsigned long flags;
414 /* barriers don't appear to be supported :-( */
415 set_bit(BarriersNotsupp, &rdev->flags);
416 mddev->barriers_work = 0;
417 spin_lock_irqsave(&mddev->write_lock, flags);
418 bio2->bi_next = mddev->biolist;
419 mddev->biolist = bio2;
420 spin_unlock_irqrestore(&mddev->write_lock, flags);
421 wake_up(&mddev->sb_wait);
422 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200423 } else {
424 bio_put(bio2);
425 bio->bi_private = rdev;
426 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800427 }
NeilBrowna9701a32005-11-08 21:39:34 -0800428}
429
NeilBrown7bfa19f2005-06-21 17:17:28 -0700430void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
431 sector_t sector, int size, struct page *page)
432{
433 /* write first size bytes of page to sector of rdev
434 * Increment mddev->pending_writes before returning
435 * and decrement it on completion, waking up sb_wait
436 * if zero is reached.
437 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800438 *
439 * As we might need to resubmit the request if BIO_RW_BARRIER
440 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700441 */
442 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800443 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700444
445 bio->bi_bdev = rdev->bdev;
446 bio->bi_sector = sector;
447 bio_add_page(bio, page, size, 0);
448 bio->bi_private = rdev;
449 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800450 bio->bi_rw = rw;
451
NeilBrown7bfa19f2005-06-21 17:17:28 -0700452 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800453 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
454 struct bio *rbio;
455 rw |= (1<<BIO_RW_BARRIER);
456 rbio = bio_clone(bio, GFP_NOIO);
457 rbio->bi_private = bio;
458 rbio->bi_end_io = super_written_barrier;
459 submit_bio(rw, rbio);
460 } else
461 submit_bio(rw, bio);
462}
463
464void md_super_wait(mddev_t *mddev)
465{
466 /* wait for all superblock writes that were scheduled to complete.
467 * if any had to be retried (due to BARRIER problems), retry them
468 */
469 DEFINE_WAIT(wq);
470 for(;;) {
471 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
472 if (atomic_read(&mddev->pending_writes)==0)
473 break;
474 while (mddev->biolist) {
475 struct bio *bio;
476 spin_lock_irq(&mddev->write_lock);
477 bio = mddev->biolist;
478 mddev->biolist = bio->bi_next ;
479 bio->bi_next = NULL;
480 spin_unlock_irq(&mddev->write_lock);
481 submit_bio(bio->bi_rw, bio);
482 }
483 schedule();
484 }
485 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700486}
487
NeilBrown6712ecf2007-09-27 12:47:43 +0200488static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
NeilBrowna654b9d82005-06-21 17:17:27 -0700493int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct page *page, int rw)
495{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700496 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct completion event;
498 int ret;
499
500 rw |= (1 << BIO_RW_SYNC);
501
502 bio->bi_bdev = bdev;
503 bio->bi_sector = sector;
504 bio_add_page(bio, page, size, 0);
505 init_completion(&event);
506 bio->bi_private = &event;
507 bio->bi_end_io = bi_complete;
508 submit_bio(rw, bio);
509 wait_for_completion(&event);
510
511 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
512 bio_put(bio);
513 return ret;
514}
NeilBrowna8745db2006-01-06 00:20:34 -0800515EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
NeilBrown0002b272005-09-09 16:23:53 -0700517static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 char b[BDEVNAME_SIZE];
520 if (!rdev->sb_page) {
521 MD_BUG();
522 return -EINVAL;
523 }
524 if (rdev->sb_loaded)
525 return 0;
526
527
NeilBrown0002b272005-09-09 16:23:53 -0700528 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto fail;
530 rdev->sb_loaded = 1;
531 return 0;
532
533fail:
534 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
535 bdevname(rdev->bdev,b));
536 return -EINVAL;
537}
538
539static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
540{
541 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
542 (sb1->set_uuid1 == sb2->set_uuid1) &&
543 (sb1->set_uuid2 == sb2->set_uuid2) &&
544 (sb1->set_uuid3 == sb2->set_uuid3))
545
546 return 1;
547
548 return 0;
549}
550
551
552static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
553{
554 int ret;
555 mdp_super_t *tmp1, *tmp2;
556
557 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
558 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
559
560 if (!tmp1 || !tmp2) {
561 ret = 0;
562 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
563 goto abort;
564 }
565
566 *tmp1 = *sb1;
567 *tmp2 = *sb2;
568
569 /*
570 * nr_disks is not constant
571 */
572 tmp1->nr_disks = 0;
573 tmp2->nr_disks = 0;
574
575 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
576 ret = 0;
577 else
578 ret = 1;
579
580abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700581 kfree(tmp1);
582 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return ret;
584}
585
NeilBrown4d167f02007-05-09 02:35:37 -0700586
587static u32 md_csum_fold(u32 csum)
588{
589 csum = (csum & 0xffff) + (csum >> 16);
590 return (csum & 0xffff) + (csum >> 16);
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static unsigned int calc_sb_csum(mdp_super_t * sb)
594{
NeilBrown4d167f02007-05-09 02:35:37 -0700595 u64 newcsum = 0;
596 u32 *sb32 = (u32*)sb;
597 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 unsigned int disk_csum, csum;
599
600 disk_csum = sb->sb_csum;
601 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700602
603 for (i = 0; i < MD_SB_BYTES/4 ; i++)
604 newcsum += sb32[i];
605 csum = (newcsum & 0xffffffff) + (newcsum>>32);
606
607
608#ifdef CONFIG_ALPHA
609 /* This used to use csum_partial, which was wrong for several
610 * reasons including that different results are returned on
611 * different architectures. It isn't critical that we get exactly
612 * the same return value as before (we always csum_fold before
613 * testing, and that removes any differences). However as we
614 * know that csum_partial always returned a 16bit value on
615 * alphas, do a fold to maximise conformity to previous behaviour.
616 */
617 sb->sb_csum = md_csum_fold(disk_csum);
618#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700620#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return csum;
622}
623
624
625/*
626 * Handle superblock details.
627 * We want to be able to handle multiple superblock formats
628 * so we have a common interface to them all, and an array of
629 * different handlers.
630 * We rely on user-space to write the initial superblock, and support
631 * reading and updating of superblocks.
632 * Interface methods are:
633 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
634 * loads and validates a superblock on dev.
635 * if refdev != NULL, compare superblocks on both devices
636 * Return:
637 * 0 - dev has a superblock that is compatible with refdev
638 * 1 - dev has a superblock that is compatible and newer than refdev
639 * so dev should be used as the refdev in future
640 * -EINVAL superblock incompatible or invalid
641 * -othererror e.g. -EIO
642 *
643 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
644 * Verify that dev is acceptable into mddev.
645 * The first time, mddev->raid_disks will be 0, and data from
646 * dev should be merged in. Subsequent calls check that dev
647 * is new enough. Return 0 or -EINVAL
648 *
649 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
650 * Update the superblock for rdev with data in mddev
651 * This does not write to disc.
652 *
653 */
654
655struct super_type {
656 char *name;
657 struct module *owner;
658 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
659 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
660 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
661};
662
663/*
664 * load_super for 0.90.0
665 */
666static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
667{
668 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
669 mdp_super_t *sb;
670 int ret;
671 sector_t sb_offset;
672
673 /*
674 * Calculate the position of the superblock,
675 * it's at the end of the disk.
676 *
677 * It also happens to be a multiple of 4Kb.
678 */
679 sb_offset = calc_dev_sboffset(rdev->bdev);
680 rdev->sb_offset = sb_offset;
681
NeilBrown0002b272005-09-09 16:23:53 -0700682 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (ret) return ret;
684
685 ret = -EINVAL;
686
687 bdevname(rdev->bdev, b);
688 sb = (mdp_super_t*)page_address(rdev->sb_page);
689
690 if (sb->md_magic != MD_SB_MAGIC) {
691 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
692 b);
693 goto abort;
694 }
695
696 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800697 sb->minor_version < 90 ||
698 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
700 sb->major_version, sb->minor_version,
701 b);
702 goto abort;
703 }
704
705 if (sb->raid_disks <= 0)
706 goto abort;
707
NeilBrown4d167f02007-05-09 02:35:37 -0700708 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
710 b);
711 goto abort;
712 }
713
714 rdev->preferred_minor = sb->md_minor;
715 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700716 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
NeilBrowne11e93f2007-05-09 02:35:36 -0700718 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
719 if (sb->level != 1 && sb->level != 4
720 && sb->level != 5 && sb->level != 6
721 && sb->level != 10) {
722 /* FIXME use a better test */
723 printk(KERN_WARNING
724 "md: bitmaps not supported for this level.\n");
725 goto abort;
726 }
727 }
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (sb->level == LEVEL_MULTIPATH)
730 rdev->desc_nr = -1;
731 else
732 rdev->desc_nr = sb->this_disk.number;
733
734 if (refdev == 0)
735 ret = 1;
736 else {
737 __u64 ev1, ev2;
738 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
739 if (!uuid_equal(refsb, sb)) {
740 printk(KERN_WARNING "md: %s has different UUID to %s\n",
741 b, bdevname(refdev->bdev,b2));
742 goto abort;
743 }
744 if (!sb_equal(refsb, sb)) {
745 printk(KERN_WARNING "md: %s has same UUID"
746 " but different superblock to %s\n",
747 b, bdevname(refdev->bdev, b2));
748 goto abort;
749 }
750 ev1 = md_event(sb);
751 ev2 = md_event(refsb);
752 if (ev1 > ev2)
753 ret = 1;
754 else
755 ret = 0;
756 }
757 rdev->size = calc_dev_size(rdev, sb->chunk_size);
758
NeilBrown2bf071b2006-01-06 00:20:55 -0800759 if (rdev->size < sb->size && sb->level > 1)
760 /* "this cannot possibly happen" ... */
761 ret = -EINVAL;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 abort:
764 return ret;
765}
766
767/*
768 * validate_super for 0.90.0
769 */
770static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
771{
772 mdp_disk_t *desc;
773 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700774 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
NeilBrown41158c72005-06-21 17:17:25 -0700776 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800777 clear_bit(Faulty, &rdev->flags);
778 clear_bit(In_sync, &rdev->flags);
779 clear_bit(WriteMostly, &rdev->flags);
780 clear_bit(BarriersNotsupp, &rdev->flags);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (mddev->raid_disks == 0) {
783 mddev->major_version = 0;
784 mddev->minor_version = sb->minor_version;
785 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800786 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 mddev->chunk_size = sb->chunk_size;
788 mddev->ctime = sb->ctime;
789 mddev->utime = sb->utime;
790 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800791 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 mddev->layout = sb->layout;
793 mddev->raid_disks = sb->raid_disks;
794 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700795 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700796 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700797 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
NeilBrownf6705572006-03-27 01:18:11 -0800799 if (mddev->minor_version >= 91) {
800 mddev->reshape_position = sb->reshape_position;
801 mddev->delta_disks = sb->delta_disks;
802 mddev->new_level = sb->new_level;
803 mddev->new_layout = sb->new_layout;
804 mddev->new_chunk = sb->new_chunk;
805 } else {
806 mddev->reshape_position = MaxSector;
807 mddev->delta_disks = 0;
808 mddev->new_level = mddev->level;
809 mddev->new_layout = mddev->layout;
810 mddev->new_chunk = mddev->chunk_size;
811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (sb->state & (1<<MD_SB_CLEAN))
814 mddev->recovery_cp = MaxSector;
815 else {
816 if (sb->events_hi == sb->cp_events_hi &&
817 sb->events_lo == sb->cp_events_lo) {
818 mddev->recovery_cp = sb->recovery_cp;
819 } else
820 mddev->recovery_cp = 0;
821 }
822
823 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
824 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
825 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
826 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
827
828 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700829
830 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700831 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700832 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700833
NeilBrown41158c72005-06-21 17:17:25 -0700834 } else if (mddev->pers == NULL) {
835 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 ++ev1;
837 if (ev1 < mddev->events)
838 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700839 } else if (mddev->bitmap) {
840 /* if adding to array with a bitmap, then we can accept an
841 * older device ... but not too old.
842 */
NeilBrown41158c72005-06-21 17:17:25 -0700843 if (ev1 < mddev->bitmap->events_cleared)
844 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700845 } else {
846 if (ev1 < mddev->events)
847 /* just a hot-add of a new device, leave raid_disk at -1 */
848 return 0;
849 }
NeilBrown41158c72005-06-21 17:17:25 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 desc = sb->disks + rdev->desc_nr;
853
854 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800855 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700856 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
857 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800858 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 rdev->raid_disk = desc->raid_disk;
860 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700861 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
862 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700863 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800864 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return 0;
866}
867
868/*
869 * sync_super for 0.90.0
870 */
871static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
872{
873 mdp_super_t *sb;
874 struct list_head *tmp;
875 mdk_rdev_t *rdev2;
876 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /* make rdev->sb match mddev data..
880 *
881 * 1/ zero out disks
882 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
883 * 3/ any empty disks < next_spare become removed
884 *
885 * disks[0] gets initialised to REMOVED because
886 * we cannot be sure from other fields if it has
887 * been initialised or not.
888 */
889 int i;
890 int active=0, working=0,failed=0,spare=0,nr_disks=0;
891
NeilBrown61181562005-09-09 16:24:02 -0700892 rdev->sb_size = MD_SB_BYTES;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 sb = (mdp_super_t*)page_address(rdev->sb_page);
895
896 memset(sb, 0, sizeof(*sb));
897
898 sb->md_magic = MD_SB_MAGIC;
899 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sb->patch_version = mddev->patch_version;
901 sb->gvalid_words = 0; /* ignored */
902 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
903 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
904 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
905 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
906
907 sb->ctime = mddev->ctime;
908 sb->level = mddev->level;
909 sb->size = mddev->size;
910 sb->raid_disks = mddev->raid_disks;
911 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800912 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sb->utime = mddev->utime;
914 sb->state = 0;
915 sb->events_hi = (mddev->events>>32);
916 sb->events_lo = (u32)mddev->events;
917
NeilBrownf6705572006-03-27 01:18:11 -0800918 if (mddev->reshape_position == MaxSector)
919 sb->minor_version = 90;
920 else {
921 sb->minor_version = 91;
922 sb->reshape_position = mddev->reshape_position;
923 sb->new_level = mddev->new_level;
924 sb->delta_disks = mddev->delta_disks;
925 sb->new_layout = mddev->new_layout;
926 sb->new_chunk = mddev->new_chunk;
927 }
928 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (mddev->in_sync)
930 {
931 sb->recovery_cp = mddev->recovery_cp;
932 sb->cp_events_hi = (mddev->events>>32);
933 sb->cp_events_lo = (u32)mddev->events;
934 if (mddev->recovery_cp == MaxSector)
935 sb->state = (1<< MD_SB_CLEAN);
936 } else
937 sb->recovery_cp = 0;
938
939 sb->layout = mddev->layout;
940 sb->chunk_size = mddev->chunk_size;
941
NeilBrowna654b9d82005-06-21 17:17:27 -0700942 if (mddev->bitmap && mddev->bitmap_file == NULL)
943 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sb->disks[0].state = (1<<MD_DISK_REMOVED);
946 ITERATE_RDEV(mddev,rdev2,tmp) {
947 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800948 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800949 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
950 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800951 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800953 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800954 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 d = &sb->disks[rdev2->desc_nr];
956 nr_disks++;
957 d->number = rdev2->desc_nr;
958 d->major = MAJOR(rdev2->bdev->bd_dev);
959 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800960 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
961 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 d->raid_disk = rdev2->raid_disk;
963 else
964 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800965 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800967 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 d->state = (1<<MD_DISK_ACTIVE);
969 d->state |= (1<<MD_DISK_SYNC);
970 active++;
971 working++;
972 } else {
973 d->state = 0;
974 spare++;
975 working++;
976 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700977 if (test_bit(WriteMostly, &rdev2->flags))
978 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* now set the "removed" and "faulty" bits on any missing devices */
981 for (i=0 ; i < mddev->raid_disks ; i++) {
982 mdp_disk_t *d = &sb->disks[i];
983 if (d->state == 0 && d->number == 0) {
984 d->number = i;
985 d->raid_disk = i;
986 d->state = (1<<MD_DISK_REMOVED);
987 d->state |= (1<<MD_DISK_FAULTY);
988 failed++;
989 }
990 }
991 sb->nr_disks = nr_disks;
992 sb->active_disks = active;
993 sb->working_disks = working;
994 sb->failed_disks = failed;
995 sb->spare_disks = spare;
996
997 sb->this_disk = sb->disks[rdev->desc_nr];
998 sb->sb_csum = calc_sb_csum(sb);
999}
1000
1001/*
1002 * version 1 superblock
1003 */
1004
NeilBrown1c05b4b2006-10-21 10:24:08 -07001005static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001007 __le32 disk_csum;
1008 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 unsigned long long newcsum;
1010 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001011 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int i;
1013
1014 disk_csum = sb->sb_csum;
1015 sb->sb_csum = 0;
1016 newcsum = 0;
1017 for (i=0; size>=4; size -= 4 )
1018 newcsum += le32_to_cpu(*isuper++);
1019
1020 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001021 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1024 sb->sb_csum = disk_csum;
1025 return cpu_to_le32(csum);
1026}
1027
1028static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1029{
1030 struct mdp_superblock_1 *sb;
1031 int ret;
1032 sector_t sb_offset;
1033 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001034 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 /*
1037 * Calculate the position of the superblock.
1038 * It is always aligned to a 4K boundary and
1039 * depeding on minor_version, it can be:
1040 * 0: At least 8K, but less than 12K, from end of device
1041 * 1: At start of device
1042 * 2: 4K from start of device.
1043 */
1044 switch(minor_version) {
1045 case 0:
1046 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1047 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001048 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* convert from sectors to K */
1050 sb_offset /= 2;
1051 break;
1052 case 1:
1053 sb_offset = 0;
1054 break;
1055 case 2:
1056 sb_offset = 4;
1057 break;
1058 default:
1059 return -EINVAL;
1060 }
1061 rdev->sb_offset = sb_offset;
1062
NeilBrown0002b272005-09-09 16:23:53 -07001063 /* superblock is rarely larger than 1K, but it can be larger,
1064 * and it is safe to read 4k, so we do that
1065 */
1066 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (ret) return ret;
1068
1069
1070 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1071
1072 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1073 sb->major_version != cpu_to_le32(1) ||
1074 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1075 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001076 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return -EINVAL;
1078
1079 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1080 printk("md: invalid superblock checksum on %s\n",
1081 bdevname(rdev->bdev,b));
1082 return -EINVAL;
1083 }
1084 if (le64_to_cpu(sb->data_size) < 10) {
1085 printk("md: data_size too small on %s\n",
1086 bdevname(rdev->bdev,b));
1087 return -EINVAL;
1088 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001089 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1090 if (sb->level != cpu_to_le32(1) &&
1091 sb->level != cpu_to_le32(4) &&
1092 sb->level != cpu_to_le32(5) &&
1093 sb->level != cpu_to_le32(6) &&
1094 sb->level != cpu_to_le32(10)) {
1095 printk(KERN_WARNING
1096 "md: bitmaps not supported for this level.\n");
1097 return -EINVAL;
1098 }
1099 }
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 rdev->preferred_minor = 0xffff;
1102 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001103 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
NeilBrown0002b272005-09-09 16:23:53 -07001105 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001106 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001107 if (rdev->sb_size & bmask)
1108 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1109
NeilBrown31b65a02006-07-10 04:44:14 -07001110 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1111 rdev->desc_nr = -1;
1112 else
1113 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001116 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 else {
1118 __u64 ev1, ev2;
1119 struct mdp_superblock_1 *refsb =
1120 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1121
1122 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1123 sb->level != refsb->level ||
1124 sb->layout != refsb->layout ||
1125 sb->chunksize != refsb->chunksize) {
1126 printk(KERN_WARNING "md: %s has strangely different"
1127 " superblock to %s\n",
1128 bdevname(rdev->bdev,b),
1129 bdevname(refdev->bdev,b2));
1130 return -EINVAL;
1131 }
1132 ev1 = le64_to_cpu(sb->events);
1133 ev2 = le64_to_cpu(refsb->events);
1134
1135 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001136 ret = 1;
1137 else
1138 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 if (minor_version)
1141 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1142 else
1143 rdev->size = rdev->sb_offset;
1144 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1145 return -EINVAL;
1146 rdev->size = le64_to_cpu(sb->data_size)/2;
1147 if (le32_to_cpu(sb->chunksize))
1148 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001149
NeilBrown1c05b4b2006-10-21 10:24:08 -07001150 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001151 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001152 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
1155static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1156{
1157 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001158 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
NeilBrown41158c72005-06-21 17:17:25 -07001160 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001161 clear_bit(Faulty, &rdev->flags);
1162 clear_bit(In_sync, &rdev->flags);
1163 clear_bit(WriteMostly, &rdev->flags);
1164 clear_bit(BarriersNotsupp, &rdev->flags);
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (mddev->raid_disks == 0) {
1167 mddev->major_version = 1;
1168 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001169 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1171 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1172 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1173 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001174 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 mddev->layout = le32_to_cpu(sb->layout);
1176 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1177 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001178 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001179 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001180 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1183 memcpy(mddev->uuid, sb->set_uuid, 16);
1184
1185 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001186
NeilBrown71c08052005-09-09 16:23:51 -07001187 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001188 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001189 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001190
NeilBrownf6705572006-03-27 01:18:11 -08001191 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1192 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1193 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1194 mddev->new_level = le32_to_cpu(sb->new_level);
1195 mddev->new_layout = le32_to_cpu(sb->new_layout);
1196 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1197 } else {
1198 mddev->reshape_position = MaxSector;
1199 mddev->delta_disks = 0;
1200 mddev->new_level = mddev->level;
1201 mddev->new_layout = mddev->layout;
1202 mddev->new_chunk = mddev->chunk_size;
1203 }
1204
NeilBrown41158c72005-06-21 17:17:25 -07001205 } else if (mddev->pers == NULL) {
1206 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ++ev1;
1208 if (ev1 < mddev->events)
1209 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001210 } else if (mddev->bitmap) {
1211 /* If adding to array with a bitmap, then we can accept an
1212 * older device, but not too old.
1213 */
NeilBrown41158c72005-06-21 17:17:25 -07001214 if (ev1 < mddev->bitmap->events_cleared)
1215 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001216 } else {
1217 if (ev1 < mddev->events)
1218 /* just a hot-add of a new device, leave raid_disk at -1 */
1219 return 0;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (mddev->level != LEVEL_MULTIPATH) {
1222 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1224 switch(role) {
1225 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 break;
1227 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001228 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 break;
1230 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001231 if ((le32_to_cpu(sb->feature_map) &
1232 MD_FEATURE_RECOVERY_OFFSET))
1233 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1234 else
1235 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 rdev->raid_disk = role;
1237 break;
1238 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001239 if (sb->devflags & WriteMostly1)
1240 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001241 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001242 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
1245}
1246
1247static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1248{
1249 struct mdp_superblock_1 *sb;
1250 struct list_head *tmp;
1251 mdk_rdev_t *rdev2;
1252 int max_dev, i;
1253 /* make rdev->sb match mddev and rdev data. */
1254
1255 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1256
1257 sb->feature_map = 0;
1258 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001259 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 memset(sb->pad1, 0, sizeof(sb->pad1));
1261 memset(sb->pad2, 0, sizeof(sb->pad2));
1262 memset(sb->pad3, 0, sizeof(sb->pad3));
1263
1264 sb->utime = cpu_to_le64((__u64)mddev->utime);
1265 sb->events = cpu_to_le64(mddev->events);
1266 if (mddev->in_sync)
1267 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1268 else
1269 sb->resync_offset = cpu_to_le64(0);
1270
NeilBrown1c05b4b2006-10-21 10:24:08 -07001271 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001272
NeilBrownf0ca3402006-02-02 14:28:04 -08001273 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001274 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001275
NeilBrowna654b9d82005-06-21 17:17:27 -07001276 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1277 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001278 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001279 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001280
1281 if (rdev->raid_disk >= 0 &&
1282 !test_bit(In_sync, &rdev->flags) &&
1283 rdev->recovery_offset > 0) {
1284 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1285 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1286 }
1287
NeilBrownf6705572006-03-27 01:18:11 -08001288 if (mddev->reshape_position != MaxSector) {
1289 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1290 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1291 sb->new_layout = cpu_to_le32(mddev->new_layout);
1292 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1293 sb->new_level = cpu_to_le32(mddev->new_level);
1294 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1295 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 max_dev = 0;
1298 ITERATE_RDEV(mddev,rdev2,tmp)
1299 if (rdev2->desc_nr+1 > max_dev)
1300 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001301
1302 if (max_dev > le32_to_cpu(sb->max_dev))
1303 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 for (i=0; i<max_dev;i++)
1305 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1306
1307 ITERATE_RDEV(mddev,rdev2,tmp) {
1308 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001309 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001311 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001313 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1314 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 else
1316 sb->dev_roles[i] = cpu_to_le16(0xffff);
1317 }
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 sb->sb_csum = calc_sb_1_csum(sb);
1320}
1321
1322
Adrian Bunk75c96f82005-05-05 16:16:09 -07001323static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 [0] = {
1325 .name = "0.90.0",
1326 .owner = THIS_MODULE,
1327 .load_super = super_90_load,
1328 .validate_super = super_90_validate,
1329 .sync_super = super_90_sync,
1330 },
1331 [1] = {
1332 .name = "md-1",
1333 .owner = THIS_MODULE,
1334 .load_super = super_1_load,
1335 .validate_super = super_1_validate,
1336 .sync_super = super_1_sync,
1337 },
1338};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1341{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001342 struct list_head *tmp, *tmp2;
1343 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 ITERATE_RDEV(mddev1,rdev,tmp)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001346 ITERATE_RDEV(mddev2, rdev2, tmp2)
1347 if (rdev->bdev->bd_contains ==
1348 rdev2->bdev->bd_contains)
1349 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 return 0;
1352}
1353
1354static LIST_HEAD(pending_raid_disks);
1355
1356static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1357{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001358 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001359 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001360 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001361 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363 if (rdev->mddev) {
1364 MD_BUG();
1365 return -EINVAL;
1366 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001367 /* make sure rdev->size exceeds mddev->size */
1368 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001369 if (mddev->pers) {
1370 /* Cannot change size, so fail
1371 * If mddev->level <= 0, then we don't care
1372 * about aligning sizes (e.g. linear)
1373 */
1374 if (mddev->level > 0)
1375 return -ENOSPC;
1376 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001377 mddev->size = rdev->size;
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* Verify rdev->desc_nr is unique.
1381 * If it is -1, assign a free number, else
1382 * check number is not in use
1383 */
1384 if (rdev->desc_nr < 0) {
1385 int choice = 0;
1386 if (mddev->pers) choice = mddev->raid_disks;
1387 while (find_rdev_nr(mddev, choice))
1388 choice++;
1389 rdev->desc_nr = choice;
1390 } else {
1391 if (find_rdev_nr(mddev, rdev->desc_nr))
1392 return -EBUSY;
1393 }
NeilBrown19133a42005-11-08 21:39:35 -08001394 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001395 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001396 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001399 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001400
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001401 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001402 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001403
NeilBrownf637b9f2005-11-08 21:39:37 -08001404 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001405 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001406 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001407 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001408 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1409 kobject_del(&rdev->kobj);
1410 goto fail;
1411 }
1412 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001413 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001415
1416 fail:
1417 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1418 b, mdname(mddev));
1419 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
NeilBrown5792a282007-04-04 19:08:18 -07001422static void delayed_delete(struct work_struct *ws)
1423{
1424 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1425 kobject_del(&rdev->kobj);
1426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1429{
1430 char b[BDEVNAME_SIZE];
1431 if (!rdev->mddev) {
1432 MD_BUG();
1433 return;
1434 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001435 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 list_del_init(&rdev->same_set);
1437 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1438 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001439 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001440
1441 /* We need to delay this, otherwise we can deadlock when
1442 * writing to 'remove' to "dev/state"
1443 */
1444 INIT_WORK(&rdev->del_work, delayed_delete);
1445 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
1448/*
1449 * prevent the device from being mounted, repartitioned or
1450 * otherwise reused by a RAID array (or any other kernel
1451 * subsystem), by bd_claiming the device.
1452 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001453static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
1455 int err = 0;
1456 struct block_device *bdev;
1457 char b[BDEVNAME_SIZE];
1458
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001459 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (IS_ERR(bdev)) {
1461 printk(KERN_ERR "md: could not open %s.\n",
1462 __bdevname(dev, b));
1463 return PTR_ERR(bdev);
1464 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001465 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (err) {
1467 printk(KERN_ERR "md: could not bd_claim %s.\n",
1468 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001469 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 return err;
1471 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001472 if (!shared)
1473 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 rdev->bdev = bdev;
1475 return err;
1476}
1477
1478static void unlock_rdev(mdk_rdev_t *rdev)
1479{
1480 struct block_device *bdev = rdev->bdev;
1481 rdev->bdev = NULL;
1482 if (!bdev)
1483 MD_BUG();
1484 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001485 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488void md_autodetect_dev(dev_t dev);
1489
1490static void export_rdev(mdk_rdev_t * rdev)
1491{
1492 char b[BDEVNAME_SIZE];
1493 printk(KERN_INFO "md: export_rdev(%s)\n",
1494 bdevname(rdev->bdev,b));
1495 if (rdev->mddev)
1496 MD_BUG();
1497 free_disk_sb(rdev);
1498 list_del_init(&rdev->same_set);
1499#ifndef MODULE
1500 md_autodetect_dev(rdev->bdev->bd_dev);
1501#endif
1502 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001503 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
1506static void kick_rdev_from_array(mdk_rdev_t * rdev)
1507{
1508 unbind_rdev_from_array(rdev);
1509 export_rdev(rdev);
1510}
1511
1512static void export_array(mddev_t *mddev)
1513{
1514 struct list_head *tmp;
1515 mdk_rdev_t *rdev;
1516
1517 ITERATE_RDEV(mddev,rdev,tmp) {
1518 if (!rdev->mddev) {
1519 MD_BUG();
1520 continue;
1521 }
1522 kick_rdev_from_array(rdev);
1523 }
1524 if (!list_empty(&mddev->disks))
1525 MD_BUG();
1526 mddev->raid_disks = 0;
1527 mddev->major_version = 0;
1528}
1529
1530static void print_desc(mdp_disk_t *desc)
1531{
1532 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1533 desc->major,desc->minor,desc->raid_disk,desc->state);
1534}
1535
1536static void print_sb(mdp_super_t *sb)
1537{
1538 int i;
1539
1540 printk(KERN_INFO
1541 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1542 sb->major_version, sb->minor_version, sb->patch_version,
1543 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1544 sb->ctime);
1545 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1546 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1547 sb->md_minor, sb->layout, sb->chunk_size);
1548 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1549 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1550 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1551 sb->failed_disks, sb->spare_disks,
1552 sb->sb_csum, (unsigned long)sb->events_lo);
1553
1554 printk(KERN_INFO);
1555 for (i = 0; i < MD_SB_DISKS; i++) {
1556 mdp_disk_t *desc;
1557
1558 desc = sb->disks + i;
1559 if (desc->number || desc->major || desc->minor ||
1560 desc->raid_disk || (desc->state && (desc->state != 4))) {
1561 printk(" D %2d: ", i);
1562 print_desc(desc);
1563 }
1564 }
1565 printk(KERN_INFO "md: THIS: ");
1566 print_desc(&sb->this_disk);
1567
1568}
1569
1570static void print_rdev(mdk_rdev_t *rdev)
1571{
1572 char b[BDEVNAME_SIZE];
1573 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1574 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001575 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1576 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (rdev->sb_loaded) {
1578 printk(KERN_INFO "md: rdev superblock:\n");
1579 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1580 } else
1581 printk(KERN_INFO "md: no rdev superblock!\n");
1582}
1583
Adrian Bunk5e563412006-06-26 00:27:42 -07001584static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
1586 struct list_head *tmp, *tmp2;
1587 mdk_rdev_t *rdev;
1588 mddev_t *mddev;
1589 char b[BDEVNAME_SIZE];
1590
1591 printk("\n");
1592 printk("md: **********************************\n");
1593 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1594 printk("md: **********************************\n");
1595 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
NeilBrown32a76272005-06-21 17:17:14 -07001597 if (mddev->bitmap)
1598 bitmap_print_sb(mddev->bitmap);
1599 else
1600 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 ITERATE_RDEV(mddev,rdev,tmp2)
1602 printk("<%s>", bdevname(rdev->bdev,b));
1603 printk("\n");
1604
1605 ITERATE_RDEV(mddev,rdev,tmp2)
1606 print_rdev(rdev);
1607 }
1608 printk("md: **********************************\n");
1609 printk("\n");
1610}
1611
1612
NeilBrown42543762006-06-26 00:27:57 -07001613static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
NeilBrown42543762006-06-26 00:27:57 -07001615 /* Update each superblock (in-memory image), but
1616 * if we are allowed to, skip spares which already
1617 * have the right event counter, or have one earlier
1618 * (which would mean they aren't being marked as dirty
1619 * with the rest of the array)
1620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 mdk_rdev_t *rdev;
1622 struct list_head *tmp;
1623
1624 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrown42543762006-06-26 00:27:57 -07001625 if (rdev->sb_events == mddev->events ||
1626 (nospares &&
1627 rdev->raid_disk < 0 &&
1628 (rdev->sb_events&1)==0 &&
1629 rdev->sb_events+1 == mddev->events)) {
1630 /* Don't update this superblock */
1631 rdev->sb_loaded = 2;
1632 } else {
1633 super_types[mddev->major_version].
1634 sync_super(mddev, rdev);
1635 rdev->sb_loaded = 1;
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638}
1639
NeilBrown850b2b42006-10-03 01:15:46 -07001640static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct list_head *tmp;
1643 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001644 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001645 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001648 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001649
NeilBrown850b2b42006-10-03 01:15:46 -07001650 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1651 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1652 force_change = 1;
1653 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1654 /* just a clean<-> dirty transition, possibly leave spares alone,
1655 * though if events isn't the right even/odd, we will have to do
1656 * spares after all
1657 */
1658 nospares = 1;
1659 if (force_change)
1660 nospares = 0;
1661 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001662 /* If the array is degraded, then skipping spares is both
1663 * dangerous and fairly pointless.
1664 * Dangerous because a device that was removed from the array
1665 * might have a event_count that still looks up-to-date,
1666 * so it can be re-added without a resync.
1667 * Pointless because if there are any spares to skip,
1668 * then a recovery will happen and soon that array won't
1669 * be degraded any more and the spare can go back to sleep then.
1670 */
NeilBrown850b2b42006-10-03 01:15:46 -07001671 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001672
NeilBrown06d91a52005-06-21 17:17:12 -07001673 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001675
1676 /* If this is just a dirty<->clean transition, and the array is clean
1677 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001678 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001679 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001680 && (mddev->events & 1)
1681 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001682 mddev->events--;
1683 else {
1684 /* otherwise we have to go forward and ... */
1685 mddev->events ++;
1686 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1687 /* .. if the array isn't clean, insist on an odd 'events' */
1688 if ((mddev->events&1)==0) {
1689 mddev->events++;
1690 nospares = 0;
1691 }
1692 } else {
1693 /* otherwise insist on an even 'events' (for clean states) */
1694 if ((mddev->events&1)) {
1695 mddev->events++;
1696 nospares = 0;
1697 }
1698 }
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 if (!mddev->events) {
1702 /*
1703 * oops, this 64-bit counter should never wrap.
1704 * Either we are in around ~1 trillion A.C., assuming
1705 * 1 reboot per second, or we have a bug:
1706 */
1707 MD_BUG();
1708 mddev->events --;
1709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 /*
1712 * do not write anything to disk if using
1713 * nonpersistent superblocks
1714 */
NeilBrown06d91a52005-06-21 17:17:12 -07001715 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001716 if (!mddev->external)
1717 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1718
NeilBrowna9701a32005-11-08 21:39:34 -08001719 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001720 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001722 }
NeilBrowne6910632008-02-06 01:39:51 -08001723 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001724 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 dprintk(KERN_INFO
1727 "md: updating %s RAID superblock on device (in sync %d)\n",
1728 mdname(mddev),mddev->in_sync);
1729
NeilBrown4ad13662007-07-17 04:06:13 -07001730 bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 ITERATE_RDEV(mddev,rdev,tmp) {
1732 char b[BDEVNAME_SIZE];
1733 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001734 if (rdev->sb_loaded != 1)
1735 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001736 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 dprintk("(skipping faulty ");
1738
1739 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001740 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001741 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001742 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001743 rdev->sb_page);
1744 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1745 bdevname(rdev->bdev,b),
1746 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001747 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 } else
1750 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001751 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /* only need to write one superblock... */
1753 break;
1754 }
NeilBrowna9701a32005-11-08 21:39:34 -08001755 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001756 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001757
NeilBrowna9701a32005-11-08 21:39:34 -08001758 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001759 if (mddev->in_sync != sync_req ||
1760 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001761 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001762 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001763 goto repeat;
1764 }
NeilBrown850b2b42006-10-03 01:15:46 -07001765 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001766 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001767 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
NeilBrownbce74da2006-01-06 00:20:41 -08001771/* words written to sysfs files may, or my not, be \n terminated.
1772 * We want to accept with case. For this we use cmd_match.
1773 */
1774static int cmd_match(const char *cmd, const char *str)
1775{
1776 /* See if cmd, written into a sysfs file, matches
1777 * str. They must either be the same, or cmd can
1778 * have a trailing newline
1779 */
1780 while (*cmd && *str && *cmd == *str) {
1781 cmd++;
1782 str++;
1783 }
1784 if (*cmd == '\n')
1785 cmd++;
1786 if (*str || *cmd)
1787 return 0;
1788 return 1;
1789}
1790
NeilBrown86e6ffd2005-11-08 21:39:24 -08001791struct rdev_sysfs_entry {
1792 struct attribute attr;
1793 ssize_t (*show)(mdk_rdev_t *, char *);
1794 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1795};
1796
1797static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001798state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001799{
1800 char *sep = "";
1801 int len=0;
1802
NeilBrownb2d444d2005-11-08 21:39:31 -08001803 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001804 len+= sprintf(page+len, "%sfaulty",sep);
1805 sep = ",";
1806 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001807 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001808 len += sprintf(page+len, "%sin_sync",sep);
1809 sep = ",";
1810 }
NeilBrownf6556752006-06-26 00:28:01 -07001811 if (test_bit(WriteMostly, &rdev->flags)) {
1812 len += sprintf(page+len, "%swrite_mostly",sep);
1813 sep = ",";
1814 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001815 if (!test_bit(Faulty, &rdev->flags) &&
1816 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001817 len += sprintf(page+len, "%sspare", sep);
1818 sep = ",";
1819 }
1820 return len+sprintf(page+len, "\n");
1821}
1822
NeilBrown45dc2de2006-06-26 00:27:58 -07001823static ssize_t
1824state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1825{
1826 /* can write
1827 * faulty - simulates and error
1828 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001829 * writemostly - sets write_mostly
1830 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001831 */
1832 int err = -EINVAL;
1833 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1834 md_error(rdev->mddev, rdev);
1835 err = 0;
1836 } else if (cmd_match(buf, "remove")) {
1837 if (rdev->raid_disk >= 0)
1838 err = -EBUSY;
1839 else {
1840 mddev_t *mddev = rdev->mddev;
1841 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001842 if (mddev->pers)
1843 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001844 md_new_event(mddev);
1845 err = 0;
1846 }
NeilBrownf6556752006-06-26 00:28:01 -07001847 } else if (cmd_match(buf, "writemostly")) {
1848 set_bit(WriteMostly, &rdev->flags);
1849 err = 0;
1850 } else if (cmd_match(buf, "-writemostly")) {
1851 clear_bit(WriteMostly, &rdev->flags);
1852 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001853 }
1854 return err ? err : len;
1855}
NeilBrown80ca3a42006-07-10 04:44:18 -07001856static struct rdev_sysfs_entry rdev_state =
1857__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001858
1859static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001860super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001861{
1862 if (rdev->sb_loaded && rdev->sb_size) {
1863 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1864 return rdev->sb_size;
1865 } else
1866 return 0;
1867}
NeilBrown96de1e62005-11-08 21:39:39 -08001868static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1869
NeilBrown4dbcdc72006-01-06 00:20:52 -08001870static ssize_t
1871errors_show(mdk_rdev_t *rdev, char *page)
1872{
1873 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1874}
1875
1876static ssize_t
1877errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1878{
1879 char *e;
1880 unsigned long n = simple_strtoul(buf, &e, 10);
1881 if (*buf && (*e == 0 || *e == '\n')) {
1882 atomic_set(&rdev->corrected_errors, n);
1883 return len;
1884 }
1885 return -EINVAL;
1886}
1887static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001888__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001889
NeilBrown014236d2006-01-06 00:20:55 -08001890static ssize_t
1891slot_show(mdk_rdev_t *rdev, char *page)
1892{
1893 if (rdev->raid_disk < 0)
1894 return sprintf(page, "none\n");
1895 else
1896 return sprintf(page, "%d\n", rdev->raid_disk);
1897}
1898
1899static ssize_t
1900slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1901{
1902 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001903 int err;
1904 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001905 int slot = simple_strtoul(buf, &e, 10);
1906 if (strncmp(buf, "none", 4)==0)
1907 slot = -1;
1908 else if (e==buf || (*e && *e!= '\n'))
1909 return -EINVAL;
NeilBrownc303da62008-02-06 01:39:51 -08001910 if (rdev->mddev->pers) {
1911 /* Setting 'slot' on an active array requires also
1912 * updating the 'rd%d' link, and communicating
1913 * with the personality with ->hot_*_disk.
1914 * For now we only support removing
1915 * failed/spare devices. This normally happens automatically,
1916 * but not when the metadata is externally managed.
1917 */
1918 if (slot != -1)
1919 return -EBUSY;
1920 if (rdev->raid_disk == -1)
1921 return -EEXIST;
1922 /* personality does all needed checks */
1923 if (rdev->mddev->pers->hot_add_disk == NULL)
1924 return -EINVAL;
1925 err = rdev->mddev->pers->
1926 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1927 if (err)
1928 return err;
1929 sprintf(nm, "rd%d", rdev->raid_disk);
1930 sysfs_remove_link(&rdev->mddev->kobj, nm);
1931 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1932 md_wakeup_thread(rdev->mddev->thread);
1933 } else {
1934 if (slot >= rdev->mddev->raid_disks)
1935 return -ENOSPC;
1936 rdev->raid_disk = slot;
1937 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001938 clear_bit(Faulty, &rdev->flags);
1939 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08001940 set_bit(In_sync, &rdev->flags);
1941 }
NeilBrown014236d2006-01-06 00:20:55 -08001942 return len;
1943}
1944
1945
1946static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001947__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001948
NeilBrown93c8cad2006-01-06 00:20:56 -08001949static ssize_t
1950offset_show(mdk_rdev_t *rdev, char *page)
1951{
Andrew Morton6961ece2006-01-06 00:20:59 -08001952 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001953}
1954
1955static ssize_t
1956offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1957{
1958 char *e;
1959 unsigned long long offset = simple_strtoull(buf, &e, 10);
1960 if (e==buf || (*e && *e != '\n'))
1961 return -EINVAL;
1962 if (rdev->mddev->pers)
1963 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001964 if (rdev->size && rdev->mddev->external)
1965 /* Must set offset before size, so overlap checks
1966 * can be sane */
1967 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08001968 rdev->data_offset = offset;
1969 return len;
1970}
1971
1972static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001973__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001974
NeilBrown83303b62006-01-06 00:21:06 -08001975static ssize_t
1976rdev_size_show(mdk_rdev_t *rdev, char *page)
1977{
1978 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1979}
1980
NeilBrownc5d79ad2008-02-06 01:39:54 -08001981static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
1982{
1983 /* check if two start/length pairs overlap */
1984 if (s1+l1 <= s2)
1985 return 0;
1986 if (s2+l2 <= s1)
1987 return 0;
1988 return 1;
1989}
1990
NeilBrown83303b62006-01-06 00:21:06 -08001991static ssize_t
1992rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1993{
1994 char *e;
1995 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001996 unsigned long long oldsize = rdev->size;
NeilBrown83303b62006-01-06 00:21:06 -08001997 if (e==buf || (*e && *e != '\n'))
1998 return -EINVAL;
1999 if (rdev->mddev->pers)
2000 return -EBUSY;
2001 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002002 if (size > oldsize && rdev->mddev->external) {
2003 /* need to check that all other rdevs with the same ->bdev
2004 * do not overlap. We need to unlock the mddev to avoid
2005 * a deadlock. We have already changed rdev->size, and if
2006 * we have to change it back, we will have the lock again.
2007 */
2008 mddev_t *mddev;
2009 int overlap = 0;
2010 struct list_head *tmp, *tmp2;
2011
2012 mddev_unlock(rdev->mddev);
2013 ITERATE_MDDEV(mddev, tmp) {
2014 mdk_rdev_t *rdev2;
2015
2016 mddev_lock(mddev);
2017 ITERATE_RDEV(mddev, rdev2, tmp2)
2018 if (test_bit(AllReserved, &rdev2->flags) ||
2019 (rdev->bdev == rdev2->bdev &&
2020 rdev != rdev2 &&
2021 overlaps(rdev->data_offset, rdev->size,
2022 rdev2->data_offset, rdev2->size))) {
2023 overlap = 1;
2024 break;
2025 }
2026 mddev_unlock(mddev);
2027 if (overlap) {
2028 mddev_put(mddev);
2029 break;
2030 }
2031 }
2032 mddev_lock(rdev->mddev);
2033 if (overlap) {
2034 /* Someone else could have slipped in a size
2035 * change here, but doing so is just silly.
2036 * We put oldsize back because we *know* it is
2037 * safe, and trust userspace not to race with
2038 * itself
2039 */
2040 rdev->size = oldsize;
2041 return -EBUSY;
2042 }
2043 }
NeilBrown83303b62006-01-06 00:21:06 -08002044 if (size < rdev->mddev->size || rdev->mddev->size == 0)
2045 rdev->mddev->size = size;
2046 return len;
2047}
2048
2049static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002050__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002051
NeilBrown86e6ffd2005-11-08 21:39:24 -08002052static struct attribute *rdev_default_attrs[] = {
2053 &rdev_state.attr,
2054 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002055 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002056 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002057 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002058 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002059 NULL,
2060};
2061static ssize_t
2062rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2063{
2064 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2065 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2066
2067 if (!entry->show)
2068 return -EIO;
2069 return entry->show(rdev, page);
2070}
2071
2072static ssize_t
2073rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2074 const char *page, size_t length)
2075{
2076 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2077 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
2078
2079 if (!entry->store)
2080 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002081 if (!capable(CAP_SYS_ADMIN))
2082 return -EACCES;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002083 return entry->store(rdev, page, length);
2084}
2085
2086static void rdev_free(struct kobject *ko)
2087{
2088 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2089 kfree(rdev);
2090}
2091static struct sysfs_ops rdev_sysfs_ops = {
2092 .show = rdev_attr_show,
2093 .store = rdev_attr_store,
2094};
2095static struct kobj_type rdev_ktype = {
2096 .release = rdev_free,
2097 .sysfs_ops = &rdev_sysfs_ops,
2098 .default_attrs = rdev_default_attrs,
2099};
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101/*
2102 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2103 *
2104 * mark the device faulty if:
2105 *
2106 * - the device is nonexistent (zero size)
2107 * - the device has no valid superblock
2108 *
2109 * a faulty rdev _never_ has rdev->sb set.
2110 */
2111static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2112{
2113 char b[BDEVNAME_SIZE];
2114 int err;
2115 mdk_rdev_t *rdev;
2116 sector_t size;
2117
NeilBrown9ffae0c2006-01-06 00:20:32 -08002118 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 if (!rdev) {
2120 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2121 return ERR_PTR(-ENOMEM);
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 if ((err = alloc_disk_sb(rdev)))
2125 goto abort_free;
2126
NeilBrownc5d79ad2008-02-06 01:39:54 -08002127 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (err)
2129 goto abort_free;
2130
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002131 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002134 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002135 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002136 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002138 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002140 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002141 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2144 if (!size) {
2145 printk(KERN_WARNING
2146 "md: %s has zero or unknown size, marking faulty!\n",
2147 bdevname(rdev->bdev,b));
2148 err = -EINVAL;
2149 goto abort_free;
2150 }
2151
2152 if (super_format >= 0) {
2153 err = super_types[super_format].
2154 load_super(rdev, NULL, super_minor);
2155 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002156 printk(KERN_WARNING
2157 "md: %s does not have a valid v%d.%d "
2158 "superblock, not importing!\n",
2159 bdevname(rdev->bdev,b),
2160 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 goto abort_free;
2162 }
2163 if (err < 0) {
2164 printk(KERN_WARNING
2165 "md: could not read %s's sb, not importing!\n",
2166 bdevname(rdev->bdev,b));
2167 goto abort_free;
2168 }
2169 }
2170 INIT_LIST_HEAD(&rdev->same_set);
2171
2172 return rdev;
2173
2174abort_free:
2175 if (rdev->sb_page) {
2176 if (rdev->bdev)
2177 unlock_rdev(rdev);
2178 free_disk_sb(rdev);
2179 }
2180 kfree(rdev);
2181 return ERR_PTR(err);
2182}
2183
2184/*
2185 * Check a full RAID array for plausibility
2186 */
2187
2188
NeilBrowna757e642005-04-16 15:26:42 -07002189static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
2191 int i;
2192 struct list_head *tmp;
2193 mdk_rdev_t *rdev, *freshest;
2194 char b[BDEVNAME_SIZE];
2195
2196 freshest = NULL;
2197 ITERATE_RDEV(mddev,rdev,tmp)
2198 switch (super_types[mddev->major_version].
2199 load_super(rdev, freshest, mddev->minor_version)) {
2200 case 1:
2201 freshest = rdev;
2202 break;
2203 case 0:
2204 break;
2205 default:
2206 printk( KERN_ERR \
2207 "md: fatal superblock inconsistency in %s"
2208 " -- removing from array\n",
2209 bdevname(rdev->bdev,b));
2210 kick_rdev_from_array(rdev);
2211 }
2212
2213
2214 super_types[mddev->major_version].
2215 validate_super(mddev, freshest);
2216
2217 i = 0;
2218 ITERATE_RDEV(mddev,rdev,tmp) {
2219 if (rdev != freshest)
2220 if (super_types[mddev->major_version].
2221 validate_super(mddev, rdev)) {
2222 printk(KERN_WARNING "md: kicking non-fresh %s"
2223 " from array!\n",
2224 bdevname(rdev->bdev,b));
2225 kick_rdev_from_array(rdev);
2226 continue;
2227 }
2228 if (mddev->level == LEVEL_MULTIPATH) {
2229 rdev->desc_nr = i++;
2230 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002231 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002232 } else if (rdev->raid_disk >= mddev->raid_disks) {
2233 rdev->raid_disk = -1;
2234 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 }
2237
2238
2239
2240 if (mddev->recovery_cp != MaxSector &&
2241 mddev->level >= 1)
2242 printk(KERN_ERR "md: %s: raid array is not clean"
2243 " -- starting background reconstruction\n",
2244 mdname(mddev));
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
NeilBrowneae17012005-11-08 21:39:23 -08002248static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002249safe_delay_show(mddev_t *mddev, char *page)
2250{
2251 int msec = (mddev->safemode_delay*1000)/HZ;
2252 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2253}
2254static ssize_t
2255safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2256{
2257 int scale=1;
2258 int dot=0;
2259 int i;
2260 unsigned long msec;
2261 char buf[30];
2262 char *e;
2263 /* remove a period, and count digits after it */
2264 if (len >= sizeof(buf))
2265 return -EINVAL;
2266 strlcpy(buf, cbuf, len);
2267 buf[len] = 0;
2268 for (i=0; i<len; i++) {
2269 if (dot) {
2270 if (isdigit(buf[i])) {
2271 buf[i-1] = buf[i];
2272 scale *= 10;
2273 }
2274 buf[i] = 0;
2275 } else if (buf[i] == '.') {
2276 dot=1;
2277 buf[i] = 0;
2278 }
2279 }
2280 msec = simple_strtoul(buf, &e, 10);
2281 if (e == buf || (*e && *e != '\n'))
2282 return -EINVAL;
2283 msec = (msec * 1000) / scale;
2284 if (msec == 0)
2285 mddev->safemode_delay = 0;
2286 else {
2287 mddev->safemode_delay = (msec*HZ)/1000;
2288 if (mddev->safemode_delay == 0)
2289 mddev->safemode_delay = 1;
2290 }
2291 return len;
2292}
2293static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002294__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002295
2296static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002297level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002298{
NeilBrown2604b702006-01-06 00:20:36 -08002299 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002300 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002301 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002302 else if (mddev->clevel[0])
2303 return sprintf(page, "%s\n", mddev->clevel);
2304 else if (mddev->level != LEVEL_NONE)
2305 return sprintf(page, "%d\n", mddev->level);
2306 else
2307 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002308}
2309
NeilBrownd9d166c2006-01-06 00:20:51 -08002310static ssize_t
2311level_store(mddev_t *mddev, const char *buf, size_t len)
2312{
2313 int rv = len;
2314 if (mddev->pers)
2315 return -EBUSY;
2316 if (len == 0)
2317 return 0;
2318 if (len >= sizeof(mddev->clevel))
2319 return -ENOSPC;
2320 strncpy(mddev->clevel, buf, len);
2321 if (mddev->clevel[len-1] == '\n')
2322 len--;
2323 mddev->clevel[len] = 0;
2324 mddev->level = LEVEL_NONE;
2325 return rv;
2326}
2327
2328static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002329__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002330
NeilBrownd4dbd022006-06-26 00:27:59 -07002331
2332static ssize_t
2333layout_show(mddev_t *mddev, char *page)
2334{
2335 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002336 if (mddev->reshape_position != MaxSector &&
2337 mddev->layout != mddev->new_layout)
2338 return sprintf(page, "%d (%d)\n",
2339 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002340 return sprintf(page, "%d\n", mddev->layout);
2341}
2342
2343static ssize_t
2344layout_store(mddev_t *mddev, const char *buf, size_t len)
2345{
2346 char *e;
2347 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002348
2349 if (!*buf || (*e && *e != '\n'))
2350 return -EINVAL;
2351
NeilBrown08a02ec2007-05-09 02:35:38 -07002352 if (mddev->pers)
2353 return -EBUSY;
2354 if (mddev->reshape_position != MaxSector)
2355 mddev->new_layout = n;
2356 else
2357 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002358 return len;
2359}
2360static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002361__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002362
2363
NeilBrowneae17012005-11-08 21:39:23 -08002364static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002365raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002366{
NeilBrownbb636542005-11-08 21:39:45 -08002367 if (mddev->raid_disks == 0)
2368 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002369 if (mddev->reshape_position != MaxSector &&
2370 mddev->delta_disks != 0)
2371 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2372 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002373 return sprintf(page, "%d\n", mddev->raid_disks);
2374}
2375
NeilBrownda943b992006-01-06 00:20:54 -08002376static int update_raid_disks(mddev_t *mddev, int raid_disks);
2377
2378static ssize_t
2379raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2380{
NeilBrownda943b992006-01-06 00:20:54 -08002381 char *e;
2382 int rv = 0;
2383 unsigned long n = simple_strtoul(buf, &e, 10);
2384
2385 if (!*buf || (*e && *e != '\n'))
2386 return -EINVAL;
2387
2388 if (mddev->pers)
2389 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002390 else if (mddev->reshape_position != MaxSector) {
2391 int olddisks = mddev->raid_disks - mddev->delta_disks;
2392 mddev->delta_disks = n - olddisks;
2393 mddev->raid_disks = n;
2394 } else
NeilBrownda943b992006-01-06 00:20:54 -08002395 mddev->raid_disks = n;
2396 return rv ? rv : len;
2397}
2398static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002399__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002400
NeilBrown24dd4692005-11-08 21:39:26 -08002401static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002402chunk_size_show(mddev_t *mddev, char *page)
2403{
NeilBrown08a02ec2007-05-09 02:35:38 -07002404 if (mddev->reshape_position != MaxSector &&
2405 mddev->chunk_size != mddev->new_chunk)
2406 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2407 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002408 return sprintf(page, "%d\n", mddev->chunk_size);
2409}
2410
2411static ssize_t
2412chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2413{
2414 /* can only set chunk_size if array is not yet active */
2415 char *e;
2416 unsigned long n = simple_strtoul(buf, &e, 10);
2417
NeilBrown3b343802006-01-06 00:20:47 -08002418 if (!*buf || (*e && *e != '\n'))
2419 return -EINVAL;
2420
NeilBrown08a02ec2007-05-09 02:35:38 -07002421 if (mddev->pers)
2422 return -EBUSY;
2423 else if (mddev->reshape_position != MaxSector)
2424 mddev->new_chunk = n;
2425 else
2426 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002427 return len;
2428}
2429static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002430__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002431
NeilBrowna94213b2006-06-26 00:28:00 -07002432static ssize_t
2433resync_start_show(mddev_t *mddev, char *page)
2434{
2435 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2436}
2437
2438static ssize_t
2439resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2440{
2441 /* can only set chunk_size if array is not yet active */
2442 char *e;
2443 unsigned long long n = simple_strtoull(buf, &e, 10);
2444
2445 if (mddev->pers)
2446 return -EBUSY;
2447 if (!*buf || (*e && *e != '\n'))
2448 return -EINVAL;
2449
2450 mddev->recovery_cp = n;
2451 return len;
2452}
2453static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002454__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002455
NeilBrown9e653b62006-06-26 00:27:58 -07002456/*
2457 * The array state can be:
2458 *
2459 * clear
2460 * No devices, no size, no level
2461 * Equivalent to STOP_ARRAY ioctl
2462 * inactive
2463 * May have some settings, but array is not active
2464 * all IO results in error
2465 * When written, doesn't tear down array, but just stops it
2466 * suspended (not supported yet)
2467 * All IO requests will block. The array can be reconfigured.
2468 * Writing this, if accepted, will block until array is quiessent
2469 * readonly
2470 * no resync can happen. no superblocks get written.
2471 * write requests fail
2472 * read-auto
2473 * like readonly, but behaves like 'clean' on a write request.
2474 *
2475 * clean - no pending writes, but otherwise active.
2476 * When written to inactive array, starts without resync
2477 * If a write request arrives then
2478 * if metadata is known, mark 'dirty' and switch to 'active'.
2479 * if not known, block and switch to write-pending
2480 * If written to an active array that has pending writes, then fails.
2481 * active
2482 * fully active: IO and resync can be happening.
2483 * When written to inactive array, starts with resync
2484 *
2485 * write-pending
2486 * clean, but writes are blocked waiting for 'active' to be written.
2487 *
2488 * active-idle
2489 * like active, but no writes have been seen for a while (100msec).
2490 *
2491 */
2492enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2493 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002494static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002495 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2496 "write-pending", "active-idle", NULL };
2497
2498static int match_word(const char *word, char **list)
2499{
2500 int n;
2501 for (n=0; list[n]; n++)
2502 if (cmd_match(word, list[n]))
2503 break;
2504 return n;
2505}
2506
2507static ssize_t
2508array_state_show(mddev_t *mddev, char *page)
2509{
2510 enum array_state st = inactive;
2511
2512 if (mddev->pers)
2513 switch(mddev->ro) {
2514 case 1:
2515 st = readonly;
2516 break;
2517 case 2:
2518 st = read_auto;
2519 break;
2520 case 0:
2521 if (mddev->in_sync)
2522 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002523 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2524 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002525 else if (mddev->safemode)
2526 st = active_idle;
2527 else
2528 st = active;
2529 }
2530 else {
2531 if (list_empty(&mddev->disks) &&
2532 mddev->raid_disks == 0 &&
2533 mddev->size == 0)
2534 st = clear;
2535 else
2536 st = inactive;
2537 }
2538 return sprintf(page, "%s\n", array_states[st]);
2539}
2540
2541static int do_md_stop(mddev_t * mddev, int ro);
2542static int do_md_run(mddev_t * mddev);
2543static int restart_array(mddev_t *mddev);
2544
2545static ssize_t
2546array_state_store(mddev_t *mddev, const char *buf, size_t len)
2547{
2548 int err = -EINVAL;
2549 enum array_state st = match_word(buf, array_states);
2550 switch(st) {
2551 case bad_word:
2552 break;
2553 case clear:
2554 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002555 if (atomic_read(&mddev->active) > 1)
2556 return -EBUSY;
2557 err = do_md_stop(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002558 break;
2559 case inactive:
2560 /* stopping an active array */
2561 if (mddev->pers) {
2562 if (atomic_read(&mddev->active) > 1)
2563 return -EBUSY;
2564 err = do_md_stop(mddev, 2);
NeilBrowne6910632008-02-06 01:39:51 -08002565 } else
2566 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002567 break;
2568 case suspended:
2569 break; /* not supported yet */
2570 case readonly:
2571 if (mddev->pers)
2572 err = do_md_stop(mddev, 1);
2573 else {
2574 mddev->ro = 1;
2575 err = do_md_run(mddev);
2576 }
2577 break;
2578 case read_auto:
2579 /* stopping an active array */
2580 if (mddev->pers) {
2581 err = do_md_stop(mddev, 1);
2582 if (err == 0)
2583 mddev->ro = 2; /* FIXME mark devices writable */
2584 } else {
2585 mddev->ro = 2;
2586 err = do_md_run(mddev);
2587 }
2588 break;
2589 case clean:
2590 if (mddev->pers) {
2591 restart_array(mddev);
2592 spin_lock_irq(&mddev->write_lock);
2593 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002594 if (mddev->in_sync == 0) {
2595 mddev->in_sync = 1;
2596 if (mddev->persistent)
2597 set_bit(MD_CHANGE_CLEAN,
2598 &mddev->flags);
2599 }
2600 err = 0;
2601 } else
2602 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002603 spin_unlock_irq(&mddev->write_lock);
2604 } else {
2605 mddev->ro = 0;
2606 mddev->recovery_cp = MaxSector;
2607 err = do_md_run(mddev);
2608 }
2609 break;
2610 case active:
2611 if (mddev->pers) {
2612 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002613 if (mddev->external)
2614 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002615 wake_up(&mddev->sb_wait);
2616 err = 0;
2617 } else {
2618 mddev->ro = 0;
2619 err = do_md_run(mddev);
2620 }
2621 break;
2622 case write_pending:
2623 case active_idle:
2624 /* these cannot be set */
2625 break;
2626 }
2627 if (err)
2628 return err;
2629 else
2630 return len;
2631}
NeilBrown80ca3a42006-07-10 04:44:18 -07002632static struct md_sysfs_entry md_array_state =
2633__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002634
NeilBrown6d7ff7382006-01-06 00:21:16 -08002635static ssize_t
2636null_show(mddev_t *mddev, char *page)
2637{
2638 return -EINVAL;
2639}
2640
2641static ssize_t
2642new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2643{
2644 /* buf must be %d:%d\n? giving major and minor numbers */
2645 /* The new device is added to the array.
2646 * If the array has a persistent superblock, we read the
2647 * superblock to initialise info and check validity.
2648 * Otherwise, only checking done is that in bind_rdev_to_array,
2649 * which mainly checks size.
2650 */
2651 char *e;
2652 int major = simple_strtoul(buf, &e, 10);
2653 int minor;
2654 dev_t dev;
2655 mdk_rdev_t *rdev;
2656 int err;
2657
2658 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2659 return -EINVAL;
2660 minor = simple_strtoul(e+1, &e, 10);
2661 if (*e && *e != '\n')
2662 return -EINVAL;
2663 dev = MKDEV(major, minor);
2664 if (major != MAJOR(dev) ||
2665 minor != MINOR(dev))
2666 return -EOVERFLOW;
2667
2668
2669 if (mddev->persistent) {
2670 rdev = md_import_device(dev, mddev->major_version,
2671 mddev->minor_version);
2672 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2673 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2674 mdk_rdev_t, same_set);
2675 err = super_types[mddev->major_version]
2676 .load_super(rdev, rdev0, mddev->minor_version);
2677 if (err < 0)
2678 goto out;
2679 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002680 } else if (mddev->external)
2681 rdev = md_import_device(dev, -2, -1);
2682 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08002683 rdev = md_import_device(dev, -1, -1);
2684
2685 if (IS_ERR(rdev))
2686 return PTR_ERR(rdev);
2687 err = bind_rdev_to_array(rdev, mddev);
2688 out:
2689 if (err)
2690 export_rdev(rdev);
2691 return err ? err : len;
2692}
2693
2694static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002695__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002696
2697static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002698bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2699{
2700 char *end;
2701 unsigned long chunk, end_chunk;
2702
2703 if (!mddev->bitmap)
2704 goto out;
2705 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2706 while (*buf) {
2707 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2708 if (buf == end) break;
2709 if (*end == '-') { /* range */
2710 buf = end + 1;
2711 end_chunk = simple_strtoul(buf, &end, 0);
2712 if (buf == end) break;
2713 }
2714 if (*end && !isspace(*end)) break;
2715 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2716 buf = end;
2717 while (isspace(*buf)) buf++;
2718 }
2719 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2720out:
2721 return len;
2722}
2723
2724static struct md_sysfs_entry md_bitmap =
2725__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2726
2727static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002728size_show(mddev_t *mddev, char *page)
2729{
2730 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2731}
2732
2733static int update_size(mddev_t *mddev, unsigned long size);
2734
2735static ssize_t
2736size_store(mddev_t *mddev, const char *buf, size_t len)
2737{
2738 /* If array is inactive, we can reduce the component size, but
2739 * not increase it (except from 0).
2740 * If array is active, we can try an on-line resize
2741 */
2742 char *e;
2743 int err = 0;
2744 unsigned long long size = simple_strtoull(buf, &e, 10);
2745 if (!*buf || *buf == '\n' ||
2746 (*e && *e != '\n'))
2747 return -EINVAL;
2748
2749 if (mddev->pers) {
2750 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002751 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002752 } else {
2753 if (mddev->size == 0 ||
2754 mddev->size > size)
2755 mddev->size = size;
2756 else
2757 err = -ENOSPC;
2758 }
2759 return err ? err : len;
2760}
2761
2762static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002763__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002764
NeilBrown8bb93aa2006-01-06 00:20:50 -08002765
2766/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002767 * This is one of
2768 * 'none' for arrays with no metadata (good luck...)
2769 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002770 * or N.M for internally known formats
2771 */
2772static ssize_t
2773metadata_show(mddev_t *mddev, char *page)
2774{
2775 if (mddev->persistent)
2776 return sprintf(page, "%d.%d\n",
2777 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002778 else if (mddev->external)
2779 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002780 else
2781 return sprintf(page, "none\n");
2782}
2783
2784static ssize_t
2785metadata_store(mddev_t *mddev, const char *buf, size_t len)
2786{
2787 int major, minor;
2788 char *e;
2789 if (!list_empty(&mddev->disks))
2790 return -EBUSY;
2791
2792 if (cmd_match(buf, "none")) {
2793 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002794 mddev->external = 0;
2795 mddev->major_version = 0;
2796 mddev->minor_version = 90;
2797 return len;
2798 }
2799 if (strncmp(buf, "external:", 9) == 0) {
2800 int namelen = len-9;
2801 if (namelen >= sizeof(mddev->metadata_type))
2802 namelen = sizeof(mddev->metadata_type)-1;
2803 strncpy(mddev->metadata_type, buf+9, namelen);
2804 mddev->metadata_type[namelen] = 0;
2805 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2806 mddev->metadata_type[--namelen] = 0;
2807 mddev->persistent = 0;
2808 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002809 mddev->major_version = 0;
2810 mddev->minor_version = 90;
2811 return len;
2812 }
2813 major = simple_strtoul(buf, &e, 10);
2814 if (e==buf || *e != '.')
2815 return -EINVAL;
2816 buf = e+1;
2817 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002818 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002819 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002820 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002821 return -ENOENT;
2822 mddev->major_version = major;
2823 mddev->minor_version = minor;
2824 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002825 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002826 return len;
2827}
2828
2829static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002830__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002831
NeilBrowna35b0d62006-01-06 00:20:49 -08002832static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002833action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002834{
NeilBrown7eec3142005-11-08 21:39:44 -08002835 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002836 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002837 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002838 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2839 type = "reshape";
2840 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002841 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2842 type = "resync";
2843 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2844 type = "check";
2845 else
2846 type = "repair";
2847 } else
2848 type = "recover";
2849 }
2850 return sprintf(page, "%s\n", type);
2851}
2852
2853static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002854action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002855{
NeilBrown7eec3142005-11-08 21:39:44 -08002856 if (!mddev->pers || !mddev->pers->sync_request)
2857 return -EINVAL;
2858
NeilBrownbce74da2006-01-06 00:20:41 -08002859 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002860 if (mddev->sync_thread) {
2861 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2862 md_unregister_thread(mddev->sync_thread);
2863 mddev->sync_thread = NULL;
2864 mddev->recovery = 0;
2865 }
NeilBrown03c902e2006-01-06 00:20:46 -08002866 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2867 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002868 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002869 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002870 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002871 else if (cmd_match(page, "reshape")) {
2872 int err;
2873 if (mddev->pers->start_reshape == NULL)
2874 return -EINVAL;
2875 err = mddev->pers->start_reshape(mddev);
2876 if (err)
2877 return err;
2878 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002879 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002880 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002881 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002882 return -EINVAL;
2883 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2884 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002885 }
NeilBrown03c902e2006-01-06 00:20:46 -08002886 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002887 md_wakeup_thread(mddev->thread);
2888 return len;
2889}
2890
NeilBrown9d888832005-11-08 21:39:26 -08002891static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002892mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002893{
2894 return sprintf(page, "%llu\n",
2895 (unsigned long long) mddev->resync_mismatches);
2896}
2897
NeilBrown80ca3a42006-07-10 04:44:18 -07002898static struct md_sysfs_entry md_scan_mode =
2899__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002900
NeilBrown96de1e62005-11-08 21:39:39 -08002901
NeilBrown80ca3a42006-07-10 04:44:18 -07002902static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002903
NeilBrown88202a02006-01-06 00:21:36 -08002904static ssize_t
2905sync_min_show(mddev_t *mddev, char *page)
2906{
2907 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2908 mddev->sync_speed_min ? "local": "system");
2909}
2910
2911static ssize_t
2912sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2913{
2914 int min;
2915 char *e;
2916 if (strncmp(buf, "system", 6)==0) {
2917 mddev->sync_speed_min = 0;
2918 return len;
2919 }
2920 min = simple_strtoul(buf, &e, 10);
2921 if (buf == e || (*e && *e != '\n') || min <= 0)
2922 return -EINVAL;
2923 mddev->sync_speed_min = min;
2924 return len;
2925}
2926
2927static struct md_sysfs_entry md_sync_min =
2928__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2929
2930static ssize_t
2931sync_max_show(mddev_t *mddev, char *page)
2932{
2933 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2934 mddev->sync_speed_max ? "local": "system");
2935}
2936
2937static ssize_t
2938sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2939{
2940 int max;
2941 char *e;
2942 if (strncmp(buf, "system", 6)==0) {
2943 mddev->sync_speed_max = 0;
2944 return len;
2945 }
2946 max = simple_strtoul(buf, &e, 10);
2947 if (buf == e || (*e && *e != '\n') || max <= 0)
2948 return -EINVAL;
2949 mddev->sync_speed_max = max;
2950 return len;
2951}
2952
2953static struct md_sysfs_entry md_sync_max =
2954__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2955
Iustin Popd7f3d292007-10-16 23:30:54 -07002956static ssize_t
2957degraded_show(mddev_t *mddev, char *page)
2958{
2959 return sprintf(page, "%d\n", mddev->degraded);
2960}
2961static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08002962
2963static ssize_t
2964sync_speed_show(mddev_t *mddev, char *page)
2965{
2966 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002967 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002968 dt = ((jiffies - mddev->resync_mark) / HZ);
2969 if (!dt) dt++;
2970 db = resync - (mddev->resync_mark_cnt);
2971 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2972}
2973
NeilBrown80ca3a42006-07-10 04:44:18 -07002974static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002975
2976static ssize_t
2977sync_completed_show(mddev_t *mddev, char *page)
2978{
2979 unsigned long max_blocks, resync;
2980
2981 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2982 max_blocks = mddev->resync_max_sectors;
2983 else
2984 max_blocks = mddev->size << 1;
2985
2986 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2987 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2988}
2989
NeilBrown80ca3a42006-07-10 04:44:18 -07002990static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08002991
NeilBrowne464eaf2006-03-27 01:18:14 -08002992static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08002993max_sync_show(mddev_t *mddev, char *page)
2994{
2995 if (mddev->resync_max == MaxSector)
2996 return sprintf(page, "max\n");
2997 else
2998 return sprintf(page, "%llu\n",
2999 (unsigned long long)mddev->resync_max);
3000}
3001static ssize_t
3002max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3003{
3004 if (strncmp(buf, "max", 3) == 0)
3005 mddev->resync_max = MaxSector;
3006 else {
3007 char *ep;
3008 unsigned long long max = simple_strtoull(buf, &ep, 10);
3009 if (ep == buf || (*ep != 0 && *ep != '\n'))
3010 return -EINVAL;
3011 if (max < mddev->resync_max &&
3012 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3013 return -EBUSY;
3014
3015 /* Must be a multiple of chunk_size */
3016 if (mddev->chunk_size) {
3017 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3018 return -EINVAL;
3019 }
3020 mddev->resync_max = max;
3021 }
3022 wake_up(&mddev->recovery_wait);
3023 return len;
3024}
3025
3026static struct md_sysfs_entry md_max_sync =
3027__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3028
3029static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003030suspend_lo_show(mddev_t *mddev, char *page)
3031{
3032 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3033}
3034
3035static ssize_t
3036suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3037{
3038 char *e;
3039 unsigned long long new = simple_strtoull(buf, &e, 10);
3040
3041 if (mddev->pers->quiesce == NULL)
3042 return -EINVAL;
3043 if (buf == e || (*e && *e != '\n'))
3044 return -EINVAL;
3045 if (new >= mddev->suspend_hi ||
3046 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3047 mddev->suspend_lo = new;
3048 mddev->pers->quiesce(mddev, 2);
3049 return len;
3050 } else
3051 return -EINVAL;
3052}
3053static struct md_sysfs_entry md_suspend_lo =
3054__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3055
3056
3057static ssize_t
3058suspend_hi_show(mddev_t *mddev, char *page)
3059{
3060 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3061}
3062
3063static ssize_t
3064suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3065{
3066 char *e;
3067 unsigned long long new = simple_strtoull(buf, &e, 10);
3068
3069 if (mddev->pers->quiesce == NULL)
3070 return -EINVAL;
3071 if (buf == e || (*e && *e != '\n'))
3072 return -EINVAL;
3073 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3074 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3075 mddev->suspend_hi = new;
3076 mddev->pers->quiesce(mddev, 1);
3077 mddev->pers->quiesce(mddev, 0);
3078 return len;
3079 } else
3080 return -EINVAL;
3081}
3082static struct md_sysfs_entry md_suspend_hi =
3083__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3084
NeilBrown08a02ec2007-05-09 02:35:38 -07003085static ssize_t
3086reshape_position_show(mddev_t *mddev, char *page)
3087{
3088 if (mddev->reshape_position != MaxSector)
3089 return sprintf(page, "%llu\n",
3090 (unsigned long long)mddev->reshape_position);
3091 strcpy(page, "none\n");
3092 return 5;
3093}
3094
3095static ssize_t
3096reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3097{
3098 char *e;
3099 unsigned long long new = simple_strtoull(buf, &e, 10);
3100 if (mddev->pers)
3101 return -EBUSY;
3102 if (buf == e || (*e && *e != '\n'))
3103 return -EINVAL;
3104 mddev->reshape_position = new;
3105 mddev->delta_disks = 0;
3106 mddev->new_level = mddev->level;
3107 mddev->new_layout = mddev->layout;
3108 mddev->new_chunk = mddev->chunk_size;
3109 return len;
3110}
3111
3112static struct md_sysfs_entry md_reshape_position =
3113__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3114 reshape_position_store);
3115
NeilBrowne464eaf2006-03-27 01:18:14 -08003116
NeilBrowneae17012005-11-08 21:39:23 -08003117static struct attribute *md_default_attrs[] = {
3118 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003119 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003120 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003121 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003122 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003123 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003124 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003125 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003126 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003127 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003128 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003129 NULL,
3130};
3131
3132static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003133 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003134 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003135 &md_sync_min.attr,
3136 &md_sync_max.attr,
3137 &md_sync_speed.attr,
3138 &md_sync_completed.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003139 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003140 &md_suspend_lo.attr,
3141 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003142 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003143 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003144 NULL,
3145};
NeilBrown411036f2005-11-08 21:39:40 -08003146static struct attribute_group md_redundancy_group = {
3147 .name = NULL,
3148 .attrs = md_redundancy_attrs,
3149};
3150
NeilBrowneae17012005-11-08 21:39:23 -08003151
3152static ssize_t
3153md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3154{
3155 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3156 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003157 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003158
3159 if (!entry->show)
3160 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003161 rv = mddev_lock(mddev);
3162 if (!rv) {
3163 rv = entry->show(mddev, page);
3164 mddev_unlock(mddev);
3165 }
NeilBrown96de1e62005-11-08 21:39:39 -08003166 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003167}
3168
3169static ssize_t
3170md_attr_store(struct kobject *kobj, struct attribute *attr,
3171 const char *page, size_t length)
3172{
3173 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3174 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003175 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003176
3177 if (!entry->store)
3178 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003179 if (!capable(CAP_SYS_ADMIN))
3180 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003181 rv = mddev_lock(mddev);
3182 if (!rv) {
3183 rv = entry->store(mddev, page, length);
3184 mddev_unlock(mddev);
3185 }
NeilBrown96de1e62005-11-08 21:39:39 -08003186 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003187}
3188
3189static void md_free(struct kobject *ko)
3190{
3191 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3192 kfree(mddev);
3193}
3194
3195static struct sysfs_ops md_sysfs_ops = {
3196 .show = md_attr_show,
3197 .store = md_attr_store,
3198};
3199static struct kobj_type md_ktype = {
3200 .release = md_free,
3201 .sysfs_ops = &md_sysfs_ops,
3202 .default_attrs = md_default_attrs,
3203};
3204
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205int mdp_major = 0;
3206
3207static struct kobject *md_probe(dev_t dev, int *part, void *data)
3208{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003209 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 mddev_t *mddev = mddev_find(dev);
3211 struct gendisk *disk;
3212 int partitioned = (MAJOR(dev) != MD_MAJOR);
3213 int shift = partitioned ? MdpMinorShift : 0;
3214 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003215 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 if (!mddev)
3218 return NULL;
3219
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003220 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003222 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 mddev_put(mddev);
3224 return NULL;
3225 }
3226 disk = alloc_disk(1 << shift);
3227 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003228 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 mddev_put(mddev);
3230 return NULL;
3231 }
3232 disk->major = MAJOR(dev);
3233 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003234 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003236 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 disk->fops = &md_fops;
3239 disk->private_data = mddev;
3240 disk->queue = mddev->queue;
3241 add_disk(disk);
3242 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003243 mutex_unlock(&disks_mutex);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003244 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003245 "%s", "md");
3246 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003247 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3248 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003249 else
3250 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 return NULL;
3252}
3253
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254static void md_safemode_timeout(unsigned long data)
3255{
3256 mddev_t *mddev = (mddev_t *) data;
3257
3258 mddev->safemode = 1;
3259 md_wakeup_thread(mddev->thread);
3260}
3261
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003262static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264static int do_md_run(mddev_t * mddev)
3265{
NeilBrown2604b702006-01-06 00:20:36 -08003266 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 int chunk_size;
3268 struct list_head *tmp;
3269 mdk_rdev_t *rdev;
3270 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003271 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 char b[BDEVNAME_SIZE];
3273
NeilBrowna757e642005-04-16 15:26:42 -07003274 if (list_empty(&mddev->disks))
3275 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
3278 if (mddev->pers)
3279 return -EBUSY;
3280
3281 /*
3282 * Analyze all RAID superblock(s)
3283 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003284 if (!mddev->raid_disks) {
3285 if (!mddev->persistent)
3286 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003287 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
NeilBrown2604b702006-01-06 00:20:36 -08003292 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (chunk_size > MAX_CHUNK_SIZE) {
3294 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3295 chunk_size, MAX_CHUNK_SIZE);
3296 return -EINVAL;
3297 }
3298 /*
3299 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3300 */
3301 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003302 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return -EINVAL;
3304 }
3305 if (chunk_size < PAGE_SIZE) {
3306 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3307 chunk_size, PAGE_SIZE);
3308 return -EINVAL;
3309 }
3310
3311 /* devices must have minimum size of one chunk */
3312 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003313 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 continue;
3315 if (rdev->size < chunk_size / 1024) {
3316 printk(KERN_WARNING
3317 "md: Dev %s smaller than chunk_size:"
3318 " %lluk < %dk\n",
3319 bdevname(rdev->bdev,b),
3320 (unsigned long long)rdev->size,
3321 chunk_size / 1024);
3322 return -EINVAL;
3323 }
3324 }
3325 }
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003328 if (mddev->level != LEVEL_NONE)
3329 request_module("md-level-%d", mddev->level);
3330 else if (mddev->clevel[0])
3331 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332#endif
3333
3334 /*
3335 * Drop all container device buffers, from now on
3336 * the only valid external interface is through the md
3337 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 */
3339 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003340 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 continue;
3342 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003343 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003344
3345 /* perform some consistency tests on the device.
3346 * We don't want the data to overlap the metadata,
3347 * Internal Bitmap issues has handled elsewhere.
3348 */
3349 if (rdev->data_offset < rdev->sb_offset) {
3350 if (mddev->size &&
3351 rdev->data_offset + mddev->size*2
3352 > rdev->sb_offset*2) {
3353 printk("md: %s: data overlaps metadata\n",
3354 mdname(mddev));
3355 return -EINVAL;
3356 }
3357 } else {
3358 if (rdev->sb_offset*2 + rdev->sb_size/512
3359 > rdev->data_offset) {
3360 printk("md: %s: metadata overlaps data\n",
3361 mdname(mddev));
3362 return -EINVAL;
3363 }
3364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 }
3366
3367 md_probe(mddev->unit, NULL, NULL);
3368 disk = mddev->gendisk;
3369 if (!disk)
3370 return -ENOMEM;
3371
3372 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003373 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003374 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003376 if (mddev->level != LEVEL_NONE)
3377 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3378 mddev->level);
3379 else
3380 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3381 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return -EINVAL;
3383 }
NeilBrown2604b702006-01-06 00:20:36 -08003384 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003386 mddev->level = pers->level;
3387 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
NeilBrownf6705572006-03-27 01:18:11 -08003389 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003390 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003391 /* This personality cannot handle reshaping... */
3392 mddev->pers = NULL;
3393 module_put(pers->owner);
3394 return -EINVAL;
3395 }
3396
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003397 if (pers->sync_request) {
3398 /* Warn if this is a potentially silly
3399 * configuration.
3400 */
3401 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3402 mdk_rdev_t *rdev2;
3403 struct list_head *tmp2;
3404 int warned = 0;
3405 ITERATE_RDEV(mddev, rdev, tmp) {
3406 ITERATE_RDEV(mddev, rdev2, tmp2) {
3407 if (rdev < rdev2 &&
3408 rdev->bdev->bd_contains ==
3409 rdev2->bdev->bd_contains) {
3410 printk(KERN_WARNING
3411 "%s: WARNING: %s appears to be"
3412 " on the same physical disk as"
3413 " %s.\n",
3414 mdname(mddev),
3415 bdevname(rdev->bdev,b),
3416 bdevname(rdev2->bdev,b2));
3417 warned = 1;
3418 }
3419 }
3420 }
3421 if (warned)
3422 printk(KERN_WARNING
3423 "True protection against single-disk"
3424 " failure might be compromised.\n");
3425 }
3426
NeilBrown657390d2005-08-26 18:34:16 -07003427 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003429 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003430 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
NeilBrownf91de922005-11-08 21:39:36 -08003432 if (start_readonly)
3433 mddev->ro = 2; /* read-only, but switch on first write */
3434
NeilBrownb15c2e52006-01-06 00:20:16 -08003435 err = mddev->pers->run(mddev);
3436 if (!err && mddev->pers->sync_request) {
3437 err = bitmap_create(mddev);
3438 if (err) {
3439 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3440 mdname(mddev), err);
3441 mddev->pers->stop(mddev);
3442 }
3443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 if (err) {
3445 printk(KERN_ERR "md: pers->run() failed ...\n");
3446 module_put(mddev->pers->owner);
3447 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003448 bitmap_destroy(mddev);
3449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003451 if (mddev->pers->sync_request) {
3452 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3453 printk(KERN_WARNING
3454 "md: cannot register extra attributes for %s\n",
3455 mdname(mddev));
3456 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003457 mddev->ro = 0;
3458
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 atomic_set(&mddev->writes_pending,0);
3460 mddev->safemode = 0;
3461 mddev->safemode_timer.function = md_safemode_timeout;
3462 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003463 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003465
3466 ITERATE_RDEV(mddev,rdev,tmp)
3467 if (rdev->raid_disk >= 0) {
3468 char nm[20];
3469 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003470 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3471 printk("md: cannot register %s for %s\n",
3472 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474
3475 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3476
NeilBrown850b2b42006-10-03 01:15:46 -07003477 if (mddev->flags)
3478 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
3480 set_capacity(disk, mddev->array_size<<1);
3481
3482 /* If we call blk_queue_make_request here, it will
3483 * re-initialise max_sectors etc which may have been
3484 * refined inside -> run. So just set the bits we need to set.
3485 * Most initialisation happended when we called
3486 * blk_queue_make_request(..., md_fail_request)
3487 * earlier.
3488 */
3489 mddev->queue->queuedata = mddev;
3490 mddev->queue->make_request_fn = mddev->pers->make_request;
3491
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003492 /* If there is a partially-recovered drive we need to
3493 * start recovery here. If we leave it to md_check_recovery,
3494 * it will remove the drives and not do the right thing
3495 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003496 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003497 struct list_head *rtmp;
3498 int spares = 0;
3499 ITERATE_RDEV(mddev,rdev,rtmp)
3500 if (rdev->raid_disk >= 0 &&
3501 !test_bit(In_sync, &rdev->flags) &&
3502 !test_bit(Faulty, &rdev->flags))
3503 /* complete an interrupted recovery */
3504 spares++;
3505 if (spares && mddev->pers->sync_request) {
3506 mddev->recovery = 0;
3507 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3508 mddev->sync_thread = md_register_thread(md_do_sync,
3509 mddev,
3510 "%s_resync");
3511 if (!mddev->sync_thread) {
3512 printk(KERN_ERR "%s: could not start resync"
3513 " thread...\n",
3514 mdname(mddev));
3515 /* leave the spares where they are, it shouldn't hurt */
3516 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003517 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003518 }
3519 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003520 md_wakeup_thread(mddev->thread);
3521 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003522
Linus Torvalds44ce62942007-05-09 18:51:36 -07003523 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003524 md_new_event(mddev);
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003525 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 return 0;
3527}
3528
3529static int restart_array(mddev_t *mddev)
3530{
3531 struct gendisk *disk = mddev->gendisk;
3532 int err;
3533
3534 /*
3535 * Complain if it has no devices
3536 */
3537 err = -ENXIO;
3538 if (list_empty(&mddev->disks))
3539 goto out;
3540
3541 if (mddev->pers) {
3542 err = -EBUSY;
3543 if (!mddev->ro)
3544 goto out;
3545
3546 mddev->safemode = 0;
3547 mddev->ro = 0;
3548 set_disk_ro(disk, 0);
3549
3550 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3551 mdname(mddev));
3552 /*
3553 * Kick recovery or resync if necessary
3554 */
3555 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3556 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003557 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003559 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
3562out:
3563 return err;
3564}
3565
NeilBrownacc55e22006-06-26 00:27:47 -07003566/* similar to deny_write_access, but accounts for our holding a reference
3567 * to the file ourselves */
3568static int deny_bitmap_write_access(struct file * file)
3569{
3570 struct inode *inode = file->f_mapping->host;
3571
3572 spin_lock(&inode->i_lock);
3573 if (atomic_read(&inode->i_writecount) > 1) {
3574 spin_unlock(&inode->i_lock);
3575 return -ETXTBSY;
3576 }
3577 atomic_set(&inode->i_writecount, -1);
3578 spin_unlock(&inode->i_lock);
3579
3580 return 0;
3581}
3582
3583static void restore_bitmap_write_access(struct file *file)
3584{
3585 struct inode *inode = file->f_mapping->host;
3586
3587 spin_lock(&inode->i_lock);
3588 atomic_set(&inode->i_writecount, 1);
3589 spin_unlock(&inode->i_lock);
3590}
3591
NeilBrown9e653b62006-06-26 00:27:58 -07003592/* mode:
3593 * 0 - completely stop and dis-assemble array
3594 * 1 - switch to readonly
3595 * 2 - stop but do not disassemble array
3596 */
3597static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
3599 int err = 0;
3600 struct gendisk *disk = mddev->gendisk;
3601
3602 if (mddev->pers) {
3603 if (atomic_read(&mddev->active)>2) {
3604 printk("md: %s still in use.\n",mdname(mddev));
3605 return -EBUSY;
3606 }
3607
3608 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003609 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3611 md_unregister_thread(mddev->sync_thread);
3612 mddev->sync_thread = NULL;
3613 }
3614
3615 del_timer_sync(&mddev->safemode_timer);
3616
3617 invalidate_partition(disk, 0);
3618
NeilBrown9e653b62006-06-26 00:27:58 -07003619 switch(mode) {
3620 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003622 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 goto out;
3624 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003625 break;
3626 case 0: /* disassemble */
3627 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003628 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003629 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 if (mddev->ro)
3631 set_disk_ro(disk, 0);
3632 blk_queue_make_request(mddev->queue, md_fail_request);
3633 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003634 mddev->queue->merge_bvec_fn = NULL;
3635 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003636 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003637 if (mddev->pers->sync_request)
3638 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 module_put(mddev->pers->owner);
3641 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003642
3643 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003644 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003645
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 if (mddev->ro)
3647 mddev->ro = 0;
3648 }
NeilBrown850b2b42006-10-03 01:15:46 -07003649 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 /* mark array as shutdown cleanly */
3651 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003652 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
NeilBrown9e653b62006-06-26 00:27:58 -07003654 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003656 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 }
NeilBrown32a76272005-06-21 17:17:14 -07003658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 /*
3660 * Free resources if final stop
3661 */
NeilBrown9e653b62006-06-26 00:27:58 -07003662 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003663 mdk_rdev_t *rdev;
3664 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3667
NeilBrown978f9462006-02-02 14:28:05 -08003668 bitmap_destroy(mddev);
3669 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003670 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003671 fput(mddev->bitmap_file);
3672 mddev->bitmap_file = NULL;
3673 }
3674 mddev->bitmap_offset = 0;
3675
NeilBrown86e6ffd2005-11-08 21:39:24 -08003676 ITERATE_RDEV(mddev,rdev,tmp)
3677 if (rdev->raid_disk >= 0) {
3678 char nm[20];
3679 sprintf(nm, "rd%d", rdev->raid_disk);
3680 sysfs_remove_link(&mddev->kobj, nm);
3681 }
3682
NeilBrown5792a282007-04-04 19:08:18 -07003683 /* make sure all delayed_delete calls have finished */
3684 flush_scheduled_work();
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 export_array(mddev);
3687
3688 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003689 mddev->size = 0;
3690 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003691 mddev->recovery_cp = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003692 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003693 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003694 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003695 mddev->persistent = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003696
NeilBrowna8a55c32006-06-26 00:27:39 -07003697 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3699 mdname(mddev));
3700 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003701 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702out:
3703 return err;
3704}
3705
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003706#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707static void autorun_array(mddev_t *mddev)
3708{
3709 mdk_rdev_t *rdev;
3710 struct list_head *tmp;
3711 int err;
3712
NeilBrowna757e642005-04-16 15:26:42 -07003713 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715
3716 printk(KERN_INFO "md: running: ");
3717
3718 ITERATE_RDEV(mddev,rdev,tmp) {
3719 char b[BDEVNAME_SIZE];
3720 printk("<%s>", bdevname(rdev->bdev,b));
3721 }
3722 printk("\n");
3723
3724 err = do_md_run (mddev);
3725 if (err) {
3726 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3727 do_md_stop (mddev, 0);
3728 }
3729}
3730
3731/*
3732 * lets try to run arrays based on all disks that have arrived
3733 * until now. (those are in pending_raid_disks)
3734 *
3735 * the method: pick the first pending disk, collect all disks with
3736 * the same UUID, remove all from the pending list and put them into
3737 * the 'same_array' list. Then order this list based on superblock
3738 * update time (freshest comes first), kick out 'old' disks and
3739 * compare superblocks. If everything's fine then run it.
3740 *
3741 * If "unit" is allocated, then bump its reference count
3742 */
3743static void autorun_devices(int part)
3744{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 struct list_head *tmp;
3746 mdk_rdev_t *rdev0, *rdev;
3747 mddev_t *mddev;
3748 char b[BDEVNAME_SIZE];
3749
3750 printk(KERN_INFO "md: autorun ...\n");
3751 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003752 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003754 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 rdev0 = list_entry(pending_raid_disks.next,
3756 mdk_rdev_t, same_set);
3757
3758 printk(KERN_INFO "md: considering %s ...\n",
3759 bdevname(rdev0->bdev,b));
3760 INIT_LIST_HEAD(&candidates);
3761 ITERATE_RDEV_PENDING(rdev,tmp)
3762 if (super_90_load(rdev, rdev0, 0) >= 0) {
3763 printk(KERN_INFO "md: adding %s ...\n",
3764 bdevname(rdev->bdev,b));
3765 list_move(&rdev->same_set, &candidates);
3766 }
3767 /*
3768 * now we have a set of devices, with all of them having
3769 * mostly sane superblocks. It's time to allocate the
3770 * mddev.
3771 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003772 if (part) {
3773 dev = MKDEV(mdp_major,
3774 rdev0->preferred_minor << MdpMinorShift);
3775 unit = MINOR(dev) >> MdpMinorShift;
3776 } else {
3777 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3778 unit = MINOR(dev);
3779 }
3780 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3782 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3783 break;
3784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785
3786 md_probe(dev, NULL, NULL);
3787 mddev = mddev_find(dev);
3788 if (!mddev) {
3789 printk(KERN_ERR
3790 "md: cannot allocate memory for md drive.\n");
3791 break;
3792 }
3793 if (mddev_lock(mddev))
3794 printk(KERN_WARNING "md: %s locked, cannot run\n",
3795 mdname(mddev));
3796 else if (mddev->raid_disks || mddev->major_version
3797 || !list_empty(&mddev->disks)) {
3798 printk(KERN_WARNING
3799 "md: %s already running, cannot run %s\n",
3800 mdname(mddev), bdevname(rdev0->bdev,b));
3801 mddev_unlock(mddev);
3802 } else {
3803 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08003804 mddev->persistent = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3806 list_del_init(&rdev->same_set);
3807 if (bind_rdev_to_array(rdev, mddev))
3808 export_rdev(rdev);
3809 }
3810 autorun_array(mddev);
3811 mddev_unlock(mddev);
3812 }
3813 /* on success, candidates will be empty, on error
3814 * it won't...
3815 */
3816 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3817 export_rdev(rdev);
3818 mddev_put(mddev);
3819 }
3820 printk(KERN_INFO "md: ... autorun DONE.\n");
3821}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003822#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824static int get_version(void __user * arg)
3825{
3826 mdu_version_t ver;
3827
3828 ver.major = MD_MAJOR_VERSION;
3829 ver.minor = MD_MINOR_VERSION;
3830 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3831
3832 if (copy_to_user(arg, &ver, sizeof(ver)))
3833 return -EFAULT;
3834
3835 return 0;
3836}
3837
3838static int get_array_info(mddev_t * mddev, void __user * arg)
3839{
3840 mdu_array_info_t info;
3841 int nr,working,active,failed,spare;
3842 mdk_rdev_t *rdev;
3843 struct list_head *tmp;
3844
3845 nr=working=active=failed=spare=0;
3846 ITERATE_RDEV(mddev,rdev,tmp) {
3847 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003848 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 failed++;
3850 else {
3851 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003852 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 active++;
3854 else
3855 spare++;
3856 }
3857 }
3858
3859 info.major_version = mddev->major_version;
3860 info.minor_version = mddev->minor_version;
3861 info.patch_version = MD_PATCHLEVEL_VERSION;
3862 info.ctime = mddev->ctime;
3863 info.level = mddev->level;
3864 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003865 if (info.size != mddev->size) /* overflow */
3866 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 info.nr_disks = nr;
3868 info.raid_disks = mddev->raid_disks;
3869 info.md_minor = mddev->md_minor;
3870 info.not_persistent= !mddev->persistent;
3871
3872 info.utime = mddev->utime;
3873 info.state = 0;
3874 if (mddev->in_sync)
3875 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003876 if (mddev->bitmap && mddev->bitmap_offset)
3877 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 info.active_disks = active;
3879 info.working_disks = working;
3880 info.failed_disks = failed;
3881 info.spare_disks = spare;
3882
3883 info.layout = mddev->layout;
3884 info.chunk_size = mddev->chunk_size;
3885
3886 if (copy_to_user(arg, &info, sizeof(info)))
3887 return -EFAULT;
3888
3889 return 0;
3890}
3891
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003892static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003893{
3894 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3895 char *ptr, *buf = NULL;
3896 int err = -ENOMEM;
3897
NeilBrown2a2275d2007-01-26 00:57:11 -08003898 md_allow_write(mddev);
3899
NeilBrown32a76272005-06-21 17:17:14 -07003900 file = kmalloc(sizeof(*file), GFP_KERNEL);
3901 if (!file)
3902 goto out;
3903
3904 /* bitmap disabled, zero the first byte and copy out */
3905 if (!mddev->bitmap || !mddev->bitmap->file) {
3906 file->pathname[0] = '\0';
3907 goto copy_out;
3908 }
3909
3910 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3911 if (!buf)
3912 goto out;
3913
3914 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3915 if (!ptr)
3916 goto out;
3917
3918 strcpy(file->pathname, ptr);
3919
3920copy_out:
3921 err = 0;
3922 if (copy_to_user(arg, file, sizeof(*file)))
3923 err = -EFAULT;
3924out:
3925 kfree(buf);
3926 kfree(file);
3927 return err;
3928}
3929
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930static int get_disk_info(mddev_t * mddev, void __user * arg)
3931{
3932 mdu_disk_info_t info;
3933 unsigned int nr;
3934 mdk_rdev_t *rdev;
3935
3936 if (copy_from_user(&info, arg, sizeof(info)))
3937 return -EFAULT;
3938
3939 nr = info.number;
3940
3941 rdev = find_rdev_nr(mddev, nr);
3942 if (rdev) {
3943 info.major = MAJOR(rdev->bdev->bd_dev);
3944 info.minor = MINOR(rdev->bdev->bd_dev);
3945 info.raid_disk = rdev->raid_disk;
3946 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003947 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003949 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 info.state |= (1<<MD_DISK_ACTIVE);
3951 info.state |= (1<<MD_DISK_SYNC);
3952 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003953 if (test_bit(WriteMostly, &rdev->flags))
3954 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 } else {
3956 info.major = info.minor = 0;
3957 info.raid_disk = -1;
3958 info.state = (1<<MD_DISK_REMOVED);
3959 }
3960
3961 if (copy_to_user(arg, &info, sizeof(info)))
3962 return -EFAULT;
3963
3964 return 0;
3965}
3966
3967static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3968{
3969 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3970 mdk_rdev_t *rdev;
3971 dev_t dev = MKDEV(info->major,info->minor);
3972
3973 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3974 return -EOVERFLOW;
3975
3976 if (!mddev->raid_disks) {
3977 int err;
3978 /* expecting a device which has a superblock */
3979 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3980 if (IS_ERR(rdev)) {
3981 printk(KERN_WARNING
3982 "md: md_import_device returned %ld\n",
3983 PTR_ERR(rdev));
3984 return PTR_ERR(rdev);
3985 }
3986 if (!list_empty(&mddev->disks)) {
3987 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3988 mdk_rdev_t, same_set);
3989 int err = super_types[mddev->major_version]
3990 .load_super(rdev, rdev0, mddev->minor_version);
3991 if (err < 0) {
3992 printk(KERN_WARNING
3993 "md: %s has different UUID to %s\n",
3994 bdevname(rdev->bdev,b),
3995 bdevname(rdev0->bdev,b2));
3996 export_rdev(rdev);
3997 return -EINVAL;
3998 }
3999 }
4000 err = bind_rdev_to_array(rdev, mddev);
4001 if (err)
4002 export_rdev(rdev);
4003 return err;
4004 }
4005
4006 /*
4007 * add_new_disk can be used once the array is assembled
4008 * to add "hot spares". They must already have a superblock
4009 * written
4010 */
4011 if (mddev->pers) {
4012 int err;
4013 if (!mddev->pers->hot_add_disk) {
4014 printk(KERN_WARNING
4015 "%s: personality does not support diskops!\n",
4016 mdname(mddev));
4017 return -EINVAL;
4018 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004019 if (mddev->persistent)
4020 rdev = md_import_device(dev, mddev->major_version,
4021 mddev->minor_version);
4022 else
4023 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 if (IS_ERR(rdev)) {
4025 printk(KERN_WARNING
4026 "md: md_import_device returned %ld\n",
4027 PTR_ERR(rdev));
4028 return PTR_ERR(rdev);
4029 }
NeilBrown41158c72005-06-21 17:17:25 -07004030 /* set save_raid_disk if appropriate */
4031 if (!mddev->persistent) {
4032 if (info->state & (1<<MD_DISK_SYNC) &&
4033 info->raid_disk < mddev->raid_disks)
4034 rdev->raid_disk = info->raid_disk;
4035 else
4036 rdev->raid_disk = -1;
4037 } else
4038 super_types[mddev->major_version].
4039 validate_super(mddev, rdev);
4040 rdev->saved_raid_disk = rdev->raid_disk;
4041
NeilBrownb2d444d2005-11-08 21:39:31 -08004042 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004043 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4044 set_bit(WriteMostly, &rdev->flags);
4045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 rdev->raid_disk = -1;
4047 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004048 if (!err && !mddev->pers->hot_remove_disk) {
4049 /* If there is hot_add_disk but no hot_remove_disk
4050 * then added disks for geometry changes,
4051 * and should be added immediately.
4052 */
4053 super_types[mddev->major_version].
4054 validate_super(mddev, rdev);
4055 err = mddev->pers->hot_add_disk(mddev, rdev);
4056 if (err)
4057 unbind_rdev_from_array(rdev);
4058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 if (err)
4060 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07004061
NeilBrown17571282006-12-10 02:20:52 -08004062 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07004063 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004064 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return err;
4066 }
4067
4068 /* otherwise, add_new_disk is only allowed
4069 * for major_version==0 superblocks
4070 */
4071 if (mddev->major_version != 0) {
4072 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4073 mdname(mddev));
4074 return -EINVAL;
4075 }
4076
4077 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4078 int err;
4079 rdev = md_import_device (dev, -1, 0);
4080 if (IS_ERR(rdev)) {
4081 printk(KERN_WARNING
4082 "md: error, md_import_device() returned %ld\n",
4083 PTR_ERR(rdev));
4084 return PTR_ERR(rdev);
4085 }
4086 rdev->desc_nr = info->number;
4087 if (info->raid_disk < mddev->raid_disks)
4088 rdev->raid_disk = info->raid_disk;
4089 else
4090 rdev->raid_disk = -1;
4091
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004093 if (info->state & (1<<MD_DISK_SYNC))
4094 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004096 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4097 set_bit(WriteMostly, &rdev->flags);
4098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 if (!mddev->persistent) {
4100 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4101 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4102 } else
4103 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4104 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4105
NeilBrown2bf071b2006-01-06 00:20:55 -08004106 err = bind_rdev_to_array(rdev, mddev);
4107 if (err) {
4108 export_rdev(rdev);
4109 return err;
4110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 }
4112
4113 return 0;
4114}
4115
4116static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4117{
4118 char b[BDEVNAME_SIZE];
4119 mdk_rdev_t *rdev;
4120
4121 if (!mddev->pers)
4122 return -ENODEV;
4123
4124 rdev = find_rdev(mddev, dev);
4125 if (!rdev)
4126 return -ENXIO;
4127
4128 if (rdev->raid_disk >= 0)
4129 goto busy;
4130
4131 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004132 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004133 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 return 0;
4136busy:
4137 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
4138 bdevname(rdev->bdev,b), mdname(mddev));
4139 return -EBUSY;
4140}
4141
4142static int hot_add_disk(mddev_t * mddev, dev_t dev)
4143{
4144 char b[BDEVNAME_SIZE];
4145 int err;
4146 unsigned int size;
4147 mdk_rdev_t *rdev;
4148
4149 if (!mddev->pers)
4150 return -ENODEV;
4151
4152 if (mddev->major_version != 0) {
4153 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4154 " version-0 superblocks.\n",
4155 mdname(mddev));
4156 return -EINVAL;
4157 }
4158 if (!mddev->pers->hot_add_disk) {
4159 printk(KERN_WARNING
4160 "%s: personality does not support diskops!\n",
4161 mdname(mddev));
4162 return -EINVAL;
4163 }
4164
4165 rdev = md_import_device (dev, -1, 0);
4166 if (IS_ERR(rdev)) {
4167 printk(KERN_WARNING
4168 "md: error, md_import_device() returned %ld\n",
4169 PTR_ERR(rdev));
4170 return -EINVAL;
4171 }
4172
4173 if (mddev->persistent)
4174 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4175 else
4176 rdev->sb_offset =
4177 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4178
4179 size = calc_dev_size(rdev, mddev->chunk_size);
4180 rdev->size = size;
4181
NeilBrownb2d444d2005-11-08 21:39:31 -08004182 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 printk(KERN_WARNING
4184 "md: can not hot-add faulty %s disk to %s!\n",
4185 bdevname(rdev->bdev,b), mdname(mddev));
4186 err = -EINVAL;
4187 goto abort_export;
4188 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004189 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004191 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004192 err = bind_rdev_to_array(rdev, mddev);
4193 if (err)
4194 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 /*
4197 * The rest should better be atomic, we can have disk failures
4198 * noticed in interrupt contexts ...
4199 */
4200
4201 if (rdev->desc_nr == mddev->max_disks) {
4202 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4203 mdname(mddev));
4204 err = -EBUSY;
4205 goto abort_unbind_export;
4206 }
4207
4208 rdev->raid_disk = -1;
4209
NeilBrown850b2b42006-10-03 01:15:46 -07004210 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211
4212 /*
4213 * Kick recovery, maybe this spare has to be added to the
4214 * array immediately.
4215 */
4216 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4217 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004218 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 return 0;
4220
4221abort_unbind_export:
4222 unbind_rdev_from_array(rdev);
4223
4224abort_export:
4225 export_rdev(rdev);
4226 return err;
4227}
4228
NeilBrown32a76272005-06-21 17:17:14 -07004229static int set_bitmap_file(mddev_t *mddev, int fd)
4230{
4231 int err;
4232
NeilBrown36fa3062005-09-09 16:23:45 -07004233 if (mddev->pers) {
4234 if (!mddev->pers->quiesce)
4235 return -EBUSY;
4236 if (mddev->recovery || mddev->sync_thread)
4237 return -EBUSY;
4238 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004239 }
4240
NeilBrown36fa3062005-09-09 16:23:45 -07004241
4242 if (fd >= 0) {
4243 if (mddev->bitmap)
4244 return -EEXIST; /* cannot add when bitmap is present */
4245 mddev->bitmap_file = fget(fd);
4246
4247 if (mddev->bitmap_file == NULL) {
4248 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4249 mdname(mddev));
4250 return -EBADF;
4251 }
4252
4253 err = deny_bitmap_write_access(mddev->bitmap_file);
4254 if (err) {
4255 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4256 mdname(mddev));
4257 fput(mddev->bitmap_file);
4258 mddev->bitmap_file = NULL;
4259 return err;
4260 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004261 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004262 } else if (mddev->bitmap == NULL)
4263 return -ENOENT; /* cannot remove what isn't there */
4264 err = 0;
4265 if (mddev->pers) {
4266 mddev->pers->quiesce(mddev, 1);
4267 if (fd >= 0)
4268 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004269 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004270 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004271 fd = -1; /* make sure to put the file */
4272 }
NeilBrown36fa3062005-09-09 16:23:45 -07004273 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004274 }
4275 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004276 if (mddev->bitmap_file) {
4277 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004278 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004279 }
NeilBrown36fa3062005-09-09 16:23:45 -07004280 mddev->bitmap_file = NULL;
4281 }
4282
NeilBrown32a76272005-06-21 17:17:14 -07004283 return err;
4284}
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286/*
4287 * set_array_info is used two different ways
4288 * The original usage is when creating a new array.
4289 * In this usage, raid_disks is > 0 and it together with
4290 * level, size, not_persistent,layout,chunksize determine the
4291 * shape of the array.
4292 * This will always create an array with a type-0.90.0 superblock.
4293 * The newer usage is when assembling an array.
4294 * In this case raid_disks will be 0, and the major_version field is
4295 * use to determine which style super-blocks are to be found on the devices.
4296 * The minor and patch _version numbers are also kept incase the
4297 * super_block handler wishes to interpret them.
4298 */
4299static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4300{
4301
4302 if (info->raid_disks == 0) {
4303 /* just setting version number for superblock loading */
4304 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004305 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 super_types[info->major_version].name == NULL) {
4307 /* maybe try to auto-load a module? */
4308 printk(KERN_INFO
4309 "md: superblock version %d not known\n",
4310 info->major_version);
4311 return -EINVAL;
4312 }
4313 mddev->major_version = info->major_version;
4314 mddev->minor_version = info->minor_version;
4315 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004316 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return 0;
4318 }
4319 mddev->major_version = MD_MAJOR_VERSION;
4320 mddev->minor_version = MD_MINOR_VERSION;
4321 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4322 mddev->ctime = get_seconds();
4323
4324 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004325 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 mddev->size = info->size;
4327 mddev->raid_disks = info->raid_disks;
4328 /* don't set md_minor, it is determined by which /dev/md* was
4329 * openned
4330 */
4331 if (info->state & (1<<MD_SB_CLEAN))
4332 mddev->recovery_cp = MaxSector;
4333 else
4334 mddev->recovery_cp = 0;
4335 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004336 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
4338 mddev->layout = info->layout;
4339 mddev->chunk_size = info->chunk_size;
4340
4341 mddev->max_disks = MD_SB_DISKS;
4342
NeilBrowne6910632008-02-06 01:39:51 -08004343 if (mddev->persistent)
4344 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004345 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
NeilBrownb2a27032005-11-28 13:44:12 -08004347 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4348 mddev->bitmap_offset = 0;
4349
NeilBrownf6705572006-03-27 01:18:11 -08004350 mddev->reshape_position = MaxSector;
4351
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 /*
4353 * Generate a 128 bit UUID
4354 */
4355 get_random_bytes(mddev->uuid, 16);
4356
NeilBrownf6705572006-03-27 01:18:11 -08004357 mddev->new_level = mddev->level;
4358 mddev->new_chunk = mddev->chunk_size;
4359 mddev->new_layout = mddev->layout;
4360 mddev->delta_disks = 0;
4361
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 return 0;
4363}
4364
NeilBrowna35b0d62006-01-06 00:20:49 -08004365static int update_size(mddev_t *mddev, unsigned long size)
4366{
4367 mdk_rdev_t * rdev;
4368 int rv;
4369 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004370 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004371
4372 if (mddev->pers->resize == NULL)
4373 return -EINVAL;
4374 /* The "size" is the amount of each device that is used.
4375 * This can only make sense for arrays with redundancy.
4376 * linear and raid0 always use whatever space is available
4377 * We can only consider changing the size if no resync
4378 * or reconstruction is happening, and if the new size
4379 * is acceptable. It must fit before the sb_offset or,
4380 * if that is <data_offset, it must fit before the
4381 * size of each device.
4382 * If size is zero, we find the largest size that fits.
4383 */
4384 if (mddev->sync_thread)
4385 return -EBUSY;
4386 ITERATE_RDEV(mddev,rdev,tmp) {
4387 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004388 avail = rdev->size * 2;
4389
NeilBrowna35b0d62006-01-06 00:20:49 -08004390 if (fit && (size == 0 || size > avail/2))
4391 size = avail/2;
4392 if (avail < ((sector_t)size << 1))
4393 return -ENOSPC;
4394 }
4395 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4396 if (!rv) {
4397 struct block_device *bdev;
4398
4399 bdev = bdget_disk(mddev->gendisk, 0);
4400 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004401 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004402 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004403 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004404 bdput(bdev);
4405 }
4406 }
4407 return rv;
4408}
4409
NeilBrownda943b992006-01-06 00:20:54 -08004410static int update_raid_disks(mddev_t *mddev, int raid_disks)
4411{
4412 int rv;
4413 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004414 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004415 return -EINVAL;
4416 if (raid_disks <= 0 ||
4417 raid_disks >= mddev->max_disks)
4418 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004419 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004420 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004421 mddev->delta_disks = raid_disks - mddev->raid_disks;
4422
4423 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004424 return rv;
4425}
4426
4427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428/*
4429 * update_array_info is used to change the configuration of an
4430 * on-line array.
4431 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4432 * fields in the info are checked against the array.
4433 * Any differences that cannot be handled will cause an error.
4434 * Normally, only one change can be managed at a time.
4435 */
4436static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4437{
4438 int rv = 0;
4439 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004440 int state = 0;
4441
4442 /* calculate expected state,ignoring low bits */
4443 if (mddev->bitmap && mddev->bitmap_offset)
4444 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 if (mddev->major_version != info->major_version ||
4447 mddev->minor_version != info->minor_version ||
4448/* mddev->patch_version != info->patch_version || */
4449 mddev->ctime != info->ctime ||
4450 mddev->level != info->level ||
4451/* mddev->layout != info->layout || */
4452 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004453 mddev->chunk_size != info->chunk_size ||
4454 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4455 ((state^info->state) & 0xfffffe00)
4456 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 return -EINVAL;
4458 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004459 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 if (mddev->raid_disks != info->raid_disks) cnt++;
4461 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004462 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 if (cnt == 0) return 0;
4464 if (cnt > 1) return -EINVAL;
4465
4466 if (mddev->layout != info->layout) {
4467 /* Change layout
4468 * we don't need to do anything at the md level, the
4469 * personality will take care of it all.
4470 */
4471 if (mddev->pers->reconfig == NULL)
4472 return -EINVAL;
4473 else
4474 return mddev->pers->reconfig(mddev, info->layout, -1);
4475 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004476 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004477 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478
NeilBrownda943b992006-01-06 00:20:54 -08004479 if (mddev->raid_disks != info->raid_disks)
4480 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
NeilBrown36fa3062005-09-09 16:23:45 -07004482 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4483 if (mddev->pers->quiesce == NULL)
4484 return -EINVAL;
4485 if (mddev->recovery || mddev->sync_thread)
4486 return -EBUSY;
4487 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4488 /* add the bitmap */
4489 if (mddev->bitmap)
4490 return -EEXIST;
4491 if (mddev->default_bitmap_offset == 0)
4492 return -EINVAL;
4493 mddev->bitmap_offset = mddev->default_bitmap_offset;
4494 mddev->pers->quiesce(mddev, 1);
4495 rv = bitmap_create(mddev);
4496 if (rv)
4497 bitmap_destroy(mddev);
4498 mddev->pers->quiesce(mddev, 0);
4499 } else {
4500 /* remove the bitmap */
4501 if (!mddev->bitmap)
4502 return -ENOENT;
4503 if (mddev->bitmap->file)
4504 return -EINVAL;
4505 mddev->pers->quiesce(mddev, 1);
4506 bitmap_destroy(mddev);
4507 mddev->pers->quiesce(mddev, 0);
4508 mddev->bitmap_offset = 0;
4509 }
4510 }
NeilBrown850b2b42006-10-03 01:15:46 -07004511 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return rv;
4513}
4514
4515static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4516{
4517 mdk_rdev_t *rdev;
4518
4519 if (mddev->pers == NULL)
4520 return -ENODEV;
4521
4522 rdev = find_rdev(mddev, dev);
4523 if (!rdev)
4524 return -ENODEV;
4525
4526 md_error(mddev, rdev);
4527 return 0;
4528}
4529
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004530static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4531{
4532 mddev_t *mddev = bdev->bd_disk->private_data;
4533
4534 geo->heads = 2;
4535 geo->sectors = 4;
4536 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4537 return 0;
4538}
4539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540static int md_ioctl(struct inode *inode, struct file *file,
4541 unsigned int cmd, unsigned long arg)
4542{
4543 int err = 0;
4544 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 mddev_t *mddev = NULL;
4546
4547 if (!capable(CAP_SYS_ADMIN))
4548 return -EACCES;
4549
4550 /*
4551 * Commands dealing with the RAID driver but not any
4552 * particular array:
4553 */
4554 switch (cmd)
4555 {
4556 case RAID_VERSION:
4557 err = get_version(argp);
4558 goto done;
4559
4560 case PRINT_RAID_DEBUG:
4561 err = 0;
4562 md_print_devices();
4563 goto done;
4564
4565#ifndef MODULE
4566 case RAID_AUTORUN:
4567 err = 0;
4568 autostart_arrays(arg);
4569 goto done;
4570#endif
4571 default:;
4572 }
4573
4574 /*
4575 * Commands creating/starting a new array:
4576 */
4577
4578 mddev = inode->i_bdev->bd_disk->private_data;
4579
4580 if (!mddev) {
4581 BUG();
4582 goto abort;
4583 }
4584
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 err = mddev_lock(mddev);
4586 if (err) {
4587 printk(KERN_INFO
4588 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4589 err, cmd);
4590 goto abort;
4591 }
4592
4593 switch (cmd)
4594 {
4595 case SET_ARRAY_INFO:
4596 {
4597 mdu_array_info_t info;
4598 if (!arg)
4599 memset(&info, 0, sizeof(info));
4600 else if (copy_from_user(&info, argp, sizeof(info))) {
4601 err = -EFAULT;
4602 goto abort_unlock;
4603 }
4604 if (mddev->pers) {
4605 err = update_array_info(mddev, &info);
4606 if (err) {
4607 printk(KERN_WARNING "md: couldn't update"
4608 " array info. %d\n", err);
4609 goto abort_unlock;
4610 }
4611 goto done_unlock;
4612 }
4613 if (!list_empty(&mddev->disks)) {
4614 printk(KERN_WARNING
4615 "md: array %s already has disks!\n",
4616 mdname(mddev));
4617 err = -EBUSY;
4618 goto abort_unlock;
4619 }
4620 if (mddev->raid_disks) {
4621 printk(KERN_WARNING
4622 "md: array %s already initialised!\n",
4623 mdname(mddev));
4624 err = -EBUSY;
4625 goto abort_unlock;
4626 }
4627 err = set_array_info(mddev, &info);
4628 if (err) {
4629 printk(KERN_WARNING "md: couldn't set"
4630 " array info. %d\n", err);
4631 goto abort_unlock;
4632 }
4633 }
4634 goto done_unlock;
4635
4636 default:;
4637 }
4638
4639 /*
4640 * Commands querying/configuring an existing array:
4641 */
NeilBrown32a76272005-06-21 17:17:14 -07004642 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004643 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrown32a76272005-06-21 17:17:14 -07004644 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
NeilBrown3f9d7b02006-12-22 01:11:41 -08004645 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4646 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 err = -ENODEV;
4648 goto abort_unlock;
4649 }
4650
4651 /*
4652 * Commands even a read-only array can execute:
4653 */
4654 switch (cmd)
4655 {
4656 case GET_ARRAY_INFO:
4657 err = get_array_info(mddev, argp);
4658 goto done_unlock;
4659
NeilBrown32a76272005-06-21 17:17:14 -07004660 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004661 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004662 goto done_unlock;
4663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 case GET_DISK_INFO:
4665 err = get_disk_info(mddev, argp);
4666 goto done_unlock;
4667
4668 case RESTART_ARRAY_RW:
4669 err = restart_array(mddev);
4670 goto done_unlock;
4671
4672 case STOP_ARRAY:
4673 err = do_md_stop (mddev, 0);
4674 goto done_unlock;
4675
4676 case STOP_ARRAY_RO:
4677 err = do_md_stop (mddev, 1);
4678 goto done_unlock;
4679
4680 /*
4681 * We have a problem here : there is no easy way to give a CHS
4682 * virtual geometry. We currently pretend that we have a 2 heads
4683 * 4 sectors (with a BIG number of cylinders...). This drives
4684 * dosfs just mad... ;-)
4685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
4687
4688 /*
4689 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004690 * superblock, so we do not allow them on read-only arrays.
4691 * However non-MD ioctls (e.g. get-size) will still come through
4692 * here and hit the 'default' below, so only disallow
4693 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 */
NeilBrownf91de922005-11-08 21:39:36 -08004695 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4696 mddev->ro && mddev->pers) {
4697 if (mddev->ro == 2) {
4698 mddev->ro = 0;
4699 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4700 md_wakeup_thread(mddev->thread);
4701
4702 } else {
4703 err = -EROFS;
4704 goto abort_unlock;
4705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
4707
4708 switch (cmd)
4709 {
4710 case ADD_NEW_DISK:
4711 {
4712 mdu_disk_info_t info;
4713 if (copy_from_user(&info, argp, sizeof(info)))
4714 err = -EFAULT;
4715 else
4716 err = add_new_disk(mddev, &info);
4717 goto done_unlock;
4718 }
4719
4720 case HOT_REMOVE_DISK:
4721 err = hot_remove_disk(mddev, new_decode_dev(arg));
4722 goto done_unlock;
4723
4724 case HOT_ADD_DISK:
4725 err = hot_add_disk(mddev, new_decode_dev(arg));
4726 goto done_unlock;
4727
4728 case SET_DISK_FAULTY:
4729 err = set_disk_faulty(mddev, new_decode_dev(arg));
4730 goto done_unlock;
4731
4732 case RUN_ARRAY:
4733 err = do_md_run (mddev);
4734 goto done_unlock;
4735
NeilBrown32a76272005-06-21 17:17:14 -07004736 case SET_BITMAP_FILE:
4737 err = set_bitmap_file(mddev, (int)arg);
4738 goto done_unlock;
4739
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 err = -EINVAL;
4742 goto abort_unlock;
4743 }
4744
4745done_unlock:
4746abort_unlock:
4747 mddev_unlock(mddev);
4748
4749 return err;
4750done:
4751 if (err)
4752 MD_BUG();
4753abort:
4754 return err;
4755}
4756
4757static int md_open(struct inode *inode, struct file *file)
4758{
4759 /*
4760 * Succeed if we can lock the mddev, which confirms that
4761 * it isn't being stopped right now.
4762 */
4763 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4764 int err;
4765
NeilBrownd63a5a72006-12-08 02:36:17 -08004766 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 goto out;
4768
4769 err = 0;
4770 mddev_get(mddev);
4771 mddev_unlock(mddev);
4772
4773 check_disk_change(inode->i_bdev);
4774 out:
4775 return err;
4776}
4777
4778static int md_release(struct inode *inode, struct file * file)
4779{
4780 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4781
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004782 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 mddev_put(mddev);
4784
4785 return 0;
4786}
4787
Linus Torvalds44ce62942007-05-09 18:51:36 -07004788static int md_media_changed(struct gendisk *disk)
4789{
4790 mddev_t *mddev = disk->private_data;
4791
4792 return mddev->changed;
4793}
4794
4795static int md_revalidate(struct gendisk *disk)
4796{
4797 mddev_t *mddev = disk->private_data;
4798
4799 mddev->changed = 0;
4800 return 0;
4801}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802static struct block_device_operations md_fops =
4803{
4804 .owner = THIS_MODULE,
4805 .open = md_open,
4806 .release = md_release,
4807 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004808 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004809 .media_changed = md_media_changed,
4810 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811};
4812
Adrian Bunk75c96f82005-05-05 16:16:09 -07004813static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814{
4815 mdk_thread_t *thread = arg;
4816
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 /*
4818 * md_thread is a 'system-thread', it's priority should be very
4819 * high. We avoid resource deadlocks individually in each
4820 * raid personality. (RAID5 does preallocation) We also use RR and
4821 * the very same RT priority as kswapd, thus we will never get
4822 * into a priority inversion deadlock.
4823 *
4824 * we definitely have to have equal or higher priority than
4825 * bdflush, otherwise bdflush will deadlock if there are too
4826 * many dirty RAID5 blocks.
4827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828
NeilBrown6985c432005-10-19 21:23:47 -07004829 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004830 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
NeilBrown93588e22005-11-15 00:09:12 -08004832 /* We need to wait INTERRUPTIBLE so that
4833 * we don't add to the load-average.
4834 * That means we need to be sure no signals are
4835 * pending
4836 */
4837 if (signal_pending(current))
4838 flush_signals(current);
4839
4840 wait_event_interruptible_timeout
4841 (thread->wqueue,
4842 test_bit(THREAD_WAKEUP, &thread->flags)
4843 || kthread_should_stop(),
4844 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845
4846 clear_bit(THREAD_WAKEUP, &thread->flags);
4847
NeilBrown787453c2005-11-08 21:39:43 -08004848 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004850
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 return 0;
4852}
4853
4854void md_wakeup_thread(mdk_thread_t *thread)
4855{
4856 if (thread) {
4857 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4858 set_bit(THREAD_WAKEUP, &thread->flags);
4859 wake_up(&thread->wqueue);
4860 }
4861}
4862
4863mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4864 const char *name)
4865{
4866 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
NeilBrown9ffae0c2006-01-06 00:20:32 -08004868 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869 if (!thread)
4870 return NULL;
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 init_waitqueue_head(&thread->wqueue);
4873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 thread->run = run;
4875 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004876 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004877 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004878 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 kfree(thread);
4880 return NULL;
4881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 return thread;
4883}
4884
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885void md_unregister_thread(mdk_thread_t *thread)
4886{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07004887 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07004888
4889 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 kfree(thread);
4891}
4892
4893void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4894{
4895 if (!mddev) {
4896 MD_BUG();
4897 return;
4898 }
4899
NeilBrownb2d444d2005-11-08 21:39:31 -08004900 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 return;
NeilBrown32a76272005-06-21 17:17:14 -07004902/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4904 mdname(mddev),
4905 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4906 __builtin_return_address(0),__builtin_return_address(1),
4907 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004908*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004909 if (!mddev->pers)
4910 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 if (!mddev->pers->error_handler)
4912 return;
4913 mddev->pers->error_handler(mddev,rdev);
4914 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4915 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4916 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004917 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918}
4919
4920/* seq_file implementation /proc/mdstat */
4921
4922static void status_unused(struct seq_file *seq)
4923{
4924 int i = 0;
4925 mdk_rdev_t *rdev;
4926 struct list_head *tmp;
4927
4928 seq_printf(seq, "unused devices: ");
4929
4930 ITERATE_RDEV_PENDING(rdev,tmp) {
4931 char b[BDEVNAME_SIZE];
4932 i++;
4933 seq_printf(seq, "%s ",
4934 bdevname(rdev->bdev,b));
4935 }
4936 if (!i)
4937 seq_printf(seq, "<none>");
4938
4939 seq_printf(seq, "\n");
4940}
4941
4942
4943static void status_resync(struct seq_file *seq, mddev_t * mddev)
4944{
NeilBrown4588b422006-03-27 01:18:04 -08004945 sector_t max_blocks, resync, res;
4946 unsigned long dt, db, rt;
4947 int scale;
4948 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949
4950 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4951
4952 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4953 max_blocks = mddev->resync_max_sectors >> 1;
4954 else
4955 max_blocks = mddev->size;
4956
4957 /*
4958 * Should not happen.
4959 */
4960 if (!max_blocks) {
4961 MD_BUG();
4962 return;
4963 }
NeilBrown4588b422006-03-27 01:18:04 -08004964 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4965 * in a sector_t, and (max_blocks>>scale) will fit in a
4966 * u32, as those are the requirements for sector_div.
4967 * Thus 'scale' must be at least 10
4968 */
4969 scale = 10;
4970 if (sizeof(sector_t) > sizeof(unsigned long)) {
4971 while ( max_blocks/2 > (1ULL<<(scale+32)))
4972 scale++;
4973 }
4974 res = (resync>>scale)*1000;
4975 sector_div(res, (u32)((max_blocks>>scale)+1));
4976
4977 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 {
NeilBrown4588b422006-03-27 01:18:04 -08004979 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980 seq_printf(seq, "[");
4981 for (i = 0; i < x; i++)
4982 seq_printf(seq, "=");
4983 seq_printf(seq, ">");
4984 for (i = 0; i < y; i++)
4985 seq_printf(seq, ".");
4986 seq_printf(seq, "] ");
4987 }
NeilBrown4588b422006-03-27 01:18:04 -08004988 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004989 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4990 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07004991 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
4992 "check" :
4993 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4994 "resync" : "recovery"))),
4995 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08004996 (unsigned long long) resync,
4997 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998
4999 /*
5000 * We do not want to overflow, so the order of operands and
5001 * the * 100 / 100 trick are important. We do a +1 to be
5002 * safe against division by zero. We only estimate anyway.
5003 *
5004 * dt: time from mark until now
5005 * db: blocks written from mark until now
5006 * rt: remaining time
5007 */
5008 dt = ((jiffies - mddev->resync_mark) / HZ);
5009 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005010 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5011 - mddev->resync_mark_cnt;
5012 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013
5014 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5015
NeilBrownff4e8d92006-07-10 04:44:16 -07005016 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017}
5018
5019static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5020{
5021 struct list_head *tmp;
5022 loff_t l = *pos;
5023 mddev_t *mddev;
5024
5025 if (l >= 0x10000)
5026 return NULL;
5027 if (!l--)
5028 /* header */
5029 return (void*)1;
5030
5031 spin_lock(&all_mddevs_lock);
5032 list_for_each(tmp,&all_mddevs)
5033 if (!l--) {
5034 mddev = list_entry(tmp, mddev_t, all_mddevs);
5035 mddev_get(mddev);
5036 spin_unlock(&all_mddevs_lock);
5037 return mddev;
5038 }
5039 spin_unlock(&all_mddevs_lock);
5040 if (!l--)
5041 return (void*)2;/* tail */
5042 return NULL;
5043}
5044
5045static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5046{
5047 struct list_head *tmp;
5048 mddev_t *next_mddev, *mddev = v;
5049
5050 ++*pos;
5051 if (v == (void*)2)
5052 return NULL;
5053
5054 spin_lock(&all_mddevs_lock);
5055 if (v == (void*)1)
5056 tmp = all_mddevs.next;
5057 else
5058 tmp = mddev->all_mddevs.next;
5059 if (tmp != &all_mddevs)
5060 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5061 else {
5062 next_mddev = (void*)2;
5063 *pos = 0x10000;
5064 }
5065 spin_unlock(&all_mddevs_lock);
5066
5067 if (v != (void*)1)
5068 mddev_put(mddev);
5069 return next_mddev;
5070
5071}
5072
5073static void md_seq_stop(struct seq_file *seq, void *v)
5074{
5075 mddev_t *mddev = v;
5076
5077 if (mddev && v != (void*)1 && v != (void*)2)
5078 mddev_put(mddev);
5079}
5080
NeilBrownd7603b72006-01-06 00:20:30 -08005081struct mdstat_info {
5082 int event;
5083};
5084
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085static int md_seq_show(struct seq_file *seq, void *v)
5086{
5087 mddev_t *mddev = v;
5088 sector_t size;
5089 struct list_head *tmp2;
5090 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005091 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005092 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
5094 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005095 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 seq_printf(seq, "Personalities : ");
5097 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005098 list_for_each_entry(pers, &pers_list, list)
5099 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
5101 spin_unlock(&pers_lock);
5102 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005103 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 return 0;
5105 }
5106 if (v == (void*)2) {
5107 status_unused(seq);
5108 return 0;
5109 }
5110
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005111 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005113
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5115 seq_printf(seq, "%s : %sactive", mdname(mddev),
5116 mddev->pers ? "" : "in");
5117 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005118 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005120 if (mddev->ro==2)
5121 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 seq_printf(seq, " %s", mddev->pers->name);
5123 }
5124
5125 size = 0;
5126 ITERATE_RDEV(mddev,rdev,tmp2) {
5127 char b[BDEVNAME_SIZE];
5128 seq_printf(seq, " %s[%d]",
5129 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005130 if (test_bit(WriteMostly, &rdev->flags))
5131 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005132 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 seq_printf(seq, "(F)");
5134 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005135 } else if (rdev->raid_disk < 0)
5136 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 size += rdev->size;
5138 }
5139
5140 if (!list_empty(&mddev->disks)) {
5141 if (mddev->pers)
5142 seq_printf(seq, "\n %llu blocks",
5143 (unsigned long long)mddev->array_size);
5144 else
5145 seq_printf(seq, "\n %llu blocks",
5146 (unsigned long long)size);
5147 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005148 if (mddev->persistent) {
5149 if (mddev->major_version != 0 ||
5150 mddev->minor_version != 90) {
5151 seq_printf(seq," super %d.%d",
5152 mddev->major_version,
5153 mddev->minor_version);
5154 }
NeilBrowne6910632008-02-06 01:39:51 -08005155 } else if (mddev->external)
5156 seq_printf(seq, " super external:%s",
5157 mddev->metadata_type);
5158 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005159 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160
5161 if (mddev->pers) {
5162 mddev->pers->status (seq, mddev);
5163 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005164 if (mddev->pers->sync_request) {
5165 if (mddev->curr_resync > 2) {
5166 status_resync (seq, mddev);
5167 seq_printf(seq, "\n ");
5168 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5169 seq_printf(seq, "\tresync=DELAYED\n ");
5170 else if (mddev->recovery_cp < MaxSector)
5171 seq_printf(seq, "\tresync=PENDING\n ");
5172 }
NeilBrown32a76272005-06-21 17:17:14 -07005173 } else
5174 seq_printf(seq, "\n ");
5175
5176 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005177 unsigned long chunk_kb;
5178 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005179 spin_lock_irqsave(&bitmap->lock, flags);
5180 chunk_kb = bitmap->chunksize >> 10;
5181 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5182 "%lu%s chunk",
5183 bitmap->pages - bitmap->missing_pages,
5184 bitmap->pages,
5185 (bitmap->pages - bitmap->missing_pages)
5186 << (PAGE_SHIFT - 10),
5187 chunk_kb ? chunk_kb : bitmap->chunksize,
5188 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005189 if (bitmap->file) {
5190 seq_printf(seq, ", file: ");
Josef Sipekc649bb92006-12-08 02:37:19 -08005191 seq_path(seq, bitmap->file->f_path.mnt,
5192 bitmap->file->f_path.dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005193 }
NeilBrown78d742d2005-06-21 17:17:15 -07005194
NeilBrown32a76272005-06-21 17:17:14 -07005195 seq_printf(seq, "\n");
5196 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 }
5198
5199 seq_printf(seq, "\n");
5200 }
5201 mddev_unlock(mddev);
5202
5203 return 0;
5204}
5205
5206static struct seq_operations md_seq_ops = {
5207 .start = md_seq_start,
5208 .next = md_seq_next,
5209 .stop = md_seq_stop,
5210 .show = md_seq_show,
5211};
5212
5213static int md_seq_open(struct inode *inode, struct file *file)
5214{
5215 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005216 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5217 if (mi == NULL)
5218 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
5220 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005221 if (error)
5222 kfree(mi);
5223 else {
5224 struct seq_file *p = file->private_data;
5225 p->private = mi;
5226 mi->event = atomic_read(&md_event_count);
5227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 return error;
5229}
5230
NeilBrownd7603b72006-01-06 00:20:30 -08005231static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5232{
5233 struct seq_file *m = filp->private_data;
5234 struct mdstat_info *mi = m->private;
5235 int mask;
5236
5237 poll_wait(filp, &md_event_waiters, wait);
5238
5239 /* always allow read */
5240 mask = POLLIN | POLLRDNORM;
5241
5242 if (mi->event != atomic_read(&md_event_count))
5243 mask |= POLLERR | POLLPRI;
5244 return mask;
5245}
5246
Arjan van de Venfa027c22007-02-12 00:55:33 -08005247static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005248 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 .open = md_seq_open,
5250 .read = seq_read,
5251 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005252 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005253 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254};
5255
NeilBrown2604b702006-01-06 00:20:36 -08005256int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005259 list_add_tail(&p->list, &pers_list);
5260 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 spin_unlock(&pers_lock);
5262 return 0;
5263}
5264
NeilBrown2604b702006-01-06 00:20:36 -08005265int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266{
NeilBrown2604b702006-01-06 00:20:36 -08005267 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005269 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 spin_unlock(&pers_lock);
5271 return 0;
5272}
5273
5274static int is_mddev_idle(mddev_t *mddev)
5275{
5276 mdk_rdev_t * rdev;
5277 struct list_head *tmp;
5278 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005279 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280
5281 idle = 1;
5282 ITERATE_RDEV(mddev,rdev,tmp) {
5283 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005284 curr_events = disk_stat_read(disk, sectors[0]) +
5285 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005287 /* sync IO will cause sync_io to increase before the disk_stats
5288 * as sync_io is counted when a request starts, and
5289 * disk_stats is counted when it completes.
5290 * So resync activity will cause curr_events to be smaller than
5291 * when there was no such activity.
5292 * non-sync IO will cause disk_stat to increase without
5293 * increasing sync_io so curr_events will (eventually)
5294 * be larger than it was before. Once it becomes
5295 * substantially larger, the test below will cause
5296 * the array to appear non-idle, and resync will slow
5297 * down.
5298 * If there is a lot of outstanding resync activity when
5299 * we set last_event to curr_events, then all that activity
5300 * completing might cause the array to appear non-idle
5301 * and resync will be slowed down even though there might
5302 * not have been non-resync activity. This will only
5303 * happen once though. 'last_events' will soon reflect
5304 * the state where there is little or no outstanding
5305 * resync requests, and further resync activity will
5306 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005307 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005309 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 rdev->last_events = curr_events;
5311 idle = 0;
5312 }
5313 }
5314 return idle;
5315}
5316
5317void md_done_sync(mddev_t *mddev, int blocks, int ok)
5318{
5319 /* another "blocks" (512byte) blocks have been synced */
5320 atomic_sub(blocks, &mddev->recovery_active);
5321 wake_up(&mddev->recovery_wait);
5322 if (!ok) {
5323 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5324 md_wakeup_thread(mddev->thread);
5325 // stop recovery, signal do_sync ....
5326 }
5327}
5328
5329
NeilBrown06d91a52005-06-21 17:17:12 -07005330/* md_write_start(mddev, bi)
5331 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005332 * in superblock) before writing, schedule a superblock update
5333 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005334 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005335void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336{
NeilBrown06d91a52005-06-21 17:17:12 -07005337 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005338 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005339
NeilBrownf91de922005-11-08 21:39:36 -08005340 BUG_ON(mddev->ro == 1);
5341 if (mddev->ro == 2) {
5342 /* need to switch to read/write */
5343 mddev->ro = 0;
5344 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5345 md_wakeup_thread(mddev->thread);
5346 }
NeilBrown06d91a52005-06-21 17:17:12 -07005347 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005348 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005349 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005350 if (mddev->in_sync) {
5351 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005352 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005353 md_wakeup_thread(mddev->thread);
5354 }
NeilBrowna9701a32005-11-08 21:39:34 -08005355 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005356 }
NeilBrown850b2b42006-10-03 01:15:46 -07005357 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358}
5359
5360void md_write_end(mddev_t *mddev)
5361{
5362 if (atomic_dec_and_test(&mddev->writes_pending)) {
5363 if (mddev->safemode == 2)
5364 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005365 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5367 }
5368}
5369
NeilBrown2a2275d2007-01-26 00:57:11 -08005370/* md_allow_write(mddev)
5371 * Calling this ensures that the array is marked 'active' so that writes
5372 * may proceed without blocking. It is important to call this before
5373 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5374 * Must be called with mddev_lock held.
5375 */
5376void md_allow_write(mddev_t *mddev)
5377{
5378 if (!mddev->pers)
5379 return;
5380 if (mddev->ro)
5381 return;
5382
5383 spin_lock_irq(&mddev->write_lock);
5384 if (mddev->in_sync) {
5385 mddev->in_sync = 0;
5386 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5387 if (mddev->safemode_delay &&
5388 mddev->safemode == 0)
5389 mddev->safemode = 1;
5390 spin_unlock_irq(&mddev->write_lock);
5391 md_update_sb(mddev, 0);
5392 } else
5393 spin_unlock_irq(&mddev->write_lock);
5394}
5395EXPORT_SYMBOL_GPL(md_allow_write);
5396
Adrian Bunk75c96f82005-05-05 16:16:09 -07005397static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
5399#define SYNC_MARKS 10
5400#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005401void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402{
5403 mddev_t *mddev2;
5404 unsigned int currspeed = 0,
5405 window;
NeilBrown57afd892005-06-21 17:17:13 -07005406 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 unsigned long mark[SYNC_MARKS];
5408 sector_t mark_cnt[SYNC_MARKS];
5409 int last_mark,m;
5410 struct list_head *tmp;
5411 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005412 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005413 struct list_head *rtmp;
5414 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005415 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
5417 /* just incase thread restarts... */
5418 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5419 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005420 if (mddev->ro) /* never try to sync a read-only array */
5421 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422
NeilBrown61df9d92006-10-03 01:15:57 -07005423 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5424 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5425 desc = "data-check";
5426 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5427 desc = "requested-resync";
5428 else
5429 desc = "resync";
5430 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5431 desc = "reshape";
5432 else
5433 desc = "recovery";
5434
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 /* we overload curr_resync somewhat here.
5436 * 0 == not engaged in resync at all
5437 * 2 == checking that there is no conflict with another sync
5438 * 1 == like 2, but have yielded to allow conflicting resync to
5439 * commense
5440 * other == active in resync - this many blocks
5441 *
5442 * Before starting a resync we must have set curr_resync to
5443 * 2, and then checked that every "conflicting" array has curr_resync
5444 * less than ours. When we find one that is the same or higher
5445 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5446 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5447 * This will mean we have to start checking from the beginning again.
5448 *
5449 */
5450
5451 do {
5452 mddev->curr_resync = 2;
5453
5454 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005455 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005456 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 goto skip;
5458 }
5459 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 if (mddev2 == mddev)
5461 continue;
5462 if (mddev2->curr_resync &&
5463 match_mddev_units(mddev,mddev2)) {
5464 DEFINE_WAIT(wq);
5465 if (mddev < mddev2 && mddev->curr_resync == 2) {
5466 /* arbitrarily yield */
5467 mddev->curr_resync = 1;
5468 wake_up(&resync_wait);
5469 }
5470 if (mddev > mddev2 && mddev->curr_resync == 1)
5471 /* no need to wait here, we can wait the next
5472 * time 'round when curr_resync == 2
5473 */
5474 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005475 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5476 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005477 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005478 printk(KERN_INFO "md: delaying %s of %s"
5479 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005481 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 mddev_put(mddev2);
5483 schedule();
5484 finish_wait(&resync_wait, &wq);
5485 goto try_again;
5486 }
5487 finish_wait(&resync_wait, &wq);
5488 }
5489 }
5490 } while (mddev->curr_resync < 2);
5491
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005492 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005493 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005495 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 */
5497 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005498 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005499 /* we don't use the checkpoint if there's a bitmap */
5500 if (!mddev->bitmap &&
5501 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5502 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005503 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5504 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005505 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 /* recovery follows the physical size of devices */
5507 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005508 j = MaxSector;
5509 ITERATE_RDEV(mddev,rdev,rtmp)
5510 if (rdev->raid_disk >= 0 &&
5511 !test_bit(Faulty, &rdev->flags) &&
5512 !test_bit(In_sync, &rdev->flags) &&
5513 rdev->recovery_offset < j)
5514 j = rdev->recovery_offset;
5515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516
NeilBrown61df9d92006-10-03 01:15:57 -07005517 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5518 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5519 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005520 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005521 "(but not more than %d KB/sec) for %s.\n",
5522 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005523
5524 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005525
NeilBrown57afd892005-06-21 17:17:13 -07005526 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 for (m = 0; m < SYNC_MARKS; m++) {
5528 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005529 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 }
5531 last_mark = 0;
5532 mddev->resync_mark = mark[last_mark];
5533 mddev->resync_mark_cnt = mark_cnt[last_mark];
5534
5535 /*
5536 * Tune reconstruction:
5537 */
5538 window = 32*(PAGE_SIZE/512);
5539 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5540 window/2,(unsigned long long) max_sectors/2);
5541
5542 atomic_set(&mddev->recovery_active, 0);
5543 init_waitqueue_head(&mddev->recovery_wait);
5544 last_check = 0;
5545
5546 if (j>2) {
5547 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005548 "md: resuming %s of %s from checkpoint.\n",
5549 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 mddev->curr_resync = j;
5551 }
5552
5553 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005554 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555
NeilBrown57afd892005-06-21 17:17:13 -07005556 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005557 if (j >= mddev->resync_max) {
5558 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5559 wait_event(mddev->recovery_wait,
5560 mddev->resync_max > j
5561 || kthread_should_stop());
5562 }
5563 if (kthread_should_stop())
5564 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005565 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005566 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005567 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5569 goto out;
5570 }
NeilBrown57afd892005-06-21 17:17:13 -07005571
5572 if (!skipped) { /* actual IO requested */
5573 io_sectors += sectors;
5574 atomic_add(sectors, &mddev->recovery_active);
5575 }
5576
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 j += sectors;
5578 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005579 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005580 if (last_check == 0)
5581 /* this is the earliers that rebuilt will be
5582 * visible in /proc/mdstat
5583 */
5584 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005585
5586 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587 continue;
5588
NeilBrown57afd892005-06-21 17:17:13 -07005589 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590
5591 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5592 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5593 break;
5594
5595 repeat:
5596 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5597 /* step marks */
5598 int next = (last_mark+1) % SYNC_MARKS;
5599
5600 mddev->resync_mark = mark[next];
5601 mddev->resync_mark_cnt = mark_cnt[next];
5602 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005603 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 last_mark = next;
5605 }
5606
5607
NeilBrownc6207272008-02-06 01:39:52 -08005608 if (kthread_should_stop())
5609 goto interrupted;
5610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611
5612 /*
5613 * this loop exits only if either when we are slower than
5614 * the 'hard' speed limit, or the system was IO-idle for
5615 * a jiffy.
5616 * the system might be non-idle CPU-wise, but we only care
5617 * about not overloading the IO subsystem. (things like an
5618 * e2fsck being done on the RAID array should execute fast)
5619 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005620 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 cond_resched();
5622
NeilBrown57afd892005-06-21 17:17:13 -07005623 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5624 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
NeilBrown88202a02006-01-06 00:21:36 -08005626 if (currspeed > speed_min(mddev)) {
5627 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005629 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 goto repeat;
5631 }
5632 }
5633 }
NeilBrown61df9d92006-10-03 01:15:57 -07005634 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 /*
5636 * this also signals 'finished resyncing' to md_stop
5637 */
5638 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005639 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
5641 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5642
5643 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005644 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645
5646 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005647 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005648 mddev->curr_resync > 2) {
5649 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5650 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5651 if (mddev->curr_resync >= mddev->recovery_cp) {
5652 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005653 "md: checkpointing %s of %s.\n",
5654 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005655 mddev->recovery_cp = mddev->curr_resync;
5656 }
5657 } else
5658 mddev->recovery_cp = MaxSector;
5659 } else {
5660 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5661 mddev->curr_resync = MaxSector;
5662 ITERATE_RDEV(mddev,rdev,rtmp)
5663 if (rdev->raid_disk >= 0 &&
5664 !test_bit(Faulty, &rdev->flags) &&
5665 !test_bit(In_sync, &rdev->flags) &&
5666 rdev->recovery_offset < mddev->curr_resync)
5667 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 }
NeilBrown17571282006-12-10 02:20:52 -08005670 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 skip:
5673 mddev->curr_resync = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005674 mddev->resync_max = MaxSector;
5675 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 wake_up(&resync_wait);
5677 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5678 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005679 return;
5680
5681 interrupted:
5682 /*
5683 * got a signal, exit.
5684 */
5685 printk(KERN_INFO
5686 "md: md_do_sync() got signal ... exiting\n");
5687 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5688 goto out;
5689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690}
NeilBrown29269552006-03-27 01:18:10 -08005691EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
5693
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005694static int remove_and_add_spares(mddev_t *mddev)
5695{
5696 mdk_rdev_t *rdev;
5697 struct list_head *rtmp;
5698 int spares = 0;
5699
5700 ITERATE_RDEV(mddev,rdev,rtmp)
5701 if (rdev->raid_disk >= 0 &&
NeilBrownc303da62008-02-06 01:39:51 -08005702 !mddev->external &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005703 (test_bit(Faulty, &rdev->flags) ||
5704 ! test_bit(In_sync, &rdev->flags)) &&
5705 atomic_read(&rdev->nr_pending)==0) {
5706 if (mddev->pers->hot_remove_disk(
5707 mddev, rdev->raid_disk)==0) {
5708 char nm[20];
5709 sprintf(nm,"rd%d", rdev->raid_disk);
5710 sysfs_remove_link(&mddev->kobj, nm);
5711 rdev->raid_disk = -1;
5712 }
5713 }
5714
5715 if (mddev->degraded) {
5716 ITERATE_RDEV(mddev,rdev,rtmp)
5717 if (rdev->raid_disk < 0
5718 && !test_bit(Faulty, &rdev->flags)) {
5719 rdev->recovery_offset = 0;
5720 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5721 char nm[20];
5722 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005723 if (sysfs_create_link(&mddev->kobj,
5724 &rdev->kobj, nm))
5725 printk(KERN_WARNING
5726 "md: cannot register "
5727 "%s for %s\n",
5728 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005729 spares++;
5730 md_new_event(mddev);
5731 } else
5732 break;
5733 }
5734 }
5735 return spares;
5736}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737/*
5738 * This routine is regularly called by all per-raid-array threads to
5739 * deal with generic issues like resync and super-block update.
5740 * Raid personalities that don't have a thread (linear/raid0) do not
5741 * need this as they never do any recovery or update the superblock.
5742 *
5743 * It does not do any resync itself, but rather "forks" off other threads
5744 * to do that as needed.
5745 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5746 * "->recovery" and create a thread at ->sync_thread.
5747 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5748 * and wakeups up this thread which will reap the thread and finish up.
5749 * This thread also removes any faulty devices (with nr_pending == 0).
5750 *
5751 * The overall approach is:
5752 * 1/ if the superblock needs updating, update it.
5753 * 2/ If a recovery thread is running, don't do anything else.
5754 * 3/ If recovery has finished, clean up, possibly marking spares active.
5755 * 4/ If there are any faulty devices, remove them.
5756 * 5/ If array is degraded, try to add spares devices
5757 * 6/ If array has spares or is not in-sync, start a resync thread.
5758 */
5759void md_check_recovery(mddev_t *mddev)
5760{
5761 mdk_rdev_t *rdev;
5762 struct list_head *rtmp;
5763
5764
NeilBrown5f404022005-06-21 17:17:16 -07005765 if (mddev->bitmap)
5766 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
5768 if (mddev->ro)
5769 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005770
5771 if (signal_pending(current)) {
5772 if (mddev->pers->sync_request) {
5773 printk(KERN_INFO "md: %s in immediate safe mode\n",
5774 mdname(mddev));
5775 mddev->safemode = 2;
5776 }
5777 flush_signals(current);
5778 }
5779
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08005781 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005783 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5784 (mddev->safemode == 1) ||
5785 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5786 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 ))
5788 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005789
NeilBrowndf5b89b2006-03-27 01:18:20 -08005790 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005791 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005792
NeilBrowna9701a32005-11-08 21:39:34 -08005793 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005794 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5795 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5796 mddev->in_sync = 1;
NeilBrowne6910632008-02-06 01:39:51 -08005797 if (mddev->persistent)
5798 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005799 }
5800 if (mddev->safemode == 1)
5801 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005802 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005803
NeilBrown850b2b42006-10-03 01:15:46 -07005804 if (mddev->flags)
5805 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005806
NeilBrown06d91a52005-06-21 17:17:12 -07005807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5809 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5810 /* resync/recovery still happening */
5811 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5812 goto unlock;
5813 }
5814 if (mddev->sync_thread) {
5815 /* resync has finished, collect result */
5816 md_unregister_thread(mddev->sync_thread);
5817 mddev->sync_thread = NULL;
5818 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5819 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5820 /* success...*/
5821 /* activate any spares */
5822 mddev->pers->spare_active(mddev);
5823 }
NeilBrown850b2b42006-10-03 01:15:46 -07005824 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005825
5826 /* if array is no-longer degraded, then any saved_raid_disk
5827 * information must be scrapped
5828 */
5829 if (!mddev->degraded)
5830 ITERATE_RDEV(mddev,rdev,rtmp)
5831 rdev->saved_raid_disk = -1;
5832
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 mddev->recovery = 0;
5834 /* flag recovery needed just to double check */
5835 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005836 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837 goto unlock;
5838 }
NeilBrown24dd4692005-11-08 21:39:26 -08005839 /* Clear some bits that don't mean anything, but
5840 * might be left set
5841 */
5842 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5843 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5844 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5845 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005847 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5848 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 /* no recovery is running.
5850 * remove any failed drives, then
5851 * add spares if possible.
5852 * Spare are also removed and re-added, to allow
5853 * the personality to fail the re-add.
5854 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005856 if (mddev->reshape_position != MaxSector) {
5857 if (mddev->pers->check_reshape(mddev) != 0)
5858 /* Cannot proceed */
5859 goto unlock;
5860 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5861 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005862 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5863 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5864 } else if (mddev->recovery_cp < MaxSector) {
5865 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5866 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5867 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005869
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 if (mddev->pers->sync_request) {
5871 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005872 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5873 /* We are adding a device or devices to an array
5874 * which has the bitmap stored on all devices.
5875 * So make sure all bitmap pages get written
5876 */
5877 bitmap_write_all(mddev->bitmap);
5878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 mddev->sync_thread = md_register_thread(md_do_sync,
5880 mddev,
5881 "%s_resync");
5882 if (!mddev->sync_thread) {
5883 printk(KERN_ERR "%s: could not start resync"
5884 " thread...\n",
5885 mdname(mddev));
5886 /* leave the spares where they are, it shouldn't hurt */
5887 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005888 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005890 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891 }
5892 unlock:
5893 mddev_unlock(mddev);
5894 }
5895}
5896
Adrian Bunk75c96f82005-05-05 16:16:09 -07005897static int md_notify_reboot(struct notifier_block *this,
5898 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899{
5900 struct list_head *tmp;
5901 mddev_t *mddev;
5902
5903 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5904
5905 printk(KERN_INFO "md: stopping all md devices.\n");
5906
5907 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005908 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005910 mddev_unlock(mddev);
5911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 /*
5913 * certain more exotic SCSI devices are known to be
5914 * volatile wrt too early system reboots. While the
5915 * right place to handle this issue is the given
5916 * driver, we do want to have a safe RAID driver ...
5917 */
5918 mdelay(1000*1);
5919 }
5920 return NOTIFY_DONE;
5921}
5922
Adrian Bunk75c96f82005-05-05 16:16:09 -07005923static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 .notifier_call = md_notify_reboot,
5925 .next = NULL,
5926 .priority = INT_MAX, /* before any real devices */
5927};
5928
5929static void md_geninit(void)
5930{
5931 struct proc_dir_entry *p;
5932
5933 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5934
5935 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5936 if (p)
5937 p->proc_fops = &md_seq_fops;
5938}
5939
Adrian Bunk75c96f82005-05-05 16:16:09 -07005940static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 if (register_blkdev(MAJOR_NR, "md"))
5943 return -1;
5944 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5945 unregister_blkdev(MAJOR_NR, "md");
5946 return -1;
5947 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005948 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5949 md_probe, NULL, NULL);
5950 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 md_probe, NULL, NULL);
5952
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005954 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955
5956 md_geninit();
5957 return (0);
5958}
5959
5960
5961#ifndef MODULE
5962
5963/*
5964 * Searches all registered partitions for autorun RAID arrays
5965 * at boot time.
5966 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005967
5968static LIST_HEAD(all_detected_devices);
5969struct detected_devices_node {
5970 struct list_head list;
5971 dev_t dev;
5972};
Linus Torvalds1da177e2005-04-16 15:20:36 -07005973
5974void md_autodetect_dev(dev_t dev)
5975{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005976 struct detected_devices_node *node_detected_dev;
5977
5978 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
5979 if (node_detected_dev) {
5980 node_detected_dev->dev = dev;
5981 list_add_tail(&node_detected_dev->list, &all_detected_devices);
5982 } else {
5983 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
5984 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
5985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986}
5987
5988
5989static void autostart_arrays(int part)
5990{
5991 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07005992 struct detected_devices_node *node_detected_dev;
5993 dev_t dev;
5994 int i_scanned, i_passed;
5995
5996 i_scanned = 0;
5997 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
5999 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6000
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006001 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6002 i_scanned++;
6003 node_detected_dev = list_entry(all_detected_devices.next,
6004 struct detected_devices_node, list);
6005 list_del(&node_detected_dev->list);
6006 dev = node_detected_dev->dev;
6007 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006008 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 if (IS_ERR(rdev))
6010 continue;
6011
NeilBrownb2d444d2005-11-08 21:39:31 -08006012 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 MD_BUG();
6014 continue;
6015 }
6016 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006017 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006019
6020 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6021 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022
6023 autorun_devices(part);
6024}
6025
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006026#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
6028static __exit void md_exit(void)
6029{
6030 mddev_t *mddev;
6031 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006032
NeilBrowne8703fe2006-10-03 01:15:59 -07006033 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6034 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
6036 unregister_blkdev(MAJOR_NR,"md");
6037 unregister_blkdev(mdp_major, "mdp");
6038 unregister_reboot_notifier(&md_notifier);
6039 unregister_sysctl_table(raid_table_header);
6040 remove_proc_entry("mdstat", NULL);
6041 ITERATE_MDDEV(mddev,tmp) {
6042 struct gendisk *disk = mddev->gendisk;
6043 if (!disk)
6044 continue;
6045 export_array(mddev);
6046 del_gendisk(disk);
6047 put_disk(disk);
6048 mddev->gendisk = NULL;
6049 mddev_put(mddev);
6050 }
6051}
6052
Dan Williams685784a2007-07-09 11:56:42 -07006053subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054module_exit(md_exit)
6055
NeilBrownf91de922005-11-08 21:39:36 -08006056static int get_ro(char *buffer, struct kernel_param *kp)
6057{
6058 return sprintf(buffer, "%d", start_readonly);
6059}
6060static int set_ro(const char *val, struct kernel_param *kp)
6061{
6062 char *e;
6063 int num = simple_strtoul(val, &e, 10);
6064 if (*val && (*e == '\0' || *e == '\n')) {
6065 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006066 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006067 }
6068 return -EINVAL;
6069}
6070
NeilBrown80ca3a42006-07-10 04:44:18 -07006071module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6072module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006073
NeilBrownf91de922005-11-08 21:39:36 -08006074
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075EXPORT_SYMBOL(register_md_personality);
6076EXPORT_SYMBOL(unregister_md_personality);
6077EXPORT_SYMBOL(md_error);
6078EXPORT_SYMBOL(md_done_sync);
6079EXPORT_SYMBOL(md_write_start);
6080EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081EXPORT_SYMBOL(md_register_thread);
6082EXPORT_SYMBOL(md_unregister_thread);
6083EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084EXPORT_SYMBOL(md_check_recovery);
6085MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006086MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006087MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);