blob: f19b874753a9c4aec3802b557ab05100f387ff61 [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>
36#include <linux/config.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>
42#include <linux/devfs_fs_kernel.h>
43#include <linux/buffer_head.h> /* for invalidate_bdev */
44#include <linux/suspend.h>
NeilBrownd7603b72006-01-06 00:20:30 -080045#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080046#include <linux/mutex.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
75/*
76 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
77 * is 1000 KB/sec, so the extra system load does not show up that much.
78 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070079 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * subsystem is idle. There is also an 'absolute maximum' reconstruction
81 * speed limit - in case reconstruction slows down your system despite
82 * idle IO detection.
83 *
84 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080085 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
87
88static int sysctl_speed_limit_min = 1000;
89static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080090static inline int speed_min(mddev_t *mddev)
91{
92 return mddev->sync_speed_min ?
93 mddev->sync_speed_min : sysctl_speed_limit_min;
94}
95
96static inline int speed_max(mddev_t *mddev)
97{
98 return mddev->sync_speed_max ?
99 mddev->sync_speed_max : sysctl_speed_limit_max;
100}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102static struct ctl_table_header *raid_table_header;
103
104static ctl_table raid_table[] = {
105 {
106 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
107 .procname = "speed_limit_min",
108 .data = &sysctl_speed_limit_min,
109 .maxlen = sizeof(int),
110 .mode = 0644,
111 .proc_handler = &proc_dointvec,
112 },
113 {
114 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
115 .procname = "speed_limit_max",
116 .data = &sysctl_speed_limit_max,
117 .maxlen = sizeof(int),
118 .mode = 0644,
119 .proc_handler = &proc_dointvec,
120 },
121 { .ctl_name = 0 }
122};
123
124static ctl_table raid_dir_table[] = {
125 {
126 .ctl_name = DEV_RAID,
127 .procname = "raid",
128 .maxlen = 0,
129 .mode = 0555,
130 .child = raid_table,
131 },
132 { .ctl_name = 0 }
133};
134
135static ctl_table raid_root_table[] = {
136 {
137 .ctl_name = CTL_DEV,
138 .procname = "dev",
139 .maxlen = 0,
140 .mode = 0555,
141 .child = raid_dir_table,
142 },
143 { .ctl_name = 0 }
144};
145
146static struct block_device_operations md_fops;
147
NeilBrownf91de922005-11-08 21:39:36 -0800148static int start_readonly;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150/*
NeilBrownd7603b72006-01-06 00:20:30 -0800151 * We have a system wide 'event count' that is incremented
152 * on any 'interesting' event, and readers of /proc/mdstat
153 * can use 'poll' or 'select' to find out when the event
154 * count increases.
155 *
156 * Events are:
157 * start array, stop array, error, add device, remove device,
158 * start build, activate spare
159 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800160static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800161static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800162void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800163{
164 atomic_inc(&md_event_count);
165 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100166 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800167}
NeilBrown29269552006-03-27 01:18:10 -0800168EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800169
NeilBrownc331eb02006-05-30 21:27:13 -0700170/* Alternate version that can be called from interrupts
171 * when calling sysfs_notify isn't needed.
172 */
173void md_new_event_inintr(mddev_t *mddev)
174{
175 atomic_inc(&md_event_count);
176 wake_up(&md_event_waiters);
177}
178
NeilBrownd7603b72006-01-06 00:20:30 -0800179/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Enables to iterate over all existing md arrays
181 * all_mddevs_lock protects this list.
182 */
183static LIST_HEAD(all_mddevs);
184static DEFINE_SPINLOCK(all_mddevs_lock);
185
186
187/*
188 * iterates through all used mddevs in the system.
189 * We take care to grab the all_mddevs_lock whenever navigating
190 * the list, and to always hold a refcount when unlocked.
191 * Any code which breaks out of this loop while own
192 * a reference to the current mddev and must mddev_put it.
193 */
194#define ITERATE_MDDEV(mddev,tmp) \
195 \
196 for (({ spin_lock(&all_mddevs_lock); \
197 tmp = all_mddevs.next; \
198 mddev = NULL;}); \
199 ({ if (tmp != &all_mddevs) \
200 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
201 spin_unlock(&all_mddevs_lock); \
202 if (mddev) mddev_put(mddev); \
203 mddev = list_entry(tmp, mddev_t, all_mddevs); \
204 tmp != &all_mddevs;}); \
205 ({ spin_lock(&all_mddevs_lock); \
206 tmp = tmp->next;}) \
207 )
208
209
210static int md_fail_request (request_queue_t *q, struct bio *bio)
211{
212 bio_io_error(bio, bio->bi_size);
213 return 0;
214}
215
216static inline mddev_t *mddev_get(mddev_t *mddev)
217{
218 atomic_inc(&mddev->active);
219 return mddev;
220}
221
222static void mddev_put(mddev_t *mddev)
223{
224 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
225 return;
226 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
227 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800228 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500229 blk_cleanup_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800230 kobject_unregister(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800231 } else
232 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235static mddev_t * mddev_find(dev_t unit)
236{
237 mddev_t *mddev, *new = NULL;
238
239 retry:
240 spin_lock(&all_mddevs_lock);
241 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
242 if (mddev->unit == unit) {
243 mddev_get(mddev);
244 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700245 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return mddev;
247 }
248
249 if (new) {
250 list_add(&new->all_mddevs, &all_mddevs);
251 spin_unlock(&all_mddevs_lock);
252 return new;
253 }
254 spin_unlock(&all_mddevs_lock);
255
NeilBrown9ffae0c2006-01-06 00:20:32 -0800256 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!new)
258 return NULL;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 new->unit = unit;
261 if (MAJOR(unit) == MD_MAJOR)
262 new->md_minor = MINOR(unit);
263 else
264 new->md_minor = MINOR(unit) >> MdpMinorShift;
265
NeilBrowndf5b89b2006-03-27 01:18:20 -0800266 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 INIT_LIST_HEAD(&new->disks);
268 INIT_LIST_HEAD(&new->all_mddevs);
269 init_timer(&new->safemode_timer);
270 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700271 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700272 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 new->queue = blk_alloc_queue(GFP_KERNEL);
275 if (!new->queue) {
276 kfree(new);
277 return NULL;
278 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800279 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 blk_queue_make_request(new->queue, md_fail_request);
282
283 goto retry;
284}
285
286static inline int mddev_lock(mddev_t * mddev)
287{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800288 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static inline int mddev_trylock(mddev_t * mddev)
292{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800293 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296static inline void mddev_unlock(mddev_t * mddev)
297{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800298 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
NeilBrown005eca52005-08-22 13:11:08 -0700300 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
NeilBrown2989ddb2006-01-06 00:20:43 -0800303static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 mdk_rdev_t * rdev;
306 struct list_head *tmp;
307
308 ITERATE_RDEV(mddev,rdev,tmp) {
309 if (rdev->desc_nr == nr)
310 return rdev;
311 }
312 return NULL;
313}
314
315static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
316{
317 struct list_head *tmp;
318 mdk_rdev_t *rdev;
319
320 ITERATE_RDEV(mddev,rdev,tmp) {
321 if (rdev->bdev->bd_dev == dev)
322 return rdev;
323 }
324 return NULL;
325}
326
NeilBrownd9d166c2006-01-06 00:20:51 -0800327static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800328{
329 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800330 list_for_each_entry(pers, &pers_list, list) {
331 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800332 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800333 if (strcmp(pers->name, clevel)==0)
334 return pers;
335 }
NeilBrown2604b702006-01-06 00:20:36 -0800336 return NULL;
337}
338
Jesper Juhl77933d72005-07-27 11:46:09 -0700339static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
342 return MD_NEW_SIZE_BLOCKS(size);
343}
344
345static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
346{
347 sector_t size;
348
349 size = rdev->sb_offset;
350
351 if (chunk_size)
352 size &= ~((sector_t)chunk_size/1024 - 1);
353 return size;
354}
355
356static int alloc_disk_sb(mdk_rdev_t * rdev)
357{
358 if (rdev->sb_page)
359 MD_BUG();
360
361 rdev->sb_page = alloc_page(GFP_KERNEL);
362 if (!rdev->sb_page) {
363 printk(KERN_ALERT "md: out of memory.\n");
364 return -EINVAL;
365 }
366
367 return 0;
368}
369
370static void free_disk_sb(mdk_rdev_t * rdev)
371{
372 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800373 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 rdev->sb_loaded = 0;
375 rdev->sb_page = NULL;
376 rdev->sb_offset = 0;
377 rdev->size = 0;
378 }
379}
380
381
NeilBrown7bfa19f2005-06-21 17:17:28 -0700382static int super_written(struct bio *bio, unsigned int bytes_done, int error)
383{
384 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800385 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700386 if (bio->bi_size)
387 return 1;
388
389 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800390 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700391
NeilBrowna9701a32005-11-08 21:39:34 -0800392 if (atomic_dec_and_test(&mddev->pending_writes))
393 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700394 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700395 return 0;
396}
397
NeilBrowna9701a32005-11-08 21:39:34 -0800398static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
399{
400 struct bio *bio2 = bio->bi_private;
401 mdk_rdev_t *rdev = bio2->bi_private;
402 mddev_t *mddev = rdev->mddev;
403 if (bio->bi_size)
404 return 1;
405
406 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
407 error == -EOPNOTSUPP) {
408 unsigned long flags;
409 /* barriers don't appear to be supported :-( */
410 set_bit(BarriersNotsupp, &rdev->flags);
411 mddev->barriers_work = 0;
412 spin_lock_irqsave(&mddev->write_lock, flags);
413 bio2->bi_next = mddev->biolist;
414 mddev->biolist = bio2;
415 spin_unlock_irqrestore(&mddev->write_lock, flags);
416 wake_up(&mddev->sb_wait);
417 bio_put(bio);
418 return 0;
419 }
420 bio_put(bio2);
421 bio->bi_private = rdev;
422 return super_written(bio, bytes_done, error);
423}
424
NeilBrown7bfa19f2005-06-21 17:17:28 -0700425void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
426 sector_t sector, int size, struct page *page)
427{
428 /* write first size bytes of page to sector of rdev
429 * Increment mddev->pending_writes before returning
430 * and decrement it on completion, waking up sb_wait
431 * if zero is reached.
432 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800433 *
434 * As we might need to resubmit the request if BIO_RW_BARRIER
435 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700436 */
437 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800438 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700439
440 bio->bi_bdev = rdev->bdev;
441 bio->bi_sector = sector;
442 bio_add_page(bio, page, size, 0);
443 bio->bi_private = rdev;
444 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800445 bio->bi_rw = rw;
446
NeilBrown7bfa19f2005-06-21 17:17:28 -0700447 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800448 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
449 struct bio *rbio;
450 rw |= (1<<BIO_RW_BARRIER);
451 rbio = bio_clone(bio, GFP_NOIO);
452 rbio->bi_private = bio;
453 rbio->bi_end_io = super_written_barrier;
454 submit_bio(rw, rbio);
455 } else
456 submit_bio(rw, bio);
457}
458
459void md_super_wait(mddev_t *mddev)
460{
461 /* wait for all superblock writes that were scheduled to complete.
462 * if any had to be retried (due to BARRIER problems), retry them
463 */
464 DEFINE_WAIT(wq);
465 for(;;) {
466 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
467 if (atomic_read(&mddev->pending_writes)==0)
468 break;
469 while (mddev->biolist) {
470 struct bio *bio;
471 spin_lock_irq(&mddev->write_lock);
472 bio = mddev->biolist;
473 mddev->biolist = bio->bi_next ;
474 bio->bi_next = NULL;
475 spin_unlock_irq(&mddev->write_lock);
476 submit_bio(bio->bi_rw, bio);
477 }
478 schedule();
479 }
480 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700481}
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
484{
485 if (bio->bi_size)
486 return 1;
487
488 complete((struct completion*)bio->bi_private);
489 return 0;
490}
491
NeilBrowna654b9d82005-06-21 17:17:27 -0700492int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct page *page, int rw)
494{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700495 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 struct completion event;
497 int ret;
498
499 rw |= (1 << BIO_RW_SYNC);
500
501 bio->bi_bdev = bdev;
502 bio->bi_sector = sector;
503 bio_add_page(bio, page, size, 0);
504 init_completion(&event);
505 bio->bi_private = &event;
506 bio->bi_end_io = bi_complete;
507 submit_bio(rw, bio);
508 wait_for_completion(&event);
509
510 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
511 bio_put(bio);
512 return ret;
513}
NeilBrowna8745db2006-01-06 00:20:34 -0800514EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
NeilBrown0002b272005-09-09 16:23:53 -0700516static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 char b[BDEVNAME_SIZE];
519 if (!rdev->sb_page) {
520 MD_BUG();
521 return -EINVAL;
522 }
523 if (rdev->sb_loaded)
524 return 0;
525
526
NeilBrown0002b272005-09-09 16:23:53 -0700527 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 goto fail;
529 rdev->sb_loaded = 1;
530 return 0;
531
532fail:
533 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
534 bdevname(rdev->bdev,b));
535 return -EINVAL;
536}
537
538static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
539{
540 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
541 (sb1->set_uuid1 == sb2->set_uuid1) &&
542 (sb1->set_uuid2 == sb2->set_uuid2) &&
543 (sb1->set_uuid3 == sb2->set_uuid3))
544
545 return 1;
546
547 return 0;
548}
549
550
551static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
552{
553 int ret;
554 mdp_super_t *tmp1, *tmp2;
555
556 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
557 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
558
559 if (!tmp1 || !tmp2) {
560 ret = 0;
561 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
562 goto abort;
563 }
564
565 *tmp1 = *sb1;
566 *tmp2 = *sb2;
567
568 /*
569 * nr_disks is not constant
570 */
571 tmp1->nr_disks = 0;
572 tmp2->nr_disks = 0;
573
574 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
575 ret = 0;
576 else
577 ret = 1;
578
579abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700580 kfree(tmp1);
581 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return ret;
583}
584
585static unsigned int calc_sb_csum(mdp_super_t * sb)
586{
587 unsigned int disk_csum, csum;
588
589 disk_csum = sb->sb_csum;
590 sb->sb_csum = 0;
591 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
592 sb->sb_csum = disk_csum;
593 return csum;
594}
595
596
597/*
598 * Handle superblock details.
599 * We want to be able to handle multiple superblock formats
600 * so we have a common interface to them all, and an array of
601 * different handlers.
602 * We rely on user-space to write the initial superblock, and support
603 * reading and updating of superblocks.
604 * Interface methods are:
605 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
606 * loads and validates a superblock on dev.
607 * if refdev != NULL, compare superblocks on both devices
608 * Return:
609 * 0 - dev has a superblock that is compatible with refdev
610 * 1 - dev has a superblock that is compatible and newer than refdev
611 * so dev should be used as the refdev in future
612 * -EINVAL superblock incompatible or invalid
613 * -othererror e.g. -EIO
614 *
615 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
616 * Verify that dev is acceptable into mddev.
617 * The first time, mddev->raid_disks will be 0, and data from
618 * dev should be merged in. Subsequent calls check that dev
619 * is new enough. Return 0 or -EINVAL
620 *
621 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
622 * Update the superblock for rdev with data in mddev
623 * This does not write to disc.
624 *
625 */
626
627struct super_type {
628 char *name;
629 struct module *owner;
630 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
631 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
632 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
633};
634
635/*
636 * load_super for 0.90.0
637 */
638static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
639{
640 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
641 mdp_super_t *sb;
642 int ret;
643 sector_t sb_offset;
644
645 /*
646 * Calculate the position of the superblock,
647 * it's at the end of the disk.
648 *
649 * It also happens to be a multiple of 4Kb.
650 */
651 sb_offset = calc_dev_sboffset(rdev->bdev);
652 rdev->sb_offset = sb_offset;
653
NeilBrown0002b272005-09-09 16:23:53 -0700654 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (ret) return ret;
656
657 ret = -EINVAL;
658
659 bdevname(rdev->bdev, b);
660 sb = (mdp_super_t*)page_address(rdev->sb_page);
661
662 if (sb->md_magic != MD_SB_MAGIC) {
663 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
664 b);
665 goto abort;
666 }
667
668 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800669 sb->minor_version < 90 ||
670 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
672 sb->major_version, sb->minor_version,
673 b);
674 goto abort;
675 }
676
677 if (sb->raid_disks <= 0)
678 goto abort;
679
680 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
681 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
682 b);
683 goto abort;
684 }
685
686 rdev->preferred_minor = sb->md_minor;
687 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700688 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 if (sb->level == LEVEL_MULTIPATH)
691 rdev->desc_nr = -1;
692 else
693 rdev->desc_nr = sb->this_disk.number;
694
695 if (refdev == 0)
696 ret = 1;
697 else {
698 __u64 ev1, ev2;
699 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
700 if (!uuid_equal(refsb, sb)) {
701 printk(KERN_WARNING "md: %s has different UUID to %s\n",
702 b, bdevname(refdev->bdev,b2));
703 goto abort;
704 }
705 if (!sb_equal(refsb, sb)) {
706 printk(KERN_WARNING "md: %s has same UUID"
707 " but different superblock to %s\n",
708 b, bdevname(refdev->bdev, b2));
709 goto abort;
710 }
711 ev1 = md_event(sb);
712 ev2 = md_event(refsb);
713 if (ev1 > ev2)
714 ret = 1;
715 else
716 ret = 0;
717 }
718 rdev->size = calc_dev_size(rdev, sb->chunk_size);
719
NeilBrown2bf071b2006-01-06 00:20:55 -0800720 if (rdev->size < sb->size && sb->level > 1)
721 /* "this cannot possibly happen" ... */
722 ret = -EINVAL;
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 abort:
725 return ret;
726}
727
728/*
729 * validate_super for 0.90.0
730 */
731static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
732{
733 mdp_disk_t *desc;
734 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
735
NeilBrown41158c72005-06-21 17:17:25 -0700736 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800737 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (mddev->raid_disks == 0) {
739 mddev->major_version = 0;
740 mddev->minor_version = sb->minor_version;
741 mddev->patch_version = sb->patch_version;
742 mddev->persistent = ! sb->not_persistent;
743 mddev->chunk_size = sb->chunk_size;
744 mddev->ctime = sb->ctime;
745 mddev->utime = sb->utime;
746 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800747 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 mddev->layout = sb->layout;
749 mddev->raid_disks = sb->raid_disks;
750 mddev->size = sb->size;
751 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700752 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700753 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
NeilBrownf6705572006-03-27 01:18:11 -0800755 if (mddev->minor_version >= 91) {
756 mddev->reshape_position = sb->reshape_position;
757 mddev->delta_disks = sb->delta_disks;
758 mddev->new_level = sb->new_level;
759 mddev->new_layout = sb->new_layout;
760 mddev->new_chunk = sb->new_chunk;
761 } else {
762 mddev->reshape_position = MaxSector;
763 mddev->delta_disks = 0;
764 mddev->new_level = mddev->level;
765 mddev->new_layout = mddev->layout;
766 mddev->new_chunk = mddev->chunk_size;
767 }
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (sb->state & (1<<MD_SB_CLEAN))
770 mddev->recovery_cp = MaxSector;
771 else {
772 if (sb->events_hi == sb->cp_events_hi &&
773 sb->events_lo == sb->cp_events_lo) {
774 mddev->recovery_cp = sb->recovery_cp;
775 } else
776 mddev->recovery_cp = 0;
777 }
778
779 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
780 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
781 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
782 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
783
784 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700785
786 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
787 mddev->bitmap_file == NULL) {
NeilBrownc5a10f62006-03-27 01:18:03 -0800788 if (mddev->level != 1 && mddev->level != 4
789 && mddev->level != 5 && mddev->level != 6
NeilBrown6cce3b22006-01-06 00:20:16 -0800790 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700791 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800792 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700793 return -EINVAL;
794 }
NeilBrown36fa3062005-09-09 16:23:45 -0700795 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700796 }
797
NeilBrown41158c72005-06-21 17:17:25 -0700798 } else if (mddev->pers == NULL) {
799 /* Insist on good event counter while assembling */
800 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 ++ev1;
802 if (ev1 < mddev->events)
803 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700804 } else if (mddev->bitmap) {
805 /* if adding to array with a bitmap, then we can accept an
806 * older device ... but not too old.
807 */
808 __u64 ev1 = md_event(sb);
809 if (ev1 < mddev->bitmap->events_cleared)
810 return 0;
811 } else /* just a hot-add of a new device, leave raid_disk at -1 */
812 return 0;
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 desc = sb->disks + rdev->desc_nr;
816
817 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800818 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 else if (desc->state & (1<<MD_DISK_SYNC) &&
820 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800821 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 rdev->raid_disk = desc->raid_disk;
823 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700824 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
825 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700826 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800827 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return 0;
829}
830
831/*
832 * sync_super for 0.90.0
833 */
834static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
835{
836 mdp_super_t *sb;
837 struct list_head *tmp;
838 mdk_rdev_t *rdev2;
839 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /* make rdev->sb match mddev data..
843 *
844 * 1/ zero out disks
845 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
846 * 3/ any empty disks < next_spare become removed
847 *
848 * disks[0] gets initialised to REMOVED because
849 * we cannot be sure from other fields if it has
850 * been initialised or not.
851 */
852 int i;
853 int active=0, working=0,failed=0,spare=0,nr_disks=0;
854
NeilBrown61181562005-09-09 16:24:02 -0700855 rdev->sb_size = MD_SB_BYTES;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 sb = (mdp_super_t*)page_address(rdev->sb_page);
858
859 memset(sb, 0, sizeof(*sb));
860
861 sb->md_magic = MD_SB_MAGIC;
862 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 sb->patch_version = mddev->patch_version;
864 sb->gvalid_words = 0; /* ignored */
865 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
866 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
867 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
868 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
869
870 sb->ctime = mddev->ctime;
871 sb->level = mddev->level;
872 sb->size = mddev->size;
873 sb->raid_disks = mddev->raid_disks;
874 sb->md_minor = mddev->md_minor;
875 sb->not_persistent = !mddev->persistent;
876 sb->utime = mddev->utime;
877 sb->state = 0;
878 sb->events_hi = (mddev->events>>32);
879 sb->events_lo = (u32)mddev->events;
880
NeilBrownf6705572006-03-27 01:18:11 -0800881 if (mddev->reshape_position == MaxSector)
882 sb->minor_version = 90;
883 else {
884 sb->minor_version = 91;
885 sb->reshape_position = mddev->reshape_position;
886 sb->new_level = mddev->new_level;
887 sb->delta_disks = mddev->delta_disks;
888 sb->new_layout = mddev->new_layout;
889 sb->new_chunk = mddev->new_chunk;
890 }
891 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (mddev->in_sync)
893 {
894 sb->recovery_cp = mddev->recovery_cp;
895 sb->cp_events_hi = (mddev->events>>32);
896 sb->cp_events_lo = (u32)mddev->events;
897 if (mddev->recovery_cp == MaxSector)
898 sb->state = (1<< MD_SB_CLEAN);
899 } else
900 sb->recovery_cp = 0;
901
902 sb->layout = mddev->layout;
903 sb->chunk_size = mddev->chunk_size;
904
NeilBrowna654b9d82005-06-21 17:17:27 -0700905 if (mddev->bitmap && mddev->bitmap_file == NULL)
906 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 sb->disks[0].state = (1<<MD_DISK_REMOVED);
909 ITERATE_RDEV(mddev,rdev2,tmp) {
910 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800911 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800912 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
913 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800914 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800916 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800917 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 d = &sb->disks[rdev2->desc_nr];
919 nr_disks++;
920 d->number = rdev2->desc_nr;
921 d->major = MAJOR(rdev2->bdev->bd_dev);
922 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800923 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
924 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 d->raid_disk = rdev2->raid_disk;
926 else
927 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800928 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800930 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 d->state = (1<<MD_DISK_ACTIVE);
932 d->state |= (1<<MD_DISK_SYNC);
933 active++;
934 working++;
935 } else {
936 d->state = 0;
937 spare++;
938 working++;
939 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700940 if (test_bit(WriteMostly, &rdev2->flags))
941 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* now set the "removed" and "faulty" bits on any missing devices */
944 for (i=0 ; i < mddev->raid_disks ; i++) {
945 mdp_disk_t *d = &sb->disks[i];
946 if (d->state == 0 && d->number == 0) {
947 d->number = i;
948 d->raid_disk = i;
949 d->state = (1<<MD_DISK_REMOVED);
950 d->state |= (1<<MD_DISK_FAULTY);
951 failed++;
952 }
953 }
954 sb->nr_disks = nr_disks;
955 sb->active_disks = active;
956 sb->working_disks = working;
957 sb->failed_disks = failed;
958 sb->spare_disks = spare;
959
960 sb->this_disk = sb->disks[rdev->desc_nr];
961 sb->sb_csum = calc_sb_csum(sb);
962}
963
964/*
965 * version 1 superblock
966 */
967
968static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
969{
970 unsigned int disk_csum, csum;
971 unsigned long long newcsum;
972 int size = 256 + le32_to_cpu(sb->max_dev)*2;
973 unsigned int *isuper = (unsigned int*)sb;
974 int i;
975
976 disk_csum = sb->sb_csum;
977 sb->sb_csum = 0;
978 newcsum = 0;
979 for (i=0; size>=4; size -= 4 )
980 newcsum += le32_to_cpu(*isuper++);
981
982 if (size == 2)
983 newcsum += le16_to_cpu(*(unsigned short*) isuper);
984
985 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
986 sb->sb_csum = disk_csum;
987 return cpu_to_le32(csum);
988}
989
990static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
991{
992 struct mdp_superblock_1 *sb;
993 int ret;
994 sector_t sb_offset;
995 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700996 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /*
999 * Calculate the position of the superblock.
1000 * It is always aligned to a 4K boundary and
1001 * depeding on minor_version, it can be:
1002 * 0: At least 8K, but less than 12K, from end of device
1003 * 1: At start of device
1004 * 2: 4K from start of device.
1005 */
1006 switch(minor_version) {
1007 case 0:
1008 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1009 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001010 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* convert from sectors to K */
1012 sb_offset /= 2;
1013 break;
1014 case 1:
1015 sb_offset = 0;
1016 break;
1017 case 2:
1018 sb_offset = 4;
1019 break;
1020 default:
1021 return -EINVAL;
1022 }
1023 rdev->sb_offset = sb_offset;
1024
NeilBrown0002b272005-09-09 16:23:53 -07001025 /* superblock is rarely larger than 1K, but it can be larger,
1026 * and it is safe to read 4k, so we do that
1027 */
1028 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (ret) return ret;
1030
1031
1032 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1033
1034 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1035 sb->major_version != cpu_to_le32(1) ||
1036 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1037 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001038 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -EINVAL;
1040
1041 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1042 printk("md: invalid superblock checksum on %s\n",
1043 bdevname(rdev->bdev,b));
1044 return -EINVAL;
1045 }
1046 if (le64_to_cpu(sb->data_size) < 10) {
1047 printk("md: data_size too small on %s\n",
1048 bdevname(rdev->bdev,b));
1049 return -EINVAL;
1050 }
1051 rdev->preferred_minor = 0xffff;
1052 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001053 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
NeilBrown0002b272005-09-09 16:23:53 -07001055 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001056 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001057 if (rdev->sb_size & bmask)
1058 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001061 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 else {
1063 __u64 ev1, ev2;
1064 struct mdp_superblock_1 *refsb =
1065 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1066
1067 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1068 sb->level != refsb->level ||
1069 sb->layout != refsb->layout ||
1070 sb->chunksize != refsb->chunksize) {
1071 printk(KERN_WARNING "md: %s has strangely different"
1072 " superblock to %s\n",
1073 bdevname(rdev->bdev,b),
1074 bdevname(refdev->bdev,b2));
1075 return -EINVAL;
1076 }
1077 ev1 = le64_to_cpu(sb->events);
1078 ev2 = le64_to_cpu(refsb->events);
1079
1080 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001081 ret = 1;
1082 else
1083 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085 if (minor_version)
1086 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1087 else
1088 rdev->size = rdev->sb_offset;
1089 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1090 return -EINVAL;
1091 rdev->size = le64_to_cpu(sb->data_size)/2;
1092 if (le32_to_cpu(sb->chunksize))
1093 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001094
1095 if (le32_to_cpu(sb->size) > rdev->size*2)
1096 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001097 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1101{
1102 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1103
NeilBrown41158c72005-06-21 17:17:25 -07001104 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001105 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (mddev->raid_disks == 0) {
1107 mddev->major_version = 1;
1108 mddev->patch_version = 0;
1109 mddev->persistent = 1;
1110 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1111 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1112 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1113 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001114 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 mddev->layout = le32_to_cpu(sb->layout);
1116 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1117 mddev->size = le64_to_cpu(sb->size)/2;
1118 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001119 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001120 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1123 memcpy(mddev->uuid, sb->set_uuid, 16);
1124
1125 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001126
NeilBrown71c08052005-09-09 16:23:51 -07001127 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001128 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001129 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1130 && mddev->level != 10) {
1131 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001132 return -EINVAL;
1133 }
1134 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1135 }
NeilBrownf6705572006-03-27 01:18:11 -08001136 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1137 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1138 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1139 mddev->new_level = le32_to_cpu(sb->new_level);
1140 mddev->new_layout = le32_to_cpu(sb->new_layout);
1141 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1142 } else {
1143 mddev->reshape_position = MaxSector;
1144 mddev->delta_disks = 0;
1145 mddev->new_level = mddev->level;
1146 mddev->new_layout = mddev->layout;
1147 mddev->new_chunk = mddev->chunk_size;
1148 }
1149
NeilBrown41158c72005-06-21 17:17:25 -07001150 } else if (mddev->pers == NULL) {
1151 /* Insist of good event counter while assembling */
1152 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 ++ev1;
1154 if (ev1 < mddev->events)
1155 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001156 } else if (mddev->bitmap) {
1157 /* If adding to array with a bitmap, then we can accept an
1158 * older device, but not too old.
1159 */
1160 __u64 ev1 = le64_to_cpu(sb->events);
1161 if (ev1 < mddev->bitmap->events_cleared)
1162 return 0;
1163 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (mddev->level != LEVEL_MULTIPATH) {
1167 int role;
1168 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1169 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1170 switch(role) {
1171 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001174 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 break;
1176 default:
NeilBrownb2d444d2005-11-08 21:39:31 -08001177 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 rdev->raid_disk = role;
1179 break;
1180 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001181 if (sb->devflags & WriteMostly1)
1182 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001183 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001184 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return 0;
1187}
1188
1189static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1190{
1191 struct mdp_superblock_1 *sb;
1192 struct list_head *tmp;
1193 mdk_rdev_t *rdev2;
1194 int max_dev, i;
1195 /* make rdev->sb match mddev and rdev data. */
1196
1197 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1198
1199 sb->feature_map = 0;
1200 sb->pad0 = 0;
1201 memset(sb->pad1, 0, sizeof(sb->pad1));
1202 memset(sb->pad2, 0, sizeof(sb->pad2));
1203 memset(sb->pad3, 0, sizeof(sb->pad3));
1204
1205 sb->utime = cpu_to_le64((__u64)mddev->utime);
1206 sb->events = cpu_to_le64(mddev->events);
1207 if (mddev->in_sync)
1208 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1209 else
1210 sb->resync_offset = cpu_to_le64(0);
1211
NeilBrown4dbcdc72006-01-06 00:20:52 -08001212 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1213
NeilBrownf0ca3402006-02-02 14:28:04 -08001214 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001215 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001216
NeilBrowna654b9d82005-06-21 17:17:27 -07001217 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1218 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001219 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001220 }
NeilBrownf6705572006-03-27 01:18:11 -08001221 if (mddev->reshape_position != MaxSector) {
1222 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1223 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1224 sb->new_layout = cpu_to_le32(mddev->new_layout);
1225 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1226 sb->new_level = cpu_to_le32(mddev->new_level);
1227 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1228 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 max_dev = 0;
1231 ITERATE_RDEV(mddev,rdev2,tmp)
1232 if (rdev2->desc_nr+1 > max_dev)
1233 max_dev = rdev2->desc_nr+1;
1234
1235 sb->max_dev = cpu_to_le32(max_dev);
1236 for (i=0; i<max_dev;i++)
1237 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1238
1239 ITERATE_RDEV(mddev,rdev2,tmp) {
1240 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001241 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001243 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1245 else
1246 sb->dev_roles[i] = cpu_to_le16(0xffff);
1247 }
1248
1249 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1250 sb->sb_csum = calc_sb_1_csum(sb);
1251}
1252
1253
Adrian Bunk75c96f82005-05-05 16:16:09 -07001254static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 [0] = {
1256 .name = "0.90.0",
1257 .owner = THIS_MODULE,
1258 .load_super = super_90_load,
1259 .validate_super = super_90_validate,
1260 .sync_super = super_90_sync,
1261 },
1262 [1] = {
1263 .name = "md-1",
1264 .owner = THIS_MODULE,
1265 .load_super = super_1_load,
1266 .validate_super = super_1_validate,
1267 .sync_super = super_1_sync,
1268 },
1269};
1270
1271static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1272{
1273 struct list_head *tmp;
1274 mdk_rdev_t *rdev;
1275
1276 ITERATE_RDEV(mddev,rdev,tmp)
1277 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1278 return rdev;
1279
1280 return NULL;
1281}
1282
1283static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1284{
1285 struct list_head *tmp;
1286 mdk_rdev_t *rdev;
1287
1288 ITERATE_RDEV(mddev1,rdev,tmp)
1289 if (match_dev_unit(mddev2, rdev))
1290 return 1;
1291
1292 return 0;
1293}
1294
1295static LIST_HEAD(pending_raid_disks);
1296
1297static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1298{
1299 mdk_rdev_t *same_pdev;
1300 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001301 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001302 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (rdev->mddev) {
1305 MD_BUG();
1306 return -EINVAL;
1307 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001308 /* make sure rdev->size exceeds mddev->size */
1309 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1310 if (mddev->pers)
1311 /* Cannot change size, so fail */
1312 return -ENOSPC;
1313 else
1314 mddev->size = rdev->size;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 same_pdev = match_dev_unit(mddev, rdev);
1317 if (same_pdev)
1318 printk(KERN_WARNING
1319 "%s: WARNING: %s appears to be on the same physical"
1320 " disk as %s. True\n protection against single-disk"
1321 " failure might be compromised.\n",
1322 mdname(mddev), bdevname(rdev->bdev,b),
1323 bdevname(same_pdev->bdev,b2));
1324
1325 /* Verify rdev->desc_nr is unique.
1326 * If it is -1, assign a free number, else
1327 * check number is not in use
1328 */
1329 if (rdev->desc_nr < 0) {
1330 int choice = 0;
1331 if (mddev->pers) choice = mddev->raid_disks;
1332 while (find_rdev_nr(mddev, choice))
1333 choice++;
1334 rdev->desc_nr = choice;
1335 } else {
1336 if (find_rdev_nr(mddev, rdev->desc_nr))
1337 return -EBUSY;
1338 }
NeilBrown19133a42005-11-08 21:39:35 -08001339 bdevname(rdev->bdev,b);
1340 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1341 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001342 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1343 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 list_add(&rdev->same_set, &mddev->disks);
1346 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001347 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001348
NeilBrown9c791972005-11-08 21:39:28 -08001349 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001350 kobject_add(&rdev->kobj);
1351
NeilBrownf637b9f2005-11-08 21:39:37 -08001352 if (rdev->bdev->bd_part)
1353 ko = &rdev->bdev->bd_part->kobj;
1354 else
1355 ko = &rdev->bdev->bd_disk->kobj;
1356 sysfs_create_link(&rdev->kobj, ko, "block");
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001357 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return 0;
1359}
1360
1361static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1362{
1363 char b[BDEVNAME_SIZE];
1364 if (!rdev->mddev) {
1365 MD_BUG();
1366 return;
1367 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001368 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 list_del_init(&rdev->same_set);
1370 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1371 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001372 sysfs_remove_link(&rdev->kobj, "block");
1373 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
1376/*
1377 * prevent the device from being mounted, repartitioned or
1378 * otherwise reused by a RAID array (or any other kernel
1379 * subsystem), by bd_claiming the device.
1380 */
1381static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1382{
1383 int err = 0;
1384 struct block_device *bdev;
1385 char b[BDEVNAME_SIZE];
1386
1387 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1388 if (IS_ERR(bdev)) {
1389 printk(KERN_ERR "md: could not open %s.\n",
1390 __bdevname(dev, b));
1391 return PTR_ERR(bdev);
1392 }
1393 err = bd_claim(bdev, rdev);
1394 if (err) {
1395 printk(KERN_ERR "md: could not bd_claim %s.\n",
1396 bdevname(bdev, b));
1397 blkdev_put(bdev);
1398 return err;
1399 }
1400 rdev->bdev = bdev;
1401 return err;
1402}
1403
1404static void unlock_rdev(mdk_rdev_t *rdev)
1405{
1406 struct block_device *bdev = rdev->bdev;
1407 rdev->bdev = NULL;
1408 if (!bdev)
1409 MD_BUG();
1410 bd_release(bdev);
1411 blkdev_put(bdev);
1412}
1413
1414void md_autodetect_dev(dev_t dev);
1415
1416static void export_rdev(mdk_rdev_t * rdev)
1417{
1418 char b[BDEVNAME_SIZE];
1419 printk(KERN_INFO "md: export_rdev(%s)\n",
1420 bdevname(rdev->bdev,b));
1421 if (rdev->mddev)
1422 MD_BUG();
1423 free_disk_sb(rdev);
1424 list_del_init(&rdev->same_set);
1425#ifndef MODULE
1426 md_autodetect_dev(rdev->bdev->bd_dev);
1427#endif
1428 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001429 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432static void kick_rdev_from_array(mdk_rdev_t * rdev)
1433{
1434 unbind_rdev_from_array(rdev);
1435 export_rdev(rdev);
1436}
1437
1438static void export_array(mddev_t *mddev)
1439{
1440 struct list_head *tmp;
1441 mdk_rdev_t *rdev;
1442
1443 ITERATE_RDEV(mddev,rdev,tmp) {
1444 if (!rdev->mddev) {
1445 MD_BUG();
1446 continue;
1447 }
1448 kick_rdev_from_array(rdev);
1449 }
1450 if (!list_empty(&mddev->disks))
1451 MD_BUG();
1452 mddev->raid_disks = 0;
1453 mddev->major_version = 0;
1454}
1455
1456static void print_desc(mdp_disk_t *desc)
1457{
1458 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1459 desc->major,desc->minor,desc->raid_disk,desc->state);
1460}
1461
1462static void print_sb(mdp_super_t *sb)
1463{
1464 int i;
1465
1466 printk(KERN_INFO
1467 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1468 sb->major_version, sb->minor_version, sb->patch_version,
1469 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1470 sb->ctime);
1471 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1472 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1473 sb->md_minor, sb->layout, sb->chunk_size);
1474 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1475 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1476 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1477 sb->failed_disks, sb->spare_disks,
1478 sb->sb_csum, (unsigned long)sb->events_lo);
1479
1480 printk(KERN_INFO);
1481 for (i = 0; i < MD_SB_DISKS; i++) {
1482 mdp_disk_t *desc;
1483
1484 desc = sb->disks + i;
1485 if (desc->number || desc->major || desc->minor ||
1486 desc->raid_disk || (desc->state && (desc->state != 4))) {
1487 printk(" D %2d: ", i);
1488 print_desc(desc);
1489 }
1490 }
1491 printk(KERN_INFO "md: THIS: ");
1492 print_desc(&sb->this_disk);
1493
1494}
1495
1496static void print_rdev(mdk_rdev_t *rdev)
1497{
1498 char b[BDEVNAME_SIZE];
1499 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1500 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001501 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1502 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (rdev->sb_loaded) {
1504 printk(KERN_INFO "md: rdev superblock:\n");
1505 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1506 } else
1507 printk(KERN_INFO "md: no rdev superblock!\n");
1508}
1509
1510void md_print_devices(void)
1511{
1512 struct list_head *tmp, *tmp2;
1513 mdk_rdev_t *rdev;
1514 mddev_t *mddev;
1515 char b[BDEVNAME_SIZE];
1516
1517 printk("\n");
1518 printk("md: **********************************\n");
1519 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1520 printk("md: **********************************\n");
1521 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
NeilBrown32a76272005-06-21 17:17:14 -07001523 if (mddev->bitmap)
1524 bitmap_print_sb(mddev->bitmap);
1525 else
1526 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 ITERATE_RDEV(mddev,rdev,tmp2)
1528 printk("<%s>", bdevname(rdev->bdev,b));
1529 printk("\n");
1530
1531 ITERATE_RDEV(mddev,rdev,tmp2)
1532 print_rdev(rdev);
1533 }
1534 printk("md: **********************************\n");
1535 printk("\n");
1536}
1537
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539static void sync_sbs(mddev_t * mddev)
1540{
1541 mdk_rdev_t *rdev;
1542 struct list_head *tmp;
1543
1544 ITERATE_RDEV(mddev,rdev,tmp) {
1545 super_types[mddev->major_version].
1546 sync_super(mddev, rdev);
1547 rdev->sb_loaded = 1;
1548 }
1549}
1550
NeilBrownf6705572006-03-27 01:18:11 -08001551void md_update_sb(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001553 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 struct list_head *tmp;
1555 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001556 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001559 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001560 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 mddev->utime = get_seconds();
1562 mddev->events ++;
1563
1564 if (!mddev->events) {
1565 /*
1566 * oops, this 64-bit counter should never wrap.
1567 * Either we are in around ~1 trillion A.C., assuming
1568 * 1 reboot per second, or we have a bug:
1569 */
1570 MD_BUG();
1571 mddev->events --;
1572 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001573 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 sync_sbs(mddev);
1575
1576 /*
1577 * do not write anything to disk if using
1578 * nonpersistent superblocks
1579 */
NeilBrown06d91a52005-06-21 17:17:12 -07001580 if (!mddev->persistent) {
1581 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001582 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001583 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001585 }
NeilBrowna9701a32005-11-08 21:39:34 -08001586 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 dprintk(KERN_INFO
1589 "md: updating %s RAID superblock on device (in sync %d)\n",
1590 mdname(mddev),mddev->in_sync);
1591
NeilBrown32a76272005-06-21 17:17:14 -07001592 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 ITERATE_RDEV(mddev,rdev,tmp) {
1594 char b[BDEVNAME_SIZE];
1595 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001596 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 dprintk("(skipping faulty ");
1598
1599 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001600 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001601 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001602 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001603 rdev->sb_page);
1604 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1605 bdevname(rdev->bdev,b),
1606 (unsigned long long)rdev->sb_offset);
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 } else
1609 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001610 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 /* only need to write one superblock... */
1612 break;
1613 }
NeilBrowna9701a32005-11-08 21:39:34 -08001614 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001615 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1616
NeilBrowna9701a32005-11-08 21:39:34 -08001617 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001618 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001619 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001620 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001621 goto repeat;
1622 }
1623 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001624 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001625 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
NeilBrownf6705572006-03-27 01:18:11 -08001628EXPORT_SYMBOL_GPL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
NeilBrownbce74da2006-01-06 00:20:41 -08001630/* words written to sysfs files may, or my not, be \n terminated.
1631 * We want to accept with case. For this we use cmd_match.
1632 */
1633static int cmd_match(const char *cmd, const char *str)
1634{
1635 /* See if cmd, written into a sysfs file, matches
1636 * str. They must either be the same, or cmd can
1637 * have a trailing newline
1638 */
1639 while (*cmd && *str && *cmd == *str) {
1640 cmd++;
1641 str++;
1642 }
1643 if (*cmd == '\n')
1644 cmd++;
1645 if (*str || *cmd)
1646 return 0;
1647 return 1;
1648}
1649
NeilBrown86e6ffd2005-11-08 21:39:24 -08001650struct rdev_sysfs_entry {
1651 struct attribute attr;
1652 ssize_t (*show)(mdk_rdev_t *, char *);
1653 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1654};
1655
1656static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001657state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001658{
1659 char *sep = "";
1660 int len=0;
1661
NeilBrownb2d444d2005-11-08 21:39:31 -08001662 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001663 len+= sprintf(page+len, "%sfaulty",sep);
1664 sep = ",";
1665 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001666 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001667 len += sprintf(page+len, "%sin_sync",sep);
1668 sep = ",";
1669 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001670 if (!test_bit(Faulty, &rdev->flags) &&
1671 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001672 len += sprintf(page+len, "%sspare", sep);
1673 sep = ",";
1674 }
1675 return len+sprintf(page+len, "\n");
1676}
1677
NeilBrown96de1e62005-11-08 21:39:39 -08001678static struct rdev_sysfs_entry
1679rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001680
1681static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001682super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001683{
1684 if (rdev->sb_loaded && rdev->sb_size) {
1685 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1686 return rdev->sb_size;
1687 } else
1688 return 0;
1689}
NeilBrown96de1e62005-11-08 21:39:39 -08001690static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1691
NeilBrown4dbcdc72006-01-06 00:20:52 -08001692static ssize_t
1693errors_show(mdk_rdev_t *rdev, char *page)
1694{
1695 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1696}
1697
1698static ssize_t
1699errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1700{
1701 char *e;
1702 unsigned long n = simple_strtoul(buf, &e, 10);
1703 if (*buf && (*e == 0 || *e == '\n')) {
1704 atomic_set(&rdev->corrected_errors, n);
1705 return len;
1706 }
1707 return -EINVAL;
1708}
1709static struct rdev_sysfs_entry rdev_errors =
1710__ATTR(errors, 0644, errors_show, errors_store);
1711
NeilBrown014236d2006-01-06 00:20:55 -08001712static ssize_t
1713slot_show(mdk_rdev_t *rdev, char *page)
1714{
1715 if (rdev->raid_disk < 0)
1716 return sprintf(page, "none\n");
1717 else
1718 return sprintf(page, "%d\n", rdev->raid_disk);
1719}
1720
1721static ssize_t
1722slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1723{
1724 char *e;
1725 int slot = simple_strtoul(buf, &e, 10);
1726 if (strncmp(buf, "none", 4)==0)
1727 slot = -1;
1728 else if (e==buf || (*e && *e!= '\n'))
1729 return -EINVAL;
1730 if (rdev->mddev->pers)
1731 /* Cannot set slot in active array (yet) */
1732 return -EBUSY;
1733 if (slot >= rdev->mddev->raid_disks)
1734 return -ENOSPC;
1735 rdev->raid_disk = slot;
1736 /* assume it is working */
1737 rdev->flags = 0;
1738 set_bit(In_sync, &rdev->flags);
1739 return len;
1740}
1741
1742
1743static struct rdev_sysfs_entry rdev_slot =
1744__ATTR(slot, 0644, slot_show, slot_store);
1745
NeilBrown93c8cad2006-01-06 00:20:56 -08001746static ssize_t
1747offset_show(mdk_rdev_t *rdev, char *page)
1748{
Andrew Morton6961ece2006-01-06 00:20:59 -08001749 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001750}
1751
1752static ssize_t
1753offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1754{
1755 char *e;
1756 unsigned long long offset = simple_strtoull(buf, &e, 10);
1757 if (e==buf || (*e && *e != '\n'))
1758 return -EINVAL;
1759 if (rdev->mddev->pers)
1760 return -EBUSY;
1761 rdev->data_offset = offset;
1762 return len;
1763}
1764
1765static struct rdev_sysfs_entry rdev_offset =
1766__ATTR(offset, 0644, offset_show, offset_store);
1767
NeilBrown83303b62006-01-06 00:21:06 -08001768static ssize_t
1769rdev_size_show(mdk_rdev_t *rdev, char *page)
1770{
1771 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1772}
1773
1774static ssize_t
1775rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1776{
1777 char *e;
1778 unsigned long long size = simple_strtoull(buf, &e, 10);
1779 if (e==buf || (*e && *e != '\n'))
1780 return -EINVAL;
1781 if (rdev->mddev->pers)
1782 return -EBUSY;
1783 rdev->size = size;
1784 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1785 rdev->mddev->size = size;
1786 return len;
1787}
1788
1789static struct rdev_sysfs_entry rdev_size =
1790__ATTR(size, 0644, rdev_size_show, rdev_size_store);
1791
NeilBrown86e6ffd2005-11-08 21:39:24 -08001792static struct attribute *rdev_default_attrs[] = {
1793 &rdev_state.attr,
1794 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001795 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001796 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001797 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001798 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001799 NULL,
1800};
1801static ssize_t
1802rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1803{
1804 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1805 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1806
1807 if (!entry->show)
1808 return -EIO;
1809 return entry->show(rdev, page);
1810}
1811
1812static ssize_t
1813rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1814 const char *page, size_t length)
1815{
1816 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1817 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1818
1819 if (!entry->store)
1820 return -EIO;
1821 return entry->store(rdev, page, length);
1822}
1823
1824static void rdev_free(struct kobject *ko)
1825{
1826 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1827 kfree(rdev);
1828}
1829static struct sysfs_ops rdev_sysfs_ops = {
1830 .show = rdev_attr_show,
1831 .store = rdev_attr_store,
1832};
1833static struct kobj_type rdev_ktype = {
1834 .release = rdev_free,
1835 .sysfs_ops = &rdev_sysfs_ops,
1836 .default_attrs = rdev_default_attrs,
1837};
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839/*
1840 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1841 *
1842 * mark the device faulty if:
1843 *
1844 * - the device is nonexistent (zero size)
1845 * - the device has no valid superblock
1846 *
1847 * a faulty rdev _never_ has rdev->sb set.
1848 */
1849static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1850{
1851 char b[BDEVNAME_SIZE];
1852 int err;
1853 mdk_rdev_t *rdev;
1854 sector_t size;
1855
NeilBrown9ffae0c2006-01-06 00:20:32 -08001856 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (!rdev) {
1858 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1859 return ERR_PTR(-ENOMEM);
1860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 if ((err = alloc_disk_sb(rdev)))
1863 goto abort_free;
1864
1865 err = lock_rdev(rdev, newdev);
1866 if (err)
1867 goto abort_free;
1868
NeilBrown86e6ffd2005-11-08 21:39:24 -08001869 rdev->kobj.parent = NULL;
1870 rdev->kobj.ktype = &rdev_ktype;
1871 kobject_init(&rdev->kobj);
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001874 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 rdev->data_offset = 0;
1876 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001877 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001878 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1881 if (!size) {
1882 printk(KERN_WARNING
1883 "md: %s has zero or unknown size, marking faulty!\n",
1884 bdevname(rdev->bdev,b));
1885 err = -EINVAL;
1886 goto abort_free;
1887 }
1888
1889 if (super_format >= 0) {
1890 err = super_types[super_format].
1891 load_super(rdev, NULL, super_minor);
1892 if (err == -EINVAL) {
1893 printk(KERN_WARNING
1894 "md: %s has invalid sb, not importing!\n",
1895 bdevname(rdev->bdev,b));
1896 goto abort_free;
1897 }
1898 if (err < 0) {
1899 printk(KERN_WARNING
1900 "md: could not read %s's sb, not importing!\n",
1901 bdevname(rdev->bdev,b));
1902 goto abort_free;
1903 }
1904 }
1905 INIT_LIST_HEAD(&rdev->same_set);
1906
1907 return rdev;
1908
1909abort_free:
1910 if (rdev->sb_page) {
1911 if (rdev->bdev)
1912 unlock_rdev(rdev);
1913 free_disk_sb(rdev);
1914 }
1915 kfree(rdev);
1916 return ERR_PTR(err);
1917}
1918
1919/*
1920 * Check a full RAID array for plausibility
1921 */
1922
1923
NeilBrowna757e642005-04-16 15:26:42 -07001924static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 int i;
1927 struct list_head *tmp;
1928 mdk_rdev_t *rdev, *freshest;
1929 char b[BDEVNAME_SIZE];
1930
1931 freshest = NULL;
1932 ITERATE_RDEV(mddev,rdev,tmp)
1933 switch (super_types[mddev->major_version].
1934 load_super(rdev, freshest, mddev->minor_version)) {
1935 case 1:
1936 freshest = rdev;
1937 break;
1938 case 0:
1939 break;
1940 default:
1941 printk( KERN_ERR \
1942 "md: fatal superblock inconsistency in %s"
1943 " -- removing from array\n",
1944 bdevname(rdev->bdev,b));
1945 kick_rdev_from_array(rdev);
1946 }
1947
1948
1949 super_types[mddev->major_version].
1950 validate_super(mddev, freshest);
1951
1952 i = 0;
1953 ITERATE_RDEV(mddev,rdev,tmp) {
1954 if (rdev != freshest)
1955 if (super_types[mddev->major_version].
1956 validate_super(mddev, rdev)) {
1957 printk(KERN_WARNING "md: kicking non-fresh %s"
1958 " from array!\n",
1959 bdevname(rdev->bdev,b));
1960 kick_rdev_from_array(rdev);
1961 continue;
1962 }
1963 if (mddev->level == LEVEL_MULTIPATH) {
1964 rdev->desc_nr = i++;
1965 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001966 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 }
1968 }
1969
1970
1971
1972 if (mddev->recovery_cp != MaxSector &&
1973 mddev->level >= 1)
1974 printk(KERN_ERR "md: %s: raid array is not clean"
1975 " -- starting background reconstruction\n",
1976 mdname(mddev));
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
NeilBrowneae17012005-11-08 21:39:23 -08001980static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001981level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001982{
NeilBrown2604b702006-01-06 00:20:36 -08001983 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08001984 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08001985 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08001986 else if (mddev->clevel[0])
1987 return sprintf(page, "%s\n", mddev->clevel);
1988 else if (mddev->level != LEVEL_NONE)
1989 return sprintf(page, "%d\n", mddev->level);
1990 else
1991 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08001992}
1993
NeilBrownd9d166c2006-01-06 00:20:51 -08001994static ssize_t
1995level_store(mddev_t *mddev, const char *buf, size_t len)
1996{
1997 int rv = len;
1998 if (mddev->pers)
1999 return -EBUSY;
2000 if (len == 0)
2001 return 0;
2002 if (len >= sizeof(mddev->clevel))
2003 return -ENOSPC;
2004 strncpy(mddev->clevel, buf, len);
2005 if (mddev->clevel[len-1] == '\n')
2006 len--;
2007 mddev->clevel[len] = 0;
2008 mddev->level = LEVEL_NONE;
2009 return rv;
2010}
2011
2012static struct md_sysfs_entry md_level =
2013__ATTR(level, 0644, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002014
2015static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002016raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002017{
NeilBrownbb636542005-11-08 21:39:45 -08002018 if (mddev->raid_disks == 0)
2019 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002020 return sprintf(page, "%d\n", mddev->raid_disks);
2021}
2022
NeilBrownda943b992006-01-06 00:20:54 -08002023static int update_raid_disks(mddev_t *mddev, int raid_disks);
2024
2025static ssize_t
2026raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2027{
2028 /* can only set raid_disks if array is not yet active */
2029 char *e;
2030 int rv = 0;
2031 unsigned long n = simple_strtoul(buf, &e, 10);
2032
2033 if (!*buf || (*e && *e != '\n'))
2034 return -EINVAL;
2035
2036 if (mddev->pers)
2037 rv = update_raid_disks(mddev, n);
2038 else
2039 mddev->raid_disks = n;
2040 return rv ? rv : len;
2041}
2042static struct md_sysfs_entry md_raid_disks =
2043__ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002044
NeilBrown24dd4692005-11-08 21:39:26 -08002045static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002046chunk_size_show(mddev_t *mddev, char *page)
2047{
2048 return sprintf(page, "%d\n", mddev->chunk_size);
2049}
2050
2051static ssize_t
2052chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2053{
2054 /* can only set chunk_size if array is not yet active */
2055 char *e;
2056 unsigned long n = simple_strtoul(buf, &e, 10);
2057
2058 if (mddev->pers)
2059 return -EBUSY;
2060 if (!*buf || (*e && *e != '\n'))
2061 return -EINVAL;
2062
2063 mddev->chunk_size = n;
2064 return len;
2065}
2066static struct md_sysfs_entry md_chunk_size =
2067__ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2068
NeilBrown6d7ff732006-01-06 00:21:16 -08002069static ssize_t
2070null_show(mddev_t *mddev, char *page)
2071{
2072 return -EINVAL;
2073}
2074
2075static ssize_t
2076new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2077{
2078 /* buf must be %d:%d\n? giving major and minor numbers */
2079 /* The new device is added to the array.
2080 * If the array has a persistent superblock, we read the
2081 * superblock to initialise info and check validity.
2082 * Otherwise, only checking done is that in bind_rdev_to_array,
2083 * which mainly checks size.
2084 */
2085 char *e;
2086 int major = simple_strtoul(buf, &e, 10);
2087 int minor;
2088 dev_t dev;
2089 mdk_rdev_t *rdev;
2090 int err;
2091
2092 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2093 return -EINVAL;
2094 minor = simple_strtoul(e+1, &e, 10);
2095 if (*e && *e != '\n')
2096 return -EINVAL;
2097 dev = MKDEV(major, minor);
2098 if (major != MAJOR(dev) ||
2099 minor != MINOR(dev))
2100 return -EOVERFLOW;
2101
2102
2103 if (mddev->persistent) {
2104 rdev = md_import_device(dev, mddev->major_version,
2105 mddev->minor_version);
2106 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2107 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2108 mdk_rdev_t, same_set);
2109 err = super_types[mddev->major_version]
2110 .load_super(rdev, rdev0, mddev->minor_version);
2111 if (err < 0)
2112 goto out;
2113 }
2114 } else
2115 rdev = md_import_device(dev, -1, -1);
2116
2117 if (IS_ERR(rdev))
2118 return PTR_ERR(rdev);
2119 err = bind_rdev_to_array(rdev, mddev);
2120 out:
2121 if (err)
2122 export_rdev(rdev);
2123 return err ? err : len;
2124}
2125
2126static struct md_sysfs_entry md_new_device =
2127__ATTR(new_dev, 0200, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002128
2129static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002130size_show(mddev_t *mddev, char *page)
2131{
2132 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2133}
2134
2135static int update_size(mddev_t *mddev, unsigned long size);
2136
2137static ssize_t
2138size_store(mddev_t *mddev, const char *buf, size_t len)
2139{
2140 /* If array is inactive, we can reduce the component size, but
2141 * not increase it (except from 0).
2142 * If array is active, we can try an on-line resize
2143 */
2144 char *e;
2145 int err = 0;
2146 unsigned long long size = simple_strtoull(buf, &e, 10);
2147 if (!*buf || *buf == '\n' ||
2148 (*e && *e != '\n'))
2149 return -EINVAL;
2150
2151 if (mddev->pers) {
2152 err = update_size(mddev, size);
2153 md_update_sb(mddev);
2154 } else {
2155 if (mddev->size == 0 ||
2156 mddev->size > size)
2157 mddev->size = size;
2158 else
2159 err = -ENOSPC;
2160 }
2161 return err ? err : len;
2162}
2163
2164static struct md_sysfs_entry md_size =
2165__ATTR(component_size, 0644, size_show, size_store);
2166
NeilBrown8bb93aa2006-01-06 00:20:50 -08002167
2168/* Metdata version.
2169 * This is either 'none' for arrays with externally managed metadata,
2170 * or N.M for internally known formats
2171 */
2172static ssize_t
2173metadata_show(mddev_t *mddev, char *page)
2174{
2175 if (mddev->persistent)
2176 return sprintf(page, "%d.%d\n",
2177 mddev->major_version, mddev->minor_version);
2178 else
2179 return sprintf(page, "none\n");
2180}
2181
2182static ssize_t
2183metadata_store(mddev_t *mddev, const char *buf, size_t len)
2184{
2185 int major, minor;
2186 char *e;
2187 if (!list_empty(&mddev->disks))
2188 return -EBUSY;
2189
2190 if (cmd_match(buf, "none")) {
2191 mddev->persistent = 0;
2192 mddev->major_version = 0;
2193 mddev->minor_version = 90;
2194 return len;
2195 }
2196 major = simple_strtoul(buf, &e, 10);
2197 if (e==buf || *e != '.')
2198 return -EINVAL;
2199 buf = e+1;
2200 minor = simple_strtoul(buf, &e, 10);
2201 if (e==buf || *e != '\n')
2202 return -EINVAL;
2203 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2204 super_types[major].name == NULL)
2205 return -ENOENT;
2206 mddev->major_version = major;
2207 mddev->minor_version = minor;
2208 mddev->persistent = 1;
2209 return len;
2210}
2211
2212static struct md_sysfs_entry md_metadata =
2213__ATTR(metadata_version, 0644, metadata_show, metadata_store);
2214
NeilBrowna35b0d62006-01-06 00:20:49 -08002215static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002216action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002217{
NeilBrown7eec3142005-11-08 21:39:44 -08002218 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002219 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2220 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002221 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2222 type = "reshape";
2223 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002224 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2225 type = "resync";
2226 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2227 type = "check";
2228 else
2229 type = "repair";
2230 } else
2231 type = "recover";
2232 }
2233 return sprintf(page, "%s\n", type);
2234}
2235
2236static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002237action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002238{
NeilBrown7eec3142005-11-08 21:39:44 -08002239 if (!mddev->pers || !mddev->pers->sync_request)
2240 return -EINVAL;
2241
NeilBrownbce74da2006-01-06 00:20:41 -08002242 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002243 if (mddev->sync_thread) {
2244 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2245 md_unregister_thread(mddev->sync_thread);
2246 mddev->sync_thread = NULL;
2247 mddev->recovery = 0;
2248 }
NeilBrown03c902e2006-01-06 00:20:46 -08002249 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2250 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002251 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002252 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002253 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002254 else if (cmd_match(page, "reshape")) {
2255 int err;
2256 if (mddev->pers->start_reshape == NULL)
2257 return -EINVAL;
2258 err = mddev->pers->start_reshape(mddev);
2259 if (err)
2260 return err;
2261 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002262 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002263 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002264 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002265 return -EINVAL;
2266 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2267 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002268 }
NeilBrown03c902e2006-01-06 00:20:46 -08002269 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002270 md_wakeup_thread(mddev->thread);
2271 return len;
2272}
2273
NeilBrown9d888832005-11-08 21:39:26 -08002274static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002275mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002276{
2277 return sprintf(page, "%llu\n",
2278 (unsigned long long) mddev->resync_mismatches);
2279}
2280
NeilBrown96de1e62005-11-08 21:39:39 -08002281static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08002282md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002283
NeilBrown96de1e62005-11-08 21:39:39 -08002284
2285static struct md_sysfs_entry
2286md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002287
NeilBrown88202a02006-01-06 00:21:36 -08002288static ssize_t
2289sync_min_show(mddev_t *mddev, char *page)
2290{
2291 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2292 mddev->sync_speed_min ? "local": "system");
2293}
2294
2295static ssize_t
2296sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2297{
2298 int min;
2299 char *e;
2300 if (strncmp(buf, "system", 6)==0) {
2301 mddev->sync_speed_min = 0;
2302 return len;
2303 }
2304 min = simple_strtoul(buf, &e, 10);
2305 if (buf == e || (*e && *e != '\n') || min <= 0)
2306 return -EINVAL;
2307 mddev->sync_speed_min = min;
2308 return len;
2309}
2310
2311static struct md_sysfs_entry md_sync_min =
2312__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2313
2314static ssize_t
2315sync_max_show(mddev_t *mddev, char *page)
2316{
2317 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2318 mddev->sync_speed_max ? "local": "system");
2319}
2320
2321static ssize_t
2322sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2323{
2324 int max;
2325 char *e;
2326 if (strncmp(buf, "system", 6)==0) {
2327 mddev->sync_speed_max = 0;
2328 return len;
2329 }
2330 max = simple_strtoul(buf, &e, 10);
2331 if (buf == e || (*e && *e != '\n') || max <= 0)
2332 return -EINVAL;
2333 mddev->sync_speed_max = max;
2334 return len;
2335}
2336
2337static struct md_sysfs_entry md_sync_max =
2338__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2339
2340
2341static ssize_t
2342sync_speed_show(mddev_t *mddev, char *page)
2343{
2344 unsigned long resync, dt, db;
2345 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2346 dt = ((jiffies - mddev->resync_mark) / HZ);
2347 if (!dt) dt++;
2348 db = resync - (mddev->resync_mark_cnt);
2349 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2350}
2351
2352static struct md_sysfs_entry
2353md_sync_speed = __ATTR_RO(sync_speed);
2354
2355static ssize_t
2356sync_completed_show(mddev_t *mddev, char *page)
2357{
2358 unsigned long max_blocks, resync;
2359
2360 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2361 max_blocks = mddev->resync_max_sectors;
2362 else
2363 max_blocks = mddev->size << 1;
2364
2365 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2366 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2367}
2368
2369static struct md_sysfs_entry
2370md_sync_completed = __ATTR_RO(sync_completed);
2371
NeilBrowne464eaf2006-03-27 01:18:14 -08002372static ssize_t
2373suspend_lo_show(mddev_t *mddev, char *page)
2374{
2375 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2376}
2377
2378static ssize_t
2379suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2380{
2381 char *e;
2382 unsigned long long new = simple_strtoull(buf, &e, 10);
2383
2384 if (mddev->pers->quiesce == NULL)
2385 return -EINVAL;
2386 if (buf == e || (*e && *e != '\n'))
2387 return -EINVAL;
2388 if (new >= mddev->suspend_hi ||
2389 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2390 mddev->suspend_lo = new;
2391 mddev->pers->quiesce(mddev, 2);
2392 return len;
2393 } else
2394 return -EINVAL;
2395}
2396static struct md_sysfs_entry md_suspend_lo =
2397__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2398
2399
2400static ssize_t
2401suspend_hi_show(mddev_t *mddev, char *page)
2402{
2403 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2404}
2405
2406static ssize_t
2407suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2408{
2409 char *e;
2410 unsigned long long new = simple_strtoull(buf, &e, 10);
2411
2412 if (mddev->pers->quiesce == NULL)
2413 return -EINVAL;
2414 if (buf == e || (*e && *e != '\n'))
2415 return -EINVAL;
2416 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2417 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2418 mddev->suspend_hi = new;
2419 mddev->pers->quiesce(mddev, 1);
2420 mddev->pers->quiesce(mddev, 0);
2421 return len;
2422 } else
2423 return -EINVAL;
2424}
2425static struct md_sysfs_entry md_suspend_hi =
2426__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2427
2428
NeilBrowneae17012005-11-08 21:39:23 -08002429static struct attribute *md_default_attrs[] = {
2430 &md_level.attr,
2431 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002432 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002433 &md_size.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002434 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002435 &md_new_device.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002436 NULL,
2437};
2438
2439static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002440 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002441 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002442 &md_sync_min.attr,
2443 &md_sync_max.attr,
2444 &md_sync_speed.attr,
2445 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002446 &md_suspend_lo.attr,
2447 &md_suspend_hi.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002448 NULL,
2449};
NeilBrown411036f2005-11-08 21:39:40 -08002450static struct attribute_group md_redundancy_group = {
2451 .name = NULL,
2452 .attrs = md_redundancy_attrs,
2453};
2454
NeilBrowneae17012005-11-08 21:39:23 -08002455
2456static ssize_t
2457md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2458{
2459 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2460 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002461 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002462
2463 if (!entry->show)
2464 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002465 rv = mddev_lock(mddev);
2466 if (!rv) {
2467 rv = entry->show(mddev, page);
2468 mddev_unlock(mddev);
2469 }
NeilBrown96de1e62005-11-08 21:39:39 -08002470 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002471}
2472
2473static ssize_t
2474md_attr_store(struct kobject *kobj, struct attribute *attr,
2475 const char *page, size_t length)
2476{
2477 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2478 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002479 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002480
2481 if (!entry->store)
2482 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002483 rv = mddev_lock(mddev);
2484 if (!rv) {
2485 rv = entry->store(mddev, page, length);
2486 mddev_unlock(mddev);
2487 }
NeilBrown96de1e62005-11-08 21:39:39 -08002488 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002489}
2490
2491static void md_free(struct kobject *ko)
2492{
2493 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2494 kfree(mddev);
2495}
2496
2497static struct sysfs_ops md_sysfs_ops = {
2498 .show = md_attr_show,
2499 .store = md_attr_store,
2500};
2501static struct kobj_type md_ktype = {
2502 .release = md_free,
2503 .sysfs_ops = &md_sysfs_ops,
2504 .default_attrs = md_default_attrs,
2505};
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507int mdp_major = 0;
2508
2509static struct kobject *md_probe(dev_t dev, int *part, void *data)
2510{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002511 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 mddev_t *mddev = mddev_find(dev);
2513 struct gendisk *disk;
2514 int partitioned = (MAJOR(dev) != MD_MAJOR);
2515 int shift = partitioned ? MdpMinorShift : 0;
2516 int unit = MINOR(dev) >> shift;
2517
2518 if (!mddev)
2519 return NULL;
2520
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002521 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002523 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 mddev_put(mddev);
2525 return NULL;
2526 }
2527 disk = alloc_disk(1 << shift);
2528 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002529 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 mddev_put(mddev);
2531 return NULL;
2532 }
2533 disk->major = MAJOR(dev);
2534 disk->first_minor = unit << shift;
2535 if (partitioned) {
2536 sprintf(disk->disk_name, "md_d%d", unit);
2537 sprintf(disk->devfs_name, "md/d%d", unit);
2538 } else {
2539 sprintf(disk->disk_name, "md%d", unit);
2540 sprintf(disk->devfs_name, "md/%d", unit);
2541 }
2542 disk->fops = &md_fops;
2543 disk->private_data = mddev;
2544 disk->queue = mddev->queue;
2545 add_disk(disk);
2546 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002547 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002548 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002549 mddev->kobj.k_name = NULL;
2550 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2551 mddev->kobj.ktype = &md_ktype;
2552 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return NULL;
2554}
2555
2556void md_wakeup_thread(mdk_thread_t *thread);
2557
2558static void md_safemode_timeout(unsigned long data)
2559{
2560 mddev_t *mddev = (mddev_t *) data;
2561
2562 mddev->safemode = 1;
2563 md_wakeup_thread(mddev->thread);
2564}
2565
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002566static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568static int do_md_run(mddev_t * mddev)
2569{
NeilBrown2604b702006-01-06 00:20:36 -08002570 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 int chunk_size;
2572 struct list_head *tmp;
2573 mdk_rdev_t *rdev;
2574 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002575 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 char b[BDEVNAME_SIZE];
2577
NeilBrowna757e642005-04-16 15:26:42 -07002578 if (list_empty(&mddev->disks))
2579 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 if (mddev->pers)
2583 return -EBUSY;
2584
2585 /*
2586 * Analyze all RAID superblock(s)
2587 */
NeilBrowna757e642005-04-16 15:26:42 -07002588 if (!mddev->raid_disks)
2589 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
NeilBrown2604b702006-01-06 00:20:36 -08002593 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (chunk_size > MAX_CHUNK_SIZE) {
2595 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2596 chunk_size, MAX_CHUNK_SIZE);
2597 return -EINVAL;
2598 }
2599 /*
2600 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2601 */
2602 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07002603 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return -EINVAL;
2605 }
2606 if (chunk_size < PAGE_SIZE) {
2607 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2608 chunk_size, PAGE_SIZE);
2609 return -EINVAL;
2610 }
2611
2612 /* devices must have minimum size of one chunk */
2613 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002614 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 continue;
2616 if (rdev->size < chunk_size / 1024) {
2617 printk(KERN_WARNING
2618 "md: Dev %s smaller than chunk_size:"
2619 " %lluk < %dk\n",
2620 bdevname(rdev->bdev,b),
2621 (unsigned long long)rdev->size,
2622 chunk_size / 1024);
2623 return -EINVAL;
2624 }
2625 }
2626 }
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08002629 if (mddev->level != LEVEL_NONE)
2630 request_module("md-level-%d", mddev->level);
2631 else if (mddev->clevel[0])
2632 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633#endif
2634
2635 /*
2636 * Drop all container device buffers, from now on
2637 * the only valid external interface is through the md
2638 * device.
2639 * Also find largest hardsector size
2640 */
2641 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002642 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 continue;
2644 sync_blockdev(rdev->bdev);
2645 invalidate_bdev(rdev->bdev, 0);
2646 }
2647
2648 md_probe(mddev->unit, NULL, NULL);
2649 disk = mddev->gendisk;
2650 if (!disk)
2651 return -ENOMEM;
2652
2653 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002654 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08002655 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002657 if (mddev->level != LEVEL_NONE)
2658 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2659 mddev->level);
2660 else
2661 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
2662 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 return -EINVAL;
2664 }
NeilBrown2604b702006-01-06 00:20:36 -08002665 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002667 mddev->level = pers->level;
2668 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
NeilBrownf6705572006-03-27 01:18:11 -08002670 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08002671 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08002672 /* This personality cannot handle reshaping... */
2673 mddev->pers = NULL;
2674 module_put(pers->owner);
2675 return -EINVAL;
2676 }
2677
NeilBrown657390d2005-08-26 18:34:16 -07002678 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002680 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002681 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
NeilBrownf91de922005-11-08 21:39:36 -08002683 if (start_readonly)
2684 mddev->ro = 2; /* read-only, but switch on first write */
2685
NeilBrownb15c2e52006-01-06 00:20:16 -08002686 err = mddev->pers->run(mddev);
2687 if (!err && mddev->pers->sync_request) {
2688 err = bitmap_create(mddev);
2689 if (err) {
2690 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2691 mdname(mddev), err);
2692 mddev->pers->stop(mddev);
2693 }
2694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 if (err) {
2696 printk(KERN_ERR "md: pers->run() failed ...\n");
2697 module_put(mddev->pers->owner);
2698 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002699 bitmap_destroy(mddev);
2700 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 }
NeilBrown411036f2005-11-08 21:39:40 -08002702 if (mddev->pers->sync_request)
2703 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002704 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2705 mddev->ro = 0;
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 atomic_set(&mddev->writes_pending,0);
2708 mddev->safemode = 0;
2709 mddev->safemode_timer.function = md_safemode_timeout;
2710 mddev->safemode_timer.data = (unsigned long) mddev;
2711 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2712 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002713
2714 ITERATE_RDEV(mddev,rdev,tmp)
2715 if (rdev->raid_disk >= 0) {
2716 char nm[20];
2717 sprintf(nm, "rd%d", rdev->raid_disk);
2718 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002722 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
2724 if (mddev->sb_dirty)
2725 md_update_sb(mddev);
2726
2727 set_capacity(disk, mddev->array_size<<1);
2728
2729 /* If we call blk_queue_make_request here, it will
2730 * re-initialise max_sectors etc which may have been
2731 * refined inside -> run. So just set the bits we need to set.
2732 * Most initialisation happended when we called
2733 * blk_queue_make_request(..., md_fail_request)
2734 * earlier.
2735 */
2736 mddev->queue->queuedata = mddev;
2737 mddev->queue->make_request_fn = mddev->pers->make_request;
2738
2739 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08002740 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return 0;
2742}
2743
2744static int restart_array(mddev_t *mddev)
2745{
2746 struct gendisk *disk = mddev->gendisk;
2747 int err;
2748
2749 /*
2750 * Complain if it has no devices
2751 */
2752 err = -ENXIO;
2753 if (list_empty(&mddev->disks))
2754 goto out;
2755
2756 if (mddev->pers) {
2757 err = -EBUSY;
2758 if (!mddev->ro)
2759 goto out;
2760
2761 mddev->safemode = 0;
2762 mddev->ro = 0;
2763 set_disk_ro(disk, 0);
2764
2765 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2766 mdname(mddev));
2767 /*
2768 * Kick recovery or resync if necessary
2769 */
2770 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2771 md_wakeup_thread(mddev->thread);
2772 err = 0;
2773 } else {
2774 printk(KERN_ERR "md: %s has no personality assigned.\n",
2775 mdname(mddev));
2776 err = -EINVAL;
2777 }
2778
2779out:
2780 return err;
2781}
2782
2783static int do_md_stop(mddev_t * mddev, int ro)
2784{
2785 int err = 0;
2786 struct gendisk *disk = mddev->gendisk;
2787
2788 if (mddev->pers) {
2789 if (atomic_read(&mddev->active)>2) {
2790 printk("md: %s still in use.\n",mdname(mddev));
2791 return -EBUSY;
2792 }
2793
2794 if (mddev->sync_thread) {
2795 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2796 md_unregister_thread(mddev->sync_thread);
2797 mddev->sync_thread = NULL;
2798 }
2799
2800 del_timer_sync(&mddev->safemode_timer);
2801
2802 invalidate_partition(disk, 0);
2803
2804 if (ro) {
2805 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002806 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 goto out;
2808 mddev->ro = 1;
2809 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002810 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002811 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (mddev->ro)
2813 set_disk_ro(disk, 0);
2814 blk_queue_make_request(mddev->queue, md_fail_request);
2815 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002816 if (mddev->pers->sync_request)
2817 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 module_put(mddev->pers->owner);
2820 mddev->pers = NULL;
2821 if (mddev->ro)
2822 mddev->ro = 0;
2823 }
2824 if (!mddev->in_sync) {
2825 /* mark array as shutdown cleanly */
2826 mddev->in_sync = 1;
2827 md_update_sb(mddev);
2828 }
2829 if (ro)
2830 set_disk_ro(disk, 1);
2831 }
NeilBrown32a76272005-06-21 17:17:14 -07002832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 /*
2834 * Free resources if final stop
2835 */
2836 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002837 mdk_rdev_t *rdev;
2838 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 struct gendisk *disk;
2840 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2841
NeilBrown978f9462006-02-02 14:28:05 -08002842 bitmap_destroy(mddev);
2843 if (mddev->bitmap_file) {
2844 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2845 fput(mddev->bitmap_file);
2846 mddev->bitmap_file = NULL;
2847 }
2848 mddev->bitmap_offset = 0;
2849
NeilBrown86e6ffd2005-11-08 21:39:24 -08002850 ITERATE_RDEV(mddev,rdev,tmp)
2851 if (rdev->raid_disk >= 0) {
2852 char nm[20];
2853 sprintf(nm, "rd%d", rdev->raid_disk);
2854 sysfs_remove_link(&mddev->kobj, nm);
2855 }
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 export_array(mddev);
2858
2859 mddev->array_size = 0;
2860 disk = mddev->gendisk;
2861 if (disk)
2862 set_capacity(disk, 0);
2863 mddev->changed = 1;
2864 } else
2865 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2866 mdname(mddev));
2867 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08002868 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869out:
2870 return err;
2871}
2872
2873static void autorun_array(mddev_t *mddev)
2874{
2875 mdk_rdev_t *rdev;
2876 struct list_head *tmp;
2877 int err;
2878
NeilBrowna757e642005-04-16 15:26:42 -07002879 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 printk(KERN_INFO "md: running: ");
2883
2884 ITERATE_RDEV(mddev,rdev,tmp) {
2885 char b[BDEVNAME_SIZE];
2886 printk("<%s>", bdevname(rdev->bdev,b));
2887 }
2888 printk("\n");
2889
2890 err = do_md_run (mddev);
2891 if (err) {
2892 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2893 do_md_stop (mddev, 0);
2894 }
2895}
2896
2897/*
2898 * lets try to run arrays based on all disks that have arrived
2899 * until now. (those are in pending_raid_disks)
2900 *
2901 * the method: pick the first pending disk, collect all disks with
2902 * the same UUID, remove all from the pending list and put them into
2903 * the 'same_array' list. Then order this list based on superblock
2904 * update time (freshest comes first), kick out 'old' disks and
2905 * compare superblocks. If everything's fine then run it.
2906 *
2907 * If "unit" is allocated, then bump its reference count
2908 */
2909static void autorun_devices(int part)
2910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 struct list_head *tmp;
2912 mdk_rdev_t *rdev0, *rdev;
2913 mddev_t *mddev;
2914 char b[BDEVNAME_SIZE];
2915
2916 printk(KERN_INFO "md: autorun ...\n");
2917 while (!list_empty(&pending_raid_disks)) {
2918 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08002919 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 rdev0 = list_entry(pending_raid_disks.next,
2921 mdk_rdev_t, same_set);
2922
2923 printk(KERN_INFO "md: considering %s ...\n",
2924 bdevname(rdev0->bdev,b));
2925 INIT_LIST_HEAD(&candidates);
2926 ITERATE_RDEV_PENDING(rdev,tmp)
2927 if (super_90_load(rdev, rdev0, 0) >= 0) {
2928 printk(KERN_INFO "md: adding %s ...\n",
2929 bdevname(rdev->bdev,b));
2930 list_move(&rdev->same_set, &candidates);
2931 }
2932 /*
2933 * now we have a set of devices, with all of them having
2934 * mostly sane superblocks. It's time to allocate the
2935 * mddev.
2936 */
2937 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2938 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2939 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2940 break;
2941 }
2942 if (part)
2943 dev = MKDEV(mdp_major,
2944 rdev0->preferred_minor << MdpMinorShift);
2945 else
2946 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2947
2948 md_probe(dev, NULL, NULL);
2949 mddev = mddev_find(dev);
2950 if (!mddev) {
2951 printk(KERN_ERR
2952 "md: cannot allocate memory for md drive.\n");
2953 break;
2954 }
2955 if (mddev_lock(mddev))
2956 printk(KERN_WARNING "md: %s locked, cannot run\n",
2957 mdname(mddev));
2958 else if (mddev->raid_disks || mddev->major_version
2959 || !list_empty(&mddev->disks)) {
2960 printk(KERN_WARNING
2961 "md: %s already running, cannot run %s\n",
2962 mdname(mddev), bdevname(rdev0->bdev,b));
2963 mddev_unlock(mddev);
2964 } else {
2965 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2966 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2967 list_del_init(&rdev->same_set);
2968 if (bind_rdev_to_array(rdev, mddev))
2969 export_rdev(rdev);
2970 }
2971 autorun_array(mddev);
2972 mddev_unlock(mddev);
2973 }
2974 /* on success, candidates will be empty, on error
2975 * it won't...
2976 */
2977 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2978 export_rdev(rdev);
2979 mddev_put(mddev);
2980 }
2981 printk(KERN_INFO "md: ... autorun DONE.\n");
2982}
2983
2984/*
2985 * import RAID devices based on one partition
2986 * if possible, the array gets run as well.
2987 */
2988
2989static int autostart_array(dev_t startdev)
2990{
2991 char b[BDEVNAME_SIZE];
2992 int err = -EINVAL, i;
2993 mdp_super_t *sb = NULL;
2994 mdk_rdev_t *start_rdev = NULL, *rdev;
2995
2996 start_rdev = md_import_device(startdev, 0, 0);
2997 if (IS_ERR(start_rdev))
2998 return err;
2999
3000
3001 /* NOTE: this can only work for 0.90.0 superblocks */
3002 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
3003 if (sb->major_version != 0 ||
3004 sb->minor_version != 90 ) {
3005 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
3006 export_rdev(start_rdev);
3007 return err;
3008 }
3009
NeilBrownb2d444d2005-11-08 21:39:31 -08003010 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 printk(KERN_WARNING
3012 "md: can not autostart based on faulty %s!\n",
3013 bdevname(start_rdev->bdev,b));
3014 export_rdev(start_rdev);
3015 return err;
3016 }
3017 list_add(&start_rdev->same_set, &pending_raid_disks);
3018
3019 for (i = 0; i < MD_SB_DISKS; i++) {
3020 mdp_disk_t *desc = sb->disks + i;
3021 dev_t dev = MKDEV(desc->major, desc->minor);
3022
3023 if (!dev)
3024 continue;
3025 if (dev == startdev)
3026 continue;
3027 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3028 continue;
3029 rdev = md_import_device(dev, 0, 0);
3030 if (IS_ERR(rdev))
3031 continue;
3032
3033 list_add(&rdev->same_set, &pending_raid_disks);
3034 }
3035
3036 /*
3037 * possibly return codes
3038 */
3039 autorun_devices(0);
3040 return 0;
3041
3042}
3043
3044
3045static int get_version(void __user * arg)
3046{
3047 mdu_version_t ver;
3048
3049 ver.major = MD_MAJOR_VERSION;
3050 ver.minor = MD_MINOR_VERSION;
3051 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3052
3053 if (copy_to_user(arg, &ver, sizeof(ver)))
3054 return -EFAULT;
3055
3056 return 0;
3057}
3058
3059static int get_array_info(mddev_t * mddev, void __user * arg)
3060{
3061 mdu_array_info_t info;
3062 int nr,working,active,failed,spare;
3063 mdk_rdev_t *rdev;
3064 struct list_head *tmp;
3065
3066 nr=working=active=failed=spare=0;
3067 ITERATE_RDEV(mddev,rdev,tmp) {
3068 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003069 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 failed++;
3071 else {
3072 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003073 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 active++;
3075 else
3076 spare++;
3077 }
3078 }
3079
3080 info.major_version = mddev->major_version;
3081 info.minor_version = mddev->minor_version;
3082 info.patch_version = MD_PATCHLEVEL_VERSION;
3083 info.ctime = mddev->ctime;
3084 info.level = mddev->level;
3085 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003086 if (info.size != mddev->size) /* overflow */
3087 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 info.nr_disks = nr;
3089 info.raid_disks = mddev->raid_disks;
3090 info.md_minor = mddev->md_minor;
3091 info.not_persistent= !mddev->persistent;
3092
3093 info.utime = mddev->utime;
3094 info.state = 0;
3095 if (mddev->in_sync)
3096 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003097 if (mddev->bitmap && mddev->bitmap_offset)
3098 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 info.active_disks = active;
3100 info.working_disks = working;
3101 info.failed_disks = failed;
3102 info.spare_disks = spare;
3103
3104 info.layout = mddev->layout;
3105 info.chunk_size = mddev->chunk_size;
3106
3107 if (copy_to_user(arg, &info, sizeof(info)))
3108 return -EFAULT;
3109
3110 return 0;
3111}
3112
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003113static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003114{
3115 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3116 char *ptr, *buf = NULL;
3117 int err = -ENOMEM;
3118
3119 file = kmalloc(sizeof(*file), GFP_KERNEL);
3120 if (!file)
3121 goto out;
3122
3123 /* bitmap disabled, zero the first byte and copy out */
3124 if (!mddev->bitmap || !mddev->bitmap->file) {
3125 file->pathname[0] = '\0';
3126 goto copy_out;
3127 }
3128
3129 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3130 if (!buf)
3131 goto out;
3132
3133 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3134 if (!ptr)
3135 goto out;
3136
3137 strcpy(file->pathname, ptr);
3138
3139copy_out:
3140 err = 0;
3141 if (copy_to_user(arg, file, sizeof(*file)))
3142 err = -EFAULT;
3143out:
3144 kfree(buf);
3145 kfree(file);
3146 return err;
3147}
3148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149static int get_disk_info(mddev_t * mddev, void __user * arg)
3150{
3151 mdu_disk_info_t info;
3152 unsigned int nr;
3153 mdk_rdev_t *rdev;
3154
3155 if (copy_from_user(&info, arg, sizeof(info)))
3156 return -EFAULT;
3157
3158 nr = info.number;
3159
3160 rdev = find_rdev_nr(mddev, nr);
3161 if (rdev) {
3162 info.major = MAJOR(rdev->bdev->bd_dev);
3163 info.minor = MINOR(rdev->bdev->bd_dev);
3164 info.raid_disk = rdev->raid_disk;
3165 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003166 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003168 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 info.state |= (1<<MD_DISK_ACTIVE);
3170 info.state |= (1<<MD_DISK_SYNC);
3171 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003172 if (test_bit(WriteMostly, &rdev->flags))
3173 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 } else {
3175 info.major = info.minor = 0;
3176 info.raid_disk = -1;
3177 info.state = (1<<MD_DISK_REMOVED);
3178 }
3179
3180 if (copy_to_user(arg, &info, sizeof(info)))
3181 return -EFAULT;
3182
3183 return 0;
3184}
3185
3186static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3187{
3188 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3189 mdk_rdev_t *rdev;
3190 dev_t dev = MKDEV(info->major,info->minor);
3191
3192 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3193 return -EOVERFLOW;
3194
3195 if (!mddev->raid_disks) {
3196 int err;
3197 /* expecting a device which has a superblock */
3198 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3199 if (IS_ERR(rdev)) {
3200 printk(KERN_WARNING
3201 "md: md_import_device returned %ld\n",
3202 PTR_ERR(rdev));
3203 return PTR_ERR(rdev);
3204 }
3205 if (!list_empty(&mddev->disks)) {
3206 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3207 mdk_rdev_t, same_set);
3208 int err = super_types[mddev->major_version]
3209 .load_super(rdev, rdev0, mddev->minor_version);
3210 if (err < 0) {
3211 printk(KERN_WARNING
3212 "md: %s has different UUID to %s\n",
3213 bdevname(rdev->bdev,b),
3214 bdevname(rdev0->bdev,b2));
3215 export_rdev(rdev);
3216 return -EINVAL;
3217 }
3218 }
3219 err = bind_rdev_to_array(rdev, mddev);
3220 if (err)
3221 export_rdev(rdev);
3222 return err;
3223 }
3224
3225 /*
3226 * add_new_disk can be used once the array is assembled
3227 * to add "hot spares". They must already have a superblock
3228 * written
3229 */
3230 if (mddev->pers) {
3231 int err;
3232 if (!mddev->pers->hot_add_disk) {
3233 printk(KERN_WARNING
3234 "%s: personality does not support diskops!\n",
3235 mdname(mddev));
3236 return -EINVAL;
3237 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003238 if (mddev->persistent)
3239 rdev = md_import_device(dev, mddev->major_version,
3240 mddev->minor_version);
3241 else
3242 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 if (IS_ERR(rdev)) {
3244 printk(KERN_WARNING
3245 "md: md_import_device returned %ld\n",
3246 PTR_ERR(rdev));
3247 return PTR_ERR(rdev);
3248 }
NeilBrown41158c72005-06-21 17:17:25 -07003249 /* set save_raid_disk if appropriate */
3250 if (!mddev->persistent) {
3251 if (info->state & (1<<MD_DISK_SYNC) &&
3252 info->raid_disk < mddev->raid_disks)
3253 rdev->raid_disk = info->raid_disk;
3254 else
3255 rdev->raid_disk = -1;
3256 } else
3257 super_types[mddev->major_version].
3258 validate_super(mddev, rdev);
3259 rdev->saved_raid_disk = rdev->raid_disk;
3260
NeilBrownb2d444d2005-11-08 21:39:31 -08003261 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003262 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3263 set_bit(WriteMostly, &rdev->flags);
3264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 rdev->raid_disk = -1;
3266 err = bind_rdev_to_array(rdev, mddev);
3267 if (err)
3268 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003269
3270 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003271 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return err;
3273 }
3274
3275 /* otherwise, add_new_disk is only allowed
3276 * for major_version==0 superblocks
3277 */
3278 if (mddev->major_version != 0) {
3279 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3280 mdname(mddev));
3281 return -EINVAL;
3282 }
3283
3284 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3285 int err;
3286 rdev = md_import_device (dev, -1, 0);
3287 if (IS_ERR(rdev)) {
3288 printk(KERN_WARNING
3289 "md: error, md_import_device() returned %ld\n",
3290 PTR_ERR(rdev));
3291 return PTR_ERR(rdev);
3292 }
3293 rdev->desc_nr = info->number;
3294 if (info->raid_disk < mddev->raid_disks)
3295 rdev->raid_disk = info->raid_disk;
3296 else
3297 rdev->raid_disk = -1;
3298
NeilBrownb2d444d2005-11-08 21:39:31 -08003299 rdev->flags = 0;
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003302 if (info->state & (1<<MD_DISK_SYNC))
3303 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003305 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3306 set_bit(WriteMostly, &rdev->flags);
3307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 if (!mddev->persistent) {
3309 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3310 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3311 } else
3312 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3313 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3314
NeilBrown2bf071b2006-01-06 00:20:55 -08003315 err = bind_rdev_to_array(rdev, mddev);
3316 if (err) {
3317 export_rdev(rdev);
3318 return err;
3319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 }
3321
3322 return 0;
3323}
3324
3325static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3326{
3327 char b[BDEVNAME_SIZE];
3328 mdk_rdev_t *rdev;
3329
3330 if (!mddev->pers)
3331 return -ENODEV;
3332
3333 rdev = find_rdev(mddev, dev);
3334 if (!rdev)
3335 return -ENXIO;
3336
3337 if (rdev->raid_disk >= 0)
3338 goto busy;
3339
3340 kick_rdev_from_array(rdev);
3341 md_update_sb(mddev);
NeilBrownd7603b72006-01-06 00:20:30 -08003342 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
3344 return 0;
3345busy:
3346 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3347 bdevname(rdev->bdev,b), mdname(mddev));
3348 return -EBUSY;
3349}
3350
3351static int hot_add_disk(mddev_t * mddev, dev_t dev)
3352{
3353 char b[BDEVNAME_SIZE];
3354 int err;
3355 unsigned int size;
3356 mdk_rdev_t *rdev;
3357
3358 if (!mddev->pers)
3359 return -ENODEV;
3360
3361 if (mddev->major_version != 0) {
3362 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3363 " version-0 superblocks.\n",
3364 mdname(mddev));
3365 return -EINVAL;
3366 }
3367 if (!mddev->pers->hot_add_disk) {
3368 printk(KERN_WARNING
3369 "%s: personality does not support diskops!\n",
3370 mdname(mddev));
3371 return -EINVAL;
3372 }
3373
3374 rdev = md_import_device (dev, -1, 0);
3375 if (IS_ERR(rdev)) {
3376 printk(KERN_WARNING
3377 "md: error, md_import_device() returned %ld\n",
3378 PTR_ERR(rdev));
3379 return -EINVAL;
3380 }
3381
3382 if (mddev->persistent)
3383 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3384 else
3385 rdev->sb_offset =
3386 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3387
3388 size = calc_dev_size(rdev, mddev->chunk_size);
3389 rdev->size = size;
3390
NeilBrownb2d444d2005-11-08 21:39:31 -08003391 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 printk(KERN_WARNING
3393 "md: can not hot-add faulty %s disk to %s!\n",
3394 bdevname(rdev->bdev,b), mdname(mddev));
3395 err = -EINVAL;
3396 goto abort_export;
3397 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003398 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 rdev->desc_nr = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003400 err = bind_rdev_to_array(rdev, mddev);
3401 if (err)
3402 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
3404 /*
3405 * The rest should better be atomic, we can have disk failures
3406 * noticed in interrupt contexts ...
3407 */
3408
3409 if (rdev->desc_nr == mddev->max_disks) {
3410 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3411 mdname(mddev));
3412 err = -EBUSY;
3413 goto abort_unbind_export;
3414 }
3415
3416 rdev->raid_disk = -1;
3417
3418 md_update_sb(mddev);
3419
3420 /*
3421 * Kick recovery, maybe this spare has to be added to the
3422 * array immediately.
3423 */
3424 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3425 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003426 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 return 0;
3428
3429abort_unbind_export:
3430 unbind_rdev_from_array(rdev);
3431
3432abort_export:
3433 export_rdev(rdev);
3434 return err;
3435}
3436
NeilBrown32a76272005-06-21 17:17:14 -07003437/* similar to deny_write_access, but accounts for our holding a reference
3438 * to the file ourselves */
3439static int deny_bitmap_write_access(struct file * file)
3440{
3441 struct inode *inode = file->f_mapping->host;
3442
3443 spin_lock(&inode->i_lock);
3444 if (atomic_read(&inode->i_writecount) > 1) {
3445 spin_unlock(&inode->i_lock);
3446 return -ETXTBSY;
3447 }
3448 atomic_set(&inode->i_writecount, -1);
3449 spin_unlock(&inode->i_lock);
3450
3451 return 0;
3452}
3453
3454static int set_bitmap_file(mddev_t *mddev, int fd)
3455{
3456 int err;
3457
NeilBrown36fa3062005-09-09 16:23:45 -07003458 if (mddev->pers) {
3459 if (!mddev->pers->quiesce)
3460 return -EBUSY;
3461 if (mddev->recovery || mddev->sync_thread)
3462 return -EBUSY;
3463 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003464 }
3465
NeilBrown36fa3062005-09-09 16:23:45 -07003466
3467 if (fd >= 0) {
3468 if (mddev->bitmap)
3469 return -EEXIST; /* cannot add when bitmap is present */
3470 mddev->bitmap_file = fget(fd);
3471
3472 if (mddev->bitmap_file == NULL) {
3473 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3474 mdname(mddev));
3475 return -EBADF;
3476 }
3477
3478 err = deny_bitmap_write_access(mddev->bitmap_file);
3479 if (err) {
3480 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3481 mdname(mddev));
3482 fput(mddev->bitmap_file);
3483 mddev->bitmap_file = NULL;
3484 return err;
3485 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003486 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003487 } else if (mddev->bitmap == NULL)
3488 return -ENOENT; /* cannot remove what isn't there */
3489 err = 0;
3490 if (mddev->pers) {
3491 mddev->pers->quiesce(mddev, 1);
3492 if (fd >= 0)
3493 err = bitmap_create(mddev);
3494 if (fd < 0 || err)
3495 bitmap_destroy(mddev);
3496 mddev->pers->quiesce(mddev, 0);
3497 } else if (fd < 0) {
3498 if (mddev->bitmap_file)
3499 fput(mddev->bitmap_file);
3500 mddev->bitmap_file = NULL;
3501 }
3502
NeilBrown32a76272005-06-21 17:17:14 -07003503 return err;
3504}
3505
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506/*
3507 * set_array_info is used two different ways
3508 * The original usage is when creating a new array.
3509 * In this usage, raid_disks is > 0 and it together with
3510 * level, size, not_persistent,layout,chunksize determine the
3511 * shape of the array.
3512 * This will always create an array with a type-0.90.0 superblock.
3513 * The newer usage is when assembling an array.
3514 * In this case raid_disks will be 0, and the major_version field is
3515 * use to determine which style super-blocks are to be found on the devices.
3516 * The minor and patch _version numbers are also kept incase the
3517 * super_block handler wishes to interpret them.
3518 */
3519static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3520{
3521
3522 if (info->raid_disks == 0) {
3523 /* just setting version number for superblock loading */
3524 if (info->major_version < 0 ||
3525 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3526 super_types[info->major_version].name == NULL) {
3527 /* maybe try to auto-load a module? */
3528 printk(KERN_INFO
3529 "md: superblock version %d not known\n",
3530 info->major_version);
3531 return -EINVAL;
3532 }
3533 mddev->major_version = info->major_version;
3534 mddev->minor_version = info->minor_version;
3535 mddev->patch_version = info->patch_version;
3536 return 0;
3537 }
3538 mddev->major_version = MD_MAJOR_VERSION;
3539 mddev->minor_version = MD_MINOR_VERSION;
3540 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3541 mddev->ctime = get_seconds();
3542
3543 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08003544 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 mddev->size = info->size;
3546 mddev->raid_disks = info->raid_disks;
3547 /* don't set md_minor, it is determined by which /dev/md* was
3548 * openned
3549 */
3550 if (info->state & (1<<MD_SB_CLEAN))
3551 mddev->recovery_cp = MaxSector;
3552 else
3553 mddev->recovery_cp = 0;
3554 mddev->persistent = ! info->not_persistent;
3555
3556 mddev->layout = info->layout;
3557 mddev->chunk_size = info->chunk_size;
3558
3559 mddev->max_disks = MD_SB_DISKS;
3560
3561 mddev->sb_dirty = 1;
3562
NeilBrownb2a27032005-11-28 13:44:12 -08003563 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3564 mddev->bitmap_offset = 0;
3565
NeilBrownf6705572006-03-27 01:18:11 -08003566 mddev->reshape_position = MaxSector;
3567
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 /*
3569 * Generate a 128 bit UUID
3570 */
3571 get_random_bytes(mddev->uuid, 16);
3572
NeilBrownf6705572006-03-27 01:18:11 -08003573 mddev->new_level = mddev->level;
3574 mddev->new_chunk = mddev->chunk_size;
3575 mddev->new_layout = mddev->layout;
3576 mddev->delta_disks = 0;
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 return 0;
3579}
3580
NeilBrowna35b0d62006-01-06 00:20:49 -08003581static int update_size(mddev_t *mddev, unsigned long size)
3582{
3583 mdk_rdev_t * rdev;
3584 int rv;
3585 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08003586 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08003587
3588 if (mddev->pers->resize == NULL)
3589 return -EINVAL;
3590 /* The "size" is the amount of each device that is used.
3591 * This can only make sense for arrays with redundancy.
3592 * linear and raid0 always use whatever space is available
3593 * We can only consider changing the size if no resync
3594 * or reconstruction is happening, and if the new size
3595 * is acceptable. It must fit before the sb_offset or,
3596 * if that is <data_offset, it must fit before the
3597 * size of each device.
3598 * If size is zero, we find the largest size that fits.
3599 */
3600 if (mddev->sync_thread)
3601 return -EBUSY;
3602 ITERATE_RDEV(mddev,rdev,tmp) {
3603 sector_t avail;
NeilBrowna35b0d62006-01-06 00:20:49 -08003604 if (rdev->sb_offset > rdev->data_offset)
3605 avail = (rdev->sb_offset*2) - rdev->data_offset;
3606 else
3607 avail = get_capacity(rdev->bdev->bd_disk)
3608 - rdev->data_offset;
3609 if (fit && (size == 0 || size > avail/2))
3610 size = avail/2;
3611 if (avail < ((sector_t)size << 1))
3612 return -ENOSPC;
3613 }
3614 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3615 if (!rv) {
3616 struct block_device *bdev;
3617
3618 bdev = bdget_disk(mddev->gendisk, 0);
3619 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003620 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08003621 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003622 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08003623 bdput(bdev);
3624 }
3625 }
3626 return rv;
3627}
3628
NeilBrownda943b992006-01-06 00:20:54 -08003629static int update_raid_disks(mddev_t *mddev, int raid_disks)
3630{
3631 int rv;
3632 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08003633 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08003634 return -EINVAL;
3635 if (raid_disks <= 0 ||
3636 raid_disks >= mddev->max_disks)
3637 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08003638 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08003639 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08003640 mddev->delta_disks = raid_disks - mddev->raid_disks;
3641
3642 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08003643 return rv;
3644}
3645
3646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647/*
3648 * update_array_info is used to change the configuration of an
3649 * on-line array.
3650 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3651 * fields in the info are checked against the array.
3652 * Any differences that cannot be handled will cause an error.
3653 * Normally, only one change can be managed at a time.
3654 */
3655static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3656{
3657 int rv = 0;
3658 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07003659 int state = 0;
3660
3661 /* calculate expected state,ignoring low bits */
3662 if (mddev->bitmap && mddev->bitmap_offset)
3663 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
3665 if (mddev->major_version != info->major_version ||
3666 mddev->minor_version != info->minor_version ||
3667/* mddev->patch_version != info->patch_version || */
3668 mddev->ctime != info->ctime ||
3669 mddev->level != info->level ||
3670/* mddev->layout != info->layout || */
3671 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07003672 mddev->chunk_size != info->chunk_size ||
3673 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3674 ((state^info->state) & 0xfffffe00)
3675 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return -EINVAL;
3677 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08003678 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 if (mddev->raid_disks != info->raid_disks) cnt++;
3680 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07003681 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 if (cnt == 0) return 0;
3683 if (cnt > 1) return -EINVAL;
3684
3685 if (mddev->layout != info->layout) {
3686 /* Change layout
3687 * we don't need to do anything at the md level, the
3688 * personality will take care of it all.
3689 */
3690 if (mddev->pers->reconfig == NULL)
3691 return -EINVAL;
3692 else
3693 return mddev->pers->reconfig(mddev, info->layout, -1);
3694 }
NeilBrown284ae7c2006-02-03 03:03:40 -08003695 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08003696 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
NeilBrownda943b992006-01-06 00:20:54 -08003698 if (mddev->raid_disks != info->raid_disks)
3699 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
NeilBrown36fa3062005-09-09 16:23:45 -07003701 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3702 if (mddev->pers->quiesce == NULL)
3703 return -EINVAL;
3704 if (mddev->recovery || mddev->sync_thread)
3705 return -EBUSY;
3706 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3707 /* add the bitmap */
3708 if (mddev->bitmap)
3709 return -EEXIST;
3710 if (mddev->default_bitmap_offset == 0)
3711 return -EINVAL;
3712 mddev->bitmap_offset = mddev->default_bitmap_offset;
3713 mddev->pers->quiesce(mddev, 1);
3714 rv = bitmap_create(mddev);
3715 if (rv)
3716 bitmap_destroy(mddev);
3717 mddev->pers->quiesce(mddev, 0);
3718 } else {
3719 /* remove the bitmap */
3720 if (!mddev->bitmap)
3721 return -ENOENT;
3722 if (mddev->bitmap->file)
3723 return -EINVAL;
3724 mddev->pers->quiesce(mddev, 1);
3725 bitmap_destroy(mddev);
3726 mddev->pers->quiesce(mddev, 0);
3727 mddev->bitmap_offset = 0;
3728 }
3729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 md_update_sb(mddev);
3731 return rv;
3732}
3733
3734static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3735{
3736 mdk_rdev_t *rdev;
3737
3738 if (mddev->pers == NULL)
3739 return -ENODEV;
3740
3741 rdev = find_rdev(mddev, dev);
3742 if (!rdev)
3743 return -ENODEV;
3744
3745 md_error(mddev, rdev);
3746 return 0;
3747}
3748
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003749static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3750{
3751 mddev_t *mddev = bdev->bd_disk->private_data;
3752
3753 geo->heads = 2;
3754 geo->sectors = 4;
3755 geo->cylinders = get_capacity(mddev->gendisk) / 8;
3756 return 0;
3757}
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759static int md_ioctl(struct inode *inode, struct file *file,
3760 unsigned int cmd, unsigned long arg)
3761{
3762 int err = 0;
3763 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 mddev_t *mddev = NULL;
3765
3766 if (!capable(CAP_SYS_ADMIN))
3767 return -EACCES;
3768
3769 /*
3770 * Commands dealing with the RAID driver but not any
3771 * particular array:
3772 */
3773 switch (cmd)
3774 {
3775 case RAID_VERSION:
3776 err = get_version(argp);
3777 goto done;
3778
3779 case PRINT_RAID_DEBUG:
3780 err = 0;
3781 md_print_devices();
3782 goto done;
3783
3784#ifndef MODULE
3785 case RAID_AUTORUN:
3786 err = 0;
3787 autostart_arrays(arg);
3788 goto done;
3789#endif
3790 default:;
3791 }
3792
3793 /*
3794 * Commands creating/starting a new array:
3795 */
3796
3797 mddev = inode->i_bdev->bd_disk->private_data;
3798
3799 if (!mddev) {
3800 BUG();
3801 goto abort;
3802 }
3803
3804
3805 if (cmd == START_ARRAY) {
3806 /* START_ARRAY doesn't need to lock the array as autostart_array
3807 * does the locking, and it could even be a different array
3808 */
3809 static int cnt = 3;
3810 if (cnt > 0 ) {
3811 printk(KERN_WARNING
3812 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003813 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 current->comm, current->pid);
3815 cnt--;
3816 }
3817 err = autostart_array(new_decode_dev(arg));
3818 if (err) {
3819 printk(KERN_WARNING "md: autostart failed!\n");
3820 goto abort;
3821 }
3822 goto done;
3823 }
3824
3825 err = mddev_lock(mddev);
3826 if (err) {
3827 printk(KERN_INFO
3828 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3829 err, cmd);
3830 goto abort;
3831 }
3832
3833 switch (cmd)
3834 {
3835 case SET_ARRAY_INFO:
3836 {
3837 mdu_array_info_t info;
3838 if (!arg)
3839 memset(&info, 0, sizeof(info));
3840 else if (copy_from_user(&info, argp, sizeof(info))) {
3841 err = -EFAULT;
3842 goto abort_unlock;
3843 }
3844 if (mddev->pers) {
3845 err = update_array_info(mddev, &info);
3846 if (err) {
3847 printk(KERN_WARNING "md: couldn't update"
3848 " array info. %d\n", err);
3849 goto abort_unlock;
3850 }
3851 goto done_unlock;
3852 }
3853 if (!list_empty(&mddev->disks)) {
3854 printk(KERN_WARNING
3855 "md: array %s already has disks!\n",
3856 mdname(mddev));
3857 err = -EBUSY;
3858 goto abort_unlock;
3859 }
3860 if (mddev->raid_disks) {
3861 printk(KERN_WARNING
3862 "md: array %s already initialised!\n",
3863 mdname(mddev));
3864 err = -EBUSY;
3865 goto abort_unlock;
3866 }
3867 err = set_array_info(mddev, &info);
3868 if (err) {
3869 printk(KERN_WARNING "md: couldn't set"
3870 " array info. %d\n", err);
3871 goto abort_unlock;
3872 }
3873 }
3874 goto done_unlock;
3875
3876 default:;
3877 }
3878
3879 /*
3880 * Commands querying/configuring an existing array:
3881 */
NeilBrown32a76272005-06-21 17:17:14 -07003882 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3883 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3884 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3885 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 err = -ENODEV;
3887 goto abort_unlock;
3888 }
3889
3890 /*
3891 * Commands even a read-only array can execute:
3892 */
3893 switch (cmd)
3894 {
3895 case GET_ARRAY_INFO:
3896 err = get_array_info(mddev, argp);
3897 goto done_unlock;
3898
NeilBrown32a76272005-06-21 17:17:14 -07003899 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003900 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003901 goto done_unlock;
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 case GET_DISK_INFO:
3904 err = get_disk_info(mddev, argp);
3905 goto done_unlock;
3906
3907 case RESTART_ARRAY_RW:
3908 err = restart_array(mddev);
3909 goto done_unlock;
3910
3911 case STOP_ARRAY:
3912 err = do_md_stop (mddev, 0);
3913 goto done_unlock;
3914
3915 case STOP_ARRAY_RO:
3916 err = do_md_stop (mddev, 1);
3917 goto done_unlock;
3918
3919 /*
3920 * We have a problem here : there is no easy way to give a CHS
3921 * virtual geometry. We currently pretend that we have a 2 heads
3922 * 4 sectors (with a BIG number of cylinders...). This drives
3923 * dosfs just mad... ;-)
3924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
3926
3927 /*
3928 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003929 * superblock, so we do not allow them on read-only arrays.
3930 * However non-MD ioctls (e.g. get-size) will still come through
3931 * here and hit the 'default' below, so only disallow
3932 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 */
NeilBrownf91de922005-11-08 21:39:36 -08003934 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3935 mddev->ro && mddev->pers) {
3936 if (mddev->ro == 2) {
3937 mddev->ro = 0;
3938 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3939 md_wakeup_thread(mddev->thread);
3940
3941 } else {
3942 err = -EROFS;
3943 goto abort_unlock;
3944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 }
3946
3947 switch (cmd)
3948 {
3949 case ADD_NEW_DISK:
3950 {
3951 mdu_disk_info_t info;
3952 if (copy_from_user(&info, argp, sizeof(info)))
3953 err = -EFAULT;
3954 else
3955 err = add_new_disk(mddev, &info);
3956 goto done_unlock;
3957 }
3958
3959 case HOT_REMOVE_DISK:
3960 err = hot_remove_disk(mddev, new_decode_dev(arg));
3961 goto done_unlock;
3962
3963 case HOT_ADD_DISK:
3964 err = hot_add_disk(mddev, new_decode_dev(arg));
3965 goto done_unlock;
3966
3967 case SET_DISK_FAULTY:
3968 err = set_disk_faulty(mddev, new_decode_dev(arg));
3969 goto done_unlock;
3970
3971 case RUN_ARRAY:
3972 err = do_md_run (mddev);
3973 goto done_unlock;
3974
NeilBrown32a76272005-06-21 17:17:14 -07003975 case SET_BITMAP_FILE:
3976 err = set_bitmap_file(mddev, (int)arg);
3977 goto done_unlock;
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 default:
3980 if (_IOC_TYPE(cmd) == MD_MAJOR)
3981 printk(KERN_WARNING "md: %s(pid %d) used"
3982 " obsolete MD ioctl, upgrade your"
3983 " software to use new ictls.\n",
3984 current->comm, current->pid);
3985 err = -EINVAL;
3986 goto abort_unlock;
3987 }
3988
3989done_unlock:
3990abort_unlock:
3991 mddev_unlock(mddev);
3992
3993 return err;
3994done:
3995 if (err)
3996 MD_BUG();
3997abort:
3998 return err;
3999}
4000
4001static int md_open(struct inode *inode, struct file *file)
4002{
4003 /*
4004 * Succeed if we can lock the mddev, which confirms that
4005 * it isn't being stopped right now.
4006 */
4007 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4008 int err;
4009
4010 if ((err = mddev_lock(mddev)))
4011 goto out;
4012
4013 err = 0;
4014 mddev_get(mddev);
4015 mddev_unlock(mddev);
4016
4017 check_disk_change(inode->i_bdev);
4018 out:
4019 return err;
4020}
4021
4022static int md_release(struct inode *inode, struct file * file)
4023{
4024 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4025
4026 if (!mddev)
4027 BUG();
4028 mddev_put(mddev);
4029
4030 return 0;
4031}
4032
4033static int md_media_changed(struct gendisk *disk)
4034{
4035 mddev_t *mddev = disk->private_data;
4036
4037 return mddev->changed;
4038}
4039
4040static int md_revalidate(struct gendisk *disk)
4041{
4042 mddev_t *mddev = disk->private_data;
4043
4044 mddev->changed = 0;
4045 return 0;
4046}
4047static struct block_device_operations md_fops =
4048{
4049 .owner = THIS_MODULE,
4050 .open = md_open,
4051 .release = md_release,
4052 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004053 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 .media_changed = md_media_changed,
4055 .revalidate_disk= md_revalidate,
4056};
4057
Adrian Bunk75c96f82005-05-05 16:16:09 -07004058static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059{
4060 mdk_thread_t *thread = arg;
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 /*
4063 * md_thread is a 'system-thread', it's priority should be very
4064 * high. We avoid resource deadlocks individually in each
4065 * raid personality. (RAID5 does preallocation) We also use RR and
4066 * the very same RT priority as kswapd, thus we will never get
4067 * into a priority inversion deadlock.
4068 *
4069 * we definitely have to have equal or higher priority than
4070 * bdflush, otherwise bdflush will deadlock if there are too
4071 * many dirty RAID5 blocks.
4072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
NeilBrown6985c432005-10-19 21:23:47 -07004074 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004075 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
NeilBrown93588e22005-11-15 00:09:12 -08004077 /* We need to wait INTERRUPTIBLE so that
4078 * we don't add to the load-average.
4079 * That means we need to be sure no signals are
4080 * pending
4081 */
4082 if (signal_pending(current))
4083 flush_signals(current);
4084
4085 wait_event_interruptible_timeout
4086 (thread->wqueue,
4087 test_bit(THREAD_WAKEUP, &thread->flags)
4088 || kthread_should_stop(),
4089 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004090 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
4092 clear_bit(THREAD_WAKEUP, &thread->flags);
4093
NeilBrown787453c2005-11-08 21:39:43 -08004094 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004096
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return 0;
4098}
4099
4100void md_wakeup_thread(mdk_thread_t *thread)
4101{
4102 if (thread) {
4103 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4104 set_bit(THREAD_WAKEUP, &thread->flags);
4105 wake_up(&thread->wqueue);
4106 }
4107}
4108
4109mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4110 const char *name)
4111{
4112 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
NeilBrown9ffae0c2006-01-06 00:20:32 -08004114 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 if (!thread)
4116 return NULL;
4117
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 init_waitqueue_head(&thread->wqueue);
4119
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 thread->run = run;
4121 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004122 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004123 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004124 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 kfree(thread);
4126 return NULL;
4127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 return thread;
4129}
4130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131void md_unregister_thread(mdk_thread_t *thread)
4132{
NeilBrownd28446f2005-04-16 15:26:41 -07004133 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004134
4135 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 kfree(thread);
4137}
4138
4139void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4140{
4141 if (!mddev) {
4142 MD_BUG();
4143 return;
4144 }
4145
NeilBrownb2d444d2005-11-08 21:39:31 -08004146 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 return;
NeilBrown32a76272005-06-21 17:17:14 -07004148/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4150 mdname(mddev),
4151 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4152 __builtin_return_address(0),__builtin_return_address(1),
4153 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004154*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 if (!mddev->pers->error_handler)
4156 return;
4157 mddev->pers->error_handler(mddev,rdev);
4158 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4159 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4160 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004161 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162}
4163
4164/* seq_file implementation /proc/mdstat */
4165
4166static void status_unused(struct seq_file *seq)
4167{
4168 int i = 0;
4169 mdk_rdev_t *rdev;
4170 struct list_head *tmp;
4171
4172 seq_printf(seq, "unused devices: ");
4173
4174 ITERATE_RDEV_PENDING(rdev,tmp) {
4175 char b[BDEVNAME_SIZE];
4176 i++;
4177 seq_printf(seq, "%s ",
4178 bdevname(rdev->bdev,b));
4179 }
4180 if (!i)
4181 seq_printf(seq, "<none>");
4182
4183 seq_printf(seq, "\n");
4184}
4185
4186
4187static void status_resync(struct seq_file *seq, mddev_t * mddev)
4188{
NeilBrown4588b422006-03-27 01:18:04 -08004189 sector_t max_blocks, resync, res;
4190 unsigned long dt, db, rt;
4191 int scale;
4192 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
4194 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4195
4196 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4197 max_blocks = mddev->resync_max_sectors >> 1;
4198 else
4199 max_blocks = mddev->size;
4200
4201 /*
4202 * Should not happen.
4203 */
4204 if (!max_blocks) {
4205 MD_BUG();
4206 return;
4207 }
NeilBrown4588b422006-03-27 01:18:04 -08004208 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4209 * in a sector_t, and (max_blocks>>scale) will fit in a
4210 * u32, as those are the requirements for sector_div.
4211 * Thus 'scale' must be at least 10
4212 */
4213 scale = 10;
4214 if (sizeof(sector_t) > sizeof(unsigned long)) {
4215 while ( max_blocks/2 > (1ULL<<(scale+32)))
4216 scale++;
4217 }
4218 res = (resync>>scale)*1000;
4219 sector_div(res, (u32)((max_blocks>>scale)+1));
4220
4221 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 {
NeilBrown4588b422006-03-27 01:18:04 -08004223 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 seq_printf(seq, "[");
4225 for (i = 0; i < x; i++)
4226 seq_printf(seq, "=");
4227 seq_printf(seq, ">");
4228 for (i = 0; i < y; i++)
4229 seq_printf(seq, ".");
4230 seq_printf(seq, "] ");
4231 }
NeilBrown4588b422006-03-27 01:18:04 -08004232 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004233 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4234 "reshape" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
NeilBrownccfcc3c2006-03-27 01:18:09 -08004236 "resync" : "recovery")),
NeilBrown4588b422006-03-27 01:18:04 -08004237 per_milli/10, per_milli % 10,
4238 (unsigned long long) resync,
4239 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240
4241 /*
4242 * We do not want to overflow, so the order of operands and
4243 * the * 100 / 100 trick are important. We do a +1 to be
4244 * safe against division by zero. We only estimate anyway.
4245 *
4246 * dt: time from mark until now
4247 * db: blocks written from mark until now
4248 * rt: remaining time
4249 */
4250 dt = ((jiffies - mddev->resync_mark) / HZ);
4251 if (!dt) dt++;
4252 db = resync - (mddev->resync_mark_cnt/2);
NeilBrown4588b422006-03-27 01:18:04 -08004253 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254
4255 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4256
4257 seq_printf(seq, " speed=%ldK/sec", db/dt);
4258}
4259
4260static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4261{
4262 struct list_head *tmp;
4263 loff_t l = *pos;
4264 mddev_t *mddev;
4265
4266 if (l >= 0x10000)
4267 return NULL;
4268 if (!l--)
4269 /* header */
4270 return (void*)1;
4271
4272 spin_lock(&all_mddevs_lock);
4273 list_for_each(tmp,&all_mddevs)
4274 if (!l--) {
4275 mddev = list_entry(tmp, mddev_t, all_mddevs);
4276 mddev_get(mddev);
4277 spin_unlock(&all_mddevs_lock);
4278 return mddev;
4279 }
4280 spin_unlock(&all_mddevs_lock);
4281 if (!l--)
4282 return (void*)2;/* tail */
4283 return NULL;
4284}
4285
4286static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4287{
4288 struct list_head *tmp;
4289 mddev_t *next_mddev, *mddev = v;
4290
4291 ++*pos;
4292 if (v == (void*)2)
4293 return NULL;
4294
4295 spin_lock(&all_mddevs_lock);
4296 if (v == (void*)1)
4297 tmp = all_mddevs.next;
4298 else
4299 tmp = mddev->all_mddevs.next;
4300 if (tmp != &all_mddevs)
4301 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4302 else {
4303 next_mddev = (void*)2;
4304 *pos = 0x10000;
4305 }
4306 spin_unlock(&all_mddevs_lock);
4307
4308 if (v != (void*)1)
4309 mddev_put(mddev);
4310 return next_mddev;
4311
4312}
4313
4314static void md_seq_stop(struct seq_file *seq, void *v)
4315{
4316 mddev_t *mddev = v;
4317
4318 if (mddev && v != (void*)1 && v != (void*)2)
4319 mddev_put(mddev);
4320}
4321
NeilBrownd7603b72006-01-06 00:20:30 -08004322struct mdstat_info {
4323 int event;
4324};
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326static int md_seq_show(struct seq_file *seq, void *v)
4327{
4328 mddev_t *mddev = v;
4329 sector_t size;
4330 struct list_head *tmp2;
4331 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004332 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004333 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004336 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 seq_printf(seq, "Personalities : ");
4338 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004339 list_for_each_entry(pers, &pers_list, list)
4340 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
4342 spin_unlock(&pers_lock);
4343 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004344 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 return 0;
4346 }
4347 if (v == (void*)2) {
4348 status_unused(seq);
4349 return 0;
4350 }
4351
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004352 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4356 seq_printf(seq, "%s : %sactive", mdname(mddev),
4357 mddev->pers ? "" : "in");
4358 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004359 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004361 if (mddev->ro==2)
4362 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 seq_printf(seq, " %s", mddev->pers->name);
4364 }
4365
4366 size = 0;
4367 ITERATE_RDEV(mddev,rdev,tmp2) {
4368 char b[BDEVNAME_SIZE];
4369 seq_printf(seq, " %s[%d]",
4370 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004371 if (test_bit(WriteMostly, &rdev->flags))
4372 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004373 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 seq_printf(seq, "(F)");
4375 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004376 } else if (rdev->raid_disk < 0)
4377 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 size += rdev->size;
4379 }
4380
4381 if (!list_empty(&mddev->disks)) {
4382 if (mddev->pers)
4383 seq_printf(seq, "\n %llu blocks",
4384 (unsigned long long)mddev->array_size);
4385 else
4386 seq_printf(seq, "\n %llu blocks",
4387 (unsigned long long)size);
4388 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004389 if (mddev->persistent) {
4390 if (mddev->major_version != 0 ||
4391 mddev->minor_version != 90) {
4392 seq_printf(seq," super %d.%d",
4393 mddev->major_version,
4394 mddev->minor_version);
4395 }
4396 } else
4397 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
4399 if (mddev->pers) {
4400 mddev->pers->status (seq, mddev);
4401 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004402 if (mddev->pers->sync_request) {
4403 if (mddev->curr_resync > 2) {
4404 status_resync (seq, mddev);
4405 seq_printf(seq, "\n ");
4406 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4407 seq_printf(seq, "\tresync=DELAYED\n ");
4408 else if (mddev->recovery_cp < MaxSector)
4409 seq_printf(seq, "\tresync=PENDING\n ");
4410 }
NeilBrown32a76272005-06-21 17:17:14 -07004411 } else
4412 seq_printf(seq, "\n ");
4413
4414 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004415 unsigned long chunk_kb;
4416 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004417 spin_lock_irqsave(&bitmap->lock, flags);
4418 chunk_kb = bitmap->chunksize >> 10;
4419 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4420 "%lu%s chunk",
4421 bitmap->pages - bitmap->missing_pages,
4422 bitmap->pages,
4423 (bitmap->pages - bitmap->missing_pages)
4424 << (PAGE_SHIFT - 10),
4425 chunk_kb ? chunk_kb : bitmap->chunksize,
4426 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004427 if (bitmap->file) {
4428 seq_printf(seq, ", file: ");
4429 seq_path(seq, bitmap->file->f_vfsmnt,
4430 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004431 }
NeilBrown78d742d2005-06-21 17:17:15 -07004432
NeilBrown32a76272005-06-21 17:17:14 -07004433 seq_printf(seq, "\n");
4434 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 }
4436
4437 seq_printf(seq, "\n");
4438 }
4439 mddev_unlock(mddev);
4440
4441 return 0;
4442}
4443
4444static struct seq_operations md_seq_ops = {
4445 .start = md_seq_start,
4446 .next = md_seq_next,
4447 .stop = md_seq_stop,
4448 .show = md_seq_show,
4449};
4450
4451static int md_seq_open(struct inode *inode, struct file *file)
4452{
4453 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004454 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4455 if (mi == NULL)
4456 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004459 if (error)
4460 kfree(mi);
4461 else {
4462 struct seq_file *p = file->private_data;
4463 p->private = mi;
4464 mi->event = atomic_read(&md_event_count);
4465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return error;
4467}
4468
NeilBrownd7603b72006-01-06 00:20:30 -08004469static int md_seq_release(struct inode *inode, struct file *file)
4470{
4471 struct seq_file *m = file->private_data;
4472 struct mdstat_info *mi = m->private;
4473 m->private = NULL;
4474 kfree(mi);
4475 return seq_release(inode, file);
4476}
4477
4478static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4479{
4480 struct seq_file *m = filp->private_data;
4481 struct mdstat_info *mi = m->private;
4482 int mask;
4483
4484 poll_wait(filp, &md_event_waiters, wait);
4485
4486 /* always allow read */
4487 mask = POLLIN | POLLRDNORM;
4488
4489 if (mi->event != atomic_read(&md_event_count))
4490 mask |= POLLERR | POLLPRI;
4491 return mask;
4492}
4493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494static struct file_operations md_seq_fops = {
4495 .open = md_seq_open,
4496 .read = seq_read,
4497 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004498 .release = md_seq_release,
4499 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500};
4501
NeilBrown2604b702006-01-06 00:20:36 -08004502int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004505 list_add_tail(&p->list, &pers_list);
4506 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 spin_unlock(&pers_lock);
4508 return 0;
4509}
4510
NeilBrown2604b702006-01-06 00:20:36 -08004511int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512{
NeilBrown2604b702006-01-06 00:20:36 -08004513 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004515 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 spin_unlock(&pers_lock);
4517 return 0;
4518}
4519
4520static int is_mddev_idle(mddev_t *mddev)
4521{
4522 mdk_rdev_t * rdev;
4523 struct list_head *tmp;
4524 int idle;
4525 unsigned long curr_events;
4526
4527 idle = 1;
4528 ITERATE_RDEV(mddev,rdev,tmp) {
4529 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004530 curr_events = disk_stat_read(disk, sectors[0]) +
4531 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004533 /* The difference between curr_events and last_events
4534 * will be affected by any new non-sync IO (making
4535 * curr_events bigger) and any difference in the amount of
4536 * in-flight syncio (making current_events bigger or smaller)
4537 * The amount in-flight is currently limited to
4538 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4539 * which is at most 4096 sectors.
4540 * These numbers are fairly fragile and should be made
4541 * more robust, probably by enforcing the
4542 * 'window size' that md_do_sync sort-of uses.
4543 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 * Note: the following is an unsigned comparison.
4545 */
NeilBrownc0e48522005-11-18 01:11:01 -08004546 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 rdev->last_events = curr_events;
4548 idle = 0;
4549 }
4550 }
4551 return idle;
4552}
4553
4554void md_done_sync(mddev_t *mddev, int blocks, int ok)
4555{
4556 /* another "blocks" (512byte) blocks have been synced */
4557 atomic_sub(blocks, &mddev->recovery_active);
4558 wake_up(&mddev->recovery_wait);
4559 if (!ok) {
4560 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4561 md_wakeup_thread(mddev->thread);
4562 // stop recovery, signal do_sync ....
4563 }
4564}
4565
4566
NeilBrown06d91a52005-06-21 17:17:12 -07004567/* md_write_start(mddev, bi)
4568 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004569 * in superblock) before writing, schedule a superblock update
4570 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07004571 */
NeilBrown3d310eb2005-06-21 17:17:26 -07004572void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
NeilBrown06d91a52005-06-21 17:17:12 -07004574 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07004575 return;
NeilBrown06d91a52005-06-21 17:17:12 -07004576
NeilBrownf91de922005-11-08 21:39:36 -08004577 BUG_ON(mddev->ro == 1);
4578 if (mddev->ro == 2) {
4579 /* need to switch to read/write */
4580 mddev->ro = 0;
4581 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4582 md_wakeup_thread(mddev->thread);
4583 }
NeilBrown06d91a52005-06-21 17:17:12 -07004584 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07004585 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08004586 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07004587 if (mddev->in_sync) {
4588 mddev->in_sync = 0;
4589 mddev->sb_dirty = 1;
4590 md_wakeup_thread(mddev->thread);
4591 }
NeilBrowna9701a32005-11-08 21:39:34 -08004592 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07004593 }
NeilBrown3d310eb2005-06-21 17:17:26 -07004594 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595}
4596
4597void md_write_end(mddev_t *mddev)
4598{
4599 if (atomic_dec_and_test(&mddev->writes_pending)) {
4600 if (mddev->safemode == 2)
4601 md_wakeup_thread(mddev->thread);
4602 else
4603 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4604 }
4605}
4606
Adrian Bunk75c96f82005-05-05 16:16:09 -07004607static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
4609#define SYNC_MARKS 10
4610#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08004611void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
4613 mddev_t *mddev2;
4614 unsigned int currspeed = 0,
4615 window;
NeilBrown57afd892005-06-21 17:17:13 -07004616 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 unsigned long mark[SYNC_MARKS];
4618 sector_t mark_cnt[SYNC_MARKS];
4619 int last_mark,m;
4620 struct list_head *tmp;
4621 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07004622 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624 /* just incase thread restarts... */
4625 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4626 return;
4627
4628 /* we overload curr_resync somewhat here.
4629 * 0 == not engaged in resync at all
4630 * 2 == checking that there is no conflict with another sync
4631 * 1 == like 2, but have yielded to allow conflicting resync to
4632 * commense
4633 * other == active in resync - this many blocks
4634 *
4635 * Before starting a resync we must have set curr_resync to
4636 * 2, and then checked that every "conflicting" array has curr_resync
4637 * less than ours. When we find one that is the same or higher
4638 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4639 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4640 * This will mean we have to start checking from the beginning again.
4641 *
4642 */
4643
4644 do {
4645 mddev->curr_resync = 2;
4646
4647 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08004648 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07004649 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 goto skip;
4651 }
4652 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 if (mddev2 == mddev)
4654 continue;
4655 if (mddev2->curr_resync &&
4656 match_mddev_units(mddev,mddev2)) {
4657 DEFINE_WAIT(wq);
4658 if (mddev < mddev2 && mddev->curr_resync == 2) {
4659 /* arbitrarily yield */
4660 mddev->curr_resync = 1;
4661 wake_up(&resync_wait);
4662 }
4663 if (mddev > mddev2 && mddev->curr_resync == 1)
4664 /* no need to wait here, we can wait the next
4665 * time 'round when curr_resync == 2
4666 */
4667 continue;
NeilBrown787453c2005-11-08 21:39:43 -08004668 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4669 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07004670 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 printk(KERN_INFO "md: delaying resync of %s"
4672 " until %s has finished resync (they"
4673 " share one or more physical units)\n",
4674 mdname(mddev), mdname(mddev2));
4675 mddev_put(mddev2);
4676 schedule();
4677 finish_wait(&resync_wait, &wq);
4678 goto try_again;
4679 }
4680 finish_wait(&resync_wait, &wq);
4681 }
4682 }
4683 } while (mddev->curr_resync < 2);
4684
NeilBrown9d888832005-11-08 21:39:26 -08004685 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004687 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 */
4689 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004690 mddev->resync_mismatches = 0;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004691 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4692 max_sectors = mddev->size << 1;
4693 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 /* recovery follows the physical size of devices */
4695 max_sectors = mddev->size << 1;
4696
4697 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4698 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
NeilBrown88202a02006-01-06 00:21:36 -08004699 " %d KB/sec/disc.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07004700 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 "(but not more than %d KB/sec) for reconstruction.\n",
NeilBrown88202a02006-01-06 00:21:36 -08004702 speed_max(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703
4704 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004705 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004706 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4707 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 j = mddev->recovery_cp;
4709 else
4710 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004711 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 for (m = 0; m < SYNC_MARKS; m++) {
4713 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004714 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 }
4716 last_mark = 0;
4717 mddev->resync_mark = mark[last_mark];
4718 mddev->resync_mark_cnt = mark_cnt[last_mark];
4719
4720 /*
4721 * Tune reconstruction:
4722 */
4723 window = 32*(PAGE_SIZE/512);
4724 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4725 window/2,(unsigned long long) max_sectors/2);
4726
4727 atomic_set(&mddev->recovery_active, 0);
4728 init_waitqueue_head(&mddev->recovery_wait);
4729 last_check = 0;
4730
4731 if (j>2) {
4732 printk(KERN_INFO
4733 "md: resuming recovery of %s from checkpoint.\n",
4734 mdname(mddev));
4735 mddev->curr_resync = j;
4736 }
4737
4738 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004739 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
NeilBrown57afd892005-06-21 17:17:13 -07004741 skipped = 0;
4742 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08004743 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07004744 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4746 goto out;
4747 }
NeilBrown57afd892005-06-21 17:17:13 -07004748
4749 if (!skipped) { /* actual IO requested */
4750 io_sectors += sectors;
4751 atomic_add(sectors, &mddev->recovery_active);
4752 }
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 j += sectors;
4755 if (j>1) mddev->curr_resync = j;
NeilBrownd7603b72006-01-06 00:20:30 -08004756 if (last_check == 0)
4757 /* this is the earliers that rebuilt will be
4758 * visible in /proc/mdstat
4759 */
4760 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07004761
4762 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 continue;
4764
NeilBrown57afd892005-06-21 17:17:13 -07004765 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766
4767 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4768 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4769 break;
4770
4771 repeat:
4772 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4773 /* step marks */
4774 int next = (last_mark+1) % SYNC_MARKS;
4775
4776 mddev->resync_mark = mark[next];
4777 mddev->resync_mark_cnt = mark_cnt[next];
4778 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004779 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780 last_mark = next;
4781 }
4782
4783
NeilBrown787453c2005-11-08 21:39:43 -08004784 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 /*
4786 * got a signal, exit.
4787 */
4788 printk(KERN_INFO
4789 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4791 goto out;
4792 }
4793
4794 /*
4795 * this loop exits only if either when we are slower than
4796 * the 'hard' speed limit, or the system was IO-idle for
4797 * a jiffy.
4798 * the system might be non-idle CPU-wise, but we only care
4799 * about not overloading the IO subsystem. (things like an
4800 * e2fsck being done on the RAID array should execute fast)
4801 */
4802 mddev->queue->unplug_fn(mddev->queue);
4803 cond_resched();
4804
NeilBrown57afd892005-06-21 17:17:13 -07004805 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4806 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
NeilBrown88202a02006-01-06 00:21:36 -08004808 if (currspeed > speed_min(mddev)) {
4809 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004811 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 goto repeat;
4813 }
4814 }
4815 }
4816 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4817 /*
4818 * this also signals 'finished resyncing' to md_stop
4819 */
4820 out:
4821 mddev->queue->unplug_fn(mddev->queue);
4822
4823 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4824
4825 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004826 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08004829 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
4830 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 mddev->curr_resync > 2 &&
4832 mddev->curr_resync >= mddev->recovery_cp) {
4833 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4834 printk(KERN_INFO
4835 "md: checkpointing recovery of %s.\n",
4836 mdname(mddev));
4837 mddev->recovery_cp = mddev->curr_resync;
4838 } else
4839 mddev->recovery_cp = MaxSector;
4840 }
4841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 skip:
4843 mddev->curr_resync = 0;
4844 wake_up(&resync_wait);
4845 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4846 md_wakeup_thread(mddev->thread);
4847}
NeilBrown29269552006-03-27 01:18:10 -08004848EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
4850
4851/*
4852 * This routine is regularly called by all per-raid-array threads to
4853 * deal with generic issues like resync and super-block update.
4854 * Raid personalities that don't have a thread (linear/raid0) do not
4855 * need this as they never do any recovery or update the superblock.
4856 *
4857 * It does not do any resync itself, but rather "forks" off other threads
4858 * to do that as needed.
4859 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4860 * "->recovery" and create a thread at ->sync_thread.
4861 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4862 * and wakeups up this thread which will reap the thread and finish up.
4863 * This thread also removes any faulty devices (with nr_pending == 0).
4864 *
4865 * The overall approach is:
4866 * 1/ if the superblock needs updating, update it.
4867 * 2/ If a recovery thread is running, don't do anything else.
4868 * 3/ If recovery has finished, clean up, possibly marking spares active.
4869 * 4/ If there are any faulty devices, remove them.
4870 * 5/ If array is degraded, try to add spares devices
4871 * 6/ If array has spares or is not in-sync, start a resync thread.
4872 */
4873void md_check_recovery(mddev_t *mddev)
4874{
4875 mdk_rdev_t *rdev;
4876 struct list_head *rtmp;
4877
4878
NeilBrown5f404022005-06-21 17:17:16 -07004879 if (mddev->bitmap)
4880 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 if (mddev->ro)
4883 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004884
4885 if (signal_pending(current)) {
4886 if (mddev->pers->sync_request) {
4887 printk(KERN_INFO "md: %s in immediate safe mode\n",
4888 mdname(mddev));
4889 mddev->safemode = 2;
4890 }
4891 flush_signals(current);
4892 }
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 if ( ! (
4895 mddev->sb_dirty ||
4896 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004897 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4898 (mddev->safemode == 1) ||
4899 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4900 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901 ))
4902 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004903
NeilBrowndf5b89b2006-03-27 01:18:20 -08004904 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004906
NeilBrowna9701a32005-11-08 21:39:34 -08004907 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004908 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4909 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4910 mddev->in_sync = 1;
4911 mddev->sb_dirty = 1;
4912 }
4913 if (mddev->safemode == 1)
4914 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004915 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 if (mddev->sb_dirty)
4918 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004919
NeilBrown06d91a52005-06-21 17:17:12 -07004920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4922 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4923 /* resync/recovery still happening */
4924 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4925 goto unlock;
4926 }
4927 if (mddev->sync_thread) {
4928 /* resync has finished, collect result */
4929 md_unregister_thread(mddev->sync_thread);
4930 mddev->sync_thread = NULL;
4931 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4932 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4933 /* success...*/
4934 /* activate any spares */
4935 mddev->pers->spare_active(mddev);
4936 }
4937 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004938
4939 /* if array is no-longer degraded, then any saved_raid_disk
4940 * information must be scrapped
4941 */
4942 if (!mddev->degraded)
4943 ITERATE_RDEV(mddev,rdev,rtmp)
4944 rdev->saved_raid_disk = -1;
4945
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 mddev->recovery = 0;
4947 /* flag recovery needed just to double check */
4948 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08004949 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 goto unlock;
4951 }
NeilBrown24dd4692005-11-08 21:39:26 -08004952 /* Clear some bits that don't mean anything, but
4953 * might be left set
4954 */
4955 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4956 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4957 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4958 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
4960 /* no recovery is running.
4961 * remove any failed drives, then
4962 * add spares if possible.
4963 * Spare are also removed and re-added, to allow
4964 * the personality to fail the re-add.
4965 */
4966 ITERATE_RDEV(mddev,rdev,rtmp)
4967 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004968 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004970 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4971 char nm[20];
4972 sprintf(nm,"rd%d", rdev->raid_disk);
4973 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 }
4977
4978 if (mddev->degraded) {
4979 ITERATE_RDEV(mddev,rdev,rtmp)
4980 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004981 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004982 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4983 char nm[20];
4984 sprintf(nm, "rd%d", rdev->raid_disk);
4985 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08004987 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08004988 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989 break;
4990 }
4991 }
4992
NeilBrown24dd4692005-11-08 21:39:26 -08004993 if (spares) {
4994 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4995 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4996 } else if (mddev->recovery_cp < MaxSector) {
4997 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4998 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4999 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 if (mddev->pers->sync_request) {
5003 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005004 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5005 /* We are adding a device or devices to an array
5006 * which has the bitmap stored on all devices.
5007 * So make sure all bitmap pages get written
5008 */
5009 bitmap_write_all(mddev->bitmap);
5010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 mddev->sync_thread = md_register_thread(md_do_sync,
5012 mddev,
5013 "%s_resync");
5014 if (!mddev->sync_thread) {
5015 printk(KERN_ERR "%s: could not start resync"
5016 " thread...\n",
5017 mdname(mddev));
5018 /* leave the spares where they are, it shouldn't hurt */
5019 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005020 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005022 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 }
5024 unlock:
5025 mddev_unlock(mddev);
5026 }
5027}
5028
Adrian Bunk75c96f82005-05-05 16:16:09 -07005029static int md_notify_reboot(struct notifier_block *this,
5030 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031{
5032 struct list_head *tmp;
5033 mddev_t *mddev;
5034
5035 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5036
5037 printk(KERN_INFO "md: stopping all md devices.\n");
5038
5039 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005040 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005042 mddev_unlock(mddev);
5043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 /*
5045 * certain more exotic SCSI devices are known to be
5046 * volatile wrt too early system reboots. While the
5047 * right place to handle this issue is the given
5048 * driver, we do want to have a safe RAID driver ...
5049 */
5050 mdelay(1000*1);
5051 }
5052 return NOTIFY_DONE;
5053}
5054
Adrian Bunk75c96f82005-05-05 16:16:09 -07005055static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 .notifier_call = md_notify_reboot,
5057 .next = NULL,
5058 .priority = INT_MAX, /* before any real devices */
5059};
5060
5061static void md_geninit(void)
5062{
5063 struct proc_dir_entry *p;
5064
5065 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5066
5067 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5068 if (p)
5069 p->proc_fops = &md_seq_fops;
5070}
5071
Adrian Bunk75c96f82005-05-05 16:16:09 -07005072static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073{
5074 int minor;
5075
5076 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5077 " MD_SB_DISKS=%d\n",
5078 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5079 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08005080 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07005081 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
5083 if (register_blkdev(MAJOR_NR, "md"))
5084 return -1;
5085 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5086 unregister_blkdev(MAJOR_NR, "md");
5087 return -1;
5088 }
5089 devfs_mk_dir("md");
5090 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5091 md_probe, NULL, NULL);
5092 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5093 md_probe, NULL, NULL);
5094
5095 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5096 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
5097 S_IFBLK|S_IRUSR|S_IWUSR,
5098 "md/%d", minor);
5099
5100 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5101 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
5102 S_IFBLK|S_IRUSR|S_IWUSR,
5103 "md/mdp%d", minor);
5104
5105
5106 register_reboot_notifier(&md_notifier);
5107 raid_table_header = register_sysctl_table(raid_root_table, 1);
5108
5109 md_geninit();
5110 return (0);
5111}
5112
5113
5114#ifndef MODULE
5115
5116/*
5117 * Searches all registered partitions for autorun RAID arrays
5118 * at boot time.
5119 */
5120static dev_t detected_devices[128];
5121static int dev_cnt;
5122
5123void md_autodetect_dev(dev_t dev)
5124{
5125 if (dev_cnt >= 0 && dev_cnt < 127)
5126 detected_devices[dev_cnt++] = dev;
5127}
5128
5129
5130static void autostart_arrays(int part)
5131{
5132 mdk_rdev_t *rdev;
5133 int i;
5134
5135 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5136
5137 for (i = 0; i < dev_cnt; i++) {
5138 dev_t dev = detected_devices[i];
5139
5140 rdev = md_import_device(dev,0, 0);
5141 if (IS_ERR(rdev))
5142 continue;
5143
NeilBrownb2d444d2005-11-08 21:39:31 -08005144 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 MD_BUG();
5146 continue;
5147 }
5148 list_add(&rdev->same_set, &pending_raid_disks);
5149 }
5150 dev_cnt = 0;
5151
5152 autorun_devices(part);
5153}
5154
5155#endif
5156
5157static __exit void md_exit(void)
5158{
5159 mddev_t *mddev;
5160 struct list_head *tmp;
5161 int i;
5162 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5163 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5164 for (i=0; i < MAX_MD_DEVS; i++)
5165 devfs_remove("md/%d", i);
5166 for (i=0; i < MAX_MD_DEVS; i++)
5167 devfs_remove("md/d%d", i);
5168
5169 devfs_remove("md");
5170
5171 unregister_blkdev(MAJOR_NR,"md");
5172 unregister_blkdev(mdp_major, "mdp");
5173 unregister_reboot_notifier(&md_notifier);
5174 unregister_sysctl_table(raid_table_header);
5175 remove_proc_entry("mdstat", NULL);
5176 ITERATE_MDDEV(mddev,tmp) {
5177 struct gendisk *disk = mddev->gendisk;
5178 if (!disk)
5179 continue;
5180 export_array(mddev);
5181 del_gendisk(disk);
5182 put_disk(disk);
5183 mddev->gendisk = NULL;
5184 mddev_put(mddev);
5185 }
5186}
5187
5188module_init(md_init)
5189module_exit(md_exit)
5190
NeilBrownf91de922005-11-08 21:39:36 -08005191static int get_ro(char *buffer, struct kernel_param *kp)
5192{
5193 return sprintf(buffer, "%d", start_readonly);
5194}
5195static int set_ro(const char *val, struct kernel_param *kp)
5196{
5197 char *e;
5198 int num = simple_strtoul(val, &e, 10);
5199 if (*val && (*e == '\0' || *e == '\n')) {
5200 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005201 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005202 }
5203 return -EINVAL;
5204}
5205
5206module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005207module_param(start_dirty_degraded, int, 0644);
5208
NeilBrownf91de922005-11-08 21:39:36 -08005209
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210EXPORT_SYMBOL(register_md_personality);
5211EXPORT_SYMBOL(unregister_md_personality);
5212EXPORT_SYMBOL(md_error);
5213EXPORT_SYMBOL(md_done_sync);
5214EXPORT_SYMBOL(md_write_start);
5215EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216EXPORT_SYMBOL(md_register_thread);
5217EXPORT_SYMBOL(md_unregister_thread);
5218EXPORT_SYMBOL(md_wakeup_thread);
5219EXPORT_SYMBOL(md_print_devices);
5220EXPORT_SYMBOL(md_check_recovery);
5221MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005222MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005223MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);