blob: 039e071c10074190ec999079b713cf6c8f82a603 [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);
166}
NeilBrown29269552006-03-27 01:18:10 -0800167EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800168
169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Enables to iterate over all existing md arrays
171 * all_mddevs_lock protects this list.
172 */
173static LIST_HEAD(all_mddevs);
174static DEFINE_SPINLOCK(all_mddevs_lock);
175
176
177/*
178 * iterates through all used mddevs in the system.
179 * We take care to grab the all_mddevs_lock whenever navigating
180 * the list, and to always hold a refcount when unlocked.
181 * Any code which breaks out of this loop while own
182 * a reference to the current mddev and must mddev_put it.
183 */
184#define ITERATE_MDDEV(mddev,tmp) \
185 \
186 for (({ spin_lock(&all_mddevs_lock); \
187 tmp = all_mddevs.next; \
188 mddev = NULL;}); \
189 ({ if (tmp != &all_mddevs) \
190 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
191 spin_unlock(&all_mddevs_lock); \
192 if (mddev) mddev_put(mddev); \
193 mddev = list_entry(tmp, mddev_t, all_mddevs); \
194 tmp != &all_mddevs;}); \
195 ({ spin_lock(&all_mddevs_lock); \
196 tmp = tmp->next;}) \
197 )
198
199
200static int md_fail_request (request_queue_t *q, struct bio *bio)
201{
202 bio_io_error(bio, bio->bi_size);
203 return 0;
204}
205
206static inline mddev_t *mddev_get(mddev_t *mddev)
207{
208 atomic_inc(&mddev->active);
209 return mddev;
210}
211
212static void mddev_put(mddev_t *mddev)
213{
214 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
215 return;
216 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
217 list_del(&mddev->all_mddevs);
Al Viro1312f402006-03-12 11:02:03 -0500218 /* that blocks */
219 blk_cleanup_queue(mddev->queue);
220 /* that also blocks */
NeilBrowneae17012005-11-08 21:39:23 -0800221 kobject_unregister(&mddev->kobj);
Al Viro1312f402006-03-12 11:02:03 -0500222 /* result blows... */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224 spin_unlock(&all_mddevs_lock);
225}
226
227static mddev_t * mddev_find(dev_t unit)
228{
229 mddev_t *mddev, *new = NULL;
230
231 retry:
232 spin_lock(&all_mddevs_lock);
233 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
234 if (mddev->unit == unit) {
235 mddev_get(mddev);
236 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700237 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return mddev;
239 }
240
241 if (new) {
242 list_add(&new->all_mddevs, &all_mddevs);
243 spin_unlock(&all_mddevs_lock);
244 return new;
245 }
246 spin_unlock(&all_mddevs_lock);
247
NeilBrown9ffae0c2006-01-06 00:20:32 -0800248 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (!new)
250 return NULL;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 new->unit = unit;
253 if (MAJOR(unit) == MD_MAJOR)
254 new->md_minor = MINOR(unit);
255 else
256 new->md_minor = MINOR(unit) >> MdpMinorShift;
257
NeilBrowndf5b89b2006-03-27 01:18:20 -0800258 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 INIT_LIST_HEAD(&new->disks);
260 INIT_LIST_HEAD(&new->all_mddevs);
261 init_timer(&new->safemode_timer);
262 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700263 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700264 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 new->queue = blk_alloc_queue(GFP_KERNEL);
267 if (!new->queue) {
268 kfree(new);
269 return NULL;
270 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800271 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 blk_queue_make_request(new->queue, md_fail_request);
274
275 goto retry;
276}
277
278static inline int mddev_lock(mddev_t * mddev)
279{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800280 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281}
282
283static inline void mddev_lock_uninterruptible(mddev_t * mddev)
284{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800285 mutex_lock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288static inline int mddev_trylock(mddev_t * mddev)
289{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800290 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static inline void mddev_unlock(mddev_t * mddev)
294{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800295 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
NeilBrown005eca52005-08-22 13:11:08 -0700297 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
NeilBrown2989ddb2006-01-06 00:20:43 -0800300static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 mdk_rdev_t * rdev;
303 struct list_head *tmp;
304
305 ITERATE_RDEV(mddev,rdev,tmp) {
306 if (rdev->desc_nr == nr)
307 return rdev;
308 }
309 return NULL;
310}
311
312static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
313{
314 struct list_head *tmp;
315 mdk_rdev_t *rdev;
316
317 ITERATE_RDEV(mddev,rdev,tmp) {
318 if (rdev->bdev->bd_dev == dev)
319 return rdev;
320 }
321 return NULL;
322}
323
NeilBrownd9d166c2006-01-06 00:20:51 -0800324static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800325{
326 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800327 list_for_each_entry(pers, &pers_list, list) {
328 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800329 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800330 if (strcmp(pers->name, clevel)==0)
331 return pers;
332 }
NeilBrown2604b702006-01-06 00:20:36 -0800333 return NULL;
334}
335
Jesper Juhl77933d72005-07-27 11:46:09 -0700336static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
339 return MD_NEW_SIZE_BLOCKS(size);
340}
341
342static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
343{
344 sector_t size;
345
346 size = rdev->sb_offset;
347
348 if (chunk_size)
349 size &= ~((sector_t)chunk_size/1024 - 1);
350 return size;
351}
352
353static int alloc_disk_sb(mdk_rdev_t * rdev)
354{
355 if (rdev->sb_page)
356 MD_BUG();
357
358 rdev->sb_page = alloc_page(GFP_KERNEL);
359 if (!rdev->sb_page) {
360 printk(KERN_ALERT "md: out of memory.\n");
361 return -EINVAL;
362 }
363
364 return 0;
365}
366
367static void free_disk_sb(mdk_rdev_t * rdev)
368{
369 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800370 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 rdev->sb_loaded = 0;
372 rdev->sb_page = NULL;
373 rdev->sb_offset = 0;
374 rdev->size = 0;
375 }
376}
377
378
NeilBrown7bfa19f2005-06-21 17:17:28 -0700379static int super_written(struct bio *bio, unsigned int bytes_done, int error)
380{
381 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800382 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700383 if (bio->bi_size)
384 return 1;
385
386 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800387 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700388
NeilBrowna9701a32005-11-08 21:39:34 -0800389 if (atomic_dec_and_test(&mddev->pending_writes))
390 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700391 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700392 return 0;
393}
394
NeilBrowna9701a32005-11-08 21:39:34 -0800395static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
396{
397 struct bio *bio2 = bio->bi_private;
398 mdk_rdev_t *rdev = bio2->bi_private;
399 mddev_t *mddev = rdev->mddev;
400 if (bio->bi_size)
401 return 1;
402
403 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
404 error == -EOPNOTSUPP) {
405 unsigned long flags;
406 /* barriers don't appear to be supported :-( */
407 set_bit(BarriersNotsupp, &rdev->flags);
408 mddev->barriers_work = 0;
409 spin_lock_irqsave(&mddev->write_lock, flags);
410 bio2->bi_next = mddev->biolist;
411 mddev->biolist = bio2;
412 spin_unlock_irqrestore(&mddev->write_lock, flags);
413 wake_up(&mddev->sb_wait);
414 bio_put(bio);
415 return 0;
416 }
417 bio_put(bio2);
418 bio->bi_private = rdev;
419 return super_written(bio, bytes_done, error);
420}
421
NeilBrown7bfa19f2005-06-21 17:17:28 -0700422void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
423 sector_t sector, int size, struct page *page)
424{
425 /* write first size bytes of page to sector of rdev
426 * Increment mddev->pending_writes before returning
427 * and decrement it on completion, waking up sb_wait
428 * if zero is reached.
429 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800430 *
431 * As we might need to resubmit the request if BIO_RW_BARRIER
432 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700433 */
434 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800435 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700436
437 bio->bi_bdev = rdev->bdev;
438 bio->bi_sector = sector;
439 bio_add_page(bio, page, size, 0);
440 bio->bi_private = rdev;
441 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800442 bio->bi_rw = rw;
443
NeilBrown7bfa19f2005-06-21 17:17:28 -0700444 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800445 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
446 struct bio *rbio;
447 rw |= (1<<BIO_RW_BARRIER);
448 rbio = bio_clone(bio, GFP_NOIO);
449 rbio->bi_private = bio;
450 rbio->bi_end_io = super_written_barrier;
451 submit_bio(rw, rbio);
452 } else
453 submit_bio(rw, bio);
454}
455
456void md_super_wait(mddev_t *mddev)
457{
458 /* wait for all superblock writes that were scheduled to complete.
459 * if any had to be retried (due to BARRIER problems), retry them
460 */
461 DEFINE_WAIT(wq);
462 for(;;) {
463 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
464 if (atomic_read(&mddev->pending_writes)==0)
465 break;
466 while (mddev->biolist) {
467 struct bio *bio;
468 spin_lock_irq(&mddev->write_lock);
469 bio = mddev->biolist;
470 mddev->biolist = bio->bi_next ;
471 bio->bi_next = NULL;
472 spin_unlock_irq(&mddev->write_lock);
473 submit_bio(bio->bi_rw, bio);
474 }
475 schedule();
476 }
477 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
481{
482 if (bio->bi_size)
483 return 1;
484
485 complete((struct completion*)bio->bi_private);
486 return 0;
487}
488
NeilBrowna654b9d82005-06-21 17:17:27 -0700489int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct page *page, int rw)
491{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700492 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 struct completion event;
494 int ret;
495
496 rw |= (1 << BIO_RW_SYNC);
497
498 bio->bi_bdev = bdev;
499 bio->bi_sector = sector;
500 bio_add_page(bio, page, size, 0);
501 init_completion(&event);
502 bio->bi_private = &event;
503 bio->bi_end_io = bi_complete;
504 submit_bio(rw, bio);
505 wait_for_completion(&event);
506
507 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
508 bio_put(bio);
509 return ret;
510}
NeilBrowna8745db2006-01-06 00:20:34 -0800511EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
NeilBrown0002b272005-09-09 16:23:53 -0700513static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 char b[BDEVNAME_SIZE];
516 if (!rdev->sb_page) {
517 MD_BUG();
518 return -EINVAL;
519 }
520 if (rdev->sb_loaded)
521 return 0;
522
523
NeilBrown0002b272005-09-09 16:23:53 -0700524 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 goto fail;
526 rdev->sb_loaded = 1;
527 return 0;
528
529fail:
530 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
531 bdevname(rdev->bdev,b));
532 return -EINVAL;
533}
534
535static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
536{
537 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
538 (sb1->set_uuid1 == sb2->set_uuid1) &&
539 (sb1->set_uuid2 == sb2->set_uuid2) &&
540 (sb1->set_uuid3 == sb2->set_uuid3))
541
542 return 1;
543
544 return 0;
545}
546
547
548static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
549{
550 int ret;
551 mdp_super_t *tmp1, *tmp2;
552
553 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
554 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
555
556 if (!tmp1 || !tmp2) {
557 ret = 0;
558 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
559 goto abort;
560 }
561
562 *tmp1 = *sb1;
563 *tmp2 = *sb2;
564
565 /*
566 * nr_disks is not constant
567 */
568 tmp1->nr_disks = 0;
569 tmp2->nr_disks = 0;
570
571 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
572 ret = 0;
573 else
574 ret = 1;
575
576abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700577 kfree(tmp1);
578 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return ret;
580}
581
582static unsigned int calc_sb_csum(mdp_super_t * sb)
583{
584 unsigned int disk_csum, csum;
585
586 disk_csum = sb->sb_csum;
587 sb->sb_csum = 0;
588 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
589 sb->sb_csum = disk_csum;
590 return csum;
591}
592
593
594/*
595 * Handle superblock details.
596 * We want to be able to handle multiple superblock formats
597 * so we have a common interface to them all, and an array of
598 * different handlers.
599 * We rely on user-space to write the initial superblock, and support
600 * reading and updating of superblocks.
601 * Interface methods are:
602 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
603 * loads and validates a superblock on dev.
604 * if refdev != NULL, compare superblocks on both devices
605 * Return:
606 * 0 - dev has a superblock that is compatible with refdev
607 * 1 - dev has a superblock that is compatible and newer than refdev
608 * so dev should be used as the refdev in future
609 * -EINVAL superblock incompatible or invalid
610 * -othererror e.g. -EIO
611 *
612 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
613 * Verify that dev is acceptable into mddev.
614 * The first time, mddev->raid_disks will be 0, and data from
615 * dev should be merged in. Subsequent calls check that dev
616 * is new enough. Return 0 or -EINVAL
617 *
618 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
619 * Update the superblock for rdev with data in mddev
620 * This does not write to disc.
621 *
622 */
623
624struct super_type {
625 char *name;
626 struct module *owner;
627 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
628 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
629 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
630};
631
632/*
633 * load_super for 0.90.0
634 */
635static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
636{
637 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
638 mdp_super_t *sb;
639 int ret;
640 sector_t sb_offset;
641
642 /*
643 * Calculate the position of the superblock,
644 * it's at the end of the disk.
645 *
646 * It also happens to be a multiple of 4Kb.
647 */
648 sb_offset = calc_dev_sboffset(rdev->bdev);
649 rdev->sb_offset = sb_offset;
650
NeilBrown0002b272005-09-09 16:23:53 -0700651 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (ret) return ret;
653
654 ret = -EINVAL;
655
656 bdevname(rdev->bdev, b);
657 sb = (mdp_super_t*)page_address(rdev->sb_page);
658
659 if (sb->md_magic != MD_SB_MAGIC) {
660 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
661 b);
662 goto abort;
663 }
664
665 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800666 sb->minor_version < 90 ||
667 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
669 sb->major_version, sb->minor_version,
670 b);
671 goto abort;
672 }
673
674 if (sb->raid_disks <= 0)
675 goto abort;
676
677 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
678 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
679 b);
680 goto abort;
681 }
682
683 rdev->preferred_minor = sb->md_minor;
684 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700685 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 if (sb->level == LEVEL_MULTIPATH)
688 rdev->desc_nr = -1;
689 else
690 rdev->desc_nr = sb->this_disk.number;
691
692 if (refdev == 0)
693 ret = 1;
694 else {
695 __u64 ev1, ev2;
696 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
697 if (!uuid_equal(refsb, sb)) {
698 printk(KERN_WARNING "md: %s has different UUID to %s\n",
699 b, bdevname(refdev->bdev,b2));
700 goto abort;
701 }
702 if (!sb_equal(refsb, sb)) {
703 printk(KERN_WARNING "md: %s has same UUID"
704 " but different superblock to %s\n",
705 b, bdevname(refdev->bdev, b2));
706 goto abort;
707 }
708 ev1 = md_event(sb);
709 ev2 = md_event(refsb);
710 if (ev1 > ev2)
711 ret = 1;
712 else
713 ret = 0;
714 }
715 rdev->size = calc_dev_size(rdev, sb->chunk_size);
716
NeilBrown2bf071b2006-01-06 00:20:55 -0800717 if (rdev->size < sb->size && sb->level > 1)
718 /* "this cannot possibly happen" ... */
719 ret = -EINVAL;
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 abort:
722 return ret;
723}
724
725/*
726 * validate_super for 0.90.0
727 */
728static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
729{
730 mdp_disk_t *desc;
731 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
732
NeilBrown41158c72005-06-21 17:17:25 -0700733 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800734 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (mddev->raid_disks == 0) {
736 mddev->major_version = 0;
737 mddev->minor_version = sb->minor_version;
738 mddev->patch_version = sb->patch_version;
739 mddev->persistent = ! sb->not_persistent;
740 mddev->chunk_size = sb->chunk_size;
741 mddev->ctime = sb->ctime;
742 mddev->utime = sb->utime;
743 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800744 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 mddev->layout = sb->layout;
746 mddev->raid_disks = sb->raid_disks;
747 mddev->size = sb->size;
748 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700749 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700750 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
NeilBrownf6705572006-03-27 01:18:11 -0800752 if (mddev->minor_version >= 91) {
753 mddev->reshape_position = sb->reshape_position;
754 mddev->delta_disks = sb->delta_disks;
755 mddev->new_level = sb->new_level;
756 mddev->new_layout = sb->new_layout;
757 mddev->new_chunk = sb->new_chunk;
758 } else {
759 mddev->reshape_position = MaxSector;
760 mddev->delta_disks = 0;
761 mddev->new_level = mddev->level;
762 mddev->new_layout = mddev->layout;
763 mddev->new_chunk = mddev->chunk_size;
764 }
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (sb->state & (1<<MD_SB_CLEAN))
767 mddev->recovery_cp = MaxSector;
768 else {
769 if (sb->events_hi == sb->cp_events_hi &&
770 sb->events_lo == sb->cp_events_lo) {
771 mddev->recovery_cp = sb->recovery_cp;
772 } else
773 mddev->recovery_cp = 0;
774 }
775
776 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
777 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
778 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
779 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
780
781 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700782
783 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
784 mddev->bitmap_file == NULL) {
NeilBrownc5a10f62006-03-27 01:18:03 -0800785 if (mddev->level != 1 && mddev->level != 4
786 && mddev->level != 5 && mddev->level != 6
NeilBrown6cce3b22006-01-06 00:20:16 -0800787 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700788 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800789 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700790 return -EINVAL;
791 }
NeilBrown36fa3062005-09-09 16:23:45 -0700792 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700793 }
794
NeilBrown41158c72005-06-21 17:17:25 -0700795 } else if (mddev->pers == NULL) {
796 /* Insist on good event counter while assembling */
797 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 ++ev1;
799 if (ev1 < mddev->events)
800 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700801 } else if (mddev->bitmap) {
802 /* if adding to array with a bitmap, then we can accept an
803 * older device ... but not too old.
804 */
805 __u64 ev1 = md_event(sb);
806 if (ev1 < mddev->bitmap->events_cleared)
807 return 0;
808 } else /* just a hot-add of a new device, leave raid_disk at -1 */
809 return 0;
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 desc = sb->disks + rdev->desc_nr;
813
814 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800815 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 else if (desc->state & (1<<MD_DISK_SYNC) &&
817 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800818 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 rdev->raid_disk = desc->raid_disk;
820 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700821 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
822 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700823 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800824 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return 0;
826}
827
828/*
829 * sync_super for 0.90.0
830 */
831static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
832{
833 mdp_super_t *sb;
834 struct list_head *tmp;
835 mdk_rdev_t *rdev2;
836 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 /* make rdev->sb match mddev data..
840 *
841 * 1/ zero out disks
842 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
843 * 3/ any empty disks < next_spare become removed
844 *
845 * disks[0] gets initialised to REMOVED because
846 * we cannot be sure from other fields if it has
847 * been initialised or not.
848 */
849 int i;
850 int active=0, working=0,failed=0,spare=0,nr_disks=0;
851
NeilBrown61181562005-09-09 16:24:02 -0700852 rdev->sb_size = MD_SB_BYTES;
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 sb = (mdp_super_t*)page_address(rdev->sb_page);
855
856 memset(sb, 0, sizeof(*sb));
857
858 sb->md_magic = MD_SB_MAGIC;
859 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 sb->patch_version = mddev->patch_version;
861 sb->gvalid_words = 0; /* ignored */
862 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
863 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
864 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
865 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
866
867 sb->ctime = mddev->ctime;
868 sb->level = mddev->level;
869 sb->size = mddev->size;
870 sb->raid_disks = mddev->raid_disks;
871 sb->md_minor = mddev->md_minor;
872 sb->not_persistent = !mddev->persistent;
873 sb->utime = mddev->utime;
874 sb->state = 0;
875 sb->events_hi = (mddev->events>>32);
876 sb->events_lo = (u32)mddev->events;
877
NeilBrownf6705572006-03-27 01:18:11 -0800878 if (mddev->reshape_position == MaxSector)
879 sb->minor_version = 90;
880 else {
881 sb->minor_version = 91;
882 sb->reshape_position = mddev->reshape_position;
883 sb->new_level = mddev->new_level;
884 sb->delta_disks = mddev->delta_disks;
885 sb->new_layout = mddev->new_layout;
886 sb->new_chunk = mddev->new_chunk;
887 }
888 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (mddev->in_sync)
890 {
891 sb->recovery_cp = mddev->recovery_cp;
892 sb->cp_events_hi = (mddev->events>>32);
893 sb->cp_events_lo = (u32)mddev->events;
894 if (mddev->recovery_cp == MaxSector)
895 sb->state = (1<< MD_SB_CLEAN);
896 } else
897 sb->recovery_cp = 0;
898
899 sb->layout = mddev->layout;
900 sb->chunk_size = mddev->chunk_size;
901
NeilBrowna654b9d82005-06-21 17:17:27 -0700902 if (mddev->bitmap && mddev->bitmap_file == NULL)
903 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 sb->disks[0].state = (1<<MD_DISK_REMOVED);
906 ITERATE_RDEV(mddev,rdev2,tmp) {
907 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800908 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800909 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
910 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800911 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800913 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800914 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 d = &sb->disks[rdev2->desc_nr];
916 nr_disks++;
917 d->number = rdev2->desc_nr;
918 d->major = MAJOR(rdev2->bdev->bd_dev);
919 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800920 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
921 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 d->raid_disk = rdev2->raid_disk;
923 else
924 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800925 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800927 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 d->state = (1<<MD_DISK_ACTIVE);
929 d->state |= (1<<MD_DISK_SYNC);
930 active++;
931 working++;
932 } else {
933 d->state = 0;
934 spare++;
935 working++;
936 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700937 if (test_bit(WriteMostly, &rdev2->flags))
938 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 /* now set the "removed" and "faulty" bits on any missing devices */
941 for (i=0 ; i < mddev->raid_disks ; i++) {
942 mdp_disk_t *d = &sb->disks[i];
943 if (d->state == 0 && d->number == 0) {
944 d->number = i;
945 d->raid_disk = i;
946 d->state = (1<<MD_DISK_REMOVED);
947 d->state |= (1<<MD_DISK_FAULTY);
948 failed++;
949 }
950 }
951 sb->nr_disks = nr_disks;
952 sb->active_disks = active;
953 sb->working_disks = working;
954 sb->failed_disks = failed;
955 sb->spare_disks = spare;
956
957 sb->this_disk = sb->disks[rdev->desc_nr];
958 sb->sb_csum = calc_sb_csum(sb);
959}
960
961/*
962 * version 1 superblock
963 */
964
965static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
966{
967 unsigned int disk_csum, csum;
968 unsigned long long newcsum;
969 int size = 256 + le32_to_cpu(sb->max_dev)*2;
970 unsigned int *isuper = (unsigned int*)sb;
971 int i;
972
973 disk_csum = sb->sb_csum;
974 sb->sb_csum = 0;
975 newcsum = 0;
976 for (i=0; size>=4; size -= 4 )
977 newcsum += le32_to_cpu(*isuper++);
978
979 if (size == 2)
980 newcsum += le16_to_cpu(*(unsigned short*) isuper);
981
982 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
983 sb->sb_csum = disk_csum;
984 return cpu_to_le32(csum);
985}
986
987static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
988{
989 struct mdp_superblock_1 *sb;
990 int ret;
991 sector_t sb_offset;
992 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700993 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
996 * Calculate the position of the superblock.
997 * It is always aligned to a 4K boundary and
998 * depeding on minor_version, it can be:
999 * 0: At least 8K, but less than 12K, from end of device
1000 * 1: At start of device
1001 * 2: 4K from start of device.
1002 */
1003 switch(minor_version) {
1004 case 0:
1005 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1006 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001007 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 /* convert from sectors to K */
1009 sb_offset /= 2;
1010 break;
1011 case 1:
1012 sb_offset = 0;
1013 break;
1014 case 2:
1015 sb_offset = 4;
1016 break;
1017 default:
1018 return -EINVAL;
1019 }
1020 rdev->sb_offset = sb_offset;
1021
NeilBrown0002b272005-09-09 16:23:53 -07001022 /* superblock is rarely larger than 1K, but it can be larger,
1023 * and it is safe to read 4k, so we do that
1024 */
1025 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (ret) return ret;
1027
1028
1029 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1030
1031 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1032 sb->major_version != cpu_to_le32(1) ||
1033 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1034 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001035 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return -EINVAL;
1037
1038 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1039 printk("md: invalid superblock checksum on %s\n",
1040 bdevname(rdev->bdev,b));
1041 return -EINVAL;
1042 }
1043 if (le64_to_cpu(sb->data_size) < 10) {
1044 printk("md: data_size too small on %s\n",
1045 bdevname(rdev->bdev,b));
1046 return -EINVAL;
1047 }
1048 rdev->preferred_minor = 0xffff;
1049 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001050 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
NeilBrown0002b272005-09-09 16:23:53 -07001052 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001053 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001054 if (rdev->sb_size & bmask)
1055 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001058 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 else {
1060 __u64 ev1, ev2;
1061 struct mdp_superblock_1 *refsb =
1062 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1063
1064 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1065 sb->level != refsb->level ||
1066 sb->layout != refsb->layout ||
1067 sb->chunksize != refsb->chunksize) {
1068 printk(KERN_WARNING "md: %s has strangely different"
1069 " superblock to %s\n",
1070 bdevname(rdev->bdev,b),
1071 bdevname(refdev->bdev,b2));
1072 return -EINVAL;
1073 }
1074 ev1 = le64_to_cpu(sb->events);
1075 ev2 = le64_to_cpu(refsb->events);
1076
1077 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001078 ret = 1;
1079 else
1080 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082 if (minor_version)
1083 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1084 else
1085 rdev->size = rdev->sb_offset;
1086 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1087 return -EINVAL;
1088 rdev->size = le64_to_cpu(sb->data_size)/2;
1089 if (le32_to_cpu(sb->chunksize))
1090 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001091
1092 if (le32_to_cpu(sb->size) > rdev->size*2)
1093 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001094 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
1097static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1098{
1099 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1100
NeilBrown41158c72005-06-21 17:17:25 -07001101 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001102 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (mddev->raid_disks == 0) {
1104 mddev->major_version = 1;
1105 mddev->patch_version = 0;
1106 mddev->persistent = 1;
1107 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1108 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1109 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1110 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001111 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 mddev->layout = le32_to_cpu(sb->layout);
1113 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1114 mddev->size = le64_to_cpu(sb->size)/2;
1115 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001116 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001117 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1120 memcpy(mddev->uuid, sb->set_uuid, 16);
1121
1122 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001123
NeilBrown71c08052005-09-09 16:23:51 -07001124 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001125 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001126 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1127 && mddev->level != 10) {
1128 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001129 return -EINVAL;
1130 }
1131 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1132 }
NeilBrownf6705572006-03-27 01:18:11 -08001133 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1134 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1135 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1136 mddev->new_level = le32_to_cpu(sb->new_level);
1137 mddev->new_layout = le32_to_cpu(sb->new_layout);
1138 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1139 } else {
1140 mddev->reshape_position = MaxSector;
1141 mddev->delta_disks = 0;
1142 mddev->new_level = mddev->level;
1143 mddev->new_layout = mddev->layout;
1144 mddev->new_chunk = mddev->chunk_size;
1145 }
1146
NeilBrown41158c72005-06-21 17:17:25 -07001147 } else if (mddev->pers == NULL) {
1148 /* Insist of good event counter while assembling */
1149 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 ++ev1;
1151 if (ev1 < mddev->events)
1152 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001153 } else if (mddev->bitmap) {
1154 /* If adding to array with a bitmap, then we can accept an
1155 * older device, but not too old.
1156 */
1157 __u64 ev1 = le64_to_cpu(sb->events);
1158 if (ev1 < mddev->bitmap->events_cleared)
1159 return 0;
1160 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 if (mddev->level != LEVEL_MULTIPATH) {
1164 int role;
1165 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1166 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1167 switch(role) {
1168 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 break;
1170 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001171 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 default:
NeilBrownb2d444d2005-11-08 21:39:31 -08001174 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 rdev->raid_disk = role;
1176 break;
1177 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001178 if (sb->devflags & WriteMostly1)
1179 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001180 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001181 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return 0;
1184}
1185
1186static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1187{
1188 struct mdp_superblock_1 *sb;
1189 struct list_head *tmp;
1190 mdk_rdev_t *rdev2;
1191 int max_dev, i;
1192 /* make rdev->sb match mddev and rdev data. */
1193
1194 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1195
1196 sb->feature_map = 0;
1197 sb->pad0 = 0;
1198 memset(sb->pad1, 0, sizeof(sb->pad1));
1199 memset(sb->pad2, 0, sizeof(sb->pad2));
1200 memset(sb->pad3, 0, sizeof(sb->pad3));
1201
1202 sb->utime = cpu_to_le64((__u64)mddev->utime);
1203 sb->events = cpu_to_le64(mddev->events);
1204 if (mddev->in_sync)
1205 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1206 else
1207 sb->resync_offset = cpu_to_le64(0);
1208
NeilBrown4dbcdc72006-01-06 00:20:52 -08001209 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1210
NeilBrownf0ca3402006-02-02 14:28:04 -08001211 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001212 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001213
NeilBrowna654b9d82005-06-21 17:17:27 -07001214 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1215 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001216 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001217 }
NeilBrownf6705572006-03-27 01:18:11 -08001218 if (mddev->reshape_position != MaxSector) {
1219 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1220 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1221 sb->new_layout = cpu_to_le32(mddev->new_layout);
1222 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1223 sb->new_level = cpu_to_le32(mddev->new_level);
1224 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1225 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 max_dev = 0;
1228 ITERATE_RDEV(mddev,rdev2,tmp)
1229 if (rdev2->desc_nr+1 > max_dev)
1230 max_dev = rdev2->desc_nr+1;
1231
1232 sb->max_dev = cpu_to_le32(max_dev);
1233 for (i=0; i<max_dev;i++)
1234 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1235
1236 ITERATE_RDEV(mddev,rdev2,tmp) {
1237 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001238 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001240 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1242 else
1243 sb->dev_roles[i] = cpu_to_le16(0xffff);
1244 }
1245
1246 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1247 sb->sb_csum = calc_sb_1_csum(sb);
1248}
1249
1250
Adrian Bunk75c96f82005-05-05 16:16:09 -07001251static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 [0] = {
1253 .name = "0.90.0",
1254 .owner = THIS_MODULE,
1255 .load_super = super_90_load,
1256 .validate_super = super_90_validate,
1257 .sync_super = super_90_sync,
1258 },
1259 [1] = {
1260 .name = "md-1",
1261 .owner = THIS_MODULE,
1262 .load_super = super_1_load,
1263 .validate_super = super_1_validate,
1264 .sync_super = super_1_sync,
1265 },
1266};
1267
1268static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1269{
1270 struct list_head *tmp;
1271 mdk_rdev_t *rdev;
1272
1273 ITERATE_RDEV(mddev,rdev,tmp)
1274 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1275 return rdev;
1276
1277 return NULL;
1278}
1279
1280static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1281{
1282 struct list_head *tmp;
1283 mdk_rdev_t *rdev;
1284
1285 ITERATE_RDEV(mddev1,rdev,tmp)
1286 if (match_dev_unit(mddev2, rdev))
1287 return 1;
1288
1289 return 0;
1290}
1291
1292static LIST_HEAD(pending_raid_disks);
1293
1294static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1295{
1296 mdk_rdev_t *same_pdev;
1297 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001298 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001299 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if (rdev->mddev) {
1302 MD_BUG();
1303 return -EINVAL;
1304 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001305 /* make sure rdev->size exceeds mddev->size */
1306 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1307 if (mddev->pers)
1308 /* Cannot change size, so fail */
1309 return -ENOSPC;
1310 else
1311 mddev->size = rdev->size;
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 same_pdev = match_dev_unit(mddev, rdev);
1314 if (same_pdev)
1315 printk(KERN_WARNING
1316 "%s: WARNING: %s appears to be on the same physical"
1317 " disk as %s. True\n protection against single-disk"
1318 " failure might be compromised.\n",
1319 mdname(mddev), bdevname(rdev->bdev,b),
1320 bdevname(same_pdev->bdev,b2));
1321
1322 /* Verify rdev->desc_nr is unique.
1323 * If it is -1, assign a free number, else
1324 * check number is not in use
1325 */
1326 if (rdev->desc_nr < 0) {
1327 int choice = 0;
1328 if (mddev->pers) choice = mddev->raid_disks;
1329 while (find_rdev_nr(mddev, choice))
1330 choice++;
1331 rdev->desc_nr = choice;
1332 } else {
1333 if (find_rdev_nr(mddev, rdev->desc_nr))
1334 return -EBUSY;
1335 }
NeilBrown19133a42005-11-08 21:39:35 -08001336 bdevname(rdev->bdev,b);
1337 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1338 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001339 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1340 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 list_add(&rdev->same_set, &mddev->disks);
1343 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001344 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001345
NeilBrown9c791972005-11-08 21:39:28 -08001346 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001347 kobject_add(&rdev->kobj);
1348
NeilBrownf637b9f2005-11-08 21:39:37 -08001349 if (rdev->bdev->bd_part)
1350 ko = &rdev->bdev->bd_part->kobj;
1351 else
1352 ko = &rdev->bdev->bd_disk->kobj;
1353 sysfs_create_link(&rdev->kobj, ko, "block");
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001354 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return 0;
1356}
1357
1358static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1359{
1360 char b[BDEVNAME_SIZE];
1361 if (!rdev->mddev) {
1362 MD_BUG();
1363 return;
1364 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001365 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 list_del_init(&rdev->same_set);
1367 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1368 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001369 sysfs_remove_link(&rdev->kobj, "block");
1370 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372
1373/*
1374 * prevent the device from being mounted, repartitioned or
1375 * otherwise reused by a RAID array (or any other kernel
1376 * subsystem), by bd_claiming the device.
1377 */
1378static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1379{
1380 int err = 0;
1381 struct block_device *bdev;
1382 char b[BDEVNAME_SIZE];
1383
1384 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1385 if (IS_ERR(bdev)) {
1386 printk(KERN_ERR "md: could not open %s.\n",
1387 __bdevname(dev, b));
1388 return PTR_ERR(bdev);
1389 }
1390 err = bd_claim(bdev, rdev);
1391 if (err) {
1392 printk(KERN_ERR "md: could not bd_claim %s.\n",
1393 bdevname(bdev, b));
1394 blkdev_put(bdev);
1395 return err;
1396 }
1397 rdev->bdev = bdev;
1398 return err;
1399}
1400
1401static void unlock_rdev(mdk_rdev_t *rdev)
1402{
1403 struct block_device *bdev = rdev->bdev;
1404 rdev->bdev = NULL;
1405 if (!bdev)
1406 MD_BUG();
1407 bd_release(bdev);
1408 blkdev_put(bdev);
1409}
1410
1411void md_autodetect_dev(dev_t dev);
1412
1413static void export_rdev(mdk_rdev_t * rdev)
1414{
1415 char b[BDEVNAME_SIZE];
1416 printk(KERN_INFO "md: export_rdev(%s)\n",
1417 bdevname(rdev->bdev,b));
1418 if (rdev->mddev)
1419 MD_BUG();
1420 free_disk_sb(rdev);
1421 list_del_init(&rdev->same_set);
1422#ifndef MODULE
1423 md_autodetect_dev(rdev->bdev->bd_dev);
1424#endif
1425 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001426 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
1429static void kick_rdev_from_array(mdk_rdev_t * rdev)
1430{
1431 unbind_rdev_from_array(rdev);
1432 export_rdev(rdev);
1433}
1434
1435static void export_array(mddev_t *mddev)
1436{
1437 struct list_head *tmp;
1438 mdk_rdev_t *rdev;
1439
1440 ITERATE_RDEV(mddev,rdev,tmp) {
1441 if (!rdev->mddev) {
1442 MD_BUG();
1443 continue;
1444 }
1445 kick_rdev_from_array(rdev);
1446 }
1447 if (!list_empty(&mddev->disks))
1448 MD_BUG();
1449 mddev->raid_disks = 0;
1450 mddev->major_version = 0;
1451}
1452
1453static void print_desc(mdp_disk_t *desc)
1454{
1455 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1456 desc->major,desc->minor,desc->raid_disk,desc->state);
1457}
1458
1459static void print_sb(mdp_super_t *sb)
1460{
1461 int i;
1462
1463 printk(KERN_INFO
1464 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1465 sb->major_version, sb->minor_version, sb->patch_version,
1466 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1467 sb->ctime);
1468 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1469 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1470 sb->md_minor, sb->layout, sb->chunk_size);
1471 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1472 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1473 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1474 sb->failed_disks, sb->spare_disks,
1475 sb->sb_csum, (unsigned long)sb->events_lo);
1476
1477 printk(KERN_INFO);
1478 for (i = 0; i < MD_SB_DISKS; i++) {
1479 mdp_disk_t *desc;
1480
1481 desc = sb->disks + i;
1482 if (desc->number || desc->major || desc->minor ||
1483 desc->raid_disk || (desc->state && (desc->state != 4))) {
1484 printk(" D %2d: ", i);
1485 print_desc(desc);
1486 }
1487 }
1488 printk(KERN_INFO "md: THIS: ");
1489 print_desc(&sb->this_disk);
1490
1491}
1492
1493static void print_rdev(mdk_rdev_t *rdev)
1494{
1495 char b[BDEVNAME_SIZE];
1496 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1497 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001498 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1499 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (rdev->sb_loaded) {
1501 printk(KERN_INFO "md: rdev superblock:\n");
1502 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1503 } else
1504 printk(KERN_INFO "md: no rdev superblock!\n");
1505}
1506
1507void md_print_devices(void)
1508{
1509 struct list_head *tmp, *tmp2;
1510 mdk_rdev_t *rdev;
1511 mddev_t *mddev;
1512 char b[BDEVNAME_SIZE];
1513
1514 printk("\n");
1515 printk("md: **********************************\n");
1516 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1517 printk("md: **********************************\n");
1518 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
NeilBrown32a76272005-06-21 17:17:14 -07001520 if (mddev->bitmap)
1521 bitmap_print_sb(mddev->bitmap);
1522 else
1523 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 ITERATE_RDEV(mddev,rdev,tmp2)
1525 printk("<%s>", bdevname(rdev->bdev,b));
1526 printk("\n");
1527
1528 ITERATE_RDEV(mddev,rdev,tmp2)
1529 print_rdev(rdev);
1530 }
1531 printk("md: **********************************\n");
1532 printk("\n");
1533}
1534
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536static void sync_sbs(mddev_t * mddev)
1537{
1538 mdk_rdev_t *rdev;
1539 struct list_head *tmp;
1540
1541 ITERATE_RDEV(mddev,rdev,tmp) {
1542 super_types[mddev->major_version].
1543 sync_super(mddev, rdev);
1544 rdev->sb_loaded = 1;
1545 }
1546}
1547
NeilBrownf6705572006-03-27 01:18:11 -08001548void md_update_sb(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001550 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 struct list_head *tmp;
1552 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001553 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001556 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001557 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 mddev->utime = get_seconds();
1559 mddev->events ++;
1560
1561 if (!mddev->events) {
1562 /*
1563 * oops, this 64-bit counter should never wrap.
1564 * Either we are in around ~1 trillion A.C., assuming
1565 * 1 reboot per second, or we have a bug:
1566 */
1567 MD_BUG();
1568 mddev->events --;
1569 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001570 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 sync_sbs(mddev);
1572
1573 /*
1574 * do not write anything to disk if using
1575 * nonpersistent superblocks
1576 */
NeilBrown06d91a52005-06-21 17:17:12 -07001577 if (!mddev->persistent) {
1578 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001579 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001580 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001582 }
NeilBrowna9701a32005-11-08 21:39:34 -08001583 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 dprintk(KERN_INFO
1586 "md: updating %s RAID superblock on device (in sync %d)\n",
1587 mdname(mddev),mddev->in_sync);
1588
NeilBrown32a76272005-06-21 17:17:14 -07001589 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 ITERATE_RDEV(mddev,rdev,tmp) {
1591 char b[BDEVNAME_SIZE];
1592 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001593 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 dprintk("(skipping faulty ");
1595
1596 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001597 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001598 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001599 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001600 rdev->sb_page);
1601 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1602 bdevname(rdev->bdev,b),
1603 (unsigned long long)rdev->sb_offset);
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 } else
1606 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001607 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 /* only need to write one superblock... */
1609 break;
1610 }
NeilBrowna9701a32005-11-08 21:39:34 -08001611 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001612 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1613
NeilBrowna9701a32005-11-08 21:39:34 -08001614 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001615 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001616 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001617 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001618 goto repeat;
1619 }
1620 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001621 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001622 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
NeilBrownf6705572006-03-27 01:18:11 -08001625EXPORT_SYMBOL_GPL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
NeilBrownbce74da2006-01-06 00:20:41 -08001627/* words written to sysfs files may, or my not, be \n terminated.
1628 * We want to accept with case. For this we use cmd_match.
1629 */
1630static int cmd_match(const char *cmd, const char *str)
1631{
1632 /* See if cmd, written into a sysfs file, matches
1633 * str. They must either be the same, or cmd can
1634 * have a trailing newline
1635 */
1636 while (*cmd && *str && *cmd == *str) {
1637 cmd++;
1638 str++;
1639 }
1640 if (*cmd == '\n')
1641 cmd++;
1642 if (*str || *cmd)
1643 return 0;
1644 return 1;
1645}
1646
NeilBrown86e6ffd2005-11-08 21:39:24 -08001647struct rdev_sysfs_entry {
1648 struct attribute attr;
1649 ssize_t (*show)(mdk_rdev_t *, char *);
1650 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1651};
1652
1653static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001654state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001655{
1656 char *sep = "";
1657 int len=0;
1658
NeilBrownb2d444d2005-11-08 21:39:31 -08001659 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001660 len+= sprintf(page+len, "%sfaulty",sep);
1661 sep = ",";
1662 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001663 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001664 len += sprintf(page+len, "%sin_sync",sep);
1665 sep = ",";
1666 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001667 if (!test_bit(Faulty, &rdev->flags) &&
1668 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001669 len += sprintf(page+len, "%sspare", sep);
1670 sep = ",";
1671 }
1672 return len+sprintf(page+len, "\n");
1673}
1674
NeilBrown96de1e62005-11-08 21:39:39 -08001675static struct rdev_sysfs_entry
1676rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001677
1678static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001679super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001680{
1681 if (rdev->sb_loaded && rdev->sb_size) {
1682 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1683 return rdev->sb_size;
1684 } else
1685 return 0;
1686}
NeilBrown96de1e62005-11-08 21:39:39 -08001687static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1688
NeilBrown4dbcdc72006-01-06 00:20:52 -08001689static ssize_t
1690errors_show(mdk_rdev_t *rdev, char *page)
1691{
1692 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1693}
1694
1695static ssize_t
1696errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1697{
1698 char *e;
1699 unsigned long n = simple_strtoul(buf, &e, 10);
1700 if (*buf && (*e == 0 || *e == '\n')) {
1701 atomic_set(&rdev->corrected_errors, n);
1702 return len;
1703 }
1704 return -EINVAL;
1705}
1706static struct rdev_sysfs_entry rdev_errors =
1707__ATTR(errors, 0644, errors_show, errors_store);
1708
NeilBrown014236d2006-01-06 00:20:55 -08001709static ssize_t
1710slot_show(mdk_rdev_t *rdev, char *page)
1711{
1712 if (rdev->raid_disk < 0)
1713 return sprintf(page, "none\n");
1714 else
1715 return sprintf(page, "%d\n", rdev->raid_disk);
1716}
1717
1718static ssize_t
1719slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1720{
1721 char *e;
1722 int slot = simple_strtoul(buf, &e, 10);
1723 if (strncmp(buf, "none", 4)==0)
1724 slot = -1;
1725 else if (e==buf || (*e && *e!= '\n'))
1726 return -EINVAL;
1727 if (rdev->mddev->pers)
1728 /* Cannot set slot in active array (yet) */
1729 return -EBUSY;
1730 if (slot >= rdev->mddev->raid_disks)
1731 return -ENOSPC;
1732 rdev->raid_disk = slot;
1733 /* assume it is working */
1734 rdev->flags = 0;
1735 set_bit(In_sync, &rdev->flags);
1736 return len;
1737}
1738
1739
1740static struct rdev_sysfs_entry rdev_slot =
1741__ATTR(slot, 0644, slot_show, slot_store);
1742
NeilBrown93c8cad2006-01-06 00:20:56 -08001743static ssize_t
1744offset_show(mdk_rdev_t *rdev, char *page)
1745{
Andrew Morton6961ece2006-01-06 00:20:59 -08001746 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001747}
1748
1749static ssize_t
1750offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1751{
1752 char *e;
1753 unsigned long long offset = simple_strtoull(buf, &e, 10);
1754 if (e==buf || (*e && *e != '\n'))
1755 return -EINVAL;
1756 if (rdev->mddev->pers)
1757 return -EBUSY;
1758 rdev->data_offset = offset;
1759 return len;
1760}
1761
1762static struct rdev_sysfs_entry rdev_offset =
1763__ATTR(offset, 0644, offset_show, offset_store);
1764
NeilBrown83303b62006-01-06 00:21:06 -08001765static ssize_t
1766rdev_size_show(mdk_rdev_t *rdev, char *page)
1767{
1768 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1769}
1770
1771static ssize_t
1772rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1773{
1774 char *e;
1775 unsigned long long size = simple_strtoull(buf, &e, 10);
1776 if (e==buf || (*e && *e != '\n'))
1777 return -EINVAL;
1778 if (rdev->mddev->pers)
1779 return -EBUSY;
1780 rdev->size = size;
1781 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1782 rdev->mddev->size = size;
1783 return len;
1784}
1785
1786static struct rdev_sysfs_entry rdev_size =
1787__ATTR(size, 0644, rdev_size_show, rdev_size_store);
1788
NeilBrown86e6ffd2005-11-08 21:39:24 -08001789static struct attribute *rdev_default_attrs[] = {
1790 &rdev_state.attr,
1791 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001792 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001793 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001794 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001795 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001796 NULL,
1797};
1798static ssize_t
1799rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1800{
1801 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1802 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1803
1804 if (!entry->show)
1805 return -EIO;
1806 return entry->show(rdev, page);
1807}
1808
1809static ssize_t
1810rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1811 const char *page, size_t length)
1812{
1813 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1814 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1815
1816 if (!entry->store)
1817 return -EIO;
1818 return entry->store(rdev, page, length);
1819}
1820
1821static void rdev_free(struct kobject *ko)
1822{
1823 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1824 kfree(rdev);
1825}
1826static struct sysfs_ops rdev_sysfs_ops = {
1827 .show = rdev_attr_show,
1828 .store = rdev_attr_store,
1829};
1830static struct kobj_type rdev_ktype = {
1831 .release = rdev_free,
1832 .sysfs_ops = &rdev_sysfs_ops,
1833 .default_attrs = rdev_default_attrs,
1834};
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/*
1837 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1838 *
1839 * mark the device faulty if:
1840 *
1841 * - the device is nonexistent (zero size)
1842 * - the device has no valid superblock
1843 *
1844 * a faulty rdev _never_ has rdev->sb set.
1845 */
1846static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1847{
1848 char b[BDEVNAME_SIZE];
1849 int err;
1850 mdk_rdev_t *rdev;
1851 sector_t size;
1852
NeilBrown9ffae0c2006-01-06 00:20:32 -08001853 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (!rdev) {
1855 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1856 return ERR_PTR(-ENOMEM);
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 if ((err = alloc_disk_sb(rdev)))
1860 goto abort_free;
1861
1862 err = lock_rdev(rdev, newdev);
1863 if (err)
1864 goto abort_free;
1865
NeilBrown86e6ffd2005-11-08 21:39:24 -08001866 rdev->kobj.parent = NULL;
1867 rdev->kobj.ktype = &rdev_ktype;
1868 kobject_init(&rdev->kobj);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001871 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 rdev->data_offset = 0;
1873 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001874 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001875 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1878 if (!size) {
1879 printk(KERN_WARNING
1880 "md: %s has zero or unknown size, marking faulty!\n",
1881 bdevname(rdev->bdev,b));
1882 err = -EINVAL;
1883 goto abort_free;
1884 }
1885
1886 if (super_format >= 0) {
1887 err = super_types[super_format].
1888 load_super(rdev, NULL, super_minor);
1889 if (err == -EINVAL) {
1890 printk(KERN_WARNING
1891 "md: %s has invalid sb, not importing!\n",
1892 bdevname(rdev->bdev,b));
1893 goto abort_free;
1894 }
1895 if (err < 0) {
1896 printk(KERN_WARNING
1897 "md: could not read %s's sb, not importing!\n",
1898 bdevname(rdev->bdev,b));
1899 goto abort_free;
1900 }
1901 }
1902 INIT_LIST_HEAD(&rdev->same_set);
1903
1904 return rdev;
1905
1906abort_free:
1907 if (rdev->sb_page) {
1908 if (rdev->bdev)
1909 unlock_rdev(rdev);
1910 free_disk_sb(rdev);
1911 }
1912 kfree(rdev);
1913 return ERR_PTR(err);
1914}
1915
1916/*
1917 * Check a full RAID array for plausibility
1918 */
1919
1920
NeilBrowna757e642005-04-16 15:26:42 -07001921static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
1923 int i;
1924 struct list_head *tmp;
1925 mdk_rdev_t *rdev, *freshest;
1926 char b[BDEVNAME_SIZE];
1927
1928 freshest = NULL;
1929 ITERATE_RDEV(mddev,rdev,tmp)
1930 switch (super_types[mddev->major_version].
1931 load_super(rdev, freshest, mddev->minor_version)) {
1932 case 1:
1933 freshest = rdev;
1934 break;
1935 case 0:
1936 break;
1937 default:
1938 printk( KERN_ERR \
1939 "md: fatal superblock inconsistency in %s"
1940 " -- removing from array\n",
1941 bdevname(rdev->bdev,b));
1942 kick_rdev_from_array(rdev);
1943 }
1944
1945
1946 super_types[mddev->major_version].
1947 validate_super(mddev, freshest);
1948
1949 i = 0;
1950 ITERATE_RDEV(mddev,rdev,tmp) {
1951 if (rdev != freshest)
1952 if (super_types[mddev->major_version].
1953 validate_super(mddev, rdev)) {
1954 printk(KERN_WARNING "md: kicking non-fresh %s"
1955 " from array!\n",
1956 bdevname(rdev->bdev,b));
1957 kick_rdev_from_array(rdev);
1958 continue;
1959 }
1960 if (mddev->level == LEVEL_MULTIPATH) {
1961 rdev->desc_nr = i++;
1962 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001963 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 }
1966
1967
1968
1969 if (mddev->recovery_cp != MaxSector &&
1970 mddev->level >= 1)
1971 printk(KERN_ERR "md: %s: raid array is not clean"
1972 " -- starting background reconstruction\n",
1973 mdname(mddev));
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
NeilBrowneae17012005-11-08 21:39:23 -08001977static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001978level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08001979{
NeilBrown2604b702006-01-06 00:20:36 -08001980 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08001981 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08001982 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08001983 else if (mddev->clevel[0])
1984 return sprintf(page, "%s\n", mddev->clevel);
1985 else if (mddev->level != LEVEL_NONE)
1986 return sprintf(page, "%d\n", mddev->level);
1987 else
1988 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08001989}
1990
NeilBrownd9d166c2006-01-06 00:20:51 -08001991static ssize_t
1992level_store(mddev_t *mddev, const char *buf, size_t len)
1993{
1994 int rv = len;
1995 if (mddev->pers)
1996 return -EBUSY;
1997 if (len == 0)
1998 return 0;
1999 if (len >= sizeof(mddev->clevel))
2000 return -ENOSPC;
2001 strncpy(mddev->clevel, buf, len);
2002 if (mddev->clevel[len-1] == '\n')
2003 len--;
2004 mddev->clevel[len] = 0;
2005 mddev->level = LEVEL_NONE;
2006 return rv;
2007}
2008
2009static struct md_sysfs_entry md_level =
2010__ATTR(level, 0644, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002011
2012static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002013raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002014{
NeilBrownbb636542005-11-08 21:39:45 -08002015 if (mddev->raid_disks == 0)
2016 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002017 return sprintf(page, "%d\n", mddev->raid_disks);
2018}
2019
NeilBrownda943b992006-01-06 00:20:54 -08002020static int update_raid_disks(mddev_t *mddev, int raid_disks);
2021
2022static ssize_t
2023raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2024{
2025 /* can only set raid_disks if array is not yet active */
2026 char *e;
2027 int rv = 0;
2028 unsigned long n = simple_strtoul(buf, &e, 10);
2029
2030 if (!*buf || (*e && *e != '\n'))
2031 return -EINVAL;
2032
2033 if (mddev->pers)
2034 rv = update_raid_disks(mddev, n);
2035 else
2036 mddev->raid_disks = n;
2037 return rv ? rv : len;
2038}
2039static struct md_sysfs_entry md_raid_disks =
2040__ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002041
NeilBrown24dd4692005-11-08 21:39:26 -08002042static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002043chunk_size_show(mddev_t *mddev, char *page)
2044{
2045 return sprintf(page, "%d\n", mddev->chunk_size);
2046}
2047
2048static ssize_t
2049chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2050{
2051 /* can only set chunk_size if array is not yet active */
2052 char *e;
2053 unsigned long n = simple_strtoul(buf, &e, 10);
2054
2055 if (mddev->pers)
2056 return -EBUSY;
2057 if (!*buf || (*e && *e != '\n'))
2058 return -EINVAL;
2059
2060 mddev->chunk_size = n;
2061 return len;
2062}
2063static struct md_sysfs_entry md_chunk_size =
2064__ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2065
NeilBrown6d7ff7382006-01-06 00:21:16 -08002066static ssize_t
2067null_show(mddev_t *mddev, char *page)
2068{
2069 return -EINVAL;
2070}
2071
2072static ssize_t
2073new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2074{
2075 /* buf must be %d:%d\n? giving major and minor numbers */
2076 /* The new device is added to the array.
2077 * If the array has a persistent superblock, we read the
2078 * superblock to initialise info and check validity.
2079 * Otherwise, only checking done is that in bind_rdev_to_array,
2080 * which mainly checks size.
2081 */
2082 char *e;
2083 int major = simple_strtoul(buf, &e, 10);
2084 int minor;
2085 dev_t dev;
2086 mdk_rdev_t *rdev;
2087 int err;
2088
2089 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2090 return -EINVAL;
2091 minor = simple_strtoul(e+1, &e, 10);
2092 if (*e && *e != '\n')
2093 return -EINVAL;
2094 dev = MKDEV(major, minor);
2095 if (major != MAJOR(dev) ||
2096 minor != MINOR(dev))
2097 return -EOVERFLOW;
2098
2099
2100 if (mddev->persistent) {
2101 rdev = md_import_device(dev, mddev->major_version,
2102 mddev->minor_version);
2103 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2104 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2105 mdk_rdev_t, same_set);
2106 err = super_types[mddev->major_version]
2107 .load_super(rdev, rdev0, mddev->minor_version);
2108 if (err < 0)
2109 goto out;
2110 }
2111 } else
2112 rdev = md_import_device(dev, -1, -1);
2113
2114 if (IS_ERR(rdev))
2115 return PTR_ERR(rdev);
2116 err = bind_rdev_to_array(rdev, mddev);
2117 out:
2118 if (err)
2119 export_rdev(rdev);
2120 return err ? err : len;
2121}
2122
2123static struct md_sysfs_entry md_new_device =
2124__ATTR(new_dev, 0200, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002125
2126static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002127size_show(mddev_t *mddev, char *page)
2128{
2129 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2130}
2131
2132static int update_size(mddev_t *mddev, unsigned long size);
2133
2134static ssize_t
2135size_store(mddev_t *mddev, const char *buf, size_t len)
2136{
2137 /* If array is inactive, we can reduce the component size, but
2138 * not increase it (except from 0).
2139 * If array is active, we can try an on-line resize
2140 */
2141 char *e;
2142 int err = 0;
2143 unsigned long long size = simple_strtoull(buf, &e, 10);
2144 if (!*buf || *buf == '\n' ||
2145 (*e && *e != '\n'))
2146 return -EINVAL;
2147
2148 if (mddev->pers) {
2149 err = update_size(mddev, size);
2150 md_update_sb(mddev);
2151 } else {
2152 if (mddev->size == 0 ||
2153 mddev->size > size)
2154 mddev->size = size;
2155 else
2156 err = -ENOSPC;
2157 }
2158 return err ? err : len;
2159}
2160
2161static struct md_sysfs_entry md_size =
2162__ATTR(component_size, 0644, size_show, size_store);
2163
NeilBrown8bb93aa2006-01-06 00:20:50 -08002164
2165/* Metdata version.
2166 * This is either 'none' for arrays with externally managed metadata,
2167 * or N.M for internally known formats
2168 */
2169static ssize_t
2170metadata_show(mddev_t *mddev, char *page)
2171{
2172 if (mddev->persistent)
2173 return sprintf(page, "%d.%d\n",
2174 mddev->major_version, mddev->minor_version);
2175 else
2176 return sprintf(page, "none\n");
2177}
2178
2179static ssize_t
2180metadata_store(mddev_t *mddev, const char *buf, size_t len)
2181{
2182 int major, minor;
2183 char *e;
2184 if (!list_empty(&mddev->disks))
2185 return -EBUSY;
2186
2187 if (cmd_match(buf, "none")) {
2188 mddev->persistent = 0;
2189 mddev->major_version = 0;
2190 mddev->minor_version = 90;
2191 return len;
2192 }
2193 major = simple_strtoul(buf, &e, 10);
2194 if (e==buf || *e != '.')
2195 return -EINVAL;
2196 buf = e+1;
2197 minor = simple_strtoul(buf, &e, 10);
2198 if (e==buf || *e != '\n')
2199 return -EINVAL;
2200 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2201 super_types[major].name == NULL)
2202 return -ENOENT;
2203 mddev->major_version = major;
2204 mddev->minor_version = minor;
2205 mddev->persistent = 1;
2206 return len;
2207}
2208
2209static struct md_sysfs_entry md_metadata =
2210__ATTR(metadata_version, 0644, metadata_show, metadata_store);
2211
NeilBrowna35b0d62006-01-06 00:20:49 -08002212static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002213action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002214{
NeilBrown7eec3142005-11-08 21:39:44 -08002215 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002216 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2217 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002218 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2219 type = "reshape";
2220 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002221 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2222 type = "resync";
2223 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2224 type = "check";
2225 else
2226 type = "repair";
2227 } else
2228 type = "recover";
2229 }
2230 return sprintf(page, "%s\n", type);
2231}
2232
2233static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002234action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002235{
NeilBrown7eec3142005-11-08 21:39:44 -08002236 if (!mddev->pers || !mddev->pers->sync_request)
2237 return -EINVAL;
2238
NeilBrownbce74da2006-01-06 00:20:41 -08002239 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002240 if (mddev->sync_thread) {
2241 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2242 md_unregister_thread(mddev->sync_thread);
2243 mddev->sync_thread = NULL;
2244 mddev->recovery = 0;
2245 }
NeilBrown03c902e2006-01-06 00:20:46 -08002246 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2247 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002248 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002249 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002250 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002251 else if (cmd_match(page, "reshape")) {
2252 int err;
2253 if (mddev->pers->start_reshape == NULL)
2254 return -EINVAL;
2255 err = mddev->pers->start_reshape(mddev);
2256 if (err)
2257 return err;
2258 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002259 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002260 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownbce74da2006-01-06 00:20:41 -08002261 else if (cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002262 return -EINVAL;
2263 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2264 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002265 }
NeilBrown03c902e2006-01-06 00:20:46 -08002266 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002267 md_wakeup_thread(mddev->thread);
2268 return len;
2269}
2270
NeilBrown9d888832005-11-08 21:39:26 -08002271static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002272mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002273{
2274 return sprintf(page, "%llu\n",
2275 (unsigned long long) mddev->resync_mismatches);
2276}
2277
NeilBrown96de1e62005-11-08 21:39:39 -08002278static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08002279md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002280
NeilBrown96de1e62005-11-08 21:39:39 -08002281
2282static struct md_sysfs_entry
2283md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002284
NeilBrown88202a02006-01-06 00:21:36 -08002285static ssize_t
2286sync_min_show(mddev_t *mddev, char *page)
2287{
2288 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2289 mddev->sync_speed_min ? "local": "system");
2290}
2291
2292static ssize_t
2293sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2294{
2295 int min;
2296 char *e;
2297 if (strncmp(buf, "system", 6)==0) {
2298 mddev->sync_speed_min = 0;
2299 return len;
2300 }
2301 min = simple_strtoul(buf, &e, 10);
2302 if (buf == e || (*e && *e != '\n') || min <= 0)
2303 return -EINVAL;
2304 mddev->sync_speed_min = min;
2305 return len;
2306}
2307
2308static struct md_sysfs_entry md_sync_min =
2309__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2310
2311static ssize_t
2312sync_max_show(mddev_t *mddev, char *page)
2313{
2314 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2315 mddev->sync_speed_max ? "local": "system");
2316}
2317
2318static ssize_t
2319sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2320{
2321 int max;
2322 char *e;
2323 if (strncmp(buf, "system", 6)==0) {
2324 mddev->sync_speed_max = 0;
2325 return len;
2326 }
2327 max = simple_strtoul(buf, &e, 10);
2328 if (buf == e || (*e && *e != '\n') || max <= 0)
2329 return -EINVAL;
2330 mddev->sync_speed_max = max;
2331 return len;
2332}
2333
2334static struct md_sysfs_entry md_sync_max =
2335__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2336
2337
2338static ssize_t
2339sync_speed_show(mddev_t *mddev, char *page)
2340{
2341 unsigned long resync, dt, db;
2342 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2343 dt = ((jiffies - mddev->resync_mark) / HZ);
2344 if (!dt) dt++;
2345 db = resync - (mddev->resync_mark_cnt);
2346 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2347}
2348
2349static struct md_sysfs_entry
2350md_sync_speed = __ATTR_RO(sync_speed);
2351
2352static ssize_t
2353sync_completed_show(mddev_t *mddev, char *page)
2354{
2355 unsigned long max_blocks, resync;
2356
2357 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2358 max_blocks = mddev->resync_max_sectors;
2359 else
2360 max_blocks = mddev->size << 1;
2361
2362 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2363 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2364}
2365
2366static struct md_sysfs_entry
2367md_sync_completed = __ATTR_RO(sync_completed);
2368
NeilBrowne464eaf2006-03-27 01:18:14 -08002369static ssize_t
2370suspend_lo_show(mddev_t *mddev, char *page)
2371{
2372 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2373}
2374
2375static ssize_t
2376suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2377{
2378 char *e;
2379 unsigned long long new = simple_strtoull(buf, &e, 10);
2380
2381 if (mddev->pers->quiesce == NULL)
2382 return -EINVAL;
2383 if (buf == e || (*e && *e != '\n'))
2384 return -EINVAL;
2385 if (new >= mddev->suspend_hi ||
2386 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2387 mddev->suspend_lo = new;
2388 mddev->pers->quiesce(mddev, 2);
2389 return len;
2390 } else
2391 return -EINVAL;
2392}
2393static struct md_sysfs_entry md_suspend_lo =
2394__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2395
2396
2397static ssize_t
2398suspend_hi_show(mddev_t *mddev, char *page)
2399{
2400 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2401}
2402
2403static ssize_t
2404suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2405{
2406 char *e;
2407 unsigned long long new = simple_strtoull(buf, &e, 10);
2408
2409 if (mddev->pers->quiesce == NULL)
2410 return -EINVAL;
2411 if (buf == e || (*e && *e != '\n'))
2412 return -EINVAL;
2413 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2414 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2415 mddev->suspend_hi = new;
2416 mddev->pers->quiesce(mddev, 1);
2417 mddev->pers->quiesce(mddev, 0);
2418 return len;
2419 } else
2420 return -EINVAL;
2421}
2422static struct md_sysfs_entry md_suspend_hi =
2423__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2424
2425
NeilBrowneae17012005-11-08 21:39:23 -08002426static struct attribute *md_default_attrs[] = {
2427 &md_level.attr,
2428 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002429 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002430 &md_size.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002431 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08002432 &md_new_device.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002433 NULL,
2434};
2435
2436static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002437 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002438 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002439 &md_sync_min.attr,
2440 &md_sync_max.attr,
2441 &md_sync_speed.attr,
2442 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002443 &md_suspend_lo.attr,
2444 &md_suspend_hi.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002445 NULL,
2446};
NeilBrown411036f2005-11-08 21:39:40 -08002447static struct attribute_group md_redundancy_group = {
2448 .name = NULL,
2449 .attrs = md_redundancy_attrs,
2450};
2451
NeilBrowneae17012005-11-08 21:39:23 -08002452
2453static ssize_t
2454md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2455{
2456 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2457 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002458 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002459
2460 if (!entry->show)
2461 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08002462 mddev_lock(mddev);
2463 rv = entry->show(mddev, page);
2464 mddev_unlock(mddev);
2465 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002466}
2467
2468static ssize_t
2469md_attr_store(struct kobject *kobj, struct attribute *attr,
2470 const char *page, size_t length)
2471{
2472 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2473 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002474 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002475
2476 if (!entry->store)
2477 return -EIO;
NeilBrown96de1e62005-11-08 21:39:39 -08002478 mddev_lock(mddev);
2479 rv = entry->store(mddev, page, length);
2480 mddev_unlock(mddev);
2481 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002482}
2483
2484static void md_free(struct kobject *ko)
2485{
2486 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2487 kfree(mddev);
2488}
2489
2490static struct sysfs_ops md_sysfs_ops = {
2491 .show = md_attr_show,
2492 .store = md_attr_store,
2493};
2494static struct kobj_type md_ktype = {
2495 .release = md_free,
2496 .sysfs_ops = &md_sysfs_ops,
2497 .default_attrs = md_default_attrs,
2498};
2499
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500int mdp_major = 0;
2501
2502static struct kobject *md_probe(dev_t dev, int *part, void *data)
2503{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002504 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 mddev_t *mddev = mddev_find(dev);
2506 struct gendisk *disk;
2507 int partitioned = (MAJOR(dev) != MD_MAJOR);
2508 int shift = partitioned ? MdpMinorShift : 0;
2509 int unit = MINOR(dev) >> shift;
2510
2511 if (!mddev)
2512 return NULL;
2513
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002514 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002516 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 mddev_put(mddev);
2518 return NULL;
2519 }
2520 disk = alloc_disk(1 << shift);
2521 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002522 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 mddev_put(mddev);
2524 return NULL;
2525 }
2526 disk->major = MAJOR(dev);
2527 disk->first_minor = unit << shift;
2528 if (partitioned) {
2529 sprintf(disk->disk_name, "md_d%d", unit);
2530 sprintf(disk->devfs_name, "md/d%d", unit);
2531 } else {
2532 sprintf(disk->disk_name, "md%d", unit);
2533 sprintf(disk->devfs_name, "md/%d", unit);
2534 }
2535 disk->fops = &md_fops;
2536 disk->private_data = mddev;
2537 disk->queue = mddev->queue;
2538 add_disk(disk);
2539 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002540 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002541 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002542 mddev->kobj.k_name = NULL;
2543 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2544 mddev->kobj.ktype = &md_ktype;
2545 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 return NULL;
2547}
2548
2549void md_wakeup_thread(mdk_thread_t *thread);
2550
2551static void md_safemode_timeout(unsigned long data)
2552{
2553 mddev_t *mddev = (mddev_t *) data;
2554
2555 mddev->safemode = 1;
2556 md_wakeup_thread(mddev->thread);
2557}
2558
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002559static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561static int do_md_run(mddev_t * mddev)
2562{
NeilBrown2604b702006-01-06 00:20:36 -08002563 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 int chunk_size;
2565 struct list_head *tmp;
2566 mdk_rdev_t *rdev;
2567 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002568 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 char b[BDEVNAME_SIZE];
2570
NeilBrowna757e642005-04-16 15:26:42 -07002571 if (list_empty(&mddev->disks))
2572 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 if (mddev->pers)
2576 return -EBUSY;
2577
2578 /*
2579 * Analyze all RAID superblock(s)
2580 */
NeilBrowna757e642005-04-16 15:26:42 -07002581 if (!mddev->raid_disks)
2582 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
NeilBrown2604b702006-01-06 00:20:36 -08002586 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (chunk_size > MAX_CHUNK_SIZE) {
2588 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2589 chunk_size, MAX_CHUNK_SIZE);
2590 return -EINVAL;
2591 }
2592 /*
2593 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2594 */
2595 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07002596 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 return -EINVAL;
2598 }
2599 if (chunk_size < PAGE_SIZE) {
2600 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2601 chunk_size, PAGE_SIZE);
2602 return -EINVAL;
2603 }
2604
2605 /* devices must have minimum size of one chunk */
2606 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002607 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 continue;
2609 if (rdev->size < chunk_size / 1024) {
2610 printk(KERN_WARNING
2611 "md: Dev %s smaller than chunk_size:"
2612 " %lluk < %dk\n",
2613 bdevname(rdev->bdev,b),
2614 (unsigned long long)rdev->size,
2615 chunk_size / 1024);
2616 return -EINVAL;
2617 }
2618 }
2619 }
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08002622 if (mddev->level != LEVEL_NONE)
2623 request_module("md-level-%d", mddev->level);
2624 else if (mddev->clevel[0])
2625 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626#endif
2627
2628 /*
2629 * Drop all container device buffers, from now on
2630 * the only valid external interface is through the md
2631 * device.
2632 * Also find largest hardsector size
2633 */
2634 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002635 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 continue;
2637 sync_blockdev(rdev->bdev);
2638 invalidate_bdev(rdev->bdev, 0);
2639 }
2640
2641 md_probe(mddev->unit, NULL, NULL);
2642 disk = mddev->gendisk;
2643 if (!disk)
2644 return -ENOMEM;
2645
2646 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002647 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08002648 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002650 if (mddev->level != LEVEL_NONE)
2651 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2652 mddev->level);
2653 else
2654 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
2655 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return -EINVAL;
2657 }
NeilBrown2604b702006-01-06 00:20:36 -08002658 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002660 mddev->level = pers->level;
2661 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
NeilBrownf6705572006-03-27 01:18:11 -08002663 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08002664 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08002665 /* This personality cannot handle reshaping... */
2666 mddev->pers = NULL;
2667 module_put(pers->owner);
2668 return -EINVAL;
2669 }
2670
NeilBrown657390d2005-08-26 18:34:16 -07002671 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002673 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002674 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
NeilBrownf91de922005-11-08 21:39:36 -08002676 if (start_readonly)
2677 mddev->ro = 2; /* read-only, but switch on first write */
2678
NeilBrownb15c2e52006-01-06 00:20:16 -08002679 err = mddev->pers->run(mddev);
2680 if (!err && mddev->pers->sync_request) {
2681 err = bitmap_create(mddev);
2682 if (err) {
2683 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2684 mdname(mddev), err);
2685 mddev->pers->stop(mddev);
2686 }
2687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (err) {
2689 printk(KERN_ERR "md: pers->run() failed ...\n");
2690 module_put(mddev->pers->owner);
2691 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002692 bitmap_destroy(mddev);
2693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
NeilBrown411036f2005-11-08 21:39:40 -08002695 if (mddev->pers->sync_request)
2696 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002697 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2698 mddev->ro = 0;
2699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 atomic_set(&mddev->writes_pending,0);
2701 mddev->safemode = 0;
2702 mddev->safemode_timer.function = md_safemode_timeout;
2703 mddev->safemode_timer.data = (unsigned long) mddev;
2704 mddev->safemode_delay = (20 * HZ)/1000 +1; /* 20 msec delay */
2705 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002706
2707 ITERATE_RDEV(mddev,rdev,tmp)
2708 if (rdev->raid_disk >= 0) {
2709 char nm[20];
2710 sprintf(nm, "rd%d", rdev->raid_disk);
2711 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002715 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 if (mddev->sb_dirty)
2718 md_update_sb(mddev);
2719
2720 set_capacity(disk, mddev->array_size<<1);
2721
2722 /* If we call blk_queue_make_request here, it will
2723 * re-initialise max_sectors etc which may have been
2724 * refined inside -> run. So just set the bits we need to set.
2725 * Most initialisation happended when we called
2726 * blk_queue_make_request(..., md_fail_request)
2727 * earlier.
2728 */
2729 mddev->queue->queuedata = mddev;
2730 mddev->queue->make_request_fn = mddev->pers->make_request;
2731
2732 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08002733 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 return 0;
2735}
2736
2737static int restart_array(mddev_t *mddev)
2738{
2739 struct gendisk *disk = mddev->gendisk;
2740 int err;
2741
2742 /*
2743 * Complain if it has no devices
2744 */
2745 err = -ENXIO;
2746 if (list_empty(&mddev->disks))
2747 goto out;
2748
2749 if (mddev->pers) {
2750 err = -EBUSY;
2751 if (!mddev->ro)
2752 goto out;
2753
2754 mddev->safemode = 0;
2755 mddev->ro = 0;
2756 set_disk_ro(disk, 0);
2757
2758 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2759 mdname(mddev));
2760 /*
2761 * Kick recovery or resync if necessary
2762 */
2763 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2764 md_wakeup_thread(mddev->thread);
2765 err = 0;
2766 } else {
2767 printk(KERN_ERR "md: %s has no personality assigned.\n",
2768 mdname(mddev));
2769 err = -EINVAL;
2770 }
2771
2772out:
2773 return err;
2774}
2775
2776static int do_md_stop(mddev_t * mddev, int ro)
2777{
2778 int err = 0;
2779 struct gendisk *disk = mddev->gendisk;
2780
2781 if (mddev->pers) {
2782 if (atomic_read(&mddev->active)>2) {
2783 printk("md: %s still in use.\n",mdname(mddev));
2784 return -EBUSY;
2785 }
2786
2787 if (mddev->sync_thread) {
2788 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2789 md_unregister_thread(mddev->sync_thread);
2790 mddev->sync_thread = NULL;
2791 }
2792
2793 del_timer_sync(&mddev->safemode_timer);
2794
2795 invalidate_partition(disk, 0);
2796
2797 if (ro) {
2798 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002799 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 goto out;
2801 mddev->ro = 1;
2802 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002803 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002804 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (mddev->ro)
2806 set_disk_ro(disk, 0);
2807 blk_queue_make_request(mddev->queue, md_fail_request);
2808 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002809 if (mddev->pers->sync_request)
2810 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 module_put(mddev->pers->owner);
2813 mddev->pers = NULL;
2814 if (mddev->ro)
2815 mddev->ro = 0;
2816 }
2817 if (!mddev->in_sync) {
2818 /* mark array as shutdown cleanly */
2819 mddev->in_sync = 1;
2820 md_update_sb(mddev);
2821 }
2822 if (ro)
2823 set_disk_ro(disk, 1);
2824 }
NeilBrown32a76272005-06-21 17:17:14 -07002825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 /*
2827 * Free resources if final stop
2828 */
2829 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002830 mdk_rdev_t *rdev;
2831 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 struct gendisk *disk;
2833 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2834
NeilBrown978f9462006-02-02 14:28:05 -08002835 bitmap_destroy(mddev);
2836 if (mddev->bitmap_file) {
2837 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2838 fput(mddev->bitmap_file);
2839 mddev->bitmap_file = NULL;
2840 }
2841 mddev->bitmap_offset = 0;
2842
NeilBrown86e6ffd2005-11-08 21:39:24 -08002843 ITERATE_RDEV(mddev,rdev,tmp)
2844 if (rdev->raid_disk >= 0) {
2845 char nm[20];
2846 sprintf(nm, "rd%d", rdev->raid_disk);
2847 sysfs_remove_link(&mddev->kobj, nm);
2848 }
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 export_array(mddev);
2851
2852 mddev->array_size = 0;
2853 disk = mddev->gendisk;
2854 if (disk)
2855 set_capacity(disk, 0);
2856 mddev->changed = 1;
2857 } else
2858 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2859 mdname(mddev));
2860 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08002861 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862out:
2863 return err;
2864}
2865
2866static void autorun_array(mddev_t *mddev)
2867{
2868 mdk_rdev_t *rdev;
2869 struct list_head *tmp;
2870 int err;
2871
NeilBrowna757e642005-04-16 15:26:42 -07002872 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 printk(KERN_INFO "md: running: ");
2876
2877 ITERATE_RDEV(mddev,rdev,tmp) {
2878 char b[BDEVNAME_SIZE];
2879 printk("<%s>", bdevname(rdev->bdev,b));
2880 }
2881 printk("\n");
2882
2883 err = do_md_run (mddev);
2884 if (err) {
2885 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2886 do_md_stop (mddev, 0);
2887 }
2888}
2889
2890/*
2891 * lets try to run arrays based on all disks that have arrived
2892 * until now. (those are in pending_raid_disks)
2893 *
2894 * the method: pick the first pending disk, collect all disks with
2895 * the same UUID, remove all from the pending list and put them into
2896 * the 'same_array' list. Then order this list based on superblock
2897 * update time (freshest comes first), kick out 'old' disks and
2898 * compare superblocks. If everything's fine then run it.
2899 *
2900 * If "unit" is allocated, then bump its reference count
2901 */
2902static void autorun_devices(int part)
2903{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 struct list_head *tmp;
2905 mdk_rdev_t *rdev0, *rdev;
2906 mddev_t *mddev;
2907 char b[BDEVNAME_SIZE];
2908
2909 printk(KERN_INFO "md: autorun ...\n");
2910 while (!list_empty(&pending_raid_disks)) {
2911 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08002912 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 rdev0 = list_entry(pending_raid_disks.next,
2914 mdk_rdev_t, same_set);
2915
2916 printk(KERN_INFO "md: considering %s ...\n",
2917 bdevname(rdev0->bdev,b));
2918 INIT_LIST_HEAD(&candidates);
2919 ITERATE_RDEV_PENDING(rdev,tmp)
2920 if (super_90_load(rdev, rdev0, 0) >= 0) {
2921 printk(KERN_INFO "md: adding %s ...\n",
2922 bdevname(rdev->bdev,b));
2923 list_move(&rdev->same_set, &candidates);
2924 }
2925 /*
2926 * now we have a set of devices, with all of them having
2927 * mostly sane superblocks. It's time to allocate the
2928 * mddev.
2929 */
2930 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2931 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2932 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2933 break;
2934 }
2935 if (part)
2936 dev = MKDEV(mdp_major,
2937 rdev0->preferred_minor << MdpMinorShift);
2938 else
2939 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2940
2941 md_probe(dev, NULL, NULL);
2942 mddev = mddev_find(dev);
2943 if (!mddev) {
2944 printk(KERN_ERR
2945 "md: cannot allocate memory for md drive.\n");
2946 break;
2947 }
2948 if (mddev_lock(mddev))
2949 printk(KERN_WARNING "md: %s locked, cannot run\n",
2950 mdname(mddev));
2951 else if (mddev->raid_disks || mddev->major_version
2952 || !list_empty(&mddev->disks)) {
2953 printk(KERN_WARNING
2954 "md: %s already running, cannot run %s\n",
2955 mdname(mddev), bdevname(rdev0->bdev,b));
2956 mddev_unlock(mddev);
2957 } else {
2958 printk(KERN_INFO "md: created %s\n", mdname(mddev));
2959 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
2960 list_del_init(&rdev->same_set);
2961 if (bind_rdev_to_array(rdev, mddev))
2962 export_rdev(rdev);
2963 }
2964 autorun_array(mddev);
2965 mddev_unlock(mddev);
2966 }
2967 /* on success, candidates will be empty, on error
2968 * it won't...
2969 */
2970 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
2971 export_rdev(rdev);
2972 mddev_put(mddev);
2973 }
2974 printk(KERN_INFO "md: ... autorun DONE.\n");
2975}
2976
2977/*
2978 * import RAID devices based on one partition
2979 * if possible, the array gets run as well.
2980 */
2981
2982static int autostart_array(dev_t startdev)
2983{
2984 char b[BDEVNAME_SIZE];
2985 int err = -EINVAL, i;
2986 mdp_super_t *sb = NULL;
2987 mdk_rdev_t *start_rdev = NULL, *rdev;
2988
2989 start_rdev = md_import_device(startdev, 0, 0);
2990 if (IS_ERR(start_rdev))
2991 return err;
2992
2993
2994 /* NOTE: this can only work for 0.90.0 superblocks */
2995 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
2996 if (sb->major_version != 0 ||
2997 sb->minor_version != 90 ) {
2998 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
2999 export_rdev(start_rdev);
3000 return err;
3001 }
3002
NeilBrownb2d444d2005-11-08 21:39:31 -08003003 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 printk(KERN_WARNING
3005 "md: can not autostart based on faulty %s!\n",
3006 bdevname(start_rdev->bdev,b));
3007 export_rdev(start_rdev);
3008 return err;
3009 }
3010 list_add(&start_rdev->same_set, &pending_raid_disks);
3011
3012 for (i = 0; i < MD_SB_DISKS; i++) {
3013 mdp_disk_t *desc = sb->disks + i;
3014 dev_t dev = MKDEV(desc->major, desc->minor);
3015
3016 if (!dev)
3017 continue;
3018 if (dev == startdev)
3019 continue;
3020 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3021 continue;
3022 rdev = md_import_device(dev, 0, 0);
3023 if (IS_ERR(rdev))
3024 continue;
3025
3026 list_add(&rdev->same_set, &pending_raid_disks);
3027 }
3028
3029 /*
3030 * possibly return codes
3031 */
3032 autorun_devices(0);
3033 return 0;
3034
3035}
3036
3037
3038static int get_version(void __user * arg)
3039{
3040 mdu_version_t ver;
3041
3042 ver.major = MD_MAJOR_VERSION;
3043 ver.minor = MD_MINOR_VERSION;
3044 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3045
3046 if (copy_to_user(arg, &ver, sizeof(ver)))
3047 return -EFAULT;
3048
3049 return 0;
3050}
3051
3052static int get_array_info(mddev_t * mddev, void __user * arg)
3053{
3054 mdu_array_info_t info;
3055 int nr,working,active,failed,spare;
3056 mdk_rdev_t *rdev;
3057 struct list_head *tmp;
3058
3059 nr=working=active=failed=spare=0;
3060 ITERATE_RDEV(mddev,rdev,tmp) {
3061 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003062 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 failed++;
3064 else {
3065 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003066 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 active++;
3068 else
3069 spare++;
3070 }
3071 }
3072
3073 info.major_version = mddev->major_version;
3074 info.minor_version = mddev->minor_version;
3075 info.patch_version = MD_PATCHLEVEL_VERSION;
3076 info.ctime = mddev->ctime;
3077 info.level = mddev->level;
3078 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003079 if (info.size != mddev->size) /* overflow */
3080 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 info.nr_disks = nr;
3082 info.raid_disks = mddev->raid_disks;
3083 info.md_minor = mddev->md_minor;
3084 info.not_persistent= !mddev->persistent;
3085
3086 info.utime = mddev->utime;
3087 info.state = 0;
3088 if (mddev->in_sync)
3089 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003090 if (mddev->bitmap && mddev->bitmap_offset)
3091 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 info.active_disks = active;
3093 info.working_disks = working;
3094 info.failed_disks = failed;
3095 info.spare_disks = spare;
3096
3097 info.layout = mddev->layout;
3098 info.chunk_size = mddev->chunk_size;
3099
3100 if (copy_to_user(arg, &info, sizeof(info)))
3101 return -EFAULT;
3102
3103 return 0;
3104}
3105
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003106static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003107{
3108 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3109 char *ptr, *buf = NULL;
3110 int err = -ENOMEM;
3111
3112 file = kmalloc(sizeof(*file), GFP_KERNEL);
3113 if (!file)
3114 goto out;
3115
3116 /* bitmap disabled, zero the first byte and copy out */
3117 if (!mddev->bitmap || !mddev->bitmap->file) {
3118 file->pathname[0] = '\0';
3119 goto copy_out;
3120 }
3121
3122 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3123 if (!buf)
3124 goto out;
3125
3126 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3127 if (!ptr)
3128 goto out;
3129
3130 strcpy(file->pathname, ptr);
3131
3132copy_out:
3133 err = 0;
3134 if (copy_to_user(arg, file, sizeof(*file)))
3135 err = -EFAULT;
3136out:
3137 kfree(buf);
3138 kfree(file);
3139 return err;
3140}
3141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142static int get_disk_info(mddev_t * mddev, void __user * arg)
3143{
3144 mdu_disk_info_t info;
3145 unsigned int nr;
3146 mdk_rdev_t *rdev;
3147
3148 if (copy_from_user(&info, arg, sizeof(info)))
3149 return -EFAULT;
3150
3151 nr = info.number;
3152
3153 rdev = find_rdev_nr(mddev, nr);
3154 if (rdev) {
3155 info.major = MAJOR(rdev->bdev->bd_dev);
3156 info.minor = MINOR(rdev->bdev->bd_dev);
3157 info.raid_disk = rdev->raid_disk;
3158 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003159 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003161 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 info.state |= (1<<MD_DISK_ACTIVE);
3163 info.state |= (1<<MD_DISK_SYNC);
3164 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003165 if (test_bit(WriteMostly, &rdev->flags))
3166 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 } else {
3168 info.major = info.minor = 0;
3169 info.raid_disk = -1;
3170 info.state = (1<<MD_DISK_REMOVED);
3171 }
3172
3173 if (copy_to_user(arg, &info, sizeof(info)))
3174 return -EFAULT;
3175
3176 return 0;
3177}
3178
3179static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3180{
3181 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3182 mdk_rdev_t *rdev;
3183 dev_t dev = MKDEV(info->major,info->minor);
3184
3185 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3186 return -EOVERFLOW;
3187
3188 if (!mddev->raid_disks) {
3189 int err;
3190 /* expecting a device which has a superblock */
3191 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3192 if (IS_ERR(rdev)) {
3193 printk(KERN_WARNING
3194 "md: md_import_device returned %ld\n",
3195 PTR_ERR(rdev));
3196 return PTR_ERR(rdev);
3197 }
3198 if (!list_empty(&mddev->disks)) {
3199 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3200 mdk_rdev_t, same_set);
3201 int err = super_types[mddev->major_version]
3202 .load_super(rdev, rdev0, mddev->minor_version);
3203 if (err < 0) {
3204 printk(KERN_WARNING
3205 "md: %s has different UUID to %s\n",
3206 bdevname(rdev->bdev,b),
3207 bdevname(rdev0->bdev,b2));
3208 export_rdev(rdev);
3209 return -EINVAL;
3210 }
3211 }
3212 err = bind_rdev_to_array(rdev, mddev);
3213 if (err)
3214 export_rdev(rdev);
3215 return err;
3216 }
3217
3218 /*
3219 * add_new_disk can be used once the array is assembled
3220 * to add "hot spares". They must already have a superblock
3221 * written
3222 */
3223 if (mddev->pers) {
3224 int err;
3225 if (!mddev->pers->hot_add_disk) {
3226 printk(KERN_WARNING
3227 "%s: personality does not support diskops!\n",
3228 mdname(mddev));
3229 return -EINVAL;
3230 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003231 if (mddev->persistent)
3232 rdev = md_import_device(dev, mddev->major_version,
3233 mddev->minor_version);
3234 else
3235 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 if (IS_ERR(rdev)) {
3237 printk(KERN_WARNING
3238 "md: md_import_device returned %ld\n",
3239 PTR_ERR(rdev));
3240 return PTR_ERR(rdev);
3241 }
NeilBrown41158c72005-06-21 17:17:25 -07003242 /* set save_raid_disk if appropriate */
3243 if (!mddev->persistent) {
3244 if (info->state & (1<<MD_DISK_SYNC) &&
3245 info->raid_disk < mddev->raid_disks)
3246 rdev->raid_disk = info->raid_disk;
3247 else
3248 rdev->raid_disk = -1;
3249 } else
3250 super_types[mddev->major_version].
3251 validate_super(mddev, rdev);
3252 rdev->saved_raid_disk = rdev->raid_disk;
3253
NeilBrownb2d444d2005-11-08 21:39:31 -08003254 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003255 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3256 set_bit(WriteMostly, &rdev->flags);
3257
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 rdev->raid_disk = -1;
3259 err = bind_rdev_to_array(rdev, mddev);
3260 if (err)
3261 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003262
3263 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003264 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return err;
3266 }
3267
3268 /* otherwise, add_new_disk is only allowed
3269 * for major_version==0 superblocks
3270 */
3271 if (mddev->major_version != 0) {
3272 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3273 mdname(mddev));
3274 return -EINVAL;
3275 }
3276
3277 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3278 int err;
3279 rdev = md_import_device (dev, -1, 0);
3280 if (IS_ERR(rdev)) {
3281 printk(KERN_WARNING
3282 "md: error, md_import_device() returned %ld\n",
3283 PTR_ERR(rdev));
3284 return PTR_ERR(rdev);
3285 }
3286 rdev->desc_nr = info->number;
3287 if (info->raid_disk < mddev->raid_disks)
3288 rdev->raid_disk = info->raid_disk;
3289 else
3290 rdev->raid_disk = -1;
3291
NeilBrownb2d444d2005-11-08 21:39:31 -08003292 rdev->flags = 0;
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003295 if (info->state & (1<<MD_DISK_SYNC))
3296 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003298 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3299 set_bit(WriteMostly, &rdev->flags);
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 if (!mddev->persistent) {
3302 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3303 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3304 } else
3305 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3306 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3307
NeilBrown2bf071b2006-01-06 00:20:55 -08003308 err = bind_rdev_to_array(rdev, mddev);
3309 if (err) {
3310 export_rdev(rdev);
3311 return err;
3312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314
3315 return 0;
3316}
3317
3318static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3319{
3320 char b[BDEVNAME_SIZE];
3321 mdk_rdev_t *rdev;
3322
3323 if (!mddev->pers)
3324 return -ENODEV;
3325
3326 rdev = find_rdev(mddev, dev);
3327 if (!rdev)
3328 return -ENXIO;
3329
3330 if (rdev->raid_disk >= 0)
3331 goto busy;
3332
3333 kick_rdev_from_array(rdev);
3334 md_update_sb(mddev);
NeilBrownd7603b72006-01-06 00:20:30 -08003335 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336
3337 return 0;
3338busy:
3339 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3340 bdevname(rdev->bdev,b), mdname(mddev));
3341 return -EBUSY;
3342}
3343
3344static int hot_add_disk(mddev_t * mddev, dev_t dev)
3345{
3346 char b[BDEVNAME_SIZE];
3347 int err;
3348 unsigned int size;
3349 mdk_rdev_t *rdev;
3350
3351 if (!mddev->pers)
3352 return -ENODEV;
3353
3354 if (mddev->major_version != 0) {
3355 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3356 " version-0 superblocks.\n",
3357 mdname(mddev));
3358 return -EINVAL;
3359 }
3360 if (!mddev->pers->hot_add_disk) {
3361 printk(KERN_WARNING
3362 "%s: personality does not support diskops!\n",
3363 mdname(mddev));
3364 return -EINVAL;
3365 }
3366
3367 rdev = md_import_device (dev, -1, 0);
3368 if (IS_ERR(rdev)) {
3369 printk(KERN_WARNING
3370 "md: error, md_import_device() returned %ld\n",
3371 PTR_ERR(rdev));
3372 return -EINVAL;
3373 }
3374
3375 if (mddev->persistent)
3376 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3377 else
3378 rdev->sb_offset =
3379 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3380
3381 size = calc_dev_size(rdev, mddev->chunk_size);
3382 rdev->size = size;
3383
NeilBrownb2d444d2005-11-08 21:39:31 -08003384 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 printk(KERN_WARNING
3386 "md: can not hot-add faulty %s disk to %s!\n",
3387 bdevname(rdev->bdev,b), mdname(mddev));
3388 err = -EINVAL;
3389 goto abort_export;
3390 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003391 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 rdev->desc_nr = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003393 err = bind_rdev_to_array(rdev, mddev);
3394 if (err)
3395 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
3397 /*
3398 * The rest should better be atomic, we can have disk failures
3399 * noticed in interrupt contexts ...
3400 */
3401
3402 if (rdev->desc_nr == mddev->max_disks) {
3403 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3404 mdname(mddev));
3405 err = -EBUSY;
3406 goto abort_unbind_export;
3407 }
3408
3409 rdev->raid_disk = -1;
3410
3411 md_update_sb(mddev);
3412
3413 /*
3414 * Kick recovery, maybe this spare has to be added to the
3415 * array immediately.
3416 */
3417 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3418 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003419 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 return 0;
3421
3422abort_unbind_export:
3423 unbind_rdev_from_array(rdev);
3424
3425abort_export:
3426 export_rdev(rdev);
3427 return err;
3428}
3429
NeilBrown32a76272005-06-21 17:17:14 -07003430/* similar to deny_write_access, but accounts for our holding a reference
3431 * to the file ourselves */
3432static int deny_bitmap_write_access(struct file * file)
3433{
3434 struct inode *inode = file->f_mapping->host;
3435
3436 spin_lock(&inode->i_lock);
3437 if (atomic_read(&inode->i_writecount) > 1) {
3438 spin_unlock(&inode->i_lock);
3439 return -ETXTBSY;
3440 }
3441 atomic_set(&inode->i_writecount, -1);
3442 spin_unlock(&inode->i_lock);
3443
3444 return 0;
3445}
3446
3447static int set_bitmap_file(mddev_t *mddev, int fd)
3448{
3449 int err;
3450
NeilBrown36fa3062005-09-09 16:23:45 -07003451 if (mddev->pers) {
3452 if (!mddev->pers->quiesce)
3453 return -EBUSY;
3454 if (mddev->recovery || mddev->sync_thread)
3455 return -EBUSY;
3456 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003457 }
3458
NeilBrown36fa3062005-09-09 16:23:45 -07003459
3460 if (fd >= 0) {
3461 if (mddev->bitmap)
3462 return -EEXIST; /* cannot add when bitmap is present */
3463 mddev->bitmap_file = fget(fd);
3464
3465 if (mddev->bitmap_file == NULL) {
3466 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3467 mdname(mddev));
3468 return -EBADF;
3469 }
3470
3471 err = deny_bitmap_write_access(mddev->bitmap_file);
3472 if (err) {
3473 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3474 mdname(mddev));
3475 fput(mddev->bitmap_file);
3476 mddev->bitmap_file = NULL;
3477 return err;
3478 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003479 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003480 } else if (mddev->bitmap == NULL)
3481 return -ENOENT; /* cannot remove what isn't there */
3482 err = 0;
3483 if (mddev->pers) {
3484 mddev->pers->quiesce(mddev, 1);
3485 if (fd >= 0)
3486 err = bitmap_create(mddev);
3487 if (fd < 0 || err)
3488 bitmap_destroy(mddev);
3489 mddev->pers->quiesce(mddev, 0);
3490 } else if (fd < 0) {
3491 if (mddev->bitmap_file)
3492 fput(mddev->bitmap_file);
3493 mddev->bitmap_file = NULL;
3494 }
3495
NeilBrown32a76272005-06-21 17:17:14 -07003496 return err;
3497}
3498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499/*
3500 * set_array_info is used two different ways
3501 * The original usage is when creating a new array.
3502 * In this usage, raid_disks is > 0 and it together with
3503 * level, size, not_persistent,layout,chunksize determine the
3504 * shape of the array.
3505 * This will always create an array with a type-0.90.0 superblock.
3506 * The newer usage is when assembling an array.
3507 * In this case raid_disks will be 0, and the major_version field is
3508 * use to determine which style super-blocks are to be found on the devices.
3509 * The minor and patch _version numbers are also kept incase the
3510 * super_block handler wishes to interpret them.
3511 */
3512static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3513{
3514
3515 if (info->raid_disks == 0) {
3516 /* just setting version number for superblock loading */
3517 if (info->major_version < 0 ||
3518 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3519 super_types[info->major_version].name == NULL) {
3520 /* maybe try to auto-load a module? */
3521 printk(KERN_INFO
3522 "md: superblock version %d not known\n",
3523 info->major_version);
3524 return -EINVAL;
3525 }
3526 mddev->major_version = info->major_version;
3527 mddev->minor_version = info->minor_version;
3528 mddev->patch_version = info->patch_version;
3529 return 0;
3530 }
3531 mddev->major_version = MD_MAJOR_VERSION;
3532 mddev->minor_version = MD_MINOR_VERSION;
3533 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3534 mddev->ctime = get_seconds();
3535
3536 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08003537 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 mddev->size = info->size;
3539 mddev->raid_disks = info->raid_disks;
3540 /* don't set md_minor, it is determined by which /dev/md* was
3541 * openned
3542 */
3543 if (info->state & (1<<MD_SB_CLEAN))
3544 mddev->recovery_cp = MaxSector;
3545 else
3546 mddev->recovery_cp = 0;
3547 mddev->persistent = ! info->not_persistent;
3548
3549 mddev->layout = info->layout;
3550 mddev->chunk_size = info->chunk_size;
3551
3552 mddev->max_disks = MD_SB_DISKS;
3553
3554 mddev->sb_dirty = 1;
3555
NeilBrownb2a27032005-11-28 13:44:12 -08003556 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3557 mddev->bitmap_offset = 0;
3558
NeilBrownf6705572006-03-27 01:18:11 -08003559 mddev->reshape_position = MaxSector;
3560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 /*
3562 * Generate a 128 bit UUID
3563 */
3564 get_random_bytes(mddev->uuid, 16);
3565
NeilBrownf6705572006-03-27 01:18:11 -08003566 mddev->new_level = mddev->level;
3567 mddev->new_chunk = mddev->chunk_size;
3568 mddev->new_layout = mddev->layout;
3569 mddev->delta_disks = 0;
3570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 return 0;
3572}
3573
NeilBrowna35b0d62006-01-06 00:20:49 -08003574static int update_size(mddev_t *mddev, unsigned long size)
3575{
3576 mdk_rdev_t * rdev;
3577 int rv;
3578 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08003579 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08003580
3581 if (mddev->pers->resize == NULL)
3582 return -EINVAL;
3583 /* The "size" is the amount of each device that is used.
3584 * This can only make sense for arrays with redundancy.
3585 * linear and raid0 always use whatever space is available
3586 * We can only consider changing the size if no resync
3587 * or reconstruction is happening, and if the new size
3588 * is acceptable. It must fit before the sb_offset or,
3589 * if that is <data_offset, it must fit before the
3590 * size of each device.
3591 * If size is zero, we find the largest size that fits.
3592 */
3593 if (mddev->sync_thread)
3594 return -EBUSY;
3595 ITERATE_RDEV(mddev,rdev,tmp) {
3596 sector_t avail;
NeilBrowna35b0d62006-01-06 00:20:49 -08003597 if (rdev->sb_offset > rdev->data_offset)
3598 avail = (rdev->sb_offset*2) - rdev->data_offset;
3599 else
3600 avail = get_capacity(rdev->bdev->bd_disk)
3601 - rdev->data_offset;
3602 if (fit && (size == 0 || size > avail/2))
3603 size = avail/2;
3604 if (avail < ((sector_t)size << 1))
3605 return -ENOSPC;
3606 }
3607 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3608 if (!rv) {
3609 struct block_device *bdev;
3610
3611 bdev = bdget_disk(mddev->gendisk, 0);
3612 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003613 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08003614 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003615 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08003616 bdput(bdev);
3617 }
3618 }
3619 return rv;
3620}
3621
NeilBrownda943b992006-01-06 00:20:54 -08003622static int update_raid_disks(mddev_t *mddev, int raid_disks)
3623{
3624 int rv;
3625 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08003626 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08003627 return -EINVAL;
3628 if (raid_disks <= 0 ||
3629 raid_disks >= mddev->max_disks)
3630 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08003631 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08003632 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08003633 mddev->delta_disks = raid_disks - mddev->raid_disks;
3634
3635 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08003636 return rv;
3637}
3638
3639
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640/*
3641 * update_array_info is used to change the configuration of an
3642 * on-line array.
3643 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3644 * fields in the info are checked against the array.
3645 * Any differences that cannot be handled will cause an error.
3646 * Normally, only one change can be managed at a time.
3647 */
3648static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3649{
3650 int rv = 0;
3651 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07003652 int state = 0;
3653
3654 /* calculate expected state,ignoring low bits */
3655 if (mddev->bitmap && mddev->bitmap_offset)
3656 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 if (mddev->major_version != info->major_version ||
3659 mddev->minor_version != info->minor_version ||
3660/* mddev->patch_version != info->patch_version || */
3661 mddev->ctime != info->ctime ||
3662 mddev->level != info->level ||
3663/* mddev->layout != info->layout || */
3664 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07003665 mddev->chunk_size != info->chunk_size ||
3666 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3667 ((state^info->state) & 0xfffffe00)
3668 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 return -EINVAL;
3670 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08003671 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 if (mddev->raid_disks != info->raid_disks) cnt++;
3673 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07003674 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 if (cnt == 0) return 0;
3676 if (cnt > 1) return -EINVAL;
3677
3678 if (mddev->layout != info->layout) {
3679 /* Change layout
3680 * we don't need to do anything at the md level, the
3681 * personality will take care of it all.
3682 */
3683 if (mddev->pers->reconfig == NULL)
3684 return -EINVAL;
3685 else
3686 return mddev->pers->reconfig(mddev, info->layout, -1);
3687 }
NeilBrown284ae7c2006-02-03 03:03:40 -08003688 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08003689 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690
NeilBrownda943b992006-01-06 00:20:54 -08003691 if (mddev->raid_disks != info->raid_disks)
3692 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693
NeilBrown36fa3062005-09-09 16:23:45 -07003694 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3695 if (mddev->pers->quiesce == NULL)
3696 return -EINVAL;
3697 if (mddev->recovery || mddev->sync_thread)
3698 return -EBUSY;
3699 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3700 /* add the bitmap */
3701 if (mddev->bitmap)
3702 return -EEXIST;
3703 if (mddev->default_bitmap_offset == 0)
3704 return -EINVAL;
3705 mddev->bitmap_offset = mddev->default_bitmap_offset;
3706 mddev->pers->quiesce(mddev, 1);
3707 rv = bitmap_create(mddev);
3708 if (rv)
3709 bitmap_destroy(mddev);
3710 mddev->pers->quiesce(mddev, 0);
3711 } else {
3712 /* remove the bitmap */
3713 if (!mddev->bitmap)
3714 return -ENOENT;
3715 if (mddev->bitmap->file)
3716 return -EINVAL;
3717 mddev->pers->quiesce(mddev, 1);
3718 bitmap_destroy(mddev);
3719 mddev->pers->quiesce(mddev, 0);
3720 mddev->bitmap_offset = 0;
3721 }
3722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 md_update_sb(mddev);
3724 return rv;
3725}
3726
3727static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3728{
3729 mdk_rdev_t *rdev;
3730
3731 if (mddev->pers == NULL)
3732 return -ENODEV;
3733
3734 rdev = find_rdev(mddev, dev);
3735 if (!rdev)
3736 return -ENODEV;
3737
3738 md_error(mddev, rdev);
3739 return 0;
3740}
3741
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003742static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3743{
3744 mddev_t *mddev = bdev->bd_disk->private_data;
3745
3746 geo->heads = 2;
3747 geo->sectors = 4;
3748 geo->cylinders = get_capacity(mddev->gendisk) / 8;
3749 return 0;
3750}
3751
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752static int md_ioctl(struct inode *inode, struct file *file,
3753 unsigned int cmd, unsigned long arg)
3754{
3755 int err = 0;
3756 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 mddev_t *mddev = NULL;
3758
3759 if (!capable(CAP_SYS_ADMIN))
3760 return -EACCES;
3761
3762 /*
3763 * Commands dealing with the RAID driver but not any
3764 * particular array:
3765 */
3766 switch (cmd)
3767 {
3768 case RAID_VERSION:
3769 err = get_version(argp);
3770 goto done;
3771
3772 case PRINT_RAID_DEBUG:
3773 err = 0;
3774 md_print_devices();
3775 goto done;
3776
3777#ifndef MODULE
3778 case RAID_AUTORUN:
3779 err = 0;
3780 autostart_arrays(arg);
3781 goto done;
3782#endif
3783 default:;
3784 }
3785
3786 /*
3787 * Commands creating/starting a new array:
3788 */
3789
3790 mddev = inode->i_bdev->bd_disk->private_data;
3791
3792 if (!mddev) {
3793 BUG();
3794 goto abort;
3795 }
3796
3797
3798 if (cmd == START_ARRAY) {
3799 /* START_ARRAY doesn't need to lock the array as autostart_array
3800 * does the locking, and it could even be a different array
3801 */
3802 static int cnt = 3;
3803 if (cnt > 0 ) {
3804 printk(KERN_WARNING
3805 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003806 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 current->comm, current->pid);
3808 cnt--;
3809 }
3810 err = autostart_array(new_decode_dev(arg));
3811 if (err) {
3812 printk(KERN_WARNING "md: autostart failed!\n");
3813 goto abort;
3814 }
3815 goto done;
3816 }
3817
3818 err = mddev_lock(mddev);
3819 if (err) {
3820 printk(KERN_INFO
3821 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3822 err, cmd);
3823 goto abort;
3824 }
3825
3826 switch (cmd)
3827 {
3828 case SET_ARRAY_INFO:
3829 {
3830 mdu_array_info_t info;
3831 if (!arg)
3832 memset(&info, 0, sizeof(info));
3833 else if (copy_from_user(&info, argp, sizeof(info))) {
3834 err = -EFAULT;
3835 goto abort_unlock;
3836 }
3837 if (mddev->pers) {
3838 err = update_array_info(mddev, &info);
3839 if (err) {
3840 printk(KERN_WARNING "md: couldn't update"
3841 " array info. %d\n", err);
3842 goto abort_unlock;
3843 }
3844 goto done_unlock;
3845 }
3846 if (!list_empty(&mddev->disks)) {
3847 printk(KERN_WARNING
3848 "md: array %s already has disks!\n",
3849 mdname(mddev));
3850 err = -EBUSY;
3851 goto abort_unlock;
3852 }
3853 if (mddev->raid_disks) {
3854 printk(KERN_WARNING
3855 "md: array %s already initialised!\n",
3856 mdname(mddev));
3857 err = -EBUSY;
3858 goto abort_unlock;
3859 }
3860 err = set_array_info(mddev, &info);
3861 if (err) {
3862 printk(KERN_WARNING "md: couldn't set"
3863 " array info. %d\n", err);
3864 goto abort_unlock;
3865 }
3866 }
3867 goto done_unlock;
3868
3869 default:;
3870 }
3871
3872 /*
3873 * Commands querying/configuring an existing array:
3874 */
NeilBrown32a76272005-06-21 17:17:14 -07003875 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3876 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3877 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3878 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 err = -ENODEV;
3880 goto abort_unlock;
3881 }
3882
3883 /*
3884 * Commands even a read-only array can execute:
3885 */
3886 switch (cmd)
3887 {
3888 case GET_ARRAY_INFO:
3889 err = get_array_info(mddev, argp);
3890 goto done_unlock;
3891
NeilBrown32a76272005-06-21 17:17:14 -07003892 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003893 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003894 goto done_unlock;
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 case GET_DISK_INFO:
3897 err = get_disk_info(mddev, argp);
3898 goto done_unlock;
3899
3900 case RESTART_ARRAY_RW:
3901 err = restart_array(mddev);
3902 goto done_unlock;
3903
3904 case STOP_ARRAY:
3905 err = do_md_stop (mddev, 0);
3906 goto done_unlock;
3907
3908 case STOP_ARRAY_RO:
3909 err = do_md_stop (mddev, 1);
3910 goto done_unlock;
3911
3912 /*
3913 * We have a problem here : there is no easy way to give a CHS
3914 * virtual geometry. We currently pretend that we have a 2 heads
3915 * 4 sectors (with a BIG number of cylinders...). This drives
3916 * dosfs just mad... ;-)
3917 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 }
3919
3920 /*
3921 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003922 * superblock, so we do not allow them on read-only arrays.
3923 * However non-MD ioctls (e.g. get-size) will still come through
3924 * here and hit the 'default' below, so only disallow
3925 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 */
NeilBrownf91de922005-11-08 21:39:36 -08003927 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3928 mddev->ro && mddev->pers) {
3929 if (mddev->ro == 2) {
3930 mddev->ro = 0;
3931 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3932 md_wakeup_thread(mddev->thread);
3933
3934 } else {
3935 err = -EROFS;
3936 goto abort_unlock;
3937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 }
3939
3940 switch (cmd)
3941 {
3942 case ADD_NEW_DISK:
3943 {
3944 mdu_disk_info_t info;
3945 if (copy_from_user(&info, argp, sizeof(info)))
3946 err = -EFAULT;
3947 else
3948 err = add_new_disk(mddev, &info);
3949 goto done_unlock;
3950 }
3951
3952 case HOT_REMOVE_DISK:
3953 err = hot_remove_disk(mddev, new_decode_dev(arg));
3954 goto done_unlock;
3955
3956 case HOT_ADD_DISK:
3957 err = hot_add_disk(mddev, new_decode_dev(arg));
3958 goto done_unlock;
3959
3960 case SET_DISK_FAULTY:
3961 err = set_disk_faulty(mddev, new_decode_dev(arg));
3962 goto done_unlock;
3963
3964 case RUN_ARRAY:
3965 err = do_md_run (mddev);
3966 goto done_unlock;
3967
NeilBrown32a76272005-06-21 17:17:14 -07003968 case SET_BITMAP_FILE:
3969 err = set_bitmap_file(mddev, (int)arg);
3970 goto done_unlock;
3971
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 default:
3973 if (_IOC_TYPE(cmd) == MD_MAJOR)
3974 printk(KERN_WARNING "md: %s(pid %d) used"
3975 " obsolete MD ioctl, upgrade your"
3976 " software to use new ictls.\n",
3977 current->comm, current->pid);
3978 err = -EINVAL;
3979 goto abort_unlock;
3980 }
3981
3982done_unlock:
3983abort_unlock:
3984 mddev_unlock(mddev);
3985
3986 return err;
3987done:
3988 if (err)
3989 MD_BUG();
3990abort:
3991 return err;
3992}
3993
3994static int md_open(struct inode *inode, struct file *file)
3995{
3996 /*
3997 * Succeed if we can lock the mddev, which confirms that
3998 * it isn't being stopped right now.
3999 */
4000 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4001 int err;
4002
4003 if ((err = mddev_lock(mddev)))
4004 goto out;
4005
4006 err = 0;
4007 mddev_get(mddev);
4008 mddev_unlock(mddev);
4009
4010 check_disk_change(inode->i_bdev);
4011 out:
4012 return err;
4013}
4014
4015static int md_release(struct inode *inode, struct file * file)
4016{
4017 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4018
4019 if (!mddev)
4020 BUG();
4021 mddev_put(mddev);
4022
4023 return 0;
4024}
4025
4026static int md_media_changed(struct gendisk *disk)
4027{
4028 mddev_t *mddev = disk->private_data;
4029
4030 return mddev->changed;
4031}
4032
4033static int md_revalidate(struct gendisk *disk)
4034{
4035 mddev_t *mddev = disk->private_data;
4036
4037 mddev->changed = 0;
4038 return 0;
4039}
4040static struct block_device_operations md_fops =
4041{
4042 .owner = THIS_MODULE,
4043 .open = md_open,
4044 .release = md_release,
4045 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004046 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 .media_changed = md_media_changed,
4048 .revalidate_disk= md_revalidate,
4049};
4050
Adrian Bunk75c96f82005-05-05 16:16:09 -07004051static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
4053 mdk_thread_t *thread = arg;
4054
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 /*
4056 * md_thread is a 'system-thread', it's priority should be very
4057 * high. We avoid resource deadlocks individually in each
4058 * raid personality. (RAID5 does preallocation) We also use RR and
4059 * the very same RT priority as kswapd, thus we will never get
4060 * into a priority inversion deadlock.
4061 *
4062 * we definitely have to have equal or higher priority than
4063 * bdflush, otherwise bdflush will deadlock if there are too
4064 * many dirty RAID5 blocks.
4065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
NeilBrown6985c432005-10-19 21:23:47 -07004067 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004068 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
NeilBrown93588e22005-11-15 00:09:12 -08004070 /* We need to wait INTERRUPTIBLE so that
4071 * we don't add to the load-average.
4072 * That means we need to be sure no signals are
4073 * pending
4074 */
4075 if (signal_pending(current))
4076 flush_signals(current);
4077
4078 wait_event_interruptible_timeout
4079 (thread->wqueue,
4080 test_bit(THREAD_WAKEUP, &thread->flags)
4081 || kthread_should_stop(),
4082 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004083 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
4085 clear_bit(THREAD_WAKEUP, &thread->flags);
4086
NeilBrown787453c2005-11-08 21:39:43 -08004087 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004089
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 return 0;
4091}
4092
4093void md_wakeup_thread(mdk_thread_t *thread)
4094{
4095 if (thread) {
4096 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4097 set_bit(THREAD_WAKEUP, &thread->flags);
4098 wake_up(&thread->wqueue);
4099 }
4100}
4101
4102mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4103 const char *name)
4104{
4105 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106
NeilBrown9ffae0c2006-01-06 00:20:32 -08004107 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 if (!thread)
4109 return NULL;
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 init_waitqueue_head(&thread->wqueue);
4112
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 thread->run = run;
4114 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004115 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004116 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004117 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 kfree(thread);
4119 return NULL;
4120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 return thread;
4122}
4123
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124void md_unregister_thread(mdk_thread_t *thread)
4125{
NeilBrownd28446f2005-04-16 15:26:41 -07004126 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004127
4128 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 kfree(thread);
4130}
4131
4132void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4133{
4134 if (!mddev) {
4135 MD_BUG();
4136 return;
4137 }
4138
NeilBrownb2d444d2005-11-08 21:39:31 -08004139 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return;
NeilBrown32a76272005-06-21 17:17:14 -07004141/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4143 mdname(mddev),
4144 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4145 __builtin_return_address(0),__builtin_return_address(1),
4146 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004147*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 if (!mddev->pers->error_handler)
4149 return;
4150 mddev->pers->error_handler(mddev,rdev);
4151 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4152 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4153 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004154 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155}
4156
4157/* seq_file implementation /proc/mdstat */
4158
4159static void status_unused(struct seq_file *seq)
4160{
4161 int i = 0;
4162 mdk_rdev_t *rdev;
4163 struct list_head *tmp;
4164
4165 seq_printf(seq, "unused devices: ");
4166
4167 ITERATE_RDEV_PENDING(rdev,tmp) {
4168 char b[BDEVNAME_SIZE];
4169 i++;
4170 seq_printf(seq, "%s ",
4171 bdevname(rdev->bdev,b));
4172 }
4173 if (!i)
4174 seq_printf(seq, "<none>");
4175
4176 seq_printf(seq, "\n");
4177}
4178
4179
4180static void status_resync(struct seq_file *seq, mddev_t * mddev)
4181{
NeilBrown4588b422006-03-27 01:18:04 -08004182 sector_t max_blocks, resync, res;
4183 unsigned long dt, db, rt;
4184 int scale;
4185 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
4187 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4188
4189 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4190 max_blocks = mddev->resync_max_sectors >> 1;
4191 else
4192 max_blocks = mddev->size;
4193
4194 /*
4195 * Should not happen.
4196 */
4197 if (!max_blocks) {
4198 MD_BUG();
4199 return;
4200 }
NeilBrown4588b422006-03-27 01:18:04 -08004201 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4202 * in a sector_t, and (max_blocks>>scale) will fit in a
4203 * u32, as those are the requirements for sector_div.
4204 * Thus 'scale' must be at least 10
4205 */
4206 scale = 10;
4207 if (sizeof(sector_t) > sizeof(unsigned long)) {
4208 while ( max_blocks/2 > (1ULL<<(scale+32)))
4209 scale++;
4210 }
4211 res = (resync>>scale)*1000;
4212 sector_div(res, (u32)((max_blocks>>scale)+1));
4213
4214 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 {
NeilBrown4588b422006-03-27 01:18:04 -08004216 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 seq_printf(seq, "[");
4218 for (i = 0; i < x; i++)
4219 seq_printf(seq, "=");
4220 seq_printf(seq, ">");
4221 for (i = 0; i < y; i++)
4222 seq_printf(seq, ".");
4223 seq_printf(seq, "] ");
4224 }
NeilBrown4588b422006-03-27 01:18:04 -08004225 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004226 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4227 "reshape" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
NeilBrownccfcc3c2006-03-27 01:18:09 -08004229 "resync" : "recovery")),
NeilBrown4588b422006-03-27 01:18:04 -08004230 per_milli/10, per_milli % 10,
4231 (unsigned long long) resync,
4232 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
4234 /*
4235 * We do not want to overflow, so the order of operands and
4236 * the * 100 / 100 trick are important. We do a +1 to be
4237 * safe against division by zero. We only estimate anyway.
4238 *
4239 * dt: time from mark until now
4240 * db: blocks written from mark until now
4241 * rt: remaining time
4242 */
4243 dt = ((jiffies - mddev->resync_mark) / HZ);
4244 if (!dt) dt++;
4245 db = resync - (mddev->resync_mark_cnt/2);
NeilBrown4588b422006-03-27 01:18:04 -08004246 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
4248 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4249
4250 seq_printf(seq, " speed=%ldK/sec", db/dt);
4251}
4252
4253static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4254{
4255 struct list_head *tmp;
4256 loff_t l = *pos;
4257 mddev_t *mddev;
4258
4259 if (l >= 0x10000)
4260 return NULL;
4261 if (!l--)
4262 /* header */
4263 return (void*)1;
4264
4265 spin_lock(&all_mddevs_lock);
4266 list_for_each(tmp,&all_mddevs)
4267 if (!l--) {
4268 mddev = list_entry(tmp, mddev_t, all_mddevs);
4269 mddev_get(mddev);
4270 spin_unlock(&all_mddevs_lock);
4271 return mddev;
4272 }
4273 spin_unlock(&all_mddevs_lock);
4274 if (!l--)
4275 return (void*)2;/* tail */
4276 return NULL;
4277}
4278
4279static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4280{
4281 struct list_head *tmp;
4282 mddev_t *next_mddev, *mddev = v;
4283
4284 ++*pos;
4285 if (v == (void*)2)
4286 return NULL;
4287
4288 spin_lock(&all_mddevs_lock);
4289 if (v == (void*)1)
4290 tmp = all_mddevs.next;
4291 else
4292 tmp = mddev->all_mddevs.next;
4293 if (tmp != &all_mddevs)
4294 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4295 else {
4296 next_mddev = (void*)2;
4297 *pos = 0x10000;
4298 }
4299 spin_unlock(&all_mddevs_lock);
4300
4301 if (v != (void*)1)
4302 mddev_put(mddev);
4303 return next_mddev;
4304
4305}
4306
4307static void md_seq_stop(struct seq_file *seq, void *v)
4308{
4309 mddev_t *mddev = v;
4310
4311 if (mddev && v != (void*)1 && v != (void*)2)
4312 mddev_put(mddev);
4313}
4314
NeilBrownd7603b72006-01-06 00:20:30 -08004315struct mdstat_info {
4316 int event;
4317};
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319static int md_seq_show(struct seq_file *seq, void *v)
4320{
4321 mddev_t *mddev = v;
4322 sector_t size;
4323 struct list_head *tmp2;
4324 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004325 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004326 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327
4328 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004329 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 seq_printf(seq, "Personalities : ");
4331 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004332 list_for_each_entry(pers, &pers_list, list)
4333 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 spin_unlock(&pers_lock);
4336 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004337 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 return 0;
4339 }
4340 if (v == (void*)2) {
4341 status_unused(seq);
4342 return 0;
4343 }
4344
4345 if (mddev_lock(mddev)!=0)
4346 return -EINTR;
4347 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4348 seq_printf(seq, "%s : %sactive", mdname(mddev),
4349 mddev->pers ? "" : "in");
4350 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004351 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004353 if (mddev->ro==2)
4354 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 seq_printf(seq, " %s", mddev->pers->name);
4356 }
4357
4358 size = 0;
4359 ITERATE_RDEV(mddev,rdev,tmp2) {
4360 char b[BDEVNAME_SIZE];
4361 seq_printf(seq, " %s[%d]",
4362 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004363 if (test_bit(WriteMostly, &rdev->flags))
4364 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004365 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 seq_printf(seq, "(F)");
4367 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004368 } else if (rdev->raid_disk < 0)
4369 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 size += rdev->size;
4371 }
4372
4373 if (!list_empty(&mddev->disks)) {
4374 if (mddev->pers)
4375 seq_printf(seq, "\n %llu blocks",
4376 (unsigned long long)mddev->array_size);
4377 else
4378 seq_printf(seq, "\n %llu blocks",
4379 (unsigned long long)size);
4380 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004381 if (mddev->persistent) {
4382 if (mddev->major_version != 0 ||
4383 mddev->minor_version != 90) {
4384 seq_printf(seq," super %d.%d",
4385 mddev->major_version,
4386 mddev->minor_version);
4387 }
4388 } else
4389 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
4391 if (mddev->pers) {
4392 mddev->pers->status (seq, mddev);
4393 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004394 if (mddev->pers->sync_request) {
4395 if (mddev->curr_resync > 2) {
4396 status_resync (seq, mddev);
4397 seq_printf(seq, "\n ");
4398 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4399 seq_printf(seq, "\tresync=DELAYED\n ");
4400 else if (mddev->recovery_cp < MaxSector)
4401 seq_printf(seq, "\tresync=PENDING\n ");
4402 }
NeilBrown32a76272005-06-21 17:17:14 -07004403 } else
4404 seq_printf(seq, "\n ");
4405
4406 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004407 unsigned long chunk_kb;
4408 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004409 spin_lock_irqsave(&bitmap->lock, flags);
4410 chunk_kb = bitmap->chunksize >> 10;
4411 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4412 "%lu%s chunk",
4413 bitmap->pages - bitmap->missing_pages,
4414 bitmap->pages,
4415 (bitmap->pages - bitmap->missing_pages)
4416 << (PAGE_SHIFT - 10),
4417 chunk_kb ? chunk_kb : bitmap->chunksize,
4418 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004419 if (bitmap->file) {
4420 seq_printf(seq, ", file: ");
4421 seq_path(seq, bitmap->file->f_vfsmnt,
4422 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004423 }
NeilBrown78d742d2005-06-21 17:17:15 -07004424
NeilBrown32a76272005-06-21 17:17:14 -07004425 seq_printf(seq, "\n");
4426 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
4428
4429 seq_printf(seq, "\n");
4430 }
4431 mddev_unlock(mddev);
4432
4433 return 0;
4434}
4435
4436static struct seq_operations md_seq_ops = {
4437 .start = md_seq_start,
4438 .next = md_seq_next,
4439 .stop = md_seq_stop,
4440 .show = md_seq_show,
4441};
4442
4443static int md_seq_open(struct inode *inode, struct file *file)
4444{
4445 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004446 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4447 if (mi == NULL)
4448 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
4450 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004451 if (error)
4452 kfree(mi);
4453 else {
4454 struct seq_file *p = file->private_data;
4455 p->private = mi;
4456 mi->event = atomic_read(&md_event_count);
4457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 return error;
4459}
4460
NeilBrownd7603b72006-01-06 00:20:30 -08004461static int md_seq_release(struct inode *inode, struct file *file)
4462{
4463 struct seq_file *m = file->private_data;
4464 struct mdstat_info *mi = m->private;
4465 m->private = NULL;
4466 kfree(mi);
4467 return seq_release(inode, file);
4468}
4469
4470static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4471{
4472 struct seq_file *m = filp->private_data;
4473 struct mdstat_info *mi = m->private;
4474 int mask;
4475
4476 poll_wait(filp, &md_event_waiters, wait);
4477
4478 /* always allow read */
4479 mask = POLLIN | POLLRDNORM;
4480
4481 if (mi->event != atomic_read(&md_event_count))
4482 mask |= POLLERR | POLLPRI;
4483 return mask;
4484}
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486static struct file_operations md_seq_fops = {
4487 .open = md_seq_open,
4488 .read = seq_read,
4489 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004490 .release = md_seq_release,
4491 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492};
4493
NeilBrown2604b702006-01-06 00:20:36 -08004494int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004497 list_add_tail(&p->list, &pers_list);
4498 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 spin_unlock(&pers_lock);
4500 return 0;
4501}
4502
NeilBrown2604b702006-01-06 00:20:36 -08004503int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
NeilBrown2604b702006-01-06 00:20:36 -08004505 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004507 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 spin_unlock(&pers_lock);
4509 return 0;
4510}
4511
4512static int is_mddev_idle(mddev_t *mddev)
4513{
4514 mdk_rdev_t * rdev;
4515 struct list_head *tmp;
4516 int idle;
4517 unsigned long curr_events;
4518
4519 idle = 1;
4520 ITERATE_RDEV(mddev,rdev,tmp) {
4521 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004522 curr_events = disk_stat_read(disk, sectors[0]) +
4523 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004525 /* The difference between curr_events and last_events
4526 * will be affected by any new non-sync IO (making
4527 * curr_events bigger) and any difference in the amount of
4528 * in-flight syncio (making current_events bigger or smaller)
4529 * The amount in-flight is currently limited to
4530 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4531 * which is at most 4096 sectors.
4532 * These numbers are fairly fragile and should be made
4533 * more robust, probably by enforcing the
4534 * 'window size' that md_do_sync sort-of uses.
4535 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 * Note: the following is an unsigned comparison.
4537 */
NeilBrownc0e48522005-11-18 01:11:01 -08004538 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 rdev->last_events = curr_events;
4540 idle = 0;
4541 }
4542 }
4543 return idle;
4544}
4545
4546void md_done_sync(mddev_t *mddev, int blocks, int ok)
4547{
4548 /* another "blocks" (512byte) blocks have been synced */
4549 atomic_sub(blocks, &mddev->recovery_active);
4550 wake_up(&mddev->recovery_wait);
4551 if (!ok) {
4552 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4553 md_wakeup_thread(mddev->thread);
4554 // stop recovery, signal do_sync ....
4555 }
4556}
4557
4558
NeilBrown06d91a52005-06-21 17:17:12 -07004559/* md_write_start(mddev, bi)
4560 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004561 * in superblock) before writing, schedule a superblock update
4562 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07004563 */
NeilBrown3d310eb2005-06-21 17:17:26 -07004564void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
NeilBrown06d91a52005-06-21 17:17:12 -07004566 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07004567 return;
NeilBrown06d91a52005-06-21 17:17:12 -07004568
NeilBrownf91de922005-11-08 21:39:36 -08004569 BUG_ON(mddev->ro == 1);
4570 if (mddev->ro == 2) {
4571 /* need to switch to read/write */
4572 mddev->ro = 0;
4573 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4574 md_wakeup_thread(mddev->thread);
4575 }
NeilBrown06d91a52005-06-21 17:17:12 -07004576 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07004577 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08004578 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07004579 if (mddev->in_sync) {
4580 mddev->in_sync = 0;
4581 mddev->sb_dirty = 1;
4582 md_wakeup_thread(mddev->thread);
4583 }
NeilBrowna9701a32005-11-08 21:39:34 -08004584 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07004585 }
NeilBrown3d310eb2005-06-21 17:17:26 -07004586 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587}
4588
4589void md_write_end(mddev_t *mddev)
4590{
4591 if (atomic_dec_and_test(&mddev->writes_pending)) {
4592 if (mddev->safemode == 2)
4593 md_wakeup_thread(mddev->thread);
4594 else
4595 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4596 }
4597}
4598
Adrian Bunk75c96f82005-05-05 16:16:09 -07004599static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
4601#define SYNC_MARKS 10
4602#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08004603void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604{
4605 mddev_t *mddev2;
4606 unsigned int currspeed = 0,
4607 window;
NeilBrown57afd892005-06-21 17:17:13 -07004608 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 unsigned long mark[SYNC_MARKS];
4610 sector_t mark_cnt[SYNC_MARKS];
4611 int last_mark,m;
4612 struct list_head *tmp;
4613 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07004614 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
4616 /* just incase thread restarts... */
4617 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4618 return;
4619
4620 /* we overload curr_resync somewhat here.
4621 * 0 == not engaged in resync at all
4622 * 2 == checking that there is no conflict with another sync
4623 * 1 == like 2, but have yielded to allow conflicting resync to
4624 * commense
4625 * other == active in resync - this many blocks
4626 *
4627 * Before starting a resync we must have set curr_resync to
4628 * 2, and then checked that every "conflicting" array has curr_resync
4629 * less than ours. When we find one that is the same or higher
4630 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4631 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4632 * This will mean we have to start checking from the beginning again.
4633 *
4634 */
4635
4636 do {
4637 mddev->curr_resync = 2;
4638
4639 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08004640 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07004641 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 goto skip;
4643 }
4644 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 if (mddev2 == mddev)
4646 continue;
4647 if (mddev2->curr_resync &&
4648 match_mddev_units(mddev,mddev2)) {
4649 DEFINE_WAIT(wq);
4650 if (mddev < mddev2 && mddev->curr_resync == 2) {
4651 /* arbitrarily yield */
4652 mddev->curr_resync = 1;
4653 wake_up(&resync_wait);
4654 }
4655 if (mddev > mddev2 && mddev->curr_resync == 1)
4656 /* no need to wait here, we can wait the next
4657 * time 'round when curr_resync == 2
4658 */
4659 continue;
NeilBrown787453c2005-11-08 21:39:43 -08004660 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4661 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07004662 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 printk(KERN_INFO "md: delaying resync of %s"
4664 " until %s has finished resync (they"
4665 " share one or more physical units)\n",
4666 mdname(mddev), mdname(mddev2));
4667 mddev_put(mddev2);
4668 schedule();
4669 finish_wait(&resync_wait, &wq);
4670 goto try_again;
4671 }
4672 finish_wait(&resync_wait, &wq);
4673 }
4674 }
4675 } while (mddev->curr_resync < 2);
4676
NeilBrown9d888832005-11-08 21:39:26 -08004677 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004679 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 */
4681 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004682 mddev->resync_mismatches = 0;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004683 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4684 max_sectors = mddev->size << 1;
4685 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /* recovery follows the physical size of devices */
4687 max_sectors = mddev->size << 1;
4688
4689 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4690 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
NeilBrown88202a02006-01-06 00:21:36 -08004691 " %d KB/sec/disc.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07004692 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 "(but not more than %d KB/sec) for reconstruction.\n",
NeilBrown88202a02006-01-06 00:21:36 -08004694 speed_max(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
4696 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004697 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004698 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4699 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 j = mddev->recovery_cp;
4701 else
4702 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004703 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 for (m = 0; m < SYNC_MARKS; m++) {
4705 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004706 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 }
4708 last_mark = 0;
4709 mddev->resync_mark = mark[last_mark];
4710 mddev->resync_mark_cnt = mark_cnt[last_mark];
4711
4712 /*
4713 * Tune reconstruction:
4714 */
4715 window = 32*(PAGE_SIZE/512);
4716 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4717 window/2,(unsigned long long) max_sectors/2);
4718
4719 atomic_set(&mddev->recovery_active, 0);
4720 init_waitqueue_head(&mddev->recovery_wait);
4721 last_check = 0;
4722
4723 if (j>2) {
4724 printk(KERN_INFO
4725 "md: resuming recovery of %s from checkpoint.\n",
4726 mdname(mddev));
4727 mddev->curr_resync = j;
4728 }
4729
4730 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004731 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
NeilBrown57afd892005-06-21 17:17:13 -07004733 skipped = 0;
4734 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08004735 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07004736 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4738 goto out;
4739 }
NeilBrown57afd892005-06-21 17:17:13 -07004740
4741 if (!skipped) { /* actual IO requested */
4742 io_sectors += sectors;
4743 atomic_add(sectors, &mddev->recovery_active);
4744 }
4745
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 j += sectors;
4747 if (j>1) mddev->curr_resync = j;
NeilBrownd7603b72006-01-06 00:20:30 -08004748 if (last_check == 0)
4749 /* this is the earliers that rebuilt will be
4750 * visible in /proc/mdstat
4751 */
4752 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07004753
4754 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 continue;
4756
NeilBrown57afd892005-06-21 17:17:13 -07004757 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
4759 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4760 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4761 break;
4762
4763 repeat:
4764 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4765 /* step marks */
4766 int next = (last_mark+1) % SYNC_MARKS;
4767
4768 mddev->resync_mark = mark[next];
4769 mddev->resync_mark_cnt = mark_cnt[next];
4770 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004771 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 last_mark = next;
4773 }
4774
4775
NeilBrown787453c2005-11-08 21:39:43 -08004776 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 /*
4778 * got a signal, exit.
4779 */
4780 printk(KERN_INFO
4781 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4783 goto out;
4784 }
4785
4786 /*
4787 * this loop exits only if either when we are slower than
4788 * the 'hard' speed limit, or the system was IO-idle for
4789 * a jiffy.
4790 * the system might be non-idle CPU-wise, but we only care
4791 * about not overloading the IO subsystem. (things like an
4792 * e2fsck being done on the RAID array should execute fast)
4793 */
4794 mddev->queue->unplug_fn(mddev->queue);
4795 cond_resched();
4796
NeilBrown57afd892005-06-21 17:17:13 -07004797 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4798 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799
NeilBrown88202a02006-01-06 00:21:36 -08004800 if (currspeed > speed_min(mddev)) {
4801 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004803 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 goto repeat;
4805 }
4806 }
4807 }
4808 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4809 /*
4810 * this also signals 'finished resyncing' to md_stop
4811 */
4812 out:
4813 mddev->queue->unplug_fn(mddev->queue);
4814
4815 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4816
4817 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004818 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819
4820 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08004821 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
4822 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 mddev->curr_resync > 2 &&
4824 mddev->curr_resync >= mddev->recovery_cp) {
4825 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4826 printk(KERN_INFO
4827 "md: checkpointing recovery of %s.\n",
4828 mdname(mddev));
4829 mddev->recovery_cp = mddev->curr_resync;
4830 } else
4831 mddev->recovery_cp = MaxSector;
4832 }
4833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 skip:
4835 mddev->curr_resync = 0;
4836 wake_up(&resync_wait);
4837 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4838 md_wakeup_thread(mddev->thread);
4839}
NeilBrown29269552006-03-27 01:18:10 -08004840EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
4842
4843/*
4844 * This routine is regularly called by all per-raid-array threads to
4845 * deal with generic issues like resync and super-block update.
4846 * Raid personalities that don't have a thread (linear/raid0) do not
4847 * need this as they never do any recovery or update the superblock.
4848 *
4849 * It does not do any resync itself, but rather "forks" off other threads
4850 * to do that as needed.
4851 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4852 * "->recovery" and create a thread at ->sync_thread.
4853 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4854 * and wakeups up this thread which will reap the thread and finish up.
4855 * This thread also removes any faulty devices (with nr_pending == 0).
4856 *
4857 * The overall approach is:
4858 * 1/ if the superblock needs updating, update it.
4859 * 2/ If a recovery thread is running, don't do anything else.
4860 * 3/ If recovery has finished, clean up, possibly marking spares active.
4861 * 4/ If there are any faulty devices, remove them.
4862 * 5/ If array is degraded, try to add spares devices
4863 * 6/ If array has spares or is not in-sync, start a resync thread.
4864 */
4865void md_check_recovery(mddev_t *mddev)
4866{
4867 mdk_rdev_t *rdev;
4868 struct list_head *rtmp;
4869
4870
NeilBrown5f404022005-06-21 17:17:16 -07004871 if (mddev->bitmap)
4872 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873
4874 if (mddev->ro)
4875 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004876
4877 if (signal_pending(current)) {
4878 if (mddev->pers->sync_request) {
4879 printk(KERN_INFO "md: %s in immediate safe mode\n",
4880 mdname(mddev));
4881 mddev->safemode = 2;
4882 }
4883 flush_signals(current);
4884 }
4885
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 if ( ! (
4887 mddev->sb_dirty ||
4888 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004889 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4890 (mddev->safemode == 1) ||
4891 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4892 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 ))
4894 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004895
NeilBrowndf5b89b2006-03-27 01:18:20 -08004896 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004898
NeilBrowna9701a32005-11-08 21:39:34 -08004899 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004900 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4901 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4902 mddev->in_sync = 1;
4903 mddev->sb_dirty = 1;
4904 }
4905 if (mddev->safemode == 1)
4906 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004907 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004908
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 if (mddev->sb_dirty)
4910 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004911
NeilBrown06d91a52005-06-21 17:17:12 -07004912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4914 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4915 /* resync/recovery still happening */
4916 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4917 goto unlock;
4918 }
4919 if (mddev->sync_thread) {
4920 /* resync has finished, collect result */
4921 md_unregister_thread(mddev->sync_thread);
4922 mddev->sync_thread = NULL;
4923 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4924 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4925 /* success...*/
4926 /* activate any spares */
4927 mddev->pers->spare_active(mddev);
4928 }
4929 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004930
4931 /* if array is no-longer degraded, then any saved_raid_disk
4932 * information must be scrapped
4933 */
4934 if (!mddev->degraded)
4935 ITERATE_RDEV(mddev,rdev,rtmp)
4936 rdev->saved_raid_disk = -1;
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 mddev->recovery = 0;
4939 /* flag recovery needed just to double check */
4940 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08004941 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 goto unlock;
4943 }
NeilBrown24dd4692005-11-08 21:39:26 -08004944 /* Clear some bits that don't mean anything, but
4945 * might be left set
4946 */
4947 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4948 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
4949 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
4950 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952 /* no recovery is running.
4953 * remove any failed drives, then
4954 * add spares if possible.
4955 * Spare are also removed and re-added, to allow
4956 * the personality to fail the re-add.
4957 */
4958 ITERATE_RDEV(mddev,rdev,rtmp)
4959 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08004960 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004962 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
4963 char nm[20];
4964 sprintf(nm,"rd%d", rdev->raid_disk);
4965 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 }
4969
4970 if (mddev->degraded) {
4971 ITERATE_RDEV(mddev,rdev,rtmp)
4972 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08004973 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004974 if (mddev->pers->hot_add_disk(mddev,rdev)) {
4975 char nm[20];
4976 sprintf(nm, "rd%d", rdev->raid_disk);
4977 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08004979 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08004980 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 break;
4982 }
4983 }
4984
NeilBrown24dd4692005-11-08 21:39:26 -08004985 if (spares) {
4986 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4987 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4988 } else if (mddev->recovery_cp < MaxSector) {
4989 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4990 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4991 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08004993
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 if (mddev->pers->sync_request) {
4995 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07004996 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
4997 /* We are adding a device or devices to an array
4998 * which has the bitmap stored on all devices.
4999 * So make sure all bitmap pages get written
5000 */
5001 bitmap_write_all(mddev->bitmap);
5002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 mddev->sync_thread = md_register_thread(md_do_sync,
5004 mddev,
5005 "%s_resync");
5006 if (!mddev->sync_thread) {
5007 printk(KERN_ERR "%s: could not start resync"
5008 " thread...\n",
5009 mdname(mddev));
5010 /* leave the spares where they are, it shouldn't hurt */
5011 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005012 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005014 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 }
5016 unlock:
5017 mddev_unlock(mddev);
5018 }
5019}
5020
Adrian Bunk75c96f82005-05-05 16:16:09 -07005021static int md_notify_reboot(struct notifier_block *this,
5022 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
5024 struct list_head *tmp;
5025 mddev_t *mddev;
5026
5027 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5028
5029 printk(KERN_INFO "md: stopping all md devices.\n");
5030
5031 ITERATE_MDDEV(mddev,tmp)
NeilBrowndf5b89b2006-03-27 01:18:20 -08005032 if (mddev_trylock(mddev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 do_md_stop (mddev, 1);
5034 /*
5035 * certain more exotic SCSI devices are known to be
5036 * volatile wrt too early system reboots. While the
5037 * right place to handle this issue is the given
5038 * driver, we do want to have a safe RAID driver ...
5039 */
5040 mdelay(1000*1);
5041 }
5042 return NOTIFY_DONE;
5043}
5044
Adrian Bunk75c96f82005-05-05 16:16:09 -07005045static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 .notifier_call = md_notify_reboot,
5047 .next = NULL,
5048 .priority = INT_MAX, /* before any real devices */
5049};
5050
5051static void md_geninit(void)
5052{
5053 struct proc_dir_entry *p;
5054
5055 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5056
5057 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5058 if (p)
5059 p->proc_fops = &md_seq_fops;
5060}
5061
Adrian Bunk75c96f82005-05-05 16:16:09 -07005062static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
5064 int minor;
5065
5066 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5067 " MD_SB_DISKS=%d\n",
5068 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5069 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08005070 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07005071 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
5073 if (register_blkdev(MAJOR_NR, "md"))
5074 return -1;
5075 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5076 unregister_blkdev(MAJOR_NR, "md");
5077 return -1;
5078 }
5079 devfs_mk_dir("md");
5080 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5081 md_probe, NULL, NULL);
5082 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5083 md_probe, NULL, NULL);
5084
5085 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5086 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
5087 S_IFBLK|S_IRUSR|S_IWUSR,
5088 "md/%d", minor);
5089
5090 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5091 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
5092 S_IFBLK|S_IRUSR|S_IWUSR,
5093 "md/mdp%d", minor);
5094
5095
5096 register_reboot_notifier(&md_notifier);
5097 raid_table_header = register_sysctl_table(raid_root_table, 1);
5098
5099 md_geninit();
5100 return (0);
5101}
5102
5103
5104#ifndef MODULE
5105
5106/*
5107 * Searches all registered partitions for autorun RAID arrays
5108 * at boot time.
5109 */
5110static dev_t detected_devices[128];
5111static int dev_cnt;
5112
5113void md_autodetect_dev(dev_t dev)
5114{
5115 if (dev_cnt >= 0 && dev_cnt < 127)
5116 detected_devices[dev_cnt++] = dev;
5117}
5118
5119
5120static void autostart_arrays(int part)
5121{
5122 mdk_rdev_t *rdev;
5123 int i;
5124
5125 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5126
5127 for (i = 0; i < dev_cnt; i++) {
5128 dev_t dev = detected_devices[i];
5129
5130 rdev = md_import_device(dev,0, 0);
5131 if (IS_ERR(rdev))
5132 continue;
5133
NeilBrownb2d444d2005-11-08 21:39:31 -08005134 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 MD_BUG();
5136 continue;
5137 }
5138 list_add(&rdev->same_set, &pending_raid_disks);
5139 }
5140 dev_cnt = 0;
5141
5142 autorun_devices(part);
5143}
5144
5145#endif
5146
5147static __exit void md_exit(void)
5148{
5149 mddev_t *mddev;
5150 struct list_head *tmp;
5151 int i;
5152 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5153 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5154 for (i=0; i < MAX_MD_DEVS; i++)
5155 devfs_remove("md/%d", i);
5156 for (i=0; i < MAX_MD_DEVS; i++)
5157 devfs_remove("md/d%d", i);
5158
5159 devfs_remove("md");
5160
5161 unregister_blkdev(MAJOR_NR,"md");
5162 unregister_blkdev(mdp_major, "mdp");
5163 unregister_reboot_notifier(&md_notifier);
5164 unregister_sysctl_table(raid_table_header);
5165 remove_proc_entry("mdstat", NULL);
5166 ITERATE_MDDEV(mddev,tmp) {
5167 struct gendisk *disk = mddev->gendisk;
5168 if (!disk)
5169 continue;
5170 export_array(mddev);
5171 del_gendisk(disk);
5172 put_disk(disk);
5173 mddev->gendisk = NULL;
5174 mddev_put(mddev);
5175 }
5176}
5177
5178module_init(md_init)
5179module_exit(md_exit)
5180
NeilBrownf91de922005-11-08 21:39:36 -08005181static int get_ro(char *buffer, struct kernel_param *kp)
5182{
5183 return sprintf(buffer, "%d", start_readonly);
5184}
5185static int set_ro(const char *val, struct kernel_param *kp)
5186{
5187 char *e;
5188 int num = simple_strtoul(val, &e, 10);
5189 if (*val && (*e == '\0' || *e == '\n')) {
5190 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005191 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005192 }
5193 return -EINVAL;
5194}
5195
5196module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005197module_param(start_dirty_degraded, int, 0644);
5198
NeilBrownf91de922005-11-08 21:39:36 -08005199
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200EXPORT_SYMBOL(register_md_personality);
5201EXPORT_SYMBOL(unregister_md_personality);
5202EXPORT_SYMBOL(md_error);
5203EXPORT_SYMBOL(md_done_sync);
5204EXPORT_SYMBOL(md_write_start);
5205EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206EXPORT_SYMBOL(md_register_thread);
5207EXPORT_SYMBOL(md_unregister_thread);
5208EXPORT_SYMBOL(md_wakeup_thread);
5209EXPORT_SYMBOL(md_print_devices);
5210EXPORT_SYMBOL(md_check_recovery);
5211MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005212MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005213MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);