blob: 65ddc887dfd72dc97a2ace8bb4c8bbf93c8e5f94 [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
214static int md_fail_request (request_queue_t *q, struct bio *bio)
215{
216 bio_io_error(bio, bio->bi_size);
217 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);
NeilBrowneae17012005-11-08 21:39:23 -0800234 kobject_unregister(&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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 new->queue = blk_alloc_queue(GFP_KERNEL);
280 if (!new->queue) {
281 kfree(new);
282 return NULL;
283 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800284 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 blk_queue_make_request(new->queue, md_fail_request);
287
288 goto retry;
289}
290
291static inline int mddev_lock(mddev_t * mddev)
292{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800293 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static inline int mddev_trylock(mddev_t * mddev)
297{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800298 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static inline void mddev_unlock(mddev_t * mddev)
302{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800303 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
NeilBrown005eca52005-08-22 13:11:08 -0700305 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
NeilBrown2989ddb2006-01-06 00:20:43 -0800308static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 mdk_rdev_t * rdev;
311 struct list_head *tmp;
312
313 ITERATE_RDEV(mddev,rdev,tmp) {
314 if (rdev->desc_nr == nr)
315 return rdev;
316 }
317 return NULL;
318}
319
320static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
321{
322 struct list_head *tmp;
323 mdk_rdev_t *rdev;
324
325 ITERATE_RDEV(mddev,rdev,tmp) {
326 if (rdev->bdev->bd_dev == dev)
327 return rdev;
328 }
329 return NULL;
330}
331
NeilBrownd9d166c2006-01-06 00:20:51 -0800332static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800333{
334 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800335 list_for_each_entry(pers, &pers_list, list) {
336 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800337 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800338 if (strcmp(pers->name, clevel)==0)
339 return pers;
340 }
NeilBrown2604b702006-01-06 00:20:36 -0800341 return NULL;
342}
343
Jesper Juhl77933d72005-07-27 11:46:09 -0700344static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
347 return MD_NEW_SIZE_BLOCKS(size);
348}
349
350static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
351{
352 sector_t size;
353
354 size = rdev->sb_offset;
355
356 if (chunk_size)
357 size &= ~((sector_t)chunk_size/1024 - 1);
358 return size;
359}
360
361static int alloc_disk_sb(mdk_rdev_t * rdev)
362{
363 if (rdev->sb_page)
364 MD_BUG();
365
366 rdev->sb_page = alloc_page(GFP_KERNEL);
367 if (!rdev->sb_page) {
368 printk(KERN_ALERT "md: out of memory.\n");
369 return -EINVAL;
370 }
371
372 return 0;
373}
374
375static void free_disk_sb(mdk_rdev_t * rdev)
376{
377 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800378 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rdev->sb_loaded = 0;
380 rdev->sb_page = NULL;
381 rdev->sb_offset = 0;
382 rdev->size = 0;
383 }
384}
385
386
NeilBrown7bfa19f2005-06-21 17:17:28 -0700387static int super_written(struct bio *bio, unsigned int bytes_done, int error)
388{
389 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800390 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700391 if (bio->bi_size)
392 return 1;
393
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700394 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
395 printk("md: super_written gets error=%d, uptodate=%d\n",
396 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
397 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800398 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700399 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700400
NeilBrowna9701a32005-11-08 21:39:34 -0800401 if (atomic_dec_and_test(&mddev->pending_writes))
402 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700403 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700404 return 0;
405}
406
NeilBrowna9701a32005-11-08 21:39:34 -0800407static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
408{
409 struct bio *bio2 = bio->bi_private;
410 mdk_rdev_t *rdev = bio2->bi_private;
411 mddev_t *mddev = rdev->mddev;
412 if (bio->bi_size)
413 return 1;
414
415 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
416 error == -EOPNOTSUPP) {
417 unsigned long flags;
418 /* barriers don't appear to be supported :-( */
419 set_bit(BarriersNotsupp, &rdev->flags);
420 mddev->barriers_work = 0;
421 spin_lock_irqsave(&mddev->write_lock, flags);
422 bio2->bi_next = mddev->biolist;
423 mddev->biolist = bio2;
424 spin_unlock_irqrestore(&mddev->write_lock, flags);
425 wake_up(&mddev->sb_wait);
426 bio_put(bio);
427 return 0;
428 }
429 bio_put(bio2);
430 bio->bi_private = rdev;
431 return super_written(bio, bytes_done, error);
432}
433
NeilBrown7bfa19f2005-06-21 17:17:28 -0700434void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
435 sector_t sector, int size, struct page *page)
436{
437 /* write first size bytes of page to sector of rdev
438 * Increment mddev->pending_writes before returning
439 * and decrement it on completion, waking up sb_wait
440 * if zero is reached.
441 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800442 *
443 * As we might need to resubmit the request if BIO_RW_BARRIER
444 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700445 */
446 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800447 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700448
449 bio->bi_bdev = rdev->bdev;
450 bio->bi_sector = sector;
451 bio_add_page(bio, page, size, 0);
452 bio->bi_private = rdev;
453 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800454 bio->bi_rw = rw;
455
NeilBrown7bfa19f2005-06-21 17:17:28 -0700456 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800457 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
458 struct bio *rbio;
459 rw |= (1<<BIO_RW_BARRIER);
460 rbio = bio_clone(bio, GFP_NOIO);
461 rbio->bi_private = bio;
462 rbio->bi_end_io = super_written_barrier;
463 submit_bio(rw, rbio);
464 } else
465 submit_bio(rw, bio);
466}
467
468void md_super_wait(mddev_t *mddev)
469{
470 /* wait for all superblock writes that were scheduled to complete.
471 * if any had to be retried (due to BARRIER problems), retry them
472 */
473 DEFINE_WAIT(wq);
474 for(;;) {
475 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
476 if (atomic_read(&mddev->pending_writes)==0)
477 break;
478 while (mddev->biolist) {
479 struct bio *bio;
480 spin_lock_irq(&mddev->write_lock);
481 bio = mddev->biolist;
482 mddev->biolist = bio->bi_next ;
483 bio->bi_next = NULL;
484 spin_unlock_irq(&mddev->write_lock);
485 submit_bio(bio->bi_rw, bio);
486 }
487 schedule();
488 }
489 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
493{
494 if (bio->bi_size)
495 return 1;
496
497 complete((struct completion*)bio->bi_private);
498 return 0;
499}
500
NeilBrowna654b9d82005-06-21 17:17:27 -0700501int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct page *page, int rw)
503{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700504 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct completion event;
506 int ret;
507
508 rw |= (1 << BIO_RW_SYNC);
509
510 bio->bi_bdev = bdev;
511 bio->bi_sector = sector;
512 bio_add_page(bio, page, size, 0);
513 init_completion(&event);
514 bio->bi_private = &event;
515 bio->bi_end_io = bi_complete;
516 submit_bio(rw, bio);
517 wait_for_completion(&event);
518
519 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
520 bio_put(bio);
521 return ret;
522}
NeilBrowna8745db2006-01-06 00:20:34 -0800523EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
NeilBrown0002b272005-09-09 16:23:53 -0700525static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 char b[BDEVNAME_SIZE];
528 if (!rdev->sb_page) {
529 MD_BUG();
530 return -EINVAL;
531 }
532 if (rdev->sb_loaded)
533 return 0;
534
535
NeilBrown0002b272005-09-09 16:23:53 -0700536 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto fail;
538 rdev->sb_loaded = 1;
539 return 0;
540
541fail:
542 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
543 bdevname(rdev->bdev,b));
544 return -EINVAL;
545}
546
547static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
548{
549 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
550 (sb1->set_uuid1 == sb2->set_uuid1) &&
551 (sb1->set_uuid2 == sb2->set_uuid2) &&
552 (sb1->set_uuid3 == sb2->set_uuid3))
553
554 return 1;
555
556 return 0;
557}
558
559
560static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
561{
562 int ret;
563 mdp_super_t *tmp1, *tmp2;
564
565 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
566 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
567
568 if (!tmp1 || !tmp2) {
569 ret = 0;
570 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
571 goto abort;
572 }
573
574 *tmp1 = *sb1;
575 *tmp2 = *sb2;
576
577 /*
578 * nr_disks is not constant
579 */
580 tmp1->nr_disks = 0;
581 tmp2->nr_disks = 0;
582
583 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
584 ret = 0;
585 else
586 ret = 1;
587
588abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700589 kfree(tmp1);
590 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return ret;
592}
593
NeilBrown4d167f02007-05-09 02:35:37 -0700594
595static u32 md_csum_fold(u32 csum)
596{
597 csum = (csum & 0xffff) + (csum >> 16);
598 return (csum & 0xffff) + (csum >> 16);
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static unsigned int calc_sb_csum(mdp_super_t * sb)
602{
NeilBrown4d167f02007-05-09 02:35:37 -0700603 u64 newcsum = 0;
604 u32 *sb32 = (u32*)sb;
605 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unsigned int disk_csum, csum;
607
608 disk_csum = sb->sb_csum;
609 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700610
611 for (i = 0; i < MD_SB_BYTES/4 ; i++)
612 newcsum += sb32[i];
613 csum = (newcsum & 0xffffffff) + (newcsum>>32);
614
615
616#ifdef CONFIG_ALPHA
617 /* This used to use csum_partial, which was wrong for several
618 * reasons including that different results are returned on
619 * different architectures. It isn't critical that we get exactly
620 * the same return value as before (we always csum_fold before
621 * testing, and that removes any differences). However as we
622 * know that csum_partial always returned a 16bit value on
623 * alphas, do a fold to maximise conformity to previous behaviour.
624 */
625 sb->sb_csum = md_csum_fold(disk_csum);
626#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return csum;
630}
631
632
633/*
634 * Handle superblock details.
635 * We want to be able to handle multiple superblock formats
636 * so we have a common interface to them all, and an array of
637 * different handlers.
638 * We rely on user-space to write the initial superblock, and support
639 * reading and updating of superblocks.
640 * Interface methods are:
641 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
642 * loads and validates a superblock on dev.
643 * if refdev != NULL, compare superblocks on both devices
644 * Return:
645 * 0 - dev has a superblock that is compatible with refdev
646 * 1 - dev has a superblock that is compatible and newer than refdev
647 * so dev should be used as the refdev in future
648 * -EINVAL superblock incompatible or invalid
649 * -othererror e.g. -EIO
650 *
651 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
652 * Verify that dev is acceptable into mddev.
653 * The first time, mddev->raid_disks will be 0, and data from
654 * dev should be merged in. Subsequent calls check that dev
655 * is new enough. Return 0 or -EINVAL
656 *
657 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
658 * Update the superblock for rdev with data in mddev
659 * This does not write to disc.
660 *
661 */
662
663struct super_type {
664 char *name;
665 struct module *owner;
666 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
667 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
668 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
669};
670
671/*
672 * load_super for 0.90.0
673 */
674static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
675{
676 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
677 mdp_super_t *sb;
678 int ret;
679 sector_t sb_offset;
680
681 /*
682 * Calculate the position of the superblock,
683 * it's at the end of the disk.
684 *
685 * It also happens to be a multiple of 4Kb.
686 */
687 sb_offset = calc_dev_sboffset(rdev->bdev);
688 rdev->sb_offset = sb_offset;
689
NeilBrown0002b272005-09-09 16:23:53 -0700690 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (ret) return ret;
692
693 ret = -EINVAL;
694
695 bdevname(rdev->bdev, b);
696 sb = (mdp_super_t*)page_address(rdev->sb_page);
697
698 if (sb->md_magic != MD_SB_MAGIC) {
699 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
700 b);
701 goto abort;
702 }
703
704 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800705 sb->minor_version < 90 ||
706 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
708 sb->major_version, sb->minor_version,
709 b);
710 goto abort;
711 }
712
713 if (sb->raid_disks <= 0)
714 goto abort;
715
NeilBrown4d167f02007-05-09 02:35:37 -0700716 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
718 b);
719 goto abort;
720 }
721
722 rdev->preferred_minor = sb->md_minor;
723 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700724 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
NeilBrowne11e93f2007-05-09 02:35:36 -0700726 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
727 if (sb->level != 1 && sb->level != 4
728 && sb->level != 5 && sb->level != 6
729 && sb->level != 10) {
730 /* FIXME use a better test */
731 printk(KERN_WARNING
732 "md: bitmaps not supported for this level.\n");
733 goto abort;
734 }
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (sb->level == LEVEL_MULTIPATH)
738 rdev->desc_nr = -1;
739 else
740 rdev->desc_nr = sb->this_disk.number;
741
742 if (refdev == 0)
743 ret = 1;
744 else {
745 __u64 ev1, ev2;
746 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
747 if (!uuid_equal(refsb, sb)) {
748 printk(KERN_WARNING "md: %s has different UUID to %s\n",
749 b, bdevname(refdev->bdev,b2));
750 goto abort;
751 }
752 if (!sb_equal(refsb, sb)) {
753 printk(KERN_WARNING "md: %s has same UUID"
754 " but different superblock to %s\n",
755 b, bdevname(refdev->bdev, b2));
756 goto abort;
757 }
758 ev1 = md_event(sb);
759 ev2 = md_event(refsb);
760 if (ev1 > ev2)
761 ret = 1;
762 else
763 ret = 0;
764 }
765 rdev->size = calc_dev_size(rdev, sb->chunk_size);
766
NeilBrown2bf071b2006-01-06 00:20:55 -0800767 if (rdev->size < sb->size && sb->level > 1)
768 /* "this cannot possibly happen" ... */
769 ret = -EINVAL;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 abort:
772 return ret;
773}
774
775/*
776 * validate_super for 0.90.0
777 */
778static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
779{
780 mdp_disk_t *desc;
781 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700782 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
NeilBrown41158c72005-06-21 17:17:25 -0700784 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800785 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (mddev->raid_disks == 0) {
787 mddev->major_version = 0;
788 mddev->minor_version = sb->minor_version;
789 mddev->patch_version = sb->patch_version;
790 mddev->persistent = ! sb->not_persistent;
791 mddev->chunk_size = sb->chunk_size;
792 mddev->ctime = sb->ctime;
793 mddev->utime = sb->utime;
794 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800795 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 mddev->layout = sb->layout;
797 mddev->raid_disks = sb->raid_disks;
798 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700799 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700800 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700801 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
NeilBrownf6705572006-03-27 01:18:11 -0800803 if (mddev->minor_version >= 91) {
804 mddev->reshape_position = sb->reshape_position;
805 mddev->delta_disks = sb->delta_disks;
806 mddev->new_level = sb->new_level;
807 mddev->new_layout = sb->new_layout;
808 mddev->new_chunk = sb->new_chunk;
809 } else {
810 mddev->reshape_position = MaxSector;
811 mddev->delta_disks = 0;
812 mddev->new_level = mddev->level;
813 mddev->new_layout = mddev->layout;
814 mddev->new_chunk = mddev->chunk_size;
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (sb->state & (1<<MD_SB_CLEAN))
818 mddev->recovery_cp = MaxSector;
819 else {
820 if (sb->events_hi == sb->cp_events_hi &&
821 sb->events_lo == sb->cp_events_lo) {
822 mddev->recovery_cp = sb->recovery_cp;
823 } else
824 mddev->recovery_cp = 0;
825 }
826
827 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
828 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
829 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
830 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
831
832 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700833
834 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700835 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700836 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700837
NeilBrown41158c72005-06-21 17:17:25 -0700838 } else if (mddev->pers == NULL) {
839 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ++ev1;
841 if (ev1 < mddev->events)
842 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700843 } else if (mddev->bitmap) {
844 /* if adding to array with a bitmap, then we can accept an
845 * older device ... but not too old.
846 */
NeilBrown41158c72005-06-21 17:17:25 -0700847 if (ev1 < mddev->bitmap->events_cleared)
848 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700849 } else {
850 if (ev1 < mddev->events)
851 /* just a hot-add of a new device, leave raid_disk at -1 */
852 return 0;
853 }
NeilBrown41158c72005-06-21 17:17:25 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 desc = sb->disks + rdev->desc_nr;
857
858 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800859 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700860 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
861 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800862 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 rdev->raid_disk = desc->raid_disk;
864 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700865 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
866 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700867 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800868 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 0;
870}
871
872/*
873 * sync_super for 0.90.0
874 */
875static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
876{
877 mdp_super_t *sb;
878 struct list_head *tmp;
879 mdk_rdev_t *rdev2;
880 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 /* make rdev->sb match mddev data..
884 *
885 * 1/ zero out disks
886 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
887 * 3/ any empty disks < next_spare become removed
888 *
889 * disks[0] gets initialised to REMOVED because
890 * we cannot be sure from other fields if it has
891 * been initialised or not.
892 */
893 int i;
894 int active=0, working=0,failed=0,spare=0,nr_disks=0;
895
NeilBrown61181562005-09-09 16:24:02 -0700896 rdev->sb_size = MD_SB_BYTES;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 sb = (mdp_super_t*)page_address(rdev->sb_page);
899
900 memset(sb, 0, sizeof(*sb));
901
902 sb->md_magic = MD_SB_MAGIC;
903 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 sb->patch_version = mddev->patch_version;
905 sb->gvalid_words = 0; /* ignored */
906 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
907 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
908 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
909 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
910
911 sb->ctime = mddev->ctime;
912 sb->level = mddev->level;
913 sb->size = mddev->size;
914 sb->raid_disks = mddev->raid_disks;
915 sb->md_minor = mddev->md_minor;
916 sb->not_persistent = !mddev->persistent;
917 sb->utime = mddev->utime;
918 sb->state = 0;
919 sb->events_hi = (mddev->events>>32);
920 sb->events_lo = (u32)mddev->events;
921
NeilBrownf6705572006-03-27 01:18:11 -0800922 if (mddev->reshape_position == MaxSector)
923 sb->minor_version = 90;
924 else {
925 sb->minor_version = 91;
926 sb->reshape_position = mddev->reshape_position;
927 sb->new_level = mddev->new_level;
928 sb->delta_disks = mddev->delta_disks;
929 sb->new_layout = mddev->new_layout;
930 sb->new_chunk = mddev->new_chunk;
931 }
932 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (mddev->in_sync)
934 {
935 sb->recovery_cp = mddev->recovery_cp;
936 sb->cp_events_hi = (mddev->events>>32);
937 sb->cp_events_lo = (u32)mddev->events;
938 if (mddev->recovery_cp == MaxSector)
939 sb->state = (1<< MD_SB_CLEAN);
940 } else
941 sb->recovery_cp = 0;
942
943 sb->layout = mddev->layout;
944 sb->chunk_size = mddev->chunk_size;
945
NeilBrowna654b9d82005-06-21 17:17:27 -0700946 if (mddev->bitmap && mddev->bitmap_file == NULL)
947 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 sb->disks[0].state = (1<<MD_DISK_REMOVED);
950 ITERATE_RDEV(mddev,rdev2,tmp) {
951 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800952 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800953 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
954 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800955 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800957 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800958 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 d = &sb->disks[rdev2->desc_nr];
960 nr_disks++;
961 d->number = rdev2->desc_nr;
962 d->major = MAJOR(rdev2->bdev->bd_dev);
963 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800964 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
965 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 d->raid_disk = rdev2->raid_disk;
967 else
968 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800969 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800971 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 d->state = (1<<MD_DISK_ACTIVE);
973 d->state |= (1<<MD_DISK_SYNC);
974 active++;
975 working++;
976 } else {
977 d->state = 0;
978 spare++;
979 working++;
980 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700981 if (test_bit(WriteMostly, &rdev2->flags))
982 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /* now set the "removed" and "faulty" bits on any missing devices */
985 for (i=0 ; i < mddev->raid_disks ; i++) {
986 mdp_disk_t *d = &sb->disks[i];
987 if (d->state == 0 && d->number == 0) {
988 d->number = i;
989 d->raid_disk = i;
990 d->state = (1<<MD_DISK_REMOVED);
991 d->state |= (1<<MD_DISK_FAULTY);
992 failed++;
993 }
994 }
995 sb->nr_disks = nr_disks;
996 sb->active_disks = active;
997 sb->working_disks = working;
998 sb->failed_disks = failed;
999 sb->spare_disks = spare;
1000
1001 sb->this_disk = sb->disks[rdev->desc_nr];
1002 sb->sb_csum = calc_sb_csum(sb);
1003}
1004
1005/*
1006 * version 1 superblock
1007 */
1008
NeilBrown1c05b4b2006-10-21 10:24:08 -07001009static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001011 __le32 disk_csum;
1012 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned long long newcsum;
1014 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001015 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int i;
1017
1018 disk_csum = sb->sb_csum;
1019 sb->sb_csum = 0;
1020 newcsum = 0;
1021 for (i=0; size>=4; size -= 4 )
1022 newcsum += le32_to_cpu(*isuper++);
1023
1024 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001025 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1028 sb->sb_csum = disk_csum;
1029 return cpu_to_le32(csum);
1030}
1031
1032static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1033{
1034 struct mdp_superblock_1 *sb;
1035 int ret;
1036 sector_t sb_offset;
1037 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001038 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /*
1041 * Calculate the position of the superblock.
1042 * It is always aligned to a 4K boundary and
1043 * depeding on minor_version, it can be:
1044 * 0: At least 8K, but less than 12K, from end of device
1045 * 1: At start of device
1046 * 2: 4K from start of device.
1047 */
1048 switch(minor_version) {
1049 case 0:
1050 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1051 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001052 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* convert from sectors to K */
1054 sb_offset /= 2;
1055 break;
1056 case 1:
1057 sb_offset = 0;
1058 break;
1059 case 2:
1060 sb_offset = 4;
1061 break;
1062 default:
1063 return -EINVAL;
1064 }
1065 rdev->sb_offset = sb_offset;
1066
NeilBrown0002b272005-09-09 16:23:53 -07001067 /* superblock is rarely larger than 1K, but it can be larger,
1068 * and it is safe to read 4k, so we do that
1069 */
1070 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (ret) return ret;
1072
1073
1074 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1075
1076 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1077 sb->major_version != cpu_to_le32(1) ||
1078 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1079 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001080 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -EINVAL;
1082
1083 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1084 printk("md: invalid superblock checksum on %s\n",
1085 bdevname(rdev->bdev,b));
1086 return -EINVAL;
1087 }
1088 if (le64_to_cpu(sb->data_size) < 10) {
1089 printk("md: data_size too small on %s\n",
1090 bdevname(rdev->bdev,b));
1091 return -EINVAL;
1092 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001093 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1094 if (sb->level != cpu_to_le32(1) &&
1095 sb->level != cpu_to_le32(4) &&
1096 sb->level != cpu_to_le32(5) &&
1097 sb->level != cpu_to_le32(6) &&
1098 sb->level != cpu_to_le32(10)) {
1099 printk(KERN_WARNING
1100 "md: bitmaps not supported for this level.\n");
1101 return -EINVAL;
1102 }
1103 }
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 rdev->preferred_minor = 0xffff;
1106 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001107 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
NeilBrown0002b272005-09-09 16:23:53 -07001109 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001110 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001111 if (rdev->sb_size & bmask)
1112 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1113
NeilBrown31b65a02006-07-10 04:44:14 -07001114 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1115 rdev->desc_nr = -1;
1116 else
1117 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001120 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 else {
1122 __u64 ev1, ev2;
1123 struct mdp_superblock_1 *refsb =
1124 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1125
1126 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1127 sb->level != refsb->level ||
1128 sb->layout != refsb->layout ||
1129 sb->chunksize != refsb->chunksize) {
1130 printk(KERN_WARNING "md: %s has strangely different"
1131 " superblock to %s\n",
1132 bdevname(rdev->bdev,b),
1133 bdevname(refdev->bdev,b2));
1134 return -EINVAL;
1135 }
1136 ev1 = le64_to_cpu(sb->events);
1137 ev2 = le64_to_cpu(refsb->events);
1138
1139 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001140 ret = 1;
1141 else
1142 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 if (minor_version)
1145 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1146 else
1147 rdev->size = rdev->sb_offset;
1148 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1149 return -EINVAL;
1150 rdev->size = le64_to_cpu(sb->data_size)/2;
1151 if (le32_to_cpu(sb->chunksize))
1152 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001153
NeilBrown1c05b4b2006-10-21 10:24:08 -07001154 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001155 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001156 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1160{
1161 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001162 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
NeilBrown41158c72005-06-21 17:17:25 -07001164 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001165 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (mddev->raid_disks == 0) {
1167 mddev->major_version = 1;
1168 mddev->patch_version = 0;
1169 mddev->persistent = 1;
1170 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);
1395 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1396 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001397 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1398 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001401 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001402
NeilBrown9c791972005-11-08 21:39:28 -08001403 rdev->kobj.parent = &mddev->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001404 if ((err = kobject_add(&rdev->kobj)))
1405 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001406
NeilBrownf637b9f2005-11-08 21:39:37 -08001407 if (rdev->bdev->bd_part)
1408 ko = &rdev->bdev->bd_part->kobj;
1409 else
1410 ko = &rdev->bdev->bd_disk->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001411 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1412 kobject_del(&rdev->kobj);
1413 goto fail;
1414 }
1415 list_add(&rdev->same_set, &mddev->disks);
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001416 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001418
1419 fail:
1420 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1421 b, mdname(mddev));
1422 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
NeilBrown5792a282007-04-04 19:08:18 -07001425static void delayed_delete(struct work_struct *ws)
1426{
1427 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1428 kobject_del(&rdev->kobj);
1429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1432{
1433 char b[BDEVNAME_SIZE];
1434 if (!rdev->mddev) {
1435 MD_BUG();
1436 return;
1437 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001438 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 list_del_init(&rdev->same_set);
1440 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1441 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001442 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001443
1444 /* We need to delay this, otherwise we can deadlock when
1445 * writing to 'remove' to "dev/state"
1446 */
1447 INIT_WORK(&rdev->del_work, delayed_delete);
1448 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
1451/*
1452 * prevent the device from being mounted, repartitioned or
1453 * otherwise reused by a RAID array (or any other kernel
1454 * subsystem), by bd_claiming the device.
1455 */
1456static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1457{
1458 int err = 0;
1459 struct block_device *bdev;
1460 char b[BDEVNAME_SIZE];
1461
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001462 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 if (IS_ERR(bdev)) {
1464 printk(KERN_ERR "md: could not open %s.\n",
1465 __bdevname(dev, b));
1466 return PTR_ERR(bdev);
1467 }
1468 err = bd_claim(bdev, rdev);
1469 if (err) {
1470 printk(KERN_ERR "md: could not bd_claim %s.\n",
1471 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001472 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return err;
1474 }
1475 rdev->bdev = bdev;
1476 return err;
1477}
1478
1479static void unlock_rdev(mdk_rdev_t *rdev)
1480{
1481 struct block_device *bdev = rdev->bdev;
1482 rdev->bdev = NULL;
1483 if (!bdev)
1484 MD_BUG();
1485 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001486 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
1489void md_autodetect_dev(dev_t dev);
1490
1491static void export_rdev(mdk_rdev_t * rdev)
1492{
1493 char b[BDEVNAME_SIZE];
1494 printk(KERN_INFO "md: export_rdev(%s)\n",
1495 bdevname(rdev->bdev,b));
1496 if (rdev->mddev)
1497 MD_BUG();
1498 free_disk_sb(rdev);
1499 list_del_init(&rdev->same_set);
1500#ifndef MODULE
1501 md_autodetect_dev(rdev->bdev->bd_dev);
1502#endif
1503 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001504 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
1506
1507static void kick_rdev_from_array(mdk_rdev_t * rdev)
1508{
1509 unbind_rdev_from_array(rdev);
1510 export_rdev(rdev);
1511}
1512
1513static void export_array(mddev_t *mddev)
1514{
1515 struct list_head *tmp;
1516 mdk_rdev_t *rdev;
1517
1518 ITERATE_RDEV(mddev,rdev,tmp) {
1519 if (!rdev->mddev) {
1520 MD_BUG();
1521 continue;
1522 }
1523 kick_rdev_from_array(rdev);
1524 }
1525 if (!list_empty(&mddev->disks))
1526 MD_BUG();
1527 mddev->raid_disks = 0;
1528 mddev->major_version = 0;
1529}
1530
1531static void print_desc(mdp_disk_t *desc)
1532{
1533 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1534 desc->major,desc->minor,desc->raid_disk,desc->state);
1535}
1536
1537static void print_sb(mdp_super_t *sb)
1538{
1539 int i;
1540
1541 printk(KERN_INFO
1542 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1543 sb->major_version, sb->minor_version, sb->patch_version,
1544 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1545 sb->ctime);
1546 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1547 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1548 sb->md_minor, sb->layout, sb->chunk_size);
1549 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1550 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1551 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1552 sb->failed_disks, sb->spare_disks,
1553 sb->sb_csum, (unsigned long)sb->events_lo);
1554
1555 printk(KERN_INFO);
1556 for (i = 0; i < MD_SB_DISKS; i++) {
1557 mdp_disk_t *desc;
1558
1559 desc = sb->disks + i;
1560 if (desc->number || desc->major || desc->minor ||
1561 desc->raid_disk || (desc->state && (desc->state != 4))) {
1562 printk(" D %2d: ", i);
1563 print_desc(desc);
1564 }
1565 }
1566 printk(KERN_INFO "md: THIS: ");
1567 print_desc(&sb->this_disk);
1568
1569}
1570
1571static void print_rdev(mdk_rdev_t *rdev)
1572{
1573 char b[BDEVNAME_SIZE];
1574 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1575 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001576 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1577 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 if (rdev->sb_loaded) {
1579 printk(KERN_INFO "md: rdev superblock:\n");
1580 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1581 } else
1582 printk(KERN_INFO "md: no rdev superblock!\n");
1583}
1584
Adrian Bunk5e563412006-06-26 00:27:42 -07001585static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
1587 struct list_head *tmp, *tmp2;
1588 mdk_rdev_t *rdev;
1589 mddev_t *mddev;
1590 char b[BDEVNAME_SIZE];
1591
1592 printk("\n");
1593 printk("md: **********************************\n");
1594 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1595 printk("md: **********************************\n");
1596 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
NeilBrown32a76272005-06-21 17:17:14 -07001598 if (mddev->bitmap)
1599 bitmap_print_sb(mddev->bitmap);
1600 else
1601 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 ITERATE_RDEV(mddev,rdev,tmp2)
1603 printk("<%s>", bdevname(rdev->bdev,b));
1604 printk("\n");
1605
1606 ITERATE_RDEV(mddev,rdev,tmp2)
1607 print_rdev(rdev);
1608 }
1609 printk("md: **********************************\n");
1610 printk("\n");
1611}
1612
1613
NeilBrown42543762006-06-26 00:27:57 -07001614static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
NeilBrown42543762006-06-26 00:27:57 -07001616 /* Update each superblock (in-memory image), but
1617 * if we are allowed to, skip spares which already
1618 * have the right event counter, or have one earlier
1619 * (which would mean they aren't being marked as dirty
1620 * with the rest of the array)
1621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 mdk_rdev_t *rdev;
1623 struct list_head *tmp;
1624
1625 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrown42543762006-06-26 00:27:57 -07001626 if (rdev->sb_events == mddev->events ||
1627 (nospares &&
1628 rdev->raid_disk < 0 &&
1629 (rdev->sb_events&1)==0 &&
1630 rdev->sb_events+1 == mddev->events)) {
1631 /* Don't update this superblock */
1632 rdev->sb_loaded = 2;
1633 } else {
1634 super_types[mddev->major_version].
1635 sync_super(mddev, rdev);
1636 rdev->sb_loaded = 1;
1637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639}
1640
NeilBrown850b2b42006-10-03 01:15:46 -07001641static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 struct list_head *tmp;
1644 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001645 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001646 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001649 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001650
NeilBrown850b2b42006-10-03 01:15:46 -07001651 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1652 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1653 force_change = 1;
1654 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1655 /* just a clean<-> dirty transition, possibly leave spares alone,
1656 * though if events isn't the right even/odd, we will have to do
1657 * spares after all
1658 */
1659 nospares = 1;
1660 if (force_change)
1661 nospares = 0;
1662 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001663 /* If the array is degraded, then skipping spares is both
1664 * dangerous and fairly pointless.
1665 * Dangerous because a device that was removed from the array
1666 * might have a event_count that still looks up-to-date,
1667 * so it can be re-added without a resync.
1668 * Pointless because if there are any spares to skip,
1669 * then a recovery will happen and soon that array won't
1670 * be degraded any more and the spare can go back to sleep then.
1671 */
NeilBrown850b2b42006-10-03 01:15:46 -07001672 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001673
NeilBrown06d91a52005-06-21 17:17:12 -07001674 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001676
1677 /* If this is just a dirty<->clean transition, and the array is clean
1678 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001679 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001680 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001681 && (mddev->events & 1)
1682 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001683 mddev->events--;
1684 else {
1685 /* otherwise we have to go forward and ... */
1686 mddev->events ++;
1687 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1688 /* .. if the array isn't clean, insist on an odd 'events' */
1689 if ((mddev->events&1)==0) {
1690 mddev->events++;
1691 nospares = 0;
1692 }
1693 } else {
1694 /* otherwise insist on an even 'events' (for clean states) */
1695 if ((mddev->events&1)) {
1696 mddev->events++;
1697 nospares = 0;
1698 }
1699 }
1700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
1702 if (!mddev->events) {
1703 /*
1704 * oops, this 64-bit counter should never wrap.
1705 * Either we are in around ~1 trillion A.C., assuming
1706 * 1 reboot per second, or we have a bug:
1707 */
1708 MD_BUG();
1709 mddev->events --;
1710 }
NeilBrown42543762006-06-26 00:27:57 -07001711 sync_sbs(mddev, nospares);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 /*
1714 * do not write anything to disk if using
1715 * nonpersistent superblocks
1716 */
NeilBrown06d91a52005-06-21 17:17:12 -07001717 if (!mddev->persistent) {
NeilBrown850b2b42006-10-03 01:15:46 -07001718 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
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 }
NeilBrowna9701a32005-11-08 21:39:34 -08001723 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725 dprintk(KERN_INFO
1726 "md: updating %s RAID superblock on device (in sync %d)\n",
1727 mdname(mddev),mddev->in_sync);
1728
NeilBrown4ad13662007-07-17 04:06:13 -07001729 bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 ITERATE_RDEV(mddev,rdev,tmp) {
1731 char b[BDEVNAME_SIZE];
1732 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001733 if (rdev->sb_loaded != 1)
1734 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001735 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 dprintk("(skipping faulty ");
1737
1738 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001739 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001740 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001741 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001742 rdev->sb_page);
1743 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1744 bdevname(rdev->bdev,b),
1745 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001746 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 } else
1749 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001750 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /* only need to write one superblock... */
1752 break;
1753 }
NeilBrowna9701a32005-11-08 21:39:34 -08001754 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001755 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001756
NeilBrowna9701a32005-11-08 21:39:34 -08001757 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001758 if (mddev->in_sync != sync_req ||
1759 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001760 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001761 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001762 goto repeat;
1763 }
NeilBrown850b2b42006-10-03 01:15:46 -07001764 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001765 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001766 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769
NeilBrownbce74da2006-01-06 00:20:41 -08001770/* words written to sysfs files may, or my not, be \n terminated.
1771 * We want to accept with case. For this we use cmd_match.
1772 */
1773static int cmd_match(const char *cmd, const char *str)
1774{
1775 /* See if cmd, written into a sysfs file, matches
1776 * str. They must either be the same, or cmd can
1777 * have a trailing newline
1778 */
1779 while (*cmd && *str && *cmd == *str) {
1780 cmd++;
1781 str++;
1782 }
1783 if (*cmd == '\n')
1784 cmd++;
1785 if (*str || *cmd)
1786 return 0;
1787 return 1;
1788}
1789
NeilBrown86e6ffd2005-11-08 21:39:24 -08001790struct rdev_sysfs_entry {
1791 struct attribute attr;
1792 ssize_t (*show)(mdk_rdev_t *, char *);
1793 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1794};
1795
1796static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001797state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001798{
1799 char *sep = "";
1800 int len=0;
1801
NeilBrownb2d444d2005-11-08 21:39:31 -08001802 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001803 len+= sprintf(page+len, "%sfaulty",sep);
1804 sep = ",";
1805 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001806 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001807 len += sprintf(page+len, "%sin_sync",sep);
1808 sep = ",";
1809 }
NeilBrownf6556752006-06-26 00:28:01 -07001810 if (test_bit(WriteMostly, &rdev->flags)) {
1811 len += sprintf(page+len, "%swrite_mostly",sep);
1812 sep = ",";
1813 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001814 if (!test_bit(Faulty, &rdev->flags) &&
1815 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001816 len += sprintf(page+len, "%sspare", sep);
1817 sep = ",";
1818 }
1819 return len+sprintf(page+len, "\n");
1820}
1821
NeilBrown45dc2de2006-06-26 00:27:58 -07001822static ssize_t
1823state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1824{
1825 /* can write
1826 * faulty - simulates and error
1827 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001828 * writemostly - sets write_mostly
1829 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001830 */
1831 int err = -EINVAL;
1832 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1833 md_error(rdev->mddev, rdev);
1834 err = 0;
1835 } else if (cmd_match(buf, "remove")) {
1836 if (rdev->raid_disk >= 0)
1837 err = -EBUSY;
1838 else {
1839 mddev_t *mddev = rdev->mddev;
1840 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001841 if (mddev->pers)
1842 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001843 md_new_event(mddev);
1844 err = 0;
1845 }
NeilBrownf6556752006-06-26 00:28:01 -07001846 } else if (cmd_match(buf, "writemostly")) {
1847 set_bit(WriteMostly, &rdev->flags);
1848 err = 0;
1849 } else if (cmd_match(buf, "-writemostly")) {
1850 clear_bit(WriteMostly, &rdev->flags);
1851 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001852 }
1853 return err ? err : len;
1854}
NeilBrown80ca3a42006-07-10 04:44:18 -07001855static struct rdev_sysfs_entry rdev_state =
1856__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001857
1858static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001859super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001860{
1861 if (rdev->sb_loaded && rdev->sb_size) {
1862 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1863 return rdev->sb_size;
1864 } else
1865 return 0;
1866}
NeilBrown96de1e62005-11-08 21:39:39 -08001867static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1868
NeilBrown4dbcdc72006-01-06 00:20:52 -08001869static ssize_t
1870errors_show(mdk_rdev_t *rdev, char *page)
1871{
1872 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1873}
1874
1875static ssize_t
1876errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1877{
1878 char *e;
1879 unsigned long n = simple_strtoul(buf, &e, 10);
1880 if (*buf && (*e == 0 || *e == '\n')) {
1881 atomic_set(&rdev->corrected_errors, n);
1882 return len;
1883 }
1884 return -EINVAL;
1885}
1886static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001887__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001888
NeilBrown014236d2006-01-06 00:20:55 -08001889static ssize_t
1890slot_show(mdk_rdev_t *rdev, char *page)
1891{
1892 if (rdev->raid_disk < 0)
1893 return sprintf(page, "none\n");
1894 else
1895 return sprintf(page, "%d\n", rdev->raid_disk);
1896}
1897
1898static ssize_t
1899slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1900{
1901 char *e;
1902 int slot = simple_strtoul(buf, &e, 10);
1903 if (strncmp(buf, "none", 4)==0)
1904 slot = -1;
1905 else if (e==buf || (*e && *e!= '\n'))
1906 return -EINVAL;
1907 if (rdev->mddev->pers)
1908 /* Cannot set slot in active array (yet) */
1909 return -EBUSY;
1910 if (slot >= rdev->mddev->raid_disks)
1911 return -ENOSPC;
1912 rdev->raid_disk = slot;
1913 /* assume it is working */
1914 rdev->flags = 0;
1915 set_bit(In_sync, &rdev->flags);
1916 return len;
1917}
1918
1919
1920static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001921__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001922
NeilBrown93c8cad2006-01-06 00:20:56 -08001923static ssize_t
1924offset_show(mdk_rdev_t *rdev, char *page)
1925{
Andrew Morton6961ece2006-01-06 00:20:59 -08001926 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001927}
1928
1929static ssize_t
1930offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1931{
1932 char *e;
1933 unsigned long long offset = simple_strtoull(buf, &e, 10);
1934 if (e==buf || (*e && *e != '\n'))
1935 return -EINVAL;
1936 if (rdev->mddev->pers)
1937 return -EBUSY;
1938 rdev->data_offset = offset;
1939 return len;
1940}
1941
1942static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001943__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001944
NeilBrown83303b62006-01-06 00:21:06 -08001945static ssize_t
1946rdev_size_show(mdk_rdev_t *rdev, char *page)
1947{
1948 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1949}
1950
1951static ssize_t
1952rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1953{
1954 char *e;
1955 unsigned long long size = simple_strtoull(buf, &e, 10);
1956 if (e==buf || (*e && *e != '\n'))
1957 return -EINVAL;
1958 if (rdev->mddev->pers)
1959 return -EBUSY;
1960 rdev->size = size;
1961 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1962 rdev->mddev->size = size;
1963 return len;
1964}
1965
1966static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07001967__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08001968
NeilBrown86e6ffd2005-11-08 21:39:24 -08001969static struct attribute *rdev_default_attrs[] = {
1970 &rdev_state.attr,
1971 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001972 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001973 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001974 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001975 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001976 NULL,
1977};
1978static ssize_t
1979rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1980{
1981 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1982 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1983
1984 if (!entry->show)
1985 return -EIO;
1986 return entry->show(rdev, page);
1987}
1988
1989static ssize_t
1990rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1991 const char *page, size_t length)
1992{
1993 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1994 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1995
1996 if (!entry->store)
1997 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07001998 if (!capable(CAP_SYS_ADMIN))
1999 return -EACCES;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002000 return entry->store(rdev, page, length);
2001}
2002
2003static void rdev_free(struct kobject *ko)
2004{
2005 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2006 kfree(rdev);
2007}
2008static struct sysfs_ops rdev_sysfs_ops = {
2009 .show = rdev_attr_show,
2010 .store = rdev_attr_store,
2011};
2012static struct kobj_type rdev_ktype = {
2013 .release = rdev_free,
2014 .sysfs_ops = &rdev_sysfs_ops,
2015 .default_attrs = rdev_default_attrs,
2016};
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018/*
2019 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2020 *
2021 * mark the device faulty if:
2022 *
2023 * - the device is nonexistent (zero size)
2024 * - the device has no valid superblock
2025 *
2026 * a faulty rdev _never_ has rdev->sb set.
2027 */
2028static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2029{
2030 char b[BDEVNAME_SIZE];
2031 int err;
2032 mdk_rdev_t *rdev;
2033 sector_t size;
2034
NeilBrown9ffae0c2006-01-06 00:20:32 -08002035 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (!rdev) {
2037 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2038 return ERR_PTR(-ENOMEM);
2039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 if ((err = alloc_disk_sb(rdev)))
2042 goto abort_free;
2043
2044 err = lock_rdev(rdev, newdev);
2045 if (err)
2046 goto abort_free;
2047
NeilBrown86e6ffd2005-11-08 21:39:24 -08002048 rdev->kobj.parent = NULL;
2049 rdev->kobj.ktype = &rdev_ktype;
2050 kobject_init(&rdev->kobj);
2051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002053 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002054 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002055 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002057 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002059 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002060 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2063 if (!size) {
2064 printk(KERN_WARNING
2065 "md: %s has zero or unknown size, marking faulty!\n",
2066 bdevname(rdev->bdev,b));
2067 err = -EINVAL;
2068 goto abort_free;
2069 }
2070
2071 if (super_format >= 0) {
2072 err = super_types[super_format].
2073 load_super(rdev, NULL, super_minor);
2074 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002075 printk(KERN_WARNING
2076 "md: %s does not have a valid v%d.%d "
2077 "superblock, not importing!\n",
2078 bdevname(rdev->bdev,b),
2079 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 goto abort_free;
2081 }
2082 if (err < 0) {
2083 printk(KERN_WARNING
2084 "md: could not read %s's sb, not importing!\n",
2085 bdevname(rdev->bdev,b));
2086 goto abort_free;
2087 }
2088 }
2089 INIT_LIST_HEAD(&rdev->same_set);
2090
2091 return rdev;
2092
2093abort_free:
2094 if (rdev->sb_page) {
2095 if (rdev->bdev)
2096 unlock_rdev(rdev);
2097 free_disk_sb(rdev);
2098 }
2099 kfree(rdev);
2100 return ERR_PTR(err);
2101}
2102
2103/*
2104 * Check a full RAID array for plausibility
2105 */
2106
2107
NeilBrowna757e642005-04-16 15:26:42 -07002108static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
2110 int i;
2111 struct list_head *tmp;
2112 mdk_rdev_t *rdev, *freshest;
2113 char b[BDEVNAME_SIZE];
2114
2115 freshest = NULL;
2116 ITERATE_RDEV(mddev,rdev,tmp)
2117 switch (super_types[mddev->major_version].
2118 load_super(rdev, freshest, mddev->minor_version)) {
2119 case 1:
2120 freshest = rdev;
2121 break;
2122 case 0:
2123 break;
2124 default:
2125 printk( KERN_ERR \
2126 "md: fatal superblock inconsistency in %s"
2127 " -- removing from array\n",
2128 bdevname(rdev->bdev,b));
2129 kick_rdev_from_array(rdev);
2130 }
2131
2132
2133 super_types[mddev->major_version].
2134 validate_super(mddev, freshest);
2135
2136 i = 0;
2137 ITERATE_RDEV(mddev,rdev,tmp) {
2138 if (rdev != freshest)
2139 if (super_types[mddev->major_version].
2140 validate_super(mddev, rdev)) {
2141 printk(KERN_WARNING "md: kicking non-fresh %s"
2142 " from array!\n",
2143 bdevname(rdev->bdev,b));
2144 kick_rdev_from_array(rdev);
2145 continue;
2146 }
2147 if (mddev->level == LEVEL_MULTIPATH) {
2148 rdev->desc_nr = i++;
2149 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002150 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002151 } else if (rdev->raid_disk >= mddev->raid_disks) {
2152 rdev->raid_disk = -1;
2153 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155 }
2156
2157
2158
2159 if (mddev->recovery_cp != MaxSector &&
2160 mddev->level >= 1)
2161 printk(KERN_ERR "md: %s: raid array is not clean"
2162 " -- starting background reconstruction\n",
2163 mdname(mddev));
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
NeilBrowneae17012005-11-08 21:39:23 -08002167static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002168safe_delay_show(mddev_t *mddev, char *page)
2169{
2170 int msec = (mddev->safemode_delay*1000)/HZ;
2171 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2172}
2173static ssize_t
2174safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2175{
2176 int scale=1;
2177 int dot=0;
2178 int i;
2179 unsigned long msec;
2180 char buf[30];
2181 char *e;
2182 /* remove a period, and count digits after it */
2183 if (len >= sizeof(buf))
2184 return -EINVAL;
2185 strlcpy(buf, cbuf, len);
2186 buf[len] = 0;
2187 for (i=0; i<len; i++) {
2188 if (dot) {
2189 if (isdigit(buf[i])) {
2190 buf[i-1] = buf[i];
2191 scale *= 10;
2192 }
2193 buf[i] = 0;
2194 } else if (buf[i] == '.') {
2195 dot=1;
2196 buf[i] = 0;
2197 }
2198 }
2199 msec = simple_strtoul(buf, &e, 10);
2200 if (e == buf || (*e && *e != '\n'))
2201 return -EINVAL;
2202 msec = (msec * 1000) / scale;
2203 if (msec == 0)
2204 mddev->safemode_delay = 0;
2205 else {
2206 mddev->safemode_delay = (msec*HZ)/1000;
2207 if (mddev->safemode_delay == 0)
2208 mddev->safemode_delay = 1;
2209 }
2210 return len;
2211}
2212static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002213__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002214
2215static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002216level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002217{
NeilBrown2604b702006-01-06 00:20:36 -08002218 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002219 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002220 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002221 else if (mddev->clevel[0])
2222 return sprintf(page, "%s\n", mddev->clevel);
2223 else if (mddev->level != LEVEL_NONE)
2224 return sprintf(page, "%d\n", mddev->level);
2225 else
2226 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002227}
2228
NeilBrownd9d166c2006-01-06 00:20:51 -08002229static ssize_t
2230level_store(mddev_t *mddev, const char *buf, size_t len)
2231{
2232 int rv = len;
2233 if (mddev->pers)
2234 return -EBUSY;
2235 if (len == 0)
2236 return 0;
2237 if (len >= sizeof(mddev->clevel))
2238 return -ENOSPC;
2239 strncpy(mddev->clevel, buf, len);
2240 if (mddev->clevel[len-1] == '\n')
2241 len--;
2242 mddev->clevel[len] = 0;
2243 mddev->level = LEVEL_NONE;
2244 return rv;
2245}
2246
2247static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002248__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002249
NeilBrownd4dbd022006-06-26 00:27:59 -07002250
2251static ssize_t
2252layout_show(mddev_t *mddev, char *page)
2253{
2254 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002255 if (mddev->reshape_position != MaxSector &&
2256 mddev->layout != mddev->new_layout)
2257 return sprintf(page, "%d (%d)\n",
2258 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002259 return sprintf(page, "%d\n", mddev->layout);
2260}
2261
2262static ssize_t
2263layout_store(mddev_t *mddev, const char *buf, size_t len)
2264{
2265 char *e;
2266 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002267
2268 if (!*buf || (*e && *e != '\n'))
2269 return -EINVAL;
2270
NeilBrown08a02ec2007-05-09 02:35:38 -07002271 if (mddev->pers)
2272 return -EBUSY;
2273 if (mddev->reshape_position != MaxSector)
2274 mddev->new_layout = n;
2275 else
2276 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002277 return len;
2278}
2279static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002280__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002281
2282
NeilBrowneae17012005-11-08 21:39:23 -08002283static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002284raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002285{
NeilBrownbb636542005-11-08 21:39:45 -08002286 if (mddev->raid_disks == 0)
2287 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002288 if (mddev->reshape_position != MaxSector &&
2289 mddev->delta_disks != 0)
2290 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2291 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002292 return sprintf(page, "%d\n", mddev->raid_disks);
2293}
2294
NeilBrownda943b992006-01-06 00:20:54 -08002295static int update_raid_disks(mddev_t *mddev, int raid_disks);
2296
2297static ssize_t
2298raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2299{
NeilBrownda943b992006-01-06 00:20:54 -08002300 char *e;
2301 int rv = 0;
2302 unsigned long n = simple_strtoul(buf, &e, 10);
2303
2304 if (!*buf || (*e && *e != '\n'))
2305 return -EINVAL;
2306
2307 if (mddev->pers)
2308 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002309 else if (mddev->reshape_position != MaxSector) {
2310 int olddisks = mddev->raid_disks - mddev->delta_disks;
2311 mddev->delta_disks = n - olddisks;
2312 mddev->raid_disks = n;
2313 } else
NeilBrownda943b992006-01-06 00:20:54 -08002314 mddev->raid_disks = n;
2315 return rv ? rv : len;
2316}
2317static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002318__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002319
NeilBrown24dd4692005-11-08 21:39:26 -08002320static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002321chunk_size_show(mddev_t *mddev, char *page)
2322{
NeilBrown08a02ec2007-05-09 02:35:38 -07002323 if (mddev->reshape_position != MaxSector &&
2324 mddev->chunk_size != mddev->new_chunk)
2325 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2326 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002327 return sprintf(page, "%d\n", mddev->chunk_size);
2328}
2329
2330static ssize_t
2331chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2332{
2333 /* can only set chunk_size if array is not yet active */
2334 char *e;
2335 unsigned long n = simple_strtoul(buf, &e, 10);
2336
NeilBrown3b343802006-01-06 00:20:47 -08002337 if (!*buf || (*e && *e != '\n'))
2338 return -EINVAL;
2339
NeilBrown08a02ec2007-05-09 02:35:38 -07002340 if (mddev->pers)
2341 return -EBUSY;
2342 else if (mddev->reshape_position != MaxSector)
2343 mddev->new_chunk = n;
2344 else
2345 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002346 return len;
2347}
2348static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002349__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002350
NeilBrowna94213b2006-06-26 00:28:00 -07002351static ssize_t
2352resync_start_show(mddev_t *mddev, char *page)
2353{
2354 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2355}
2356
2357static ssize_t
2358resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2359{
2360 /* can only set chunk_size if array is not yet active */
2361 char *e;
2362 unsigned long long n = simple_strtoull(buf, &e, 10);
2363
2364 if (mddev->pers)
2365 return -EBUSY;
2366 if (!*buf || (*e && *e != '\n'))
2367 return -EINVAL;
2368
2369 mddev->recovery_cp = n;
2370 return len;
2371}
2372static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002373__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002374
NeilBrown9e653b62006-06-26 00:27:58 -07002375/*
2376 * The array state can be:
2377 *
2378 * clear
2379 * No devices, no size, no level
2380 * Equivalent to STOP_ARRAY ioctl
2381 * inactive
2382 * May have some settings, but array is not active
2383 * all IO results in error
2384 * When written, doesn't tear down array, but just stops it
2385 * suspended (not supported yet)
2386 * All IO requests will block. The array can be reconfigured.
2387 * Writing this, if accepted, will block until array is quiessent
2388 * readonly
2389 * no resync can happen. no superblocks get written.
2390 * write requests fail
2391 * read-auto
2392 * like readonly, but behaves like 'clean' on a write request.
2393 *
2394 * clean - no pending writes, but otherwise active.
2395 * When written to inactive array, starts without resync
2396 * If a write request arrives then
2397 * if metadata is known, mark 'dirty' and switch to 'active'.
2398 * if not known, block and switch to write-pending
2399 * If written to an active array that has pending writes, then fails.
2400 * active
2401 * fully active: IO and resync can be happening.
2402 * When written to inactive array, starts with resync
2403 *
2404 * write-pending
2405 * clean, but writes are blocked waiting for 'active' to be written.
2406 *
2407 * active-idle
2408 * like active, but no writes have been seen for a while (100msec).
2409 *
2410 */
2411enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2412 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002413static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002414 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2415 "write-pending", "active-idle", NULL };
2416
2417static int match_word(const char *word, char **list)
2418{
2419 int n;
2420 for (n=0; list[n]; n++)
2421 if (cmd_match(word, list[n]))
2422 break;
2423 return n;
2424}
2425
2426static ssize_t
2427array_state_show(mddev_t *mddev, char *page)
2428{
2429 enum array_state st = inactive;
2430
2431 if (mddev->pers)
2432 switch(mddev->ro) {
2433 case 1:
2434 st = readonly;
2435 break;
2436 case 2:
2437 st = read_auto;
2438 break;
2439 case 0:
2440 if (mddev->in_sync)
2441 st = clean;
2442 else if (mddev->safemode)
2443 st = active_idle;
2444 else
2445 st = active;
2446 }
2447 else {
2448 if (list_empty(&mddev->disks) &&
2449 mddev->raid_disks == 0 &&
2450 mddev->size == 0)
2451 st = clear;
2452 else
2453 st = inactive;
2454 }
2455 return sprintf(page, "%s\n", array_states[st]);
2456}
2457
2458static int do_md_stop(mddev_t * mddev, int ro);
2459static int do_md_run(mddev_t * mddev);
2460static int restart_array(mddev_t *mddev);
2461
2462static ssize_t
2463array_state_store(mddev_t *mddev, const char *buf, size_t len)
2464{
2465 int err = -EINVAL;
2466 enum array_state st = match_word(buf, array_states);
2467 switch(st) {
2468 case bad_word:
2469 break;
2470 case clear:
2471 /* stopping an active array */
2472 if (mddev->pers) {
2473 if (atomic_read(&mddev->active) > 1)
2474 return -EBUSY;
2475 err = do_md_stop(mddev, 0);
2476 }
2477 break;
2478 case inactive:
2479 /* stopping an active array */
2480 if (mddev->pers) {
2481 if (atomic_read(&mddev->active) > 1)
2482 return -EBUSY;
2483 err = do_md_stop(mddev, 2);
2484 }
2485 break;
2486 case suspended:
2487 break; /* not supported yet */
2488 case readonly:
2489 if (mddev->pers)
2490 err = do_md_stop(mddev, 1);
2491 else {
2492 mddev->ro = 1;
2493 err = do_md_run(mddev);
2494 }
2495 break;
2496 case read_auto:
2497 /* stopping an active array */
2498 if (mddev->pers) {
2499 err = do_md_stop(mddev, 1);
2500 if (err == 0)
2501 mddev->ro = 2; /* FIXME mark devices writable */
2502 } else {
2503 mddev->ro = 2;
2504 err = do_md_run(mddev);
2505 }
2506 break;
2507 case clean:
2508 if (mddev->pers) {
2509 restart_array(mddev);
2510 spin_lock_irq(&mddev->write_lock);
2511 if (atomic_read(&mddev->writes_pending) == 0) {
2512 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07002513 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002514 }
2515 spin_unlock_irq(&mddev->write_lock);
2516 } else {
2517 mddev->ro = 0;
2518 mddev->recovery_cp = MaxSector;
2519 err = do_md_run(mddev);
2520 }
2521 break;
2522 case active:
2523 if (mddev->pers) {
2524 restart_array(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002525 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002526 wake_up(&mddev->sb_wait);
2527 err = 0;
2528 } else {
2529 mddev->ro = 0;
2530 err = do_md_run(mddev);
2531 }
2532 break;
2533 case write_pending:
2534 case active_idle:
2535 /* these cannot be set */
2536 break;
2537 }
2538 if (err)
2539 return err;
2540 else
2541 return len;
2542}
NeilBrown80ca3a42006-07-10 04:44:18 -07002543static struct md_sysfs_entry md_array_state =
2544__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002545
NeilBrown6d7ff732006-01-06 00:21:16 -08002546static ssize_t
2547null_show(mddev_t *mddev, char *page)
2548{
2549 return -EINVAL;
2550}
2551
2552static ssize_t
2553new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2554{
2555 /* buf must be %d:%d\n? giving major and minor numbers */
2556 /* The new device is added to the array.
2557 * If the array has a persistent superblock, we read the
2558 * superblock to initialise info and check validity.
2559 * Otherwise, only checking done is that in bind_rdev_to_array,
2560 * which mainly checks size.
2561 */
2562 char *e;
2563 int major = simple_strtoul(buf, &e, 10);
2564 int minor;
2565 dev_t dev;
2566 mdk_rdev_t *rdev;
2567 int err;
2568
2569 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2570 return -EINVAL;
2571 minor = simple_strtoul(e+1, &e, 10);
2572 if (*e && *e != '\n')
2573 return -EINVAL;
2574 dev = MKDEV(major, minor);
2575 if (major != MAJOR(dev) ||
2576 minor != MINOR(dev))
2577 return -EOVERFLOW;
2578
2579
2580 if (mddev->persistent) {
2581 rdev = md_import_device(dev, mddev->major_version,
2582 mddev->minor_version);
2583 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2584 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2585 mdk_rdev_t, same_set);
2586 err = super_types[mddev->major_version]
2587 .load_super(rdev, rdev0, mddev->minor_version);
2588 if (err < 0)
2589 goto out;
2590 }
2591 } else
2592 rdev = md_import_device(dev, -1, -1);
2593
2594 if (IS_ERR(rdev))
2595 return PTR_ERR(rdev);
2596 err = bind_rdev_to_array(rdev, mddev);
2597 out:
2598 if (err)
2599 export_rdev(rdev);
2600 return err ? err : len;
2601}
2602
2603static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002604__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002605
2606static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002607bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2608{
2609 char *end;
2610 unsigned long chunk, end_chunk;
2611
2612 if (!mddev->bitmap)
2613 goto out;
2614 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2615 while (*buf) {
2616 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2617 if (buf == end) break;
2618 if (*end == '-') { /* range */
2619 buf = end + 1;
2620 end_chunk = simple_strtoul(buf, &end, 0);
2621 if (buf == end) break;
2622 }
2623 if (*end && !isspace(*end)) break;
2624 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2625 buf = end;
2626 while (isspace(*buf)) buf++;
2627 }
2628 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2629out:
2630 return len;
2631}
2632
2633static struct md_sysfs_entry md_bitmap =
2634__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2635
2636static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002637size_show(mddev_t *mddev, char *page)
2638{
2639 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2640}
2641
2642static int update_size(mddev_t *mddev, unsigned long size);
2643
2644static ssize_t
2645size_store(mddev_t *mddev, const char *buf, size_t len)
2646{
2647 /* If array is inactive, we can reduce the component size, but
2648 * not increase it (except from 0).
2649 * If array is active, we can try an on-line resize
2650 */
2651 char *e;
2652 int err = 0;
2653 unsigned long long size = simple_strtoull(buf, &e, 10);
2654 if (!*buf || *buf == '\n' ||
2655 (*e && *e != '\n'))
2656 return -EINVAL;
2657
2658 if (mddev->pers) {
2659 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002660 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002661 } else {
2662 if (mddev->size == 0 ||
2663 mddev->size > size)
2664 mddev->size = size;
2665 else
2666 err = -ENOSPC;
2667 }
2668 return err ? err : len;
2669}
2670
2671static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002672__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002673
NeilBrown8bb93aa2006-01-06 00:20:50 -08002674
2675/* Metdata version.
2676 * This is either 'none' for arrays with externally managed metadata,
2677 * or N.M for internally known formats
2678 */
2679static ssize_t
2680metadata_show(mddev_t *mddev, char *page)
2681{
2682 if (mddev->persistent)
2683 return sprintf(page, "%d.%d\n",
2684 mddev->major_version, mddev->minor_version);
2685 else
2686 return sprintf(page, "none\n");
2687}
2688
2689static ssize_t
2690metadata_store(mddev_t *mddev, const char *buf, size_t len)
2691{
2692 int major, minor;
2693 char *e;
2694 if (!list_empty(&mddev->disks))
2695 return -EBUSY;
2696
2697 if (cmd_match(buf, "none")) {
2698 mddev->persistent = 0;
2699 mddev->major_version = 0;
2700 mddev->minor_version = 90;
2701 return len;
2702 }
2703 major = simple_strtoul(buf, &e, 10);
2704 if (e==buf || *e != '.')
2705 return -EINVAL;
2706 buf = e+1;
2707 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002708 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002709 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002710 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002711 return -ENOENT;
2712 mddev->major_version = major;
2713 mddev->minor_version = minor;
2714 mddev->persistent = 1;
2715 return len;
2716}
2717
2718static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002719__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002720
NeilBrowna35b0d62006-01-06 00:20:49 -08002721static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002722action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002723{
NeilBrown7eec3142005-11-08 21:39:44 -08002724 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002725 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2726 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002727 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2728 type = "reshape";
2729 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002730 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2731 type = "resync";
2732 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2733 type = "check";
2734 else
2735 type = "repair";
2736 } else
2737 type = "recover";
2738 }
2739 return sprintf(page, "%s\n", type);
2740}
2741
2742static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002743action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002744{
NeilBrown7eec3142005-11-08 21:39:44 -08002745 if (!mddev->pers || !mddev->pers->sync_request)
2746 return -EINVAL;
2747
NeilBrownbce74da2006-01-06 00:20:41 -08002748 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002749 if (mddev->sync_thread) {
2750 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2751 md_unregister_thread(mddev->sync_thread);
2752 mddev->sync_thread = NULL;
2753 mddev->recovery = 0;
2754 }
NeilBrown03c902e2006-01-06 00:20:46 -08002755 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2756 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002757 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002758 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002759 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002760 else if (cmd_match(page, "reshape")) {
2761 int err;
2762 if (mddev->pers->start_reshape == NULL)
2763 return -EINVAL;
2764 err = mddev->pers->start_reshape(mddev);
2765 if (err)
2766 return err;
2767 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002768 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002769 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002770 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002771 return -EINVAL;
2772 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2773 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002774 }
NeilBrown03c902e2006-01-06 00:20:46 -08002775 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002776 md_wakeup_thread(mddev->thread);
2777 return len;
2778}
2779
NeilBrown9d888832005-11-08 21:39:26 -08002780static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002781mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002782{
2783 return sprintf(page, "%llu\n",
2784 (unsigned long long) mddev->resync_mismatches);
2785}
2786
NeilBrown80ca3a42006-07-10 04:44:18 -07002787static struct md_sysfs_entry md_scan_mode =
2788__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002789
NeilBrown96de1e62005-11-08 21:39:39 -08002790
NeilBrown80ca3a42006-07-10 04:44:18 -07002791static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002792
NeilBrown88202a02006-01-06 00:21:36 -08002793static ssize_t
2794sync_min_show(mddev_t *mddev, char *page)
2795{
2796 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2797 mddev->sync_speed_min ? "local": "system");
2798}
2799
2800static ssize_t
2801sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2802{
2803 int min;
2804 char *e;
2805 if (strncmp(buf, "system", 6)==0) {
2806 mddev->sync_speed_min = 0;
2807 return len;
2808 }
2809 min = simple_strtoul(buf, &e, 10);
2810 if (buf == e || (*e && *e != '\n') || min <= 0)
2811 return -EINVAL;
2812 mddev->sync_speed_min = min;
2813 return len;
2814}
2815
2816static struct md_sysfs_entry md_sync_min =
2817__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2818
2819static ssize_t
2820sync_max_show(mddev_t *mddev, char *page)
2821{
2822 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2823 mddev->sync_speed_max ? "local": "system");
2824}
2825
2826static ssize_t
2827sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2828{
2829 int max;
2830 char *e;
2831 if (strncmp(buf, "system", 6)==0) {
2832 mddev->sync_speed_max = 0;
2833 return len;
2834 }
2835 max = simple_strtoul(buf, &e, 10);
2836 if (buf == e || (*e && *e != '\n') || max <= 0)
2837 return -EINVAL;
2838 mddev->sync_speed_max = max;
2839 return len;
2840}
2841
2842static struct md_sysfs_entry md_sync_max =
2843__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2844
2845
2846static ssize_t
2847sync_speed_show(mddev_t *mddev, char *page)
2848{
2849 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002850 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002851 dt = ((jiffies - mddev->resync_mark) / HZ);
2852 if (!dt) dt++;
2853 db = resync - (mddev->resync_mark_cnt);
2854 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2855}
2856
NeilBrown80ca3a42006-07-10 04:44:18 -07002857static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002858
2859static ssize_t
2860sync_completed_show(mddev_t *mddev, char *page)
2861{
2862 unsigned long max_blocks, resync;
2863
2864 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2865 max_blocks = mddev->resync_max_sectors;
2866 else
2867 max_blocks = mddev->size << 1;
2868
2869 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2870 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2871}
2872
NeilBrown80ca3a42006-07-10 04:44:18 -07002873static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08002874
NeilBrowne464eaf2006-03-27 01:18:14 -08002875static ssize_t
2876suspend_lo_show(mddev_t *mddev, char *page)
2877{
2878 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2879}
2880
2881static ssize_t
2882suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2883{
2884 char *e;
2885 unsigned long long new = simple_strtoull(buf, &e, 10);
2886
2887 if (mddev->pers->quiesce == NULL)
2888 return -EINVAL;
2889 if (buf == e || (*e && *e != '\n'))
2890 return -EINVAL;
2891 if (new >= mddev->suspend_hi ||
2892 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2893 mddev->suspend_lo = new;
2894 mddev->pers->quiesce(mddev, 2);
2895 return len;
2896 } else
2897 return -EINVAL;
2898}
2899static struct md_sysfs_entry md_suspend_lo =
2900__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2901
2902
2903static ssize_t
2904suspend_hi_show(mddev_t *mddev, char *page)
2905{
2906 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2907}
2908
2909static ssize_t
2910suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2911{
2912 char *e;
2913 unsigned long long new = simple_strtoull(buf, &e, 10);
2914
2915 if (mddev->pers->quiesce == NULL)
2916 return -EINVAL;
2917 if (buf == e || (*e && *e != '\n'))
2918 return -EINVAL;
2919 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2920 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2921 mddev->suspend_hi = new;
2922 mddev->pers->quiesce(mddev, 1);
2923 mddev->pers->quiesce(mddev, 0);
2924 return len;
2925 } else
2926 return -EINVAL;
2927}
2928static struct md_sysfs_entry md_suspend_hi =
2929__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2930
NeilBrown08a02ec2007-05-09 02:35:38 -07002931static ssize_t
2932reshape_position_show(mddev_t *mddev, char *page)
2933{
2934 if (mddev->reshape_position != MaxSector)
2935 return sprintf(page, "%llu\n",
2936 (unsigned long long)mddev->reshape_position);
2937 strcpy(page, "none\n");
2938 return 5;
2939}
2940
2941static ssize_t
2942reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
2943{
2944 char *e;
2945 unsigned long long new = simple_strtoull(buf, &e, 10);
2946 if (mddev->pers)
2947 return -EBUSY;
2948 if (buf == e || (*e && *e != '\n'))
2949 return -EINVAL;
2950 mddev->reshape_position = new;
2951 mddev->delta_disks = 0;
2952 mddev->new_level = mddev->level;
2953 mddev->new_layout = mddev->layout;
2954 mddev->new_chunk = mddev->chunk_size;
2955 return len;
2956}
2957
2958static struct md_sysfs_entry md_reshape_position =
2959__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
2960 reshape_position_store);
2961
NeilBrowne464eaf2006-03-27 01:18:14 -08002962
NeilBrowneae17012005-11-08 21:39:23 -08002963static struct attribute *md_default_attrs[] = {
2964 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07002965 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002966 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002967 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002968 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07002969 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002970 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002971 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002972 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07002973 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07002974 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002975 NULL,
2976};
2977
2978static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002979 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002980 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002981 &md_sync_min.attr,
2982 &md_sync_max.attr,
2983 &md_sync_speed.attr,
2984 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002985 &md_suspend_lo.attr,
2986 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07002987 &md_bitmap.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002988 NULL,
2989};
NeilBrown411036f2005-11-08 21:39:40 -08002990static struct attribute_group md_redundancy_group = {
2991 .name = NULL,
2992 .attrs = md_redundancy_attrs,
2993};
2994
NeilBrowneae17012005-11-08 21:39:23 -08002995
2996static ssize_t
2997md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2998{
2999 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3000 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003001 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003002
3003 if (!entry->show)
3004 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003005 rv = mddev_lock(mddev);
3006 if (!rv) {
3007 rv = entry->show(mddev, page);
3008 mddev_unlock(mddev);
3009 }
NeilBrown96de1e62005-11-08 21:39:39 -08003010 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003011}
3012
3013static ssize_t
3014md_attr_store(struct kobject *kobj, struct attribute *attr,
3015 const char *page, size_t length)
3016{
3017 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3018 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003019 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003020
3021 if (!entry->store)
3022 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003023 if (!capable(CAP_SYS_ADMIN))
3024 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003025 rv = mddev_lock(mddev);
3026 if (!rv) {
3027 rv = entry->store(mddev, page, length);
3028 mddev_unlock(mddev);
3029 }
NeilBrown96de1e62005-11-08 21:39:39 -08003030 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003031}
3032
3033static void md_free(struct kobject *ko)
3034{
3035 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3036 kfree(mddev);
3037}
3038
3039static struct sysfs_ops md_sysfs_ops = {
3040 .show = md_attr_show,
3041 .store = md_attr_store,
3042};
3043static struct kobj_type md_ktype = {
3044 .release = md_free,
3045 .sysfs_ops = &md_sysfs_ops,
3046 .default_attrs = md_default_attrs,
3047};
3048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049int mdp_major = 0;
3050
3051static struct kobject *md_probe(dev_t dev, int *part, void *data)
3052{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003053 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 mddev_t *mddev = mddev_find(dev);
3055 struct gendisk *disk;
3056 int partitioned = (MAJOR(dev) != MD_MAJOR);
3057 int shift = partitioned ? MdpMinorShift : 0;
3058 int unit = MINOR(dev) >> shift;
3059
3060 if (!mddev)
3061 return NULL;
3062
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003063 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003065 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 mddev_put(mddev);
3067 return NULL;
3068 }
3069 disk = alloc_disk(1 << shift);
3070 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003071 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 mddev_put(mddev);
3073 return NULL;
3074 }
3075 disk->major = MAJOR(dev);
3076 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003077 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003079 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 disk->fops = &md_fops;
3082 disk->private_data = mddev;
3083 disk->queue = mddev->queue;
3084 add_disk(disk);
3085 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003086 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08003087 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08003088 mddev->kobj.k_name = NULL;
3089 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
3090 mddev->kobj.ktype = &md_ktype;
NeilBrown5e55e2f2007-03-26 21:32:14 -08003091 if (kobject_register(&mddev->kobj))
3092 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3093 disk->disk_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 return NULL;
3095}
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097static void md_safemode_timeout(unsigned long data)
3098{
3099 mddev_t *mddev = (mddev_t *) data;
3100
3101 mddev->safemode = 1;
3102 md_wakeup_thread(mddev->thread);
3103}
3104
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003105static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107static int do_md_run(mddev_t * mddev)
3108{
NeilBrown2604b702006-01-06 00:20:36 -08003109 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 int chunk_size;
3111 struct list_head *tmp;
3112 mdk_rdev_t *rdev;
3113 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003114 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 char b[BDEVNAME_SIZE];
3116
NeilBrowna757e642005-04-16 15:26:42 -07003117 if (list_empty(&mddev->disks))
3118 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 if (mddev->pers)
3122 return -EBUSY;
3123
3124 /*
3125 * Analyze all RAID superblock(s)
3126 */
NeilBrowna757e642005-04-16 15:26:42 -07003127 if (!mddev->raid_disks)
3128 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129
3130 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
NeilBrown2604b702006-01-06 00:20:36 -08003132 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (chunk_size > MAX_CHUNK_SIZE) {
3134 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3135 chunk_size, MAX_CHUNK_SIZE);
3136 return -EINVAL;
3137 }
3138 /*
3139 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3140 */
3141 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003142 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 return -EINVAL;
3144 }
3145 if (chunk_size < PAGE_SIZE) {
3146 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3147 chunk_size, PAGE_SIZE);
3148 return -EINVAL;
3149 }
3150
3151 /* devices must have minimum size of one chunk */
3152 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003153 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 continue;
3155 if (rdev->size < chunk_size / 1024) {
3156 printk(KERN_WARNING
3157 "md: Dev %s smaller than chunk_size:"
3158 " %lluk < %dk\n",
3159 bdevname(rdev->bdev,b),
3160 (unsigned long long)rdev->size,
3161 chunk_size / 1024);
3162 return -EINVAL;
3163 }
3164 }
3165 }
3166
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003168 if (mddev->level != LEVEL_NONE)
3169 request_module("md-level-%d", mddev->level);
3170 else if (mddev->clevel[0])
3171 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172#endif
3173
3174 /*
3175 * Drop all container device buffers, from now on
3176 * the only valid external interface is through the md
3177 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 */
3179 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003180 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 continue;
3182 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003183 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003184
3185 /* perform some consistency tests on the device.
3186 * We don't want the data to overlap the metadata,
3187 * Internal Bitmap issues has handled elsewhere.
3188 */
3189 if (rdev->data_offset < rdev->sb_offset) {
3190 if (mddev->size &&
3191 rdev->data_offset + mddev->size*2
3192 > rdev->sb_offset*2) {
3193 printk("md: %s: data overlaps metadata\n",
3194 mdname(mddev));
3195 return -EINVAL;
3196 }
3197 } else {
3198 if (rdev->sb_offset*2 + rdev->sb_size/512
3199 > rdev->data_offset) {
3200 printk("md: %s: metadata overlaps data\n",
3201 mdname(mddev));
3202 return -EINVAL;
3203 }
3204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
3206
3207 md_probe(mddev->unit, NULL, NULL);
3208 disk = mddev->gendisk;
3209 if (!disk)
3210 return -ENOMEM;
3211
3212 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003213 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003214 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003216 if (mddev->level != LEVEL_NONE)
3217 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3218 mddev->level);
3219 else
3220 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3221 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 return -EINVAL;
3223 }
NeilBrown2604b702006-01-06 00:20:36 -08003224 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003226 mddev->level = pers->level;
3227 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
NeilBrownf6705572006-03-27 01:18:11 -08003229 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003230 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003231 /* This personality cannot handle reshaping... */
3232 mddev->pers = NULL;
3233 module_put(pers->owner);
3234 return -EINVAL;
3235 }
3236
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003237 if (pers->sync_request) {
3238 /* Warn if this is a potentially silly
3239 * configuration.
3240 */
3241 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3242 mdk_rdev_t *rdev2;
3243 struct list_head *tmp2;
3244 int warned = 0;
3245 ITERATE_RDEV(mddev, rdev, tmp) {
3246 ITERATE_RDEV(mddev, rdev2, tmp2) {
3247 if (rdev < rdev2 &&
3248 rdev->bdev->bd_contains ==
3249 rdev2->bdev->bd_contains) {
3250 printk(KERN_WARNING
3251 "%s: WARNING: %s appears to be"
3252 " on the same physical disk as"
3253 " %s.\n",
3254 mdname(mddev),
3255 bdevname(rdev->bdev,b),
3256 bdevname(rdev2->bdev,b2));
3257 warned = 1;
3258 }
3259 }
3260 }
3261 if (warned)
3262 printk(KERN_WARNING
3263 "True protection against single-disk"
3264 " failure might be compromised.\n");
3265 }
3266
NeilBrown657390d2005-08-26 18:34:16 -07003267 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003269 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003270 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
NeilBrownf91de922005-11-08 21:39:36 -08003272 if (start_readonly)
3273 mddev->ro = 2; /* read-only, but switch on first write */
3274
NeilBrownb15c2e52006-01-06 00:20:16 -08003275 err = mddev->pers->run(mddev);
3276 if (!err && mddev->pers->sync_request) {
3277 err = bitmap_create(mddev);
3278 if (err) {
3279 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3280 mdname(mddev), err);
3281 mddev->pers->stop(mddev);
3282 }
3283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 if (err) {
3285 printk(KERN_ERR "md: pers->run() failed ...\n");
3286 module_put(mddev->pers->owner);
3287 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003288 bitmap_destroy(mddev);
3289 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003291 if (mddev->pers->sync_request) {
3292 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3293 printk(KERN_WARNING
3294 "md: cannot register extra attributes for %s\n",
3295 mdname(mddev));
3296 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003297 mddev->ro = 0;
3298
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 atomic_set(&mddev->writes_pending,0);
3300 mddev->safemode = 0;
3301 mddev->safemode_timer.function = md_safemode_timeout;
3302 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003303 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003305
3306 ITERATE_RDEV(mddev,rdev,tmp)
3307 if (rdev->raid_disk >= 0) {
3308 char nm[20];
3309 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003310 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3311 printk("md: cannot register %s for %s\n",
3312 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3316
NeilBrown850b2b42006-10-03 01:15:46 -07003317 if (mddev->flags)
3318 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
3320 set_capacity(disk, mddev->array_size<<1);
3321
3322 /* If we call blk_queue_make_request here, it will
3323 * re-initialise max_sectors etc which may have been
3324 * refined inside -> run. So just set the bits we need to set.
3325 * Most initialisation happended when we called
3326 * blk_queue_make_request(..., md_fail_request)
3327 * earlier.
3328 */
3329 mddev->queue->queuedata = mddev;
3330 mddev->queue->make_request_fn = mddev->pers->make_request;
3331
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003332 /* If there is a partially-recovered drive we need to
3333 * start recovery here. If we leave it to md_check_recovery,
3334 * it will remove the drives and not do the right thing
3335 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003336 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003337 struct list_head *rtmp;
3338 int spares = 0;
3339 ITERATE_RDEV(mddev,rdev,rtmp)
3340 if (rdev->raid_disk >= 0 &&
3341 !test_bit(In_sync, &rdev->flags) &&
3342 !test_bit(Faulty, &rdev->flags))
3343 /* complete an interrupted recovery */
3344 spares++;
3345 if (spares && mddev->pers->sync_request) {
3346 mddev->recovery = 0;
3347 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3348 mddev->sync_thread = md_register_thread(md_do_sync,
3349 mddev,
3350 "%s_resync");
3351 if (!mddev->sync_thread) {
3352 printk(KERN_ERR "%s: could not start resync"
3353 " thread...\n",
3354 mdname(mddev));
3355 /* leave the spares where they are, it shouldn't hurt */
3356 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003357 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003358 }
3359 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003360 md_wakeup_thread(mddev->thread);
3361 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003362
Linus Torvalds44ce62942007-05-09 18:51:36 -07003363 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003364 md_new_event(mddev);
NeilBrown2f471302006-11-08 17:44:47 -08003365 kobject_uevent(&mddev->gendisk->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 return 0;
3367}
3368
3369static int restart_array(mddev_t *mddev)
3370{
3371 struct gendisk *disk = mddev->gendisk;
3372 int err;
3373
3374 /*
3375 * Complain if it has no devices
3376 */
3377 err = -ENXIO;
3378 if (list_empty(&mddev->disks))
3379 goto out;
3380
3381 if (mddev->pers) {
3382 err = -EBUSY;
3383 if (!mddev->ro)
3384 goto out;
3385
3386 mddev->safemode = 0;
3387 mddev->ro = 0;
3388 set_disk_ro(disk, 0);
3389
3390 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3391 mdname(mddev));
3392 /*
3393 * Kick recovery or resync if necessary
3394 */
3395 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3396 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003397 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003399 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
3402out:
3403 return err;
3404}
3405
NeilBrownacc55e22006-06-26 00:27:47 -07003406/* similar to deny_write_access, but accounts for our holding a reference
3407 * to the file ourselves */
3408static int deny_bitmap_write_access(struct file * file)
3409{
3410 struct inode *inode = file->f_mapping->host;
3411
3412 spin_lock(&inode->i_lock);
3413 if (atomic_read(&inode->i_writecount) > 1) {
3414 spin_unlock(&inode->i_lock);
3415 return -ETXTBSY;
3416 }
3417 atomic_set(&inode->i_writecount, -1);
3418 spin_unlock(&inode->i_lock);
3419
3420 return 0;
3421}
3422
3423static void restore_bitmap_write_access(struct file *file)
3424{
3425 struct inode *inode = file->f_mapping->host;
3426
3427 spin_lock(&inode->i_lock);
3428 atomic_set(&inode->i_writecount, 1);
3429 spin_unlock(&inode->i_lock);
3430}
3431
NeilBrown9e653b62006-06-26 00:27:58 -07003432/* mode:
3433 * 0 - completely stop and dis-assemble array
3434 * 1 - switch to readonly
3435 * 2 - stop but do not disassemble array
3436 */
3437static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438{
3439 int err = 0;
3440 struct gendisk *disk = mddev->gendisk;
3441
3442 if (mddev->pers) {
3443 if (atomic_read(&mddev->active)>2) {
3444 printk("md: %s still in use.\n",mdname(mddev));
3445 return -EBUSY;
3446 }
3447
3448 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003449 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3451 md_unregister_thread(mddev->sync_thread);
3452 mddev->sync_thread = NULL;
3453 }
3454
3455 del_timer_sync(&mddev->safemode_timer);
3456
3457 invalidate_partition(disk, 0);
3458
NeilBrown9e653b62006-06-26 00:27:58 -07003459 switch(mode) {
3460 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003462 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 goto out;
3464 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003465 break;
3466 case 0: /* disassemble */
3467 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003468 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003469 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 if (mddev->ro)
3471 set_disk_ro(disk, 0);
3472 blk_queue_make_request(mddev->queue, md_fail_request);
3473 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003474 mddev->queue->merge_bvec_fn = NULL;
3475 mddev->queue->unplug_fn = NULL;
3476 mddev->queue->issue_flush_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003477 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003478 if (mddev->pers->sync_request)
3479 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 module_put(mddev->pers->owner);
3482 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003483
3484 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003485 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 if (mddev->ro)
3488 mddev->ro = 0;
3489 }
NeilBrown850b2b42006-10-03 01:15:46 -07003490 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 /* mark array as shutdown cleanly */
3492 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003493 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 }
NeilBrown9e653b62006-06-26 00:27:58 -07003495 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003497 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
NeilBrown32a76272005-06-21 17:17:14 -07003499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 /*
3501 * Free resources if final stop
3502 */
NeilBrown9e653b62006-06-26 00:27:58 -07003503 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003504 mdk_rdev_t *rdev;
3505 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003506
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3508
NeilBrown978f9462006-02-02 14:28:05 -08003509 bitmap_destroy(mddev);
3510 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003511 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003512 fput(mddev->bitmap_file);
3513 mddev->bitmap_file = NULL;
3514 }
3515 mddev->bitmap_offset = 0;
3516
NeilBrown86e6ffd2005-11-08 21:39:24 -08003517 ITERATE_RDEV(mddev,rdev,tmp)
3518 if (rdev->raid_disk >= 0) {
3519 char nm[20];
3520 sprintf(nm, "rd%d", rdev->raid_disk);
3521 sysfs_remove_link(&mddev->kobj, nm);
3522 }
3523
NeilBrown5792a282007-04-04 19:08:18 -07003524 /* make sure all delayed_delete calls have finished */
3525 flush_scheduled_work();
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 export_array(mddev);
3528
3529 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003530 mddev->size = 0;
3531 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003532 mddev->recovery_cp = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003533 mddev->reshape_position = MaxSector;
NeilBrown9e653b62006-06-26 00:27:58 -07003534
NeilBrowna8a55c32006-06-26 00:27:39 -07003535 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3537 mdname(mddev));
3538 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003539 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540out:
3541 return err;
3542}
3543
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003544#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545static void autorun_array(mddev_t *mddev)
3546{
3547 mdk_rdev_t *rdev;
3548 struct list_head *tmp;
3549 int err;
3550
NeilBrowna757e642005-04-16 15:26:42 -07003551 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553
3554 printk(KERN_INFO "md: running: ");
3555
3556 ITERATE_RDEV(mddev,rdev,tmp) {
3557 char b[BDEVNAME_SIZE];
3558 printk("<%s>", bdevname(rdev->bdev,b));
3559 }
3560 printk("\n");
3561
3562 err = do_md_run (mddev);
3563 if (err) {
3564 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3565 do_md_stop (mddev, 0);
3566 }
3567}
3568
3569/*
3570 * lets try to run arrays based on all disks that have arrived
3571 * until now. (those are in pending_raid_disks)
3572 *
3573 * the method: pick the first pending disk, collect all disks with
3574 * the same UUID, remove all from the pending list and put them into
3575 * the 'same_array' list. Then order this list based on superblock
3576 * update time (freshest comes first), kick out 'old' disks and
3577 * compare superblocks. If everything's fine then run it.
3578 *
3579 * If "unit" is allocated, then bump its reference count
3580 */
3581static void autorun_devices(int part)
3582{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 struct list_head *tmp;
3584 mdk_rdev_t *rdev0, *rdev;
3585 mddev_t *mddev;
3586 char b[BDEVNAME_SIZE];
3587
3588 printk(KERN_INFO "md: autorun ...\n");
3589 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003590 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003592 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 rdev0 = list_entry(pending_raid_disks.next,
3594 mdk_rdev_t, same_set);
3595
3596 printk(KERN_INFO "md: considering %s ...\n",
3597 bdevname(rdev0->bdev,b));
3598 INIT_LIST_HEAD(&candidates);
3599 ITERATE_RDEV_PENDING(rdev,tmp)
3600 if (super_90_load(rdev, rdev0, 0) >= 0) {
3601 printk(KERN_INFO "md: adding %s ...\n",
3602 bdevname(rdev->bdev,b));
3603 list_move(&rdev->same_set, &candidates);
3604 }
3605 /*
3606 * now we have a set of devices, with all of them having
3607 * mostly sane superblocks. It's time to allocate the
3608 * mddev.
3609 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003610 if (part) {
3611 dev = MKDEV(mdp_major,
3612 rdev0->preferred_minor << MdpMinorShift);
3613 unit = MINOR(dev) >> MdpMinorShift;
3614 } else {
3615 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3616 unit = MINOR(dev);
3617 }
3618 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3620 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3621 break;
3622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623
3624 md_probe(dev, NULL, NULL);
3625 mddev = mddev_find(dev);
3626 if (!mddev) {
3627 printk(KERN_ERR
3628 "md: cannot allocate memory for md drive.\n");
3629 break;
3630 }
3631 if (mddev_lock(mddev))
3632 printk(KERN_WARNING "md: %s locked, cannot run\n",
3633 mdname(mddev));
3634 else if (mddev->raid_disks || mddev->major_version
3635 || !list_empty(&mddev->disks)) {
3636 printk(KERN_WARNING
3637 "md: %s already running, cannot run %s\n",
3638 mdname(mddev), bdevname(rdev0->bdev,b));
3639 mddev_unlock(mddev);
3640 } else {
3641 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3642 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3643 list_del_init(&rdev->same_set);
3644 if (bind_rdev_to_array(rdev, mddev))
3645 export_rdev(rdev);
3646 }
3647 autorun_array(mddev);
3648 mddev_unlock(mddev);
3649 }
3650 /* on success, candidates will be empty, on error
3651 * it won't...
3652 */
3653 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3654 export_rdev(rdev);
3655 mddev_put(mddev);
3656 }
3657 printk(KERN_INFO "md: ... autorun DONE.\n");
3658}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003659#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661static int get_version(void __user * arg)
3662{
3663 mdu_version_t ver;
3664
3665 ver.major = MD_MAJOR_VERSION;
3666 ver.minor = MD_MINOR_VERSION;
3667 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3668
3669 if (copy_to_user(arg, &ver, sizeof(ver)))
3670 return -EFAULT;
3671
3672 return 0;
3673}
3674
3675static int get_array_info(mddev_t * mddev, void __user * arg)
3676{
3677 mdu_array_info_t info;
3678 int nr,working,active,failed,spare;
3679 mdk_rdev_t *rdev;
3680 struct list_head *tmp;
3681
3682 nr=working=active=failed=spare=0;
3683 ITERATE_RDEV(mddev,rdev,tmp) {
3684 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003685 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 failed++;
3687 else {
3688 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003689 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 active++;
3691 else
3692 spare++;
3693 }
3694 }
3695
3696 info.major_version = mddev->major_version;
3697 info.minor_version = mddev->minor_version;
3698 info.patch_version = MD_PATCHLEVEL_VERSION;
3699 info.ctime = mddev->ctime;
3700 info.level = mddev->level;
3701 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003702 if (info.size != mddev->size) /* overflow */
3703 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 info.nr_disks = nr;
3705 info.raid_disks = mddev->raid_disks;
3706 info.md_minor = mddev->md_minor;
3707 info.not_persistent= !mddev->persistent;
3708
3709 info.utime = mddev->utime;
3710 info.state = 0;
3711 if (mddev->in_sync)
3712 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003713 if (mddev->bitmap && mddev->bitmap_offset)
3714 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 info.active_disks = active;
3716 info.working_disks = working;
3717 info.failed_disks = failed;
3718 info.spare_disks = spare;
3719
3720 info.layout = mddev->layout;
3721 info.chunk_size = mddev->chunk_size;
3722
3723 if (copy_to_user(arg, &info, sizeof(info)))
3724 return -EFAULT;
3725
3726 return 0;
3727}
3728
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003729static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003730{
3731 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3732 char *ptr, *buf = NULL;
3733 int err = -ENOMEM;
3734
NeilBrown2a2275d2007-01-26 00:57:11 -08003735 md_allow_write(mddev);
3736
NeilBrown32a76272005-06-21 17:17:14 -07003737 file = kmalloc(sizeof(*file), GFP_KERNEL);
3738 if (!file)
3739 goto out;
3740
3741 /* bitmap disabled, zero the first byte and copy out */
3742 if (!mddev->bitmap || !mddev->bitmap->file) {
3743 file->pathname[0] = '\0';
3744 goto copy_out;
3745 }
3746
3747 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3748 if (!buf)
3749 goto out;
3750
3751 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3752 if (!ptr)
3753 goto out;
3754
3755 strcpy(file->pathname, ptr);
3756
3757copy_out:
3758 err = 0;
3759 if (copy_to_user(arg, file, sizeof(*file)))
3760 err = -EFAULT;
3761out:
3762 kfree(buf);
3763 kfree(file);
3764 return err;
3765}
3766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767static int get_disk_info(mddev_t * mddev, void __user * arg)
3768{
3769 mdu_disk_info_t info;
3770 unsigned int nr;
3771 mdk_rdev_t *rdev;
3772
3773 if (copy_from_user(&info, arg, sizeof(info)))
3774 return -EFAULT;
3775
3776 nr = info.number;
3777
3778 rdev = find_rdev_nr(mddev, nr);
3779 if (rdev) {
3780 info.major = MAJOR(rdev->bdev->bd_dev);
3781 info.minor = MINOR(rdev->bdev->bd_dev);
3782 info.raid_disk = rdev->raid_disk;
3783 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003784 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003786 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 info.state |= (1<<MD_DISK_ACTIVE);
3788 info.state |= (1<<MD_DISK_SYNC);
3789 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003790 if (test_bit(WriteMostly, &rdev->flags))
3791 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 } else {
3793 info.major = info.minor = 0;
3794 info.raid_disk = -1;
3795 info.state = (1<<MD_DISK_REMOVED);
3796 }
3797
3798 if (copy_to_user(arg, &info, sizeof(info)))
3799 return -EFAULT;
3800
3801 return 0;
3802}
3803
3804static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3805{
3806 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3807 mdk_rdev_t *rdev;
3808 dev_t dev = MKDEV(info->major,info->minor);
3809
3810 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3811 return -EOVERFLOW;
3812
3813 if (!mddev->raid_disks) {
3814 int err;
3815 /* expecting a device which has a superblock */
3816 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3817 if (IS_ERR(rdev)) {
3818 printk(KERN_WARNING
3819 "md: md_import_device returned %ld\n",
3820 PTR_ERR(rdev));
3821 return PTR_ERR(rdev);
3822 }
3823 if (!list_empty(&mddev->disks)) {
3824 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3825 mdk_rdev_t, same_set);
3826 int err = super_types[mddev->major_version]
3827 .load_super(rdev, rdev0, mddev->minor_version);
3828 if (err < 0) {
3829 printk(KERN_WARNING
3830 "md: %s has different UUID to %s\n",
3831 bdevname(rdev->bdev,b),
3832 bdevname(rdev0->bdev,b2));
3833 export_rdev(rdev);
3834 return -EINVAL;
3835 }
3836 }
3837 err = bind_rdev_to_array(rdev, mddev);
3838 if (err)
3839 export_rdev(rdev);
3840 return err;
3841 }
3842
3843 /*
3844 * add_new_disk can be used once the array is assembled
3845 * to add "hot spares". They must already have a superblock
3846 * written
3847 */
3848 if (mddev->pers) {
3849 int err;
3850 if (!mddev->pers->hot_add_disk) {
3851 printk(KERN_WARNING
3852 "%s: personality does not support diskops!\n",
3853 mdname(mddev));
3854 return -EINVAL;
3855 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003856 if (mddev->persistent)
3857 rdev = md_import_device(dev, mddev->major_version,
3858 mddev->minor_version);
3859 else
3860 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 if (IS_ERR(rdev)) {
3862 printk(KERN_WARNING
3863 "md: md_import_device returned %ld\n",
3864 PTR_ERR(rdev));
3865 return PTR_ERR(rdev);
3866 }
NeilBrown41158c72005-06-21 17:17:25 -07003867 /* set save_raid_disk if appropriate */
3868 if (!mddev->persistent) {
3869 if (info->state & (1<<MD_DISK_SYNC) &&
3870 info->raid_disk < mddev->raid_disks)
3871 rdev->raid_disk = info->raid_disk;
3872 else
3873 rdev->raid_disk = -1;
3874 } else
3875 super_types[mddev->major_version].
3876 validate_super(mddev, rdev);
3877 rdev->saved_raid_disk = rdev->raid_disk;
3878
NeilBrownb2d444d2005-11-08 21:39:31 -08003879 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003880 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3881 set_bit(WriteMostly, &rdev->flags);
3882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 rdev->raid_disk = -1;
3884 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07003885 if (!err && !mddev->pers->hot_remove_disk) {
3886 /* If there is hot_add_disk but no hot_remove_disk
3887 * then added disks for geometry changes,
3888 * and should be added immediately.
3889 */
3890 super_types[mddev->major_version].
3891 validate_super(mddev, rdev);
3892 err = mddev->pers->hot_add_disk(mddev, rdev);
3893 if (err)
3894 unbind_rdev_from_array(rdev);
3895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 if (err)
3897 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003898
NeilBrown17571282006-12-10 02:20:52 -08003899 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07003900 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003901 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return err;
3903 }
3904
3905 /* otherwise, add_new_disk is only allowed
3906 * for major_version==0 superblocks
3907 */
3908 if (mddev->major_version != 0) {
3909 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3910 mdname(mddev));
3911 return -EINVAL;
3912 }
3913
3914 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3915 int err;
3916 rdev = md_import_device (dev, -1, 0);
3917 if (IS_ERR(rdev)) {
3918 printk(KERN_WARNING
3919 "md: error, md_import_device() returned %ld\n",
3920 PTR_ERR(rdev));
3921 return PTR_ERR(rdev);
3922 }
3923 rdev->desc_nr = info->number;
3924 if (info->raid_disk < mddev->raid_disks)
3925 rdev->raid_disk = info->raid_disk;
3926 else
3927 rdev->raid_disk = -1;
3928
NeilBrownb2d444d2005-11-08 21:39:31 -08003929 rdev->flags = 0;
3930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003932 if (info->state & (1<<MD_DISK_SYNC))
3933 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003935 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3936 set_bit(WriteMostly, &rdev->flags);
3937
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (!mddev->persistent) {
3939 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3940 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3941 } else
3942 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3943 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3944
NeilBrown2bf071b2006-01-06 00:20:55 -08003945 err = bind_rdev_to_array(rdev, mddev);
3946 if (err) {
3947 export_rdev(rdev);
3948 return err;
3949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 }
3951
3952 return 0;
3953}
3954
3955static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3956{
3957 char b[BDEVNAME_SIZE];
3958 mdk_rdev_t *rdev;
3959
3960 if (!mddev->pers)
3961 return -ENODEV;
3962
3963 rdev = find_rdev(mddev, dev);
3964 if (!rdev)
3965 return -ENXIO;
3966
3967 if (rdev->raid_disk >= 0)
3968 goto busy;
3969
3970 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07003971 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08003972 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
3974 return 0;
3975busy:
3976 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3977 bdevname(rdev->bdev,b), mdname(mddev));
3978 return -EBUSY;
3979}
3980
3981static int hot_add_disk(mddev_t * mddev, dev_t dev)
3982{
3983 char b[BDEVNAME_SIZE];
3984 int err;
3985 unsigned int size;
3986 mdk_rdev_t *rdev;
3987
3988 if (!mddev->pers)
3989 return -ENODEV;
3990
3991 if (mddev->major_version != 0) {
3992 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3993 " version-0 superblocks.\n",
3994 mdname(mddev));
3995 return -EINVAL;
3996 }
3997 if (!mddev->pers->hot_add_disk) {
3998 printk(KERN_WARNING
3999 "%s: personality does not support diskops!\n",
4000 mdname(mddev));
4001 return -EINVAL;
4002 }
4003
4004 rdev = md_import_device (dev, -1, 0);
4005 if (IS_ERR(rdev)) {
4006 printk(KERN_WARNING
4007 "md: error, md_import_device() returned %ld\n",
4008 PTR_ERR(rdev));
4009 return -EINVAL;
4010 }
4011
4012 if (mddev->persistent)
4013 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4014 else
4015 rdev->sb_offset =
4016 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4017
4018 size = calc_dev_size(rdev, mddev->chunk_size);
4019 rdev->size = size;
4020
NeilBrownb2d444d2005-11-08 21:39:31 -08004021 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 printk(KERN_WARNING
4023 "md: can not hot-add faulty %s disk to %s!\n",
4024 bdevname(rdev->bdev,b), mdname(mddev));
4025 err = -EINVAL;
4026 goto abort_export;
4027 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004028 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004030 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004031 err = bind_rdev_to_array(rdev, mddev);
4032 if (err)
4033 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
4035 /*
4036 * The rest should better be atomic, we can have disk failures
4037 * noticed in interrupt contexts ...
4038 */
4039
4040 if (rdev->desc_nr == mddev->max_disks) {
4041 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4042 mdname(mddev));
4043 err = -EBUSY;
4044 goto abort_unbind_export;
4045 }
4046
4047 rdev->raid_disk = -1;
4048
NeilBrown850b2b42006-10-03 01:15:46 -07004049 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
4051 /*
4052 * Kick recovery, maybe this spare has to be added to the
4053 * array immediately.
4054 */
4055 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4056 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004057 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 return 0;
4059
4060abort_unbind_export:
4061 unbind_rdev_from_array(rdev);
4062
4063abort_export:
4064 export_rdev(rdev);
4065 return err;
4066}
4067
NeilBrown32a76272005-06-21 17:17:14 -07004068static int set_bitmap_file(mddev_t *mddev, int fd)
4069{
4070 int err;
4071
NeilBrown36fa3062005-09-09 16:23:45 -07004072 if (mddev->pers) {
4073 if (!mddev->pers->quiesce)
4074 return -EBUSY;
4075 if (mddev->recovery || mddev->sync_thread)
4076 return -EBUSY;
4077 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004078 }
4079
NeilBrown36fa3062005-09-09 16:23:45 -07004080
4081 if (fd >= 0) {
4082 if (mddev->bitmap)
4083 return -EEXIST; /* cannot add when bitmap is present */
4084 mddev->bitmap_file = fget(fd);
4085
4086 if (mddev->bitmap_file == NULL) {
4087 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4088 mdname(mddev));
4089 return -EBADF;
4090 }
4091
4092 err = deny_bitmap_write_access(mddev->bitmap_file);
4093 if (err) {
4094 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4095 mdname(mddev));
4096 fput(mddev->bitmap_file);
4097 mddev->bitmap_file = NULL;
4098 return err;
4099 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004100 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004101 } else if (mddev->bitmap == NULL)
4102 return -ENOENT; /* cannot remove what isn't there */
4103 err = 0;
4104 if (mddev->pers) {
4105 mddev->pers->quiesce(mddev, 1);
4106 if (fd >= 0)
4107 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004108 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004109 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004110 fd = -1; /* make sure to put the file */
4111 }
NeilBrown36fa3062005-09-09 16:23:45 -07004112 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004113 }
4114 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004115 if (mddev->bitmap_file) {
4116 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004117 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004118 }
NeilBrown36fa3062005-09-09 16:23:45 -07004119 mddev->bitmap_file = NULL;
4120 }
4121
NeilBrown32a76272005-06-21 17:17:14 -07004122 return err;
4123}
4124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125/*
4126 * set_array_info is used two different ways
4127 * The original usage is when creating a new array.
4128 * In this usage, raid_disks is > 0 and it together with
4129 * level, size, not_persistent,layout,chunksize determine the
4130 * shape of the array.
4131 * This will always create an array with a type-0.90.0 superblock.
4132 * The newer usage is when assembling an array.
4133 * In this case raid_disks will be 0, and the major_version field is
4134 * use to determine which style super-blocks are to be found on the devices.
4135 * The minor and patch _version numbers are also kept incase the
4136 * super_block handler wishes to interpret them.
4137 */
4138static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4139{
4140
4141 if (info->raid_disks == 0) {
4142 /* just setting version number for superblock loading */
4143 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004144 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 super_types[info->major_version].name == NULL) {
4146 /* maybe try to auto-load a module? */
4147 printk(KERN_INFO
4148 "md: superblock version %d not known\n",
4149 info->major_version);
4150 return -EINVAL;
4151 }
4152 mddev->major_version = info->major_version;
4153 mddev->minor_version = info->minor_version;
4154 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004155 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 return 0;
4157 }
4158 mddev->major_version = MD_MAJOR_VERSION;
4159 mddev->minor_version = MD_MINOR_VERSION;
4160 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4161 mddev->ctime = get_seconds();
4162
4163 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004164 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 mddev->size = info->size;
4166 mddev->raid_disks = info->raid_disks;
4167 /* don't set md_minor, it is determined by which /dev/md* was
4168 * openned
4169 */
4170 if (info->state & (1<<MD_SB_CLEAN))
4171 mddev->recovery_cp = MaxSector;
4172 else
4173 mddev->recovery_cp = 0;
4174 mddev->persistent = ! info->not_persistent;
4175
4176 mddev->layout = info->layout;
4177 mddev->chunk_size = info->chunk_size;
4178
4179 mddev->max_disks = MD_SB_DISKS;
4180
NeilBrown850b2b42006-10-03 01:15:46 -07004181 mddev->flags = 0;
4182 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
NeilBrownb2a27032005-11-28 13:44:12 -08004184 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4185 mddev->bitmap_offset = 0;
4186
NeilBrownf6705572006-03-27 01:18:11 -08004187 mddev->reshape_position = MaxSector;
4188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 /*
4190 * Generate a 128 bit UUID
4191 */
4192 get_random_bytes(mddev->uuid, 16);
4193
NeilBrownf6705572006-03-27 01:18:11 -08004194 mddev->new_level = mddev->level;
4195 mddev->new_chunk = mddev->chunk_size;
4196 mddev->new_layout = mddev->layout;
4197 mddev->delta_disks = 0;
4198
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return 0;
4200}
4201
NeilBrowna35b0d62006-01-06 00:20:49 -08004202static int update_size(mddev_t *mddev, unsigned long size)
4203{
4204 mdk_rdev_t * rdev;
4205 int rv;
4206 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004207 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004208
4209 if (mddev->pers->resize == NULL)
4210 return -EINVAL;
4211 /* The "size" is the amount of each device that is used.
4212 * This can only make sense for arrays with redundancy.
4213 * linear and raid0 always use whatever space is available
4214 * We can only consider changing the size if no resync
4215 * or reconstruction is happening, and if the new size
4216 * is acceptable. It must fit before the sb_offset or,
4217 * if that is <data_offset, it must fit before the
4218 * size of each device.
4219 * If size is zero, we find the largest size that fits.
4220 */
4221 if (mddev->sync_thread)
4222 return -EBUSY;
4223 ITERATE_RDEV(mddev,rdev,tmp) {
4224 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004225 avail = rdev->size * 2;
4226
NeilBrowna35b0d62006-01-06 00:20:49 -08004227 if (fit && (size == 0 || size > avail/2))
4228 size = avail/2;
4229 if (avail < ((sector_t)size << 1))
4230 return -ENOSPC;
4231 }
4232 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4233 if (!rv) {
4234 struct block_device *bdev;
4235
4236 bdev = bdget_disk(mddev->gendisk, 0);
4237 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004238 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004239 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004240 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004241 bdput(bdev);
4242 }
4243 }
4244 return rv;
4245}
4246
NeilBrownda943b992006-01-06 00:20:54 -08004247static int update_raid_disks(mddev_t *mddev, int raid_disks)
4248{
4249 int rv;
4250 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004251 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004252 return -EINVAL;
4253 if (raid_disks <= 0 ||
4254 raid_disks >= mddev->max_disks)
4255 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004256 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004257 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004258 mddev->delta_disks = raid_disks - mddev->raid_disks;
4259
4260 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004261 return rv;
4262}
4263
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265/*
4266 * update_array_info is used to change the configuration of an
4267 * on-line array.
4268 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4269 * fields in the info are checked against the array.
4270 * Any differences that cannot be handled will cause an error.
4271 * Normally, only one change can be managed at a time.
4272 */
4273static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4274{
4275 int rv = 0;
4276 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004277 int state = 0;
4278
4279 /* calculate expected state,ignoring low bits */
4280 if (mddev->bitmap && mddev->bitmap_offset)
4281 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
4283 if (mddev->major_version != info->major_version ||
4284 mddev->minor_version != info->minor_version ||
4285/* mddev->patch_version != info->patch_version || */
4286 mddev->ctime != info->ctime ||
4287 mddev->level != info->level ||
4288/* mddev->layout != info->layout || */
4289 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004290 mddev->chunk_size != info->chunk_size ||
4291 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4292 ((state^info->state) & 0xfffffe00)
4293 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return -EINVAL;
4295 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004296 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (mddev->raid_disks != info->raid_disks) cnt++;
4298 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004299 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 if (cnt == 0) return 0;
4301 if (cnt > 1) return -EINVAL;
4302
4303 if (mddev->layout != info->layout) {
4304 /* Change layout
4305 * we don't need to do anything at the md level, the
4306 * personality will take care of it all.
4307 */
4308 if (mddev->pers->reconfig == NULL)
4309 return -EINVAL;
4310 else
4311 return mddev->pers->reconfig(mddev, info->layout, -1);
4312 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004313 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004314 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
NeilBrownda943b992006-01-06 00:20:54 -08004316 if (mddev->raid_disks != info->raid_disks)
4317 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
NeilBrown36fa3062005-09-09 16:23:45 -07004319 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4320 if (mddev->pers->quiesce == NULL)
4321 return -EINVAL;
4322 if (mddev->recovery || mddev->sync_thread)
4323 return -EBUSY;
4324 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4325 /* add the bitmap */
4326 if (mddev->bitmap)
4327 return -EEXIST;
4328 if (mddev->default_bitmap_offset == 0)
4329 return -EINVAL;
4330 mddev->bitmap_offset = mddev->default_bitmap_offset;
4331 mddev->pers->quiesce(mddev, 1);
4332 rv = bitmap_create(mddev);
4333 if (rv)
4334 bitmap_destroy(mddev);
4335 mddev->pers->quiesce(mddev, 0);
4336 } else {
4337 /* remove the bitmap */
4338 if (!mddev->bitmap)
4339 return -ENOENT;
4340 if (mddev->bitmap->file)
4341 return -EINVAL;
4342 mddev->pers->quiesce(mddev, 1);
4343 bitmap_destroy(mddev);
4344 mddev->pers->quiesce(mddev, 0);
4345 mddev->bitmap_offset = 0;
4346 }
4347 }
NeilBrown850b2b42006-10-03 01:15:46 -07004348 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return rv;
4350}
4351
4352static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4353{
4354 mdk_rdev_t *rdev;
4355
4356 if (mddev->pers == NULL)
4357 return -ENODEV;
4358
4359 rdev = find_rdev(mddev, dev);
4360 if (!rdev)
4361 return -ENODEV;
4362
4363 md_error(mddev, rdev);
4364 return 0;
4365}
4366
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004367static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4368{
4369 mddev_t *mddev = bdev->bd_disk->private_data;
4370
4371 geo->heads = 2;
4372 geo->sectors = 4;
4373 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4374 return 0;
4375}
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377static int md_ioctl(struct inode *inode, struct file *file,
4378 unsigned int cmd, unsigned long arg)
4379{
4380 int err = 0;
4381 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 mddev_t *mddev = NULL;
4383
4384 if (!capable(CAP_SYS_ADMIN))
4385 return -EACCES;
4386
4387 /*
4388 * Commands dealing with the RAID driver but not any
4389 * particular array:
4390 */
4391 switch (cmd)
4392 {
4393 case RAID_VERSION:
4394 err = get_version(argp);
4395 goto done;
4396
4397 case PRINT_RAID_DEBUG:
4398 err = 0;
4399 md_print_devices();
4400 goto done;
4401
4402#ifndef MODULE
4403 case RAID_AUTORUN:
4404 err = 0;
4405 autostart_arrays(arg);
4406 goto done;
4407#endif
4408 default:;
4409 }
4410
4411 /*
4412 * Commands creating/starting a new array:
4413 */
4414
4415 mddev = inode->i_bdev->bd_disk->private_data;
4416
4417 if (!mddev) {
4418 BUG();
4419 goto abort;
4420 }
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 err = mddev_lock(mddev);
4423 if (err) {
4424 printk(KERN_INFO
4425 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4426 err, cmd);
4427 goto abort;
4428 }
4429
4430 switch (cmd)
4431 {
4432 case SET_ARRAY_INFO:
4433 {
4434 mdu_array_info_t info;
4435 if (!arg)
4436 memset(&info, 0, sizeof(info));
4437 else if (copy_from_user(&info, argp, sizeof(info))) {
4438 err = -EFAULT;
4439 goto abort_unlock;
4440 }
4441 if (mddev->pers) {
4442 err = update_array_info(mddev, &info);
4443 if (err) {
4444 printk(KERN_WARNING "md: couldn't update"
4445 " array info. %d\n", err);
4446 goto abort_unlock;
4447 }
4448 goto done_unlock;
4449 }
4450 if (!list_empty(&mddev->disks)) {
4451 printk(KERN_WARNING
4452 "md: array %s already has disks!\n",
4453 mdname(mddev));
4454 err = -EBUSY;
4455 goto abort_unlock;
4456 }
4457 if (mddev->raid_disks) {
4458 printk(KERN_WARNING
4459 "md: array %s already initialised!\n",
4460 mdname(mddev));
4461 err = -EBUSY;
4462 goto abort_unlock;
4463 }
4464 err = set_array_info(mddev, &info);
4465 if (err) {
4466 printk(KERN_WARNING "md: couldn't set"
4467 " array info. %d\n", err);
4468 goto abort_unlock;
4469 }
4470 }
4471 goto done_unlock;
4472
4473 default:;
4474 }
4475
4476 /*
4477 * Commands querying/configuring an existing array:
4478 */
NeilBrown32a76272005-06-21 17:17:14 -07004479 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004480 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrown32a76272005-06-21 17:17:14 -07004481 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
NeilBrown3f9d7b02006-12-22 01:11:41 -08004482 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4483 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 err = -ENODEV;
4485 goto abort_unlock;
4486 }
4487
4488 /*
4489 * Commands even a read-only array can execute:
4490 */
4491 switch (cmd)
4492 {
4493 case GET_ARRAY_INFO:
4494 err = get_array_info(mddev, argp);
4495 goto done_unlock;
4496
NeilBrown32a76272005-06-21 17:17:14 -07004497 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004498 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004499 goto done_unlock;
4500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 case GET_DISK_INFO:
4502 err = get_disk_info(mddev, argp);
4503 goto done_unlock;
4504
4505 case RESTART_ARRAY_RW:
4506 err = restart_array(mddev);
4507 goto done_unlock;
4508
4509 case STOP_ARRAY:
4510 err = do_md_stop (mddev, 0);
4511 goto done_unlock;
4512
4513 case STOP_ARRAY_RO:
4514 err = do_md_stop (mddev, 1);
4515 goto done_unlock;
4516
4517 /*
4518 * We have a problem here : there is no easy way to give a CHS
4519 * virtual geometry. We currently pretend that we have a 2 heads
4520 * 4 sectors (with a BIG number of cylinders...). This drives
4521 * dosfs just mad... ;-)
4522 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 }
4524
4525 /*
4526 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004527 * superblock, so we do not allow them on read-only arrays.
4528 * However non-MD ioctls (e.g. get-size) will still come through
4529 * here and hit the 'default' below, so only disallow
4530 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 */
NeilBrownf91de922005-11-08 21:39:36 -08004532 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4533 mddev->ro && mddev->pers) {
4534 if (mddev->ro == 2) {
4535 mddev->ro = 0;
4536 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4537 md_wakeup_thread(mddev->thread);
4538
4539 } else {
4540 err = -EROFS;
4541 goto abort_unlock;
4542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 }
4544
4545 switch (cmd)
4546 {
4547 case ADD_NEW_DISK:
4548 {
4549 mdu_disk_info_t info;
4550 if (copy_from_user(&info, argp, sizeof(info)))
4551 err = -EFAULT;
4552 else
4553 err = add_new_disk(mddev, &info);
4554 goto done_unlock;
4555 }
4556
4557 case HOT_REMOVE_DISK:
4558 err = hot_remove_disk(mddev, new_decode_dev(arg));
4559 goto done_unlock;
4560
4561 case HOT_ADD_DISK:
4562 err = hot_add_disk(mddev, new_decode_dev(arg));
4563 goto done_unlock;
4564
4565 case SET_DISK_FAULTY:
4566 err = set_disk_faulty(mddev, new_decode_dev(arg));
4567 goto done_unlock;
4568
4569 case RUN_ARRAY:
4570 err = do_md_run (mddev);
4571 goto done_unlock;
4572
NeilBrown32a76272005-06-21 17:17:14 -07004573 case SET_BITMAP_FILE:
4574 err = set_bitmap_file(mddev, (int)arg);
4575 goto done_unlock;
4576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 err = -EINVAL;
4579 goto abort_unlock;
4580 }
4581
4582done_unlock:
4583abort_unlock:
4584 mddev_unlock(mddev);
4585
4586 return err;
4587done:
4588 if (err)
4589 MD_BUG();
4590abort:
4591 return err;
4592}
4593
4594static int md_open(struct inode *inode, struct file *file)
4595{
4596 /*
4597 * Succeed if we can lock the mddev, which confirms that
4598 * it isn't being stopped right now.
4599 */
4600 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4601 int err;
4602
NeilBrownd63a5a72006-12-08 02:36:17 -08004603 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 goto out;
4605
4606 err = 0;
4607 mddev_get(mddev);
4608 mddev_unlock(mddev);
4609
4610 check_disk_change(inode->i_bdev);
4611 out:
4612 return err;
4613}
4614
4615static int md_release(struct inode *inode, struct file * file)
4616{
4617 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4618
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004619 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 mddev_put(mddev);
4621
4622 return 0;
4623}
4624
Linus Torvalds44ce62942007-05-09 18:51:36 -07004625static int md_media_changed(struct gendisk *disk)
4626{
4627 mddev_t *mddev = disk->private_data;
4628
4629 return mddev->changed;
4630}
4631
4632static int md_revalidate(struct gendisk *disk)
4633{
4634 mddev_t *mddev = disk->private_data;
4635
4636 mddev->changed = 0;
4637 return 0;
4638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639static struct block_device_operations md_fops =
4640{
4641 .owner = THIS_MODULE,
4642 .open = md_open,
4643 .release = md_release,
4644 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004645 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07004646 .media_changed = md_media_changed,
4647 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648};
4649
Adrian Bunk75c96f82005-05-05 16:16:09 -07004650static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
4652 mdk_thread_t *thread = arg;
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 /*
4655 * md_thread is a 'system-thread', it's priority should be very
4656 * high. We avoid resource deadlocks individually in each
4657 * raid personality. (RAID5 does preallocation) We also use RR and
4658 * the very same RT priority as kswapd, thus we will never get
4659 * into a priority inversion deadlock.
4660 *
4661 * we definitely have to have equal or higher priority than
4662 * bdflush, otherwise bdflush will deadlock if there are too
4663 * many dirty RAID5 blocks.
4664 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
NeilBrown6985c432005-10-19 21:23:47 -07004666 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004667 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
NeilBrown93588e22005-11-15 00:09:12 -08004669 /* We need to wait INTERRUPTIBLE so that
4670 * we don't add to the load-average.
4671 * That means we need to be sure no signals are
4672 * pending
4673 */
4674 if (signal_pending(current))
4675 flush_signals(current);
4676
4677 wait_event_interruptible_timeout
4678 (thread->wqueue,
4679 test_bit(THREAD_WAKEUP, &thread->flags)
4680 || kthread_should_stop(),
4681 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682
4683 clear_bit(THREAD_WAKEUP, &thread->flags);
4684
NeilBrown787453c2005-11-08 21:39:43 -08004685 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 return 0;
4689}
4690
4691void md_wakeup_thread(mdk_thread_t *thread)
4692{
4693 if (thread) {
4694 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4695 set_bit(THREAD_WAKEUP, &thread->flags);
4696 wake_up(&thread->wqueue);
4697 }
4698}
4699
4700mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4701 const char *name)
4702{
4703 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
NeilBrown9ffae0c2006-01-06 00:20:32 -08004705 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 if (!thread)
4707 return NULL;
4708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 init_waitqueue_head(&thread->wqueue);
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 thread->run = run;
4712 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004713 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004714 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004715 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 kfree(thread);
4717 return NULL;
4718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 return thread;
4720}
4721
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722void md_unregister_thread(mdk_thread_t *thread)
4723{
NeilBrownd28446f2005-04-16 15:26:41 -07004724 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004725
4726 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 kfree(thread);
4728}
4729
4730void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4731{
4732 if (!mddev) {
4733 MD_BUG();
4734 return;
4735 }
4736
NeilBrownb2d444d2005-11-08 21:39:31 -08004737 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 return;
NeilBrown32a76272005-06-21 17:17:14 -07004739/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4741 mdname(mddev),
4742 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4743 __builtin_return_address(0),__builtin_return_address(1),
4744 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004745*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004746 if (!mddev->pers)
4747 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 if (!mddev->pers->error_handler)
4749 return;
4750 mddev->pers->error_handler(mddev,rdev);
4751 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4752 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4753 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004754 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755}
4756
4757/* seq_file implementation /proc/mdstat */
4758
4759static void status_unused(struct seq_file *seq)
4760{
4761 int i = 0;
4762 mdk_rdev_t *rdev;
4763 struct list_head *tmp;
4764
4765 seq_printf(seq, "unused devices: ");
4766
4767 ITERATE_RDEV_PENDING(rdev,tmp) {
4768 char b[BDEVNAME_SIZE];
4769 i++;
4770 seq_printf(seq, "%s ",
4771 bdevname(rdev->bdev,b));
4772 }
4773 if (!i)
4774 seq_printf(seq, "<none>");
4775
4776 seq_printf(seq, "\n");
4777}
4778
4779
4780static void status_resync(struct seq_file *seq, mddev_t * mddev)
4781{
NeilBrown4588b422006-03-27 01:18:04 -08004782 sector_t max_blocks, resync, res;
4783 unsigned long dt, db, rt;
4784 int scale;
4785 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786
4787 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4788
4789 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4790 max_blocks = mddev->resync_max_sectors >> 1;
4791 else
4792 max_blocks = mddev->size;
4793
4794 /*
4795 * Should not happen.
4796 */
4797 if (!max_blocks) {
4798 MD_BUG();
4799 return;
4800 }
NeilBrown4588b422006-03-27 01:18:04 -08004801 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4802 * in a sector_t, and (max_blocks>>scale) will fit in a
4803 * u32, as those are the requirements for sector_div.
4804 * Thus 'scale' must be at least 10
4805 */
4806 scale = 10;
4807 if (sizeof(sector_t) > sizeof(unsigned long)) {
4808 while ( max_blocks/2 > (1ULL<<(scale+32)))
4809 scale++;
4810 }
4811 res = (resync>>scale)*1000;
4812 sector_div(res, (u32)((max_blocks>>scale)+1));
4813
4814 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 {
NeilBrown4588b422006-03-27 01:18:04 -08004816 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 seq_printf(seq, "[");
4818 for (i = 0; i < x; i++)
4819 seq_printf(seq, "=");
4820 seq_printf(seq, ">");
4821 for (i = 0; i < y; i++)
4822 seq_printf(seq, ".");
4823 seq_printf(seq, "] ");
4824 }
NeilBrown4588b422006-03-27 01:18:04 -08004825 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004826 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4827 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07004828 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
4829 "check" :
4830 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4831 "resync" : "recovery"))),
4832 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08004833 (unsigned long long) resync,
4834 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
4836 /*
4837 * We do not want to overflow, so the order of operands and
4838 * the * 100 / 100 trick are important. We do a +1 to be
4839 * safe against division by zero. We only estimate anyway.
4840 *
4841 * dt: time from mark until now
4842 * db: blocks written from mark until now
4843 * rt: remaining time
4844 */
4845 dt = ((jiffies - mddev->resync_mark) / HZ);
4846 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07004847 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
4848 - mddev->resync_mark_cnt;
4849 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
4851 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4852
NeilBrownff4e8d92006-07-10 04:44:16 -07004853 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854}
4855
4856static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4857{
4858 struct list_head *tmp;
4859 loff_t l = *pos;
4860 mddev_t *mddev;
4861
4862 if (l >= 0x10000)
4863 return NULL;
4864 if (!l--)
4865 /* header */
4866 return (void*)1;
4867
4868 spin_lock(&all_mddevs_lock);
4869 list_for_each(tmp,&all_mddevs)
4870 if (!l--) {
4871 mddev = list_entry(tmp, mddev_t, all_mddevs);
4872 mddev_get(mddev);
4873 spin_unlock(&all_mddevs_lock);
4874 return mddev;
4875 }
4876 spin_unlock(&all_mddevs_lock);
4877 if (!l--)
4878 return (void*)2;/* tail */
4879 return NULL;
4880}
4881
4882static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4883{
4884 struct list_head *tmp;
4885 mddev_t *next_mddev, *mddev = v;
4886
4887 ++*pos;
4888 if (v == (void*)2)
4889 return NULL;
4890
4891 spin_lock(&all_mddevs_lock);
4892 if (v == (void*)1)
4893 tmp = all_mddevs.next;
4894 else
4895 tmp = mddev->all_mddevs.next;
4896 if (tmp != &all_mddevs)
4897 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4898 else {
4899 next_mddev = (void*)2;
4900 *pos = 0x10000;
4901 }
4902 spin_unlock(&all_mddevs_lock);
4903
4904 if (v != (void*)1)
4905 mddev_put(mddev);
4906 return next_mddev;
4907
4908}
4909
4910static void md_seq_stop(struct seq_file *seq, void *v)
4911{
4912 mddev_t *mddev = v;
4913
4914 if (mddev && v != (void*)1 && v != (void*)2)
4915 mddev_put(mddev);
4916}
4917
NeilBrownd7603b72006-01-06 00:20:30 -08004918struct mdstat_info {
4919 int event;
4920};
4921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922static int md_seq_show(struct seq_file *seq, void *v)
4923{
4924 mddev_t *mddev = v;
4925 sector_t size;
4926 struct list_head *tmp2;
4927 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004928 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004929 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930
4931 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004932 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 seq_printf(seq, "Personalities : ");
4934 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004935 list_for_each_entry(pers, &pers_list, list)
4936 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
4938 spin_unlock(&pers_lock);
4939 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004940 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 return 0;
4942 }
4943 if (v == (void*)2) {
4944 status_unused(seq);
4945 return 0;
4946 }
4947
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004948 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004950
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4952 seq_printf(seq, "%s : %sactive", mdname(mddev),
4953 mddev->pers ? "" : "in");
4954 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004955 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004957 if (mddev->ro==2)
4958 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 seq_printf(seq, " %s", mddev->pers->name);
4960 }
4961
4962 size = 0;
4963 ITERATE_RDEV(mddev,rdev,tmp2) {
4964 char b[BDEVNAME_SIZE];
4965 seq_printf(seq, " %s[%d]",
4966 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004967 if (test_bit(WriteMostly, &rdev->flags))
4968 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004969 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 seq_printf(seq, "(F)");
4971 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004972 } else if (rdev->raid_disk < 0)
4973 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 size += rdev->size;
4975 }
4976
4977 if (!list_empty(&mddev->disks)) {
4978 if (mddev->pers)
4979 seq_printf(seq, "\n %llu blocks",
4980 (unsigned long long)mddev->array_size);
4981 else
4982 seq_printf(seq, "\n %llu blocks",
4983 (unsigned long long)size);
4984 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004985 if (mddev->persistent) {
4986 if (mddev->major_version != 0 ||
4987 mddev->minor_version != 90) {
4988 seq_printf(seq," super %d.%d",
4989 mddev->major_version,
4990 mddev->minor_version);
4991 }
4992 } else
4993 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994
4995 if (mddev->pers) {
4996 mddev->pers->status (seq, mddev);
4997 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004998 if (mddev->pers->sync_request) {
4999 if (mddev->curr_resync > 2) {
5000 status_resync (seq, mddev);
5001 seq_printf(seq, "\n ");
5002 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5003 seq_printf(seq, "\tresync=DELAYED\n ");
5004 else if (mddev->recovery_cp < MaxSector)
5005 seq_printf(seq, "\tresync=PENDING\n ");
5006 }
NeilBrown32a76272005-06-21 17:17:14 -07005007 } else
5008 seq_printf(seq, "\n ");
5009
5010 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005011 unsigned long chunk_kb;
5012 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005013 spin_lock_irqsave(&bitmap->lock, flags);
5014 chunk_kb = bitmap->chunksize >> 10;
5015 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5016 "%lu%s chunk",
5017 bitmap->pages - bitmap->missing_pages,
5018 bitmap->pages,
5019 (bitmap->pages - bitmap->missing_pages)
5020 << (PAGE_SHIFT - 10),
5021 chunk_kb ? chunk_kb : bitmap->chunksize,
5022 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005023 if (bitmap->file) {
5024 seq_printf(seq, ", file: ");
Josef Sipekc649bb92006-12-08 02:37:19 -08005025 seq_path(seq, bitmap->file->f_path.mnt,
5026 bitmap->file->f_path.dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005027 }
NeilBrown78d742d2005-06-21 17:17:15 -07005028
NeilBrown32a76272005-06-21 17:17:14 -07005029 seq_printf(seq, "\n");
5030 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 }
5032
5033 seq_printf(seq, "\n");
5034 }
5035 mddev_unlock(mddev);
5036
5037 return 0;
5038}
5039
5040static struct seq_operations md_seq_ops = {
5041 .start = md_seq_start,
5042 .next = md_seq_next,
5043 .stop = md_seq_stop,
5044 .show = md_seq_show,
5045};
5046
5047static int md_seq_open(struct inode *inode, struct file *file)
5048{
5049 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005050 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5051 if (mi == NULL)
5052 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
5054 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005055 if (error)
5056 kfree(mi);
5057 else {
5058 struct seq_file *p = file->private_data;
5059 p->private = mi;
5060 mi->event = atomic_read(&md_event_count);
5061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 return error;
5063}
5064
NeilBrownd7603b72006-01-06 00:20:30 -08005065static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5066{
5067 struct seq_file *m = filp->private_data;
5068 struct mdstat_info *mi = m->private;
5069 int mask;
5070
5071 poll_wait(filp, &md_event_waiters, wait);
5072
5073 /* always allow read */
5074 mask = POLLIN | POLLRDNORM;
5075
5076 if (mi->event != atomic_read(&md_event_count))
5077 mask |= POLLERR | POLLPRI;
5078 return mask;
5079}
5080
Arjan van de Venfa027c22007-02-12 00:55:33 -08005081static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005082 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 .open = md_seq_open,
5084 .read = seq_read,
5085 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005086 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005087 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088};
5089
NeilBrown2604b702006-01-06 00:20:36 -08005090int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005093 list_add_tail(&p->list, &pers_list);
5094 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 spin_unlock(&pers_lock);
5096 return 0;
5097}
5098
NeilBrown2604b702006-01-06 00:20:36 -08005099int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100{
NeilBrown2604b702006-01-06 00:20:36 -08005101 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005103 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 spin_unlock(&pers_lock);
5105 return 0;
5106}
5107
5108static int is_mddev_idle(mddev_t *mddev)
5109{
5110 mdk_rdev_t * rdev;
5111 struct list_head *tmp;
5112 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005113 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114
5115 idle = 1;
5116 ITERATE_RDEV(mddev,rdev,tmp) {
5117 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005118 curr_events = disk_stat_read(disk, sectors[0]) +
5119 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005121 /* sync IO will cause sync_io to increase before the disk_stats
5122 * as sync_io is counted when a request starts, and
5123 * disk_stats is counted when it completes.
5124 * So resync activity will cause curr_events to be smaller than
5125 * when there was no such activity.
5126 * non-sync IO will cause disk_stat to increase without
5127 * increasing sync_io so curr_events will (eventually)
5128 * be larger than it was before. Once it becomes
5129 * substantially larger, the test below will cause
5130 * the array to appear non-idle, and resync will slow
5131 * down.
5132 * If there is a lot of outstanding resync activity when
5133 * we set last_event to curr_events, then all that activity
5134 * completing might cause the array to appear non-idle
5135 * and resync will be slowed down even though there might
5136 * not have been non-resync activity. This will only
5137 * happen once though. 'last_events' will soon reflect
5138 * the state where there is little or no outstanding
5139 * resync requests, and further resync activity will
5140 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005141 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005143 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 rdev->last_events = curr_events;
5145 idle = 0;
5146 }
5147 }
5148 return idle;
5149}
5150
5151void md_done_sync(mddev_t *mddev, int blocks, int ok)
5152{
5153 /* another "blocks" (512byte) blocks have been synced */
5154 atomic_sub(blocks, &mddev->recovery_active);
5155 wake_up(&mddev->recovery_wait);
5156 if (!ok) {
5157 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5158 md_wakeup_thread(mddev->thread);
5159 // stop recovery, signal do_sync ....
5160 }
5161}
5162
5163
NeilBrown06d91a52005-06-21 17:17:12 -07005164/* md_write_start(mddev, bi)
5165 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005166 * in superblock) before writing, schedule a superblock update
5167 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005168 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005169void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170{
NeilBrown06d91a52005-06-21 17:17:12 -07005171 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005172 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005173
NeilBrownf91de922005-11-08 21:39:36 -08005174 BUG_ON(mddev->ro == 1);
5175 if (mddev->ro == 2) {
5176 /* need to switch to read/write */
5177 mddev->ro = 0;
5178 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5179 md_wakeup_thread(mddev->thread);
5180 }
NeilBrown06d91a52005-06-21 17:17:12 -07005181 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005182 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005183 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005184 if (mddev->in_sync) {
5185 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005186 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005187 md_wakeup_thread(mddev->thread);
5188 }
NeilBrowna9701a32005-11-08 21:39:34 -08005189 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005190 }
NeilBrown850b2b42006-10-03 01:15:46 -07005191 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192}
5193
5194void md_write_end(mddev_t *mddev)
5195{
5196 if (atomic_dec_and_test(&mddev->writes_pending)) {
5197 if (mddev->safemode == 2)
5198 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005199 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5201 }
5202}
5203
NeilBrown2a2275d2007-01-26 00:57:11 -08005204/* md_allow_write(mddev)
5205 * Calling this ensures that the array is marked 'active' so that writes
5206 * may proceed without blocking. It is important to call this before
5207 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5208 * Must be called with mddev_lock held.
5209 */
5210void md_allow_write(mddev_t *mddev)
5211{
5212 if (!mddev->pers)
5213 return;
5214 if (mddev->ro)
5215 return;
5216
5217 spin_lock_irq(&mddev->write_lock);
5218 if (mddev->in_sync) {
5219 mddev->in_sync = 0;
5220 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5221 if (mddev->safemode_delay &&
5222 mddev->safemode == 0)
5223 mddev->safemode = 1;
5224 spin_unlock_irq(&mddev->write_lock);
5225 md_update_sb(mddev, 0);
5226 } else
5227 spin_unlock_irq(&mddev->write_lock);
5228}
5229EXPORT_SYMBOL_GPL(md_allow_write);
5230
Adrian Bunk75c96f82005-05-05 16:16:09 -07005231static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
5233#define SYNC_MARKS 10
5234#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005235void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236{
5237 mddev_t *mddev2;
5238 unsigned int currspeed = 0,
5239 window;
NeilBrown57afd892005-06-21 17:17:13 -07005240 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 unsigned long mark[SYNC_MARKS];
5242 sector_t mark_cnt[SYNC_MARKS];
5243 int last_mark,m;
5244 struct list_head *tmp;
5245 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005246 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005247 struct list_head *rtmp;
5248 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005249 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
5251 /* just incase thread restarts... */
5252 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5253 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005254 if (mddev->ro) /* never try to sync a read-only array */
5255 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256
NeilBrown61df9d92006-10-03 01:15:57 -07005257 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5258 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5259 desc = "data-check";
5260 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5261 desc = "requested-resync";
5262 else
5263 desc = "resync";
5264 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5265 desc = "reshape";
5266 else
5267 desc = "recovery";
5268
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 /* we overload curr_resync somewhat here.
5270 * 0 == not engaged in resync at all
5271 * 2 == checking that there is no conflict with another sync
5272 * 1 == like 2, but have yielded to allow conflicting resync to
5273 * commense
5274 * other == active in resync - this many blocks
5275 *
5276 * Before starting a resync we must have set curr_resync to
5277 * 2, and then checked that every "conflicting" array has curr_resync
5278 * less than ours. When we find one that is the same or higher
5279 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5280 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5281 * This will mean we have to start checking from the beginning again.
5282 *
5283 */
5284
5285 do {
5286 mddev->curr_resync = 2;
5287
5288 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005289 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005290 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 goto skip;
5292 }
5293 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (mddev2 == mddev)
5295 continue;
5296 if (mddev2->curr_resync &&
5297 match_mddev_units(mddev,mddev2)) {
5298 DEFINE_WAIT(wq);
5299 if (mddev < mddev2 && mddev->curr_resync == 2) {
5300 /* arbitrarily yield */
5301 mddev->curr_resync = 1;
5302 wake_up(&resync_wait);
5303 }
5304 if (mddev > mddev2 && mddev->curr_resync == 1)
5305 /* no need to wait here, we can wait the next
5306 * time 'round when curr_resync == 2
5307 */
5308 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005309 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5310 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005311 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005312 printk(KERN_INFO "md: delaying %s of %s"
5313 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005315 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 mddev_put(mddev2);
5317 schedule();
5318 finish_wait(&resync_wait, &wq);
5319 goto try_again;
5320 }
5321 finish_wait(&resync_wait, &wq);
5322 }
5323 }
5324 } while (mddev->curr_resync < 2);
5325
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005326 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005327 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005329 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 */
5331 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005332 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005333 /* we don't use the checkpoint if there's a bitmap */
5334 if (!mddev->bitmap &&
5335 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5336 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005337 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5338 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005339 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 /* recovery follows the physical size of devices */
5341 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005342 j = MaxSector;
5343 ITERATE_RDEV(mddev,rdev,rtmp)
5344 if (rdev->raid_disk >= 0 &&
5345 !test_bit(Faulty, &rdev->flags) &&
5346 !test_bit(In_sync, &rdev->flags) &&
5347 rdev->recovery_offset < j)
5348 j = rdev->recovery_offset;
5349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
NeilBrown61df9d92006-10-03 01:15:57 -07005351 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5352 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5353 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005354 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005355 "(but not more than %d KB/sec) for %s.\n",
5356 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
5358 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005359
NeilBrown57afd892005-06-21 17:17:13 -07005360 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 for (m = 0; m < SYNC_MARKS; m++) {
5362 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005363 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 }
5365 last_mark = 0;
5366 mddev->resync_mark = mark[last_mark];
5367 mddev->resync_mark_cnt = mark_cnt[last_mark];
5368
5369 /*
5370 * Tune reconstruction:
5371 */
5372 window = 32*(PAGE_SIZE/512);
5373 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5374 window/2,(unsigned long long) max_sectors/2);
5375
5376 atomic_set(&mddev->recovery_active, 0);
5377 init_waitqueue_head(&mddev->recovery_wait);
5378 last_check = 0;
5379
5380 if (j>2) {
5381 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005382 "md: resuming %s of %s from checkpoint.\n",
5383 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 mddev->curr_resync = j;
5385 }
5386
5387 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005388 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389
NeilBrown57afd892005-06-21 17:17:13 -07005390 skipped = 0;
5391 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08005392 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005393 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5395 goto out;
5396 }
NeilBrown57afd892005-06-21 17:17:13 -07005397
5398 if (!skipped) { /* actual IO requested */
5399 io_sectors += sectors;
5400 atomic_add(sectors, &mddev->recovery_active);
5401 }
5402
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 j += sectors;
5404 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005405 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005406 if (last_check == 0)
5407 /* this is the earliers that rebuilt will be
5408 * visible in /proc/mdstat
5409 */
5410 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005411
5412 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 continue;
5414
NeilBrown57afd892005-06-21 17:17:13 -07005415 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416
5417 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5418 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5419 break;
5420
5421 repeat:
5422 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5423 /* step marks */
5424 int next = (last_mark+1) % SYNC_MARKS;
5425
5426 mddev->resync_mark = mark[next];
5427 mddev->resync_mark_cnt = mark_cnt[next];
5428 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005429 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 last_mark = next;
5431 }
5432
5433
NeilBrown787453c2005-11-08 21:39:43 -08005434 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 /*
5436 * got a signal, exit.
5437 */
5438 printk(KERN_INFO
5439 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5441 goto out;
5442 }
5443
5444 /*
5445 * this loop exits only if either when we are slower than
5446 * the 'hard' speed limit, or the system was IO-idle for
5447 * a jiffy.
5448 * the system might be non-idle CPU-wise, but we only care
5449 * about not overloading the IO subsystem. (things like an
5450 * e2fsck being done on the RAID array should execute fast)
5451 */
5452 mddev->queue->unplug_fn(mddev->queue);
5453 cond_resched();
5454
NeilBrown57afd892005-06-21 17:17:13 -07005455 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5456 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457
NeilBrown88202a02006-01-06 00:21:36 -08005458 if (currspeed > speed_min(mddev)) {
5459 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005461 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 goto repeat;
5463 }
5464 }
5465 }
NeilBrown61df9d92006-10-03 01:15:57 -07005466 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 /*
5468 * this also signals 'finished resyncing' to md_stop
5469 */
5470 out:
5471 mddev->queue->unplug_fn(mddev->queue);
5472
5473 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5474
5475 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005476 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477
5478 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005479 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005480 mddev->curr_resync > 2) {
5481 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5482 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5483 if (mddev->curr_resync >= mddev->recovery_cp) {
5484 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005485 "md: checkpointing %s of %s.\n",
5486 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005487 mddev->recovery_cp = mddev->curr_resync;
5488 }
5489 } else
5490 mddev->recovery_cp = MaxSector;
5491 } else {
5492 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5493 mddev->curr_resync = MaxSector;
5494 ITERATE_RDEV(mddev,rdev,rtmp)
5495 if (rdev->raid_disk >= 0 &&
5496 !test_bit(Faulty, &rdev->flags) &&
5497 !test_bit(In_sync, &rdev->flags) &&
5498 rdev->recovery_offset < mddev->curr_resync)
5499 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 }
NeilBrown17571282006-12-10 02:20:52 -08005502 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 skip:
5505 mddev->curr_resync = 0;
5506 wake_up(&resync_wait);
5507 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5508 md_wakeup_thread(mddev->thread);
5509}
NeilBrown29269552006-03-27 01:18:10 -08005510EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511
5512
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005513static int remove_and_add_spares(mddev_t *mddev)
5514{
5515 mdk_rdev_t *rdev;
5516 struct list_head *rtmp;
5517 int spares = 0;
5518
5519 ITERATE_RDEV(mddev,rdev,rtmp)
5520 if (rdev->raid_disk >= 0 &&
5521 (test_bit(Faulty, &rdev->flags) ||
5522 ! test_bit(In_sync, &rdev->flags)) &&
5523 atomic_read(&rdev->nr_pending)==0) {
5524 if (mddev->pers->hot_remove_disk(
5525 mddev, rdev->raid_disk)==0) {
5526 char nm[20];
5527 sprintf(nm,"rd%d", rdev->raid_disk);
5528 sysfs_remove_link(&mddev->kobj, nm);
5529 rdev->raid_disk = -1;
5530 }
5531 }
5532
5533 if (mddev->degraded) {
5534 ITERATE_RDEV(mddev,rdev,rtmp)
5535 if (rdev->raid_disk < 0
5536 && !test_bit(Faulty, &rdev->flags)) {
5537 rdev->recovery_offset = 0;
5538 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5539 char nm[20];
5540 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005541 if (sysfs_create_link(&mddev->kobj,
5542 &rdev->kobj, nm))
5543 printk(KERN_WARNING
5544 "md: cannot register "
5545 "%s for %s\n",
5546 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005547 spares++;
5548 md_new_event(mddev);
5549 } else
5550 break;
5551 }
5552 }
5553 return spares;
5554}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555/*
5556 * This routine is regularly called by all per-raid-array threads to
5557 * deal with generic issues like resync and super-block update.
5558 * Raid personalities that don't have a thread (linear/raid0) do not
5559 * need this as they never do any recovery or update the superblock.
5560 *
5561 * It does not do any resync itself, but rather "forks" off other threads
5562 * to do that as needed.
5563 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5564 * "->recovery" and create a thread at ->sync_thread.
5565 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5566 * and wakeups up this thread which will reap the thread and finish up.
5567 * This thread also removes any faulty devices (with nr_pending == 0).
5568 *
5569 * The overall approach is:
5570 * 1/ if the superblock needs updating, update it.
5571 * 2/ If a recovery thread is running, don't do anything else.
5572 * 3/ If recovery has finished, clean up, possibly marking spares active.
5573 * 4/ If there are any faulty devices, remove them.
5574 * 5/ If array is degraded, try to add spares devices
5575 * 6/ If array has spares or is not in-sync, start a resync thread.
5576 */
5577void md_check_recovery(mddev_t *mddev)
5578{
5579 mdk_rdev_t *rdev;
5580 struct list_head *rtmp;
5581
5582
NeilBrown5f404022005-06-21 17:17:16 -07005583 if (mddev->bitmap)
5584 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585
5586 if (mddev->ro)
5587 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005588
5589 if (signal_pending(current)) {
5590 if (mddev->pers->sync_request) {
5591 printk(KERN_INFO "md: %s in immediate safe mode\n",
5592 mdname(mddev));
5593 mddev->safemode = 2;
5594 }
5595 flush_signals(current);
5596 }
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 if ( ! (
NeilBrown850b2b42006-10-03 01:15:46 -07005599 mddev->flags ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005601 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5602 (mddev->safemode == 1) ||
5603 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5604 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 ))
5606 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005607
NeilBrowndf5b89b2006-03-27 01:18:20 -08005608 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005609 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005610
NeilBrowna9701a32005-11-08 21:39:34 -08005611 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005612 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5613 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5614 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07005615 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005616 }
5617 if (mddev->safemode == 1)
5618 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005619 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005620
NeilBrown850b2b42006-10-03 01:15:46 -07005621 if (mddev->flags)
5622 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005623
NeilBrown06d91a52005-06-21 17:17:12 -07005624
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5626 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5627 /* resync/recovery still happening */
5628 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5629 goto unlock;
5630 }
5631 if (mddev->sync_thread) {
5632 /* resync has finished, collect result */
5633 md_unregister_thread(mddev->sync_thread);
5634 mddev->sync_thread = NULL;
5635 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5636 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5637 /* success...*/
5638 /* activate any spares */
5639 mddev->pers->spare_active(mddev);
5640 }
NeilBrown850b2b42006-10-03 01:15:46 -07005641 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005642
5643 /* if array is no-longer degraded, then any saved_raid_disk
5644 * information must be scrapped
5645 */
5646 if (!mddev->degraded)
5647 ITERATE_RDEV(mddev,rdev,rtmp)
5648 rdev->saved_raid_disk = -1;
5649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 mddev->recovery = 0;
5651 /* flag recovery needed just to double check */
5652 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005653 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 goto unlock;
5655 }
NeilBrown24dd4692005-11-08 21:39:26 -08005656 /* Clear some bits that don't mean anything, but
5657 * might be left set
5658 */
5659 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5660 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5661 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5662 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005664 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5665 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 /* no recovery is running.
5667 * remove any failed drives, then
5668 * add spares if possible.
5669 * Spare are also removed and re-added, to allow
5670 * the personality to fail the re-add.
5671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005673 if (mddev->reshape_position != MaxSector) {
5674 if (mddev->pers->check_reshape(mddev) != 0)
5675 /* Cannot proceed */
5676 goto unlock;
5677 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5678 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005679 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5680 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5681 } else if (mddev->recovery_cp < MaxSector) {
5682 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5683 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5684 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005686
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 if (mddev->pers->sync_request) {
5688 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005689 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5690 /* We are adding a device or devices to an array
5691 * which has the bitmap stored on all devices.
5692 * So make sure all bitmap pages get written
5693 */
5694 bitmap_write_all(mddev->bitmap);
5695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 mddev->sync_thread = md_register_thread(md_do_sync,
5697 mddev,
5698 "%s_resync");
5699 if (!mddev->sync_thread) {
5700 printk(KERN_ERR "%s: could not start resync"
5701 " thread...\n",
5702 mdname(mddev));
5703 /* leave the spares where they are, it shouldn't hurt */
5704 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005705 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005707 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 }
5709 unlock:
5710 mddev_unlock(mddev);
5711 }
5712}
5713
Adrian Bunk75c96f82005-05-05 16:16:09 -07005714static int md_notify_reboot(struct notifier_block *this,
5715 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716{
5717 struct list_head *tmp;
5718 mddev_t *mddev;
5719
5720 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5721
5722 printk(KERN_INFO "md: stopping all md devices.\n");
5723
5724 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005725 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005727 mddev_unlock(mddev);
5728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 /*
5730 * certain more exotic SCSI devices are known to be
5731 * volatile wrt too early system reboots. While the
5732 * right place to handle this issue is the given
5733 * driver, we do want to have a safe RAID driver ...
5734 */
5735 mdelay(1000*1);
5736 }
5737 return NOTIFY_DONE;
5738}
5739
Adrian Bunk75c96f82005-05-05 16:16:09 -07005740static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 .notifier_call = md_notify_reboot,
5742 .next = NULL,
5743 .priority = INT_MAX, /* before any real devices */
5744};
5745
5746static void md_geninit(void)
5747{
5748 struct proc_dir_entry *p;
5749
5750 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5751
5752 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5753 if (p)
5754 p->proc_fops = &md_seq_fops;
5755}
5756
Adrian Bunk75c96f82005-05-05 16:16:09 -07005757static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 if (register_blkdev(MAJOR_NR, "md"))
5760 return -1;
5761 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5762 unregister_blkdev(MAJOR_NR, "md");
5763 return -1;
5764 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005765 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5766 md_probe, NULL, NULL);
5767 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 md_probe, NULL, NULL);
5769
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005771 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772
5773 md_geninit();
5774 return (0);
5775}
5776
5777
5778#ifndef MODULE
5779
5780/*
5781 * Searches all registered partitions for autorun RAID arrays
5782 * at boot time.
5783 */
5784static dev_t detected_devices[128];
5785static int dev_cnt;
5786
5787void md_autodetect_dev(dev_t dev)
5788{
5789 if (dev_cnt >= 0 && dev_cnt < 127)
5790 detected_devices[dev_cnt++] = dev;
5791}
5792
5793
5794static void autostart_arrays(int part)
5795{
5796 mdk_rdev_t *rdev;
5797 int i;
5798
5799 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5800
5801 for (i = 0; i < dev_cnt; i++) {
5802 dev_t dev = detected_devices[i];
5803
NeilBrowndf968c42007-07-17 04:06:11 -07005804 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 if (IS_ERR(rdev))
5806 continue;
5807
NeilBrownb2d444d2005-11-08 21:39:31 -08005808 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 MD_BUG();
5810 continue;
5811 }
5812 list_add(&rdev->same_set, &pending_raid_disks);
5813 }
5814 dev_cnt = 0;
5815
5816 autorun_devices(part);
5817}
5818
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005819#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820
5821static __exit void md_exit(void)
5822{
5823 mddev_t *mddev;
5824 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07005825
NeilBrowne8703fe2006-10-03 01:15:59 -07005826 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
5827 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
5829 unregister_blkdev(MAJOR_NR,"md");
5830 unregister_blkdev(mdp_major, "mdp");
5831 unregister_reboot_notifier(&md_notifier);
5832 unregister_sysctl_table(raid_table_header);
5833 remove_proc_entry("mdstat", NULL);
5834 ITERATE_MDDEV(mddev,tmp) {
5835 struct gendisk *disk = mddev->gendisk;
5836 if (!disk)
5837 continue;
5838 export_array(mddev);
5839 del_gendisk(disk);
5840 put_disk(disk);
5841 mddev->gendisk = NULL;
5842 mddev_put(mddev);
5843 }
5844}
5845
Dan Williams685784a2007-07-09 11:56:42 -07005846subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847module_exit(md_exit)
5848
NeilBrownf91de922005-11-08 21:39:36 -08005849static int get_ro(char *buffer, struct kernel_param *kp)
5850{
5851 return sprintf(buffer, "%d", start_readonly);
5852}
5853static int set_ro(const char *val, struct kernel_param *kp)
5854{
5855 char *e;
5856 int num = simple_strtoul(val, &e, 10);
5857 if (*val && (*e == '\0' || *e == '\n')) {
5858 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005859 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005860 }
5861 return -EINVAL;
5862}
5863
NeilBrown80ca3a42006-07-10 04:44:18 -07005864module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5865module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005866
NeilBrownf91de922005-11-08 21:39:36 -08005867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868EXPORT_SYMBOL(register_md_personality);
5869EXPORT_SYMBOL(unregister_md_personality);
5870EXPORT_SYMBOL(md_error);
5871EXPORT_SYMBOL(md_done_sync);
5872EXPORT_SYMBOL(md_write_start);
5873EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874EXPORT_SYMBOL(md_register_thread);
5875EXPORT_SYMBOL(md_unregister_thread);
5876EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877EXPORT_SYMBOL(md_check_recovery);
5878MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005879MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005880MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);