blob: 71d46eb2c43870b94b1080872028608f311e7624 [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
NeilBrown6cce3b232006-01-06 00:20:16 -0800791 && mddev->level != 10) {
NeilBrowna654b9d82005-06-21 17:17:27 -0700792 /* FIXME use a better test */
NeilBrown6cce3b232006-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 ) {
NeilBrown6cce3b232006-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:
NeilBrownb2d444d2005-11-08 21:39:31 -08001178 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 rdev->raid_disk = role;
1180 break;
1181 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001182 if (sb->devflags & WriteMostly1)
1183 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001184 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001185 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
1188}
1189
1190static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1191{
1192 struct mdp_superblock_1 *sb;
1193 struct list_head *tmp;
1194 mdk_rdev_t *rdev2;
1195 int max_dev, i;
1196 /* make rdev->sb match mddev and rdev data. */
1197
1198 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1199
1200 sb->feature_map = 0;
1201 sb->pad0 = 0;
1202 memset(sb->pad1, 0, sizeof(sb->pad1));
1203 memset(sb->pad2, 0, sizeof(sb->pad2));
1204 memset(sb->pad3, 0, sizeof(sb->pad3));
1205
1206 sb->utime = cpu_to_le64((__u64)mddev->utime);
1207 sb->events = cpu_to_le64(mddev->events);
1208 if (mddev->in_sync)
1209 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1210 else
1211 sb->resync_offset = cpu_to_le64(0);
1212
NeilBrown4dbcdc72006-01-06 00:20:52 -08001213 sb->cnt_corrected_read = atomic_read(&rdev->corrected_errors);
1214
NeilBrownf0ca3402006-02-02 14:28:04 -08001215 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001216 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001217
NeilBrowna654b9d82005-06-21 17:17:27 -07001218 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1219 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001220 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001221 }
NeilBrownf6705572006-03-27 01:18:11 -08001222 if (mddev->reshape_position != MaxSector) {
1223 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1224 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1225 sb->new_layout = cpu_to_le32(mddev->new_layout);
1226 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1227 sb->new_level = cpu_to_le32(mddev->new_level);
1228 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1229 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 max_dev = 0;
1232 ITERATE_RDEV(mddev,rdev2,tmp)
1233 if (rdev2->desc_nr+1 > max_dev)
1234 max_dev = rdev2->desc_nr+1;
1235
1236 sb->max_dev = cpu_to_le32(max_dev);
1237 for (i=0; i<max_dev;i++)
1238 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1239
1240 ITERATE_RDEV(mddev,rdev2,tmp) {
1241 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001242 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001244 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
1246 else
1247 sb->dev_roles[i] = cpu_to_le16(0xffff);
1248 }
1249
1250 sb->recovery_offset = cpu_to_le64(0); /* not supported yet */
1251 sb->sb_csum = calc_sb_1_csum(sb);
1252}
1253
1254
Adrian Bunk75c96f82005-05-05 16:16:09 -07001255static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 [0] = {
1257 .name = "0.90.0",
1258 .owner = THIS_MODULE,
1259 .load_super = super_90_load,
1260 .validate_super = super_90_validate,
1261 .sync_super = super_90_sync,
1262 },
1263 [1] = {
1264 .name = "md-1",
1265 .owner = THIS_MODULE,
1266 .load_super = super_1_load,
1267 .validate_super = super_1_validate,
1268 .sync_super = super_1_sync,
1269 },
1270};
1271
1272static mdk_rdev_t * match_dev_unit(mddev_t *mddev, mdk_rdev_t *dev)
1273{
1274 struct list_head *tmp;
1275 mdk_rdev_t *rdev;
1276
1277 ITERATE_RDEV(mddev,rdev,tmp)
1278 if (rdev->bdev->bd_contains == dev->bdev->bd_contains)
1279 return rdev;
1280
1281 return NULL;
1282}
1283
1284static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1285{
1286 struct list_head *tmp;
1287 mdk_rdev_t *rdev;
1288
1289 ITERATE_RDEV(mddev1,rdev,tmp)
1290 if (match_dev_unit(mddev2, rdev))
1291 return 1;
1292
1293 return 0;
1294}
1295
1296static LIST_HEAD(pending_raid_disks);
1297
1298static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1299{
1300 mdk_rdev_t *same_pdev;
1301 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001302 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001303 char *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if (rdev->mddev) {
1306 MD_BUG();
1307 return -EINVAL;
1308 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001309 /* make sure rdev->size exceeds mddev->size */
1310 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1311 if (mddev->pers)
1312 /* Cannot change size, so fail */
1313 return -ENOSPC;
1314 else
1315 mddev->size = rdev->size;
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 same_pdev = match_dev_unit(mddev, rdev);
1318 if (same_pdev)
1319 printk(KERN_WARNING
1320 "%s: WARNING: %s appears to be on the same physical"
1321 " disk as %s. True\n protection against single-disk"
1322 " failure might be compromised.\n",
1323 mdname(mddev), bdevname(rdev->bdev,b),
1324 bdevname(same_pdev->bdev,b2));
1325
1326 /* Verify rdev->desc_nr is unique.
1327 * If it is -1, assign a free number, else
1328 * check number is not in use
1329 */
1330 if (rdev->desc_nr < 0) {
1331 int choice = 0;
1332 if (mddev->pers) choice = mddev->raid_disks;
1333 while (find_rdev_nr(mddev, choice))
1334 choice++;
1335 rdev->desc_nr = choice;
1336 } else {
1337 if (find_rdev_nr(mddev, rdev->desc_nr))
1338 return -EBUSY;
1339 }
NeilBrown19133a42005-11-08 21:39:35 -08001340 bdevname(rdev->bdev,b);
1341 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1342 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001343 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1344 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 list_add(&rdev->same_set, &mddev->disks);
1347 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001348 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001349
NeilBrown9c791972005-11-08 21:39:28 -08001350 rdev->kobj.parent = &mddev->kobj;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001351 kobject_add(&rdev->kobj);
1352
NeilBrownf637b9f2005-11-08 21:39:37 -08001353 if (rdev->bdev->bd_part)
1354 ko = &rdev->bdev->bd_part->kobj;
1355 else
1356 ko = &rdev->bdev->bd_disk->kobj;
1357 sysfs_create_link(&rdev->kobj, ko, "block");
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001358 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 return 0;
1360}
1361
1362static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1363{
1364 char b[BDEVNAME_SIZE];
1365 if (!rdev->mddev) {
1366 MD_BUG();
1367 return;
1368 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001369 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 list_del_init(&rdev->same_set);
1371 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1372 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001373 sysfs_remove_link(&rdev->kobj, "block");
1374 kobject_del(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377/*
1378 * prevent the device from being mounted, repartitioned or
1379 * otherwise reused by a RAID array (or any other kernel
1380 * subsystem), by bd_claiming the device.
1381 */
1382static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1383{
1384 int err = 0;
1385 struct block_device *bdev;
1386 char b[BDEVNAME_SIZE];
1387
1388 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
1389 if (IS_ERR(bdev)) {
1390 printk(KERN_ERR "md: could not open %s.\n",
1391 __bdevname(dev, b));
1392 return PTR_ERR(bdev);
1393 }
1394 err = bd_claim(bdev, rdev);
1395 if (err) {
1396 printk(KERN_ERR "md: could not bd_claim %s.\n",
1397 bdevname(bdev, b));
1398 blkdev_put(bdev);
1399 return err;
1400 }
1401 rdev->bdev = bdev;
1402 return err;
1403}
1404
1405static void unlock_rdev(mdk_rdev_t *rdev)
1406{
1407 struct block_device *bdev = rdev->bdev;
1408 rdev->bdev = NULL;
1409 if (!bdev)
1410 MD_BUG();
1411 bd_release(bdev);
1412 blkdev_put(bdev);
1413}
1414
1415void md_autodetect_dev(dev_t dev);
1416
1417static void export_rdev(mdk_rdev_t * rdev)
1418{
1419 char b[BDEVNAME_SIZE];
1420 printk(KERN_INFO "md: export_rdev(%s)\n",
1421 bdevname(rdev->bdev,b));
1422 if (rdev->mddev)
1423 MD_BUG();
1424 free_disk_sb(rdev);
1425 list_del_init(&rdev->same_set);
1426#ifndef MODULE
1427 md_autodetect_dev(rdev->bdev->bd_dev);
1428#endif
1429 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001430 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431}
1432
1433static void kick_rdev_from_array(mdk_rdev_t * rdev)
1434{
1435 unbind_rdev_from_array(rdev);
1436 export_rdev(rdev);
1437}
1438
1439static void export_array(mddev_t *mddev)
1440{
1441 struct list_head *tmp;
1442 mdk_rdev_t *rdev;
1443
1444 ITERATE_RDEV(mddev,rdev,tmp) {
1445 if (!rdev->mddev) {
1446 MD_BUG();
1447 continue;
1448 }
1449 kick_rdev_from_array(rdev);
1450 }
1451 if (!list_empty(&mddev->disks))
1452 MD_BUG();
1453 mddev->raid_disks = 0;
1454 mddev->major_version = 0;
1455}
1456
1457static void print_desc(mdp_disk_t *desc)
1458{
1459 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1460 desc->major,desc->minor,desc->raid_disk,desc->state);
1461}
1462
1463static void print_sb(mdp_super_t *sb)
1464{
1465 int i;
1466
1467 printk(KERN_INFO
1468 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1469 sb->major_version, sb->minor_version, sb->patch_version,
1470 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1471 sb->ctime);
1472 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1473 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1474 sb->md_minor, sb->layout, sb->chunk_size);
1475 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1476 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1477 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1478 sb->failed_disks, sb->spare_disks,
1479 sb->sb_csum, (unsigned long)sb->events_lo);
1480
1481 printk(KERN_INFO);
1482 for (i = 0; i < MD_SB_DISKS; i++) {
1483 mdp_disk_t *desc;
1484
1485 desc = sb->disks + i;
1486 if (desc->number || desc->major || desc->minor ||
1487 desc->raid_disk || (desc->state && (desc->state != 4))) {
1488 printk(" D %2d: ", i);
1489 print_desc(desc);
1490 }
1491 }
1492 printk(KERN_INFO "md: THIS: ");
1493 print_desc(&sb->this_disk);
1494
1495}
1496
1497static void print_rdev(mdk_rdev_t *rdev)
1498{
1499 char b[BDEVNAME_SIZE];
1500 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1501 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001502 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1503 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (rdev->sb_loaded) {
1505 printk(KERN_INFO "md: rdev superblock:\n");
1506 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1507 } else
1508 printk(KERN_INFO "md: no rdev superblock!\n");
1509}
1510
1511void md_print_devices(void)
1512{
1513 struct list_head *tmp, *tmp2;
1514 mdk_rdev_t *rdev;
1515 mddev_t *mddev;
1516 char b[BDEVNAME_SIZE];
1517
1518 printk("\n");
1519 printk("md: **********************************\n");
1520 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1521 printk("md: **********************************\n");
1522 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
NeilBrown32a76272005-06-21 17:17:14 -07001524 if (mddev->bitmap)
1525 bitmap_print_sb(mddev->bitmap);
1526 else
1527 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 ITERATE_RDEV(mddev,rdev,tmp2)
1529 printk("<%s>", bdevname(rdev->bdev,b));
1530 printk("\n");
1531
1532 ITERATE_RDEV(mddev,rdev,tmp2)
1533 print_rdev(rdev);
1534 }
1535 printk("md: **********************************\n");
1536 printk("\n");
1537}
1538
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540static void sync_sbs(mddev_t * mddev)
1541{
1542 mdk_rdev_t *rdev;
1543 struct list_head *tmp;
1544
1545 ITERATE_RDEV(mddev,rdev,tmp) {
1546 super_types[mddev->major_version].
1547 sync_super(mddev, rdev);
1548 rdev->sb_loaded = 1;
1549 }
1550}
1551
NeilBrownf6705572006-03-27 01:18:11 -08001552void md_update_sb(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001554 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 struct list_head *tmp;
1556 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001557 int sync_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001560 spin_lock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001561 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 mddev->utime = get_seconds();
1563 mddev->events ++;
1564
1565 if (!mddev->events) {
1566 /*
1567 * oops, this 64-bit counter should never wrap.
1568 * Either we are in around ~1 trillion A.C., assuming
1569 * 1 reboot per second, or we have a bug:
1570 */
1571 MD_BUG();
1572 mddev->events --;
1573 }
NeilBrown7bfa19f2005-06-21 17:17:28 -07001574 mddev->sb_dirty = 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 sync_sbs(mddev);
1576
1577 /*
1578 * do not write anything to disk if using
1579 * nonpersistent superblocks
1580 */
NeilBrown06d91a52005-06-21 17:17:12 -07001581 if (!mddev->persistent) {
1582 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001583 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001584 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001586 }
NeilBrowna9701a32005-11-08 21:39:34 -08001587 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 dprintk(KERN_INFO
1590 "md: updating %s RAID superblock on device (in sync %d)\n",
1591 mdname(mddev),mddev->in_sync);
1592
NeilBrown32a76272005-06-21 17:17:14 -07001593 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 ITERATE_RDEV(mddev,rdev,tmp) {
1595 char b[BDEVNAME_SIZE];
1596 dprintk(KERN_INFO "md: ");
NeilBrownb2d444d2005-11-08 21:39:31 -08001597 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 dprintk("(skipping faulty ");
1599
1600 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001601 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001602 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001603 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001604 rdev->sb_page);
1605 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1606 bdevname(rdev->bdev,b),
1607 (unsigned long long)rdev->sb_offset);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 } else
1610 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001611 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /* only need to write one superblock... */
1613 break;
1614 }
NeilBrowna9701a32005-11-08 21:39:34 -08001615 md_super_wait(mddev);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001616 /* if there was a failure, sb_dirty was set to 1, and we re-write super */
1617
NeilBrowna9701a32005-11-08 21:39:34 -08001618 spin_lock_irq(&mddev->write_lock);
NeilBrown7bfa19f2005-06-21 17:17:28 -07001619 if (mddev->in_sync != sync_req|| mddev->sb_dirty == 1) {
NeilBrown06d91a52005-06-21 17:17:12 -07001620 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001621 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001622 goto repeat;
1623 }
1624 mddev->sb_dirty = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08001625 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001626 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
NeilBrownf6705572006-03-27 01:18:11 -08001629EXPORT_SYMBOL_GPL(md_update_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
NeilBrownbce74da2006-01-06 00:20:41 -08001631/* words written to sysfs files may, or my not, be \n terminated.
1632 * We want to accept with case. For this we use cmd_match.
1633 */
1634static int cmd_match(const char *cmd, const char *str)
1635{
1636 /* See if cmd, written into a sysfs file, matches
1637 * str. They must either be the same, or cmd can
1638 * have a trailing newline
1639 */
1640 while (*cmd && *str && *cmd == *str) {
1641 cmd++;
1642 str++;
1643 }
1644 if (*cmd == '\n')
1645 cmd++;
1646 if (*str || *cmd)
1647 return 0;
1648 return 1;
1649}
1650
NeilBrown86e6ffd2005-11-08 21:39:24 -08001651struct rdev_sysfs_entry {
1652 struct attribute attr;
1653 ssize_t (*show)(mdk_rdev_t *, char *);
1654 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1655};
1656
1657static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001658state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001659{
1660 char *sep = "";
1661 int len=0;
1662
NeilBrownb2d444d2005-11-08 21:39:31 -08001663 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001664 len+= sprintf(page+len, "%sfaulty",sep);
1665 sep = ",";
1666 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001667 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001668 len += sprintf(page+len, "%sin_sync",sep);
1669 sep = ",";
1670 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001671 if (!test_bit(Faulty, &rdev->flags) &&
1672 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001673 len += sprintf(page+len, "%sspare", sep);
1674 sep = ",";
1675 }
1676 return len+sprintf(page+len, "\n");
1677}
1678
NeilBrown96de1e62005-11-08 21:39:39 -08001679static struct rdev_sysfs_entry
1680rdev_state = __ATTR_RO(state);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001681
1682static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001683super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001684{
1685 if (rdev->sb_loaded && rdev->sb_size) {
1686 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1687 return rdev->sb_size;
1688 } else
1689 return 0;
1690}
NeilBrown96de1e62005-11-08 21:39:39 -08001691static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1692
NeilBrown4dbcdc72006-01-06 00:20:52 -08001693static ssize_t
1694errors_show(mdk_rdev_t *rdev, char *page)
1695{
1696 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1697}
1698
1699static ssize_t
1700errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1701{
1702 char *e;
1703 unsigned long n = simple_strtoul(buf, &e, 10);
1704 if (*buf && (*e == 0 || *e == '\n')) {
1705 atomic_set(&rdev->corrected_errors, n);
1706 return len;
1707 }
1708 return -EINVAL;
1709}
1710static struct rdev_sysfs_entry rdev_errors =
1711__ATTR(errors, 0644, errors_show, errors_store);
1712
NeilBrown014236d2006-01-06 00:20:55 -08001713static ssize_t
1714slot_show(mdk_rdev_t *rdev, char *page)
1715{
1716 if (rdev->raid_disk < 0)
1717 return sprintf(page, "none\n");
1718 else
1719 return sprintf(page, "%d\n", rdev->raid_disk);
1720}
1721
1722static ssize_t
1723slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1724{
1725 char *e;
1726 int slot = simple_strtoul(buf, &e, 10);
1727 if (strncmp(buf, "none", 4)==0)
1728 slot = -1;
1729 else if (e==buf || (*e && *e!= '\n'))
1730 return -EINVAL;
1731 if (rdev->mddev->pers)
1732 /* Cannot set slot in active array (yet) */
1733 return -EBUSY;
1734 if (slot >= rdev->mddev->raid_disks)
1735 return -ENOSPC;
1736 rdev->raid_disk = slot;
1737 /* assume it is working */
1738 rdev->flags = 0;
1739 set_bit(In_sync, &rdev->flags);
1740 return len;
1741}
1742
1743
1744static struct rdev_sysfs_entry rdev_slot =
1745__ATTR(slot, 0644, slot_show, slot_store);
1746
NeilBrown93c8cad2006-01-06 00:20:56 -08001747static ssize_t
1748offset_show(mdk_rdev_t *rdev, char *page)
1749{
Andrew Morton6961ece2006-01-06 00:20:59 -08001750 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001751}
1752
1753static ssize_t
1754offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1755{
1756 char *e;
1757 unsigned long long offset = simple_strtoull(buf, &e, 10);
1758 if (e==buf || (*e && *e != '\n'))
1759 return -EINVAL;
1760 if (rdev->mddev->pers)
1761 return -EBUSY;
1762 rdev->data_offset = offset;
1763 return len;
1764}
1765
1766static struct rdev_sysfs_entry rdev_offset =
1767__ATTR(offset, 0644, offset_show, offset_store);
1768
NeilBrown83303b62006-01-06 00:21:06 -08001769static ssize_t
1770rdev_size_show(mdk_rdev_t *rdev, char *page)
1771{
1772 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1773}
1774
1775static ssize_t
1776rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1777{
1778 char *e;
1779 unsigned long long size = simple_strtoull(buf, &e, 10);
1780 if (e==buf || (*e && *e != '\n'))
1781 return -EINVAL;
1782 if (rdev->mddev->pers)
1783 return -EBUSY;
1784 rdev->size = size;
1785 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1786 rdev->mddev->size = size;
1787 return len;
1788}
1789
1790static struct rdev_sysfs_entry rdev_size =
1791__ATTR(size, 0644, rdev_size_show, rdev_size_store);
1792
NeilBrown86e6ffd2005-11-08 21:39:24 -08001793static struct attribute *rdev_default_attrs[] = {
1794 &rdev_state.attr,
1795 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001796 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001797 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001798 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001799 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001800 NULL,
1801};
1802static ssize_t
1803rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1804{
1805 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1806 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1807
1808 if (!entry->show)
1809 return -EIO;
1810 return entry->show(rdev, page);
1811}
1812
1813static ssize_t
1814rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1815 const char *page, size_t length)
1816{
1817 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1818 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1819
1820 if (!entry->store)
1821 return -EIO;
1822 return entry->store(rdev, page, length);
1823}
1824
1825static void rdev_free(struct kobject *ko)
1826{
1827 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
1828 kfree(rdev);
1829}
1830static struct sysfs_ops rdev_sysfs_ops = {
1831 .show = rdev_attr_show,
1832 .store = rdev_attr_store,
1833};
1834static struct kobj_type rdev_ktype = {
1835 .release = rdev_free,
1836 .sysfs_ops = &rdev_sysfs_ops,
1837 .default_attrs = rdev_default_attrs,
1838};
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840/*
1841 * Import a device. If 'super_format' >= 0, then sanity check the superblock
1842 *
1843 * mark the device faulty if:
1844 *
1845 * - the device is nonexistent (zero size)
1846 * - the device has no valid superblock
1847 *
1848 * a faulty rdev _never_ has rdev->sb set.
1849 */
1850static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
1851{
1852 char b[BDEVNAME_SIZE];
1853 int err;
1854 mdk_rdev_t *rdev;
1855 sector_t size;
1856
NeilBrown9ffae0c2006-01-06 00:20:32 -08001857 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (!rdev) {
1859 printk(KERN_ERR "md: could not alloc mem for new device!\n");
1860 return ERR_PTR(-ENOMEM);
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 if ((err = alloc_disk_sb(rdev)))
1864 goto abort_free;
1865
1866 err = lock_rdev(rdev, newdev);
1867 if (err)
1868 goto abort_free;
1869
NeilBrown86e6ffd2005-11-08 21:39:24 -08001870 rdev->kobj.parent = NULL;
1871 rdev->kobj.ktype = &rdev_ktype;
1872 kobject_init(&rdev->kobj);
1873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 rdev->desc_nr = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001875 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 rdev->data_offset = 0;
1877 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08001878 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001879 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
1882 if (!size) {
1883 printk(KERN_WARNING
1884 "md: %s has zero or unknown size, marking faulty!\n",
1885 bdevname(rdev->bdev,b));
1886 err = -EINVAL;
1887 goto abort_free;
1888 }
1889
1890 if (super_format >= 0) {
1891 err = super_types[super_format].
1892 load_super(rdev, NULL, super_minor);
1893 if (err == -EINVAL) {
1894 printk(KERN_WARNING
1895 "md: %s has invalid sb, not importing!\n",
1896 bdevname(rdev->bdev,b));
1897 goto abort_free;
1898 }
1899 if (err < 0) {
1900 printk(KERN_WARNING
1901 "md: could not read %s's sb, not importing!\n",
1902 bdevname(rdev->bdev,b));
1903 goto abort_free;
1904 }
1905 }
1906 INIT_LIST_HEAD(&rdev->same_set);
1907
1908 return rdev;
1909
1910abort_free:
1911 if (rdev->sb_page) {
1912 if (rdev->bdev)
1913 unlock_rdev(rdev);
1914 free_disk_sb(rdev);
1915 }
1916 kfree(rdev);
1917 return ERR_PTR(err);
1918}
1919
1920/*
1921 * Check a full RAID array for plausibility
1922 */
1923
1924
NeilBrowna757e642005-04-16 15:26:42 -07001925static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 int i;
1928 struct list_head *tmp;
1929 mdk_rdev_t *rdev, *freshest;
1930 char b[BDEVNAME_SIZE];
1931
1932 freshest = NULL;
1933 ITERATE_RDEV(mddev,rdev,tmp)
1934 switch (super_types[mddev->major_version].
1935 load_super(rdev, freshest, mddev->minor_version)) {
1936 case 1:
1937 freshest = rdev;
1938 break;
1939 case 0:
1940 break;
1941 default:
1942 printk( KERN_ERR \
1943 "md: fatal superblock inconsistency in %s"
1944 " -- removing from array\n",
1945 bdevname(rdev->bdev,b));
1946 kick_rdev_from_array(rdev);
1947 }
1948
1949
1950 super_types[mddev->major_version].
1951 validate_super(mddev, freshest);
1952
1953 i = 0;
1954 ITERATE_RDEV(mddev,rdev,tmp) {
1955 if (rdev != freshest)
1956 if (super_types[mddev->major_version].
1957 validate_super(mddev, rdev)) {
1958 printk(KERN_WARNING "md: kicking non-fresh %s"
1959 " from array!\n",
1960 bdevname(rdev->bdev,b));
1961 kick_rdev_from_array(rdev);
1962 continue;
1963 }
1964 if (mddev->level == LEVEL_MULTIPATH) {
1965 rdev->desc_nr = i++;
1966 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001967 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
1969 }
1970
1971
1972
1973 if (mddev->recovery_cp != MaxSector &&
1974 mddev->level >= 1)
1975 printk(KERN_ERR "md: %s: raid array is not clean"
1976 " -- starting background reconstruction\n",
1977 mdname(mddev));
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
NeilBrowneae17012005-11-08 21:39:23 -08001981static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07001982safe_delay_show(mddev_t *mddev, char *page)
1983{
1984 int msec = (mddev->safemode_delay*1000)/HZ;
1985 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
1986}
1987static ssize_t
1988safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
1989{
1990 int scale=1;
1991 int dot=0;
1992 int i;
1993 unsigned long msec;
1994 char buf[30];
1995 char *e;
1996 /* remove a period, and count digits after it */
1997 if (len >= sizeof(buf))
1998 return -EINVAL;
1999 strlcpy(buf, cbuf, len);
2000 buf[len] = 0;
2001 for (i=0; i<len; i++) {
2002 if (dot) {
2003 if (isdigit(buf[i])) {
2004 buf[i-1] = buf[i];
2005 scale *= 10;
2006 }
2007 buf[i] = 0;
2008 } else if (buf[i] == '.') {
2009 dot=1;
2010 buf[i] = 0;
2011 }
2012 }
2013 msec = simple_strtoul(buf, &e, 10);
2014 if (e == buf || (*e && *e != '\n'))
2015 return -EINVAL;
2016 msec = (msec * 1000) / scale;
2017 if (msec == 0)
2018 mddev->safemode_delay = 0;
2019 else {
2020 mddev->safemode_delay = (msec*HZ)/1000;
2021 if (mddev->safemode_delay == 0)
2022 mddev->safemode_delay = 1;
2023 }
2024 return len;
2025}
2026static struct md_sysfs_entry md_safe_delay =
2027__ATTR(safe_mode_delay, 0644,safe_delay_show, safe_delay_store);
2028
2029static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002030level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002031{
NeilBrown2604b702006-01-06 00:20:36 -08002032 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002033 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002034 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002035 else if (mddev->clevel[0])
2036 return sprintf(page, "%s\n", mddev->clevel);
2037 else if (mddev->level != LEVEL_NONE)
2038 return sprintf(page, "%d\n", mddev->level);
2039 else
2040 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002041}
2042
NeilBrownd9d166c2006-01-06 00:20:51 -08002043static ssize_t
2044level_store(mddev_t *mddev, const char *buf, size_t len)
2045{
2046 int rv = len;
2047 if (mddev->pers)
2048 return -EBUSY;
2049 if (len == 0)
2050 return 0;
2051 if (len >= sizeof(mddev->clevel))
2052 return -ENOSPC;
2053 strncpy(mddev->clevel, buf, len);
2054 if (mddev->clevel[len-1] == '\n')
2055 len--;
2056 mddev->clevel[len] = 0;
2057 mddev->level = LEVEL_NONE;
2058 return rv;
2059}
2060
2061static struct md_sysfs_entry md_level =
2062__ATTR(level, 0644, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002063
2064static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002065raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002066{
NeilBrownbb636542005-11-08 21:39:45 -08002067 if (mddev->raid_disks == 0)
2068 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002069 return sprintf(page, "%d\n", mddev->raid_disks);
2070}
2071
NeilBrownda943b992006-01-06 00:20:54 -08002072static int update_raid_disks(mddev_t *mddev, int raid_disks);
2073
2074static ssize_t
2075raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2076{
2077 /* can only set raid_disks if array is not yet active */
2078 char *e;
2079 int rv = 0;
2080 unsigned long n = simple_strtoul(buf, &e, 10);
2081
2082 if (!*buf || (*e && *e != '\n'))
2083 return -EINVAL;
2084
2085 if (mddev->pers)
2086 rv = update_raid_disks(mddev, n);
2087 else
2088 mddev->raid_disks = n;
2089 return rv ? rv : len;
2090}
2091static struct md_sysfs_entry md_raid_disks =
2092__ATTR(raid_disks, 0644, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002093
NeilBrown24dd4692005-11-08 21:39:26 -08002094static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002095chunk_size_show(mddev_t *mddev, char *page)
2096{
2097 return sprintf(page, "%d\n", mddev->chunk_size);
2098}
2099
2100static ssize_t
2101chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2102{
2103 /* can only set chunk_size if array is not yet active */
2104 char *e;
2105 unsigned long n = simple_strtoul(buf, &e, 10);
2106
2107 if (mddev->pers)
2108 return -EBUSY;
2109 if (!*buf || (*e && *e != '\n'))
2110 return -EINVAL;
2111
2112 mddev->chunk_size = n;
2113 return len;
2114}
2115static struct md_sysfs_entry md_chunk_size =
2116__ATTR(chunk_size, 0644, chunk_size_show, chunk_size_store);
2117
NeilBrown6d7ff732006-01-06 00:21:16 -08002118static ssize_t
2119null_show(mddev_t *mddev, char *page)
2120{
2121 return -EINVAL;
2122}
2123
2124static ssize_t
2125new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2126{
2127 /* buf must be %d:%d\n? giving major and minor numbers */
2128 /* The new device is added to the array.
2129 * If the array has a persistent superblock, we read the
2130 * superblock to initialise info and check validity.
2131 * Otherwise, only checking done is that in bind_rdev_to_array,
2132 * which mainly checks size.
2133 */
2134 char *e;
2135 int major = simple_strtoul(buf, &e, 10);
2136 int minor;
2137 dev_t dev;
2138 mdk_rdev_t *rdev;
2139 int err;
2140
2141 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2142 return -EINVAL;
2143 minor = simple_strtoul(e+1, &e, 10);
2144 if (*e && *e != '\n')
2145 return -EINVAL;
2146 dev = MKDEV(major, minor);
2147 if (major != MAJOR(dev) ||
2148 minor != MINOR(dev))
2149 return -EOVERFLOW;
2150
2151
2152 if (mddev->persistent) {
2153 rdev = md_import_device(dev, mddev->major_version,
2154 mddev->minor_version);
2155 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2156 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2157 mdk_rdev_t, same_set);
2158 err = super_types[mddev->major_version]
2159 .load_super(rdev, rdev0, mddev->minor_version);
2160 if (err < 0)
2161 goto out;
2162 }
2163 } else
2164 rdev = md_import_device(dev, -1, -1);
2165
2166 if (IS_ERR(rdev))
2167 return PTR_ERR(rdev);
2168 err = bind_rdev_to_array(rdev, mddev);
2169 out:
2170 if (err)
2171 export_rdev(rdev);
2172 return err ? err : len;
2173}
2174
2175static struct md_sysfs_entry md_new_device =
2176__ATTR(new_dev, 0200, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002177
2178static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002179size_show(mddev_t *mddev, char *page)
2180{
2181 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2182}
2183
2184static int update_size(mddev_t *mddev, unsigned long size);
2185
2186static ssize_t
2187size_store(mddev_t *mddev, const char *buf, size_t len)
2188{
2189 /* If array is inactive, we can reduce the component size, but
2190 * not increase it (except from 0).
2191 * If array is active, we can try an on-line resize
2192 */
2193 char *e;
2194 int err = 0;
2195 unsigned long long size = simple_strtoull(buf, &e, 10);
2196 if (!*buf || *buf == '\n' ||
2197 (*e && *e != '\n'))
2198 return -EINVAL;
2199
2200 if (mddev->pers) {
2201 err = update_size(mddev, size);
2202 md_update_sb(mddev);
2203 } else {
2204 if (mddev->size == 0 ||
2205 mddev->size > size)
2206 mddev->size = size;
2207 else
2208 err = -ENOSPC;
2209 }
2210 return err ? err : len;
2211}
2212
2213static struct md_sysfs_entry md_size =
2214__ATTR(component_size, 0644, size_show, size_store);
2215
NeilBrown8bb93aa2006-01-06 00:20:50 -08002216
2217/* Metdata version.
2218 * This is either 'none' for arrays with externally managed metadata,
2219 * or N.M for internally known formats
2220 */
2221static ssize_t
2222metadata_show(mddev_t *mddev, char *page)
2223{
2224 if (mddev->persistent)
2225 return sprintf(page, "%d.%d\n",
2226 mddev->major_version, mddev->minor_version);
2227 else
2228 return sprintf(page, "none\n");
2229}
2230
2231static ssize_t
2232metadata_store(mddev_t *mddev, const char *buf, size_t len)
2233{
2234 int major, minor;
2235 char *e;
2236 if (!list_empty(&mddev->disks))
2237 return -EBUSY;
2238
2239 if (cmd_match(buf, "none")) {
2240 mddev->persistent = 0;
2241 mddev->major_version = 0;
2242 mddev->minor_version = 90;
2243 return len;
2244 }
2245 major = simple_strtoul(buf, &e, 10);
2246 if (e==buf || *e != '.')
2247 return -EINVAL;
2248 buf = e+1;
2249 minor = simple_strtoul(buf, &e, 10);
2250 if (e==buf || *e != '\n')
2251 return -EINVAL;
2252 if (major >= sizeof(super_types)/sizeof(super_types[0]) ||
2253 super_types[major].name == NULL)
2254 return -ENOENT;
2255 mddev->major_version = major;
2256 mddev->minor_version = minor;
2257 mddev->persistent = 1;
2258 return len;
2259}
2260
2261static struct md_sysfs_entry md_metadata =
2262__ATTR(metadata_version, 0644, metadata_show, metadata_store);
2263
NeilBrowna35b0d62006-01-06 00:20:49 -08002264static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002265action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002266{
NeilBrown7eec3142005-11-08 21:39:44 -08002267 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002268 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2269 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002270 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2271 type = "reshape";
2272 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002273 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2274 type = "resync";
2275 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2276 type = "check";
2277 else
2278 type = "repair";
2279 } else
2280 type = "recover";
2281 }
2282 return sprintf(page, "%s\n", type);
2283}
2284
2285static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002286action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002287{
NeilBrown7eec3142005-11-08 21:39:44 -08002288 if (!mddev->pers || !mddev->pers->sync_request)
2289 return -EINVAL;
2290
NeilBrownbce74da2006-01-06 00:20:41 -08002291 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002292 if (mddev->sync_thread) {
2293 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2294 md_unregister_thread(mddev->sync_thread);
2295 mddev->sync_thread = NULL;
2296 mddev->recovery = 0;
2297 }
NeilBrown03c902e2006-01-06 00:20:46 -08002298 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2299 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002300 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002301 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002302 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002303 else if (cmd_match(page, "reshape")) {
2304 int err;
2305 if (mddev->pers->start_reshape == NULL)
2306 return -EINVAL;
2307 err = mddev->pers->start_reshape(mddev);
2308 if (err)
2309 return err;
2310 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002311 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002312 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002313 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002314 return -EINVAL;
2315 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2316 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002317 }
NeilBrown03c902e2006-01-06 00:20:46 -08002318 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002319 md_wakeup_thread(mddev->thread);
2320 return len;
2321}
2322
NeilBrown9d888832005-11-08 21:39:26 -08002323static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002324mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002325{
2326 return sprintf(page, "%llu\n",
2327 (unsigned long long) mddev->resync_mismatches);
2328}
2329
NeilBrown96de1e62005-11-08 21:39:39 -08002330static struct md_sysfs_entry
NeilBrown7eec3142005-11-08 21:39:44 -08002331md_scan_mode = __ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002332
NeilBrown96de1e62005-11-08 21:39:39 -08002333
2334static struct md_sysfs_entry
2335md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002336
NeilBrown88202a02006-01-06 00:21:36 -08002337static ssize_t
2338sync_min_show(mddev_t *mddev, char *page)
2339{
2340 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2341 mddev->sync_speed_min ? "local": "system");
2342}
2343
2344static ssize_t
2345sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2346{
2347 int min;
2348 char *e;
2349 if (strncmp(buf, "system", 6)==0) {
2350 mddev->sync_speed_min = 0;
2351 return len;
2352 }
2353 min = simple_strtoul(buf, &e, 10);
2354 if (buf == e || (*e && *e != '\n') || min <= 0)
2355 return -EINVAL;
2356 mddev->sync_speed_min = min;
2357 return len;
2358}
2359
2360static struct md_sysfs_entry md_sync_min =
2361__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2362
2363static ssize_t
2364sync_max_show(mddev_t *mddev, char *page)
2365{
2366 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2367 mddev->sync_speed_max ? "local": "system");
2368}
2369
2370static ssize_t
2371sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2372{
2373 int max;
2374 char *e;
2375 if (strncmp(buf, "system", 6)==0) {
2376 mddev->sync_speed_max = 0;
2377 return len;
2378 }
2379 max = simple_strtoul(buf, &e, 10);
2380 if (buf == e || (*e && *e != '\n') || max <= 0)
2381 return -EINVAL;
2382 mddev->sync_speed_max = max;
2383 return len;
2384}
2385
2386static struct md_sysfs_entry md_sync_max =
2387__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2388
2389
2390static ssize_t
2391sync_speed_show(mddev_t *mddev, char *page)
2392{
2393 unsigned long resync, dt, db;
2394 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2395 dt = ((jiffies - mddev->resync_mark) / HZ);
2396 if (!dt) dt++;
2397 db = resync - (mddev->resync_mark_cnt);
2398 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2399}
2400
2401static struct md_sysfs_entry
2402md_sync_speed = __ATTR_RO(sync_speed);
2403
2404static ssize_t
2405sync_completed_show(mddev_t *mddev, char *page)
2406{
2407 unsigned long max_blocks, resync;
2408
2409 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2410 max_blocks = mddev->resync_max_sectors;
2411 else
2412 max_blocks = mddev->size << 1;
2413
2414 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2415 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2416}
2417
2418static struct md_sysfs_entry
2419md_sync_completed = __ATTR_RO(sync_completed);
2420
NeilBrowne464eaf2006-03-27 01:18:14 -08002421static ssize_t
2422suspend_lo_show(mddev_t *mddev, char *page)
2423{
2424 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2425}
2426
2427static ssize_t
2428suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2429{
2430 char *e;
2431 unsigned long long new = simple_strtoull(buf, &e, 10);
2432
2433 if (mddev->pers->quiesce == NULL)
2434 return -EINVAL;
2435 if (buf == e || (*e && *e != '\n'))
2436 return -EINVAL;
2437 if (new >= mddev->suspend_hi ||
2438 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2439 mddev->suspend_lo = new;
2440 mddev->pers->quiesce(mddev, 2);
2441 return len;
2442 } else
2443 return -EINVAL;
2444}
2445static struct md_sysfs_entry md_suspend_lo =
2446__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2447
2448
2449static ssize_t
2450suspend_hi_show(mddev_t *mddev, char *page)
2451{
2452 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2453}
2454
2455static ssize_t
2456suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2457{
2458 char *e;
2459 unsigned long long new = simple_strtoull(buf, &e, 10);
2460
2461 if (mddev->pers->quiesce == NULL)
2462 return -EINVAL;
2463 if (buf == e || (*e && *e != '\n'))
2464 return -EINVAL;
2465 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2466 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2467 mddev->suspend_hi = new;
2468 mddev->pers->quiesce(mddev, 1);
2469 mddev->pers->quiesce(mddev, 0);
2470 return len;
2471 } else
2472 return -EINVAL;
2473}
2474static struct md_sysfs_entry md_suspend_hi =
2475__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2476
2477
NeilBrowneae17012005-11-08 21:39:23 -08002478static struct attribute *md_default_attrs[] = {
2479 &md_level.attr,
2480 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002481 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002482 &md_size.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002483 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002484 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002485 &md_safe_delay.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002486 NULL,
2487};
2488
2489static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002490 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002491 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002492 &md_sync_min.attr,
2493 &md_sync_max.attr,
2494 &md_sync_speed.attr,
2495 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002496 &md_suspend_lo.attr,
2497 &md_suspend_hi.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002498 NULL,
2499};
NeilBrown411036f2005-11-08 21:39:40 -08002500static struct attribute_group md_redundancy_group = {
2501 .name = NULL,
2502 .attrs = md_redundancy_attrs,
2503};
2504
NeilBrowneae17012005-11-08 21:39:23 -08002505
2506static ssize_t
2507md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2508{
2509 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2510 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002511 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002512
2513 if (!entry->show)
2514 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002515 rv = mddev_lock(mddev);
2516 if (!rv) {
2517 rv = entry->show(mddev, page);
2518 mddev_unlock(mddev);
2519 }
NeilBrown96de1e62005-11-08 21:39:39 -08002520 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002521}
2522
2523static ssize_t
2524md_attr_store(struct kobject *kobj, struct attribute *attr,
2525 const char *page, size_t length)
2526{
2527 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2528 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002529 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002530
2531 if (!entry->store)
2532 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002533 rv = mddev_lock(mddev);
2534 if (!rv) {
2535 rv = entry->store(mddev, page, length);
2536 mddev_unlock(mddev);
2537 }
NeilBrown96de1e62005-11-08 21:39:39 -08002538 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08002539}
2540
2541static void md_free(struct kobject *ko)
2542{
2543 mddev_t *mddev = container_of(ko, mddev_t, kobj);
2544 kfree(mddev);
2545}
2546
2547static struct sysfs_ops md_sysfs_ops = {
2548 .show = md_attr_show,
2549 .store = md_attr_store,
2550};
2551static struct kobj_type md_ktype = {
2552 .release = md_free,
2553 .sysfs_ops = &md_sysfs_ops,
2554 .default_attrs = md_default_attrs,
2555};
2556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557int mdp_major = 0;
2558
2559static struct kobject *md_probe(dev_t dev, int *part, void *data)
2560{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002561 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 mddev_t *mddev = mddev_find(dev);
2563 struct gendisk *disk;
2564 int partitioned = (MAJOR(dev) != MD_MAJOR);
2565 int shift = partitioned ? MdpMinorShift : 0;
2566 int unit = MINOR(dev) >> shift;
2567
2568 if (!mddev)
2569 return NULL;
2570
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002571 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002573 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 mddev_put(mddev);
2575 return NULL;
2576 }
2577 disk = alloc_disk(1 << shift);
2578 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002579 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 mddev_put(mddev);
2581 return NULL;
2582 }
2583 disk->major = MAJOR(dev);
2584 disk->first_minor = unit << shift;
2585 if (partitioned) {
2586 sprintf(disk->disk_name, "md_d%d", unit);
2587 sprintf(disk->devfs_name, "md/d%d", unit);
2588 } else {
2589 sprintf(disk->disk_name, "md%d", unit);
2590 sprintf(disk->devfs_name, "md/%d", unit);
2591 }
2592 disk->fops = &md_fops;
2593 disk->private_data = mddev;
2594 disk->queue = mddev->queue;
2595 add_disk(disk);
2596 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08002597 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08002598 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08002599 mddev->kobj.k_name = NULL;
2600 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
2601 mddev->kobj.ktype = &md_ktype;
2602 kobject_register(&mddev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return NULL;
2604}
2605
2606void md_wakeup_thread(mdk_thread_t *thread);
2607
2608static void md_safemode_timeout(unsigned long data)
2609{
2610 mddev_t *mddev = (mddev_t *) data;
2611
2612 mddev->safemode = 1;
2613 md_wakeup_thread(mddev->thread);
2614}
2615
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002616static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618static int do_md_run(mddev_t * mddev)
2619{
NeilBrown2604b702006-01-06 00:20:36 -08002620 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 int chunk_size;
2622 struct list_head *tmp;
2623 mdk_rdev_t *rdev;
2624 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08002625 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 char b[BDEVNAME_SIZE];
2627
NeilBrowna757e642005-04-16 15:26:42 -07002628 if (list_empty(&mddev->disks))
2629 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 if (mddev->pers)
2633 return -EBUSY;
2634
2635 /*
2636 * Analyze all RAID superblock(s)
2637 */
NeilBrowna757e642005-04-16 15:26:42 -07002638 if (!mddev->raid_disks)
2639 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
NeilBrown2604b702006-01-06 00:20:36 -08002643 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (chunk_size > MAX_CHUNK_SIZE) {
2645 printk(KERN_ERR "too big chunk_size: %d > %d\n",
2646 chunk_size, MAX_CHUNK_SIZE);
2647 return -EINVAL;
2648 }
2649 /*
2650 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
2651 */
2652 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07002653 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return -EINVAL;
2655 }
2656 if (chunk_size < PAGE_SIZE) {
2657 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
2658 chunk_size, PAGE_SIZE);
2659 return -EINVAL;
2660 }
2661
2662 /* devices must have minimum size of one chunk */
2663 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002664 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 continue;
2666 if (rdev->size < chunk_size / 1024) {
2667 printk(KERN_WARNING
2668 "md: Dev %s smaller than chunk_size:"
2669 " %lluk < %dk\n",
2670 bdevname(rdev->bdev,b),
2671 (unsigned long long)rdev->size,
2672 chunk_size / 1024);
2673 return -EINVAL;
2674 }
2675 }
2676 }
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08002679 if (mddev->level != LEVEL_NONE)
2680 request_module("md-level-%d", mddev->level);
2681 else if (mddev->clevel[0])
2682 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683#endif
2684
2685 /*
2686 * Drop all container device buffers, from now on
2687 * the only valid external interface is through the md
2688 * device.
2689 * Also find largest hardsector size
2690 */
2691 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08002692 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 continue;
2694 sync_blockdev(rdev->bdev);
2695 invalidate_bdev(rdev->bdev, 0);
2696 }
2697
2698 md_probe(mddev->unit, NULL, NULL);
2699 disk = mddev->gendisk;
2700 if (!disk)
2701 return -ENOMEM;
2702
2703 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002704 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08002705 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002707 if (mddev->level != LEVEL_NONE)
2708 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
2709 mddev->level);
2710 else
2711 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
2712 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return -EINVAL;
2714 }
NeilBrown2604b702006-01-06 00:20:36 -08002715 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08002717 mddev->level = pers->level;
2718 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
NeilBrownf6705572006-03-27 01:18:11 -08002720 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08002721 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08002722 /* This personality cannot handle reshaping... */
2723 mddev->pers = NULL;
2724 module_put(pers->owner);
2725 return -EINVAL;
2726 }
2727
NeilBrown657390d2005-08-26 18:34:16 -07002728 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08002730 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08002731 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
NeilBrownf91de922005-11-08 21:39:36 -08002733 if (start_readonly)
2734 mddev->ro = 2; /* read-only, but switch on first write */
2735
NeilBrownb15c2e52006-01-06 00:20:16 -08002736 err = mddev->pers->run(mddev);
2737 if (!err && mddev->pers->sync_request) {
2738 err = bitmap_create(mddev);
2739 if (err) {
2740 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
2741 mdname(mddev), err);
2742 mddev->pers->stop(mddev);
2743 }
2744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 if (err) {
2746 printk(KERN_ERR "md: pers->run() failed ...\n");
2747 module_put(mddev->pers->owner);
2748 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07002749 bitmap_destroy(mddev);
2750 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 }
NeilBrown411036f2005-11-08 21:39:40 -08002752 if (mddev->pers->sync_request)
2753 sysfs_create_group(&mddev->kobj, &md_redundancy_group);
NeilBrownfd9d49c2005-11-08 21:39:42 -08002754 else if (mddev->ro == 2) /* auto-readonly not meaningful */
2755 mddev->ro = 0;
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 atomic_set(&mddev->writes_pending,0);
2758 mddev->safemode = 0;
2759 mddev->safemode_timer.function = md_safemode_timeout;
2760 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07002761 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002763
2764 ITERATE_RDEV(mddev,rdev,tmp)
2765 if (rdev->raid_disk >= 0) {
2766 char nm[20];
2767 sprintf(nm, "rd%d", rdev->raid_disk);
2768 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07002772 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 if (mddev->sb_dirty)
2775 md_update_sb(mddev);
2776
2777 set_capacity(disk, mddev->array_size<<1);
2778
2779 /* If we call blk_queue_make_request here, it will
2780 * re-initialise max_sectors etc which may have been
2781 * refined inside -> run. So just set the bits we need to set.
2782 * Most initialisation happended when we called
2783 * blk_queue_make_request(..., md_fail_request)
2784 * earlier.
2785 */
2786 mddev->queue->queuedata = mddev;
2787 mddev->queue->make_request_fn = mddev->pers->make_request;
2788
2789 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08002790 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return 0;
2792}
2793
2794static int restart_array(mddev_t *mddev)
2795{
2796 struct gendisk *disk = mddev->gendisk;
2797 int err;
2798
2799 /*
2800 * Complain if it has no devices
2801 */
2802 err = -ENXIO;
2803 if (list_empty(&mddev->disks))
2804 goto out;
2805
2806 if (mddev->pers) {
2807 err = -EBUSY;
2808 if (!mddev->ro)
2809 goto out;
2810
2811 mddev->safemode = 0;
2812 mddev->ro = 0;
2813 set_disk_ro(disk, 0);
2814
2815 printk(KERN_INFO "md: %s switched to read-write mode.\n",
2816 mdname(mddev));
2817 /*
2818 * Kick recovery or resync if necessary
2819 */
2820 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2821 md_wakeup_thread(mddev->thread);
2822 err = 0;
2823 } else {
2824 printk(KERN_ERR "md: %s has no personality assigned.\n",
2825 mdname(mddev));
2826 err = -EINVAL;
2827 }
2828
2829out:
2830 return err;
2831}
2832
2833static int do_md_stop(mddev_t * mddev, int ro)
2834{
2835 int err = 0;
2836 struct gendisk *disk = mddev->gendisk;
2837
2838 if (mddev->pers) {
2839 if (atomic_read(&mddev->active)>2) {
2840 printk("md: %s still in use.\n",mdname(mddev));
2841 return -EBUSY;
2842 }
2843
2844 if (mddev->sync_thread) {
2845 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2846 md_unregister_thread(mddev->sync_thread);
2847 mddev->sync_thread = NULL;
2848 }
2849
2850 del_timer_sync(&mddev->safemode_timer);
2851
2852 invalidate_partition(disk, 0);
2853
2854 if (ro) {
2855 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08002856 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 goto out;
2858 mddev->ro = 1;
2859 } else {
NeilBrown6b8b3e82005-08-04 12:53:35 -07002860 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08002861 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 if (mddev->ro)
2863 set_disk_ro(disk, 0);
2864 blk_queue_make_request(mddev->queue, md_fail_request);
2865 mddev->pers->stop(mddev);
NeilBrown411036f2005-11-08 21:39:40 -08002866 if (mddev->pers->sync_request)
2867 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 module_put(mddev->pers->owner);
2870 mddev->pers = NULL;
2871 if (mddev->ro)
2872 mddev->ro = 0;
2873 }
2874 if (!mddev->in_sync) {
2875 /* mark array as shutdown cleanly */
2876 mddev->in_sync = 1;
2877 md_update_sb(mddev);
2878 }
2879 if (ro)
2880 set_disk_ro(disk, 1);
2881 }
NeilBrown32a76272005-06-21 17:17:14 -07002882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 /*
2884 * Free resources if final stop
2885 */
2886 if (!ro) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002887 mdk_rdev_t *rdev;
2888 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct gendisk *disk;
2890 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
2891
NeilBrown978f9462006-02-02 14:28:05 -08002892 bitmap_destroy(mddev);
2893 if (mddev->bitmap_file) {
2894 atomic_set(&mddev->bitmap_file->f_dentry->d_inode->i_writecount, 1);
2895 fput(mddev->bitmap_file);
2896 mddev->bitmap_file = NULL;
2897 }
2898 mddev->bitmap_offset = 0;
2899
NeilBrown86e6ffd2005-11-08 21:39:24 -08002900 ITERATE_RDEV(mddev,rdev,tmp)
2901 if (rdev->raid_disk >= 0) {
2902 char nm[20];
2903 sprintf(nm, "rd%d", rdev->raid_disk);
2904 sysfs_remove_link(&mddev->kobj, nm);
2905 }
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 export_array(mddev);
2908
2909 mddev->array_size = 0;
2910 disk = mddev->gendisk;
2911 if (disk)
2912 set_capacity(disk, 0);
2913 mddev->changed = 1;
NeilBrowna8a55c32006-06-26 00:27:39 -07002914 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 printk(KERN_INFO "md: %s switched to read-only mode.\n",
2916 mdname(mddev));
2917 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08002918 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919out:
2920 return err;
2921}
2922
2923static void autorun_array(mddev_t *mddev)
2924{
2925 mdk_rdev_t *rdev;
2926 struct list_head *tmp;
2927 int err;
2928
NeilBrowna757e642005-04-16 15:26:42 -07002929 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
2932 printk(KERN_INFO "md: running: ");
2933
2934 ITERATE_RDEV(mddev,rdev,tmp) {
2935 char b[BDEVNAME_SIZE];
2936 printk("<%s>", bdevname(rdev->bdev,b));
2937 }
2938 printk("\n");
2939
2940 err = do_md_run (mddev);
2941 if (err) {
2942 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
2943 do_md_stop (mddev, 0);
2944 }
2945}
2946
2947/*
2948 * lets try to run arrays based on all disks that have arrived
2949 * until now. (those are in pending_raid_disks)
2950 *
2951 * the method: pick the first pending disk, collect all disks with
2952 * the same UUID, remove all from the pending list and put them into
2953 * the 'same_array' list. Then order this list based on superblock
2954 * update time (freshest comes first), kick out 'old' disks and
2955 * compare superblocks. If everything's fine then run it.
2956 *
2957 * If "unit" is allocated, then bump its reference count
2958 */
2959static void autorun_devices(int part)
2960{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 struct list_head *tmp;
2962 mdk_rdev_t *rdev0, *rdev;
2963 mddev_t *mddev;
2964 char b[BDEVNAME_SIZE];
2965
2966 printk(KERN_INFO "md: autorun ...\n");
2967 while (!list_empty(&pending_raid_disks)) {
2968 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08002969 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 rdev0 = list_entry(pending_raid_disks.next,
2971 mdk_rdev_t, same_set);
2972
2973 printk(KERN_INFO "md: considering %s ...\n",
2974 bdevname(rdev0->bdev,b));
2975 INIT_LIST_HEAD(&candidates);
2976 ITERATE_RDEV_PENDING(rdev,tmp)
2977 if (super_90_load(rdev, rdev0, 0) >= 0) {
2978 printk(KERN_INFO "md: adding %s ...\n",
2979 bdevname(rdev->bdev,b));
2980 list_move(&rdev->same_set, &candidates);
2981 }
2982 /*
2983 * now we have a set of devices, with all of them having
2984 * mostly sane superblocks. It's time to allocate the
2985 * mddev.
2986 */
2987 if (rdev0->preferred_minor < 0 || rdev0->preferred_minor >= MAX_MD_DEVS) {
2988 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
2989 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
2990 break;
2991 }
2992 if (part)
2993 dev = MKDEV(mdp_major,
2994 rdev0->preferred_minor << MdpMinorShift);
2995 else
2996 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
2997
2998 md_probe(dev, NULL, NULL);
2999 mddev = mddev_find(dev);
3000 if (!mddev) {
3001 printk(KERN_ERR
3002 "md: cannot allocate memory for md drive.\n");
3003 break;
3004 }
3005 if (mddev_lock(mddev))
3006 printk(KERN_WARNING "md: %s locked, cannot run\n",
3007 mdname(mddev));
3008 else if (mddev->raid_disks || mddev->major_version
3009 || !list_empty(&mddev->disks)) {
3010 printk(KERN_WARNING
3011 "md: %s already running, cannot run %s\n",
3012 mdname(mddev), bdevname(rdev0->bdev,b));
3013 mddev_unlock(mddev);
3014 } else {
3015 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3016 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3017 list_del_init(&rdev->same_set);
3018 if (bind_rdev_to_array(rdev, mddev))
3019 export_rdev(rdev);
3020 }
3021 autorun_array(mddev);
3022 mddev_unlock(mddev);
3023 }
3024 /* on success, candidates will be empty, on error
3025 * it won't...
3026 */
3027 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3028 export_rdev(rdev);
3029 mddev_put(mddev);
3030 }
3031 printk(KERN_INFO "md: ... autorun DONE.\n");
3032}
3033
3034/*
3035 * import RAID devices based on one partition
3036 * if possible, the array gets run as well.
3037 */
3038
3039static int autostart_array(dev_t startdev)
3040{
3041 char b[BDEVNAME_SIZE];
3042 int err = -EINVAL, i;
3043 mdp_super_t *sb = NULL;
3044 mdk_rdev_t *start_rdev = NULL, *rdev;
3045
3046 start_rdev = md_import_device(startdev, 0, 0);
3047 if (IS_ERR(start_rdev))
3048 return err;
3049
3050
3051 /* NOTE: this can only work for 0.90.0 superblocks */
3052 sb = (mdp_super_t*)page_address(start_rdev->sb_page);
3053 if (sb->major_version != 0 ||
3054 sb->minor_version != 90 ) {
3055 printk(KERN_WARNING "md: can only autostart 0.90.0 arrays\n");
3056 export_rdev(start_rdev);
3057 return err;
3058 }
3059
NeilBrownb2d444d2005-11-08 21:39:31 -08003060 if (test_bit(Faulty, &start_rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 printk(KERN_WARNING
3062 "md: can not autostart based on faulty %s!\n",
3063 bdevname(start_rdev->bdev,b));
3064 export_rdev(start_rdev);
3065 return err;
3066 }
3067 list_add(&start_rdev->same_set, &pending_raid_disks);
3068
3069 for (i = 0; i < MD_SB_DISKS; i++) {
3070 mdp_disk_t *desc = sb->disks + i;
3071 dev_t dev = MKDEV(desc->major, desc->minor);
3072
3073 if (!dev)
3074 continue;
3075 if (dev == startdev)
3076 continue;
3077 if (MAJOR(dev) != desc->major || MINOR(dev) != desc->minor)
3078 continue;
3079 rdev = md_import_device(dev, 0, 0);
3080 if (IS_ERR(rdev))
3081 continue;
3082
3083 list_add(&rdev->same_set, &pending_raid_disks);
3084 }
3085
3086 /*
3087 * possibly return codes
3088 */
3089 autorun_devices(0);
3090 return 0;
3091
3092}
3093
3094
3095static int get_version(void __user * arg)
3096{
3097 mdu_version_t ver;
3098
3099 ver.major = MD_MAJOR_VERSION;
3100 ver.minor = MD_MINOR_VERSION;
3101 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3102
3103 if (copy_to_user(arg, &ver, sizeof(ver)))
3104 return -EFAULT;
3105
3106 return 0;
3107}
3108
3109static int get_array_info(mddev_t * mddev, void __user * arg)
3110{
3111 mdu_array_info_t info;
3112 int nr,working,active,failed,spare;
3113 mdk_rdev_t *rdev;
3114 struct list_head *tmp;
3115
3116 nr=working=active=failed=spare=0;
3117 ITERATE_RDEV(mddev,rdev,tmp) {
3118 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003119 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 failed++;
3121 else {
3122 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003123 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 active++;
3125 else
3126 spare++;
3127 }
3128 }
3129
3130 info.major_version = mddev->major_version;
3131 info.minor_version = mddev->minor_version;
3132 info.patch_version = MD_PATCHLEVEL_VERSION;
3133 info.ctime = mddev->ctime;
3134 info.level = mddev->level;
3135 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003136 if (info.size != mddev->size) /* overflow */
3137 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 info.nr_disks = nr;
3139 info.raid_disks = mddev->raid_disks;
3140 info.md_minor = mddev->md_minor;
3141 info.not_persistent= !mddev->persistent;
3142
3143 info.utime = mddev->utime;
3144 info.state = 0;
3145 if (mddev->in_sync)
3146 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003147 if (mddev->bitmap && mddev->bitmap_offset)
3148 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 info.active_disks = active;
3150 info.working_disks = working;
3151 info.failed_disks = failed;
3152 info.spare_disks = spare;
3153
3154 info.layout = mddev->layout;
3155 info.chunk_size = mddev->chunk_size;
3156
3157 if (copy_to_user(arg, &info, sizeof(info)))
3158 return -EFAULT;
3159
3160 return 0;
3161}
3162
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003163static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003164{
3165 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3166 char *ptr, *buf = NULL;
3167 int err = -ENOMEM;
3168
3169 file = kmalloc(sizeof(*file), GFP_KERNEL);
3170 if (!file)
3171 goto out;
3172
3173 /* bitmap disabled, zero the first byte and copy out */
3174 if (!mddev->bitmap || !mddev->bitmap->file) {
3175 file->pathname[0] = '\0';
3176 goto copy_out;
3177 }
3178
3179 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3180 if (!buf)
3181 goto out;
3182
3183 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3184 if (!ptr)
3185 goto out;
3186
3187 strcpy(file->pathname, ptr);
3188
3189copy_out:
3190 err = 0;
3191 if (copy_to_user(arg, file, sizeof(*file)))
3192 err = -EFAULT;
3193out:
3194 kfree(buf);
3195 kfree(file);
3196 return err;
3197}
3198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199static int get_disk_info(mddev_t * mddev, void __user * arg)
3200{
3201 mdu_disk_info_t info;
3202 unsigned int nr;
3203 mdk_rdev_t *rdev;
3204
3205 if (copy_from_user(&info, arg, sizeof(info)))
3206 return -EFAULT;
3207
3208 nr = info.number;
3209
3210 rdev = find_rdev_nr(mddev, nr);
3211 if (rdev) {
3212 info.major = MAJOR(rdev->bdev->bd_dev);
3213 info.minor = MINOR(rdev->bdev->bd_dev);
3214 info.raid_disk = rdev->raid_disk;
3215 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003216 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003218 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 info.state |= (1<<MD_DISK_ACTIVE);
3220 info.state |= (1<<MD_DISK_SYNC);
3221 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003222 if (test_bit(WriteMostly, &rdev->flags))
3223 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 } else {
3225 info.major = info.minor = 0;
3226 info.raid_disk = -1;
3227 info.state = (1<<MD_DISK_REMOVED);
3228 }
3229
3230 if (copy_to_user(arg, &info, sizeof(info)))
3231 return -EFAULT;
3232
3233 return 0;
3234}
3235
3236static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3237{
3238 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3239 mdk_rdev_t *rdev;
3240 dev_t dev = MKDEV(info->major,info->minor);
3241
3242 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3243 return -EOVERFLOW;
3244
3245 if (!mddev->raid_disks) {
3246 int err;
3247 /* expecting a device which has a superblock */
3248 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3249 if (IS_ERR(rdev)) {
3250 printk(KERN_WARNING
3251 "md: md_import_device returned %ld\n",
3252 PTR_ERR(rdev));
3253 return PTR_ERR(rdev);
3254 }
3255 if (!list_empty(&mddev->disks)) {
3256 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3257 mdk_rdev_t, same_set);
3258 int err = super_types[mddev->major_version]
3259 .load_super(rdev, rdev0, mddev->minor_version);
3260 if (err < 0) {
3261 printk(KERN_WARNING
3262 "md: %s has different UUID to %s\n",
3263 bdevname(rdev->bdev,b),
3264 bdevname(rdev0->bdev,b2));
3265 export_rdev(rdev);
3266 return -EINVAL;
3267 }
3268 }
3269 err = bind_rdev_to_array(rdev, mddev);
3270 if (err)
3271 export_rdev(rdev);
3272 return err;
3273 }
3274
3275 /*
3276 * add_new_disk can be used once the array is assembled
3277 * to add "hot spares". They must already have a superblock
3278 * written
3279 */
3280 if (mddev->pers) {
3281 int err;
3282 if (!mddev->pers->hot_add_disk) {
3283 printk(KERN_WARNING
3284 "%s: personality does not support diskops!\n",
3285 mdname(mddev));
3286 return -EINVAL;
3287 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003288 if (mddev->persistent)
3289 rdev = md_import_device(dev, mddev->major_version,
3290 mddev->minor_version);
3291 else
3292 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (IS_ERR(rdev)) {
3294 printk(KERN_WARNING
3295 "md: md_import_device returned %ld\n",
3296 PTR_ERR(rdev));
3297 return PTR_ERR(rdev);
3298 }
NeilBrown41158c72005-06-21 17:17:25 -07003299 /* set save_raid_disk if appropriate */
3300 if (!mddev->persistent) {
3301 if (info->state & (1<<MD_DISK_SYNC) &&
3302 info->raid_disk < mddev->raid_disks)
3303 rdev->raid_disk = info->raid_disk;
3304 else
3305 rdev->raid_disk = -1;
3306 } else
3307 super_types[mddev->major_version].
3308 validate_super(mddev, rdev);
3309 rdev->saved_raid_disk = rdev->raid_disk;
3310
NeilBrownb2d444d2005-11-08 21:39:31 -08003311 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003312 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3313 set_bit(WriteMostly, &rdev->flags);
3314
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 rdev->raid_disk = -1;
3316 err = bind_rdev_to_array(rdev, mddev);
3317 if (err)
3318 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003319
3320 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003321 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 return err;
3323 }
3324
3325 /* otherwise, add_new_disk is only allowed
3326 * for major_version==0 superblocks
3327 */
3328 if (mddev->major_version != 0) {
3329 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3330 mdname(mddev));
3331 return -EINVAL;
3332 }
3333
3334 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3335 int err;
3336 rdev = md_import_device (dev, -1, 0);
3337 if (IS_ERR(rdev)) {
3338 printk(KERN_WARNING
3339 "md: error, md_import_device() returned %ld\n",
3340 PTR_ERR(rdev));
3341 return PTR_ERR(rdev);
3342 }
3343 rdev->desc_nr = info->number;
3344 if (info->raid_disk < mddev->raid_disks)
3345 rdev->raid_disk = info->raid_disk;
3346 else
3347 rdev->raid_disk = -1;
3348
NeilBrownb2d444d2005-11-08 21:39:31 -08003349 rdev->flags = 0;
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003352 if (info->state & (1<<MD_DISK_SYNC))
3353 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003355 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3356 set_bit(WriteMostly, &rdev->flags);
3357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 if (!mddev->persistent) {
3359 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3360 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3361 } else
3362 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3363 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3364
NeilBrown2bf071b2006-01-06 00:20:55 -08003365 err = bind_rdev_to_array(rdev, mddev);
3366 if (err) {
3367 export_rdev(rdev);
3368 return err;
3369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
3371
3372 return 0;
3373}
3374
3375static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3376{
3377 char b[BDEVNAME_SIZE];
3378 mdk_rdev_t *rdev;
3379
3380 if (!mddev->pers)
3381 return -ENODEV;
3382
3383 rdev = find_rdev(mddev, dev);
3384 if (!rdev)
3385 return -ENXIO;
3386
3387 if (rdev->raid_disk >= 0)
3388 goto busy;
3389
3390 kick_rdev_from_array(rdev);
3391 md_update_sb(mddev);
NeilBrownd7603b72006-01-06 00:20:30 -08003392 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
3394 return 0;
3395busy:
3396 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3397 bdevname(rdev->bdev,b), mdname(mddev));
3398 return -EBUSY;
3399}
3400
3401static int hot_add_disk(mddev_t * mddev, dev_t dev)
3402{
3403 char b[BDEVNAME_SIZE];
3404 int err;
3405 unsigned int size;
3406 mdk_rdev_t *rdev;
3407
3408 if (!mddev->pers)
3409 return -ENODEV;
3410
3411 if (mddev->major_version != 0) {
3412 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3413 " version-0 superblocks.\n",
3414 mdname(mddev));
3415 return -EINVAL;
3416 }
3417 if (!mddev->pers->hot_add_disk) {
3418 printk(KERN_WARNING
3419 "%s: personality does not support diskops!\n",
3420 mdname(mddev));
3421 return -EINVAL;
3422 }
3423
3424 rdev = md_import_device (dev, -1, 0);
3425 if (IS_ERR(rdev)) {
3426 printk(KERN_WARNING
3427 "md: error, md_import_device() returned %ld\n",
3428 PTR_ERR(rdev));
3429 return -EINVAL;
3430 }
3431
3432 if (mddev->persistent)
3433 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3434 else
3435 rdev->sb_offset =
3436 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3437
3438 size = calc_dev_size(rdev, mddev->chunk_size);
3439 rdev->size = size;
3440
NeilBrownb2d444d2005-11-08 21:39:31 -08003441 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 printk(KERN_WARNING
3443 "md: can not hot-add faulty %s disk to %s!\n",
3444 bdevname(rdev->bdev,b), mdname(mddev));
3445 err = -EINVAL;
3446 goto abort_export;
3447 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003448 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 rdev->desc_nr = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08003450 err = bind_rdev_to_array(rdev, mddev);
3451 if (err)
3452 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
3454 /*
3455 * The rest should better be atomic, we can have disk failures
3456 * noticed in interrupt contexts ...
3457 */
3458
3459 if (rdev->desc_nr == mddev->max_disks) {
3460 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
3461 mdname(mddev));
3462 err = -EBUSY;
3463 goto abort_unbind_export;
3464 }
3465
3466 rdev->raid_disk = -1;
3467
3468 md_update_sb(mddev);
3469
3470 /*
3471 * Kick recovery, maybe this spare has to be added to the
3472 * array immediately.
3473 */
3474 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3475 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08003476 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 return 0;
3478
3479abort_unbind_export:
3480 unbind_rdev_from_array(rdev);
3481
3482abort_export:
3483 export_rdev(rdev);
3484 return err;
3485}
3486
NeilBrown32a76272005-06-21 17:17:14 -07003487/* similar to deny_write_access, but accounts for our holding a reference
3488 * to the file ourselves */
3489static int deny_bitmap_write_access(struct file * file)
3490{
3491 struct inode *inode = file->f_mapping->host;
3492
3493 spin_lock(&inode->i_lock);
3494 if (atomic_read(&inode->i_writecount) > 1) {
3495 spin_unlock(&inode->i_lock);
3496 return -ETXTBSY;
3497 }
3498 atomic_set(&inode->i_writecount, -1);
3499 spin_unlock(&inode->i_lock);
3500
3501 return 0;
3502}
3503
3504static int set_bitmap_file(mddev_t *mddev, int fd)
3505{
3506 int err;
3507
NeilBrown36fa3062005-09-09 16:23:45 -07003508 if (mddev->pers) {
3509 if (!mddev->pers->quiesce)
3510 return -EBUSY;
3511 if (mddev->recovery || mddev->sync_thread)
3512 return -EBUSY;
3513 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07003514 }
3515
NeilBrown36fa3062005-09-09 16:23:45 -07003516
3517 if (fd >= 0) {
3518 if (mddev->bitmap)
3519 return -EEXIST; /* cannot add when bitmap is present */
3520 mddev->bitmap_file = fget(fd);
3521
3522 if (mddev->bitmap_file == NULL) {
3523 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
3524 mdname(mddev));
3525 return -EBADF;
3526 }
3527
3528 err = deny_bitmap_write_access(mddev->bitmap_file);
3529 if (err) {
3530 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
3531 mdname(mddev));
3532 fput(mddev->bitmap_file);
3533 mddev->bitmap_file = NULL;
3534 return err;
3535 }
NeilBrowna654b9d82005-06-21 17:17:27 -07003536 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07003537 } else if (mddev->bitmap == NULL)
3538 return -ENOENT; /* cannot remove what isn't there */
3539 err = 0;
3540 if (mddev->pers) {
3541 mddev->pers->quiesce(mddev, 1);
3542 if (fd >= 0)
3543 err = bitmap_create(mddev);
3544 if (fd < 0 || err)
3545 bitmap_destroy(mddev);
3546 mddev->pers->quiesce(mddev, 0);
3547 } else if (fd < 0) {
3548 if (mddev->bitmap_file)
3549 fput(mddev->bitmap_file);
3550 mddev->bitmap_file = NULL;
3551 }
3552
NeilBrown32a76272005-06-21 17:17:14 -07003553 return err;
3554}
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556/*
3557 * set_array_info is used two different ways
3558 * The original usage is when creating a new array.
3559 * In this usage, raid_disks is > 0 and it together with
3560 * level, size, not_persistent,layout,chunksize determine the
3561 * shape of the array.
3562 * This will always create an array with a type-0.90.0 superblock.
3563 * The newer usage is when assembling an array.
3564 * In this case raid_disks will be 0, and the major_version field is
3565 * use to determine which style super-blocks are to be found on the devices.
3566 * The minor and patch _version numbers are also kept incase the
3567 * super_block handler wishes to interpret them.
3568 */
3569static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
3570{
3571
3572 if (info->raid_disks == 0) {
3573 /* just setting version number for superblock loading */
3574 if (info->major_version < 0 ||
3575 info->major_version >= sizeof(super_types)/sizeof(super_types[0]) ||
3576 super_types[info->major_version].name == NULL) {
3577 /* maybe try to auto-load a module? */
3578 printk(KERN_INFO
3579 "md: superblock version %d not known\n",
3580 info->major_version);
3581 return -EINVAL;
3582 }
3583 mddev->major_version = info->major_version;
3584 mddev->minor_version = info->minor_version;
3585 mddev->patch_version = info->patch_version;
3586 return 0;
3587 }
3588 mddev->major_version = MD_MAJOR_VERSION;
3589 mddev->minor_version = MD_MINOR_VERSION;
3590 mddev->patch_version = MD_PATCHLEVEL_VERSION;
3591 mddev->ctime = get_seconds();
3592
3593 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08003594 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 mddev->size = info->size;
3596 mddev->raid_disks = info->raid_disks;
3597 /* don't set md_minor, it is determined by which /dev/md* was
3598 * openned
3599 */
3600 if (info->state & (1<<MD_SB_CLEAN))
3601 mddev->recovery_cp = MaxSector;
3602 else
3603 mddev->recovery_cp = 0;
3604 mddev->persistent = ! info->not_persistent;
3605
3606 mddev->layout = info->layout;
3607 mddev->chunk_size = info->chunk_size;
3608
3609 mddev->max_disks = MD_SB_DISKS;
3610
3611 mddev->sb_dirty = 1;
3612
NeilBrownb2a27032005-11-28 13:44:12 -08003613 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
3614 mddev->bitmap_offset = 0;
3615
NeilBrownf6705572006-03-27 01:18:11 -08003616 mddev->reshape_position = MaxSector;
3617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 /*
3619 * Generate a 128 bit UUID
3620 */
3621 get_random_bytes(mddev->uuid, 16);
3622
NeilBrownf6705572006-03-27 01:18:11 -08003623 mddev->new_level = mddev->level;
3624 mddev->new_chunk = mddev->chunk_size;
3625 mddev->new_layout = mddev->layout;
3626 mddev->delta_disks = 0;
3627
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 return 0;
3629}
3630
NeilBrowna35b0d62006-01-06 00:20:49 -08003631static int update_size(mddev_t *mddev, unsigned long size)
3632{
3633 mdk_rdev_t * rdev;
3634 int rv;
3635 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08003636 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08003637
3638 if (mddev->pers->resize == NULL)
3639 return -EINVAL;
3640 /* The "size" is the amount of each device that is used.
3641 * This can only make sense for arrays with redundancy.
3642 * linear and raid0 always use whatever space is available
3643 * We can only consider changing the size if no resync
3644 * or reconstruction is happening, and if the new size
3645 * is acceptable. It must fit before the sb_offset or,
3646 * if that is <data_offset, it must fit before the
3647 * size of each device.
3648 * If size is zero, we find the largest size that fits.
3649 */
3650 if (mddev->sync_thread)
3651 return -EBUSY;
3652 ITERATE_RDEV(mddev,rdev,tmp) {
3653 sector_t avail;
NeilBrowna35b0d62006-01-06 00:20:49 -08003654 if (rdev->sb_offset > rdev->data_offset)
3655 avail = (rdev->sb_offset*2) - rdev->data_offset;
3656 else
3657 avail = get_capacity(rdev->bdev->bd_disk)
3658 - rdev->data_offset;
3659 if (fit && (size == 0 || size > avail/2))
3660 size = avail/2;
3661 if (avail < ((sector_t)size << 1))
3662 return -ENOSPC;
3663 }
3664 rv = mddev->pers->resize(mddev, (sector_t)size *2);
3665 if (!rv) {
3666 struct block_device *bdev;
3667
3668 bdev = bdget_disk(mddev->gendisk, 0);
3669 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003670 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08003671 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003672 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08003673 bdput(bdev);
3674 }
3675 }
3676 return rv;
3677}
3678
NeilBrownda943b992006-01-06 00:20:54 -08003679static int update_raid_disks(mddev_t *mddev, int raid_disks)
3680{
3681 int rv;
3682 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08003683 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08003684 return -EINVAL;
3685 if (raid_disks <= 0 ||
3686 raid_disks >= mddev->max_disks)
3687 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08003688 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08003689 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08003690 mddev->delta_disks = raid_disks - mddev->raid_disks;
3691
3692 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08003693 return rv;
3694}
3695
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697/*
3698 * update_array_info is used to change the configuration of an
3699 * on-line array.
3700 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
3701 * fields in the info are checked against the array.
3702 * Any differences that cannot be handled will cause an error.
3703 * Normally, only one change can be managed at a time.
3704 */
3705static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
3706{
3707 int rv = 0;
3708 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07003709 int state = 0;
3710
3711 /* calculate expected state,ignoring low bits */
3712 if (mddev->bitmap && mddev->bitmap_offset)
3713 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714
3715 if (mddev->major_version != info->major_version ||
3716 mddev->minor_version != info->minor_version ||
3717/* mddev->patch_version != info->patch_version || */
3718 mddev->ctime != info->ctime ||
3719 mddev->level != info->level ||
3720/* mddev->layout != info->layout || */
3721 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07003722 mddev->chunk_size != info->chunk_size ||
3723 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
3724 ((state^info->state) & 0xfffffe00)
3725 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return -EINVAL;
3727 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08003728 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 if (mddev->raid_disks != info->raid_disks) cnt++;
3730 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07003731 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 if (cnt == 0) return 0;
3733 if (cnt > 1) return -EINVAL;
3734
3735 if (mddev->layout != info->layout) {
3736 /* Change layout
3737 * we don't need to do anything at the md level, the
3738 * personality will take care of it all.
3739 */
3740 if (mddev->pers->reconfig == NULL)
3741 return -EINVAL;
3742 else
3743 return mddev->pers->reconfig(mddev, info->layout, -1);
3744 }
NeilBrown284ae7c2006-02-03 03:03:40 -08003745 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08003746 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
NeilBrownda943b992006-01-06 00:20:54 -08003748 if (mddev->raid_disks != info->raid_disks)
3749 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
NeilBrown36fa3062005-09-09 16:23:45 -07003751 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
3752 if (mddev->pers->quiesce == NULL)
3753 return -EINVAL;
3754 if (mddev->recovery || mddev->sync_thread)
3755 return -EBUSY;
3756 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
3757 /* add the bitmap */
3758 if (mddev->bitmap)
3759 return -EEXIST;
3760 if (mddev->default_bitmap_offset == 0)
3761 return -EINVAL;
3762 mddev->bitmap_offset = mddev->default_bitmap_offset;
3763 mddev->pers->quiesce(mddev, 1);
3764 rv = bitmap_create(mddev);
3765 if (rv)
3766 bitmap_destroy(mddev);
3767 mddev->pers->quiesce(mddev, 0);
3768 } else {
3769 /* remove the bitmap */
3770 if (!mddev->bitmap)
3771 return -ENOENT;
3772 if (mddev->bitmap->file)
3773 return -EINVAL;
3774 mddev->pers->quiesce(mddev, 1);
3775 bitmap_destroy(mddev);
3776 mddev->pers->quiesce(mddev, 0);
3777 mddev->bitmap_offset = 0;
3778 }
3779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 md_update_sb(mddev);
3781 return rv;
3782}
3783
3784static int set_disk_faulty(mddev_t *mddev, dev_t dev)
3785{
3786 mdk_rdev_t *rdev;
3787
3788 if (mddev->pers == NULL)
3789 return -ENODEV;
3790
3791 rdev = find_rdev(mddev, dev);
3792 if (!rdev)
3793 return -ENODEV;
3794
3795 md_error(mddev, rdev);
3796 return 0;
3797}
3798
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08003799static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3800{
3801 mddev_t *mddev = bdev->bd_disk->private_data;
3802
3803 geo->heads = 2;
3804 geo->sectors = 4;
3805 geo->cylinders = get_capacity(mddev->gendisk) / 8;
3806 return 0;
3807}
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809static int md_ioctl(struct inode *inode, struct file *file,
3810 unsigned int cmd, unsigned long arg)
3811{
3812 int err = 0;
3813 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 mddev_t *mddev = NULL;
3815
3816 if (!capable(CAP_SYS_ADMIN))
3817 return -EACCES;
3818
3819 /*
3820 * Commands dealing with the RAID driver but not any
3821 * particular array:
3822 */
3823 switch (cmd)
3824 {
3825 case RAID_VERSION:
3826 err = get_version(argp);
3827 goto done;
3828
3829 case PRINT_RAID_DEBUG:
3830 err = 0;
3831 md_print_devices();
3832 goto done;
3833
3834#ifndef MODULE
3835 case RAID_AUTORUN:
3836 err = 0;
3837 autostart_arrays(arg);
3838 goto done;
3839#endif
3840 default:;
3841 }
3842
3843 /*
3844 * Commands creating/starting a new array:
3845 */
3846
3847 mddev = inode->i_bdev->bd_disk->private_data;
3848
3849 if (!mddev) {
3850 BUG();
3851 goto abort;
3852 }
3853
3854
3855 if (cmd == START_ARRAY) {
3856 /* START_ARRAY doesn't need to lock the array as autostart_array
3857 * does the locking, and it could even be a different array
3858 */
3859 static int cnt = 3;
3860 if (cnt > 0 ) {
3861 printk(KERN_WARNING
3862 "md: %s(pid %d) used deprecated START_ARRAY ioctl. "
NeilBrowne8a00332005-11-15 00:09:11 -08003863 "This will not be supported beyond July 2006\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 current->comm, current->pid);
3865 cnt--;
3866 }
3867 err = autostart_array(new_decode_dev(arg));
3868 if (err) {
3869 printk(KERN_WARNING "md: autostart failed!\n");
3870 goto abort;
3871 }
3872 goto done;
3873 }
3874
3875 err = mddev_lock(mddev);
3876 if (err) {
3877 printk(KERN_INFO
3878 "md: ioctl lock interrupted, reason %d, cmd %d\n",
3879 err, cmd);
3880 goto abort;
3881 }
3882
3883 switch (cmd)
3884 {
3885 case SET_ARRAY_INFO:
3886 {
3887 mdu_array_info_t info;
3888 if (!arg)
3889 memset(&info, 0, sizeof(info));
3890 else if (copy_from_user(&info, argp, sizeof(info))) {
3891 err = -EFAULT;
3892 goto abort_unlock;
3893 }
3894 if (mddev->pers) {
3895 err = update_array_info(mddev, &info);
3896 if (err) {
3897 printk(KERN_WARNING "md: couldn't update"
3898 " array info. %d\n", err);
3899 goto abort_unlock;
3900 }
3901 goto done_unlock;
3902 }
3903 if (!list_empty(&mddev->disks)) {
3904 printk(KERN_WARNING
3905 "md: array %s already has disks!\n",
3906 mdname(mddev));
3907 err = -EBUSY;
3908 goto abort_unlock;
3909 }
3910 if (mddev->raid_disks) {
3911 printk(KERN_WARNING
3912 "md: array %s already initialised!\n",
3913 mdname(mddev));
3914 err = -EBUSY;
3915 goto abort_unlock;
3916 }
3917 err = set_array_info(mddev, &info);
3918 if (err) {
3919 printk(KERN_WARNING "md: couldn't set"
3920 " array info. %d\n", err);
3921 goto abort_unlock;
3922 }
3923 }
3924 goto done_unlock;
3925
3926 default:;
3927 }
3928
3929 /*
3930 * Commands querying/configuring an existing array:
3931 */
NeilBrown32a76272005-06-21 17:17:14 -07003932 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
3933 * RUN_ARRAY, and SET_BITMAP_FILE are allowed */
3934 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
3935 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 err = -ENODEV;
3937 goto abort_unlock;
3938 }
3939
3940 /*
3941 * Commands even a read-only array can execute:
3942 */
3943 switch (cmd)
3944 {
3945 case GET_ARRAY_INFO:
3946 err = get_array_info(mddev, argp);
3947 goto done_unlock;
3948
NeilBrown32a76272005-06-21 17:17:14 -07003949 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003950 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07003951 goto done_unlock;
3952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 case GET_DISK_INFO:
3954 err = get_disk_info(mddev, argp);
3955 goto done_unlock;
3956
3957 case RESTART_ARRAY_RW:
3958 err = restart_array(mddev);
3959 goto done_unlock;
3960
3961 case STOP_ARRAY:
3962 err = do_md_stop (mddev, 0);
3963 goto done_unlock;
3964
3965 case STOP_ARRAY_RO:
3966 err = do_md_stop (mddev, 1);
3967 goto done_unlock;
3968
3969 /*
3970 * We have a problem here : there is no easy way to give a CHS
3971 * virtual geometry. We currently pretend that we have a 2 heads
3972 * 4 sectors (with a BIG number of cylinders...). This drives
3973 * dosfs just mad... ;-)
3974 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
3976
3977 /*
3978 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08003979 * superblock, so we do not allow them on read-only arrays.
3980 * However non-MD ioctls (e.g. get-size) will still come through
3981 * here and hit the 'default' below, so only disallow
3982 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 */
NeilBrownf91de922005-11-08 21:39:36 -08003984 if (_IOC_TYPE(cmd) == MD_MAJOR &&
3985 mddev->ro && mddev->pers) {
3986 if (mddev->ro == 2) {
3987 mddev->ro = 0;
3988 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3989 md_wakeup_thread(mddev->thread);
3990
3991 } else {
3992 err = -EROFS;
3993 goto abort_unlock;
3994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
3996
3997 switch (cmd)
3998 {
3999 case ADD_NEW_DISK:
4000 {
4001 mdu_disk_info_t info;
4002 if (copy_from_user(&info, argp, sizeof(info)))
4003 err = -EFAULT;
4004 else
4005 err = add_new_disk(mddev, &info);
4006 goto done_unlock;
4007 }
4008
4009 case HOT_REMOVE_DISK:
4010 err = hot_remove_disk(mddev, new_decode_dev(arg));
4011 goto done_unlock;
4012
4013 case HOT_ADD_DISK:
4014 err = hot_add_disk(mddev, new_decode_dev(arg));
4015 goto done_unlock;
4016
4017 case SET_DISK_FAULTY:
4018 err = set_disk_faulty(mddev, new_decode_dev(arg));
4019 goto done_unlock;
4020
4021 case RUN_ARRAY:
4022 err = do_md_run (mddev);
4023 goto done_unlock;
4024
NeilBrown32a76272005-06-21 17:17:14 -07004025 case SET_BITMAP_FILE:
4026 err = set_bitmap_file(mddev, (int)arg);
4027 goto done_unlock;
4028
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 err = -EINVAL;
4031 goto abort_unlock;
4032 }
4033
4034done_unlock:
4035abort_unlock:
4036 mddev_unlock(mddev);
4037
4038 return err;
4039done:
4040 if (err)
4041 MD_BUG();
4042abort:
4043 return err;
4044}
4045
4046static int md_open(struct inode *inode, struct file *file)
4047{
4048 /*
4049 * Succeed if we can lock the mddev, which confirms that
4050 * it isn't being stopped right now.
4051 */
4052 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4053 int err;
4054
4055 if ((err = mddev_lock(mddev)))
4056 goto out;
4057
4058 err = 0;
4059 mddev_get(mddev);
4060 mddev_unlock(mddev);
4061
4062 check_disk_change(inode->i_bdev);
4063 out:
4064 return err;
4065}
4066
4067static int md_release(struct inode *inode, struct file * file)
4068{
4069 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4070
4071 if (!mddev)
4072 BUG();
4073 mddev_put(mddev);
4074
4075 return 0;
4076}
4077
4078static int md_media_changed(struct gendisk *disk)
4079{
4080 mddev_t *mddev = disk->private_data;
4081
4082 return mddev->changed;
4083}
4084
4085static int md_revalidate(struct gendisk *disk)
4086{
4087 mddev_t *mddev = disk->private_data;
4088
4089 mddev->changed = 0;
4090 return 0;
4091}
4092static struct block_device_operations md_fops =
4093{
4094 .owner = THIS_MODULE,
4095 .open = md_open,
4096 .release = md_release,
4097 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004098 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 .media_changed = md_media_changed,
4100 .revalidate_disk= md_revalidate,
4101};
4102
Adrian Bunk75c96f82005-05-05 16:16:09 -07004103static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 mdk_thread_t *thread = arg;
4106
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 /*
4108 * md_thread is a 'system-thread', it's priority should be very
4109 * high. We avoid resource deadlocks individually in each
4110 * raid personality. (RAID5 does preallocation) We also use RR and
4111 * the very same RT priority as kswapd, thus we will never get
4112 * into a priority inversion deadlock.
4113 *
4114 * we definitely have to have equal or higher priority than
4115 * bdflush, otherwise bdflush will deadlock if there are too
4116 * many dirty RAID5 blocks.
4117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
NeilBrown6985c432005-10-19 21:23:47 -07004119 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004120 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
NeilBrown93588e22005-11-15 00:09:12 -08004122 /* We need to wait INTERRUPTIBLE so that
4123 * we don't add to the load-average.
4124 * That means we need to be sure no signals are
4125 * pending
4126 */
4127 if (signal_pending(current))
4128 flush_signals(current);
4129
4130 wait_event_interruptible_timeout
4131 (thread->wqueue,
4132 test_bit(THREAD_WAKEUP, &thread->flags)
4133 || kthread_should_stop(),
4134 thread->timeout);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -07004135 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 clear_bit(THREAD_WAKEUP, &thread->flags);
4138
NeilBrown787453c2005-11-08 21:39:43 -08004139 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 return 0;
4143}
4144
4145void md_wakeup_thread(mdk_thread_t *thread)
4146{
4147 if (thread) {
4148 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4149 set_bit(THREAD_WAKEUP, &thread->flags);
4150 wake_up(&thread->wqueue);
4151 }
4152}
4153
4154mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4155 const char *name)
4156{
4157 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
NeilBrown9ffae0c2006-01-06 00:20:32 -08004159 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 if (!thread)
4161 return NULL;
4162
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 init_waitqueue_head(&thread->wqueue);
4164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 thread->run = run;
4166 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004167 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004168 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004169 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 kfree(thread);
4171 return NULL;
4172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 return thread;
4174}
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176void md_unregister_thread(mdk_thread_t *thread)
4177{
NeilBrownd28446f2005-04-16 15:26:41 -07004178 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004179
4180 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 kfree(thread);
4182}
4183
4184void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4185{
4186 if (!mddev) {
4187 MD_BUG();
4188 return;
4189 }
4190
NeilBrownb2d444d2005-11-08 21:39:31 -08004191 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return;
NeilBrown32a76272005-06-21 17:17:14 -07004193/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4195 mdname(mddev),
4196 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4197 __builtin_return_address(0),__builtin_return_address(1),
4198 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004199*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 if (!mddev->pers->error_handler)
4201 return;
4202 mddev->pers->error_handler(mddev,rdev);
4203 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4204 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4205 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004206 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207}
4208
4209/* seq_file implementation /proc/mdstat */
4210
4211static void status_unused(struct seq_file *seq)
4212{
4213 int i = 0;
4214 mdk_rdev_t *rdev;
4215 struct list_head *tmp;
4216
4217 seq_printf(seq, "unused devices: ");
4218
4219 ITERATE_RDEV_PENDING(rdev,tmp) {
4220 char b[BDEVNAME_SIZE];
4221 i++;
4222 seq_printf(seq, "%s ",
4223 bdevname(rdev->bdev,b));
4224 }
4225 if (!i)
4226 seq_printf(seq, "<none>");
4227
4228 seq_printf(seq, "\n");
4229}
4230
4231
4232static void status_resync(struct seq_file *seq, mddev_t * mddev)
4233{
NeilBrown4588b422006-03-27 01:18:04 -08004234 sector_t max_blocks, resync, res;
4235 unsigned long dt, db, rt;
4236 int scale;
4237 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238
4239 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4240
4241 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4242 max_blocks = mddev->resync_max_sectors >> 1;
4243 else
4244 max_blocks = mddev->size;
4245
4246 /*
4247 * Should not happen.
4248 */
4249 if (!max_blocks) {
4250 MD_BUG();
4251 return;
4252 }
NeilBrown4588b422006-03-27 01:18:04 -08004253 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4254 * in a sector_t, and (max_blocks>>scale) will fit in a
4255 * u32, as those are the requirements for sector_div.
4256 * Thus 'scale' must be at least 10
4257 */
4258 scale = 10;
4259 if (sizeof(sector_t) > sizeof(unsigned long)) {
4260 while ( max_blocks/2 > (1ULL<<(scale+32)))
4261 scale++;
4262 }
4263 res = (resync>>scale)*1000;
4264 sector_div(res, (u32)((max_blocks>>scale)+1));
4265
4266 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 {
NeilBrown4588b422006-03-27 01:18:04 -08004268 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 seq_printf(seq, "[");
4270 for (i = 0; i < x; i++)
4271 seq_printf(seq, "=");
4272 seq_printf(seq, ">");
4273 for (i = 0; i < y; i++)
4274 seq_printf(seq, ".");
4275 seq_printf(seq, "] ");
4276 }
NeilBrown4588b422006-03-27 01:18:04 -08004277 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004278 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4279 "reshape" :
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
NeilBrownccfcc3c2006-03-27 01:18:09 -08004281 "resync" : "recovery")),
NeilBrown4588b422006-03-27 01:18:04 -08004282 per_milli/10, per_milli % 10,
4283 (unsigned long long) resync,
4284 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
4286 /*
4287 * We do not want to overflow, so the order of operands and
4288 * the * 100 / 100 trick are important. We do a +1 to be
4289 * safe against division by zero. We only estimate anyway.
4290 *
4291 * dt: time from mark until now
4292 * db: blocks written from mark until now
4293 * rt: remaining time
4294 */
4295 dt = ((jiffies - mddev->resync_mark) / HZ);
4296 if (!dt) dt++;
4297 db = resync - (mddev->resync_mark_cnt/2);
NeilBrown4588b422006-03-27 01:18:04 -08004298 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
4300 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4301
4302 seq_printf(seq, " speed=%ldK/sec", db/dt);
4303}
4304
4305static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4306{
4307 struct list_head *tmp;
4308 loff_t l = *pos;
4309 mddev_t *mddev;
4310
4311 if (l >= 0x10000)
4312 return NULL;
4313 if (!l--)
4314 /* header */
4315 return (void*)1;
4316
4317 spin_lock(&all_mddevs_lock);
4318 list_for_each(tmp,&all_mddevs)
4319 if (!l--) {
4320 mddev = list_entry(tmp, mddev_t, all_mddevs);
4321 mddev_get(mddev);
4322 spin_unlock(&all_mddevs_lock);
4323 return mddev;
4324 }
4325 spin_unlock(&all_mddevs_lock);
4326 if (!l--)
4327 return (void*)2;/* tail */
4328 return NULL;
4329}
4330
4331static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4332{
4333 struct list_head *tmp;
4334 mddev_t *next_mddev, *mddev = v;
4335
4336 ++*pos;
4337 if (v == (void*)2)
4338 return NULL;
4339
4340 spin_lock(&all_mddevs_lock);
4341 if (v == (void*)1)
4342 tmp = all_mddevs.next;
4343 else
4344 tmp = mddev->all_mddevs.next;
4345 if (tmp != &all_mddevs)
4346 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4347 else {
4348 next_mddev = (void*)2;
4349 *pos = 0x10000;
4350 }
4351 spin_unlock(&all_mddevs_lock);
4352
4353 if (v != (void*)1)
4354 mddev_put(mddev);
4355 return next_mddev;
4356
4357}
4358
4359static void md_seq_stop(struct seq_file *seq, void *v)
4360{
4361 mddev_t *mddev = v;
4362
4363 if (mddev && v != (void*)1 && v != (void*)2)
4364 mddev_put(mddev);
4365}
4366
NeilBrownd7603b72006-01-06 00:20:30 -08004367struct mdstat_info {
4368 int event;
4369};
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371static int md_seq_show(struct seq_file *seq, void *v)
4372{
4373 mddev_t *mddev = v;
4374 sector_t size;
4375 struct list_head *tmp2;
4376 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004377 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004378 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
4380 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004381 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 seq_printf(seq, "Personalities : ");
4383 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004384 list_for_each_entry(pers, &pers_list, list)
4385 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
4387 spin_unlock(&pers_lock);
4388 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004389 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return 0;
4391 }
4392 if (v == (void*)2) {
4393 status_unused(seq);
4394 return 0;
4395 }
4396
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004397 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004399
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4401 seq_printf(seq, "%s : %sactive", mdname(mddev),
4402 mddev->pers ? "" : "in");
4403 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004404 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004406 if (mddev->ro==2)
4407 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 seq_printf(seq, " %s", mddev->pers->name);
4409 }
4410
4411 size = 0;
4412 ITERATE_RDEV(mddev,rdev,tmp2) {
4413 char b[BDEVNAME_SIZE];
4414 seq_printf(seq, " %s[%d]",
4415 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004416 if (test_bit(WriteMostly, &rdev->flags))
4417 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004418 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 seq_printf(seq, "(F)");
4420 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004421 } else if (rdev->raid_disk < 0)
4422 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 size += rdev->size;
4424 }
4425
4426 if (!list_empty(&mddev->disks)) {
4427 if (mddev->pers)
4428 seq_printf(seq, "\n %llu blocks",
4429 (unsigned long long)mddev->array_size);
4430 else
4431 seq_printf(seq, "\n %llu blocks",
4432 (unsigned long long)size);
4433 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004434 if (mddev->persistent) {
4435 if (mddev->major_version != 0 ||
4436 mddev->minor_version != 90) {
4437 seq_printf(seq," super %d.%d",
4438 mddev->major_version,
4439 mddev->minor_version);
4440 }
4441 } else
4442 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 if (mddev->pers) {
4445 mddev->pers->status (seq, mddev);
4446 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004447 if (mddev->pers->sync_request) {
4448 if (mddev->curr_resync > 2) {
4449 status_resync (seq, mddev);
4450 seq_printf(seq, "\n ");
4451 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4452 seq_printf(seq, "\tresync=DELAYED\n ");
4453 else if (mddev->recovery_cp < MaxSector)
4454 seq_printf(seq, "\tresync=PENDING\n ");
4455 }
NeilBrown32a76272005-06-21 17:17:14 -07004456 } else
4457 seq_printf(seq, "\n ");
4458
4459 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004460 unsigned long chunk_kb;
4461 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004462 spin_lock_irqsave(&bitmap->lock, flags);
4463 chunk_kb = bitmap->chunksize >> 10;
4464 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4465 "%lu%s chunk",
4466 bitmap->pages - bitmap->missing_pages,
4467 bitmap->pages,
4468 (bitmap->pages - bitmap->missing_pages)
4469 << (PAGE_SHIFT - 10),
4470 chunk_kb ? chunk_kb : bitmap->chunksize,
4471 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004472 if (bitmap->file) {
4473 seq_printf(seq, ", file: ");
4474 seq_path(seq, bitmap->file->f_vfsmnt,
4475 bitmap->file->f_dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004476 }
NeilBrown78d742d2005-06-21 17:17:15 -07004477
NeilBrown32a76272005-06-21 17:17:14 -07004478 seq_printf(seq, "\n");
4479 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 }
4481
4482 seq_printf(seq, "\n");
4483 }
4484 mddev_unlock(mddev);
4485
4486 return 0;
4487}
4488
4489static struct seq_operations md_seq_ops = {
4490 .start = md_seq_start,
4491 .next = md_seq_next,
4492 .stop = md_seq_stop,
4493 .show = md_seq_show,
4494};
4495
4496static int md_seq_open(struct inode *inode, struct file *file)
4497{
4498 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08004499 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
4500 if (mi == NULL)
4501 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08004504 if (error)
4505 kfree(mi);
4506 else {
4507 struct seq_file *p = file->private_data;
4508 p->private = mi;
4509 mi->event = atomic_read(&md_event_count);
4510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return error;
4512}
4513
NeilBrownd7603b72006-01-06 00:20:30 -08004514static int md_seq_release(struct inode *inode, struct file *file)
4515{
4516 struct seq_file *m = file->private_data;
4517 struct mdstat_info *mi = m->private;
4518 m->private = NULL;
4519 kfree(mi);
4520 return seq_release(inode, file);
4521}
4522
4523static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
4524{
4525 struct seq_file *m = filp->private_data;
4526 struct mdstat_info *mi = m->private;
4527 int mask;
4528
4529 poll_wait(filp, &md_event_waiters, wait);
4530
4531 /* always allow read */
4532 mask = POLLIN | POLLRDNORM;
4533
4534 if (mi->event != atomic_read(&md_event_count))
4535 mask |= POLLERR | POLLPRI;
4536 return mask;
4537}
4538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539static struct file_operations md_seq_fops = {
4540 .open = md_seq_open,
4541 .read = seq_read,
4542 .llseek = seq_lseek,
NeilBrownd7603b72006-01-06 00:20:30 -08004543 .release = md_seq_release,
4544 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545};
4546
NeilBrown2604b702006-01-06 00:20:36 -08004547int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004550 list_add_tail(&p->list, &pers_list);
4551 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 spin_unlock(&pers_lock);
4553 return 0;
4554}
4555
NeilBrown2604b702006-01-06 00:20:36 -08004556int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
NeilBrown2604b702006-01-06 00:20:36 -08004558 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004560 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 spin_unlock(&pers_lock);
4562 return 0;
4563}
4564
4565static int is_mddev_idle(mddev_t *mddev)
4566{
4567 mdk_rdev_t * rdev;
4568 struct list_head *tmp;
4569 int idle;
4570 unsigned long curr_events;
4571
4572 idle = 1;
4573 ITERATE_RDEV(mddev,rdev,tmp) {
4574 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01004575 curr_events = disk_stat_read(disk, sectors[0]) +
4576 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08004578 /* The difference between curr_events and last_events
4579 * will be affected by any new non-sync IO (making
4580 * curr_events bigger) and any difference in the amount of
4581 * in-flight syncio (making current_events bigger or smaller)
4582 * The amount in-flight is currently limited to
4583 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
4584 * which is at most 4096 sectors.
4585 * These numbers are fairly fragile and should be made
4586 * more robust, probably by enforcing the
4587 * 'window size' that md_do_sync sort-of uses.
4588 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 * Note: the following is an unsigned comparison.
4590 */
NeilBrownc0e48522005-11-18 01:11:01 -08004591 if ((curr_events - rdev->last_events + 4096) > 8192) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 rdev->last_events = curr_events;
4593 idle = 0;
4594 }
4595 }
4596 return idle;
4597}
4598
4599void md_done_sync(mddev_t *mddev, int blocks, int ok)
4600{
4601 /* another "blocks" (512byte) blocks have been synced */
4602 atomic_sub(blocks, &mddev->recovery_active);
4603 wake_up(&mddev->recovery_wait);
4604 if (!ok) {
4605 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4606 md_wakeup_thread(mddev->thread);
4607 // stop recovery, signal do_sync ....
4608 }
4609}
4610
4611
NeilBrown06d91a52005-06-21 17:17:12 -07004612/* md_write_start(mddev, bi)
4613 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07004614 * in superblock) before writing, schedule a superblock update
4615 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07004616 */
NeilBrown3d310eb2005-06-21 17:17:26 -07004617void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
NeilBrown06d91a52005-06-21 17:17:12 -07004619 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07004620 return;
NeilBrown06d91a52005-06-21 17:17:12 -07004621
NeilBrownf91de922005-11-08 21:39:36 -08004622 BUG_ON(mddev->ro == 1);
4623 if (mddev->ro == 2) {
4624 /* need to switch to read/write */
4625 mddev->ro = 0;
4626 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4627 md_wakeup_thread(mddev->thread);
4628 }
NeilBrown06d91a52005-06-21 17:17:12 -07004629 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07004630 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08004631 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07004632 if (mddev->in_sync) {
4633 mddev->in_sync = 0;
4634 mddev->sb_dirty = 1;
4635 md_wakeup_thread(mddev->thread);
4636 }
NeilBrowna9701a32005-11-08 21:39:34 -08004637 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07004638 }
NeilBrown3d310eb2005-06-21 17:17:26 -07004639 wait_event(mddev->sb_wait, mddev->sb_dirty==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640}
4641
4642void md_write_end(mddev_t *mddev)
4643{
4644 if (atomic_dec_and_test(&mddev->writes_pending)) {
4645 if (mddev->safemode == 2)
4646 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07004647 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
4649 }
4650}
4651
Adrian Bunk75c96f82005-05-05 16:16:09 -07004652static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654#define SYNC_MARKS 10
4655#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08004656void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657{
4658 mddev_t *mddev2;
4659 unsigned int currspeed = 0,
4660 window;
NeilBrown57afd892005-06-21 17:17:13 -07004661 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 unsigned long mark[SYNC_MARKS];
4663 sector_t mark_cnt[SYNC_MARKS];
4664 int last_mark,m;
4665 struct list_head *tmp;
4666 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07004667 int skipped = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668
4669 /* just incase thread restarts... */
4670 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
4671 return;
4672
4673 /* we overload curr_resync somewhat here.
4674 * 0 == not engaged in resync at all
4675 * 2 == checking that there is no conflict with another sync
4676 * 1 == like 2, but have yielded to allow conflicting resync to
4677 * commense
4678 * other == active in resync - this many blocks
4679 *
4680 * Before starting a resync we must have set curr_resync to
4681 * 2, and then checked that every "conflicting" array has curr_resync
4682 * less than ours. When we find one that is the same or higher
4683 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
4684 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
4685 * This will mean we have to start checking from the beginning again.
4686 *
4687 */
4688
4689 do {
4690 mddev->curr_resync = 2;
4691
4692 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08004693 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07004694 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 goto skip;
4696 }
4697 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 if (mddev2 == mddev)
4699 continue;
4700 if (mddev2->curr_resync &&
4701 match_mddev_units(mddev,mddev2)) {
4702 DEFINE_WAIT(wq);
4703 if (mddev < mddev2 && mddev->curr_resync == 2) {
4704 /* arbitrarily yield */
4705 mddev->curr_resync = 1;
4706 wake_up(&resync_wait);
4707 }
4708 if (mddev > mddev2 && mddev->curr_resync == 1)
4709 /* no need to wait here, we can wait the next
4710 * time 'round when curr_resync == 2
4711 */
4712 continue;
NeilBrown787453c2005-11-08 21:39:43 -08004713 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
4714 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07004715 mddev2->curr_resync >= mddev->curr_resync) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 printk(KERN_INFO "md: delaying resync of %s"
4717 " until %s has finished resync (they"
4718 " share one or more physical units)\n",
4719 mdname(mddev), mdname(mddev2));
4720 mddev_put(mddev2);
4721 schedule();
4722 finish_wait(&resync_wait, &wq);
4723 goto try_again;
4724 }
4725 finish_wait(&resync_wait, &wq);
4726 }
4727 }
4728 } while (mddev->curr_resync < 2);
4729
NeilBrown9d888832005-11-08 21:39:26 -08004730 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07004732 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 */
4734 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08004735 mddev->resync_mismatches = 0;
NeilBrownccfcc3c2006-03-27 01:18:09 -08004736 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4737 max_sectors = mddev->size << 1;
4738 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 /* recovery follows the physical size of devices */
4740 max_sectors = mddev->size << 1;
4741
4742 printk(KERN_INFO "md: syncing RAID array %s\n", mdname(mddev));
4743 printk(KERN_INFO "md: minimum _guaranteed_ reconstruction speed:"
NeilBrown88202a02006-01-06 00:21:36 -08004744 " %d KB/sec/disc.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07004745 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 "(but not more than %d KB/sec) for reconstruction.\n",
NeilBrown88202a02006-01-06 00:21:36 -08004747 speed_max(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
4749 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown32a76272005-06-21 17:17:14 -07004750 /* we don't use the checkpoint if there's a bitmap */
NeilBrown24dd4692005-11-08 21:39:26 -08004751 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && !mddev->bitmap
4752 && ! test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 j = mddev->recovery_cp;
4754 else
4755 j = 0;
NeilBrown57afd892005-06-21 17:17:13 -07004756 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 for (m = 0; m < SYNC_MARKS; m++) {
4758 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004759 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 }
4761 last_mark = 0;
4762 mddev->resync_mark = mark[last_mark];
4763 mddev->resync_mark_cnt = mark_cnt[last_mark];
4764
4765 /*
4766 * Tune reconstruction:
4767 */
4768 window = 32*(PAGE_SIZE/512);
4769 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
4770 window/2,(unsigned long long) max_sectors/2);
4771
4772 atomic_set(&mddev->recovery_active, 0);
4773 init_waitqueue_head(&mddev->recovery_wait);
4774 last_check = 0;
4775
4776 if (j>2) {
4777 printk(KERN_INFO
4778 "md: resuming recovery of %s from checkpoint.\n",
4779 mdname(mddev));
4780 mddev->curr_resync = j;
4781 }
4782
4783 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07004784 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785
NeilBrown57afd892005-06-21 17:17:13 -07004786 skipped = 0;
4787 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08004788 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07004789 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
4791 goto out;
4792 }
NeilBrown57afd892005-06-21 17:17:13 -07004793
4794 if (!skipped) { /* actual IO requested */
4795 io_sectors += sectors;
4796 atomic_add(sectors, &mddev->recovery_active);
4797 }
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 j += sectors;
4800 if (j>1) mddev->curr_resync = j;
NeilBrownd7603b72006-01-06 00:20:30 -08004801 if (last_check == 0)
4802 /* this is the earliers that rebuilt will be
4803 * visible in /proc/mdstat
4804 */
4805 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07004806
4807 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 continue;
4809
NeilBrown57afd892005-06-21 17:17:13 -07004810 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
4812 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
4813 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
4814 break;
4815
4816 repeat:
4817 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
4818 /* step marks */
4819 int next = (last_mark+1) % SYNC_MARKS;
4820
4821 mddev->resync_mark = mark[next];
4822 mddev->resync_mark_cnt = mark_cnt[next];
4823 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07004824 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 last_mark = next;
4826 }
4827
4828
NeilBrown787453c2005-11-08 21:39:43 -08004829 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 /*
4831 * got a signal, exit.
4832 */
4833 printk(KERN_INFO
4834 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4836 goto out;
4837 }
4838
4839 /*
4840 * this loop exits only if either when we are slower than
4841 * the 'hard' speed limit, or the system was IO-idle for
4842 * a jiffy.
4843 * the system might be non-idle CPU-wise, but we only care
4844 * about not overloading the IO subsystem. (things like an
4845 * e2fsck being done on the RAID array should execute fast)
4846 */
4847 mddev->queue->unplug_fn(mddev->queue);
4848 cond_resched();
4849
NeilBrown57afd892005-06-21 17:17:13 -07004850 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
4851 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852
NeilBrown88202a02006-01-06 00:21:36 -08004853 if (currspeed > speed_min(mddev)) {
4854 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08004856 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 goto repeat;
4858 }
4859 }
4860 }
4861 printk(KERN_INFO "md: %s: sync done.\n",mdname(mddev));
4862 /*
4863 * this also signals 'finished resyncing' to md_stop
4864 */
4865 out:
4866 mddev->queue->unplug_fn(mddev->queue);
4867
4868 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
4869
4870 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07004871 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
4873 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08004874 test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
4875 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 mddev->curr_resync > 2 &&
4877 mddev->curr_resync >= mddev->recovery_cp) {
4878 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4879 printk(KERN_INFO
4880 "md: checkpointing recovery of %s.\n",
4881 mdname(mddev));
4882 mddev->recovery_cp = mddev->curr_resync;
4883 } else
4884 mddev->recovery_cp = MaxSector;
4885 }
4886
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 skip:
4888 mddev->curr_resync = 0;
4889 wake_up(&resync_wait);
4890 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
4891 md_wakeup_thread(mddev->thread);
4892}
NeilBrown29269552006-03-27 01:18:10 -08004893EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894
4895
4896/*
4897 * This routine is regularly called by all per-raid-array threads to
4898 * deal with generic issues like resync and super-block update.
4899 * Raid personalities that don't have a thread (linear/raid0) do not
4900 * need this as they never do any recovery or update the superblock.
4901 *
4902 * It does not do any resync itself, but rather "forks" off other threads
4903 * to do that as needed.
4904 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
4905 * "->recovery" and create a thread at ->sync_thread.
4906 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
4907 * and wakeups up this thread which will reap the thread and finish up.
4908 * This thread also removes any faulty devices (with nr_pending == 0).
4909 *
4910 * The overall approach is:
4911 * 1/ if the superblock needs updating, update it.
4912 * 2/ If a recovery thread is running, don't do anything else.
4913 * 3/ If recovery has finished, clean up, possibly marking spares active.
4914 * 4/ If there are any faulty devices, remove them.
4915 * 5/ If array is degraded, try to add spares devices
4916 * 6/ If array has spares or is not in-sync, start a resync thread.
4917 */
4918void md_check_recovery(mddev_t *mddev)
4919{
4920 mdk_rdev_t *rdev;
4921 struct list_head *rtmp;
4922
4923
NeilBrown5f404022005-06-21 17:17:16 -07004924 if (mddev->bitmap)
4925 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
4927 if (mddev->ro)
4928 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004929
4930 if (signal_pending(current)) {
4931 if (mddev->pers->sync_request) {
4932 printk(KERN_INFO "md: %s in immediate safe mode\n",
4933 mdname(mddev));
4934 mddev->safemode = 2;
4935 }
4936 flush_signals(current);
4937 }
4938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939 if ( ! (
4940 mddev->sb_dirty ||
4941 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07004942 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
4943 (mddev->safemode == 1) ||
4944 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
4945 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 ))
4947 return;
NeilBrownfca4d842005-06-21 17:17:11 -07004948
NeilBrowndf5b89b2006-03-27 01:18:20 -08004949 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 int spares =0;
NeilBrownfca4d842005-06-21 17:17:11 -07004951
NeilBrowna9701a32005-11-08 21:39:34 -08004952 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004953 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
4954 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
4955 mddev->in_sync = 1;
4956 mddev->sb_dirty = 1;
4957 }
4958 if (mddev->safemode == 1)
4959 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08004960 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07004961
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 if (mddev->sb_dirty)
4963 md_update_sb(mddev);
NeilBrown06d91a52005-06-21 17:17:12 -07004964
NeilBrown06d91a52005-06-21 17:17:12 -07004965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
4967 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
4968 /* resync/recovery still happening */
4969 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4970 goto unlock;
4971 }
4972 if (mddev->sync_thread) {
4973 /* resync has finished, collect result */
4974 md_unregister_thread(mddev->sync_thread);
4975 mddev->sync_thread = NULL;
4976 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
4977 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4978 /* success...*/
4979 /* activate any spares */
4980 mddev->pers->spare_active(mddev);
4981 }
4982 md_update_sb(mddev);
NeilBrown41158c72005-06-21 17:17:25 -07004983
4984 /* if array is no-longer degraded, then any saved_raid_disk
4985 * information must be scrapped
4986 */
4987 if (!mddev->degraded)
4988 ITERATE_RDEV(mddev,rdev,rtmp)
4989 rdev->saved_raid_disk = -1;
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 mddev->recovery = 0;
4992 /* flag recovery needed just to double check */
4993 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08004994 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 goto unlock;
4996 }
NeilBrown24dd4692005-11-08 21:39:26 -08004997 /* Clear some bits that don't mean anything, but
4998 * might be left set
4999 */
5000 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5001 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5002 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5003 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
5005 /* no recovery is running.
5006 * remove any failed drives, then
5007 * add spares if possible.
5008 * Spare are also removed and re-added, to allow
5009 * the personality to fail the re-add.
5010 */
5011 ITERATE_RDEV(mddev,rdev,rtmp)
5012 if (rdev->raid_disk >= 0 &&
NeilBrownb2d444d2005-11-08 21:39:31 -08005013 (test_bit(Faulty, &rdev->flags) || ! test_bit(In_sync, &rdev->flags)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 atomic_read(&rdev->nr_pending)==0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08005015 if (mddev->pers->hot_remove_disk(mddev, rdev->raid_disk)==0) {
5016 char nm[20];
5017 sprintf(nm,"rd%d", rdev->raid_disk);
5018 sysfs_remove_link(&mddev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 rdev->raid_disk = -1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08005020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 }
5022
5023 if (mddev->degraded) {
5024 ITERATE_RDEV(mddev,rdev,rtmp)
5025 if (rdev->raid_disk < 0
NeilBrownb2d444d2005-11-08 21:39:31 -08005026 && !test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08005027 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5028 char nm[20];
5029 sprintf(nm, "rd%d", rdev->raid_disk);
5030 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 spares++;
NeilBrownd7603b72006-01-06 00:20:30 -08005032 md_new_event(mddev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08005033 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 break;
5035 }
5036 }
5037
NeilBrown24dd4692005-11-08 21:39:26 -08005038 if (spares) {
5039 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5040 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5041 } else if (mddev->recovery_cp < MaxSector) {
5042 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5043 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5044 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 if (mddev->pers->sync_request) {
5048 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005049 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5050 /* We are adding a device or devices to an array
5051 * which has the bitmap stored on all devices.
5052 * So make sure all bitmap pages get written
5053 */
5054 bitmap_write_all(mddev->bitmap);
5055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 mddev->sync_thread = md_register_thread(md_do_sync,
5057 mddev,
5058 "%s_resync");
5059 if (!mddev->sync_thread) {
5060 printk(KERN_ERR "%s: could not start resync"
5061 " thread...\n",
5062 mdname(mddev));
5063 /* leave the spares where they are, it shouldn't hurt */
5064 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005065 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005067 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 }
5069 unlock:
5070 mddev_unlock(mddev);
5071 }
5072}
5073
Adrian Bunk75c96f82005-05-05 16:16:09 -07005074static int md_notify_reboot(struct notifier_block *this,
5075 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076{
5077 struct list_head *tmp;
5078 mddev_t *mddev;
5079
5080 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5081
5082 printk(KERN_INFO "md: stopping all md devices.\n");
5083
5084 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005085 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005087 mddev_unlock(mddev);
5088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 /*
5090 * certain more exotic SCSI devices are known to be
5091 * volatile wrt too early system reboots. While the
5092 * right place to handle this issue is the given
5093 * driver, we do want to have a safe RAID driver ...
5094 */
5095 mdelay(1000*1);
5096 }
5097 return NOTIFY_DONE;
5098}
5099
Adrian Bunk75c96f82005-05-05 16:16:09 -07005100static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 .notifier_call = md_notify_reboot,
5102 .next = NULL,
5103 .priority = INT_MAX, /* before any real devices */
5104};
5105
5106static void md_geninit(void)
5107{
5108 struct proc_dir_entry *p;
5109
5110 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5111
5112 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5113 if (p)
5114 p->proc_fops = &md_seq_fops;
5115}
5116
Adrian Bunk75c96f82005-05-05 16:16:09 -07005117static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118{
5119 int minor;
5120
5121 printk(KERN_INFO "md: md driver %d.%d.%d MAX_MD_DEVS=%d,"
5122 " MD_SB_DISKS=%d\n",
5123 MD_MAJOR_VERSION, MD_MINOR_VERSION,
5124 MD_PATCHLEVEL_VERSION, MAX_MD_DEVS, MD_SB_DISKS);
NeilBrownbd926c62005-11-08 21:39:32 -08005125 printk(KERN_INFO "md: bitmap version %d.%d\n", BITMAP_MAJOR_HI,
NeilBrown32a76272005-06-21 17:17:14 -07005126 BITMAP_MINOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127
5128 if (register_blkdev(MAJOR_NR, "md"))
5129 return -1;
5130 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5131 unregister_blkdev(MAJOR_NR, "md");
5132 return -1;
5133 }
5134 devfs_mk_dir("md");
5135 blk_register_region(MKDEV(MAJOR_NR, 0), MAX_MD_DEVS, THIS_MODULE,
5136 md_probe, NULL, NULL);
5137 blk_register_region(MKDEV(mdp_major, 0), MAX_MD_DEVS<<MdpMinorShift, THIS_MODULE,
5138 md_probe, NULL, NULL);
5139
5140 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5141 devfs_mk_bdev(MKDEV(MAJOR_NR, minor),
5142 S_IFBLK|S_IRUSR|S_IWUSR,
5143 "md/%d", minor);
5144
5145 for (minor=0; minor < MAX_MD_DEVS; ++minor)
5146 devfs_mk_bdev(MKDEV(mdp_major, minor<<MdpMinorShift),
5147 S_IFBLK|S_IRUSR|S_IWUSR,
5148 "md/mdp%d", minor);
5149
5150
5151 register_reboot_notifier(&md_notifier);
5152 raid_table_header = register_sysctl_table(raid_root_table, 1);
5153
5154 md_geninit();
5155 return (0);
5156}
5157
5158
5159#ifndef MODULE
5160
5161/*
5162 * Searches all registered partitions for autorun RAID arrays
5163 * at boot time.
5164 */
5165static dev_t detected_devices[128];
5166static int dev_cnt;
5167
5168void md_autodetect_dev(dev_t dev)
5169{
5170 if (dev_cnt >= 0 && dev_cnt < 127)
5171 detected_devices[dev_cnt++] = dev;
5172}
5173
5174
5175static void autostart_arrays(int part)
5176{
5177 mdk_rdev_t *rdev;
5178 int i;
5179
5180 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5181
5182 for (i = 0; i < dev_cnt; i++) {
5183 dev_t dev = detected_devices[i];
5184
5185 rdev = md_import_device(dev,0, 0);
5186 if (IS_ERR(rdev))
5187 continue;
5188
NeilBrownb2d444d2005-11-08 21:39:31 -08005189 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 MD_BUG();
5191 continue;
5192 }
5193 list_add(&rdev->same_set, &pending_raid_disks);
5194 }
5195 dev_cnt = 0;
5196
5197 autorun_devices(part);
5198}
5199
5200#endif
5201
5202static __exit void md_exit(void)
5203{
5204 mddev_t *mddev;
5205 struct list_head *tmp;
5206 int i;
5207 blk_unregister_region(MKDEV(MAJOR_NR,0), MAX_MD_DEVS);
5208 blk_unregister_region(MKDEV(mdp_major,0), MAX_MD_DEVS << MdpMinorShift);
5209 for (i=0; i < MAX_MD_DEVS; i++)
5210 devfs_remove("md/%d", i);
5211 for (i=0; i < MAX_MD_DEVS; i++)
5212 devfs_remove("md/d%d", i);
5213
5214 devfs_remove("md");
5215
5216 unregister_blkdev(MAJOR_NR,"md");
5217 unregister_blkdev(mdp_major, "mdp");
5218 unregister_reboot_notifier(&md_notifier);
5219 unregister_sysctl_table(raid_table_header);
5220 remove_proc_entry("mdstat", NULL);
5221 ITERATE_MDDEV(mddev,tmp) {
5222 struct gendisk *disk = mddev->gendisk;
5223 if (!disk)
5224 continue;
5225 export_array(mddev);
5226 del_gendisk(disk);
5227 put_disk(disk);
5228 mddev->gendisk = NULL;
5229 mddev_put(mddev);
5230 }
5231}
5232
5233module_init(md_init)
5234module_exit(md_exit)
5235
NeilBrownf91de922005-11-08 21:39:36 -08005236static int get_ro(char *buffer, struct kernel_param *kp)
5237{
5238 return sprintf(buffer, "%d", start_readonly);
5239}
5240static int set_ro(const char *val, struct kernel_param *kp)
5241{
5242 char *e;
5243 int num = simple_strtoul(val, &e, 10);
5244 if (*val && (*e == '\0' || *e == '\n')) {
5245 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005246 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005247 }
5248 return -EINVAL;
5249}
5250
5251module_param_call(start_ro, set_ro, get_ro, NULL, 0600);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08005252module_param(start_dirty_degraded, int, 0644);
5253
NeilBrownf91de922005-11-08 21:39:36 -08005254
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255EXPORT_SYMBOL(register_md_personality);
5256EXPORT_SYMBOL(unregister_md_personality);
5257EXPORT_SYMBOL(md_error);
5258EXPORT_SYMBOL(md_done_sync);
5259EXPORT_SYMBOL(md_write_start);
5260EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261EXPORT_SYMBOL(md_register_thread);
5262EXPORT_SYMBOL(md_unregister_thread);
5263EXPORT_SYMBOL(md_wakeup_thread);
5264EXPORT_SYMBOL(md_print_devices);
5265EXPORT_SYMBOL(md_check_recovery);
5266MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005267MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005268MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);