blob: a296edd7e1c34059d734752613fa05ba7aa6780f [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>
NeilBrown16f17b32006-06-26 00:27:37 -070047#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <linux/init.h>
50
NeilBrown32a76272005-06-21 17:17:14 -070051#include <linux/file.h>
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#ifdef CONFIG_KMOD
54#include <linux/kmod.h>
55#endif
56
57#include <asm/unaligned.h>
58
59#define MAJOR_NR MD_MAJOR
60#define MD_DRIVER
61
62/* 63 partitions with the alternate major number (mdp) */
63#define MdpMinorShift 6
64
65#define DEBUG 0
66#define dprintk(x...) ((void)(DEBUG && printk(x)))
67
68
69#ifndef MODULE
70static void autostart_arrays (int part);
71#endif
72
NeilBrown2604b702006-01-06 00:20:36 -080073static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static DEFINE_SPINLOCK(pers_lock);
75
76/*
77 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
78 * is 1000 KB/sec, so the extra system load does not show up that much.
79 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070080 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * subsystem is idle. There is also an 'absolute maximum' reconstruction
82 * speed limit - in case reconstruction slows down your system despite
83 * idle IO detection.
84 *
85 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080086 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88
89static int sysctl_speed_limit_min = 1000;
90static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080091static inline int speed_min(mddev_t *mddev)
92{
93 return mddev->sync_speed_min ?
94 mddev->sync_speed_min : sysctl_speed_limit_min;
95}
96
97static inline int speed_max(mddev_t *mddev)
98{
99 return mddev->sync_speed_max ?
100 mddev->sync_speed_max : sysctl_speed_limit_max;
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static struct ctl_table_header *raid_table_header;
104
105static ctl_table raid_table[] = {
106 {
107 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
108 .procname = "speed_limit_min",
109 .data = &sysctl_speed_limit_min,
110 .maxlen = sizeof(int),
111 .mode = 0644,
112 .proc_handler = &proc_dointvec,
113 },
114 {
115 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
116 .procname = "speed_limit_max",
117 .data = &sysctl_speed_limit_max,
118 .maxlen = sizeof(int),
119 .mode = 0644,
120 .proc_handler = &proc_dointvec,
121 },
122 { .ctl_name = 0 }
123};
124
125static ctl_table raid_dir_table[] = {
126 {
127 .ctl_name = DEV_RAID,
128 .procname = "raid",
129 .maxlen = 0,
130 .mode = 0555,
131 .child = raid_table,
132 },
133 { .ctl_name = 0 }
134};
135
136static ctl_table raid_root_table[] = {
137 {
138 .ctl_name = CTL_DEV,
139 .procname = "dev",
140 .maxlen = 0,
141 .mode = 0555,
142 .child = raid_dir_table,
143 },
144 { .ctl_name = 0 }
145};
146
147static struct block_device_operations md_fops;
148
NeilBrownf91de922005-11-08 21:39:36 -0800149static int start_readonly;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/*
NeilBrownd7603b72006-01-06 00:20:30 -0800152 * We have a system wide 'event count' that is incremented
153 * on any 'interesting' event, and readers of /proc/mdstat
154 * can use 'poll' or 'select' to find out when the event
155 * count increases.
156 *
157 * Events are:
158 * start array, stop array, error, add device, remove device,
159 * start build, activate spare
160 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800161static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800162static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800163void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800164{
165 atomic_inc(&md_event_count);
166 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100167 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800168}
NeilBrown29269552006-03-27 01:18:10 -0800169EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800170
NeilBrownc331eb02006-05-30 21:27:13 -0700171/* Alternate version that can be called from interrupts
172 * when calling sysfs_notify isn't needed.
173 */
174void md_new_event_inintr(mddev_t *mddev)
175{
176 atomic_inc(&md_event_count);
177 wake_up(&md_event_waiters);
178}
179
NeilBrownd7603b72006-01-06 00:20:30 -0800180/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 * Enables to iterate over all existing md arrays
182 * all_mddevs_lock protects this list.
183 */
184static LIST_HEAD(all_mddevs);
185static DEFINE_SPINLOCK(all_mddevs_lock);
186
187
188/*
189 * iterates through all used mddevs in the system.
190 * We take care to grab the all_mddevs_lock whenever navigating
191 * the list, and to always hold a refcount when unlocked.
192 * Any code which breaks out of this loop while own
193 * a reference to the current mddev and must mddev_put it.
194 */
195#define ITERATE_MDDEV(mddev,tmp) \
196 \
197 for (({ spin_lock(&all_mddevs_lock); \
198 tmp = all_mddevs.next; \
199 mddev = NULL;}); \
200 ({ if (tmp != &all_mddevs) \
201 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
202 spin_unlock(&all_mddevs_lock); \
203 if (mddev) mddev_put(mddev); \
204 mddev = list_entry(tmp, mddev_t, all_mddevs); \
205 tmp != &all_mddevs;}); \
206 ({ spin_lock(&all_mddevs_lock); \
207 tmp = tmp->next;}) \
208 )
209
210
211static int md_fail_request (request_queue_t *q, struct bio *bio)
212{
213 bio_io_error(bio, bio->bi_size);
214 return 0;
215}
216
217static inline mddev_t *mddev_get(mddev_t *mddev)
218{
219 atomic_inc(&mddev->active);
220 return mddev;
221}
222
223static void mddev_put(mddev_t *mddev)
224{
225 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
226 return;
227 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
228 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800229 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500230 blk_cleanup_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800231 kobject_unregister(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800232 } else
233 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static mddev_t * mddev_find(dev_t unit)
237{
238 mddev_t *mddev, *new = NULL;
239
240 retry:
241 spin_lock(&all_mddevs_lock);
242 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
243 if (mddev->unit == unit) {
244 mddev_get(mddev);
245 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700246 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return mddev;
248 }
249
250 if (new) {
251 list_add(&new->all_mddevs, &all_mddevs);
252 spin_unlock(&all_mddevs_lock);
253 return new;
254 }
255 spin_unlock(&all_mddevs_lock);
256
NeilBrown9ffae0c2006-01-06 00:20:32 -0800257 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!new)
259 return NULL;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 new->unit = unit;
262 if (MAJOR(unit) == MD_MAJOR)
263 new->md_minor = MINOR(unit);
264 else
265 new->md_minor = MINOR(unit) >> MdpMinorShift;
266
NeilBrowndf5b89b2006-03-27 01:18:20 -0800267 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 INIT_LIST_HEAD(&new->disks);
269 INIT_LIST_HEAD(&new->all_mddevs);
270 init_timer(&new->safemode_timer);
271 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700272 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700273 init_waitqueue_head(&new->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 new->queue = blk_alloc_queue(GFP_KERNEL);
276 if (!new->queue) {
277 kfree(new);
278 return NULL;
279 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800280 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 blk_queue_make_request(new->queue, md_fail_request);
283
284 goto retry;
285}
286
287static inline int mddev_lock(mddev_t * mddev)
288{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800289 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static inline int mddev_trylock(mddev_t * mddev)
293{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800294 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297static inline void mddev_unlock(mddev_t * mddev)
298{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800299 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
NeilBrown005eca52005-08-22 13:11:08 -0700301 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
NeilBrown2989ddb2006-01-06 00:20:43 -0800304static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 mdk_rdev_t * rdev;
307 struct list_head *tmp;
308
309 ITERATE_RDEV(mddev,rdev,tmp) {
310 if (rdev->desc_nr == nr)
311 return rdev;
312 }
313 return NULL;
314}
315
316static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
317{
318 struct list_head *tmp;
319 mdk_rdev_t *rdev;
320
321 ITERATE_RDEV(mddev,rdev,tmp) {
322 if (rdev->bdev->bd_dev == dev)
323 return rdev;
324 }
325 return NULL;
326}
327
NeilBrownd9d166c2006-01-06 00:20:51 -0800328static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800329{
330 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800331 list_for_each_entry(pers, &pers_list, list) {
332 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800333 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800334 if (strcmp(pers->name, clevel)==0)
335 return pers;
336 }
NeilBrown2604b702006-01-06 00:20:36 -0800337 return NULL;
338}
339
Jesper Juhl77933d72005-07-27 11:46:09 -0700340static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
343 return MD_NEW_SIZE_BLOCKS(size);
344}
345
346static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
347{
348 sector_t size;
349
350 size = rdev->sb_offset;
351
352 if (chunk_size)
353 size &= ~((sector_t)chunk_size/1024 - 1);
354 return size;
355}
356
357static int alloc_disk_sb(mdk_rdev_t * rdev)
358{
359 if (rdev->sb_page)
360 MD_BUG();
361
362 rdev->sb_page = alloc_page(GFP_KERNEL);
363 if (!rdev->sb_page) {
364 printk(KERN_ALERT "md: out of memory.\n");
365 return -EINVAL;
366 }
367
368 return 0;
369}
370
371static void free_disk_sb(mdk_rdev_t * rdev)
372{
373 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800374 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rdev->sb_loaded = 0;
376 rdev->sb_page = NULL;
377 rdev->sb_offset = 0;
378 rdev->size = 0;
379 }
380}
381
382
NeilBrown7bfa19f2005-06-21 17:17:28 -0700383static int super_written(struct bio *bio, unsigned int bytes_done, int error)
384{
385 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800386 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700387 if (bio->bi_size)
388 return 1;
389
390 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags))
NeilBrowna9701a32005-11-08 21:39:34 -0800391 md_error(mddev, rdev);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700392
NeilBrowna9701a32005-11-08 21:39:34 -0800393 if (atomic_dec_and_test(&mddev->pending_writes))
394 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700395 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700396 return 0;
397}
398
NeilBrowna9701a32005-11-08 21:39:34 -0800399static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
400{
401 struct bio *bio2 = bio->bi_private;
402 mdk_rdev_t *rdev = bio2->bi_private;
403 mddev_t *mddev = rdev->mddev;
404 if (bio->bi_size)
405 return 1;
406
407 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
408 error == -EOPNOTSUPP) {
409 unsigned long flags;
410 /* barriers don't appear to be supported :-( */
411 set_bit(BarriersNotsupp, &rdev->flags);
412 mddev->barriers_work = 0;
413 spin_lock_irqsave(&mddev->write_lock, flags);
414 bio2->bi_next = mddev->biolist;
415 mddev->biolist = bio2;
416 spin_unlock_irqrestore(&mddev->write_lock, flags);
417 wake_up(&mddev->sb_wait);
418 bio_put(bio);
419 return 0;
420 }
421 bio_put(bio2);
422 bio->bi_private = rdev;
423 return super_written(bio, bytes_done, error);
424}
425
NeilBrown7bfa19f2005-06-21 17:17:28 -0700426void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
427 sector_t sector, int size, struct page *page)
428{
429 /* write first size bytes of page to sector of rdev
430 * Increment mddev->pending_writes before returning
431 * and decrement it on completion, waking up sb_wait
432 * if zero is reached.
433 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800434 *
435 * As we might need to resubmit the request if BIO_RW_BARRIER
436 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700437 */
438 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800439 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700440
441 bio->bi_bdev = rdev->bdev;
442 bio->bi_sector = sector;
443 bio_add_page(bio, page, size, 0);
444 bio->bi_private = rdev;
445 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800446 bio->bi_rw = rw;
447
NeilBrown7bfa19f2005-06-21 17:17:28 -0700448 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800449 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
450 struct bio *rbio;
451 rw |= (1<<BIO_RW_BARRIER);
452 rbio = bio_clone(bio, GFP_NOIO);
453 rbio->bi_private = bio;
454 rbio->bi_end_io = super_written_barrier;
455 submit_bio(rw, rbio);
456 } else
457 submit_bio(rw, bio);
458}
459
460void md_super_wait(mddev_t *mddev)
461{
462 /* wait for all superblock writes that were scheduled to complete.
463 * if any had to be retried (due to BARRIER problems), retry them
464 */
465 DEFINE_WAIT(wq);
466 for(;;) {
467 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
468 if (atomic_read(&mddev->pending_writes)==0)
469 break;
470 while (mddev->biolist) {
471 struct bio *bio;
472 spin_lock_irq(&mddev->write_lock);
473 bio = mddev->biolist;
474 mddev->biolist = bio->bi_next ;
475 bio->bi_next = NULL;
476 spin_unlock_irq(&mddev->write_lock);
477 submit_bio(bio->bi_rw, bio);
478 }
479 schedule();
480 }
481 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
485{
486 if (bio->bi_size)
487 return 1;
488
489 complete((struct completion*)bio->bi_private);
490 return 0;
491}
492
NeilBrowna654b9d82005-06-21 17:17:27 -0700493int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 struct page *page, int rw)
495{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700496 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct completion event;
498 int ret;
499
500 rw |= (1 << BIO_RW_SYNC);
501
502 bio->bi_bdev = bdev;
503 bio->bi_sector = sector;
504 bio_add_page(bio, page, size, 0);
505 init_completion(&event);
506 bio->bi_private = &event;
507 bio->bi_end_io = bi_complete;
508 submit_bio(rw, bio);
509 wait_for_completion(&event);
510
511 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
512 bio_put(bio);
513 return ret;
514}
NeilBrowna8745db2006-01-06 00:20:34 -0800515EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
NeilBrown0002b272005-09-09 16:23:53 -0700517static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 char b[BDEVNAME_SIZE];
520 if (!rdev->sb_page) {
521 MD_BUG();
522 return -EINVAL;
523 }
524 if (rdev->sb_loaded)
525 return 0;
526
527
NeilBrown0002b272005-09-09 16:23:53 -0700528 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto fail;
530 rdev->sb_loaded = 1;
531 return 0;
532
533fail:
534 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
535 bdevname(rdev->bdev,b));
536 return -EINVAL;
537}
538
539static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
540{
541 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
542 (sb1->set_uuid1 == sb2->set_uuid1) &&
543 (sb1->set_uuid2 == sb2->set_uuid2) &&
544 (sb1->set_uuid3 == sb2->set_uuid3))
545
546 return 1;
547
548 return 0;
549}
550
551
552static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
553{
554 int ret;
555 mdp_super_t *tmp1, *tmp2;
556
557 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
558 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
559
560 if (!tmp1 || !tmp2) {
561 ret = 0;
562 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
563 goto abort;
564 }
565
566 *tmp1 = *sb1;
567 *tmp2 = *sb2;
568
569 /*
570 * nr_disks is not constant
571 */
572 tmp1->nr_disks = 0;
573 tmp2->nr_disks = 0;
574
575 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
576 ret = 0;
577 else
578 ret = 1;
579
580abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700581 kfree(tmp1);
582 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return ret;
584}
585
586static unsigned int calc_sb_csum(mdp_super_t * sb)
587{
588 unsigned int disk_csum, csum;
589
590 disk_csum = sb->sb_csum;
591 sb->sb_csum = 0;
592 csum = csum_partial((void *)sb, MD_SB_BYTES, 0);
593 sb->sb_csum = disk_csum;
594 return csum;
595}
596
597
598/*
599 * Handle superblock details.
600 * We want to be able to handle multiple superblock formats
601 * so we have a common interface to them all, and an array of
602 * different handlers.
603 * We rely on user-space to write the initial superblock, and support
604 * reading and updating of superblocks.
605 * Interface methods are:
606 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
607 * loads and validates a superblock on dev.
608 * if refdev != NULL, compare superblocks on both devices
609 * Return:
610 * 0 - dev has a superblock that is compatible with refdev
611 * 1 - dev has a superblock that is compatible and newer than refdev
612 * so dev should be used as the refdev in future
613 * -EINVAL superblock incompatible or invalid
614 * -othererror e.g. -EIO
615 *
616 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
617 * Verify that dev is acceptable into mddev.
618 * The first time, mddev->raid_disks will be 0, and data from
619 * dev should be merged in. Subsequent calls check that dev
620 * is new enough. Return 0 or -EINVAL
621 *
622 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
623 * Update the superblock for rdev with data in mddev
624 * This does not write to disc.
625 *
626 */
627
628struct super_type {
629 char *name;
630 struct module *owner;
631 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
632 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
633 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
634};
635
636/*
637 * load_super for 0.90.0
638 */
639static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
640{
641 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
642 mdp_super_t *sb;
643 int ret;
644 sector_t sb_offset;
645
646 /*
647 * Calculate the position of the superblock,
648 * it's at the end of the disk.
649 *
650 * It also happens to be a multiple of 4Kb.
651 */
652 sb_offset = calc_dev_sboffset(rdev->bdev);
653 rdev->sb_offset = sb_offset;
654
NeilBrown0002b272005-09-09 16:23:53 -0700655 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (ret) return ret;
657
658 ret = -EINVAL;
659
660 bdevname(rdev->bdev, b);
661 sb = (mdp_super_t*)page_address(rdev->sb_page);
662
663 if (sb->md_magic != MD_SB_MAGIC) {
664 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
665 b);
666 goto abort;
667 }
668
669 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800670 sb->minor_version < 90 ||
671 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
673 sb->major_version, sb->minor_version,
674 b);
675 goto abort;
676 }
677
678 if (sb->raid_disks <= 0)
679 goto abort;
680
681 if (csum_fold(calc_sb_csum(sb)) != csum_fold(sb->sb_csum)) {
682 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
683 b);
684 goto abort;
685 }
686
687 rdev->preferred_minor = sb->md_minor;
688 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700689 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (sb->level == LEVEL_MULTIPATH)
692 rdev->desc_nr = -1;
693 else
694 rdev->desc_nr = sb->this_disk.number;
695
696 if (refdev == 0)
697 ret = 1;
698 else {
699 __u64 ev1, ev2;
700 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
701 if (!uuid_equal(refsb, sb)) {
702 printk(KERN_WARNING "md: %s has different UUID to %s\n",
703 b, bdevname(refdev->bdev,b2));
704 goto abort;
705 }
706 if (!sb_equal(refsb, sb)) {
707 printk(KERN_WARNING "md: %s has same UUID"
708 " but different superblock to %s\n",
709 b, bdevname(refdev->bdev, b2));
710 goto abort;
711 }
712 ev1 = md_event(sb);
713 ev2 = md_event(refsb);
714 if (ev1 > ev2)
715 ret = 1;
716 else
717 ret = 0;
718 }
719 rdev->size = calc_dev_size(rdev, sb->chunk_size);
720
NeilBrown2bf071b2006-01-06 00:20:55 -0800721 if (rdev->size < sb->size && sb->level > 1)
722 /* "this cannot possibly happen" ... */
723 ret = -EINVAL;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 abort:
726 return ret;
727}
728
729/*
730 * validate_super for 0.90.0
731 */
732static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
733{
734 mdp_disk_t *desc;
735 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
736
NeilBrown41158c72005-06-21 17:17:25 -0700737 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800738 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (mddev->raid_disks == 0) {
740 mddev->major_version = 0;
741 mddev->minor_version = sb->minor_version;
742 mddev->patch_version = sb->patch_version;
743 mddev->persistent = ! sb->not_persistent;
744 mddev->chunk_size = sb->chunk_size;
745 mddev->ctime = sb->ctime;
746 mddev->utime = sb->utime;
747 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800748 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 mddev->layout = sb->layout;
750 mddev->raid_disks = sb->raid_disks;
751 mddev->size = sb->size;
752 mddev->events = md_event(sb);
NeilBrown92232142005-08-18 11:24:16 -0700753 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700754 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
NeilBrownf6705572006-03-27 01:18:11 -0800756 if (mddev->minor_version >= 91) {
757 mddev->reshape_position = sb->reshape_position;
758 mddev->delta_disks = sb->delta_disks;
759 mddev->new_level = sb->new_level;
760 mddev->new_layout = sb->new_layout;
761 mddev->new_chunk = sb->new_chunk;
762 } else {
763 mddev->reshape_position = MaxSector;
764 mddev->delta_disks = 0;
765 mddev->new_level = mddev->level;
766 mddev->new_layout = mddev->layout;
767 mddev->new_chunk = mddev->chunk_size;
768 }
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (sb->state & (1<<MD_SB_CLEAN))
771 mddev->recovery_cp = MaxSector;
772 else {
773 if (sb->events_hi == sb->cp_events_hi &&
774 sb->events_lo == sb->cp_events_lo) {
775 mddev->recovery_cp = sb->recovery_cp;
776 } else
777 mddev->recovery_cp = 0;
778 }
779
780 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
781 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
782 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
783 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
784
785 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700786
787 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
788 mddev->bitmap_file == NULL) {
NeilBrownc5a10f62006-03-27 01:18:03 -0800789 if (mddev->level != 1 && mddev->level != 4
790 && mddev->level != 5 && mddev->level != 6
NeilBrown6cce3b22006-01-06 00:20:16 -0800791 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700792 /* FIXME use a better test */
NeilBrown6cce3b22006-01-06 00:20:16 -0800793 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -0700794 return -EINVAL;
795 }
NeilBrown36fa3062005-09-09 16:23:45 -0700796 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700797 }
798
NeilBrown41158c72005-06-21 17:17:25 -0700799 } else if (mddev->pers == NULL) {
800 /* Insist on good event counter while assembling */
801 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 ++ev1;
803 if (ev1 < mddev->events)
804 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700805 } else if (mddev->bitmap) {
806 /* if adding to array with a bitmap, then we can accept an
807 * older device ... but not too old.
808 */
809 __u64 ev1 = md_event(sb);
810 if (ev1 < mddev->bitmap->events_cleared)
811 return 0;
812 } else /* just a hot-add of a new device, leave raid_disk at -1 */
813 return 0;
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 desc = sb->disks + rdev->desc_nr;
817
818 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800819 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 else if (desc->state & (1<<MD_DISK_SYNC) &&
821 desc->raid_disk < mddev->raid_disks) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800822 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 rdev->raid_disk = desc->raid_disk;
824 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700825 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
826 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700827 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800828 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return 0;
830}
831
832/*
833 * sync_super for 0.90.0
834 */
835static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
836{
837 mdp_super_t *sb;
838 struct list_head *tmp;
839 mdk_rdev_t *rdev2;
840 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 /* make rdev->sb match mddev data..
844 *
845 * 1/ zero out disks
846 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
847 * 3/ any empty disks < next_spare become removed
848 *
849 * disks[0] gets initialised to REMOVED because
850 * we cannot be sure from other fields if it has
851 * been initialised or not.
852 */
853 int i;
854 int active=0, working=0,failed=0,spare=0,nr_disks=0;
855
NeilBrown61181562005-09-09 16:24:02 -0700856 rdev->sb_size = MD_SB_BYTES;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 sb = (mdp_super_t*)page_address(rdev->sb_page);
859
860 memset(sb, 0, sizeof(*sb));
861
862 sb->md_magic = MD_SB_MAGIC;
863 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 sb->patch_version = mddev->patch_version;
865 sb->gvalid_words = 0; /* ignored */
866 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
867 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
868 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
869 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
870
871 sb->ctime = mddev->ctime;
872 sb->level = mddev->level;
873 sb->size = mddev->size;
874 sb->raid_disks = mddev->raid_disks;
875 sb->md_minor = mddev->md_minor;
876 sb->not_persistent = !mddev->persistent;
877 sb->utime = mddev->utime;
878 sb->state = 0;
879 sb->events_hi = (mddev->events>>32);
880 sb->events_lo = (u32)mddev->events;
881
NeilBrownf6705572006-03-27 01:18:11 -0800882 if (mddev->reshape_position == MaxSector)
883 sb->minor_version = 90;
884 else {
885 sb->minor_version = 91;
886 sb->reshape_position = mddev->reshape_position;
887 sb->new_level = mddev->new_level;
888 sb->delta_disks = mddev->delta_disks;
889 sb->new_layout = mddev->new_layout;
890 sb->new_chunk = mddev->new_chunk;
891 }
892 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (mddev->in_sync)
894 {
895 sb->recovery_cp = mddev->recovery_cp;
896 sb->cp_events_hi = (mddev->events>>32);
897 sb->cp_events_lo = (u32)mddev->events;
898 if (mddev->recovery_cp == MaxSector)
899 sb->state = (1<< MD_SB_CLEAN);
900 } else
901 sb->recovery_cp = 0;
902
903 sb->layout = mddev->layout;
904 sb->chunk_size = mddev->chunk_size;
905
NeilBrowna654b9d82005-06-21 17:17:27 -0700906 if (mddev->bitmap && mddev->bitmap_file == NULL)
907 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 sb->disks[0].state = (1<<MD_DISK_REMOVED);
910 ITERATE_RDEV(mddev,rdev2,tmp) {
911 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800912 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800913 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
914 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800915 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800917 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800918 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 d = &sb->disks[rdev2->desc_nr];
920 nr_disks++;
921 d->number = rdev2->desc_nr;
922 d->major = MAJOR(rdev2->bdev->bd_dev);
923 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800924 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
925 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 d->raid_disk = rdev2->raid_disk;
927 else
928 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800929 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800931 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 d->state = (1<<MD_DISK_ACTIVE);
933 d->state |= (1<<MD_DISK_SYNC);
934 active++;
935 working++;
936 } else {
937 d->state = 0;
938 spare++;
939 working++;
940 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700941 if (test_bit(WriteMostly, &rdev2->flags))
942 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 /* now set the "removed" and "faulty" bits on any missing devices */
945 for (i=0 ; i < mddev->raid_disks ; i++) {
946 mdp_disk_t *d = &sb->disks[i];
947 if (d->state == 0 && d->number == 0) {
948 d->number = i;
949 d->raid_disk = i;
950 d->state = (1<<MD_DISK_REMOVED);
951 d->state |= (1<<MD_DISK_FAULTY);
952 failed++;
953 }
954 }
955 sb->nr_disks = nr_disks;
956 sb->active_disks = active;
957 sb->working_disks = working;
958 sb->failed_disks = failed;
959 sb->spare_disks = spare;
960
961 sb->this_disk = sb->disks[rdev->desc_nr];
962 sb->sb_csum = calc_sb_csum(sb);
963}
964
965/*
966 * version 1 superblock
967 */
968
969static unsigned int calc_sb_1_csum(struct mdp_superblock_1 * sb)
970{
971 unsigned int disk_csum, csum;
972 unsigned long long newcsum;
973 int size = 256 + le32_to_cpu(sb->max_dev)*2;
974 unsigned int *isuper = (unsigned int*)sb;
975 int i;
976
977 disk_csum = sb->sb_csum;
978 sb->sb_csum = 0;
979 newcsum = 0;
980 for (i=0; size>=4; size -= 4 )
981 newcsum += le32_to_cpu(*isuper++);
982
983 if (size == 2)
984 newcsum += le16_to_cpu(*(unsigned short*) isuper);
985
986 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
987 sb->sb_csum = disk_csum;
988 return cpu_to_le32(csum);
989}
990
991static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
992{
993 struct mdp_superblock_1 *sb;
994 int ret;
995 sector_t sb_offset;
996 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -0700997 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 /*
1000 * Calculate the position of the superblock.
1001 * It is always aligned to a 4K boundary and
1002 * depeding on minor_version, it can be:
1003 * 0: At least 8K, but less than 12K, from end of device
1004 * 1: At start of device
1005 * 2: 4K from start of device.
1006 */
1007 switch(minor_version) {
1008 case 0:
1009 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1010 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001011 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /* convert from sectors to K */
1013 sb_offset /= 2;
1014 break;
1015 case 1:
1016 sb_offset = 0;
1017 break;
1018 case 2:
1019 sb_offset = 4;
1020 break;
1021 default:
1022 return -EINVAL;
1023 }
1024 rdev->sb_offset = sb_offset;
1025
NeilBrown0002b272005-09-09 16:23:53 -07001026 /* superblock is rarely larger than 1K, but it can be larger,
1027 * and it is safe to read 4k, so we do that
1028 */
1029 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (ret) return ret;
1031
1032
1033 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1034
1035 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1036 sb->major_version != cpu_to_le32(1) ||
1037 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1038 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001039 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return -EINVAL;
1041
1042 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1043 printk("md: invalid superblock checksum on %s\n",
1044 bdevname(rdev->bdev,b));
1045 return -EINVAL;
1046 }
1047 if (le64_to_cpu(sb->data_size) < 10) {
1048 printk("md: data_size too small on %s\n",
1049 bdevname(rdev->bdev,b));
1050 return -EINVAL;
1051 }
1052 rdev->preferred_minor = 0xffff;
1053 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001054 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
NeilBrown0002b272005-09-09 16:23:53 -07001056 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001057 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001058 if (rdev->sb_size & bmask)
1059 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001062 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 else {
1064 __u64 ev1, ev2;
1065 struct mdp_superblock_1 *refsb =
1066 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1067
1068 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1069 sb->level != refsb->level ||
1070 sb->layout != refsb->layout ||
1071 sb->chunksize != refsb->chunksize) {
1072 printk(KERN_WARNING "md: %s has strangely different"
1073 " superblock to %s\n",
1074 bdevname(rdev->bdev,b),
1075 bdevname(refdev->bdev,b2));
1076 return -EINVAL;
1077 }
1078 ev1 = le64_to_cpu(sb->events);
1079 ev2 = le64_to_cpu(refsb->events);
1080
1081 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001082 ret = 1;
1083 else
1084 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086 if (minor_version)
1087 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1088 else
1089 rdev->size = rdev->sb_offset;
1090 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1091 return -EINVAL;
1092 rdev->size = le64_to_cpu(sb->data_size)/2;
1093 if (le32_to_cpu(sb->chunksize))
1094 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001095
1096 if (le32_to_cpu(sb->size) > rdev->size*2)
1097 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001098 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1102{
1103 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1104
NeilBrown41158c72005-06-21 17:17:25 -07001105 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001106 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (mddev->raid_disks == 0) {
1108 mddev->major_version = 1;
1109 mddev->patch_version = 0;
1110 mddev->persistent = 1;
1111 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1112 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1113 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1114 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001115 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 mddev->layout = le32_to_cpu(sb->layout);
1117 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1118 mddev->size = le64_to_cpu(sb->size)/2;
1119 mddev->events = le64_to_cpu(sb->events);
NeilBrown92232142005-08-18 11:24:16 -07001120 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001121 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1124 memcpy(mddev->uuid, sb->set_uuid, 16);
1125
1126 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001127
NeilBrown71c08052005-09-09 16:23:51 -07001128 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowna654b9d82005-06-21 17:17:27 -07001129 mddev->bitmap_file == NULL ) {
NeilBrown6cce3b22006-01-06 00:20:16 -08001130 if (mddev->level != 1 && mddev->level != 5 && mddev->level != 6
1131 && mddev->level != 10) {
1132 printk(KERN_WARNING "md: bitmaps not supported for this level.\n");
NeilBrowna654b9d82005-06-21 17:17:27 -07001133 return -EINVAL;
1134 }
1135 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
1136 }
NeilBrownf6705572006-03-27 01:18:11 -08001137 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1138 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1139 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1140 mddev->new_level = le32_to_cpu(sb->new_level);
1141 mddev->new_layout = le32_to_cpu(sb->new_layout);
1142 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1143 } else {
1144 mddev->reshape_position = MaxSector;
1145 mddev->delta_disks = 0;
1146 mddev->new_level = mddev->level;
1147 mddev->new_layout = mddev->layout;
1148 mddev->new_chunk = mddev->chunk_size;
1149 }
1150
NeilBrown41158c72005-06-21 17:17:25 -07001151 } else if (mddev->pers == NULL) {
1152 /* Insist of good event counter while assembling */
1153 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 ++ev1;
1155 if (ev1 < mddev->events)
1156 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001157 } else if (mddev->bitmap) {
1158 /* If adding to array with a bitmap, then we can accept an
1159 * older device, but not too old.
1160 */
1161 __u64 ev1 = le64_to_cpu(sb->events);
1162 if (ev1 < mddev->bitmap->events_cleared)
1163 return 0;
1164 } else /* just a hot-add of a new device, leave raid_disk at -1 */
1165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 if (mddev->level != LEVEL_MULTIPATH) {
1168 int role;
1169 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1170 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1171 switch(role) {
1172 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001175 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 break;
1177 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001178 if ((le32_to_cpu(sb->feature_map) &
1179 MD_FEATURE_RECOVERY_OFFSET))
1180 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1181 else
1182 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 rdev->raid_disk = role;
1184 break;
1185 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001186 if (sb->devflags & WriteMostly1)
1187 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001188 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001189 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return 0;
1192}
1193
1194static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1195{
1196 struct mdp_superblock_1 *sb;
1197 struct list_head *tmp;
1198 mdk_rdev_t *rdev2;
1199 int max_dev, i;
1200 /* make rdev->sb match mddev and rdev data. */
1201
1202 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1203
1204 sb->feature_map = 0;
1205 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001206 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 memset(sb->pad1, 0, sizeof(sb->pad1));
1208 memset(sb->pad2, 0, sizeof(sb->pad2));
1209 memset(sb->pad3, 0, sizeof(sb->pad3));
1210
1211 sb->utime = cpu_to_le64((__u64)mddev->utime);
1212 sb->events = cpu_to_le64(mddev->events);
1213 if (mddev->in_sync)
1214 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1215 else
1216 sb->resync_offset = cpu_to_le64(0);
1217
NeilBrown4dbcdc72006-01-06 00:20:52 -08001218 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1219
NeilBrownf0ca3402006-02-02 14:28:04 -08001220 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001221 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001222
NeilBrowna654b9d82005-06-21 17:17:27 -07001223 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1224 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001225 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001226 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001227
1228 if (rdev->raid_disk >= 0 &&
1229 !test_bit(In_sync, &rdev->flags) &&
1230 rdev->recovery_offset > 0) {
1231 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1232 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1233 }
1234
NeilBrownf6705572006-03-27 01:18:11 -08001235 if (mddev->reshape_position != MaxSector) {
1236 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1237 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1238 sb->new_layout = cpu_to_le32(mddev->new_layout);
1239 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1240 sb->new_level = cpu_to_le32(mddev->new_level);
1241 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1242 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 max_dev = 0;
1245 ITERATE_RDEV(mddev,rdev2,tmp)
1246 if (rdev2->desc_nr+1 > max_dev)
1247 max_dev = rdev2->desc_nr+1;
1248
1249 sb->max_dev = cpu_to_le32(max_dev);
1250 for (i=0; i<max_dev;i++)
1251 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1252
1253 ITERATE_RDEV(mddev,rdev2,tmp) {
1254 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001255 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001257 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001259 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1260 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 else
1262 sb->dev_roles[i] = cpu_to_le16(0xffff);
1263 }
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 sb->sb_csum = calc_sb_1_csum(sb);
1266}
1267
1268
Adrian Bunk75c96f82005-05-05 16:16:09 -07001269static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 [0] = {
1271 .name = "0.90.0",
1272 .owner = THIS_MODULE,
1273 .load_super = super_90_load,
1274 .validate_super = super_90_validate,
1275 .sync_super = super_90_sync,
1276 },
1277 [1] = {
1278 .name = "md-1",
1279 .owner = THIS_MODULE,
1280 .load_super = super_1_load,
1281 .validate_super = super_1_validate,
1282 .sync_super = super_1_sync,
1283 },
1284};
1285
1286static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1287{
1288 struct list_head *tmp;
1289 mdk_rdev_t *rdev;
1290
1291 ITERATE_RDEV(mddev,rdev,tmp)
1292 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1293 return rdev;
1294
1295 return NULL;
1296}
1297
1298static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1299{
1300 struct list_head *tmp;
1301 mdk_rdev_t *rdev;
1302
1303 ITERATE_RDEV(mddev1,rdev,tmp)
1304 if (match_dev_unit(mddev2, rdev))
1305 return 1;
1306
1307 return 0;
1308}
1309
1310static LIST_HEAD(pending_raid_disks);
1311
1312static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1313{
1314 mdk_rdev_t *same_pdev;
1315 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001316 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001317 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 if (rdev->mddev) {
1320 MD_BUG();
1321 return -EINVAL;
1322 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001323 /* make sure rdev->size exceeds mddev->size */
1324 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1325 if (mddev->pers)
1326 /* Cannot change size, so fail */
1327 return -ENOSPC;
1328 else
1329 mddev->size = rdev->size;
1330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 same_pdev = match_dev_unit(mddev, rdev);
1332 if (same_pdev)
1333 printk(KERN_WARNING
1334 "%s: WARNING: %s appears to be on the same physical"
1335 " disk as %s. True\n protection against single-disk"
1336 " failure might be compromised.\n",
1337 mdname(mddev), bdevname(rdev->bdev,b),
1338 bdevname(same_pdev->bdev,b2));
1339
1340 /* Verify rdev->desc_nr is unique.
1341 * If it is -1, assign a free number, else
1342 * check number is not in use
1343 */
1344 if (rdev->desc_nr < 0) {
1345 int choice = 0;
1346 if (mddev->pers) choice = mddev->raid_disks;
1347 while (find_rdev_nr(mddev, choice))
1348 choice++;
1349 rdev->desc_nr = choice;
1350 } else {
1351 if (find_rdev_nr(mddev, rdev->desc_nr))
1352 return -EBUSY;
1353 }
NeilBrown19133a42005-11-08 21:39:35 -08001354 bdevname(rdev->bdev,b);
1355 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1356 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001357 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1358 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 list_add(&rdev->same_set, &mddev->disks);
1361 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001362 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001363
NeilBrown9c791972005-11-08 21:39:28 -08001364 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001365 kobject_add(&rdev->kobj);
1366
NeilBrownf637b9f2005-11-08 21:39:37 -08001367 if (rdev->bdev->bd_part)
1368 ko = &rdev->bdev->bd_part->kobj;
1369 else
1370 ko = &rdev->bdev->bd_disk->kobj;
1371 sysfs_create_link(&rdev->kobj, ko, "block");
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001372 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
1375
1376static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1377{
1378 char b[BDEVNAME_SIZE];
1379 if (!rdev->mddev) {
1380 MD_BUG();
1381 return;
1382 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001383 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 list_del_init(&rdev->same_set);
1385 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1386 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001387 sysfs_remove_link(&rdev->kobj, "block");
1388 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391/*
1392 * prevent the device from being mounted, repartitioned or
1393 * otherwise reused by a RAID array (or any other kernel
1394 * subsystem), by bd_claiming the device.
1395 */
1396static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1397{
1398 int err = 0;
1399 struct block_device *bdev;
1400 char b[BDEVNAME_SIZE];
1401
1402 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1403 if (IS_ERR(bdev)) {
1404 printk(KERN_ERR "md: could not open %s.\n",
1405 __bdevname(dev, b));
1406 return PTR_ERR(bdev);
1407 }
1408 err = bd_claim(bdev, rdev);
1409 if (err) {
1410 printk(KERN_ERR "md: could not bd_claim %s.\n",
1411 bdevname(bdev, b));
1412 blkdev_put(bdev);
1413 return err;
1414 }
1415 rdev->bdev = bdev;
1416 return err;
1417}
1418
1419static void unlock_rdev(mdk_rdev_t *rdev)
1420{
1421 struct block_device *bdev = rdev->bdev;
1422 rdev->bdev = NULL;
1423 if (!bdev)
1424 MD_BUG();
1425 bd_release(bdev);
1426 blkdev_put(bdev);
1427}
1428
1429void md_autodetect_dev(dev_t dev);
1430
1431static void export_rdev(mdk_rdev_t * rdev)
1432{
1433 char b[BDEVNAME_SIZE];
1434 printk(KERN_INFO "md: export_rdev(%s)\n",
1435 bdevname(rdev->bdev,b));
1436 if (rdev->mddev)
1437 MD_BUG();
1438 free_disk_sb(rdev);
1439 list_del_init(&rdev->same_set);
1440#ifndef MODULE
1441 md_autodetect_dev(rdev->bdev->bd_dev);
1442#endif
1443 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001444 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
1447static void kick_rdev_from_array(mdk_rdev_t * rdev)
1448{
1449 unbind_rdev_from_array(rdev);
1450 export_rdev(rdev);
1451}
1452
1453static void export_array(mddev_t *mddev)
1454{
1455 struct list_head *tmp;
1456 mdk_rdev_t *rdev;
1457
1458 ITERATE_RDEV(mddev,rdev,tmp) {
1459 if (!rdev->mddev) {
1460 MD_BUG();
1461 continue;
1462 }
1463 kick_rdev_from_array(rdev);
1464 }
1465 if (!list_empty(&mddev->disks))
1466 MD_BUG();
1467 mddev->raid_disks = 0;
1468 mddev->major_version = 0;
1469}
1470
1471static void print_desc(mdp_disk_t *desc)
1472{
1473 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1474 desc->major,desc->minor,desc->raid_disk,desc->state);
1475}
1476
1477static void print_sb(mdp_super_t *sb)
1478{
1479 int i;
1480
1481 printk(KERN_INFO
1482 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1483 sb->major_version, sb->minor_version, sb->patch_version,
1484 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1485 sb->ctime);
1486 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1487 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1488 sb->md_minor, sb->layout, sb->chunk_size);
1489 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1490 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1491 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1492 sb->failed_disks, sb->spare_disks,
1493 sb->sb_csum, (unsigned long)sb->events_lo);
1494
1495 printk(KERN_INFO);
1496 for (i = 0; i < MD_SB_DISKS; i++) {
1497 mdp_disk_t *desc;
1498
1499 desc = sb->disks + i;
1500 if (desc->number || desc->major || desc->minor ||
1501 desc->raid_disk || (desc->state && (desc->state != 4))) {
1502 printk(" D %2d: ", i);
1503 print_desc(desc);
1504 }
1505 }
1506 printk(KERN_INFO "md: THIS: ");
1507 print_desc(&sb->this_disk);
1508
1509}
1510
1511static void print_rdev(mdk_rdev_t *rdev)
1512{
1513 char b[BDEVNAME_SIZE];
1514 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1515 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001516 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1517 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (rdev->sb_loaded) {
1519 printk(KERN_INFO "md: rdev superblock:\n");
1520 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1521 } else
1522 printk(KERN_INFO "md: no rdev superblock!\n");
1523}
1524
1525void md_print_devices(void)
1526{
1527 struct list_head *tmp, *tmp2;
1528 mdk_rdev_t *rdev;
1529 mddev_t *mddev;
1530 char b[BDEVNAME_SIZE];
1531
1532 printk("\n");
1533 printk("md: **********************************\n");
1534 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1535 printk("md: **********************************\n");
1536 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
NeilBrown32a76272005-06-21 17:17:14 -07001538 if (mddev->bitmap)
1539 bitmap_print_sb(mddev->bitmap);
1540 else
1541 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 ITERATE_RDEV(mddev,rdev,tmp2)
1543 printk("<%s>", bdevname(rdev->bdev,b));
1544 printk("\n");
1545
1546 ITERATE_RDEV(mddev,rdev,tmp2)
1547 print_rdev(rdev);
1548 }
1549 printk("md: **********************************\n");
1550 printk("\n");
1551}
1552
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554static void sync_sbs(mddev_t * mddev)
1555{
1556 mdk_rdev_t *rdev;
1557 struct list_head *tmp;
1558
1559 ITERATE_RDEV(mddev,rdev,tmp) {
1560 super_types[mddev->major_version].
1561 sync_super(mddev, rdev);
1562 rdev->sb_loaded = 1;
1563 }
1564}
1565
NeilBrownf6705572006-03-27 01:18:11 -08001566void md_update_sb(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001568 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 struct list_head *tmp;
1570 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001571 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001574 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001575 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 mddev->utime = get_seconds();
1577 mddev->events ++;
1578
1579 if (!mddev->events) {
1580 /*
1581 * oops, this 64-bit counter should never wrap.
1582 * Either we are in around ~1 trillion A.C., assuming
1583 * 1 reboot per second, or we have a bug:
1584 */
1585 MD_BUG();
1586 mddev->events --;
1587 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001588 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 sync_sbs(mddev);
1590
1591 /*
1592 * do not write anything to disk if using
1593 * nonpersistent superblocks
1594 */
NeilBrown06d91a52005-06-21 17:17:12 -07001595 if (!mddev->persistent) {
1596 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001597 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001598 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001600 }
NeilBrowna9701a32005-11-08 21:39:34 -08001601 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 dprintk(KERN_INFO
1604 "md: updating %s RAID superblock on device (in sync %d)\n",
1605 mdname(mddev),mddev->in_sync);
1606
NeilBrown32a76272005-06-21 17:17:14 -07001607 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 ITERATE_RDEV(mddev,rdev,tmp) {
1609 char b[BDEVNAME_SIZE];
1610 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001611 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 dprintk("(skipping faulty ");
1613
1614 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001615 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001616 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001617 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001618 rdev->sb_page);
1619 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1620 bdevname(rdev->bdev,b),
1621 (unsigned long long)rdev->sb_offset);
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 } else
1624 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001625 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /* only need to write one superblock... */
1627 break;
1628 }
NeilBrowna9701a32005-11-08 21:39:34 -08001629 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001630 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1631
NeilBrowna9701a32005-11-08 21:39:34 -08001632 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001633 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001634 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001635 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001636 goto repeat;
1637 }
1638 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001639 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001640 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
NeilBrownf6705572006-03-27 01:18:11 -08001643EXPORT_SYMBOL_GPL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
NeilBrownbce74da2006-01-06 00:20:41 -08001645/* words written to sysfs files may, or my not, be \n terminated.
1646 * We want to accept with case. For this we use cmd_match.
1647 */
1648static int cmd_match(const char *cmd, const char *str)
1649{
1650 /* See if cmd, written into a sysfs file, matches
1651 * str. They must either be the same, or cmd can
1652 * have a trailing newline
1653 */
1654 while (*cmd && *str && *cmd == *str) {
1655 cmd++;
1656 str++;
1657 }
1658 if (*cmd == '\n')
1659 cmd++;
1660 if (*str || *cmd)
1661 return 0;
1662 return 1;
1663}
1664
NeilBrown86e6ffd2005-11-08 21:39:24 -08001665struct rdev_sysfs_entry {
1666 struct attribute attr;
1667 ssize_t (*show)(mdk_rdev_t *, char *);
1668 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1669};
1670
1671static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001672state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001673{
1674 char *sep = "";
1675 int len=0;
1676
NeilBrownb2d444d2005-11-08 21:39:31 -08001677 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001678 len+= sprintf(page+len, "%sfaulty",sep);
1679 sep = ",";
1680 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001681 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001682 len += sprintf(page+len, "%sin_sync",sep);
1683 sep = ",";
1684 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001685 if (!test_bit(Faulty, &rdev->flags) &&
1686 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001687 len += sprintf(page+len, "%sspare", sep);
1688 sep = ",";
1689 }
1690 return len+sprintf(page+len, "\n");
1691}
1692
NeilBrown96de1e62005-11-08 21:39:39 -08001693static struct rdev_sysfs_entry
1694rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001695
1696static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001697super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001698{
1699 if (rdev->sb_loaded && rdev->sb_size) {
1700 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1701 return rdev->sb_size;
1702 } else
1703 return 0;
1704}
NeilBrown96de1e62005-11-08 21:39:39 -08001705static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1706
NeilBrown4dbcdc72006-01-06 00:20:52 -08001707static ssize_t
1708errors_show(mdk_rdev_t *rdev, char *page)
1709{
1710 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1711}
1712
1713static ssize_t
1714errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1715{
1716 char *e;
1717 unsigned long n = simple_strtoul(buf, &e, 10);
1718 if (*buf && (*e == 0 || *e == '\n')) {
1719 atomic_set(&rdev->corrected_errors, n);
1720 return len;
1721 }
1722 return -EINVAL;
1723}
1724static struct rdev_sysfs_entry rdev_errors =
1725__ATTR(errors, 0644, errors_show, errors_store);
1726
NeilBrown014236d2006-01-06 00:20:55 -08001727static ssize_t
1728slot_show(mdk_rdev_t *rdev, char *page)
1729{
1730 if (rdev->raid_disk < 0)
1731 return sprintf(page, "none\n");
1732 else
1733 return sprintf(page, "%d\n", rdev->raid_disk);
1734}
1735
1736static ssize_t
1737slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1738{
1739 char *e;
1740 int slot = simple_strtoul(buf, &e, 10);
1741 if (strncmp(buf, "none", 4)==0)
1742 slot = -1;
1743 else if (e==buf || (*e && *e!= '\n'))
1744 return -EINVAL;
1745 if (rdev->mddev->pers)
1746 /* Cannot set slot in active array (yet) */
1747 return -EBUSY;
1748 if (slot >= rdev->mddev->raid_disks)
1749 return -ENOSPC;
1750 rdev->raid_disk = slot;
1751 /* assume it is working */
1752 rdev->flags = 0;
1753 set_bit(In_sync, &rdev->flags);
1754 return len;
1755}
1756
1757
1758static struct rdev_sysfs_entry rdev_slot =
1759__ATTR(slot, 0644, slot_show, slot_store);
1760
NeilBrown93c8cad2006-01-06 00:20:56 -08001761static ssize_t
1762offset_show(mdk_rdev_t *rdev, char *page)
1763{
Andrew Morton6961ece2006-01-06 00:20:59 -08001764 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001765}
1766
1767static ssize_t
1768offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1769{
1770 char *e;
1771 unsigned long long offset = simple_strtoull(buf, &e, 10);
1772 if (e==buf || (*e && *e != '\n'))
1773 return -EINVAL;
1774 if (rdev->mddev->pers)
1775 return -EBUSY;
1776 rdev->data_offset = offset;
1777 return len;
1778}
1779
1780static struct rdev_sysfs_entry rdev_offset =
1781__ATTR(offset, 0644, offset_show, offset_store);
1782
NeilBrown83303b62006-01-06 00:21:06 -08001783static ssize_t
1784rdev_size_show(mdk_rdev_t *rdev, char *page)
1785{
1786 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1787}
1788
1789static ssize_t
1790rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1791{
1792 char *e;
1793 unsigned long long size = simple_strtoull(buf, &e, 10);
1794 if (e==buf || (*e && *e != '\n'))
1795 return -EINVAL;
1796 if (rdev->mddev->pers)
1797 return -EBUSY;
1798 rdev->size = size;
1799 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1800 rdev->mddev->size = size;
1801 return len;
1802}
1803
1804static struct rdev_sysfs_entry rdev_size =
1805__ATTR(size, 0644, rdev_size_show, rdev_size_store);
1806
NeilBrown86e6ffd2005-11-08 21:39:24 -08001807static struct attribute *rdev_default_attrs[] = {
1808 &rdev_state.attr,
1809 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001810 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001811 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001812 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001813 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001814 NULL,
1815};
1816static ssize_t
1817rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1818{
1819 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1820 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1821
1822 if (!entry->show)
1823 return -EIO;
1824 return entry->show(rdev, page);
1825}
1826
1827static ssize_t
1828rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1829 const char *page, size_t length)
1830{
1831 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1832 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1833
1834 if (!entry->store)
1835 return -EIO;
1836 return entry->store(rdev, page, length);
1837}
1838
1839static void rdev_free(struct kobject *ko)
1840{
1841 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1842 kfree(rdev);
1843}
1844static struct sysfs_ops rdev_sysfs_ops = {
1845 .show = rdev_attr_show,
1846 .store = rdev_attr_store,
1847};
1848static struct kobj_type rdev_ktype = {
1849 .release = rdev_free,
1850 .sysfs_ops = &rdev_sysfs_ops,
1851 .default_attrs = rdev_default_attrs,
1852};
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854/*
1855 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1856 *
1857 * mark the device faulty if:
1858 *
1859 * - the device is nonexistent (zero size)
1860 * - the device has no valid superblock
1861 *
1862 * a faulty rdev _never_ has rdev->sb set.
1863 */
1864static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1865{
1866 char b[BDEVNAME_SIZE];
1867 int err;
1868 mdk_rdev_t *rdev;
1869 sector_t size;
1870
NeilBrown9ffae0c2006-01-06 00:20:32 -08001871 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (!rdev) {
1873 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1874 return ERR_PTR(-ENOMEM);
1875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 if ((err = alloc_disk_sb(rdev)))
1878 goto abort_free;
1879
1880 err = lock_rdev(rdev, newdev);
1881 if (err)
1882 goto abort_free;
1883
NeilBrown86e6ffd2005-11-08 21:39:24 -08001884 rdev->kobj.parent = NULL;
1885 rdev->kobj.ktype = &rdev_ktype;
1886 kobject_init(&rdev->kobj);
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001889 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 rdev->data_offset = 0;
1891 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001892 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001893 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1896 if (!size) {
1897 printk(KERN_WARNING
1898 "md: %s has zero or unknown size, marking faulty!\n",
1899 bdevname(rdev->bdev,b));
1900 err = -EINVAL;
1901 goto abort_free;
1902 }
1903
1904 if (super_format >= 0) {
1905 err = super_types[super_format].
1906 load_super(rdev, NULL, super_minor);
1907 if (err == -EINVAL) {
1908 printk(KERN_WARNING
1909 "md: %s has invalid sb, not importing!\n",
1910 bdevname(rdev->bdev,b));
1911 goto abort_free;
1912 }
1913 if (err < 0) {
1914 printk(KERN_WARNING
1915 "md: could not read %s's sb, not importing!\n",
1916 bdevname(rdev->bdev,b));
1917 goto abort_free;
1918 }
1919 }
1920 INIT_LIST_HEAD(&rdev->same_set);
1921
1922 return rdev;
1923
1924abort_free:
1925 if (rdev->sb_page) {
1926 if (rdev->bdev)
1927 unlock_rdev(rdev);
1928 free_disk_sb(rdev);
1929 }
1930 kfree(rdev);
1931 return ERR_PTR(err);
1932}
1933
1934/*
1935 * Check a full RAID array for plausibility
1936 */
1937
1938
NeilBrowna757e642005-04-16 15:26:42 -07001939static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940{
1941 int i;
1942 struct list_head *tmp;
1943 mdk_rdev_t *rdev, *freshest;
1944 char b[BDEVNAME_SIZE];
1945
1946 freshest = NULL;
1947 ITERATE_RDEV(mddev,rdev,tmp)
1948 switch (super_types[mddev->major_version].
1949 load_super(rdev, freshest, mddev->minor_version)) {
1950 case 1:
1951 freshest = rdev;
1952 break;
1953 case 0:
1954 break;
1955 default:
1956 printk( KERN_ERR \
1957 "md: fatal superblock inconsistency in %s"
1958 " -- removing from array\n",
1959 bdevname(rdev->bdev,b));
1960 kick_rdev_from_array(rdev);
1961 }
1962
1963
1964 super_types[mddev->major_version].
1965 validate_super(mddev, freshest);
1966
1967 i = 0;
1968 ITERATE_RDEV(mddev,rdev,tmp) {
1969 if (rdev != freshest)
1970 if (super_types[mddev->major_version].
1971 validate_super(mddev, rdev)) {
1972 printk(KERN_WARNING "md: kicking non-fresh %s"
1973 " from array!\n",
1974 bdevname(rdev->bdev,b));
1975 kick_rdev_from_array(rdev);
1976 continue;
1977 }
1978 if (mddev->level == LEVEL_MULTIPATH) {
1979 rdev->desc_nr = i++;
1980 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001981 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
1983 }
1984
1985
1986
1987 if (mddev->recovery_cp != MaxSector &&
1988 mddev->level >= 1)
1989 printk(KERN_ERR "md: %s: raid array is not clean"
1990 " -- starting background reconstruction\n",
1991 mdname(mddev));
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
NeilBrowneae17012005-11-08 21:39:23 -08001995static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07001996safe_delay_show(mddev_t *mddev, char *page)
1997{
1998 int msec = (mddev->safemode_delay*1000)/HZ;
1999 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2000}
2001static ssize_t
2002safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2003{
2004 int scale=1;
2005 int dot=0;
2006 int i;
2007 unsigned long msec;
2008 char buf[30];
2009 char *e;
2010 /* remove a period, and count digits after it */
2011 if (len >= sizeof(buf))
2012 return -EINVAL;
2013 strlcpy(buf, cbuf, len);
2014 buf[len] = 0;
2015 for (i=0; i<len; i++) {
2016 if (dot) {
2017 if (isdigit(buf[i])) {
2018 buf[i-1] = buf[i];
2019 scale *= 10;
2020 }
2021 buf[i] = 0;
2022 } else if (buf[i] == '.') {
2023 dot=1;
2024 buf[i] = 0;
2025 }
2026 }
2027 msec = simple_strtoul(buf, &e, 10);
2028 if (e == buf || (*e && *e != '\n'))
2029 return -EINVAL;
2030 msec = (msec * 1000) / scale;
2031 if (msec == 0)
2032 mddev->safemode_delay = 0;
2033 else {
2034 mddev->safemode_delay = (msec*HZ)/1000;
2035 if (mddev->safemode_delay == 0)
2036 mddev->safemode_delay = 1;
2037 }
2038 return len;
2039}
2040static struct md_sysfs_entry md_safe_delay =
2041__ATTR(safe_mode_delay, 0644,safe_delay_show, safe_delay_store);
2042
2043static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002044level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002045{
NeilBrown2604b702006-01-06 00:20:36 -08002046 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002047 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002048 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002049 else if (mddev->clevel[0])
2050 return sprintf(page, "%s\n", mddev->clevel);
2051 else if (mddev->level != LEVEL_NONE)
2052 return sprintf(page, "%d\n", mddev->level);
2053 else
2054 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002055}
2056
NeilBrownd9d166c2006-01-06 00:20:51 -08002057static ssize_t
2058level_store(mddev_t *mddev, const char *buf, size_t len)
2059{
2060 int rv = len;
2061 if (mddev->pers)
2062 return -EBUSY;
2063 if (len == 0)
2064 return 0;
2065 if (len >= sizeof(mddev->clevel))
2066 return -ENOSPC;
2067 strncpy(mddev->clevel, buf, len);
2068 if (mddev->clevel[len-1] == '\n')
2069 len--;
2070 mddev->clevel[len] = 0;
2071 mddev->level = LEVEL_NONE;
2072 return rv;
2073}
2074
2075static struct md_sysfs_entry md_level =
2076__ATTR(level, 0644, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002077
2078static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002079raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002080{
NeilBrownbb636542005-11-08 21:39:45 -08002081 if (mddev->raid_disks == 0)
2082 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002083 return sprintf(page, "%d\n", mddev->raid_disks);
2084}
2085
NeilBrownda943b992006-01-06 00:20:54 -08002086static int update_raid_disks(mddev_t *mddev, int raid_disks);
2087
2088static ssize_t
2089raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2090{
2091 /* can only set raid_disks if array is not yet active */
2092 char *e;
2093 int rv = 0;
2094 unsigned long n = simple_strtoul(buf, &e, 10);
2095
2096 if (!*buf || (*e && *e != '\n'))
2097 return -EINVAL;
2098
2099 if (mddev->pers)
2100 rv = update_raid_disks(mddev, n);
2101 else
2102 mddev->raid_disks = n;
2103 return rv ? rv : len;
2104}
2105static struct md_sysfs_entry md_raid_disks =
2106__ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002107
NeilBrown24dd4692005-11-08 21:39:26 -08002108static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002109chunk_size_show(mddev_t *mddev, char *page)
2110{
2111 return sprintf(page, "%d\n", mddev->chunk_size);
2112}
2113
2114static ssize_t
2115chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2116{
2117 /* can only set chunk_size if array is not yet active */
2118 char *e;
2119 unsigned long n = simple_strtoul(buf, &e, 10);
2120
2121 if (mddev->pers)
2122 return -EBUSY;
2123 if (!*buf || (*e && *e != '\n'))
2124 return -EINVAL;
2125
2126 mddev->chunk_size = n;
2127 return len;
2128}
2129static struct md_sysfs_entry md_chunk_size =
2130__ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2131
NeilBrown6d7ff7382006-01-06 00:21:16 -08002132static ssize_t
2133null_show(mddev_t *mddev, char *page)
2134{
2135 return -EINVAL;
2136}
2137
2138static ssize_t
2139new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2140{
2141 /* buf must be %d:%d\n? giving major and minor numbers */
2142 /* The new device is added to the array.
2143 * If the array has a persistent superblock, we read the
2144 * superblock to initialise info and check validity.
2145 * Otherwise, only checking done is that in bind_rdev_to_array,
2146 * which mainly checks size.
2147 */
2148 char *e;
2149 int major = simple_strtoul(buf, &e, 10);
2150 int minor;
2151 dev_t dev;
2152 mdk_rdev_t *rdev;
2153 int err;
2154
2155 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2156 return -EINVAL;
2157 minor = simple_strtoul(e+1, &e, 10);
2158 if (*e && *e != '\n')
2159 return -EINVAL;
2160 dev = MKDEV(major, minor);
2161 if (major != MAJOR(dev) ||
2162 minor != MINOR(dev))
2163 return -EOVERFLOW;
2164
2165
2166 if (mddev->persistent) {
2167 rdev = md_import_device(dev, mddev->major_version,
2168 mddev->minor_version);
2169 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2170 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2171 mdk_rdev_t, same_set);
2172 err = super_types[mddev->major_version]
2173 .load_super(rdev, rdev0, mddev->minor_version);
2174 if (err < 0)
2175 goto out;
2176 }
2177 } else
2178 rdev = md_import_device(dev, -1, -1);
2179
2180 if (IS_ERR(rdev))
2181 return PTR_ERR(rdev);
2182 err = bind_rdev_to_array(rdev, mddev);
2183 out:
2184 if (err)
2185 export_rdev(rdev);
2186 return err ? err : len;
2187}
2188
2189static struct md_sysfs_entry md_new_device =
2190__ATTR(new_dev, 0200, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002191
2192static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002193size_show(mddev_t *mddev, char *page)
2194{
2195 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2196}
2197
2198static int update_size(mddev_t *mddev, unsigned long size);
2199
2200static ssize_t
2201size_store(mddev_t *mddev, const char *buf, size_t len)
2202{
2203 /* If array is inactive, we can reduce the component size, but
2204 * not increase it (except from 0).
2205 * If array is active, we can try an on-line resize
2206 */
2207 char *e;
2208 int err = 0;
2209 unsigned long long size = simple_strtoull(buf, &e, 10);
2210 if (!*buf || *buf == '\n' ||
2211 (*e && *e != '\n'))
2212 return -EINVAL;
2213
2214 if (mddev->pers) {
2215 err = update_size(mddev, size);
2216 md_update_sb(mddev);
2217 } else {
2218 if (mddev->size == 0 ||
2219 mddev->size > size)
2220 mddev->size = size;
2221 else
2222 err = -ENOSPC;
2223 }
2224 return err ? err : len;
2225}
2226
2227static struct md_sysfs_entry md_size =
2228__ATTR(component_size, 0644, size_show, size_store);
2229
NeilBrown8bb93aa2006-01-06 00:20:50 -08002230
2231/* Metdata version.
2232 * This is either 'none' for arrays with externally managed metadata,
2233 * or N.M for internally known formats
2234 */
2235static ssize_t
2236metadata_show(mddev_t *mddev, char *page)
2237{
2238 if (mddev->persistent)
2239 return sprintf(page, "%d.%d\n",
2240 mddev->major_version, mddev->minor_version);
2241 else
2242 return sprintf(page, "none\n");
2243}
2244
2245static ssize_t
2246metadata_store(mddev_t *mddev, const char *buf, size_t len)
2247{
2248 int major, minor;
2249 char *e;
2250 if (!list_empty(&mddev->disks))
2251 return -EBUSY;
2252
2253 if (cmd_match(buf, "none")) {
2254 mddev->persistent = 0;
2255 mddev->major_version = 0;
2256 mddev->minor_version = 90;
2257 return len;
2258 }
2259 major = simple_strtoul(buf, &e, 10);
2260 if (e==buf || *e != '.')
2261 return -EINVAL;
2262 buf = e+1;
2263 minor = simple_strtoul(buf, &e, 10);
2264 if (e==buf || *e != '\n')
2265 return -EINVAL;
2266 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2267 super_types[major].name == NULL)
2268 return -ENOENT;
2269 mddev->major_version = major;
2270 mddev->minor_version = minor;
2271 mddev->persistent = 1;
2272 return len;
2273}
2274
2275static struct md_sysfs_entry md_metadata =
2276__ATTR(metadata_version, 0644, metadata_show, metadata_store);
2277
NeilBrowna35b0d62006-01-06 00:20:49 -08002278static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002279action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002280{
NeilBrown7eec3142005-11-08 21:39:44 -08002281 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002282 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2283 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002284 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2285 type = "reshape";
2286 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002287 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2288 type = "resync";
2289 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2290 type = "check";
2291 else
2292 type = "repair";
2293 } else
2294 type = "recover";
2295 }
2296 return sprintf(page, "%s\n", type);
2297}
2298
2299static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002300action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002301{
NeilBrown7eec3142005-11-08 21:39:44 -08002302 if (!mddev->pers || !mddev->pers->sync_request)
2303 return -EINVAL;
2304
NeilBrownbce74da2006-01-06 00:20:41 -08002305 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002306 if (mddev->sync_thread) {
2307 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2308 md_unregister_thread(mddev->sync_thread);
2309 mddev->sync_thread = NULL;
2310 mddev->recovery = 0;
2311 }
NeilBrown03c902e2006-01-06 00:20:46 -08002312 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2313 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002314 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002315 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002316 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002317 else if (cmd_match(page, "reshape")) {
2318 int err;
2319 if (mddev->pers->start_reshape == NULL)
2320 return -EINVAL;
2321 err = mddev->pers->start_reshape(mddev);
2322 if (err)
2323 return err;
2324 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002325 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002326 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002327 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002328 return -EINVAL;
2329 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2330 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002331 }
NeilBrown03c902e2006-01-06 00:20:46 -08002332 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002333 md_wakeup_thread(mddev->thread);
2334 return len;
2335}
2336
NeilBrown9d888832005-11-08 21:39:26 -08002337static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002338mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002339{
2340 return sprintf(page, "%llu\n",
2341 (unsigned long long) mddev->resync_mismatches);
2342}
2343
NeilBrown96de1e62005-11-08 21:39:39 -08002344static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08002345md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002346
NeilBrown96de1e62005-11-08 21:39:39 -08002347
2348static struct md_sysfs_entry
2349md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002350
NeilBrown88202a02006-01-06 00:21:36 -08002351static ssize_t
2352sync_min_show(mddev_t *mddev, char *page)
2353{
2354 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2355 mddev->sync_speed_min ? "local": "system");
2356}
2357
2358static ssize_t
2359sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2360{
2361 int min;
2362 char *e;
2363 if (strncmp(buf, "system", 6)==0) {
2364 mddev->sync_speed_min = 0;
2365 return len;
2366 }
2367 min = simple_strtoul(buf, &e, 10);
2368 if (buf == e || (*e && *e != '\n') || min <= 0)
2369 return -EINVAL;
2370 mddev->sync_speed_min = min;
2371 return len;
2372}
2373
2374static struct md_sysfs_entry md_sync_min =
2375__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2376
2377static ssize_t
2378sync_max_show(mddev_t *mddev, char *page)
2379{
2380 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2381 mddev->sync_speed_max ? "local": "system");
2382}
2383
2384static ssize_t
2385sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2386{
2387 int max;
2388 char *e;
2389 if (strncmp(buf, "system", 6)==0) {
2390 mddev->sync_speed_max = 0;
2391 return len;
2392 }
2393 max = simple_strtoul(buf, &e, 10);
2394 if (buf == e || (*e && *e != '\n') || max <= 0)
2395 return -EINVAL;
2396 mddev->sync_speed_max = max;
2397 return len;
2398}
2399
2400static struct md_sysfs_entry md_sync_max =
2401__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2402
2403
2404static ssize_t
2405sync_speed_show(mddev_t *mddev, char *page)
2406{
2407 unsigned long resync, dt, db;
2408 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2409 dt = ((jiffies - mddev->resync_mark) / HZ);
2410 if (!dt) dt++;
2411 db = resync - (mddev->resync_mark_cnt);
2412 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2413}
2414
2415static struct md_sysfs_entry
2416md_sync_speed = __ATTR_RO(sync_speed);
2417
2418static ssize_t
2419sync_completed_show(mddev_t *mddev, char *page)
2420{
2421 unsigned long max_blocks, resync;
2422
2423 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2424 max_blocks = mddev->resync_max_sectors;
2425 else
2426 max_blocks = mddev->size << 1;
2427
2428 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2429 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2430}
2431
2432static struct md_sysfs_entry
2433md_sync_completed = __ATTR_RO(sync_completed);
2434
NeilBrowne464eaf2006-03-27 01:18:14 -08002435static ssize_t
2436suspend_lo_show(mddev_t *mddev, char *page)
2437{
2438 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2439}
2440
2441static ssize_t
2442suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2443{
2444 char *e;
2445 unsigned long long new = simple_strtoull(buf, &e, 10);
2446
2447 if (mddev->pers->quiesce == NULL)
2448 return -EINVAL;
2449 if (buf == e || (*e && *e != '\n'))
2450 return -EINVAL;
2451 if (new >= mddev->suspend_hi ||
2452 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2453 mddev->suspend_lo = new;
2454 mddev->pers->quiesce(mddev, 2);
2455 return len;
2456 } else
2457 return -EINVAL;
2458}
2459static struct md_sysfs_entry md_suspend_lo =
2460__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2461
2462
2463static ssize_t
2464suspend_hi_show(mddev_t *mddev, char *page)
2465{
2466 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2467}
2468
2469static ssize_t
2470suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2471{
2472 char *e;
2473 unsigned long long new = simple_strtoull(buf, &e, 10);
2474
2475 if (mddev->pers->quiesce == NULL)
2476 return -EINVAL;
2477 if (buf == e || (*e && *e != '\n'))
2478 return -EINVAL;
2479 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2480 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2481 mddev->suspend_hi = new;
2482 mddev->pers->quiesce(mddev, 1);
2483 mddev->pers->quiesce(mddev, 0);
2484 return len;
2485 } else
2486 return -EINVAL;
2487}
2488static struct md_sysfs_entry md_suspend_hi =
2489__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2490
2491
NeilBrowneae17012005-11-08 21:39:23 -08002492static struct attribute *md_default_attrs[] = {
2493 &md_level.attr,
2494 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002495 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002496 &md_size.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002497 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08002498 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002499 &md_safe_delay.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002500 NULL,
2501};
2502
2503static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002504 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002505 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002506 &md_sync_min.attr,
2507 &md_sync_max.attr,
2508 &md_sync_speed.attr,
2509 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002510 &md_suspend_lo.attr,
2511 &md_suspend_hi.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002512 NULL,
2513};
NeilBrown411036f2005-11-08 21:39:40 -08002514static struct attribute_group md_redundancy_group = {
2515 .name = NULL,
2516 .attrs = md_redundancy_attrs,
2517};
2518
NeilBrowneae17012005-11-08 21:39:23 -08002519
2520static ssize_t
2521md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2522{
2523 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2524 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002525 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002526
2527 if (!entry->show)
2528 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002529 rv = mddev_lock(mddev);
2530 if (!rv) {
2531 rv = entry->show(mddev, page);
2532 mddev_unlock(mddev);
2533 }
NeilBrown96de1e62005-11-08 21:39:39 -08002534 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002535}
2536
2537static ssize_t
2538md_attr_store(struct kobject *kobj, struct attribute *attr,
2539 const char *page, size_t length)
2540{
2541 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2542 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002543 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002544
2545 if (!entry->store)
2546 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002547 rv = mddev_lock(mddev);
2548 if (!rv) {
2549 rv = entry->store(mddev, page, length);
2550 mddev_unlock(mddev);
2551 }
NeilBrown96de1e62005-11-08 21:39:39 -08002552 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002553}
2554
2555static void md_free(struct kobject *ko)
2556{
2557 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2558 kfree(mddev);
2559}
2560
2561static struct sysfs_ops md_sysfs_ops = {
2562 .show = md_attr_show,
2563 .store = md_attr_store,
2564};
2565static struct kobj_type md_ktype = {
2566 .release = md_free,
2567 .sysfs_ops = &md_sysfs_ops,
2568 .default_attrs = md_default_attrs,
2569};
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571int mdp_major = 0;
2572
2573static struct kobject *md_probe(dev_t dev, int *part, void *data)
2574{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002575 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 mddev_t *mddev = mddev_find(dev);
2577 struct gendisk *disk;
2578 int partitioned = (MAJOR(dev) != MD_MAJOR);
2579 int shift = partitioned ? MdpMinorShift : 0;
2580 int unit = MINOR(dev) >> shift;
2581
2582 if (!mddev)
2583 return NULL;
2584
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002585 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002587 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 mddev_put(mddev);
2589 return NULL;
2590 }
2591 disk = alloc_disk(1 << shift);
2592 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002593 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 mddev_put(mddev);
2595 return NULL;
2596 }
2597 disk->major = MAJOR(dev);
2598 disk->first_minor = unit << shift;
2599 if (partitioned) {
2600 sprintf(disk->disk_name, "md_d%d", unit);
2601 sprintf(disk->devfs_name, "md/d%d", unit);
2602 } else {
2603 sprintf(disk->disk_name, "md%d", unit);
2604 sprintf(disk->devfs_name, "md/%d", unit);
2605 }
2606 disk->fops = &md_fops;
2607 disk->private_data = mddev;
2608 disk->queue = mddev->queue;
2609 add_disk(disk);
2610 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002611 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002612 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002613 mddev->kobj.k_name = NULL;
2614 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2615 mddev->kobj.ktype = &md_ktype;
2616 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return NULL;
2618}
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620static void md_safemode_timeout(unsigned long data)
2621{
2622 mddev_t *mddev = (mddev_t *) data;
2623
2624 mddev->safemode = 1;
2625 md_wakeup_thread(mddev->thread);
2626}
2627
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002628static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
2630static int do_md_run(mddev_t * mddev)
2631{
NeilBrown2604b702006-01-06 00:20:36 -08002632 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 int chunk_size;
2634 struct list_head *tmp;
2635 mdk_rdev_t *rdev;
2636 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002637 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 char b[BDEVNAME_SIZE];
2639
NeilBrowna757e642005-04-16 15:26:42 -07002640 if (list_empty(&mddev->disks))
2641 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 if (mddev->pers)
2645 return -EBUSY;
2646
2647 /*
2648 * Analyze all RAID superblock(s)
2649 */
NeilBrowna757e642005-04-16 15:26:42 -07002650 if (!mddev->raid_disks)
2651 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
NeilBrown2604b702006-01-06 00:20:36 -08002655 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 if (chunk_size > MAX_CHUNK_SIZE) {
2657 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2658 chunk_size, MAX_CHUNK_SIZE);
2659 return -EINVAL;
2660 }
2661 /*
2662 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2663 */
2664 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07002665 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 return -EINVAL;
2667 }
2668 if (chunk_size < PAGE_SIZE) {
2669 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2670 chunk_size, PAGE_SIZE);
2671 return -EINVAL;
2672 }
2673
2674 /* devices must have minimum size of one chunk */
2675 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002676 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 continue;
2678 if (rdev->size < chunk_size / 1024) {
2679 printk(KERN_WARNING
2680 "md: Dev %s smaller than chunk_size:"
2681 " %lluk < %dk\n",
2682 bdevname(rdev->bdev,b),
2683 (unsigned long long)rdev->size,
2684 chunk_size / 1024);
2685 return -EINVAL;
2686 }
2687 }
2688 }
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08002691 if (mddev->level != LEVEL_NONE)
2692 request_module("md-level-%d", mddev->level);
2693 else if (mddev->clevel[0])
2694 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695#endif
2696
2697 /*
2698 * Drop all container device buffers, from now on
2699 * the only valid external interface is through the md
2700 * device.
2701 * Also find largest hardsector size
2702 */
2703 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002704 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 continue;
2706 sync_blockdev(rdev->bdev);
2707 invalidate_bdev(rdev->bdev, 0);
2708 }
2709
2710 md_probe(mddev->unit, NULL, NULL);
2711 disk = mddev->gendisk;
2712 if (!disk)
2713 return -ENOMEM;
2714
2715 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002716 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08002717 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002719 if (mddev->level != LEVEL_NONE)
2720 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2721 mddev->level);
2722 else
2723 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
2724 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return -EINVAL;
2726 }
NeilBrown2604b702006-01-06 00:20:36 -08002727 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002729 mddev->level = pers->level;
2730 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
NeilBrownf6705572006-03-27 01:18:11 -08002732 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08002733 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08002734 /* This personality cannot handle reshaping... */
2735 mddev->pers = NULL;
2736 module_put(pers->owner);
2737 return -EINVAL;
2738 }
2739
NeilBrown657390d2005-08-26 18:34:16 -07002740 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002742 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002743 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
NeilBrownf91de922005-11-08 21:39:36 -08002745 if (start_readonly)
2746 mddev->ro = 2; /* read-only, but switch on first write */
2747
NeilBrownb15c2e52006-01-06 00:20:16 -08002748 err = mddev->pers->run(mddev);
2749 if (!err && mddev->pers->sync_request) {
2750 err = bitmap_create(mddev);
2751 if (err) {
2752 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2753 mdname(mddev), err);
2754 mddev->pers->stop(mddev);
2755 }
2756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (err) {
2758 printk(KERN_ERR "md: pers->run() failed ...\n");
2759 module_put(mddev->pers->owner);
2760 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002761 bitmap_destroy(mddev);
2762 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
NeilBrown411036f2005-11-08 21:39:40 -08002764 if (mddev->pers->sync_request)
2765 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002766 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2767 mddev->ro = 0;
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 atomic_set(&mddev->writes_pending,0);
2770 mddev->safemode = 0;
2771 mddev->safemode_timer.function = md_safemode_timeout;
2772 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07002773 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002775
2776 ITERATE_RDEV(mddev,rdev,tmp)
2777 if (rdev->raid_disk >= 0) {
2778 char nm[20];
2779 sprintf(nm, "rd%d", rdev->raid_disk);
2780 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002784 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 if (mddev->sb_dirty)
2787 md_update_sb(mddev);
2788
2789 set_capacity(disk, mddev->array_size<<1);
2790
2791 /* If we call blk_queue_make_request here, it will
2792 * re-initialise max_sectors etc which may have been
2793 * refined inside -> run. So just set the bits we need to set.
2794 * Most initialisation happended when we called
2795 * blk_queue_make_request(..., md_fail_request)
2796 * earlier.
2797 */
2798 mddev->queue->queuedata = mddev;
2799 mddev->queue->make_request_fn = mddev->pers->make_request;
2800
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002801 /* If there is a partially-recovered drive we need to
2802 * start recovery here. If we leave it to md_check_recovery,
2803 * it will remove the drives and not do the right thing
2804 */
2805 if (mddev->degraded) {
2806 struct list_head *rtmp;
2807 int spares = 0;
2808 ITERATE_RDEV(mddev,rdev,rtmp)
2809 if (rdev->raid_disk >= 0 &&
2810 !test_bit(In_sync, &rdev->flags) &&
2811 !test_bit(Faulty, &rdev->flags))
2812 /* complete an interrupted recovery */
2813 spares++;
2814 if (spares && mddev->pers->sync_request) {
2815 mddev->recovery = 0;
2816 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
2817 mddev->sync_thread = md_register_thread(md_do_sync,
2818 mddev,
2819 "%s_resync");
2820 if (!mddev->sync_thread) {
2821 printk(KERN_ERR "%s: could not start resync"
2822 " thread...\n",
2823 mdname(mddev));
2824 /* leave the spares where they are, it shouldn't hurt */
2825 mddev->recovery = 0;
2826 } else
2827 md_wakeup_thread(mddev->sync_thread);
2828 }
2829 }
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08002832 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return 0;
2834}
2835
2836static int restart_array(mddev_t *mddev)
2837{
2838 struct gendisk *disk = mddev->gendisk;
2839 int err;
2840
2841 /*
2842 * Complain if it has no devices
2843 */
2844 err = -ENXIO;
2845 if (list_empty(&mddev->disks))
2846 goto out;
2847
2848 if (mddev->pers) {
2849 err = -EBUSY;
2850 if (!mddev->ro)
2851 goto out;
2852
2853 mddev->safemode = 0;
2854 mddev->ro = 0;
2855 set_disk_ro(disk, 0);
2856
2857 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2858 mdname(mddev));
2859 /*
2860 * Kick recovery or resync if necessary
2861 */
2862 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2863 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002864 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 err = 0;
2866 } else {
2867 printk(KERN_ERR "md: %s has no personality assigned.\n",
2868 mdname(mddev));
2869 err = -EINVAL;
2870 }
2871
2872out:
2873 return err;
2874}
2875
2876static int do_md_stop(mddev_t * mddev, int ro)
2877{
2878 int err = 0;
2879 struct gendisk *disk = mddev->gendisk;
2880
2881 if (mddev->pers) {
2882 if (atomic_read(&mddev->active)>2) {
2883 printk("md: %s still in use.\n",mdname(mddev));
2884 return -EBUSY;
2885 }
2886
2887 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002888 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2890 md_unregister_thread(mddev->sync_thread);
2891 mddev->sync_thread = NULL;
2892 }
2893
2894 del_timer_sync(&mddev->safemode_timer);
2895
2896 invalidate_partition(disk, 0);
2897
2898 if (ro) {
2899 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002900 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 goto out;
2902 mddev->ro = 1;
2903 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002904 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002905 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (mddev->ro)
2907 set_disk_ro(disk, 0);
2908 blk_queue_make_request(mddev->queue, md_fail_request);
2909 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002910 if (mddev->pers->sync_request)
2911 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 module_put(mddev->pers->owner);
2914 mddev->pers = NULL;
2915 if (mddev->ro)
2916 mddev->ro = 0;
2917 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002918 if (!mddev->in_sync || mddev->sb_dirty) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 /* mark array as shutdown cleanly */
2920 mddev->in_sync = 1;
2921 md_update_sb(mddev);
2922 }
2923 if (ro)
2924 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07002925 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
NeilBrown32a76272005-06-21 17:17:14 -07002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 /*
2929 * Free resources if final stop
2930 */
2931 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002932 mdk_rdev_t *rdev;
2933 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct gendisk *disk;
2935 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2936
NeilBrown978f9462006-02-02 14:28:05 -08002937 bitmap_destroy(mddev);
2938 if (mddev->bitmap_file) {
2939 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2940 fput(mddev->bitmap_file);
2941 mddev->bitmap_file = NULL;
2942 }
2943 mddev->bitmap_offset = 0;
2944
NeilBrown86e6ffd2005-11-08 21:39:24 -08002945 ITERATE_RDEV(mddev,rdev,tmp)
2946 if (rdev->raid_disk >= 0) {
2947 char nm[20];
2948 sprintf(nm, "rd%d", rdev->raid_disk);
2949 sysfs_remove_link(&mddev->kobj, nm);
2950 }
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 export_array(mddev);
2953
2954 mddev->array_size = 0;
2955 disk = mddev->gendisk;
2956 if (disk)
2957 set_capacity(disk, 0);
2958 mddev->changed = 1;
NeilBrowna8a55c32006-06-26 00:27:39 -07002959 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2961 mdname(mddev));
2962 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08002963 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964out:
2965 return err;
2966}
2967
2968static void autorun_array(mddev_t *mddev)
2969{
2970 mdk_rdev_t *rdev;
2971 struct list_head *tmp;
2972 int err;
2973
NeilBrowna757e642005-04-16 15:26:42 -07002974 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
2977 printk(KERN_INFO "md: running: ");
2978
2979 ITERATE_RDEV(mddev,rdev,tmp) {
2980 char b[BDEVNAME_SIZE];
2981 printk("<%s>", bdevname(rdev->bdev,b));
2982 }
2983 printk("\n");
2984
2985 err = do_md_run (mddev);
2986 if (err) {
2987 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2988 do_md_stop (mddev, 0);
2989 }
2990}
2991
2992/*
2993 * lets try to run arrays based on all disks that have arrived
2994 * until now. (those are in pending_raid_disks)
2995 *
2996 * the method: pick the first pending disk, collect all disks with
2997 * the same UUID, remove all from the pending list and put them into
2998 * the 'same_array' list. Then order this list based on superblock
2999 * update time (freshest comes first), kick out 'old' disks and
3000 * compare superblocks. If everything's fine then run it.
3001 *
3002 * If "unit" is allocated, then bump its reference count
3003 */
3004static void autorun_devices(int part)
3005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 struct list_head *tmp;
3007 mdk_rdev_t *rdev0, *rdev;
3008 mddev_t *mddev;
3009 char b[BDEVNAME_SIZE];
3010
3011 printk(KERN_INFO "md: autorun ...\n");
3012 while (!list_empty(&pending_raid_disks)) {
3013 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003014 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 rdev0 = list_entry(pending_raid_disks.next,
3016 mdk_rdev_t, same_set);
3017
3018 printk(KERN_INFO "md: considering %s ...\n",
3019 bdevname(rdev0->bdev,b));
3020 INIT_LIST_HEAD(&candidates);
3021 ITERATE_RDEV_PENDING(rdev,tmp)
3022 if (super_90_load(rdev, rdev0, 0) >= 0) {
3023 printk(KERN_INFO "md: adding %s ...\n",
3024 bdevname(rdev->bdev,b));
3025 list_move(&rdev->same_set, &candidates);
3026 }
3027 /*
3028 * now we have a set of devices, with all of them having
3029 * mostly sane superblocks. It's time to allocate the
3030 * mddev.
3031 */
3032 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
3033 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3034 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3035 break;
3036 }
3037 if (part)
3038 dev = MKDEV(mdp_major,
3039 rdev0->preferred_minor << MdpMinorShift);
3040 else
3041 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3042
3043 md_probe(dev, NULL, NULL);
3044 mddev = mddev_find(dev);
3045 if (!mddev) {
3046 printk(KERN_ERR
3047 "md: cannot allocate memory for md drive.\n");
3048 break;
3049 }
3050 if (mddev_lock(mddev))
3051 printk(KERN_WARNING "md: %s locked, cannot run\n",
3052 mdname(mddev));
3053 else if (mddev->raid_disks || mddev->major_version
3054 || !list_empty(&mddev->disks)) {
3055 printk(KERN_WARNING
3056 "md: %s already running, cannot run %s\n",
3057 mdname(mddev), bdevname(rdev0->bdev,b));
3058 mddev_unlock(mddev);
3059 } else {
3060 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3061 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3062 list_del_init(&rdev->same_set);
3063 if (bind_rdev_to_array(rdev, mddev))
3064 export_rdev(rdev);
3065 }
3066 autorun_array(mddev);
3067 mddev_unlock(mddev);
3068 }
3069 /* on success, candidates will be empty, on error
3070 * it won't...
3071 */
3072 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3073 export_rdev(rdev);
3074 mddev_put(mddev);
3075 }
3076 printk(KERN_INFO "md: ... autorun DONE.\n");
3077}
3078
3079/*
3080 * import RAID devices based on one partition
3081 * if possible, the array gets run as well.
3082 */
3083
3084static int autostart_array(dev_t startdev)
3085{
3086 char b[BDEVNAME_SIZE];
3087 int err = -EINVAL, i;
3088 mdp_super_t *sb = NULL;
3089 mdk_rdev_t *start_rdev = NULL, *rdev;
3090
3091 start_rdev = md_import_device(startdev, 0, 0);
3092 if (IS_ERR(start_rdev))
3093 return err;
3094
3095
3096 /* NOTE: this can only work for 0.90.0 superblocks */
3097 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
3098 if (sb->major_version != 0 ||
3099 sb->minor_version != 90 ) {
3100 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
3101 export_rdev(start_rdev);
3102 return err;
3103 }
3104
NeilBrownb2d444d2005-11-08 21:39:31 -08003105 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 printk(KERN_WARNING
3107 "md: can not autostart based on faulty %s!\n",
3108 bdevname(start_rdev->bdev,b));
3109 export_rdev(start_rdev);
3110 return err;
3111 }
3112 list_add(&start_rdev->same_set, &pending_raid_disks);
3113
3114 for (i = 0; i < MD_SB_DISKS; i++) {
3115 mdp_disk_t *desc = sb->disks + i;
3116 dev_t dev = MKDEV(desc->major, desc->minor);
3117
3118 if (!dev)
3119 continue;
3120 if (dev == startdev)
3121 continue;
3122 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3123 continue;
3124 rdev = md_import_device(dev, 0, 0);
3125 if (IS_ERR(rdev))
3126 continue;
3127
3128 list_add(&rdev->same_set, &pending_raid_disks);
3129 }
3130
3131 /*
3132 * possibly return codes
3133 */
3134 autorun_devices(0);
3135 return 0;
3136
3137}
3138
3139
3140static int get_version(void __user * arg)
3141{
3142 mdu_version_t ver;
3143
3144 ver.major = MD_MAJOR_VERSION;
3145 ver.minor = MD_MINOR_VERSION;
3146 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3147
3148 if (copy_to_user(arg, &ver, sizeof(ver)))
3149 return -EFAULT;
3150
3151 return 0;
3152}
3153
3154static int get_array_info(mddev_t * mddev, void __user * arg)
3155{
3156 mdu_array_info_t info;
3157 int nr,working,active,failed,spare;
3158 mdk_rdev_t *rdev;
3159 struct list_head *tmp;
3160
3161 nr=working=active=failed=spare=0;
3162 ITERATE_RDEV(mddev,rdev,tmp) {
3163 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003164 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 failed++;
3166 else {
3167 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003168 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 active++;
3170 else
3171 spare++;
3172 }
3173 }
3174
3175 info.major_version = mddev->major_version;
3176 info.minor_version = mddev->minor_version;
3177 info.patch_version = MD_PATCHLEVEL_VERSION;
3178 info.ctime = mddev->ctime;
3179 info.level = mddev->level;
3180 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003181 if (info.size != mddev->size) /* overflow */
3182 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 info.nr_disks = nr;
3184 info.raid_disks = mddev->raid_disks;
3185 info.md_minor = mddev->md_minor;
3186 info.not_persistent= !mddev->persistent;
3187
3188 info.utime = mddev->utime;
3189 info.state = 0;
3190 if (mddev->in_sync)
3191 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003192 if (mddev->bitmap && mddev->bitmap_offset)
3193 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 info.active_disks = active;
3195 info.working_disks = working;
3196 info.failed_disks = failed;
3197 info.spare_disks = spare;
3198
3199 info.layout = mddev->layout;
3200 info.chunk_size = mddev->chunk_size;
3201
3202 if (copy_to_user(arg, &info, sizeof(info)))
3203 return -EFAULT;
3204
3205 return 0;
3206}
3207
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003208static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003209{
3210 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3211 char *ptr, *buf = NULL;
3212 int err = -ENOMEM;
3213
3214 file = kmalloc(sizeof(*file), GFP_KERNEL);
3215 if (!file)
3216 goto out;
3217
3218 /* bitmap disabled, zero the first byte and copy out */
3219 if (!mddev->bitmap || !mddev->bitmap->file) {
3220 file->pathname[0] = '\0';
3221 goto copy_out;
3222 }
3223
3224 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3225 if (!buf)
3226 goto out;
3227
3228 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3229 if (!ptr)
3230 goto out;
3231
3232 strcpy(file->pathname, ptr);
3233
3234copy_out:
3235 err = 0;
3236 if (copy_to_user(arg, file, sizeof(*file)))
3237 err = -EFAULT;
3238out:
3239 kfree(buf);
3240 kfree(file);
3241 return err;
3242}
3243
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244static int get_disk_info(mddev_t * mddev, void __user * arg)
3245{
3246 mdu_disk_info_t info;
3247 unsigned int nr;
3248 mdk_rdev_t *rdev;
3249
3250 if (copy_from_user(&info, arg, sizeof(info)))
3251 return -EFAULT;
3252
3253 nr = info.number;
3254
3255 rdev = find_rdev_nr(mddev, nr);
3256 if (rdev) {
3257 info.major = MAJOR(rdev->bdev->bd_dev);
3258 info.minor = MINOR(rdev->bdev->bd_dev);
3259 info.raid_disk = rdev->raid_disk;
3260 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003261 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003263 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 info.state |= (1<<MD_DISK_ACTIVE);
3265 info.state |= (1<<MD_DISK_SYNC);
3266 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003267 if (test_bit(WriteMostly, &rdev->flags))
3268 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 } else {
3270 info.major = info.minor = 0;
3271 info.raid_disk = -1;
3272 info.state = (1<<MD_DISK_REMOVED);
3273 }
3274
3275 if (copy_to_user(arg, &info, sizeof(info)))
3276 return -EFAULT;
3277
3278 return 0;
3279}
3280
3281static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3282{
3283 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3284 mdk_rdev_t *rdev;
3285 dev_t dev = MKDEV(info->major,info->minor);
3286
3287 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3288 return -EOVERFLOW;
3289
3290 if (!mddev->raid_disks) {
3291 int err;
3292 /* expecting a device which has a superblock */
3293 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3294 if (IS_ERR(rdev)) {
3295 printk(KERN_WARNING
3296 "md: md_import_device returned %ld\n",
3297 PTR_ERR(rdev));
3298 return PTR_ERR(rdev);
3299 }
3300 if (!list_empty(&mddev->disks)) {
3301 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3302 mdk_rdev_t, same_set);
3303 int err = super_types[mddev->major_version]
3304 .load_super(rdev, rdev0, mddev->minor_version);
3305 if (err < 0) {
3306 printk(KERN_WARNING
3307 "md: %s has different UUID to %s\n",
3308 bdevname(rdev->bdev,b),
3309 bdevname(rdev0->bdev,b2));
3310 export_rdev(rdev);
3311 return -EINVAL;
3312 }
3313 }
3314 err = bind_rdev_to_array(rdev, mddev);
3315 if (err)
3316 export_rdev(rdev);
3317 return err;
3318 }
3319
3320 /*
3321 * add_new_disk can be used once the array is assembled
3322 * to add "hot spares". They must already have a superblock
3323 * written
3324 */
3325 if (mddev->pers) {
3326 int err;
3327 if (!mddev->pers->hot_add_disk) {
3328 printk(KERN_WARNING
3329 "%s: personality does not support diskops!\n",
3330 mdname(mddev));
3331 return -EINVAL;
3332 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003333 if (mddev->persistent)
3334 rdev = md_import_device(dev, mddev->major_version,
3335 mddev->minor_version);
3336 else
3337 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (IS_ERR(rdev)) {
3339 printk(KERN_WARNING
3340 "md: md_import_device returned %ld\n",
3341 PTR_ERR(rdev));
3342 return PTR_ERR(rdev);
3343 }
NeilBrown41158c72005-06-21 17:17:25 -07003344 /* set save_raid_disk if appropriate */
3345 if (!mddev->persistent) {
3346 if (info->state & (1<<MD_DISK_SYNC) &&
3347 info->raid_disk < mddev->raid_disks)
3348 rdev->raid_disk = info->raid_disk;
3349 else
3350 rdev->raid_disk = -1;
3351 } else
3352 super_types[mddev->major_version].
3353 validate_super(mddev, rdev);
3354 rdev->saved_raid_disk = rdev->raid_disk;
3355
NeilBrownb2d444d2005-11-08 21:39:31 -08003356 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003357 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3358 set_bit(WriteMostly, &rdev->flags);
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 rdev->raid_disk = -1;
3361 err = bind_rdev_to_array(rdev, mddev);
3362 if (err)
3363 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003364
3365 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003366 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 return err;
3368 }
3369
3370 /* otherwise, add_new_disk is only allowed
3371 * for major_version==0 superblocks
3372 */
3373 if (mddev->major_version != 0) {
3374 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3375 mdname(mddev));
3376 return -EINVAL;
3377 }
3378
3379 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3380 int err;
3381 rdev = md_import_device (dev, -1, 0);
3382 if (IS_ERR(rdev)) {
3383 printk(KERN_WARNING
3384 "md: error, md_import_device() returned %ld\n",
3385 PTR_ERR(rdev));
3386 return PTR_ERR(rdev);
3387 }
3388 rdev->desc_nr = info->number;
3389 if (info->raid_disk < mddev->raid_disks)
3390 rdev->raid_disk = info->raid_disk;
3391 else
3392 rdev->raid_disk = -1;
3393
NeilBrownb2d444d2005-11-08 21:39:31 -08003394 rdev->flags = 0;
3395
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003397 if (info->state & (1<<MD_DISK_SYNC))
3398 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003400 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3401 set_bit(WriteMostly, &rdev->flags);
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if (!mddev->persistent) {
3404 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3405 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3406 } else
3407 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3408 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3409
NeilBrown2bf071b2006-01-06 00:20:55 -08003410 err = bind_rdev_to_array(rdev, mddev);
3411 if (err) {
3412 export_rdev(rdev);
3413 return err;
3414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 }
3416
3417 return 0;
3418}
3419
3420static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3421{
3422 char b[BDEVNAME_SIZE];
3423 mdk_rdev_t *rdev;
3424
3425 if (!mddev->pers)
3426 return -ENODEV;
3427
3428 rdev = find_rdev(mddev, dev);
3429 if (!rdev)
3430 return -ENXIO;
3431
3432 if (rdev->raid_disk >= 0)
3433 goto busy;
3434
3435 kick_rdev_from_array(rdev);
3436 md_update_sb(mddev);
NeilBrownd7603b72006-01-06 00:20:30 -08003437 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 return 0;
3440busy:
3441 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3442 bdevname(rdev->bdev,b), mdname(mddev));
3443 return -EBUSY;
3444}
3445
3446static int hot_add_disk(mddev_t * mddev, dev_t dev)
3447{
3448 char b[BDEVNAME_SIZE];
3449 int err;
3450 unsigned int size;
3451 mdk_rdev_t *rdev;
3452
3453 if (!mddev->pers)
3454 return -ENODEV;
3455
3456 if (mddev->major_version != 0) {
3457 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3458 " version-0 superblocks.\n",
3459 mdname(mddev));
3460 return -EINVAL;
3461 }
3462 if (!mddev->pers->hot_add_disk) {
3463 printk(KERN_WARNING
3464 "%s: personality does not support diskops!\n",
3465 mdname(mddev));
3466 return -EINVAL;
3467 }
3468
3469 rdev = md_import_device (dev, -1, 0);
3470 if (IS_ERR(rdev)) {
3471 printk(KERN_WARNING
3472 "md: error, md_import_device() returned %ld\n",
3473 PTR_ERR(rdev));
3474 return -EINVAL;
3475 }
3476
3477 if (mddev->persistent)
3478 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3479 else
3480 rdev->sb_offset =
3481 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3482
3483 size = calc_dev_size(rdev, mddev->chunk_size);
3484 rdev->size = size;
3485
NeilBrownb2d444d2005-11-08 21:39:31 -08003486 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 printk(KERN_WARNING
3488 "md: can not hot-add faulty %s disk to %s!\n",
3489 bdevname(rdev->bdev,b), mdname(mddev));
3490 err = -EINVAL;
3491 goto abort_export;
3492 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003493 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 rdev->desc_nr = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003495 err = bind_rdev_to_array(rdev, mddev);
3496 if (err)
3497 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
3499 /*
3500 * The rest should better be atomic, we can have disk failures
3501 * noticed in interrupt contexts ...
3502 */
3503
3504 if (rdev->desc_nr == mddev->max_disks) {
3505 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3506 mdname(mddev));
3507 err = -EBUSY;
3508 goto abort_unbind_export;
3509 }
3510
3511 rdev->raid_disk = -1;
3512
3513 md_update_sb(mddev);
3514
3515 /*
3516 * Kick recovery, maybe this spare has to be added to the
3517 * array immediately.
3518 */
3519 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3520 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003521 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 return 0;
3523
3524abort_unbind_export:
3525 unbind_rdev_from_array(rdev);
3526
3527abort_export:
3528 export_rdev(rdev);
3529 return err;
3530}
3531
NeilBrown32a76272005-06-21 17:17:14 -07003532/* similar to deny_write_access, but accounts for our holding a reference
3533 * to the file ourselves */
3534static int deny_bitmap_write_access(struct file * file)
3535{
3536 struct inode *inode = file->f_mapping->host;
3537
3538 spin_lock(&inode->i_lock);
3539 if (atomic_read(&inode->i_writecount) > 1) {
3540 spin_unlock(&inode->i_lock);
3541 return -ETXTBSY;
3542 }
3543 atomic_set(&inode->i_writecount, -1);
3544 spin_unlock(&inode->i_lock);
3545
3546 return 0;
3547}
3548
3549static int set_bitmap_file(mddev_t *mddev, int fd)
3550{
3551 int err;
3552
NeilBrown36fa3062005-09-09 16:23:45 -07003553 if (mddev->pers) {
3554 if (!mddev->pers->quiesce)
3555 return -EBUSY;
3556 if (mddev->recovery || mddev->sync_thread)
3557 return -EBUSY;
3558 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003559 }
3560
NeilBrown36fa3062005-09-09 16:23:45 -07003561
3562 if (fd >= 0) {
3563 if (mddev->bitmap)
3564 return -EEXIST; /* cannot add when bitmap is present */
3565 mddev->bitmap_file = fget(fd);
3566
3567 if (mddev->bitmap_file == NULL) {
3568 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3569 mdname(mddev));
3570 return -EBADF;
3571 }
3572
3573 err = deny_bitmap_write_access(mddev->bitmap_file);
3574 if (err) {
3575 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3576 mdname(mddev));
3577 fput(mddev->bitmap_file);
3578 mddev->bitmap_file = NULL;
3579 return err;
3580 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003581 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003582 } else if (mddev->bitmap == NULL)
3583 return -ENOENT; /* cannot remove what isn't there */
3584 err = 0;
3585 if (mddev->pers) {
3586 mddev->pers->quiesce(mddev, 1);
3587 if (fd >= 0)
3588 err = bitmap_create(mddev);
3589 if (fd < 0 || err)
3590 bitmap_destroy(mddev);
3591 mddev->pers->quiesce(mddev, 0);
3592 } else if (fd < 0) {
3593 if (mddev->bitmap_file)
3594 fput(mddev->bitmap_file);
3595 mddev->bitmap_file = NULL;
3596 }
3597
NeilBrown32a76272005-06-21 17:17:14 -07003598 return err;
3599}
3600
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601/*
3602 * set_array_info is used two different ways
3603 * The original usage is when creating a new array.
3604 * In this usage, raid_disks is > 0 and it together with
3605 * level, size, not_persistent,layout,chunksize determine the
3606 * shape of the array.
3607 * This will always create an array with a type-0.90.0 superblock.
3608 * The newer usage is when assembling an array.
3609 * In this case raid_disks will be 0, and the major_version field is
3610 * use to determine which style super-blocks are to be found on the devices.
3611 * The minor and patch _version numbers are also kept incase the
3612 * super_block handler wishes to interpret them.
3613 */
3614static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3615{
3616
3617 if (info->raid_disks == 0) {
3618 /* just setting version number for superblock loading */
3619 if (info->major_version < 0 ||
3620 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3621 super_types[info->major_version].name == NULL) {
3622 /* maybe try to auto-load a module? */
3623 printk(KERN_INFO
3624 "md: superblock version %d not known\n",
3625 info->major_version);
3626 return -EINVAL;
3627 }
3628 mddev->major_version = info->major_version;
3629 mddev->minor_version = info->minor_version;
3630 mddev->patch_version = info->patch_version;
3631 return 0;
3632 }
3633 mddev->major_version = MD_MAJOR_VERSION;
3634 mddev->minor_version = MD_MINOR_VERSION;
3635 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3636 mddev->ctime = get_seconds();
3637
3638 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08003639 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 mddev->size = info->size;
3641 mddev->raid_disks = info->raid_disks;
3642 /* don't set md_minor, it is determined by which /dev/md* was
3643 * openned
3644 */
3645 if (info->state & (1<<MD_SB_CLEAN))
3646 mddev->recovery_cp = MaxSector;
3647 else
3648 mddev->recovery_cp = 0;
3649 mddev->persistent = ! info->not_persistent;
3650
3651 mddev->layout = info->layout;
3652 mddev->chunk_size = info->chunk_size;
3653
3654 mddev->max_disks = MD_SB_DISKS;
3655
3656 mddev->sb_dirty = 1;
3657
NeilBrownb2a27032005-11-28 13:44:12 -08003658 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3659 mddev->bitmap_offset = 0;
3660
NeilBrownf6705572006-03-27 01:18:11 -08003661 mddev->reshape_position = MaxSector;
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 /*
3664 * Generate a 128 bit UUID
3665 */
3666 get_random_bytes(mddev->uuid, 16);
3667
NeilBrownf6705572006-03-27 01:18:11 -08003668 mddev->new_level = mddev->level;
3669 mddev->new_chunk = mddev->chunk_size;
3670 mddev->new_layout = mddev->layout;
3671 mddev->delta_disks = 0;
3672
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return 0;
3674}
3675
NeilBrowna35b0d62006-01-06 00:20:49 -08003676static int update_size(mddev_t *mddev, unsigned long size)
3677{
3678 mdk_rdev_t * rdev;
3679 int rv;
3680 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08003681 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08003682
3683 if (mddev->pers->resize == NULL)
3684 return -EINVAL;
3685 /* The "size" is the amount of each device that is used.
3686 * This can only make sense for arrays with redundancy.
3687 * linear and raid0 always use whatever space is available
3688 * We can only consider changing the size if no resync
3689 * or reconstruction is happening, and if the new size
3690 * is acceptable. It must fit before the sb_offset or,
3691 * if that is <data_offset, it must fit before the
3692 * size of each device.
3693 * If size is zero, we find the largest size that fits.
3694 */
3695 if (mddev->sync_thread)
3696 return -EBUSY;
3697 ITERATE_RDEV(mddev,rdev,tmp) {
3698 sector_t avail;
NeilBrowna35b0d62006-01-06 00:20:49 -08003699 if (rdev->sb_offset > rdev->data_offset)
3700 avail = (rdev->sb_offset*2) - rdev->data_offset;
3701 else
3702 avail = get_capacity(rdev->bdev->bd_disk)
3703 - rdev->data_offset;
3704 if (fit && (size == 0 || size > avail/2))
3705 size = avail/2;
3706 if (avail < ((sector_t)size << 1))
3707 return -ENOSPC;
3708 }
3709 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3710 if (!rv) {
3711 struct block_device *bdev;
3712
3713 bdev = bdget_disk(mddev->gendisk, 0);
3714 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003715 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08003716 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003717 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08003718 bdput(bdev);
3719 }
3720 }
3721 return rv;
3722}
3723
NeilBrownda943b992006-01-06 00:20:54 -08003724static int update_raid_disks(mddev_t *mddev, int raid_disks)
3725{
3726 int rv;
3727 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08003728 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08003729 return -EINVAL;
3730 if (raid_disks <= 0 ||
3731 raid_disks >= mddev->max_disks)
3732 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08003733 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08003734 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08003735 mddev->delta_disks = raid_disks - mddev->raid_disks;
3736
3737 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08003738 return rv;
3739}
3740
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742/*
3743 * update_array_info is used to change the configuration of an
3744 * on-line array.
3745 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3746 * fields in the info are checked against the array.
3747 * Any differences that cannot be handled will cause an error.
3748 * Normally, only one change can be managed at a time.
3749 */
3750static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3751{
3752 int rv = 0;
3753 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07003754 int state = 0;
3755
3756 /* calculate expected state,ignoring low bits */
3757 if (mddev->bitmap && mddev->bitmap_offset)
3758 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
3760 if (mddev->major_version != info->major_version ||
3761 mddev->minor_version != info->minor_version ||
3762/* mddev->patch_version != info->patch_version || */
3763 mddev->ctime != info->ctime ||
3764 mddev->level != info->level ||
3765/* mddev->layout != info->layout || */
3766 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07003767 mddev->chunk_size != info->chunk_size ||
3768 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3769 ((state^info->state) & 0xfffffe00)
3770 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return -EINVAL;
3772 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08003773 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 if (mddev->raid_disks != info->raid_disks) cnt++;
3775 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07003776 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 if (cnt == 0) return 0;
3778 if (cnt > 1) return -EINVAL;
3779
3780 if (mddev->layout != info->layout) {
3781 /* Change layout
3782 * we don't need to do anything at the md level, the
3783 * personality will take care of it all.
3784 */
3785 if (mddev->pers->reconfig == NULL)
3786 return -EINVAL;
3787 else
3788 return mddev->pers->reconfig(mddev, info->layout, -1);
3789 }
NeilBrown284ae7c2006-02-03 03:03:40 -08003790 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08003791 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
NeilBrownda943b992006-01-06 00:20:54 -08003793 if (mddev->raid_disks != info->raid_disks)
3794 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
NeilBrown36fa3062005-09-09 16:23:45 -07003796 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3797 if (mddev->pers->quiesce == NULL)
3798 return -EINVAL;
3799 if (mddev->recovery || mddev->sync_thread)
3800 return -EBUSY;
3801 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3802 /* add the bitmap */
3803 if (mddev->bitmap)
3804 return -EEXIST;
3805 if (mddev->default_bitmap_offset == 0)
3806 return -EINVAL;
3807 mddev->bitmap_offset = mddev->default_bitmap_offset;
3808 mddev->pers->quiesce(mddev, 1);
3809 rv = bitmap_create(mddev);
3810 if (rv)
3811 bitmap_destroy(mddev);
3812 mddev->pers->quiesce(mddev, 0);
3813 } else {
3814 /* remove the bitmap */
3815 if (!mddev->bitmap)
3816 return -ENOENT;
3817 if (mddev->bitmap->file)
3818 return -EINVAL;
3819 mddev->pers->quiesce(mddev, 1);
3820 bitmap_destroy(mddev);
3821 mddev->pers->quiesce(mddev, 0);
3822 mddev->bitmap_offset = 0;
3823 }
3824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 md_update_sb(mddev);
3826 return rv;
3827}
3828
3829static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3830{
3831 mdk_rdev_t *rdev;
3832
3833 if (mddev->pers == NULL)
3834 return -ENODEV;
3835
3836 rdev = find_rdev(mddev, dev);
3837 if (!rdev)
3838 return -ENODEV;
3839
3840 md_error(mddev, rdev);
3841 return 0;
3842}
3843
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003844static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3845{
3846 mddev_t *mddev = bdev->bd_disk->private_data;
3847
3848 geo->heads = 2;
3849 geo->sectors = 4;
3850 geo->cylinders = get_capacity(mddev->gendisk) / 8;
3851 return 0;
3852}
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854static int md_ioctl(struct inode *inode, struct file *file,
3855 unsigned int cmd, unsigned long arg)
3856{
3857 int err = 0;
3858 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 mddev_t *mddev = NULL;
3860
3861 if (!capable(CAP_SYS_ADMIN))
3862 return -EACCES;
3863
3864 /*
3865 * Commands dealing with the RAID driver but not any
3866 * particular array:
3867 */
3868 switch (cmd)
3869 {
3870 case RAID_VERSION:
3871 err = get_version(argp);
3872 goto done;
3873
3874 case PRINT_RAID_DEBUG:
3875 err = 0;
3876 md_print_devices();
3877 goto done;
3878
3879#ifndef MODULE
3880 case RAID_AUTORUN:
3881 err = 0;
3882 autostart_arrays(arg);
3883 goto done;
3884#endif
3885 default:;
3886 }
3887
3888 /*
3889 * Commands creating/starting a new array:
3890 */
3891
3892 mddev = inode->i_bdev->bd_disk->private_data;
3893
3894 if (!mddev) {
3895 BUG();
3896 goto abort;
3897 }
3898
3899
3900 if (cmd == START_ARRAY) {
3901 /* START_ARRAY doesn't need to lock the array as autostart_array
3902 * does the locking, and it could even be a different array
3903 */
3904 static int cnt = 3;
3905 if (cnt > 0 ) {
3906 printk(KERN_WARNING
3907 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003908 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 current->comm, current->pid);
3910 cnt--;
3911 }
3912 err = autostart_array(new_decode_dev(arg));
3913 if (err) {
3914 printk(KERN_WARNING "md: autostart failed!\n");
3915 goto abort;
3916 }
3917 goto done;
3918 }
3919
3920 err = mddev_lock(mddev);
3921 if (err) {
3922 printk(KERN_INFO
3923 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3924 err, cmd);
3925 goto abort;
3926 }
3927
3928 switch (cmd)
3929 {
3930 case SET_ARRAY_INFO:
3931 {
3932 mdu_array_info_t info;
3933 if (!arg)
3934 memset(&info, 0, sizeof(info));
3935 else if (copy_from_user(&info, argp, sizeof(info))) {
3936 err = -EFAULT;
3937 goto abort_unlock;
3938 }
3939 if (mddev->pers) {
3940 err = update_array_info(mddev, &info);
3941 if (err) {
3942 printk(KERN_WARNING "md: couldn't update"
3943 " array info. %d\n", err);
3944 goto abort_unlock;
3945 }
3946 goto done_unlock;
3947 }
3948 if (!list_empty(&mddev->disks)) {
3949 printk(KERN_WARNING
3950 "md: array %s already has disks!\n",
3951 mdname(mddev));
3952 err = -EBUSY;
3953 goto abort_unlock;
3954 }
3955 if (mddev->raid_disks) {
3956 printk(KERN_WARNING
3957 "md: array %s already initialised!\n",
3958 mdname(mddev));
3959 err = -EBUSY;
3960 goto abort_unlock;
3961 }
3962 err = set_array_info(mddev, &info);
3963 if (err) {
3964 printk(KERN_WARNING "md: couldn't set"
3965 " array info. %d\n", err);
3966 goto abort_unlock;
3967 }
3968 }
3969 goto done_unlock;
3970
3971 default:;
3972 }
3973
3974 /*
3975 * Commands querying/configuring an existing array:
3976 */
NeilBrown32a76272005-06-21 17:17:14 -07003977 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3978 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3979 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3980 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 err = -ENODEV;
3982 goto abort_unlock;
3983 }
3984
3985 /*
3986 * Commands even a read-only array can execute:
3987 */
3988 switch (cmd)
3989 {
3990 case GET_ARRAY_INFO:
3991 err = get_array_info(mddev, argp);
3992 goto done_unlock;
3993
NeilBrown32a76272005-06-21 17:17:14 -07003994 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003995 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003996 goto done_unlock;
3997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 case GET_DISK_INFO:
3999 err = get_disk_info(mddev, argp);
4000 goto done_unlock;
4001
4002 case RESTART_ARRAY_RW:
4003 err = restart_array(mddev);
4004 goto done_unlock;
4005
4006 case STOP_ARRAY:
4007 err = do_md_stop (mddev, 0);
4008 goto done_unlock;
4009
4010 case STOP_ARRAY_RO:
4011 err = do_md_stop (mddev, 1);
4012 goto done_unlock;
4013
4014 /*
4015 * We have a problem here : there is no easy way to give a CHS
4016 * virtual geometry. We currently pretend that we have a 2 heads
4017 * 4 sectors (with a BIG number of cylinders...). This drives
4018 * dosfs just mad... ;-)
4019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 }
4021
4022 /*
4023 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004024 * superblock, so we do not allow them on read-only arrays.
4025 * However non-MD ioctls (e.g. get-size) will still come through
4026 * here and hit the 'default' below, so only disallow
4027 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 */
NeilBrownf91de922005-11-08 21:39:36 -08004029 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4030 mddev->ro && mddev->pers) {
4031 if (mddev->ro == 2) {
4032 mddev->ro = 0;
4033 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4034 md_wakeup_thread(mddev->thread);
4035
4036 } else {
4037 err = -EROFS;
4038 goto abort_unlock;
4039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
4041
4042 switch (cmd)
4043 {
4044 case ADD_NEW_DISK:
4045 {
4046 mdu_disk_info_t info;
4047 if (copy_from_user(&info, argp, sizeof(info)))
4048 err = -EFAULT;
4049 else
4050 err = add_new_disk(mddev, &info);
4051 goto done_unlock;
4052 }
4053
4054 case HOT_REMOVE_DISK:
4055 err = hot_remove_disk(mddev, new_decode_dev(arg));
4056 goto done_unlock;
4057
4058 case HOT_ADD_DISK:
4059 err = hot_add_disk(mddev, new_decode_dev(arg));
4060 goto done_unlock;
4061
4062 case SET_DISK_FAULTY:
4063 err = set_disk_faulty(mddev, new_decode_dev(arg));
4064 goto done_unlock;
4065
4066 case RUN_ARRAY:
4067 err = do_md_run (mddev);
4068 goto done_unlock;
4069
NeilBrown32a76272005-06-21 17:17:14 -07004070 case SET_BITMAP_FILE:
4071 err = set_bitmap_file(mddev, (int)arg);
4072 goto done_unlock;
4073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 err = -EINVAL;
4076 goto abort_unlock;
4077 }
4078
4079done_unlock:
4080abort_unlock:
4081 mddev_unlock(mddev);
4082
4083 return err;
4084done:
4085 if (err)
4086 MD_BUG();
4087abort:
4088 return err;
4089}
4090
4091static int md_open(struct inode *inode, struct file *file)
4092{
4093 /*
4094 * Succeed if we can lock the mddev, which confirms that
4095 * it isn't being stopped right now.
4096 */
4097 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4098 int err;
4099
4100 if ((err = mddev_lock(mddev)))
4101 goto out;
4102
4103 err = 0;
4104 mddev_get(mddev);
4105 mddev_unlock(mddev);
4106
4107 check_disk_change(inode->i_bdev);
4108 out:
4109 return err;
4110}
4111
4112static int md_release(struct inode *inode, struct file * file)
4113{
4114 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4115
4116 if (!mddev)
4117 BUG();
4118 mddev_put(mddev);
4119
4120 return 0;
4121}
4122
4123static int md_media_changed(struct gendisk *disk)
4124{
4125 mddev_t *mddev = disk->private_data;
4126
4127 return mddev->changed;
4128}
4129
4130static int md_revalidate(struct gendisk *disk)
4131{
4132 mddev_t *mddev = disk->private_data;
4133
4134 mddev->changed = 0;
4135 return 0;
4136}
4137static struct block_device_operations md_fops =
4138{
4139 .owner = THIS_MODULE,
4140 .open = md_open,
4141 .release = md_release,
4142 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004143 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 .media_changed = md_media_changed,
4145 .revalidate_disk= md_revalidate,
4146};
4147
Adrian Bunk75c96f82005-05-05 16:16:09 -07004148static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149{
4150 mdk_thread_t *thread = arg;
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 /*
4153 * md_thread is a 'system-thread', it's priority should be very
4154 * high. We avoid resource deadlocks individually in each
4155 * raid personality. (RAID5 does preallocation) We also use RR and
4156 * the very same RT priority as kswapd, thus we will never get
4157 * into a priority inversion deadlock.
4158 *
4159 * we definitely have to have equal or higher priority than
4160 * bdflush, otherwise bdflush will deadlock if there are too
4161 * many dirty RAID5 blocks.
4162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
NeilBrown6985c432005-10-19 21:23:47 -07004164 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004165 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
NeilBrown93588e22005-11-15 00:09:12 -08004167 /* We need to wait INTERRUPTIBLE so that
4168 * we don't add to the load-average.
4169 * That means we need to be sure no signals are
4170 * pending
4171 */
4172 if (signal_pending(current))
4173 flush_signals(current);
4174
4175 wait_event_interruptible_timeout
4176 (thread->wqueue,
4177 test_bit(THREAD_WAKEUP, &thread->flags)
4178 || kthread_should_stop(),
4179 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004180 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
4182 clear_bit(THREAD_WAKEUP, &thread->flags);
4183
NeilBrown787453c2005-11-08 21:39:43 -08004184 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 return 0;
4188}
4189
4190void md_wakeup_thread(mdk_thread_t *thread)
4191{
4192 if (thread) {
4193 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4194 set_bit(THREAD_WAKEUP, &thread->flags);
4195 wake_up(&thread->wqueue);
4196 }
4197}
4198
4199mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4200 const char *name)
4201{
4202 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
NeilBrown9ffae0c2006-01-06 00:20:32 -08004204 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 if (!thread)
4206 return NULL;
4207
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 init_waitqueue_head(&thread->wqueue);
4209
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 thread->run = run;
4211 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004212 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004213 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004214 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 kfree(thread);
4216 return NULL;
4217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 return thread;
4219}
4220
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221void md_unregister_thread(mdk_thread_t *thread)
4222{
NeilBrownd28446f2005-04-16 15:26:41 -07004223 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004224
4225 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 kfree(thread);
4227}
4228
4229void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4230{
4231 if (!mddev) {
4232 MD_BUG();
4233 return;
4234 }
4235
NeilBrownb2d444d2005-11-08 21:39:31 -08004236 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 return;
NeilBrown32a76272005-06-21 17:17:14 -07004238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4240 mdname(mddev),
4241 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4242 __builtin_return_address(0),__builtin_return_address(1),
4243 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004244*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 if (!mddev->pers->error_handler)
4246 return;
4247 mddev->pers->error_handler(mddev,rdev);
4248 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4249 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4250 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004251 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
4254/* seq_file implementation /proc/mdstat */
4255
4256static void status_unused(struct seq_file *seq)
4257{
4258 int i = 0;
4259 mdk_rdev_t *rdev;
4260 struct list_head *tmp;
4261
4262 seq_printf(seq, "unused devices: ");
4263
4264 ITERATE_RDEV_PENDING(rdev,tmp) {
4265 char b[BDEVNAME_SIZE];
4266 i++;
4267 seq_printf(seq, "%s ",
4268 bdevname(rdev->bdev,b));
4269 }
4270 if (!i)
4271 seq_printf(seq, "<none>");
4272
4273 seq_printf(seq, "\n");
4274}
4275
4276
4277static void status_resync(struct seq_file *seq, mddev_t * mddev)
4278{
NeilBrown4588b422006-03-27 01:18:04 -08004279 sector_t max_blocks, resync, res;
4280 unsigned long dt, db, rt;
4281 int scale;
4282 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283
4284 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4285
4286 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4287 max_blocks = mddev->resync_max_sectors >> 1;
4288 else
4289 max_blocks = mddev->size;
4290
4291 /*
4292 * Should not happen.
4293 */
4294 if (!max_blocks) {
4295 MD_BUG();
4296 return;
4297 }
NeilBrown4588b422006-03-27 01:18:04 -08004298 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4299 * in a sector_t, and (max_blocks>>scale) will fit in a
4300 * u32, as those are the requirements for sector_div.
4301 * Thus 'scale' must be at least 10
4302 */
4303 scale = 10;
4304 if (sizeof(sector_t) > sizeof(unsigned long)) {
4305 while ( max_blocks/2 > (1ULL<<(scale+32)))
4306 scale++;
4307 }
4308 res = (resync>>scale)*1000;
4309 sector_div(res, (u32)((max_blocks>>scale)+1));
4310
4311 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 {
NeilBrown4588b422006-03-27 01:18:04 -08004313 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 seq_printf(seq, "[");
4315 for (i = 0; i < x; i++)
4316 seq_printf(seq, "=");
4317 seq_printf(seq, ">");
4318 for (i = 0; i < y; i++)
4319 seq_printf(seq, ".");
4320 seq_printf(seq, "] ");
4321 }
NeilBrown4588b422006-03-27 01:18:04 -08004322 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004323 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4324 "reshape" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
NeilBrownccfcc3c2006-03-27 01:18:09 -08004326 "resync" : "recovery")),
NeilBrown4588b422006-03-27 01:18:04 -08004327 per_milli/10, per_milli % 10,
4328 (unsigned long long) resync,
4329 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 /*
4332 * We do not want to overflow, so the order of operands and
4333 * the * 100 / 100 trick are important. We do a +1 to be
4334 * safe against division by zero. We only estimate anyway.
4335 *
4336 * dt: time from mark until now
4337 * db: blocks written from mark until now
4338 * rt: remaining time
4339 */
4340 dt = ((jiffies - mddev->resync_mark) / HZ);
4341 if (!dt) dt++;
4342 db = resync - (mddev->resync_mark_cnt/2);
NeilBrown4588b422006-03-27 01:18:04 -08004343 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
4345 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4346
4347 seq_printf(seq, " speed=%ldK/sec", db/dt);
4348}
4349
4350static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4351{
4352 struct list_head *tmp;
4353 loff_t l = *pos;
4354 mddev_t *mddev;
4355
4356 if (l >= 0x10000)
4357 return NULL;
4358 if (!l--)
4359 /* header */
4360 return (void*)1;
4361
4362 spin_lock(&all_mddevs_lock);
4363 list_for_each(tmp,&all_mddevs)
4364 if (!l--) {
4365 mddev = list_entry(tmp, mddev_t, all_mddevs);
4366 mddev_get(mddev);
4367 spin_unlock(&all_mddevs_lock);
4368 return mddev;
4369 }
4370 spin_unlock(&all_mddevs_lock);
4371 if (!l--)
4372 return (void*)2;/* tail */
4373 return NULL;
4374}
4375
4376static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4377{
4378 struct list_head *tmp;
4379 mddev_t *next_mddev, *mddev = v;
4380
4381 ++*pos;
4382 if (v == (void*)2)
4383 return NULL;
4384
4385 spin_lock(&all_mddevs_lock);
4386 if (v == (void*)1)
4387 tmp = all_mddevs.next;
4388 else
4389 tmp = mddev->all_mddevs.next;
4390 if (tmp != &all_mddevs)
4391 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4392 else {
4393 next_mddev = (void*)2;
4394 *pos = 0x10000;
4395 }
4396 spin_unlock(&all_mddevs_lock);
4397
4398 if (v != (void*)1)
4399 mddev_put(mddev);
4400 return next_mddev;
4401
4402}
4403
4404static void md_seq_stop(struct seq_file *seq, void *v)
4405{
4406 mddev_t *mddev = v;
4407
4408 if (mddev && v != (void*)1 && v != (void*)2)
4409 mddev_put(mddev);
4410}
4411
NeilBrownd7603b72006-01-06 00:20:30 -08004412struct mdstat_info {
4413 int event;
4414};
4415
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416static int md_seq_show(struct seq_file *seq, void *v)
4417{
4418 mddev_t *mddev = v;
4419 sector_t size;
4420 struct list_head *tmp2;
4421 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004422 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004423 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004426 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 seq_printf(seq, "Personalities : ");
4428 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004429 list_for_each_entry(pers, &pers_list, list)
4430 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 spin_unlock(&pers_lock);
4433 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004434 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return 0;
4436 }
4437 if (v == (void*)2) {
4438 status_unused(seq);
4439 return 0;
4440 }
4441
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004442 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004444
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4446 seq_printf(seq, "%s : %sactive", mdname(mddev),
4447 mddev->pers ? "" : "in");
4448 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004449 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004451 if (mddev->ro==2)
4452 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 seq_printf(seq, " %s", mddev->pers->name);
4454 }
4455
4456 size = 0;
4457 ITERATE_RDEV(mddev,rdev,tmp2) {
4458 char b[BDEVNAME_SIZE];
4459 seq_printf(seq, " %s[%d]",
4460 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004461 if (test_bit(WriteMostly, &rdev->flags))
4462 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004463 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 seq_printf(seq, "(F)");
4465 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004466 } else if (rdev->raid_disk < 0)
4467 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 size += rdev->size;
4469 }
4470
4471 if (!list_empty(&mddev->disks)) {
4472 if (mddev->pers)
4473 seq_printf(seq, "\n %llu blocks",
4474 (unsigned long long)mddev->array_size);
4475 else
4476 seq_printf(seq, "\n %llu blocks",
4477 (unsigned long long)size);
4478 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004479 if (mddev->persistent) {
4480 if (mddev->major_version != 0 ||
4481 mddev->minor_version != 90) {
4482 seq_printf(seq," super %d.%d",
4483 mddev->major_version,
4484 mddev->minor_version);
4485 }
4486 } else
4487 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
4489 if (mddev->pers) {
4490 mddev->pers->status (seq, mddev);
4491 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004492 if (mddev->pers->sync_request) {
4493 if (mddev->curr_resync > 2) {
4494 status_resync (seq, mddev);
4495 seq_printf(seq, "\n ");
4496 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4497 seq_printf(seq, "\tresync=DELAYED\n ");
4498 else if (mddev->recovery_cp < MaxSector)
4499 seq_printf(seq, "\tresync=PENDING\n ");
4500 }
NeilBrown32a76272005-06-21 17:17:14 -07004501 } else
4502 seq_printf(seq, "\n ");
4503
4504 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004505 unsigned long chunk_kb;
4506 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004507 spin_lock_irqsave(&bitmap->lock, flags);
4508 chunk_kb = bitmap->chunksize >> 10;
4509 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4510 "%lu%s chunk",
4511 bitmap->pages - bitmap->missing_pages,
4512 bitmap->pages,
4513 (bitmap->pages - bitmap->missing_pages)
4514 << (PAGE_SHIFT - 10),
4515 chunk_kb ? chunk_kb : bitmap->chunksize,
4516 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004517 if (bitmap->file) {
4518 seq_printf(seq, ", file: ");
4519 seq_path(seq, bitmap->file->f_vfsmnt,
4520 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004521 }
NeilBrown78d742d2005-06-21 17:17:15 -07004522
NeilBrown32a76272005-06-21 17:17:14 -07004523 seq_printf(seq, "\n");
4524 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 }
4526
4527 seq_printf(seq, "\n");
4528 }
4529 mddev_unlock(mddev);
4530
4531 return 0;
4532}
4533
4534static struct seq_operations md_seq_ops = {
4535 .start = md_seq_start,
4536 .next = md_seq_next,
4537 .stop = md_seq_stop,
4538 .show = md_seq_show,
4539};
4540
4541static int md_seq_open(struct inode *inode, struct file *file)
4542{
4543 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004544 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4545 if (mi == NULL)
4546 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
4548 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004549 if (error)
4550 kfree(mi);
4551 else {
4552 struct seq_file *p = file->private_data;
4553 p->private = mi;
4554 mi->event = atomic_read(&md_event_count);
4555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 return error;
4557}
4558
NeilBrownd7603b72006-01-06 00:20:30 -08004559static int md_seq_release(struct inode *inode, struct file *file)
4560{
4561 struct seq_file *m = file->private_data;
4562 struct mdstat_info *mi = m->private;
4563 m->private = NULL;
4564 kfree(mi);
4565 return seq_release(inode, file);
4566}
4567
4568static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4569{
4570 struct seq_file *m = filp->private_data;
4571 struct mdstat_info *mi = m->private;
4572 int mask;
4573
4574 poll_wait(filp, &md_event_waiters, wait);
4575
4576 /* always allow read */
4577 mask = POLLIN | POLLRDNORM;
4578
4579 if (mi->event != atomic_read(&md_event_count))
4580 mask |= POLLERR | POLLPRI;
4581 return mask;
4582}
4583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584static struct file_operations md_seq_fops = {
4585 .open = md_seq_open,
4586 .read = seq_read,
4587 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004588 .release = md_seq_release,
4589 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590};
4591
NeilBrown2604b702006-01-06 00:20:36 -08004592int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004595 list_add_tail(&p->list, &pers_list);
4596 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 spin_unlock(&pers_lock);
4598 return 0;
4599}
4600
NeilBrown2604b702006-01-06 00:20:36 -08004601int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
NeilBrown2604b702006-01-06 00:20:36 -08004603 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004605 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 spin_unlock(&pers_lock);
4607 return 0;
4608}
4609
4610static int is_mddev_idle(mddev_t *mddev)
4611{
4612 mdk_rdev_t * rdev;
4613 struct list_head *tmp;
4614 int idle;
4615 unsigned long curr_events;
4616
4617 idle = 1;
4618 ITERATE_RDEV(mddev,rdev,tmp) {
4619 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004620 curr_events = disk_stat_read(disk, sectors[0]) +
4621 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004623 /* The difference between curr_events and last_events
4624 * will be affected by any new non-sync IO (making
4625 * curr_events bigger) and any difference in the amount of
4626 * in-flight syncio (making current_events bigger or smaller)
4627 * The amount in-flight is currently limited to
4628 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4629 * which is at most 4096 sectors.
4630 * These numbers are fairly fragile and should be made
4631 * more robust, probably by enforcing the
4632 * 'window size' that md_do_sync sort-of uses.
4633 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 * Note: the following is an unsigned comparison.
4635 */
NeilBrownc0e48522005-11-18 01:11:01 -08004636 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 rdev->last_events = curr_events;
4638 idle = 0;
4639 }
4640 }
4641 return idle;
4642}
4643
4644void md_done_sync(mddev_t *mddev, int blocks, int ok)
4645{
4646 /* another "blocks" (512byte) blocks have been synced */
4647 atomic_sub(blocks, &mddev->recovery_active);
4648 wake_up(&mddev->recovery_wait);
4649 if (!ok) {
4650 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4651 md_wakeup_thread(mddev->thread);
4652 // stop recovery, signal do_sync ....
4653 }
4654}
4655
4656
NeilBrown06d91a52005-06-21 17:17:12 -07004657/* md_write_start(mddev, bi)
4658 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004659 * in superblock) before writing, schedule a superblock update
4660 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07004661 */
NeilBrown3d310eb2005-06-21 17:17:26 -07004662void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663{
NeilBrown06d91a52005-06-21 17:17:12 -07004664 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07004665 return;
NeilBrown06d91a52005-06-21 17:17:12 -07004666
NeilBrownf91de922005-11-08 21:39:36 -08004667 BUG_ON(mddev->ro == 1);
4668 if (mddev->ro == 2) {
4669 /* need to switch to read/write */
4670 mddev->ro = 0;
4671 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4672 md_wakeup_thread(mddev->thread);
4673 }
NeilBrown06d91a52005-06-21 17:17:12 -07004674 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07004675 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08004676 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07004677 if (mddev->in_sync) {
4678 mddev->in_sync = 0;
4679 mddev->sb_dirty = 1;
4680 md_wakeup_thread(mddev->thread);
4681 }
NeilBrowna9701a32005-11-08 21:39:34 -08004682 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07004683 }
NeilBrown3d310eb2005-06-21 17:17:26 -07004684 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
4687void md_write_end(mddev_t *mddev)
4688{
4689 if (atomic_dec_and_test(&mddev->writes_pending)) {
4690 if (mddev->safemode == 2)
4691 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07004692 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4694 }
4695}
4696
Adrian Bunk75c96f82005-05-05 16:16:09 -07004697static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
4699#define SYNC_MARKS 10
4700#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08004701void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702{
4703 mddev_t *mddev2;
4704 unsigned int currspeed = 0,
4705 window;
NeilBrown57afd892005-06-21 17:17:13 -07004706 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 unsigned long mark[SYNC_MARKS];
4708 sector_t mark_cnt[SYNC_MARKS];
4709 int last_mark,m;
4710 struct list_head *tmp;
4711 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07004712 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004713 struct list_head *rtmp;
4714 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715
4716 /* just incase thread restarts... */
4717 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4718 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004719 if (mddev->ro) /* never try to sync a read-only array */
4720 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
4722 /* we overload curr_resync somewhat here.
4723 * 0 == not engaged in resync at all
4724 * 2 == checking that there is no conflict with another sync
4725 * 1 == like 2, but have yielded to allow conflicting resync to
4726 * commense
4727 * other == active in resync - this many blocks
4728 *
4729 * Before starting a resync we must have set curr_resync to
4730 * 2, and then checked that every "conflicting" array has curr_resync
4731 * less than ours. When we find one that is the same or higher
4732 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4733 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4734 * This will mean we have to start checking from the beginning again.
4735 *
4736 */
4737
4738 do {
4739 mddev->curr_resync = 2;
4740
4741 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08004742 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07004743 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 goto skip;
4745 }
4746 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 if (mddev2 == mddev)
4748 continue;
4749 if (mddev2->curr_resync &&
4750 match_mddev_units(mddev,mddev2)) {
4751 DEFINE_WAIT(wq);
4752 if (mddev < mddev2 && mddev->curr_resync == 2) {
4753 /* arbitrarily yield */
4754 mddev->curr_resync = 1;
4755 wake_up(&resync_wait);
4756 }
4757 if (mddev > mddev2 && mddev->curr_resync == 1)
4758 /* no need to wait here, we can wait the next
4759 * time 'round when curr_resync == 2
4760 */
4761 continue;
NeilBrown787453c2005-11-08 21:39:43 -08004762 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4763 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07004764 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 printk(KERN_INFO "md: delaying resync of %s"
4766 " until %s has finished resync (they"
4767 " share one or more physical units)\n",
4768 mdname(mddev), mdname(mddev2));
4769 mddev_put(mddev2);
4770 schedule();
4771 finish_wait(&resync_wait, &wq);
4772 goto try_again;
4773 }
4774 finish_wait(&resync_wait, &wq);
4775 }
4776 }
4777 } while (mddev->curr_resync < 2);
4778
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004779 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08004780 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004782 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 */
4784 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004785 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004786 /* we don't use the checkpoint if there's a bitmap */
4787 if (!mddev->bitmap &&
4788 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
4789 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004790 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4791 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004792 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 /* recovery follows the physical size of devices */
4794 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004795 j = MaxSector;
4796 ITERATE_RDEV(mddev,rdev,rtmp)
4797 if (rdev->raid_disk >= 0 &&
4798 !test_bit(Faulty, &rdev->flags) &&
4799 !test_bit(In_sync, &rdev->flags) &&
4800 rdev->recovery_offset < j)
4801 j = rdev->recovery_offset;
4802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
4804 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4805 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
NeilBrown88202a02006-01-06 00:21:36 -08004806 " %d KB/sec/disc.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07004807 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 "(but not more than %d KB/sec) for reconstruction.\n",
NeilBrown88202a02006-01-06 00:21:36 -08004809 speed_max(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810
4811 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004812
NeilBrown57afd892005-06-21 17:17:13 -07004813 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 for (m = 0; m < SYNC_MARKS; m++) {
4815 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004816 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817 }
4818 last_mark = 0;
4819 mddev->resync_mark = mark[last_mark];
4820 mddev->resync_mark_cnt = mark_cnt[last_mark];
4821
4822 /*
4823 * Tune reconstruction:
4824 */
4825 window = 32*(PAGE_SIZE/512);
4826 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4827 window/2,(unsigned long long) max_sectors/2);
4828
4829 atomic_set(&mddev->recovery_active, 0);
4830 init_waitqueue_head(&mddev->recovery_wait);
4831 last_check = 0;
4832
4833 if (j>2) {
4834 printk(KERN_INFO
4835 "md: resuming recovery of %s from checkpoint.\n",
4836 mdname(mddev));
4837 mddev->curr_resync = j;
4838 }
4839
4840 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004841 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842
NeilBrown57afd892005-06-21 17:17:13 -07004843 skipped = 0;
4844 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08004845 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07004846 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4848 goto out;
4849 }
NeilBrown57afd892005-06-21 17:17:13 -07004850
4851 if (!skipped) { /* actual IO requested */
4852 io_sectors += sectors;
4853 atomic_add(sectors, &mddev->recovery_active);
4854 }
4855
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 j += sectors;
4857 if (j>1) mddev->curr_resync = j;
NeilBrownd7603b72006-01-06 00:20:30 -08004858 if (last_check == 0)
4859 /* this is the earliers that rebuilt will be
4860 * visible in /proc/mdstat
4861 */
4862 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07004863
4864 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 continue;
4866
NeilBrown57afd892005-06-21 17:17:13 -07004867 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
4869 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4870 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4871 break;
4872
4873 repeat:
4874 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4875 /* step marks */
4876 int next = (last_mark+1) % SYNC_MARKS;
4877
4878 mddev->resync_mark = mark[next];
4879 mddev->resync_mark_cnt = mark_cnt[next];
4880 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004881 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 last_mark = next;
4883 }
4884
4885
NeilBrown787453c2005-11-08 21:39:43 -08004886 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 /*
4888 * got a signal, exit.
4889 */
4890 printk(KERN_INFO
4891 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4893 goto out;
4894 }
4895
4896 /*
4897 * this loop exits only if either when we are slower than
4898 * the 'hard' speed limit, or the system was IO-idle for
4899 * a jiffy.
4900 * the system might be non-idle CPU-wise, but we only care
4901 * about not overloading the IO subsystem. (things like an
4902 * e2fsck being done on the RAID array should execute fast)
4903 */
4904 mddev->queue->unplug_fn(mddev->queue);
4905 cond_resched();
4906
NeilBrown57afd892005-06-21 17:17:13 -07004907 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4908 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
NeilBrown88202a02006-01-06 00:21:36 -08004910 if (currspeed > speed_min(mddev)) {
4911 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004913 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 goto repeat;
4915 }
4916 }
4917 }
4918 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4919 /*
4920 * this also signals 'finished resyncing' to md_stop
4921 */
4922 out:
4923 mddev->queue->unplug_fn(mddev->queue);
4924
4925 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4926
4927 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004928 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
4930 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08004931 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
4932 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004933 mddev->curr_resync > 2) {
4934 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4935 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4936 if (mddev->curr_resync >= mddev->recovery_cp) {
4937 printk(KERN_INFO
4938 "md: checkpointing recovery of %s.\n",
4939 mdname(mddev));
4940 mddev->recovery_cp = mddev->curr_resync;
4941 }
4942 } else
4943 mddev->recovery_cp = MaxSector;
4944 } else {
4945 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4946 mddev->curr_resync = MaxSector;
4947 ITERATE_RDEV(mddev,rdev,rtmp)
4948 if (rdev->raid_disk >= 0 &&
4949 !test_bit(Faulty, &rdev->flags) &&
4950 !test_bit(In_sync, &rdev->flags) &&
4951 rdev->recovery_offset < mddev->curr_resync)
4952 rdev->recovery_offset = mddev->curr_resync;
4953 mddev->sb_dirty = 1;
4954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 }
4956
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 skip:
4958 mddev->curr_resync = 0;
4959 wake_up(&resync_wait);
4960 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4961 md_wakeup_thread(mddev->thread);
4962}
NeilBrown29269552006-03-27 01:18:10 -08004963EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
4965
4966/*
4967 * This routine is regularly called by all per-raid-array threads to
4968 * deal with generic issues like resync and super-block update.
4969 * Raid personalities that don't have a thread (linear/raid0) do not
4970 * need this as they never do any recovery or update the superblock.
4971 *
4972 * It does not do any resync itself, but rather "forks" off other threads
4973 * to do that as needed.
4974 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4975 * "->recovery" and create a thread at ->sync_thread.
4976 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4977 * and wakeups up this thread which will reap the thread and finish up.
4978 * This thread also removes any faulty devices (with nr_pending == 0).
4979 *
4980 * The overall approach is:
4981 * 1/ if the superblock needs updating, update it.
4982 * 2/ If a recovery thread is running, don't do anything else.
4983 * 3/ If recovery has finished, clean up, possibly marking spares active.
4984 * 4/ If there are any faulty devices, remove them.
4985 * 5/ If array is degraded, try to add spares devices
4986 * 6/ If array has spares or is not in-sync, start a resync thread.
4987 */
4988void md_check_recovery(mddev_t *mddev)
4989{
4990 mdk_rdev_t *rdev;
4991 struct list_head *rtmp;
4992
4993
NeilBrown5f404022005-06-21 17:17:16 -07004994 if (mddev->bitmap)
4995 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
4997 if (mddev->ro)
4998 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004999
5000 if (signal_pending(current)) {
5001 if (mddev->pers->sync_request) {
5002 printk(KERN_INFO "md: %s in immediate safe mode\n",
5003 mdname(mddev));
5004 mddev->safemode = 2;
5005 }
5006 flush_signals(current);
5007 }
5008
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 if ( ! (
5010 mddev->sb_dirty ||
5011 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005012 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5013 (mddev->safemode == 1) ||
5014 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5015 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 ))
5017 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005018
NeilBrowndf5b89b2006-03-27 01:18:20 -08005019 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07005021
NeilBrowna9701a32005-11-08 21:39:34 -08005022 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005023 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5024 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5025 mddev->in_sync = 1;
5026 mddev->sb_dirty = 1;
5027 }
5028 if (mddev->safemode == 1)
5029 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005030 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 if (mddev->sb_dirty)
5033 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07005034
NeilBrown06d91a52005-06-21 17:17:12 -07005035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5037 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5038 /* resync/recovery still happening */
5039 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5040 goto unlock;
5041 }
5042 if (mddev->sync_thread) {
5043 /* resync has finished, collect result */
5044 md_unregister_thread(mddev->sync_thread);
5045 mddev->sync_thread = NULL;
5046 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5047 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5048 /* success...*/
5049 /* activate any spares */
5050 mddev->pers->spare_active(mddev);
5051 }
5052 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07005053
5054 /* if array is no-longer degraded, then any saved_raid_disk
5055 * information must be scrapped
5056 */
5057 if (!mddev->degraded)
5058 ITERATE_RDEV(mddev,rdev,rtmp)
5059 rdev->saved_raid_disk = -1;
5060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 mddev->recovery = 0;
5062 /* flag recovery needed just to double check */
5063 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005064 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 goto unlock;
5066 }
NeilBrown24dd4692005-11-08 21:39:26 -08005067 /* Clear some bits that don't mean anything, but
5068 * might be left set
5069 */
5070 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5071 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5072 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5073 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005075 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5076 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 /* no recovery is running.
5078 * remove any failed drives, then
5079 * add spares if possible.
5080 * Spare are also removed and re-added, to allow
5081 * the personality to fail the re-add.
5082 */
5083 ITERATE_RDEV(mddev,rdev,rtmp)
5084 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005085 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08005087 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5088 char nm[20];
5089 sprintf(nm,"rd%d", rdev->raid_disk);
5090 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 }
5094
5095 if (mddev->degraded) {
5096 ITERATE_RDEV(mddev,rdev,rtmp)
5097 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08005098 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005099 rdev->recovery_offset = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005100 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5101 char nm[20];
5102 sprintf(nm, "rd%d", rdev->raid_disk);
5103 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08005105 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08005106 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 break;
5108 }
5109 }
5110
NeilBrown24dd4692005-11-08 21:39:26 -08005111 if (spares) {
5112 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5113 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5114 } else if (mddev->recovery_cp < MaxSector) {
5115 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5116 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5117 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 if (mddev->pers->sync_request) {
5121 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005122 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5123 /* We are adding a device or devices to an array
5124 * which has the bitmap stored on all devices.
5125 * So make sure all bitmap pages get written
5126 */
5127 bitmap_write_all(mddev->bitmap);
5128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 mddev->sync_thread = md_register_thread(md_do_sync,
5130 mddev,
5131 "%s_resync");
5132 if (!mddev->sync_thread) {
5133 printk(KERN_ERR "%s: could not start resync"
5134 " thread...\n",
5135 mdname(mddev));
5136 /* leave the spares where they are, it shouldn't hurt */
5137 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005138 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005140 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
5142 unlock:
5143 mddev_unlock(mddev);
5144 }
5145}
5146
Adrian Bunk75c96f82005-05-05 16:16:09 -07005147static int md_notify_reboot(struct notifier_block *this,
5148 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149{
5150 struct list_head *tmp;
5151 mddev_t *mddev;
5152
5153 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5154
5155 printk(KERN_INFO "md: stopping all md devices.\n");
5156
5157 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005158 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005160 mddev_unlock(mddev);
5161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 /*
5163 * certain more exotic SCSI devices are known to be
5164 * volatile wrt too early system reboots. While the
5165 * right place to handle this issue is the given
5166 * driver, we do want to have a safe RAID driver ...
5167 */
5168 mdelay(1000*1);
5169 }
5170 return NOTIFY_DONE;
5171}
5172
Adrian Bunk75c96f82005-05-05 16:16:09 -07005173static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 .notifier_call = md_notify_reboot,
5175 .next = NULL,
5176 .priority = INT_MAX, /* before any real devices */
5177};
5178
5179static void md_geninit(void)
5180{
5181 struct proc_dir_entry *p;
5182
5183 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5184
5185 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5186 if (p)
5187 p->proc_fops = &md_seq_fops;
5188}
5189
Adrian Bunk75c96f82005-05-05 16:16:09 -07005190static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191{
5192 int minor;
5193
5194 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5195 " MD_SB_DISKS=%d\n",
5196 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5197 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08005198 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07005199 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200
5201 if (register_blkdev(MAJOR_NR, "md"))
5202 return -1;
5203 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5204 unregister_blkdev(MAJOR_NR, "md");
5205 return -1;
5206 }
5207 devfs_mk_dir("md");
5208 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5209 md_probe, NULL, NULL);
5210 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5211 md_probe, NULL, NULL);
5212
5213 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5214 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
5215 S_IFBLK|S_IRUSR|S_IWUSR,
5216 "md/%d", minor);
5217
5218 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5219 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
5220 S_IFBLK|S_IRUSR|S_IWUSR,
5221 "md/mdp%d", minor);
5222
5223
5224 register_reboot_notifier(&md_notifier);
5225 raid_table_header = register_sysctl_table(raid_root_table, 1);
5226
5227 md_geninit();
5228 return (0);
5229}
5230
5231
5232#ifndef MODULE
5233
5234/*
5235 * Searches all registered partitions for autorun RAID arrays
5236 * at boot time.
5237 */
5238static dev_t detected_devices[128];
5239static int dev_cnt;
5240
5241void md_autodetect_dev(dev_t dev)
5242{
5243 if (dev_cnt >= 0 && dev_cnt < 127)
5244 detected_devices[dev_cnt++] = dev;
5245}
5246
5247
5248static void autostart_arrays(int part)
5249{
5250 mdk_rdev_t *rdev;
5251 int i;
5252
5253 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5254
5255 for (i = 0; i < dev_cnt; i++) {
5256 dev_t dev = detected_devices[i];
5257
5258 rdev = md_import_device(dev,0, 0);
5259 if (IS_ERR(rdev))
5260 continue;
5261
NeilBrownb2d444d2005-11-08 21:39:31 -08005262 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 MD_BUG();
5264 continue;
5265 }
5266 list_add(&rdev->same_set, &pending_raid_disks);
5267 }
5268 dev_cnt = 0;
5269
5270 autorun_devices(part);
5271}
5272
5273#endif
5274
5275static __exit void md_exit(void)
5276{
5277 mddev_t *mddev;
5278 struct list_head *tmp;
5279 int i;
5280 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5281 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5282 for (i=0; i < MAX_MD_DEVS; i++)
5283 devfs_remove("md/%d", i);
5284 for (i=0; i < MAX_MD_DEVS; i++)
5285 devfs_remove("md/d%d", i);
5286
5287 devfs_remove("md");
5288
5289 unregister_blkdev(MAJOR_NR,"md");
5290 unregister_blkdev(mdp_major, "mdp");
5291 unregister_reboot_notifier(&md_notifier);
5292 unregister_sysctl_table(raid_table_header);
5293 remove_proc_entry("mdstat", NULL);
5294 ITERATE_MDDEV(mddev,tmp) {
5295 struct gendisk *disk = mddev->gendisk;
5296 if (!disk)
5297 continue;
5298 export_array(mddev);
5299 del_gendisk(disk);
5300 put_disk(disk);
5301 mddev->gendisk = NULL;
5302 mddev_put(mddev);
5303 }
5304}
5305
5306module_init(md_init)
5307module_exit(md_exit)
5308
NeilBrownf91de922005-11-08 21:39:36 -08005309static int get_ro(char *buffer, struct kernel_param *kp)
5310{
5311 return sprintf(buffer, "%d", start_readonly);
5312}
5313static int set_ro(const char *val, struct kernel_param *kp)
5314{
5315 char *e;
5316 int num = simple_strtoul(val, &e, 10);
5317 if (*val && (*e == '\0' || *e == '\n')) {
5318 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005319 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005320 }
5321 return -EINVAL;
5322}
5323
5324module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005325module_param(start_dirty_degraded, int, 0644);
5326
NeilBrownf91de922005-11-08 21:39:36 -08005327
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328EXPORT_SYMBOL(register_md_personality);
5329EXPORT_SYMBOL(unregister_md_personality);
5330EXPORT_SYMBOL(md_error);
5331EXPORT_SYMBOL(md_done_sync);
5332EXPORT_SYMBOL(md_write_start);
5333EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334EXPORT_SYMBOL(md_register_thread);
5335EXPORT_SYMBOL(md_unregister_thread);
5336EXPORT_SYMBOL(md_wakeup_thread);
5337EXPORT_SYMBOL(md_print_devices);
5338EXPORT_SYMBOL(md_check_recovery);
5339MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005340MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005341MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);