blob: c10ce91b64e9b432cbe797df44b7d605aa7a4609 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
35#include <linux/module.h>
Ahmed S. Darwish50511da2007-05-09 02:35:34 -070036#include <linux/kernel.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070037#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/linkage.h>
39#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070040#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080043#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080044#include <linux/mutex.h>
NeilBrown16f17b32006-06-26 00:27:37 -070045#include <linux/ctype.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <linux/init.h>
49
NeilBrown32a76272005-06-21 17:17:14 -070050#include <linux/file.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef CONFIG_KMOD
53#include <linux/kmod.h>
54#endif
55
56#include <asm/unaligned.h>
57
58#define MAJOR_NR MD_MAJOR
59#define MD_DRIVER
60
61/* 63 partitions with the alternate major number (mdp) */
62#define MdpMinorShift 6
63
64#define DEBUG 0
65#define dprintk(x...) ((void)(DEBUG && printk(x)))
66
67
68#ifndef MODULE
69static void autostart_arrays (int part);
70#endif
71
NeilBrown2604b702006-01-06 00:20:36 -080072static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static DEFINE_SPINLOCK(pers_lock);
74
Adrian Bunk5e563412006-06-26 00:27:42 -070075static void md_print_devices(void);
76
77#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/*
80 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
81 * is 1000 KB/sec, so the extra system load does not show up that much.
82 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070083 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * subsystem is idle. There is also an 'absolute maximum' reconstruction
85 * speed limit - in case reconstruction slows down your system despite
86 * idle IO detection.
87 *
88 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080089 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 */
91
92static int sysctl_speed_limit_min = 1000;
93static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080094static inline int speed_min(mddev_t *mddev)
95{
96 return mddev->sync_speed_min ?
97 mddev->sync_speed_min : sysctl_speed_limit_min;
98}
99
100static inline int speed_max(mddev_t *mddev)
101{
102 return mddev->sync_speed_max ?
103 mddev->sync_speed_max : sysctl_speed_limit_max;
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106static struct ctl_table_header *raid_table_header;
107
108static ctl_table raid_table[] = {
109 {
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
111 .procname = "speed_limit_min",
112 .data = &sysctl_speed_limit_min,
113 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700114 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 .proc_handler = &proc_dointvec,
116 },
117 {
118 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
119 .procname = "speed_limit_max",
120 .data = &sysctl_speed_limit_max,
121 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700122 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .proc_handler = &proc_dointvec,
124 },
125 { .ctl_name = 0 }
126};
127
128static ctl_table raid_dir_table[] = {
129 {
130 .ctl_name = DEV_RAID,
131 .procname = "raid",
132 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700133 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 .child = raid_table,
135 },
136 { .ctl_name = 0 }
137};
138
139static ctl_table raid_root_table[] = {
140 {
141 .ctl_name = CTL_DEV,
142 .procname = "dev",
143 .maxlen = 0,
144 .mode = 0555,
145 .child = raid_dir_table,
146 },
147 { .ctl_name = 0 }
148};
149
150static struct block_device_operations md_fops;
151
NeilBrownf91de922005-11-08 21:39:36 -0800152static int start_readonly;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
NeilBrownd7603b72006-01-06 00:20:30 -0800155 * We have a system wide 'event count' that is incremented
156 * on any 'interesting' event, and readers of /proc/mdstat
157 * can use 'poll' or 'select' to find out when the event
158 * count increases.
159 *
160 * Events are:
161 * start array, stop array, error, add device, remove device,
162 * start build, activate spare
163 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800164static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800165static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800166void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800167{
168 atomic_inc(&md_event_count);
169 wake_up(&md_event_waiters);
NeilBrown4508a7a2006-03-20 17:53:53 +1100170 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -0800171}
NeilBrown29269552006-03-27 01:18:10 -0800172EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800173
NeilBrownc331eb02006-05-30 21:27:13 -0700174/* Alternate version that can be called from interrupts
175 * when calling sysfs_notify isn't needed.
176 */
Adrian Bunk05381952006-06-26 00:28:01 -0700177static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700178{
179 atomic_inc(&md_event_count);
180 wake_up(&md_event_waiters);
181}
182
NeilBrownd7603b72006-01-06 00:20:30 -0800183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Enables to iterate over all existing md arrays
185 * all_mddevs_lock protects this list.
186 */
187static LIST_HEAD(all_mddevs);
188static DEFINE_SPINLOCK(all_mddevs_lock);
189
190
191/*
192 * iterates through all used mddevs in the system.
193 * We take care to grab the all_mddevs_lock whenever navigating
194 * the list, and to always hold a refcount when unlocked.
195 * Any code which breaks out of this loop while own
196 * a reference to the current mddev and must mddev_put it.
197 */
198#define ITERATE_MDDEV(mddev,tmp) \
199 \
200 for (({ spin_lock(&all_mddevs_lock); \
201 tmp = all_mddevs.next; \
202 mddev = NULL;}); \
203 ({ if (tmp != &all_mddevs) \
204 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
205 spin_unlock(&all_mddevs_lock); \
206 if (mddev) mddev_put(mddev); \
207 mddev = list_entry(tmp, mddev_t, all_mddevs); \
208 tmp != &all_mddevs;}); \
209 ({ spin_lock(&all_mddevs_lock); \
210 tmp = tmp->next;}) \
211 )
212
213
214static int md_fail_request (request_queue_t *q, struct bio *bio)
215{
216 bio_io_error(bio, bio->bi_size);
217 return 0;
218}
219
220static inline mddev_t *mddev_get(mddev_t *mddev)
221{
222 atomic_inc(&mddev->active);
223 return mddev;
224}
225
226static void mddev_put(mddev_t *mddev)
227{
228 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
229 return;
230 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
231 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800232 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500233 blk_cleanup_queue(mddev->queue);
NeilBrowneae17012005-11-08 21:39:23 -0800234 kobject_unregister(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800235 } else
236 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
239static mddev_t * mddev_find(dev_t unit)
240{
241 mddev_t *mddev, *new = NULL;
242
243 retry:
244 spin_lock(&all_mddevs_lock);
245 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
246 if (mddev->unit == unit) {
247 mddev_get(mddev);
248 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700249 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return mddev;
251 }
252
253 if (new) {
254 list_add(&new->all_mddevs, &all_mddevs);
255 spin_unlock(&all_mddevs_lock);
256 return new;
257 }
258 spin_unlock(&all_mddevs_lock);
259
NeilBrown9ffae0c2006-01-06 00:20:32 -0800260 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!new)
262 return NULL;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 new->unit = unit;
265 if (MAJOR(unit) == MD_MAJOR)
266 new->md_minor = MINOR(unit);
267 else
268 new->md_minor = MINOR(unit) >> MdpMinorShift;
269
NeilBrowndf5b89b2006-03-27 01:18:20 -0800270 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 INIT_LIST_HEAD(&new->disks);
272 INIT_LIST_HEAD(&new->all_mddevs);
273 init_timer(&new->safemode_timer);
274 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700275 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700276 init_waitqueue_head(&new->sb_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700277 new->reshape_position = MaxSector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 new->queue = blk_alloc_queue(GFP_KERNEL);
280 if (!new->queue) {
281 kfree(new);
282 return NULL;
283 }
NeilBrown89e5c8b2006-03-27 01:18:02 -0800284 set_bit(QUEUE_FLAG_CLUSTER, &new->queue->queue_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 blk_queue_make_request(new->queue, md_fail_request);
287
288 goto retry;
289}
290
291static inline int mddev_lock(mddev_t * mddev)
292{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800293 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static inline int mddev_trylock(mddev_t * mddev)
297{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800298 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301static inline void mddev_unlock(mddev_t * mddev)
302{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800303 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
NeilBrown005eca52005-08-22 13:11:08 -0700305 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
NeilBrown2989ddb2006-01-06 00:20:43 -0800308static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 mdk_rdev_t * rdev;
311 struct list_head *tmp;
312
313 ITERATE_RDEV(mddev,rdev,tmp) {
314 if (rdev->desc_nr == nr)
315 return rdev;
316 }
317 return NULL;
318}
319
320static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
321{
322 struct list_head *tmp;
323 mdk_rdev_t *rdev;
324
325 ITERATE_RDEV(mddev,rdev,tmp) {
326 if (rdev->bdev->bd_dev == dev)
327 return rdev;
328 }
329 return NULL;
330}
331
NeilBrownd9d166c2006-01-06 00:20:51 -0800332static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800333{
334 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800335 list_for_each_entry(pers, &pers_list, list) {
336 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800337 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800338 if (strcmp(pers->name, clevel)==0)
339 return pers;
340 }
NeilBrown2604b702006-01-06 00:20:36 -0800341 return NULL;
342}
343
Jesper Juhl77933d72005-07-27 11:46:09 -0700344static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
347 return MD_NEW_SIZE_BLOCKS(size);
348}
349
350static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
351{
352 sector_t size;
353
354 size = rdev->sb_offset;
355
356 if (chunk_size)
357 size &= ~((sector_t)chunk_size/1024 - 1);
358 return size;
359}
360
361static int alloc_disk_sb(mdk_rdev_t * rdev)
362{
363 if (rdev->sb_page)
364 MD_BUG();
365
366 rdev->sb_page = alloc_page(GFP_KERNEL);
367 if (!rdev->sb_page) {
368 printk(KERN_ALERT "md: out of memory.\n");
369 return -EINVAL;
370 }
371
372 return 0;
373}
374
375static void free_disk_sb(mdk_rdev_t * rdev)
376{
377 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800378 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 rdev->sb_loaded = 0;
380 rdev->sb_page = NULL;
381 rdev->sb_offset = 0;
382 rdev->size = 0;
383 }
384}
385
386
NeilBrown7bfa19f2005-06-21 17:17:28 -0700387static int super_written(struct bio *bio, unsigned int bytes_done, int error)
388{
389 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800390 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700391 if (bio->bi_size)
392 return 1;
393
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700394 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
395 printk("md: super_written gets error=%d, uptodate=%d\n",
396 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
397 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800398 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700399 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700400
NeilBrowna9701a32005-11-08 21:39:34 -0800401 if (atomic_dec_and_test(&mddev->pending_writes))
402 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700403 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700404 return 0;
405}
406
NeilBrowna9701a32005-11-08 21:39:34 -0800407static int super_written_barrier(struct bio *bio, unsigned int bytes_done, int error)
408{
409 struct bio *bio2 = bio->bi_private;
410 mdk_rdev_t *rdev = bio2->bi_private;
411 mddev_t *mddev = rdev->mddev;
412 if (bio->bi_size)
413 return 1;
414
415 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
416 error == -EOPNOTSUPP) {
417 unsigned long flags;
418 /* barriers don't appear to be supported :-( */
419 set_bit(BarriersNotsupp, &rdev->flags);
420 mddev->barriers_work = 0;
421 spin_lock_irqsave(&mddev->write_lock, flags);
422 bio2->bi_next = mddev->biolist;
423 mddev->biolist = bio2;
424 spin_unlock_irqrestore(&mddev->write_lock, flags);
425 wake_up(&mddev->sb_wait);
426 bio_put(bio);
427 return 0;
428 }
429 bio_put(bio2);
430 bio->bi_private = rdev;
431 return super_written(bio, bytes_done, error);
432}
433
NeilBrown7bfa19f2005-06-21 17:17:28 -0700434void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
435 sector_t sector, int size, struct page *page)
436{
437 /* write first size bytes of page to sector of rdev
438 * Increment mddev->pending_writes before returning
439 * and decrement it on completion, waking up sb_wait
440 * if zero is reached.
441 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800442 *
443 * As we might need to resubmit the request if BIO_RW_BARRIER
444 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700445 */
446 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800447 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700448
449 bio->bi_bdev = rdev->bdev;
450 bio->bi_sector = sector;
451 bio_add_page(bio, page, size, 0);
452 bio->bi_private = rdev;
453 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800454 bio->bi_rw = rw;
455
NeilBrown7bfa19f2005-06-21 17:17:28 -0700456 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800457 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
458 struct bio *rbio;
459 rw |= (1<<BIO_RW_BARRIER);
460 rbio = bio_clone(bio, GFP_NOIO);
461 rbio->bi_private = bio;
462 rbio->bi_end_io = super_written_barrier;
463 submit_bio(rw, rbio);
464 } else
465 submit_bio(rw, bio);
466}
467
468void md_super_wait(mddev_t *mddev)
469{
470 /* wait for all superblock writes that were scheduled to complete.
471 * if any had to be retried (due to BARRIER problems), retry them
472 */
473 DEFINE_WAIT(wq);
474 for(;;) {
475 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
476 if (atomic_read(&mddev->pending_writes)==0)
477 break;
478 while (mddev->biolist) {
479 struct bio *bio;
480 spin_lock_irq(&mddev->write_lock);
481 bio = mddev->biolist;
482 mddev->biolist = bio->bi_next ;
483 bio->bi_next = NULL;
484 spin_unlock_irq(&mddev->write_lock);
485 submit_bio(bio->bi_rw, bio);
486 }
487 schedule();
488 }
489 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static int bi_complete(struct bio *bio, unsigned int bytes_done, int error)
493{
494 if (bio->bi_size)
495 return 1;
496
497 complete((struct completion*)bio->bi_private);
498 return 0;
499}
500
NeilBrowna654b9d82005-06-21 17:17:27 -0700501int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct page *page, int rw)
503{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700504 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 struct completion event;
506 int ret;
507
508 rw |= (1 << BIO_RW_SYNC);
509
510 bio->bi_bdev = bdev;
511 bio->bi_sector = sector;
512 bio_add_page(bio, page, size, 0);
513 init_completion(&event);
514 bio->bi_private = &event;
515 bio->bi_end_io = bi_complete;
516 submit_bio(rw, bio);
517 wait_for_completion(&event);
518
519 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
520 bio_put(bio);
521 return ret;
522}
NeilBrowna8745db2006-01-06 00:20:34 -0800523EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
NeilBrown0002b272005-09-09 16:23:53 -0700525static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 char b[BDEVNAME_SIZE];
528 if (!rdev->sb_page) {
529 MD_BUG();
530 return -EINVAL;
531 }
532 if (rdev->sb_loaded)
533 return 0;
534
535
NeilBrown0002b272005-09-09 16:23:53 -0700536 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 goto fail;
538 rdev->sb_loaded = 1;
539 return 0;
540
541fail:
542 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
543 bdevname(rdev->bdev,b));
544 return -EINVAL;
545}
546
547static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
548{
549 if ( (sb1->set_uuid0 == sb2->set_uuid0) &&
550 (sb1->set_uuid1 == sb2->set_uuid1) &&
551 (sb1->set_uuid2 == sb2->set_uuid2) &&
552 (sb1->set_uuid3 == sb2->set_uuid3))
553
554 return 1;
555
556 return 0;
557}
558
559
560static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
561{
562 int ret;
563 mdp_super_t *tmp1, *tmp2;
564
565 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
566 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
567
568 if (!tmp1 || !tmp2) {
569 ret = 0;
570 printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
571 goto abort;
572 }
573
574 *tmp1 = *sb1;
575 *tmp2 = *sb2;
576
577 /*
578 * nr_disks is not constant
579 */
580 tmp1->nr_disks = 0;
581 tmp2->nr_disks = 0;
582
583 if (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4))
584 ret = 0;
585 else
586 ret = 1;
587
588abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700589 kfree(tmp1);
590 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return ret;
592}
593
NeilBrown4d167f02007-05-09 02:35:37 -0700594
595static u32 md_csum_fold(u32 csum)
596{
597 csum = (csum & 0xffff) + (csum >> 16);
598 return (csum & 0xffff) + (csum >> 16);
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static unsigned int calc_sb_csum(mdp_super_t * sb)
602{
NeilBrown4d167f02007-05-09 02:35:37 -0700603 u64 newcsum = 0;
604 u32 *sb32 = (u32*)sb;
605 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unsigned int disk_csum, csum;
607
608 disk_csum = sb->sb_csum;
609 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700610
611 for (i = 0; i < MD_SB_BYTES/4 ; i++)
612 newcsum += sb32[i];
613 csum = (newcsum & 0xffffffff) + (newcsum>>32);
614
615
616#ifdef CONFIG_ALPHA
617 /* This used to use csum_partial, which was wrong for several
618 * reasons including that different results are returned on
619 * different architectures. It isn't critical that we get exactly
620 * the same return value as before (we always csum_fold before
621 * testing, and that removes any differences). However as we
622 * know that csum_partial always returned a 16bit value on
623 * alphas, do a fold to maximise conformity to previous behaviour.
624 */
625 sb->sb_csum = md_csum_fold(disk_csum);
626#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return csum;
630}
631
632
633/*
634 * Handle superblock details.
635 * We want to be able to handle multiple superblock formats
636 * so we have a common interface to them all, and an array of
637 * different handlers.
638 * We rely on user-space to write the initial superblock, and support
639 * reading and updating of superblocks.
640 * Interface methods are:
641 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
642 * loads and validates a superblock on dev.
643 * if refdev != NULL, compare superblocks on both devices
644 * Return:
645 * 0 - dev has a superblock that is compatible with refdev
646 * 1 - dev has a superblock that is compatible and newer than refdev
647 * so dev should be used as the refdev in future
648 * -EINVAL superblock incompatible or invalid
649 * -othererror e.g. -EIO
650 *
651 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
652 * Verify that dev is acceptable into mddev.
653 * The first time, mddev->raid_disks will be 0, and data from
654 * dev should be merged in. Subsequent calls check that dev
655 * is new enough. Return 0 or -EINVAL
656 *
657 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
658 * Update the superblock for rdev with data in mddev
659 * This does not write to disc.
660 *
661 */
662
663struct super_type {
664 char *name;
665 struct module *owner;
666 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version);
667 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
668 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
669};
670
671/*
672 * load_super for 0.90.0
673 */
674static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
675{
676 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
677 mdp_super_t *sb;
678 int ret;
679 sector_t sb_offset;
680
681 /*
682 * Calculate the position of the superblock,
683 * it's at the end of the disk.
684 *
685 * It also happens to be a multiple of 4Kb.
686 */
687 sb_offset = calc_dev_sboffset(rdev->bdev);
688 rdev->sb_offset = sb_offset;
689
NeilBrown0002b272005-09-09 16:23:53 -0700690 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (ret) return ret;
692
693 ret = -EINVAL;
694
695 bdevname(rdev->bdev, b);
696 sb = (mdp_super_t*)page_address(rdev->sb_page);
697
698 if (sb->md_magic != MD_SB_MAGIC) {
699 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
700 b);
701 goto abort;
702 }
703
704 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800705 sb->minor_version < 90 ||
706 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
708 sb->major_version, sb->minor_version,
709 b);
710 goto abort;
711 }
712
713 if (sb->raid_disks <= 0)
714 goto abort;
715
NeilBrown4d167f02007-05-09 02:35:37 -0700716 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
718 b);
719 goto abort;
720 }
721
722 rdev->preferred_minor = sb->md_minor;
723 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700724 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
NeilBrowne11e93f2007-05-09 02:35:36 -0700726 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
727 if (sb->level != 1 && sb->level != 4
728 && sb->level != 5 && sb->level != 6
729 && sb->level != 10) {
730 /* FIXME use a better test */
731 printk(KERN_WARNING
732 "md: bitmaps not supported for this level.\n");
733 goto abort;
734 }
735 }
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (sb->level == LEVEL_MULTIPATH)
738 rdev->desc_nr = -1;
739 else
740 rdev->desc_nr = sb->this_disk.number;
741
742 if (refdev == 0)
743 ret = 1;
744 else {
745 __u64 ev1, ev2;
746 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
747 if (!uuid_equal(refsb, sb)) {
748 printk(KERN_WARNING "md: %s has different UUID to %s\n",
749 b, bdevname(refdev->bdev,b2));
750 goto abort;
751 }
752 if (!sb_equal(refsb, sb)) {
753 printk(KERN_WARNING "md: %s has same UUID"
754 " but different superblock to %s\n",
755 b, bdevname(refdev->bdev, b2));
756 goto abort;
757 }
758 ev1 = md_event(sb);
759 ev2 = md_event(refsb);
760 if (ev1 > ev2)
761 ret = 1;
762 else
763 ret = 0;
764 }
765 rdev->size = calc_dev_size(rdev, sb->chunk_size);
766
NeilBrown2bf071b2006-01-06 00:20:55 -0800767 if (rdev->size < sb->size && sb->level > 1)
768 /* "this cannot possibly happen" ... */
769 ret = -EINVAL;
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 abort:
772 return ret;
773}
774
775/*
776 * validate_super for 0.90.0
777 */
778static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
779{
780 mdp_disk_t *desc;
781 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700782 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
NeilBrown41158c72005-06-21 17:17:25 -0700784 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -0800785 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (mddev->raid_disks == 0) {
787 mddev->major_version = 0;
788 mddev->minor_version = sb->minor_version;
789 mddev->patch_version = sb->patch_version;
790 mddev->persistent = ! sb->not_persistent;
791 mddev->chunk_size = sb->chunk_size;
792 mddev->ctime = sb->ctime;
793 mddev->utime = sb->utime;
794 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800795 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 mddev->layout = sb->layout;
797 mddev->raid_disks = sb->raid_disks;
798 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700799 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700800 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700801 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
NeilBrownf6705572006-03-27 01:18:11 -0800803 if (mddev->minor_version >= 91) {
804 mddev->reshape_position = sb->reshape_position;
805 mddev->delta_disks = sb->delta_disks;
806 mddev->new_level = sb->new_level;
807 mddev->new_layout = sb->new_layout;
808 mddev->new_chunk = sb->new_chunk;
809 } else {
810 mddev->reshape_position = MaxSector;
811 mddev->delta_disks = 0;
812 mddev->new_level = mddev->level;
813 mddev->new_layout = mddev->layout;
814 mddev->new_chunk = mddev->chunk_size;
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (sb->state & (1<<MD_SB_CLEAN))
818 mddev->recovery_cp = MaxSector;
819 else {
820 if (sb->events_hi == sb->cp_events_hi &&
821 sb->events_lo == sb->cp_events_lo) {
822 mddev->recovery_cp = sb->recovery_cp;
823 } else
824 mddev->recovery_cp = 0;
825 }
826
827 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
828 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
829 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
830 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
831
832 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700833
834 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700835 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700836 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700837
NeilBrown41158c72005-06-21 17:17:25 -0700838 } else if (mddev->pers == NULL) {
839 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 ++ev1;
841 if (ev1 < mddev->events)
842 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700843 } else if (mddev->bitmap) {
844 /* if adding to array with a bitmap, then we can accept an
845 * older device ... but not too old.
846 */
NeilBrown41158c72005-06-21 17:17:25 -0700847 if (ev1 < mddev->bitmap->events_cleared)
848 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700849 } else {
850 if (ev1 < mddev->events)
851 /* just a hot-add of a new device, leave raid_disk at -1 */
852 return 0;
853 }
NeilBrown41158c72005-06-21 17:17:25 -0700854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 desc = sb->disks + rdev->desc_nr;
857
858 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800859 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700860 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
861 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800862 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 rdev->raid_disk = desc->raid_disk;
864 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700865 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
866 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700867 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800868 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return 0;
870}
871
872/*
873 * sync_super for 0.90.0
874 */
875static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
876{
877 mdp_super_t *sb;
878 struct list_head *tmp;
879 mdk_rdev_t *rdev2;
880 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883 /* make rdev->sb match mddev data..
884 *
885 * 1/ zero out disks
886 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
887 * 3/ any empty disks < next_spare become removed
888 *
889 * disks[0] gets initialised to REMOVED because
890 * we cannot be sure from other fields if it has
891 * been initialised or not.
892 */
893 int i;
894 int active=0, working=0,failed=0,spare=0,nr_disks=0;
895
NeilBrown61181562005-09-09 16:24:02 -0700896 rdev->sb_size = MD_SB_BYTES;
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 sb = (mdp_super_t*)page_address(rdev->sb_page);
899
900 memset(sb, 0, sizeof(*sb));
901
902 sb->md_magic = MD_SB_MAGIC;
903 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 sb->patch_version = mddev->patch_version;
905 sb->gvalid_words = 0; /* ignored */
906 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
907 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
908 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
909 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
910
911 sb->ctime = mddev->ctime;
912 sb->level = mddev->level;
913 sb->size = mddev->size;
914 sb->raid_disks = mddev->raid_disks;
915 sb->md_minor = mddev->md_minor;
916 sb->not_persistent = !mddev->persistent;
917 sb->utime = mddev->utime;
918 sb->state = 0;
919 sb->events_hi = (mddev->events>>32);
920 sb->events_lo = (u32)mddev->events;
921
NeilBrownf6705572006-03-27 01:18:11 -0800922 if (mddev->reshape_position == MaxSector)
923 sb->minor_version = 90;
924 else {
925 sb->minor_version = 91;
926 sb->reshape_position = mddev->reshape_position;
927 sb->new_level = mddev->new_level;
928 sb->delta_disks = mddev->delta_disks;
929 sb->new_layout = mddev->new_layout;
930 sb->new_chunk = mddev->new_chunk;
931 }
932 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 if (mddev->in_sync)
934 {
935 sb->recovery_cp = mddev->recovery_cp;
936 sb->cp_events_hi = (mddev->events>>32);
937 sb->cp_events_lo = (u32)mddev->events;
938 if (mddev->recovery_cp == MaxSector)
939 sb->state = (1<< MD_SB_CLEAN);
940 } else
941 sb->recovery_cp = 0;
942
943 sb->layout = mddev->layout;
944 sb->chunk_size = mddev->chunk_size;
945
NeilBrowna654b9d82005-06-21 17:17:27 -0700946 if (mddev->bitmap && mddev->bitmap_file == NULL)
947 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 sb->disks[0].state = (1<<MD_DISK_REMOVED);
950 ITERATE_RDEV(mddev,rdev2,tmp) {
951 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800952 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800953 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
954 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800955 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800957 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800958 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 d = &sb->disks[rdev2->desc_nr];
960 nr_disks++;
961 d->number = rdev2->desc_nr;
962 d->major = MAJOR(rdev2->bdev->bd_dev);
963 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800964 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
965 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 d->raid_disk = rdev2->raid_disk;
967 else
968 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800969 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800971 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 d->state = (1<<MD_DISK_ACTIVE);
973 d->state |= (1<<MD_DISK_SYNC);
974 active++;
975 working++;
976 } else {
977 d->state = 0;
978 spare++;
979 working++;
980 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700981 if (test_bit(WriteMostly, &rdev2->flags))
982 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /* now set the "removed" and "faulty" bits on any missing devices */
985 for (i=0 ; i < mddev->raid_disks ; i++) {
986 mdp_disk_t *d = &sb->disks[i];
987 if (d->state == 0 && d->number == 0) {
988 d->number = i;
989 d->raid_disk = i;
990 d->state = (1<<MD_DISK_REMOVED);
991 d->state |= (1<<MD_DISK_FAULTY);
992 failed++;
993 }
994 }
995 sb->nr_disks = nr_disks;
996 sb->active_disks = active;
997 sb->working_disks = working;
998 sb->failed_disks = failed;
999 sb->spare_disks = spare;
1000
1001 sb->this_disk = sb->disks[rdev->desc_nr];
1002 sb->sb_csum = calc_sb_csum(sb);
1003}
1004
1005/*
1006 * version 1 superblock
1007 */
1008
NeilBrown1c05b4b2006-10-21 10:24:08 -07001009static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001011 __le32 disk_csum;
1012 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 unsigned long long newcsum;
1014 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001015 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int i;
1017
1018 disk_csum = sb->sb_csum;
1019 sb->sb_csum = 0;
1020 newcsum = 0;
1021 for (i=0; size>=4; size -= 4 )
1022 newcsum += le32_to_cpu(*isuper++);
1023
1024 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001025 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1028 sb->sb_csum = disk_csum;
1029 return cpu_to_le32(csum);
1030}
1031
1032static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1033{
1034 struct mdp_superblock_1 *sb;
1035 int ret;
1036 sector_t sb_offset;
1037 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001038 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /*
1041 * Calculate the position of the superblock.
1042 * It is always aligned to a 4K boundary and
1043 * depeding on minor_version, it can be:
1044 * 0: At least 8K, but less than 12K, from end of device
1045 * 1: At start of device
1046 * 2: 4K from start of device.
1047 */
1048 switch(minor_version) {
1049 case 0:
1050 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1051 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001052 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* convert from sectors to K */
1054 sb_offset /= 2;
1055 break;
1056 case 1:
1057 sb_offset = 0;
1058 break;
1059 case 2:
1060 sb_offset = 4;
1061 break;
1062 default:
1063 return -EINVAL;
1064 }
1065 rdev->sb_offset = sb_offset;
1066
NeilBrown0002b272005-09-09 16:23:53 -07001067 /* superblock is rarely larger than 1K, but it can be larger,
1068 * and it is safe to read 4k, so we do that
1069 */
1070 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (ret) return ret;
1072
1073
1074 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1075
1076 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1077 sb->major_version != cpu_to_le32(1) ||
1078 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1079 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001080 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return -EINVAL;
1082
1083 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1084 printk("md: invalid superblock checksum on %s\n",
1085 bdevname(rdev->bdev,b));
1086 return -EINVAL;
1087 }
1088 if (le64_to_cpu(sb->data_size) < 10) {
1089 printk("md: data_size too small on %s\n",
1090 bdevname(rdev->bdev,b));
1091 return -EINVAL;
1092 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001093 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1094 if (sb->level != cpu_to_le32(1) &&
1095 sb->level != cpu_to_le32(4) &&
1096 sb->level != cpu_to_le32(5) &&
1097 sb->level != cpu_to_le32(6) &&
1098 sb->level != cpu_to_le32(10)) {
1099 printk(KERN_WARNING
1100 "md: bitmaps not supported for this level.\n");
1101 return -EINVAL;
1102 }
1103 }
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 rdev->preferred_minor = 0xffff;
1106 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001107 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
NeilBrown0002b272005-09-09 16:23:53 -07001109 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001110 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001111 if (rdev->sb_size & bmask)
1112 rdev-> sb_size = (rdev->sb_size | bmask)+1;
1113
NeilBrown31b65a02006-07-10 04:44:14 -07001114 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1115 rdev->desc_nr = -1;
1116 else
1117 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (refdev == 0)
NeilBrown8ed75462006-02-03 03:03:41 -08001120 ret = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 else {
1122 __u64 ev1, ev2;
1123 struct mdp_superblock_1 *refsb =
1124 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1125
1126 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1127 sb->level != refsb->level ||
1128 sb->layout != refsb->layout ||
1129 sb->chunksize != refsb->chunksize) {
1130 printk(KERN_WARNING "md: %s has strangely different"
1131 " superblock to %s\n",
1132 bdevname(rdev->bdev,b),
1133 bdevname(refdev->bdev,b2));
1134 return -EINVAL;
1135 }
1136 ev1 = le64_to_cpu(sb->events);
1137 ev2 = le64_to_cpu(refsb->events);
1138
1139 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001140 ret = 1;
1141 else
1142 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144 if (minor_version)
1145 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1146 else
1147 rdev->size = rdev->sb_offset;
1148 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1149 return -EINVAL;
1150 rdev->size = le64_to_cpu(sb->data_size)/2;
1151 if (le32_to_cpu(sb->chunksize))
1152 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001153
NeilBrown1c05b4b2006-10-21 10:24:08 -07001154 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001155 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001156 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1160{
1161 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001162 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
NeilBrown41158c72005-06-21 17:17:25 -07001164 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08001165 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (mddev->raid_disks == 0) {
1167 mddev->major_version = 1;
1168 mddev->patch_version = 0;
1169 mddev->persistent = 1;
1170 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1171 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1172 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1173 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001174 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 mddev->layout = le32_to_cpu(sb->layout);
1176 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1177 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001178 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001179 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001180 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1183 memcpy(mddev->uuid, sb->set_uuid, 16);
1184
1185 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001186
NeilBrown71c08052005-09-09 16:23:51 -07001187 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001188 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001189 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001190
NeilBrownf6705572006-03-27 01:18:11 -08001191 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1192 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1193 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1194 mddev->new_level = le32_to_cpu(sb->new_level);
1195 mddev->new_layout = le32_to_cpu(sb->new_layout);
1196 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1197 } else {
1198 mddev->reshape_position = MaxSector;
1199 mddev->delta_disks = 0;
1200 mddev->new_level = mddev->level;
1201 mddev->new_layout = mddev->layout;
1202 mddev->new_chunk = mddev->chunk_size;
1203 }
1204
NeilBrown41158c72005-06-21 17:17:25 -07001205 } else if (mddev->pers == NULL) {
1206 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 ++ev1;
1208 if (ev1 < mddev->events)
1209 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001210 } else if (mddev->bitmap) {
1211 /* If adding to array with a bitmap, then we can accept an
1212 * older device, but not too old.
1213 */
NeilBrown41158c72005-06-21 17:17:25 -07001214 if (ev1 < mddev->bitmap->events_cleared)
1215 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001216 } else {
1217 if (ev1 < mddev->events)
1218 /* just a hot-add of a new device, leave raid_disk at -1 */
1219 return 0;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (mddev->level != LEVEL_MULTIPATH) {
1222 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1224 switch(role) {
1225 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 break;
1227 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001228 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 break;
1230 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001231 if ((le32_to_cpu(sb->feature_map) &
1232 MD_FEATURE_RECOVERY_OFFSET))
1233 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1234 else
1235 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 rdev->raid_disk = role;
1237 break;
1238 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001239 if (sb->devflags & WriteMostly1)
1240 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001241 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001242 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return 0;
1245}
1246
1247static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1248{
1249 struct mdp_superblock_1 *sb;
1250 struct list_head *tmp;
1251 mdk_rdev_t *rdev2;
1252 int max_dev, i;
1253 /* make rdev->sb match mddev and rdev data. */
1254
1255 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1256
1257 sb->feature_map = 0;
1258 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001259 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 memset(sb->pad1, 0, sizeof(sb->pad1));
1261 memset(sb->pad2, 0, sizeof(sb->pad2));
1262 memset(sb->pad3, 0, sizeof(sb->pad3));
1263
1264 sb->utime = cpu_to_le64((__u64)mddev->utime);
1265 sb->events = cpu_to_le64(mddev->events);
1266 if (mddev->in_sync)
1267 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1268 else
1269 sb->resync_offset = cpu_to_le64(0);
1270
NeilBrown1c05b4b2006-10-21 10:24:08 -07001271 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001272
NeilBrownf0ca3402006-02-02 14:28:04 -08001273 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001274 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001275
NeilBrowna654b9d82005-06-21 17:17:27 -07001276 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1277 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001278 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001279 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001280
1281 if (rdev->raid_disk >= 0 &&
1282 !test_bit(In_sync, &rdev->flags) &&
1283 rdev->recovery_offset > 0) {
1284 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1285 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1286 }
1287
NeilBrownf6705572006-03-27 01:18:11 -08001288 if (mddev->reshape_position != MaxSector) {
1289 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1290 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1291 sb->new_layout = cpu_to_le32(mddev->new_layout);
1292 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1293 sb->new_level = cpu_to_le32(mddev->new_level);
1294 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1295 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 max_dev = 0;
1298 ITERATE_RDEV(mddev,rdev2,tmp)
1299 if (rdev2->desc_nr+1 > max_dev)
1300 max_dev = rdev2->desc_nr+1;
1301
1302 sb->max_dev = cpu_to_le32(max_dev);
1303 for (i=0; i<max_dev;i++)
1304 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1305
1306 ITERATE_RDEV(mddev,rdev2,tmp) {
1307 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001308 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001310 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001312 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1313 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 else
1315 sb->dev_roles[i] = cpu_to_le16(0xffff);
1316 }
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 sb->sb_csum = calc_sb_1_csum(sb);
1319}
1320
1321
Adrian Bunk75c96f82005-05-05 16:16:09 -07001322static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 [0] = {
1324 .name = "0.90.0",
1325 .owner = THIS_MODULE,
1326 .load_super = super_90_load,
1327 .validate_super = super_90_validate,
1328 .sync_super = super_90_sync,
1329 },
1330 [1] = {
1331 .name = "md-1",
1332 .owner = THIS_MODULE,
1333 .load_super = super_1_load,
1334 .validate_super = super_1_validate,
1335 .sync_super = super_1_sync,
1336 },
1337};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1340{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001341 struct list_head *tmp, *tmp2;
1342 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
1344 ITERATE_RDEV(mddev1,rdev,tmp)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001345 ITERATE_RDEV(mddev2, rdev2, tmp2)
1346 if (rdev->bdev->bd_contains ==
1347 rdev2->bdev->bd_contains)
1348 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 return 0;
1351}
1352
1353static LIST_HEAD(pending_raid_disks);
1354
1355static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1356{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001357 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001358 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001359 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 if (rdev->mddev) {
1363 MD_BUG();
1364 return -EINVAL;
1365 }
NeilBrown2bf071b2006-01-06 00:20:55 -08001366 /* make sure rdev->size exceeds mddev->size */
1367 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
1368 if (mddev->pers)
1369 /* Cannot change size, so fail */
1370 return -ENOSPC;
1371 else
1372 mddev->size = rdev->size;
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /* Verify rdev->desc_nr is unique.
1376 * If it is -1, assign a free number, else
1377 * check number is not in use
1378 */
1379 if (rdev->desc_nr < 0) {
1380 int choice = 0;
1381 if (mddev->pers) choice = mddev->raid_disks;
1382 while (find_rdev_nr(mddev, choice))
1383 choice++;
1384 rdev->desc_nr = choice;
1385 } else {
1386 if (find_rdev_nr(mddev, rdev->desc_nr))
1387 return -EBUSY;
1388 }
NeilBrown19133a42005-11-08 21:39:35 -08001389 bdevname(rdev->bdev,b);
1390 if (kobject_set_name(&rdev->kobj, "dev-%s", b) < 0)
1391 return -ENOMEM;
Neil Brown1edf80d2006-01-12 01:05:23 -08001392 while ( (s=strchr(rdev->kobj.k_name, '/')) != NULL)
1393 *s = '!';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001396 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001397
NeilBrown9c791972005-11-08 21:39:28 -08001398 rdev->kobj.parent = &mddev->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001399 if ((err = kobject_add(&rdev->kobj)))
1400 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001401
NeilBrownf637b9f2005-11-08 21:39:37 -08001402 if (rdev->bdev->bd_part)
1403 ko = &rdev->bdev->bd_part->kobj;
1404 else
1405 ko = &rdev->bdev->bd_disk->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001406 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1407 kobject_del(&rdev->kobj);
1408 goto fail;
1409 }
1410 list_add(&rdev->same_set, &mddev->disks);
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001411 bd_claim_by_disk(rdev->bdev, rdev, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001413
1414 fail:
1415 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1416 b, mdname(mddev));
1417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
NeilBrown5792a282007-04-04 19:08:18 -07001420static void delayed_delete(struct work_struct *ws)
1421{
1422 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1423 kobject_del(&rdev->kobj);
1424}
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1427{
1428 char b[BDEVNAME_SIZE];
1429 if (!rdev->mddev) {
1430 MD_BUG();
1431 return;
1432 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001433 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 list_del_init(&rdev->same_set);
1435 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1436 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001437 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001438
1439 /* We need to delay this, otherwise we can deadlock when
1440 * writing to 'remove' to "dev/state"
1441 */
1442 INIT_WORK(&rdev->del_work, delayed_delete);
1443 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446/*
1447 * prevent the device from being mounted, repartitioned or
1448 * otherwise reused by a RAID array (or any other kernel
1449 * subsystem), by bd_claiming the device.
1450 */
1451static int lock_rdev(mdk_rdev_t *rdev, dev_t dev)
1452{
1453 int err = 0;
1454 struct block_device *bdev;
1455 char b[BDEVNAME_SIZE];
1456
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001457 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 if (IS_ERR(bdev)) {
1459 printk(KERN_ERR "md: could not open %s.\n",
1460 __bdevname(dev, b));
1461 return PTR_ERR(bdev);
1462 }
1463 err = bd_claim(bdev, rdev);
1464 if (err) {
1465 printk(KERN_ERR "md: could not bd_claim %s.\n",
1466 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001467 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return err;
1469 }
1470 rdev->bdev = bdev;
1471 return err;
1472}
1473
1474static void unlock_rdev(mdk_rdev_t *rdev)
1475{
1476 struct block_device *bdev = rdev->bdev;
1477 rdev->bdev = NULL;
1478 if (!bdev)
1479 MD_BUG();
1480 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001481 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484void md_autodetect_dev(dev_t dev);
1485
1486static void export_rdev(mdk_rdev_t * rdev)
1487{
1488 char b[BDEVNAME_SIZE];
1489 printk(KERN_INFO "md: export_rdev(%s)\n",
1490 bdevname(rdev->bdev,b));
1491 if (rdev->mddev)
1492 MD_BUG();
1493 free_disk_sb(rdev);
1494 list_del_init(&rdev->same_set);
1495#ifndef MODULE
1496 md_autodetect_dev(rdev->bdev->bd_dev);
1497#endif
1498 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001499 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502static void kick_rdev_from_array(mdk_rdev_t * rdev)
1503{
1504 unbind_rdev_from_array(rdev);
1505 export_rdev(rdev);
1506}
1507
1508static void export_array(mddev_t *mddev)
1509{
1510 struct list_head *tmp;
1511 mdk_rdev_t *rdev;
1512
1513 ITERATE_RDEV(mddev,rdev,tmp) {
1514 if (!rdev->mddev) {
1515 MD_BUG();
1516 continue;
1517 }
1518 kick_rdev_from_array(rdev);
1519 }
1520 if (!list_empty(&mddev->disks))
1521 MD_BUG();
1522 mddev->raid_disks = 0;
1523 mddev->major_version = 0;
1524}
1525
1526static void print_desc(mdp_disk_t *desc)
1527{
1528 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1529 desc->major,desc->minor,desc->raid_disk,desc->state);
1530}
1531
1532static void print_sb(mdp_super_t *sb)
1533{
1534 int i;
1535
1536 printk(KERN_INFO
1537 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1538 sb->major_version, sb->minor_version, sb->patch_version,
1539 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1540 sb->ctime);
1541 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1542 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1543 sb->md_minor, sb->layout, sb->chunk_size);
1544 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1545 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1546 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1547 sb->failed_disks, sb->spare_disks,
1548 sb->sb_csum, (unsigned long)sb->events_lo);
1549
1550 printk(KERN_INFO);
1551 for (i = 0; i < MD_SB_DISKS; i++) {
1552 mdp_disk_t *desc;
1553
1554 desc = sb->disks + i;
1555 if (desc->number || desc->major || desc->minor ||
1556 desc->raid_disk || (desc->state && (desc->state != 4))) {
1557 printk(" D %2d: ", i);
1558 print_desc(desc);
1559 }
1560 }
1561 printk(KERN_INFO "md: THIS: ");
1562 print_desc(&sb->this_disk);
1563
1564}
1565
1566static void print_rdev(mdk_rdev_t *rdev)
1567{
1568 char b[BDEVNAME_SIZE];
1569 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1570 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001571 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1572 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if (rdev->sb_loaded) {
1574 printk(KERN_INFO "md: rdev superblock:\n");
1575 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1576 } else
1577 printk(KERN_INFO "md: no rdev superblock!\n");
1578}
1579
Adrian Bunk5e563412006-06-26 00:27:42 -07001580static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 struct list_head *tmp, *tmp2;
1583 mdk_rdev_t *rdev;
1584 mddev_t *mddev;
1585 char b[BDEVNAME_SIZE];
1586
1587 printk("\n");
1588 printk("md: **********************************\n");
1589 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1590 printk("md: **********************************\n");
1591 ITERATE_MDDEV(mddev,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
NeilBrown32a76272005-06-21 17:17:14 -07001593 if (mddev->bitmap)
1594 bitmap_print_sb(mddev->bitmap);
1595 else
1596 printk("%s: ", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 ITERATE_RDEV(mddev,rdev,tmp2)
1598 printk("<%s>", bdevname(rdev->bdev,b));
1599 printk("\n");
1600
1601 ITERATE_RDEV(mddev,rdev,tmp2)
1602 print_rdev(rdev);
1603 }
1604 printk("md: **********************************\n");
1605 printk("\n");
1606}
1607
1608
NeilBrown42543762006-06-26 00:27:57 -07001609static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
NeilBrown42543762006-06-26 00:27:57 -07001611 /* Update each superblock (in-memory image), but
1612 * if we are allowed to, skip spares which already
1613 * have the right event counter, or have one earlier
1614 * (which would mean they aren't being marked as dirty
1615 * with the rest of the array)
1616 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 mdk_rdev_t *rdev;
1618 struct list_head *tmp;
1619
1620 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrown42543762006-06-26 00:27:57 -07001621 if (rdev->sb_events == mddev->events ||
1622 (nospares &&
1623 rdev->raid_disk < 0 &&
1624 (rdev->sb_events&1)==0 &&
1625 rdev->sb_events+1 == mddev->events)) {
1626 /* Don't update this superblock */
1627 rdev->sb_loaded = 2;
1628 } else {
1629 super_types[mddev->major_version].
1630 sync_super(mddev, rdev);
1631 rdev->sb_loaded = 1;
1632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 }
1634}
1635
NeilBrown850b2b42006-10-03 01:15:46 -07001636static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
NeilBrown7bfa19f2005-06-21 17:17:28 -07001638 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 struct list_head *tmp;
1640 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001641 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001642 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001645 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001646
NeilBrown850b2b42006-10-03 01:15:46 -07001647 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1648 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1649 force_change = 1;
1650 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1651 /* just a clean<-> dirty transition, possibly leave spares alone,
1652 * though if events isn't the right even/odd, we will have to do
1653 * spares after all
1654 */
1655 nospares = 1;
1656 if (force_change)
1657 nospares = 0;
1658 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001659 /* If the array is degraded, then skipping spares is both
1660 * dangerous and fairly pointless.
1661 * Dangerous because a device that was removed from the array
1662 * might have a event_count that still looks up-to-date,
1663 * so it can be re-added without a resync.
1664 * Pointless because if there are any spares to skip,
1665 * then a recovery will happen and soon that array won't
1666 * be degraded any more and the spare can go back to sleep then.
1667 */
NeilBrown850b2b42006-10-03 01:15:46 -07001668 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001669
NeilBrown06d91a52005-06-21 17:17:12 -07001670 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001672
1673 /* If this is just a dirty<->clean transition, and the array is clean
1674 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001675 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001676 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001677 && (mddev->events & 1)
1678 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001679 mddev->events--;
1680 else {
1681 /* otherwise we have to go forward and ... */
1682 mddev->events ++;
1683 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1684 /* .. if the array isn't clean, insist on an odd 'events' */
1685 if ((mddev->events&1)==0) {
1686 mddev->events++;
1687 nospares = 0;
1688 }
1689 } else {
1690 /* otherwise insist on an even 'events' (for clean states) */
1691 if ((mddev->events&1)) {
1692 mddev->events++;
1693 nospares = 0;
1694 }
1695 }
1696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698 if (!mddev->events) {
1699 /*
1700 * oops, this 64-bit counter should never wrap.
1701 * Either we are in around ~1 trillion A.C., assuming
1702 * 1 reboot per second, or we have a bug:
1703 */
1704 MD_BUG();
1705 mddev->events --;
1706 }
NeilBrown42543762006-06-26 00:27:57 -07001707 sync_sbs(mddev, nospares);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 /*
1710 * do not write anything to disk if using
1711 * nonpersistent superblocks
1712 */
NeilBrown06d91a52005-06-21 17:17:12 -07001713 if (!mddev->persistent) {
NeilBrown850b2b42006-10-03 01:15:46 -07001714 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001715 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001716 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001718 }
NeilBrowna9701a32005-11-08 21:39:34 -08001719 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 dprintk(KERN_INFO
1722 "md: updating %s RAID superblock on device (in sync %d)\n",
1723 mdname(mddev),mddev->in_sync);
1724
NeilBrown32a76272005-06-21 17:17:14 -07001725 err = bitmap_update_sb(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 ITERATE_RDEV(mddev,rdev,tmp) {
1727 char b[BDEVNAME_SIZE];
1728 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001729 if (rdev->sb_loaded != 1)
1730 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001731 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 dprintk("(skipping faulty ");
1733
1734 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001735 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001736 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001737 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001738 rdev->sb_page);
1739 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1740 bdevname(rdev->bdev,b),
1741 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001742 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 } else
1745 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001746 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /* only need to write one superblock... */
1748 break;
1749 }
NeilBrowna9701a32005-11-08 21:39:34 -08001750 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001751 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001752
NeilBrowna9701a32005-11-08 21:39:34 -08001753 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001754 if (mddev->in_sync != sync_req ||
1755 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001756 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001757 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001758 goto repeat;
1759 }
NeilBrown850b2b42006-10-03 01:15:46 -07001760 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001761 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001762 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
NeilBrownbce74da2006-01-06 00:20:41 -08001766/* words written to sysfs files may, or my not, be \n terminated.
1767 * We want to accept with case. For this we use cmd_match.
1768 */
1769static int cmd_match(const char *cmd, const char *str)
1770{
1771 /* See if cmd, written into a sysfs file, matches
1772 * str. They must either be the same, or cmd can
1773 * have a trailing newline
1774 */
1775 while (*cmd && *str && *cmd == *str) {
1776 cmd++;
1777 str++;
1778 }
1779 if (*cmd == '\n')
1780 cmd++;
1781 if (*str || *cmd)
1782 return 0;
1783 return 1;
1784}
1785
NeilBrown86e6ffd2005-11-08 21:39:24 -08001786struct rdev_sysfs_entry {
1787 struct attribute attr;
1788 ssize_t (*show)(mdk_rdev_t *, char *);
1789 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1790};
1791
1792static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001793state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001794{
1795 char *sep = "";
1796 int len=0;
1797
NeilBrownb2d444d2005-11-08 21:39:31 -08001798 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001799 len+= sprintf(page+len, "%sfaulty",sep);
1800 sep = ",";
1801 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001802 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001803 len += sprintf(page+len, "%sin_sync",sep);
1804 sep = ",";
1805 }
NeilBrownf6556752006-06-26 00:28:01 -07001806 if (test_bit(WriteMostly, &rdev->flags)) {
1807 len += sprintf(page+len, "%swrite_mostly",sep);
1808 sep = ",";
1809 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001810 if (!test_bit(Faulty, &rdev->flags) &&
1811 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001812 len += sprintf(page+len, "%sspare", sep);
1813 sep = ",";
1814 }
1815 return len+sprintf(page+len, "\n");
1816}
1817
NeilBrown45dc2de2006-06-26 00:27:58 -07001818static ssize_t
1819state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1820{
1821 /* can write
1822 * faulty - simulates and error
1823 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001824 * writemostly - sets write_mostly
1825 * -writemostly - clears write_mostly
NeilBrown45dc2de2006-06-26 00:27:58 -07001826 */
1827 int err = -EINVAL;
1828 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1829 md_error(rdev->mddev, rdev);
1830 err = 0;
1831 } else if (cmd_match(buf, "remove")) {
1832 if (rdev->raid_disk >= 0)
1833 err = -EBUSY;
1834 else {
1835 mddev_t *mddev = rdev->mddev;
1836 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001837 if (mddev->pers)
1838 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001839 md_new_event(mddev);
1840 err = 0;
1841 }
NeilBrownf6556752006-06-26 00:28:01 -07001842 } else if (cmd_match(buf, "writemostly")) {
1843 set_bit(WriteMostly, &rdev->flags);
1844 err = 0;
1845 } else if (cmd_match(buf, "-writemostly")) {
1846 clear_bit(WriteMostly, &rdev->flags);
1847 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001848 }
1849 return err ? err : len;
1850}
NeilBrown80ca3a42006-07-10 04:44:18 -07001851static struct rdev_sysfs_entry rdev_state =
1852__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001853
1854static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001855super_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001856{
1857 if (rdev->sb_loaded && rdev->sb_size) {
1858 memcpy(page, page_address(rdev->sb_page), rdev->sb_size);
1859 return rdev->sb_size;
1860 } else
1861 return 0;
1862}
NeilBrown96de1e62005-11-08 21:39:39 -08001863static struct rdev_sysfs_entry rdev_super = __ATTR_RO(super);
1864
NeilBrown4dbcdc72006-01-06 00:20:52 -08001865static ssize_t
1866errors_show(mdk_rdev_t *rdev, char *page)
1867{
1868 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1869}
1870
1871static ssize_t
1872errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1873{
1874 char *e;
1875 unsigned long n = simple_strtoul(buf, &e, 10);
1876 if (*buf && (*e == 0 || *e == '\n')) {
1877 atomic_set(&rdev->corrected_errors, n);
1878 return len;
1879 }
1880 return -EINVAL;
1881}
1882static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001883__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001884
NeilBrown014236d2006-01-06 00:20:55 -08001885static ssize_t
1886slot_show(mdk_rdev_t *rdev, char *page)
1887{
1888 if (rdev->raid_disk < 0)
1889 return sprintf(page, "none\n");
1890 else
1891 return sprintf(page, "%d\n", rdev->raid_disk);
1892}
1893
1894static ssize_t
1895slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1896{
1897 char *e;
1898 int slot = simple_strtoul(buf, &e, 10);
1899 if (strncmp(buf, "none", 4)==0)
1900 slot = -1;
1901 else if (e==buf || (*e && *e!= '\n'))
1902 return -EINVAL;
1903 if (rdev->mddev->pers)
1904 /* Cannot set slot in active array (yet) */
1905 return -EBUSY;
1906 if (slot >= rdev->mddev->raid_disks)
1907 return -ENOSPC;
1908 rdev->raid_disk = slot;
1909 /* assume it is working */
1910 rdev->flags = 0;
1911 set_bit(In_sync, &rdev->flags);
1912 return len;
1913}
1914
1915
1916static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07001917__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08001918
NeilBrown93c8cad2006-01-06 00:20:56 -08001919static ssize_t
1920offset_show(mdk_rdev_t *rdev, char *page)
1921{
Andrew Morton6961ece2006-01-06 00:20:59 -08001922 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08001923}
1924
1925static ssize_t
1926offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1927{
1928 char *e;
1929 unsigned long long offset = simple_strtoull(buf, &e, 10);
1930 if (e==buf || (*e && *e != '\n'))
1931 return -EINVAL;
1932 if (rdev->mddev->pers)
1933 return -EBUSY;
1934 rdev->data_offset = offset;
1935 return len;
1936}
1937
1938static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07001939__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08001940
NeilBrown83303b62006-01-06 00:21:06 -08001941static ssize_t
1942rdev_size_show(mdk_rdev_t *rdev, char *page)
1943{
1944 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
1945}
1946
1947static ssize_t
1948rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1949{
1950 char *e;
1951 unsigned long long size = simple_strtoull(buf, &e, 10);
1952 if (e==buf || (*e && *e != '\n'))
1953 return -EINVAL;
1954 if (rdev->mddev->pers)
1955 return -EBUSY;
1956 rdev->size = size;
1957 if (size < rdev->mddev->size || rdev->mddev->size == 0)
1958 rdev->mddev->size = size;
1959 return len;
1960}
1961
1962static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07001963__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08001964
NeilBrown86e6ffd2005-11-08 21:39:24 -08001965static struct attribute *rdev_default_attrs[] = {
1966 &rdev_state.attr,
1967 &rdev_super.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08001968 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08001969 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08001970 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08001971 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08001972 NULL,
1973};
1974static ssize_t
1975rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1976{
1977 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1978 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1979
1980 if (!entry->show)
1981 return -EIO;
1982 return entry->show(rdev, page);
1983}
1984
1985static ssize_t
1986rdev_attr_store(struct kobject *kobj, struct attribute *attr,
1987 const char *page, size_t length)
1988{
1989 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
1990 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
1991
1992 if (!entry->store)
1993 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07001994 if (!capable(CAP_SYS_ADMIN))
1995 return -EACCES;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001996 return entry->store(rdev, page, length);
1997}
1998
1999static void rdev_free(struct kobject *ko)
2000{
2001 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2002 kfree(rdev);
2003}
2004static struct sysfs_ops rdev_sysfs_ops = {
2005 .show = rdev_attr_show,
2006 .store = rdev_attr_store,
2007};
2008static struct kobj_type rdev_ktype = {
2009 .release = rdev_free,
2010 .sysfs_ops = &rdev_sysfs_ops,
2011 .default_attrs = rdev_default_attrs,
2012};
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014/*
2015 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2016 *
2017 * mark the device faulty if:
2018 *
2019 * - the device is nonexistent (zero size)
2020 * - the device has no valid superblock
2021 *
2022 * a faulty rdev _never_ has rdev->sb set.
2023 */
2024static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2025{
2026 char b[BDEVNAME_SIZE];
2027 int err;
2028 mdk_rdev_t *rdev;
2029 sector_t size;
2030
NeilBrown9ffae0c2006-01-06 00:20:32 -08002031 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (!rdev) {
2033 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2034 return ERR_PTR(-ENOMEM);
2035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 if ((err = alloc_disk_sb(rdev)))
2038 goto abort_free;
2039
2040 err = lock_rdev(rdev, newdev);
2041 if (err)
2042 goto abort_free;
2043
NeilBrown86e6ffd2005-11-08 21:39:24 -08002044 rdev->kobj.parent = NULL;
2045 rdev->kobj.ktype = &rdev_ktype;
2046 kobject_init(&rdev->kobj);
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002049 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002050 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002051 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002053 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002055 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002056 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2059 if (!size) {
2060 printk(KERN_WARNING
2061 "md: %s has zero or unknown size, marking faulty!\n",
2062 bdevname(rdev->bdev,b));
2063 err = -EINVAL;
2064 goto abort_free;
2065 }
2066
2067 if (super_format >= 0) {
2068 err = super_types[super_format].
2069 load_super(rdev, NULL, super_minor);
2070 if (err == -EINVAL) {
2071 printk(KERN_WARNING
2072 "md: %s has invalid sb, not importing!\n",
2073 bdevname(rdev->bdev,b));
2074 goto abort_free;
2075 }
2076 if (err < 0) {
2077 printk(KERN_WARNING
2078 "md: could not read %s's sb, not importing!\n",
2079 bdevname(rdev->bdev,b));
2080 goto abort_free;
2081 }
2082 }
2083 INIT_LIST_HEAD(&rdev->same_set);
2084
2085 return rdev;
2086
2087abort_free:
2088 if (rdev->sb_page) {
2089 if (rdev->bdev)
2090 unlock_rdev(rdev);
2091 free_disk_sb(rdev);
2092 }
2093 kfree(rdev);
2094 return ERR_PTR(err);
2095}
2096
2097/*
2098 * Check a full RAID array for plausibility
2099 */
2100
2101
NeilBrowna757e642005-04-16 15:26:42 -07002102static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 int i;
2105 struct list_head *tmp;
2106 mdk_rdev_t *rdev, *freshest;
2107 char b[BDEVNAME_SIZE];
2108
2109 freshest = NULL;
2110 ITERATE_RDEV(mddev,rdev,tmp)
2111 switch (super_types[mddev->major_version].
2112 load_super(rdev, freshest, mddev->minor_version)) {
2113 case 1:
2114 freshest = rdev;
2115 break;
2116 case 0:
2117 break;
2118 default:
2119 printk( KERN_ERR \
2120 "md: fatal superblock inconsistency in %s"
2121 " -- removing from array\n",
2122 bdevname(rdev->bdev,b));
2123 kick_rdev_from_array(rdev);
2124 }
2125
2126
2127 super_types[mddev->major_version].
2128 validate_super(mddev, freshest);
2129
2130 i = 0;
2131 ITERATE_RDEV(mddev,rdev,tmp) {
2132 if (rdev != freshest)
2133 if (super_types[mddev->major_version].
2134 validate_super(mddev, rdev)) {
2135 printk(KERN_WARNING "md: kicking non-fresh %s"
2136 " from array!\n",
2137 bdevname(rdev->bdev,b));
2138 kick_rdev_from_array(rdev);
2139 continue;
2140 }
2141 if (mddev->level == LEVEL_MULTIPATH) {
2142 rdev->desc_nr = i++;
2143 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002144 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146 }
2147
2148
2149
2150 if (mddev->recovery_cp != MaxSector &&
2151 mddev->level >= 1)
2152 printk(KERN_ERR "md: %s: raid array is not clean"
2153 " -- starting background reconstruction\n",
2154 mdname(mddev));
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
NeilBrowneae17012005-11-08 21:39:23 -08002158static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002159safe_delay_show(mddev_t *mddev, char *page)
2160{
2161 int msec = (mddev->safemode_delay*1000)/HZ;
2162 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2163}
2164static ssize_t
2165safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2166{
2167 int scale=1;
2168 int dot=0;
2169 int i;
2170 unsigned long msec;
2171 char buf[30];
2172 char *e;
2173 /* remove a period, and count digits after it */
2174 if (len >= sizeof(buf))
2175 return -EINVAL;
2176 strlcpy(buf, cbuf, len);
2177 buf[len] = 0;
2178 for (i=0; i<len; i++) {
2179 if (dot) {
2180 if (isdigit(buf[i])) {
2181 buf[i-1] = buf[i];
2182 scale *= 10;
2183 }
2184 buf[i] = 0;
2185 } else if (buf[i] == '.') {
2186 dot=1;
2187 buf[i] = 0;
2188 }
2189 }
2190 msec = simple_strtoul(buf, &e, 10);
2191 if (e == buf || (*e && *e != '\n'))
2192 return -EINVAL;
2193 msec = (msec * 1000) / scale;
2194 if (msec == 0)
2195 mddev->safemode_delay = 0;
2196 else {
2197 mddev->safemode_delay = (msec*HZ)/1000;
2198 if (mddev->safemode_delay == 0)
2199 mddev->safemode_delay = 1;
2200 }
2201 return len;
2202}
2203static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002204__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002205
2206static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002207level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002208{
NeilBrown2604b702006-01-06 00:20:36 -08002209 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002210 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002211 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002212 else if (mddev->clevel[0])
2213 return sprintf(page, "%s\n", mddev->clevel);
2214 else if (mddev->level != LEVEL_NONE)
2215 return sprintf(page, "%d\n", mddev->level);
2216 else
2217 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002218}
2219
NeilBrownd9d166c2006-01-06 00:20:51 -08002220static ssize_t
2221level_store(mddev_t *mddev, const char *buf, size_t len)
2222{
2223 int rv = len;
2224 if (mddev->pers)
2225 return -EBUSY;
2226 if (len == 0)
2227 return 0;
2228 if (len >= sizeof(mddev->clevel))
2229 return -ENOSPC;
2230 strncpy(mddev->clevel, buf, len);
2231 if (mddev->clevel[len-1] == '\n')
2232 len--;
2233 mddev->clevel[len] = 0;
2234 mddev->level = LEVEL_NONE;
2235 return rv;
2236}
2237
2238static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002239__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002240
NeilBrownd4dbd022006-06-26 00:27:59 -07002241
2242static ssize_t
2243layout_show(mddev_t *mddev, char *page)
2244{
2245 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002246 if (mddev->reshape_position != MaxSector &&
2247 mddev->layout != mddev->new_layout)
2248 return sprintf(page, "%d (%d)\n",
2249 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002250 return sprintf(page, "%d\n", mddev->layout);
2251}
2252
2253static ssize_t
2254layout_store(mddev_t *mddev, const char *buf, size_t len)
2255{
2256 char *e;
2257 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002258
2259 if (!*buf || (*e && *e != '\n'))
2260 return -EINVAL;
2261
NeilBrown08a02ec2007-05-09 02:35:38 -07002262 if (mddev->pers)
2263 return -EBUSY;
2264 if (mddev->reshape_position != MaxSector)
2265 mddev->new_layout = n;
2266 else
2267 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002268 return len;
2269}
2270static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002271__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002272
2273
NeilBrowneae17012005-11-08 21:39:23 -08002274static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002275raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002276{
NeilBrownbb636542005-11-08 21:39:45 -08002277 if (mddev->raid_disks == 0)
2278 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002279 if (mddev->reshape_position != MaxSector &&
2280 mddev->delta_disks != 0)
2281 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2282 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002283 return sprintf(page, "%d\n", mddev->raid_disks);
2284}
2285
NeilBrownda943b92006-01-06 00:20:54 -08002286static int update_raid_disks(mddev_t *mddev, int raid_disks);
2287
2288static ssize_t
2289raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2290{
NeilBrownda943b92006-01-06 00:20:54 -08002291 char *e;
2292 int rv = 0;
2293 unsigned long n = simple_strtoul(buf, &e, 10);
2294
2295 if (!*buf || (*e && *e != '\n'))
2296 return -EINVAL;
2297
2298 if (mddev->pers)
2299 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002300 else if (mddev->reshape_position != MaxSector) {
2301 int olddisks = mddev->raid_disks - mddev->delta_disks;
2302 mddev->delta_disks = n - olddisks;
2303 mddev->raid_disks = n;
2304 } else
NeilBrownda943b92006-01-06 00:20:54 -08002305 mddev->raid_disks = n;
2306 return rv ? rv : len;
2307}
2308static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002309__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002310
NeilBrown24dd4692005-11-08 21:39:26 -08002311static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002312chunk_size_show(mddev_t *mddev, char *page)
2313{
NeilBrown08a02ec2007-05-09 02:35:38 -07002314 if (mddev->reshape_position != MaxSector &&
2315 mddev->chunk_size != mddev->new_chunk)
2316 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2317 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002318 return sprintf(page, "%d\n", mddev->chunk_size);
2319}
2320
2321static ssize_t
2322chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2323{
2324 /* can only set chunk_size if array is not yet active */
2325 char *e;
2326 unsigned long n = simple_strtoul(buf, &e, 10);
2327
NeilBrown3b343802006-01-06 00:20:47 -08002328 if (!*buf || (*e && *e != '\n'))
2329 return -EINVAL;
2330
NeilBrown08a02ec2007-05-09 02:35:38 -07002331 if (mddev->pers)
2332 return -EBUSY;
2333 else if (mddev->reshape_position != MaxSector)
2334 mddev->new_chunk = n;
2335 else
2336 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002337 return len;
2338}
2339static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002340__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002341
NeilBrowna94213b2006-06-26 00:28:00 -07002342static ssize_t
2343resync_start_show(mddev_t *mddev, char *page)
2344{
2345 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2346}
2347
2348static ssize_t
2349resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2350{
2351 /* can only set chunk_size if array is not yet active */
2352 char *e;
2353 unsigned long long n = simple_strtoull(buf, &e, 10);
2354
2355 if (mddev->pers)
2356 return -EBUSY;
2357 if (!*buf || (*e && *e != '\n'))
2358 return -EINVAL;
2359
2360 mddev->recovery_cp = n;
2361 return len;
2362}
2363static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002364__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002365
NeilBrown9e653b62006-06-26 00:27:58 -07002366/*
2367 * The array state can be:
2368 *
2369 * clear
2370 * No devices, no size, no level
2371 * Equivalent to STOP_ARRAY ioctl
2372 * inactive
2373 * May have some settings, but array is not active
2374 * all IO results in error
2375 * When written, doesn't tear down array, but just stops it
2376 * suspended (not supported yet)
2377 * All IO requests will block. The array can be reconfigured.
2378 * Writing this, if accepted, will block until array is quiessent
2379 * readonly
2380 * no resync can happen. no superblocks get written.
2381 * write requests fail
2382 * read-auto
2383 * like readonly, but behaves like 'clean' on a write request.
2384 *
2385 * clean - no pending writes, but otherwise active.
2386 * When written to inactive array, starts without resync
2387 * If a write request arrives then
2388 * if metadata is known, mark 'dirty' and switch to 'active'.
2389 * if not known, block and switch to write-pending
2390 * If written to an active array that has pending writes, then fails.
2391 * active
2392 * fully active: IO and resync can be happening.
2393 * When written to inactive array, starts with resync
2394 *
2395 * write-pending
2396 * clean, but writes are blocked waiting for 'active' to be written.
2397 *
2398 * active-idle
2399 * like active, but no writes have been seen for a while (100msec).
2400 *
2401 */
2402enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2403 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002404static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002405 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2406 "write-pending", "active-idle", NULL };
2407
2408static int match_word(const char *word, char **list)
2409{
2410 int n;
2411 for (n=0; list[n]; n++)
2412 if (cmd_match(word, list[n]))
2413 break;
2414 return n;
2415}
2416
2417static ssize_t
2418array_state_show(mddev_t *mddev, char *page)
2419{
2420 enum array_state st = inactive;
2421
2422 if (mddev->pers)
2423 switch(mddev->ro) {
2424 case 1:
2425 st = readonly;
2426 break;
2427 case 2:
2428 st = read_auto;
2429 break;
2430 case 0:
2431 if (mddev->in_sync)
2432 st = clean;
2433 else if (mddev->safemode)
2434 st = active_idle;
2435 else
2436 st = active;
2437 }
2438 else {
2439 if (list_empty(&mddev->disks) &&
2440 mddev->raid_disks == 0 &&
2441 mddev->size == 0)
2442 st = clear;
2443 else
2444 st = inactive;
2445 }
2446 return sprintf(page, "%s\n", array_states[st]);
2447}
2448
2449static int do_md_stop(mddev_t * mddev, int ro);
2450static int do_md_run(mddev_t * mddev);
2451static int restart_array(mddev_t *mddev);
2452
2453static ssize_t
2454array_state_store(mddev_t *mddev, const char *buf, size_t len)
2455{
2456 int err = -EINVAL;
2457 enum array_state st = match_word(buf, array_states);
2458 switch(st) {
2459 case bad_word:
2460 break;
2461 case clear:
2462 /* stopping an active array */
2463 if (mddev->pers) {
2464 if (atomic_read(&mddev->active) > 1)
2465 return -EBUSY;
2466 err = do_md_stop(mddev, 0);
2467 }
2468 break;
2469 case inactive:
2470 /* stopping an active array */
2471 if (mddev->pers) {
2472 if (atomic_read(&mddev->active) > 1)
2473 return -EBUSY;
2474 err = do_md_stop(mddev, 2);
2475 }
2476 break;
2477 case suspended:
2478 break; /* not supported yet */
2479 case readonly:
2480 if (mddev->pers)
2481 err = do_md_stop(mddev, 1);
2482 else {
2483 mddev->ro = 1;
2484 err = do_md_run(mddev);
2485 }
2486 break;
2487 case read_auto:
2488 /* stopping an active array */
2489 if (mddev->pers) {
2490 err = do_md_stop(mddev, 1);
2491 if (err == 0)
2492 mddev->ro = 2; /* FIXME mark devices writable */
2493 } else {
2494 mddev->ro = 2;
2495 err = do_md_run(mddev);
2496 }
2497 break;
2498 case clean:
2499 if (mddev->pers) {
2500 restart_array(mddev);
2501 spin_lock_irq(&mddev->write_lock);
2502 if (atomic_read(&mddev->writes_pending) == 0) {
2503 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07002504 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002505 }
2506 spin_unlock_irq(&mddev->write_lock);
2507 } else {
2508 mddev->ro = 0;
2509 mddev->recovery_cp = MaxSector;
2510 err = do_md_run(mddev);
2511 }
2512 break;
2513 case active:
2514 if (mddev->pers) {
2515 restart_array(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002516 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002517 wake_up(&mddev->sb_wait);
2518 err = 0;
2519 } else {
2520 mddev->ro = 0;
2521 err = do_md_run(mddev);
2522 }
2523 break;
2524 case write_pending:
2525 case active_idle:
2526 /* these cannot be set */
2527 break;
2528 }
2529 if (err)
2530 return err;
2531 else
2532 return len;
2533}
NeilBrown80ca3a42006-07-10 04:44:18 -07002534static struct md_sysfs_entry md_array_state =
2535__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002536
NeilBrown6d7ff732006-01-06 00:21:16 -08002537static ssize_t
2538null_show(mddev_t *mddev, char *page)
2539{
2540 return -EINVAL;
2541}
2542
2543static ssize_t
2544new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2545{
2546 /* buf must be %d:%d\n? giving major and minor numbers */
2547 /* The new device is added to the array.
2548 * If the array has a persistent superblock, we read the
2549 * superblock to initialise info and check validity.
2550 * Otherwise, only checking done is that in bind_rdev_to_array,
2551 * which mainly checks size.
2552 */
2553 char *e;
2554 int major = simple_strtoul(buf, &e, 10);
2555 int minor;
2556 dev_t dev;
2557 mdk_rdev_t *rdev;
2558 int err;
2559
2560 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2561 return -EINVAL;
2562 minor = simple_strtoul(e+1, &e, 10);
2563 if (*e && *e != '\n')
2564 return -EINVAL;
2565 dev = MKDEV(major, minor);
2566 if (major != MAJOR(dev) ||
2567 minor != MINOR(dev))
2568 return -EOVERFLOW;
2569
2570
2571 if (mddev->persistent) {
2572 rdev = md_import_device(dev, mddev->major_version,
2573 mddev->minor_version);
2574 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2575 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2576 mdk_rdev_t, same_set);
2577 err = super_types[mddev->major_version]
2578 .load_super(rdev, rdev0, mddev->minor_version);
2579 if (err < 0)
2580 goto out;
2581 }
2582 } else
2583 rdev = md_import_device(dev, -1, -1);
2584
2585 if (IS_ERR(rdev))
2586 return PTR_ERR(rdev);
2587 err = bind_rdev_to_array(rdev, mddev);
2588 out:
2589 if (err)
2590 export_rdev(rdev);
2591 return err ? err : len;
2592}
2593
2594static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002595__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002596
2597static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002598bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2599{
2600 char *end;
2601 unsigned long chunk, end_chunk;
2602
2603 if (!mddev->bitmap)
2604 goto out;
2605 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2606 while (*buf) {
2607 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2608 if (buf == end) break;
2609 if (*end == '-') { /* range */
2610 buf = end + 1;
2611 end_chunk = simple_strtoul(buf, &end, 0);
2612 if (buf == end) break;
2613 }
2614 if (*end && !isspace(*end)) break;
2615 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2616 buf = end;
2617 while (isspace(*buf)) buf++;
2618 }
2619 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2620out:
2621 return len;
2622}
2623
2624static struct md_sysfs_entry md_bitmap =
2625__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2626
2627static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002628size_show(mddev_t *mddev, char *page)
2629{
2630 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2631}
2632
2633static int update_size(mddev_t *mddev, unsigned long size);
2634
2635static ssize_t
2636size_store(mddev_t *mddev, const char *buf, size_t len)
2637{
2638 /* If array is inactive, we can reduce the component size, but
2639 * not increase it (except from 0).
2640 * If array is active, we can try an on-line resize
2641 */
2642 char *e;
2643 int err = 0;
2644 unsigned long long size = simple_strtoull(buf, &e, 10);
2645 if (!*buf || *buf == '\n' ||
2646 (*e && *e != '\n'))
2647 return -EINVAL;
2648
2649 if (mddev->pers) {
2650 err = update_size(mddev, size);
NeilBrown850b2b42006-10-03 01:15:46 -07002651 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002652 } else {
2653 if (mddev->size == 0 ||
2654 mddev->size > size)
2655 mddev->size = size;
2656 else
2657 err = -ENOSPC;
2658 }
2659 return err ? err : len;
2660}
2661
2662static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002663__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002664
NeilBrown8bb93aa2006-01-06 00:20:50 -08002665
2666/* Metdata version.
2667 * This is either 'none' for arrays with externally managed metadata,
2668 * or N.M for internally known formats
2669 */
2670static ssize_t
2671metadata_show(mddev_t *mddev, char *page)
2672{
2673 if (mddev->persistent)
2674 return sprintf(page, "%d.%d\n",
2675 mddev->major_version, mddev->minor_version);
2676 else
2677 return sprintf(page, "none\n");
2678}
2679
2680static ssize_t
2681metadata_store(mddev_t *mddev, const char *buf, size_t len)
2682{
2683 int major, minor;
2684 char *e;
2685 if (!list_empty(&mddev->disks))
2686 return -EBUSY;
2687
2688 if (cmd_match(buf, "none")) {
2689 mddev->persistent = 0;
2690 mddev->major_version = 0;
2691 mddev->minor_version = 90;
2692 return len;
2693 }
2694 major = simple_strtoul(buf, &e, 10);
2695 if (e==buf || *e != '.')
2696 return -EINVAL;
2697 buf = e+1;
2698 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002699 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002700 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002701 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002702 return -ENOENT;
2703 mddev->major_version = major;
2704 mddev->minor_version = minor;
2705 mddev->persistent = 1;
2706 return len;
2707}
2708
2709static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002710__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002711
NeilBrowna35b0d62006-01-06 00:20:49 -08002712static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002713action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002714{
NeilBrown7eec3142005-11-08 21:39:44 -08002715 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002716 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2717 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery)) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002718 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2719 type = "reshape";
2720 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002721 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2722 type = "resync";
2723 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2724 type = "check";
2725 else
2726 type = "repair";
2727 } else
2728 type = "recover";
2729 }
2730 return sprintf(page, "%s\n", type);
2731}
2732
2733static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002734action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08002735{
NeilBrown7eec3142005-11-08 21:39:44 -08002736 if (!mddev->pers || !mddev->pers->sync_request)
2737 return -EINVAL;
2738
NeilBrownbce74da2006-01-06 00:20:41 -08002739 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08002740 if (mddev->sync_thread) {
2741 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2742 md_unregister_thread(mddev->sync_thread);
2743 mddev->sync_thread = NULL;
2744 mddev->recovery = 0;
2745 }
NeilBrown03c902e2006-01-06 00:20:46 -08002746 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
2747 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002748 return -EBUSY;
NeilBrown03c902e2006-01-06 00:20:46 -08002749 else if (cmd_match(page, "resync") || cmd_match(page, "recover"))
NeilBrown7eec3142005-11-08 21:39:44 -08002750 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown16484bf2006-03-27 01:18:13 -08002751 else if (cmd_match(page, "reshape")) {
2752 int err;
2753 if (mddev->pers->start_reshape == NULL)
2754 return -EINVAL;
2755 err = mddev->pers->start_reshape(mddev);
2756 if (err)
2757 return err;
2758 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08002759 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08002760 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07002761 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08002762 return -EINVAL;
2763 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
2764 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08002765 }
NeilBrown03c902e2006-01-06 00:20:46 -08002766 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08002767 md_wakeup_thread(mddev->thread);
2768 return len;
2769}
2770
NeilBrown9d888832005-11-08 21:39:26 -08002771static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002772mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08002773{
2774 return sprintf(page, "%llu\n",
2775 (unsigned long long) mddev->resync_mismatches);
2776}
2777
NeilBrown80ca3a42006-07-10 04:44:18 -07002778static struct md_sysfs_entry md_scan_mode =
2779__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08002780
NeilBrown96de1e62005-11-08 21:39:39 -08002781
NeilBrown80ca3a42006-07-10 04:44:18 -07002782static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08002783
NeilBrown88202a02006-01-06 00:21:36 -08002784static ssize_t
2785sync_min_show(mddev_t *mddev, char *page)
2786{
2787 return sprintf(page, "%d (%s)\n", speed_min(mddev),
2788 mddev->sync_speed_min ? "local": "system");
2789}
2790
2791static ssize_t
2792sync_min_store(mddev_t *mddev, const char *buf, size_t len)
2793{
2794 int min;
2795 char *e;
2796 if (strncmp(buf, "system", 6)==0) {
2797 mddev->sync_speed_min = 0;
2798 return len;
2799 }
2800 min = simple_strtoul(buf, &e, 10);
2801 if (buf == e || (*e && *e != '\n') || min <= 0)
2802 return -EINVAL;
2803 mddev->sync_speed_min = min;
2804 return len;
2805}
2806
2807static struct md_sysfs_entry md_sync_min =
2808__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
2809
2810static ssize_t
2811sync_max_show(mddev_t *mddev, char *page)
2812{
2813 return sprintf(page, "%d (%s)\n", speed_max(mddev),
2814 mddev->sync_speed_max ? "local": "system");
2815}
2816
2817static ssize_t
2818sync_max_store(mddev_t *mddev, const char *buf, size_t len)
2819{
2820 int max;
2821 char *e;
2822 if (strncmp(buf, "system", 6)==0) {
2823 mddev->sync_speed_max = 0;
2824 return len;
2825 }
2826 max = simple_strtoul(buf, &e, 10);
2827 if (buf == e || (*e && *e != '\n') || max <= 0)
2828 return -EINVAL;
2829 mddev->sync_speed_max = max;
2830 return len;
2831}
2832
2833static struct md_sysfs_entry md_sync_max =
2834__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
2835
2836
2837static ssize_t
2838sync_speed_show(mddev_t *mddev, char *page)
2839{
2840 unsigned long resync, dt, db;
NeilBrownff4e8d92006-07-10 04:44:16 -07002841 resync = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active));
NeilBrown88202a02006-01-06 00:21:36 -08002842 dt = ((jiffies - mddev->resync_mark) / HZ);
2843 if (!dt) dt++;
2844 db = resync - (mddev->resync_mark_cnt);
2845 return sprintf(page, "%ld\n", db/dt/2); /* K/sec */
2846}
2847
NeilBrown80ca3a42006-07-10 04:44:18 -07002848static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08002849
2850static ssize_t
2851sync_completed_show(mddev_t *mddev, char *page)
2852{
2853 unsigned long max_blocks, resync;
2854
2855 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2856 max_blocks = mddev->resync_max_sectors;
2857 else
2858 max_blocks = mddev->size << 1;
2859
2860 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
2861 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
2862}
2863
NeilBrown80ca3a42006-07-10 04:44:18 -07002864static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08002865
NeilBrowne464eaf2006-03-27 01:18:14 -08002866static ssize_t
2867suspend_lo_show(mddev_t *mddev, char *page)
2868{
2869 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
2870}
2871
2872static ssize_t
2873suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
2874{
2875 char *e;
2876 unsigned long long new = simple_strtoull(buf, &e, 10);
2877
2878 if (mddev->pers->quiesce == NULL)
2879 return -EINVAL;
2880 if (buf == e || (*e && *e != '\n'))
2881 return -EINVAL;
2882 if (new >= mddev->suspend_hi ||
2883 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
2884 mddev->suspend_lo = new;
2885 mddev->pers->quiesce(mddev, 2);
2886 return len;
2887 } else
2888 return -EINVAL;
2889}
2890static struct md_sysfs_entry md_suspend_lo =
2891__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
2892
2893
2894static ssize_t
2895suspend_hi_show(mddev_t *mddev, char *page)
2896{
2897 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
2898}
2899
2900static ssize_t
2901suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
2902{
2903 char *e;
2904 unsigned long long new = simple_strtoull(buf, &e, 10);
2905
2906 if (mddev->pers->quiesce == NULL)
2907 return -EINVAL;
2908 if (buf == e || (*e && *e != '\n'))
2909 return -EINVAL;
2910 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
2911 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
2912 mddev->suspend_hi = new;
2913 mddev->pers->quiesce(mddev, 1);
2914 mddev->pers->quiesce(mddev, 0);
2915 return len;
2916 } else
2917 return -EINVAL;
2918}
2919static struct md_sysfs_entry md_suspend_hi =
2920__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
2921
NeilBrown08a02ec2007-05-09 02:35:38 -07002922static ssize_t
2923reshape_position_show(mddev_t *mddev, char *page)
2924{
2925 if (mddev->reshape_position != MaxSector)
2926 return sprintf(page, "%llu\n",
2927 (unsigned long long)mddev->reshape_position);
2928 strcpy(page, "none\n");
2929 return 5;
2930}
2931
2932static ssize_t
2933reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
2934{
2935 char *e;
2936 unsigned long long new = simple_strtoull(buf, &e, 10);
2937 if (mddev->pers)
2938 return -EBUSY;
2939 if (buf == e || (*e && *e != '\n'))
2940 return -EINVAL;
2941 mddev->reshape_position = new;
2942 mddev->delta_disks = 0;
2943 mddev->new_level = mddev->level;
2944 mddev->new_layout = mddev->layout;
2945 mddev->new_chunk = mddev->chunk_size;
2946 return len;
2947}
2948
2949static struct md_sysfs_entry md_reshape_position =
2950__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
2951 reshape_position_store);
2952
NeilBrowne464eaf2006-03-27 01:18:14 -08002953
NeilBrowneae17012005-11-08 21:39:23 -08002954static struct attribute *md_default_attrs[] = {
2955 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07002956 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002957 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08002958 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08002959 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07002960 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002961 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08002962 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07002963 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07002964 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07002965 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08002966 NULL,
2967};
2968
2969static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08002970 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08002971 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08002972 &md_sync_min.attr,
2973 &md_sync_max.attr,
2974 &md_sync_speed.attr,
2975 &md_sync_completed.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08002976 &md_suspend_lo.attr,
2977 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07002978 &md_bitmap.attr,
NeilBrowneae17012005-11-08 21:39:23 -08002979 NULL,
2980};
NeilBrown411036f2005-11-08 21:39:40 -08002981static struct attribute_group md_redundancy_group = {
2982 .name = NULL,
2983 .attrs = md_redundancy_attrs,
2984};
2985
NeilBrowneae17012005-11-08 21:39:23 -08002986
2987static ssize_t
2988md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2989{
2990 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
2991 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08002992 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08002993
2994 if (!entry->show)
2995 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07002996 rv = mddev_lock(mddev);
2997 if (!rv) {
2998 rv = entry->show(mddev, page);
2999 mddev_unlock(mddev);
3000 }
NeilBrown96de1e62005-11-08 21:39:39 -08003001 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003002}
3003
3004static ssize_t
3005md_attr_store(struct kobject *kobj, struct attribute *attr,
3006 const char *page, size_t length)
3007{
3008 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3009 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003010 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003011
3012 if (!entry->store)
3013 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003014 if (!capable(CAP_SYS_ADMIN))
3015 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003016 rv = mddev_lock(mddev);
3017 if (!rv) {
3018 rv = entry->store(mddev, page, length);
3019 mddev_unlock(mddev);
3020 }
NeilBrown96de1e62005-11-08 21:39:39 -08003021 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003022}
3023
3024static void md_free(struct kobject *ko)
3025{
3026 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3027 kfree(mddev);
3028}
3029
3030static struct sysfs_ops md_sysfs_ops = {
3031 .show = md_attr_show,
3032 .store = md_attr_store,
3033};
3034static struct kobj_type md_ktype = {
3035 .release = md_free,
3036 .sysfs_ops = &md_sysfs_ops,
3037 .default_attrs = md_default_attrs,
3038};
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040int mdp_major = 0;
3041
3042static struct kobject *md_probe(dev_t dev, int *part, void *data)
3043{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003044 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 mddev_t *mddev = mddev_find(dev);
3046 struct gendisk *disk;
3047 int partitioned = (MAJOR(dev) != MD_MAJOR);
3048 int shift = partitioned ? MdpMinorShift : 0;
3049 int unit = MINOR(dev) >> shift;
3050
3051 if (!mddev)
3052 return NULL;
3053
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003054 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003056 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 mddev_put(mddev);
3058 return NULL;
3059 }
3060 disk = alloc_disk(1 << shift);
3061 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003062 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 mddev_put(mddev);
3064 return NULL;
3065 }
3066 disk->major = MAJOR(dev);
3067 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003068 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003070 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 disk->fops = &md_fops;
3073 disk->private_data = mddev;
3074 disk->queue = mddev->queue;
3075 add_disk(disk);
3076 mddev->gendisk = disk;
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003077 mutex_unlock(&disks_mutex);
NeilBrown9c791972005-11-08 21:39:28 -08003078 mddev->kobj.parent = &disk->kobj;
NeilBrowneae17012005-11-08 21:39:23 -08003079 mddev->kobj.k_name = NULL;
3080 snprintf(mddev->kobj.name, KOBJ_NAME_LEN, "%s", "md");
3081 mddev->kobj.ktype = &md_ktype;
NeilBrown5e55e2f2007-03-26 21:32:14 -08003082 if (kobject_register(&mddev->kobj))
3083 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3084 disk->disk_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return NULL;
3086}
3087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088static void md_safemode_timeout(unsigned long data)
3089{
3090 mddev_t *mddev = (mddev_t *) data;
3091
3092 mddev->safemode = 1;
3093 md_wakeup_thread(mddev->thread);
3094}
3095
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003096static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098static int do_md_run(mddev_t * mddev)
3099{
NeilBrown2604b702006-01-06 00:20:36 -08003100 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 int chunk_size;
3102 struct list_head *tmp;
3103 mdk_rdev_t *rdev;
3104 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003105 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 char b[BDEVNAME_SIZE];
3107
NeilBrowna757e642005-04-16 15:26:42 -07003108 if (list_empty(&mddev->disks))
3109 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 if (mddev->pers)
3113 return -EBUSY;
3114
3115 /*
3116 * Analyze all RAID superblock(s)
3117 */
NeilBrowna757e642005-04-16 15:26:42 -07003118 if (!mddev->raid_disks)
3119 analyze_sbs(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120
3121 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
NeilBrown2604b702006-01-06 00:20:36 -08003123 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 if (chunk_size > MAX_CHUNK_SIZE) {
3125 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3126 chunk_size, MAX_CHUNK_SIZE);
3127 return -EINVAL;
3128 }
3129 /*
3130 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3131 */
3132 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003133 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 return -EINVAL;
3135 }
3136 if (chunk_size < PAGE_SIZE) {
3137 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3138 chunk_size, PAGE_SIZE);
3139 return -EINVAL;
3140 }
3141
3142 /* devices must have minimum size of one chunk */
3143 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003144 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 continue;
3146 if (rdev->size < chunk_size / 1024) {
3147 printk(KERN_WARNING
3148 "md: Dev %s smaller than chunk_size:"
3149 " %lluk < %dk\n",
3150 bdevname(rdev->bdev,b),
3151 (unsigned long long)rdev->size,
3152 chunk_size / 1024);
3153 return -EINVAL;
3154 }
3155 }
3156 }
3157
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003159 if (mddev->level != LEVEL_NONE)
3160 request_module("md-level-%d", mddev->level);
3161 else if (mddev->clevel[0])
3162 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163#endif
3164
3165 /*
3166 * Drop all container device buffers, from now on
3167 * the only valid external interface is through the md
3168 * device.
3169 * Also find largest hardsector size
3170 */
3171 ITERATE_RDEV(mddev,rdev,tmp) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003172 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 continue;
3174 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003175 invalidate_bdev(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
3177
3178 md_probe(mddev->unit, NULL, NULL);
3179 disk = mddev->gendisk;
3180 if (!disk)
3181 return -ENOMEM;
3182
3183 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003184 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003185 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003187 if (mddev->level != LEVEL_NONE)
3188 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3189 mddev->level);
3190 else
3191 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3192 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 return -EINVAL;
3194 }
NeilBrown2604b702006-01-06 00:20:36 -08003195 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003197 mddev->level = pers->level;
3198 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
NeilBrownf6705572006-03-27 01:18:11 -08003200 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003201 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003202 /* This personality cannot handle reshaping... */
3203 mddev->pers = NULL;
3204 module_put(pers->owner);
3205 return -EINVAL;
3206 }
3207
NeilBrown7dd5e7c2007-02-28 20:11:35 -08003208 if (pers->sync_request) {
3209 /* Warn if this is a potentially silly
3210 * configuration.
3211 */
3212 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3213 mdk_rdev_t *rdev2;
3214 struct list_head *tmp2;
3215 int warned = 0;
3216 ITERATE_RDEV(mddev, rdev, tmp) {
3217 ITERATE_RDEV(mddev, rdev2, tmp2) {
3218 if (rdev < rdev2 &&
3219 rdev->bdev->bd_contains ==
3220 rdev2->bdev->bd_contains) {
3221 printk(KERN_WARNING
3222 "%s: WARNING: %s appears to be"
3223 " on the same physical disk as"
3224 " %s.\n",
3225 mdname(mddev),
3226 bdevname(rdev->bdev,b),
3227 bdevname(rdev2->bdev,b2));
3228 warned = 1;
3229 }
3230 }
3231 }
3232 if (warned)
3233 printk(KERN_WARNING
3234 "True protection against single-disk"
3235 " failure might be compromised.\n");
3236 }
3237
NeilBrown657390d2005-08-26 18:34:16 -07003238 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003240 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08003241 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242
NeilBrownf91de922005-11-08 21:39:36 -08003243 if (start_readonly)
3244 mddev->ro = 2; /* read-only, but switch on first write */
3245
NeilBrownb15c2e52006-01-06 00:20:16 -08003246 err = mddev->pers->run(mddev);
3247 if (!err && mddev->pers->sync_request) {
3248 err = bitmap_create(mddev);
3249 if (err) {
3250 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3251 mdname(mddev), err);
3252 mddev->pers->stop(mddev);
3253 }
3254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 if (err) {
3256 printk(KERN_ERR "md: pers->run() failed ...\n");
3257 module_put(mddev->pers->owner);
3258 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003259 bitmap_destroy(mddev);
3260 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003262 if (mddev->pers->sync_request) {
3263 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3264 printk(KERN_WARNING
3265 "md: cannot register extra attributes for %s\n",
3266 mdname(mddev));
3267 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003268 mddev->ro = 0;
3269
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 atomic_set(&mddev->writes_pending,0);
3271 mddev->safemode = 0;
3272 mddev->safemode_timer.function = md_safemode_timeout;
3273 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003274 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003276
3277 ITERATE_RDEV(mddev,rdev,tmp)
3278 if (rdev->raid_disk >= 0) {
3279 char nm[20];
3280 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003281 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3282 printk("md: cannot register %s for %s\n",
3283 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
3286 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3287
NeilBrown850b2b42006-10-03 01:15:46 -07003288 if (mddev->flags)
3289 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
3291 set_capacity(disk, mddev->array_size<<1);
3292
3293 /* If we call blk_queue_make_request here, it will
3294 * re-initialise max_sectors etc which may have been
3295 * refined inside -> run. So just set the bits we need to set.
3296 * Most initialisation happended when we called
3297 * blk_queue_make_request(..., md_fail_request)
3298 * earlier.
3299 */
3300 mddev->queue->queuedata = mddev;
3301 mddev->queue->make_request_fn = mddev->pers->make_request;
3302
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003303 /* If there is a partially-recovered drive we need to
3304 * start recovery here. If we leave it to md_check_recovery,
3305 * it will remove the drives and not do the right thing
3306 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003307 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003308 struct list_head *rtmp;
3309 int spares = 0;
3310 ITERATE_RDEV(mddev,rdev,rtmp)
3311 if (rdev->raid_disk >= 0 &&
3312 !test_bit(In_sync, &rdev->flags) &&
3313 !test_bit(Faulty, &rdev->flags))
3314 /* complete an interrupted recovery */
3315 spares++;
3316 if (spares && mddev->pers->sync_request) {
3317 mddev->recovery = 0;
3318 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3319 mddev->sync_thread = md_register_thread(md_do_sync,
3320 mddev,
3321 "%s_resync");
3322 if (!mddev->sync_thread) {
3323 printk(KERN_ERR "%s: could not start resync"
3324 " thread...\n",
3325 mdname(mddev));
3326 /* leave the spares where they are, it shouldn't hurt */
3327 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003328 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003329 }
3330 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003331 md_wakeup_thread(mddev->thread);
3332 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003333
Linus Torvalds44ce6292007-05-09 18:51:36 -07003334 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003335 md_new_event(mddev);
NeilBrown2f471302006-11-08 17:44:47 -08003336 kobject_uevent(&mddev->gendisk->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 return 0;
3338}
3339
3340static int restart_array(mddev_t *mddev)
3341{
3342 struct gendisk *disk = mddev->gendisk;
3343 int err;
3344
3345 /*
3346 * Complain if it has no devices
3347 */
3348 err = -ENXIO;
3349 if (list_empty(&mddev->disks))
3350 goto out;
3351
3352 if (mddev->pers) {
3353 err = -EBUSY;
3354 if (!mddev->ro)
3355 goto out;
3356
3357 mddev->safemode = 0;
3358 mddev->ro = 0;
3359 set_disk_ro(disk, 0);
3360
3361 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3362 mdname(mddev));
3363 /*
3364 * Kick recovery or resync if necessary
3365 */
3366 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3367 md_wakeup_thread(mddev->thread);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003368 md_wakeup_thread(mddev->sync_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 err = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003370 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373out:
3374 return err;
3375}
3376
NeilBrownacc55e22006-06-26 00:27:47 -07003377/* similar to deny_write_access, but accounts for our holding a reference
3378 * to the file ourselves */
3379static int deny_bitmap_write_access(struct file * file)
3380{
3381 struct inode *inode = file->f_mapping->host;
3382
3383 spin_lock(&inode->i_lock);
3384 if (atomic_read(&inode->i_writecount) > 1) {
3385 spin_unlock(&inode->i_lock);
3386 return -ETXTBSY;
3387 }
3388 atomic_set(&inode->i_writecount, -1);
3389 spin_unlock(&inode->i_lock);
3390
3391 return 0;
3392}
3393
3394static void restore_bitmap_write_access(struct file *file)
3395{
3396 struct inode *inode = file->f_mapping->host;
3397
3398 spin_lock(&inode->i_lock);
3399 atomic_set(&inode->i_writecount, 1);
3400 spin_unlock(&inode->i_lock);
3401}
3402
NeilBrown9e653b62006-06-26 00:27:58 -07003403/* mode:
3404 * 0 - completely stop and dis-assemble array
3405 * 1 - switch to readonly
3406 * 2 - stop but do not disassemble array
3407 */
3408static int do_md_stop(mddev_t * mddev, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
3410 int err = 0;
3411 struct gendisk *disk = mddev->gendisk;
3412
3413 if (mddev->pers) {
3414 if (atomic_read(&mddev->active)>2) {
3415 printk("md: %s still in use.\n",mdname(mddev));
3416 return -EBUSY;
3417 }
3418
3419 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003420 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3422 md_unregister_thread(mddev->sync_thread);
3423 mddev->sync_thread = NULL;
3424 }
3425
3426 del_timer_sync(&mddev->safemode_timer);
3427
3428 invalidate_partition(disk, 0);
3429
NeilBrown9e653b62006-06-26 00:27:58 -07003430 switch(mode) {
3431 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003433 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 goto out;
3435 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003436 break;
3437 case 0: /* disassemble */
3438 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003439 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003440 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 if (mddev->ro)
3442 set_disk_ro(disk, 0);
3443 blk_queue_make_request(mddev->queue, md_fail_request);
3444 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003445 mddev->queue->merge_bvec_fn = NULL;
3446 mddev->queue->unplug_fn = NULL;
3447 mddev->queue->issue_flush_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003448 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003449 if (mddev->pers->sync_request)
3450 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3451
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 module_put(mddev->pers->owner);
3453 mddev->pers = NULL;
NeilBrown0d4ca602006-12-10 02:20:44 -08003454
3455 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07003456 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 if (mddev->ro)
3459 mddev->ro = 0;
3460 }
NeilBrown850b2b42006-10-03 01:15:46 -07003461 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 /* mark array as shutdown cleanly */
3463 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003464 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 }
NeilBrown9e653b62006-06-26 00:27:58 -07003466 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003468 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 }
NeilBrown32a76272005-06-21 17:17:14 -07003470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 /*
3472 * Free resources if final stop
3473 */
NeilBrown9e653b62006-06-26 00:27:58 -07003474 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003475 mdk_rdev_t *rdev;
3476 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3479
NeilBrown978f9462006-02-02 14:28:05 -08003480 bitmap_destroy(mddev);
3481 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003482 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003483 fput(mddev->bitmap_file);
3484 mddev->bitmap_file = NULL;
3485 }
3486 mddev->bitmap_offset = 0;
3487
NeilBrown86e6ffd2005-11-08 21:39:24 -08003488 ITERATE_RDEV(mddev,rdev,tmp)
3489 if (rdev->raid_disk >= 0) {
3490 char nm[20];
3491 sprintf(nm, "rd%d", rdev->raid_disk);
3492 sysfs_remove_link(&mddev->kobj, nm);
3493 }
3494
NeilBrown5792a282007-04-04 19:08:18 -07003495 /* make sure all delayed_delete calls have finished */
3496 flush_scheduled_work();
3497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 export_array(mddev);
3499
3500 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003501 mddev->size = 0;
3502 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003503 mddev->recovery_cp = 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003504 mddev->reshape_position = MaxSector;
NeilBrown9e653b62006-06-26 00:27:58 -07003505
NeilBrowna8a55c32006-06-26 00:27:39 -07003506 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3508 mdname(mddev));
3509 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003510 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511out:
3512 return err;
3513}
3514
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003515#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516static void autorun_array(mddev_t *mddev)
3517{
3518 mdk_rdev_t *rdev;
3519 struct list_head *tmp;
3520 int err;
3521
NeilBrowna757e642005-04-16 15:26:42 -07003522 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 printk(KERN_INFO "md: running: ");
3526
3527 ITERATE_RDEV(mddev,rdev,tmp) {
3528 char b[BDEVNAME_SIZE];
3529 printk("<%s>", bdevname(rdev->bdev,b));
3530 }
3531 printk("\n");
3532
3533 err = do_md_run (mddev);
3534 if (err) {
3535 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
3536 do_md_stop (mddev, 0);
3537 }
3538}
3539
3540/*
3541 * lets try to run arrays based on all disks that have arrived
3542 * until now. (those are in pending_raid_disks)
3543 *
3544 * the method: pick the first pending disk, collect all disks with
3545 * the same UUID, remove all from the pending list and put them into
3546 * the 'same_array' list. Then order this list based on superblock
3547 * update time (freshest comes first), kick out 'old' disks and
3548 * compare superblocks. If everything's fine then run it.
3549 *
3550 * If "unit" is allocated, then bump its reference count
3551 */
3552static void autorun_devices(int part)
3553{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 struct list_head *tmp;
3555 mdk_rdev_t *rdev0, *rdev;
3556 mddev_t *mddev;
3557 char b[BDEVNAME_SIZE];
3558
3559 printk(KERN_INFO "md: autorun ...\n");
3560 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003561 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003563 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 rdev0 = list_entry(pending_raid_disks.next,
3565 mdk_rdev_t, same_set);
3566
3567 printk(KERN_INFO "md: considering %s ...\n",
3568 bdevname(rdev0->bdev,b));
3569 INIT_LIST_HEAD(&candidates);
3570 ITERATE_RDEV_PENDING(rdev,tmp)
3571 if (super_90_load(rdev, rdev0, 0) >= 0) {
3572 printk(KERN_INFO "md: adding %s ...\n",
3573 bdevname(rdev->bdev,b));
3574 list_move(&rdev->same_set, &candidates);
3575 }
3576 /*
3577 * now we have a set of devices, with all of them having
3578 * mostly sane superblocks. It's time to allocate the
3579 * mddev.
3580 */
NeilBrowne8703fe2006-10-03 01:15:59 -07003581 if (part) {
3582 dev = MKDEV(mdp_major,
3583 rdev0->preferred_minor << MdpMinorShift);
3584 unit = MINOR(dev) >> MdpMinorShift;
3585 } else {
3586 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
3587 unit = MINOR(dev);
3588 }
3589 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
3591 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
3592 break;
3593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594
3595 md_probe(dev, NULL, NULL);
3596 mddev = mddev_find(dev);
3597 if (!mddev) {
3598 printk(KERN_ERR
3599 "md: cannot allocate memory for md drive.\n");
3600 break;
3601 }
3602 if (mddev_lock(mddev))
3603 printk(KERN_WARNING "md: %s locked, cannot run\n",
3604 mdname(mddev));
3605 else if (mddev->raid_disks || mddev->major_version
3606 || !list_empty(&mddev->disks)) {
3607 printk(KERN_WARNING
3608 "md: %s already running, cannot run %s\n",
3609 mdname(mddev), bdevname(rdev0->bdev,b));
3610 mddev_unlock(mddev);
3611 } else {
3612 printk(KERN_INFO "md: created %s\n", mdname(mddev));
3613 ITERATE_RDEV_GENERIC(candidates,rdev,tmp) {
3614 list_del_init(&rdev->same_set);
3615 if (bind_rdev_to_array(rdev, mddev))
3616 export_rdev(rdev);
3617 }
3618 autorun_array(mddev);
3619 mddev_unlock(mddev);
3620 }
3621 /* on success, candidates will be empty, on error
3622 * it won't...
3623 */
3624 ITERATE_RDEV_GENERIC(candidates,rdev,tmp)
3625 export_rdev(rdev);
3626 mddev_put(mddev);
3627 }
3628 printk(KERN_INFO "md: ... autorun DONE.\n");
3629}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003630#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632static int get_version(void __user * arg)
3633{
3634 mdu_version_t ver;
3635
3636 ver.major = MD_MAJOR_VERSION;
3637 ver.minor = MD_MINOR_VERSION;
3638 ver.patchlevel = MD_PATCHLEVEL_VERSION;
3639
3640 if (copy_to_user(arg, &ver, sizeof(ver)))
3641 return -EFAULT;
3642
3643 return 0;
3644}
3645
3646static int get_array_info(mddev_t * mddev, void __user * arg)
3647{
3648 mdu_array_info_t info;
3649 int nr,working,active,failed,spare;
3650 mdk_rdev_t *rdev;
3651 struct list_head *tmp;
3652
3653 nr=working=active=failed=spare=0;
3654 ITERATE_RDEV(mddev,rdev,tmp) {
3655 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003656 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 failed++;
3658 else {
3659 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08003660 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 active++;
3662 else
3663 spare++;
3664 }
3665 }
3666
3667 info.major_version = mddev->major_version;
3668 info.minor_version = mddev->minor_version;
3669 info.patch_version = MD_PATCHLEVEL_VERSION;
3670 info.ctime = mddev->ctime;
3671 info.level = mddev->level;
3672 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08003673 if (info.size != mddev->size) /* overflow */
3674 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 info.nr_disks = nr;
3676 info.raid_disks = mddev->raid_disks;
3677 info.md_minor = mddev->md_minor;
3678 info.not_persistent= !mddev->persistent;
3679
3680 info.utime = mddev->utime;
3681 info.state = 0;
3682 if (mddev->in_sync)
3683 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07003684 if (mddev->bitmap && mddev->bitmap_offset)
3685 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 info.active_disks = active;
3687 info.working_disks = working;
3688 info.failed_disks = failed;
3689 info.spare_disks = spare;
3690
3691 info.layout = mddev->layout;
3692 info.chunk_size = mddev->chunk_size;
3693
3694 if (copy_to_user(arg, &info, sizeof(info)))
3695 return -EFAULT;
3696
3697 return 0;
3698}
3699
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01003700static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07003701{
3702 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
3703 char *ptr, *buf = NULL;
3704 int err = -ENOMEM;
3705
NeilBrown2a2275d2007-01-26 00:57:11 -08003706 md_allow_write(mddev);
3707
NeilBrown32a76272005-06-21 17:17:14 -07003708 file = kmalloc(sizeof(*file), GFP_KERNEL);
3709 if (!file)
3710 goto out;
3711
3712 /* bitmap disabled, zero the first byte and copy out */
3713 if (!mddev->bitmap || !mddev->bitmap->file) {
3714 file->pathname[0] = '\0';
3715 goto copy_out;
3716 }
3717
3718 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
3719 if (!buf)
3720 goto out;
3721
3722 ptr = file_path(mddev->bitmap->file, buf, sizeof(file->pathname));
3723 if (!ptr)
3724 goto out;
3725
3726 strcpy(file->pathname, ptr);
3727
3728copy_out:
3729 err = 0;
3730 if (copy_to_user(arg, file, sizeof(*file)))
3731 err = -EFAULT;
3732out:
3733 kfree(buf);
3734 kfree(file);
3735 return err;
3736}
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738static int get_disk_info(mddev_t * mddev, void __user * arg)
3739{
3740 mdu_disk_info_t info;
3741 unsigned int nr;
3742 mdk_rdev_t *rdev;
3743
3744 if (copy_from_user(&info, arg, sizeof(info)))
3745 return -EFAULT;
3746
3747 nr = info.number;
3748
3749 rdev = find_rdev_nr(mddev, nr);
3750 if (rdev) {
3751 info.major = MAJOR(rdev->bdev->bd_dev);
3752 info.minor = MINOR(rdev->bdev->bd_dev);
3753 info.raid_disk = rdev->raid_disk;
3754 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08003755 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08003757 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 info.state |= (1<<MD_DISK_ACTIVE);
3759 info.state |= (1<<MD_DISK_SYNC);
3760 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003761 if (test_bit(WriteMostly, &rdev->flags))
3762 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 } else {
3764 info.major = info.minor = 0;
3765 info.raid_disk = -1;
3766 info.state = (1<<MD_DISK_REMOVED);
3767 }
3768
3769 if (copy_to_user(arg, &info, sizeof(info)))
3770 return -EFAULT;
3771
3772 return 0;
3773}
3774
3775static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
3776{
3777 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3778 mdk_rdev_t *rdev;
3779 dev_t dev = MKDEV(info->major,info->minor);
3780
3781 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
3782 return -EOVERFLOW;
3783
3784 if (!mddev->raid_disks) {
3785 int err;
3786 /* expecting a device which has a superblock */
3787 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
3788 if (IS_ERR(rdev)) {
3789 printk(KERN_WARNING
3790 "md: md_import_device returned %ld\n",
3791 PTR_ERR(rdev));
3792 return PTR_ERR(rdev);
3793 }
3794 if (!list_empty(&mddev->disks)) {
3795 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3796 mdk_rdev_t, same_set);
3797 int err = super_types[mddev->major_version]
3798 .load_super(rdev, rdev0, mddev->minor_version);
3799 if (err < 0) {
3800 printk(KERN_WARNING
3801 "md: %s has different UUID to %s\n",
3802 bdevname(rdev->bdev,b),
3803 bdevname(rdev0->bdev,b2));
3804 export_rdev(rdev);
3805 return -EINVAL;
3806 }
3807 }
3808 err = bind_rdev_to_array(rdev, mddev);
3809 if (err)
3810 export_rdev(rdev);
3811 return err;
3812 }
3813
3814 /*
3815 * add_new_disk can be used once the array is assembled
3816 * to add "hot spares". They must already have a superblock
3817 * written
3818 */
3819 if (mddev->pers) {
3820 int err;
3821 if (!mddev->pers->hot_add_disk) {
3822 printk(KERN_WARNING
3823 "%s: personality does not support diskops!\n",
3824 mdname(mddev));
3825 return -EINVAL;
3826 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07003827 if (mddev->persistent)
3828 rdev = md_import_device(dev, mddev->major_version,
3829 mddev->minor_version);
3830 else
3831 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 if (IS_ERR(rdev)) {
3833 printk(KERN_WARNING
3834 "md: md_import_device returned %ld\n",
3835 PTR_ERR(rdev));
3836 return PTR_ERR(rdev);
3837 }
NeilBrown41158c72005-06-21 17:17:25 -07003838 /* set save_raid_disk if appropriate */
3839 if (!mddev->persistent) {
3840 if (info->state & (1<<MD_DISK_SYNC) &&
3841 info->raid_disk < mddev->raid_disks)
3842 rdev->raid_disk = info->raid_disk;
3843 else
3844 rdev->raid_disk = -1;
3845 } else
3846 super_types[mddev->major_version].
3847 validate_super(mddev, rdev);
3848 rdev->saved_raid_disk = rdev->raid_disk;
3849
NeilBrownb2d444d2005-11-08 21:39:31 -08003850 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003851 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3852 set_bit(WriteMostly, &rdev->flags);
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 rdev->raid_disk = -1;
3855 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07003856 if (!err && !mddev->pers->hot_remove_disk) {
3857 /* If there is hot_add_disk but no hot_remove_disk
3858 * then added disks for geometry changes,
3859 * and should be added immediately.
3860 */
3861 super_types[mddev->major_version].
3862 validate_super(mddev, rdev);
3863 err = mddev->pers->hot_add_disk(mddev, rdev);
3864 if (err)
3865 unbind_rdev_from_array(rdev);
3866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 if (err)
3868 export_rdev(rdev);
NeilBrownc3617772005-06-21 17:17:10 -07003869
NeilBrown17571282006-12-10 02:20:52 -08003870 md_update_sb(mddev, 1);
NeilBrownc3617772005-06-21 17:17:10 -07003871 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07003872 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return err;
3874 }
3875
3876 /* otherwise, add_new_disk is only allowed
3877 * for major_version==0 superblocks
3878 */
3879 if (mddev->major_version != 0) {
3880 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
3881 mdname(mddev));
3882 return -EINVAL;
3883 }
3884
3885 if (!(info->state & (1<<MD_DISK_FAULTY))) {
3886 int err;
3887 rdev = md_import_device (dev, -1, 0);
3888 if (IS_ERR(rdev)) {
3889 printk(KERN_WARNING
3890 "md: error, md_import_device() returned %ld\n",
3891 PTR_ERR(rdev));
3892 return PTR_ERR(rdev);
3893 }
3894 rdev->desc_nr = info->number;
3895 if (info->raid_disk < mddev->raid_disks)
3896 rdev->raid_disk = info->raid_disk;
3897 else
3898 rdev->raid_disk = -1;
3899
NeilBrownb2d444d2005-11-08 21:39:31 -08003900 rdev->flags = 0;
3901
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08003903 if (info->state & (1<<MD_DISK_SYNC))
3904 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
NeilBrown8ddf9ef2005-09-09 16:23:45 -07003906 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
3907 set_bit(WriteMostly, &rdev->flags);
3908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 if (!mddev->persistent) {
3910 printk(KERN_INFO "md: nonpersistent superblock ...\n");
3911 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3912 } else
3913 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3914 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
3915
NeilBrown2bf071b2006-01-06 00:20:55 -08003916 err = bind_rdev_to_array(rdev, mddev);
3917 if (err) {
3918 export_rdev(rdev);
3919 return err;
3920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 }
3922
3923 return 0;
3924}
3925
3926static int hot_remove_disk(mddev_t * mddev, dev_t dev)
3927{
3928 char b[BDEVNAME_SIZE];
3929 mdk_rdev_t *rdev;
3930
3931 if (!mddev->pers)
3932 return -ENODEV;
3933
3934 rdev = find_rdev(mddev, dev);
3935 if (!rdev)
3936 return -ENXIO;
3937
3938 if (rdev->raid_disk >= 0)
3939 goto busy;
3940
3941 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07003942 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08003943 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 return 0;
3946busy:
3947 printk(KERN_WARNING "md: cannot remove active disk %s from %s ... \n",
3948 bdevname(rdev->bdev,b), mdname(mddev));
3949 return -EBUSY;
3950}
3951
3952static int hot_add_disk(mddev_t * mddev, dev_t dev)
3953{
3954 char b[BDEVNAME_SIZE];
3955 int err;
3956 unsigned int size;
3957 mdk_rdev_t *rdev;
3958
3959 if (!mddev->pers)
3960 return -ENODEV;
3961
3962 if (mddev->major_version != 0) {
3963 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
3964 " version-0 superblocks.\n",
3965 mdname(mddev));
3966 return -EINVAL;
3967 }
3968 if (!mddev->pers->hot_add_disk) {
3969 printk(KERN_WARNING
3970 "%s: personality does not support diskops!\n",
3971 mdname(mddev));
3972 return -EINVAL;
3973 }
3974
3975 rdev = md_import_device (dev, -1, 0);
3976 if (IS_ERR(rdev)) {
3977 printk(KERN_WARNING
3978 "md: error, md_import_device() returned %ld\n",
3979 PTR_ERR(rdev));
3980 return -EINVAL;
3981 }
3982
3983 if (mddev->persistent)
3984 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
3985 else
3986 rdev->sb_offset =
3987 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
3988
3989 size = calc_dev_size(rdev, mddev->chunk_size);
3990 rdev->size = size;
3991
NeilBrownb2d444d2005-11-08 21:39:31 -08003992 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 printk(KERN_WARNING
3994 "md: can not hot-add faulty %s disk to %s!\n",
3995 bdevname(rdev->bdev,b), mdname(mddev));
3996 err = -EINVAL;
3997 goto abort_export;
3998 }
NeilBrownb2d444d2005-11-08 21:39:31 -08003999 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004001 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004002 err = bind_rdev_to_array(rdev, mddev);
4003 if (err)
4004 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 /*
4007 * The rest should better be atomic, we can have disk failures
4008 * noticed in interrupt contexts ...
4009 */
4010
4011 if (rdev->desc_nr == mddev->max_disks) {
4012 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4013 mdname(mddev));
4014 err = -EBUSY;
4015 goto abort_unbind_export;
4016 }
4017
4018 rdev->raid_disk = -1;
4019
NeilBrown850b2b42006-10-03 01:15:46 -07004020 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022 /*
4023 * Kick recovery, maybe this spare has to be added to the
4024 * array immediately.
4025 */
4026 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4027 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004028 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 return 0;
4030
4031abort_unbind_export:
4032 unbind_rdev_from_array(rdev);
4033
4034abort_export:
4035 export_rdev(rdev);
4036 return err;
4037}
4038
NeilBrown32a76272005-06-21 17:17:14 -07004039static int set_bitmap_file(mddev_t *mddev, int fd)
4040{
4041 int err;
4042
NeilBrown36fa3062005-09-09 16:23:45 -07004043 if (mddev->pers) {
4044 if (!mddev->pers->quiesce)
4045 return -EBUSY;
4046 if (mddev->recovery || mddev->sync_thread)
4047 return -EBUSY;
4048 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004049 }
4050
NeilBrown36fa3062005-09-09 16:23:45 -07004051
4052 if (fd >= 0) {
4053 if (mddev->bitmap)
4054 return -EEXIST; /* cannot add when bitmap is present */
4055 mddev->bitmap_file = fget(fd);
4056
4057 if (mddev->bitmap_file == NULL) {
4058 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4059 mdname(mddev));
4060 return -EBADF;
4061 }
4062
4063 err = deny_bitmap_write_access(mddev->bitmap_file);
4064 if (err) {
4065 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4066 mdname(mddev));
4067 fput(mddev->bitmap_file);
4068 mddev->bitmap_file = NULL;
4069 return err;
4070 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004071 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004072 } else if (mddev->bitmap == NULL)
4073 return -ENOENT; /* cannot remove what isn't there */
4074 err = 0;
4075 if (mddev->pers) {
4076 mddev->pers->quiesce(mddev, 1);
4077 if (fd >= 0)
4078 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004079 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004080 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004081 fd = -1; /* make sure to put the file */
4082 }
NeilBrown36fa3062005-09-09 16:23:45 -07004083 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004084 }
4085 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004086 if (mddev->bitmap_file) {
4087 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004088 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004089 }
NeilBrown36fa3062005-09-09 16:23:45 -07004090 mddev->bitmap_file = NULL;
4091 }
4092
NeilBrown32a76272005-06-21 17:17:14 -07004093 return err;
4094}
4095
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096/*
4097 * set_array_info is used two different ways
4098 * The original usage is when creating a new array.
4099 * In this usage, raid_disks is > 0 and it together with
4100 * level, size, not_persistent,layout,chunksize determine the
4101 * shape of the array.
4102 * This will always create an array with a type-0.90.0 superblock.
4103 * The newer usage is when assembling an array.
4104 * In this case raid_disks will be 0, and the major_version field is
4105 * use to determine which style super-blocks are to be found on the devices.
4106 * The minor and patch _version numbers are also kept incase the
4107 * super_block handler wishes to interpret them.
4108 */
4109static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4110{
4111
4112 if (info->raid_disks == 0) {
4113 /* just setting version number for superblock loading */
4114 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004115 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 super_types[info->major_version].name == NULL) {
4117 /* maybe try to auto-load a module? */
4118 printk(KERN_INFO
4119 "md: superblock version %d not known\n",
4120 info->major_version);
4121 return -EINVAL;
4122 }
4123 mddev->major_version = info->major_version;
4124 mddev->minor_version = info->minor_version;
4125 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004126 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return 0;
4128 }
4129 mddev->major_version = MD_MAJOR_VERSION;
4130 mddev->minor_version = MD_MINOR_VERSION;
4131 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4132 mddev->ctime = get_seconds();
4133
4134 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004135 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 mddev->size = info->size;
4137 mddev->raid_disks = info->raid_disks;
4138 /* don't set md_minor, it is determined by which /dev/md* was
4139 * openned
4140 */
4141 if (info->state & (1<<MD_SB_CLEAN))
4142 mddev->recovery_cp = MaxSector;
4143 else
4144 mddev->recovery_cp = 0;
4145 mddev->persistent = ! info->not_persistent;
4146
4147 mddev->layout = info->layout;
4148 mddev->chunk_size = info->chunk_size;
4149
4150 mddev->max_disks = MD_SB_DISKS;
4151
NeilBrown850b2b42006-10-03 01:15:46 -07004152 mddev->flags = 0;
4153 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
NeilBrownb2a27032005-11-28 13:44:12 -08004155 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4156 mddev->bitmap_offset = 0;
4157
NeilBrownf6705572006-03-27 01:18:11 -08004158 mddev->reshape_position = MaxSector;
4159
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 /*
4161 * Generate a 128 bit UUID
4162 */
4163 get_random_bytes(mddev->uuid, 16);
4164
NeilBrownf6705572006-03-27 01:18:11 -08004165 mddev->new_level = mddev->level;
4166 mddev->new_chunk = mddev->chunk_size;
4167 mddev->new_layout = mddev->layout;
4168 mddev->delta_disks = 0;
4169
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return 0;
4171}
4172
NeilBrowna35b0d62006-01-06 00:20:49 -08004173static int update_size(mddev_t *mddev, unsigned long size)
4174{
4175 mdk_rdev_t * rdev;
4176 int rv;
4177 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004178 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004179
4180 if (mddev->pers->resize == NULL)
4181 return -EINVAL;
4182 /* The "size" is the amount of each device that is used.
4183 * This can only make sense for arrays with redundancy.
4184 * linear and raid0 always use whatever space is available
4185 * We can only consider changing the size if no resync
4186 * or reconstruction is happening, and if the new size
4187 * is acceptable. It must fit before the sb_offset or,
4188 * if that is <data_offset, it must fit before the
4189 * size of each device.
4190 * If size is zero, we find the largest size that fits.
4191 */
4192 if (mddev->sync_thread)
4193 return -EBUSY;
4194 ITERATE_RDEV(mddev,rdev,tmp) {
4195 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004196 avail = rdev->size * 2;
4197
NeilBrowna35b0d62006-01-06 00:20:49 -08004198 if (fit && (size == 0 || size > avail/2))
4199 size = avail/2;
4200 if (avail < ((sector_t)size << 1))
4201 return -ENOSPC;
4202 }
4203 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4204 if (!rv) {
4205 struct block_device *bdev;
4206
4207 bdev = bdget_disk(mddev->gendisk, 0);
4208 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004209 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004210 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004211 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004212 bdput(bdev);
4213 }
4214 }
4215 return rv;
4216}
4217
NeilBrownda943b92006-01-06 00:20:54 -08004218static int update_raid_disks(mddev_t *mddev, int raid_disks)
4219{
4220 int rv;
4221 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004222 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08004223 return -EINVAL;
4224 if (raid_disks <= 0 ||
4225 raid_disks >= mddev->max_disks)
4226 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004227 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08004228 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004229 mddev->delta_disks = raid_disks - mddev->raid_disks;
4230
4231 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08004232 return rv;
4233}
4234
4235
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236/*
4237 * update_array_info is used to change the configuration of an
4238 * on-line array.
4239 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4240 * fields in the info are checked against the array.
4241 * Any differences that cannot be handled will cause an error.
4242 * Normally, only one change can be managed at a time.
4243 */
4244static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4245{
4246 int rv = 0;
4247 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004248 int state = 0;
4249
4250 /* calculate expected state,ignoring low bits */
4251 if (mddev->bitmap && mddev->bitmap_offset)
4252 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253
4254 if (mddev->major_version != info->major_version ||
4255 mddev->minor_version != info->minor_version ||
4256/* mddev->patch_version != info->patch_version || */
4257 mddev->ctime != info->ctime ||
4258 mddev->level != info->level ||
4259/* mddev->layout != info->layout || */
4260 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004261 mddev->chunk_size != info->chunk_size ||
4262 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4263 ((state^info->state) & 0xfffffe00)
4264 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return -EINVAL;
4266 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004267 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (mddev->raid_disks != info->raid_disks) cnt++;
4269 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004270 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 if (cnt == 0) return 0;
4272 if (cnt > 1) return -EINVAL;
4273
4274 if (mddev->layout != info->layout) {
4275 /* Change layout
4276 * we don't need to do anything at the md level, the
4277 * personality will take care of it all.
4278 */
4279 if (mddev->pers->reconfig == NULL)
4280 return -EINVAL;
4281 else
4282 return mddev->pers->reconfig(mddev, info->layout, -1);
4283 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004284 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004285 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
NeilBrownda943b92006-01-06 00:20:54 -08004287 if (mddev->raid_disks != info->raid_disks)
4288 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
NeilBrown36fa3062005-09-09 16:23:45 -07004290 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4291 if (mddev->pers->quiesce == NULL)
4292 return -EINVAL;
4293 if (mddev->recovery || mddev->sync_thread)
4294 return -EBUSY;
4295 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4296 /* add the bitmap */
4297 if (mddev->bitmap)
4298 return -EEXIST;
4299 if (mddev->default_bitmap_offset == 0)
4300 return -EINVAL;
4301 mddev->bitmap_offset = mddev->default_bitmap_offset;
4302 mddev->pers->quiesce(mddev, 1);
4303 rv = bitmap_create(mddev);
4304 if (rv)
4305 bitmap_destroy(mddev);
4306 mddev->pers->quiesce(mddev, 0);
4307 } else {
4308 /* remove the bitmap */
4309 if (!mddev->bitmap)
4310 return -ENOENT;
4311 if (mddev->bitmap->file)
4312 return -EINVAL;
4313 mddev->pers->quiesce(mddev, 1);
4314 bitmap_destroy(mddev);
4315 mddev->pers->quiesce(mddev, 0);
4316 mddev->bitmap_offset = 0;
4317 }
4318 }
NeilBrown850b2b42006-10-03 01:15:46 -07004319 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 return rv;
4321}
4322
4323static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4324{
4325 mdk_rdev_t *rdev;
4326
4327 if (mddev->pers == NULL)
4328 return -ENODEV;
4329
4330 rdev = find_rdev(mddev, dev);
4331 if (!rdev)
4332 return -ENODEV;
4333
4334 md_error(mddev, rdev);
4335 return 0;
4336}
4337
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004338static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4339{
4340 mddev_t *mddev = bdev->bd_disk->private_data;
4341
4342 geo->heads = 2;
4343 geo->sectors = 4;
4344 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4345 return 0;
4346}
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348static int md_ioctl(struct inode *inode, struct file *file,
4349 unsigned int cmd, unsigned long arg)
4350{
4351 int err = 0;
4352 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 mddev_t *mddev = NULL;
4354
4355 if (!capable(CAP_SYS_ADMIN))
4356 return -EACCES;
4357
4358 /*
4359 * Commands dealing with the RAID driver but not any
4360 * particular array:
4361 */
4362 switch (cmd)
4363 {
4364 case RAID_VERSION:
4365 err = get_version(argp);
4366 goto done;
4367
4368 case PRINT_RAID_DEBUG:
4369 err = 0;
4370 md_print_devices();
4371 goto done;
4372
4373#ifndef MODULE
4374 case RAID_AUTORUN:
4375 err = 0;
4376 autostart_arrays(arg);
4377 goto done;
4378#endif
4379 default:;
4380 }
4381
4382 /*
4383 * Commands creating/starting a new array:
4384 */
4385
4386 mddev = inode->i_bdev->bd_disk->private_data;
4387
4388 if (!mddev) {
4389 BUG();
4390 goto abort;
4391 }
4392
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 err = mddev_lock(mddev);
4394 if (err) {
4395 printk(KERN_INFO
4396 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4397 err, cmd);
4398 goto abort;
4399 }
4400
4401 switch (cmd)
4402 {
4403 case SET_ARRAY_INFO:
4404 {
4405 mdu_array_info_t info;
4406 if (!arg)
4407 memset(&info, 0, sizeof(info));
4408 else if (copy_from_user(&info, argp, sizeof(info))) {
4409 err = -EFAULT;
4410 goto abort_unlock;
4411 }
4412 if (mddev->pers) {
4413 err = update_array_info(mddev, &info);
4414 if (err) {
4415 printk(KERN_WARNING "md: couldn't update"
4416 " array info. %d\n", err);
4417 goto abort_unlock;
4418 }
4419 goto done_unlock;
4420 }
4421 if (!list_empty(&mddev->disks)) {
4422 printk(KERN_WARNING
4423 "md: array %s already has disks!\n",
4424 mdname(mddev));
4425 err = -EBUSY;
4426 goto abort_unlock;
4427 }
4428 if (mddev->raid_disks) {
4429 printk(KERN_WARNING
4430 "md: array %s already initialised!\n",
4431 mdname(mddev));
4432 err = -EBUSY;
4433 goto abort_unlock;
4434 }
4435 err = set_array_info(mddev, &info);
4436 if (err) {
4437 printk(KERN_WARNING "md: couldn't set"
4438 " array info. %d\n", err);
4439 goto abort_unlock;
4440 }
4441 }
4442 goto done_unlock;
4443
4444 default:;
4445 }
4446
4447 /*
4448 * Commands querying/configuring an existing array:
4449 */
NeilBrown32a76272005-06-21 17:17:14 -07004450 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004451 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrown32a76272005-06-21 17:17:14 -07004452 if (!mddev->raid_disks && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
NeilBrown3f9d7b02006-12-22 01:11:41 -08004453 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4454 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 err = -ENODEV;
4456 goto abort_unlock;
4457 }
4458
4459 /*
4460 * Commands even a read-only array can execute:
4461 */
4462 switch (cmd)
4463 {
4464 case GET_ARRAY_INFO:
4465 err = get_array_info(mddev, argp);
4466 goto done_unlock;
4467
NeilBrown32a76272005-06-21 17:17:14 -07004468 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004469 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004470 goto done_unlock;
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 case GET_DISK_INFO:
4473 err = get_disk_info(mddev, argp);
4474 goto done_unlock;
4475
4476 case RESTART_ARRAY_RW:
4477 err = restart_array(mddev);
4478 goto done_unlock;
4479
4480 case STOP_ARRAY:
4481 err = do_md_stop (mddev, 0);
4482 goto done_unlock;
4483
4484 case STOP_ARRAY_RO:
4485 err = do_md_stop (mddev, 1);
4486 goto done_unlock;
4487
4488 /*
4489 * We have a problem here : there is no easy way to give a CHS
4490 * virtual geometry. We currently pretend that we have a 2 heads
4491 * 4 sectors (with a BIG number of cylinders...). This drives
4492 * dosfs just mad... ;-)
4493 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 }
4495
4496 /*
4497 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004498 * superblock, so we do not allow them on read-only arrays.
4499 * However non-MD ioctls (e.g. get-size) will still come through
4500 * here and hit the 'default' below, so only disallow
4501 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 */
NeilBrownf91de922005-11-08 21:39:36 -08004503 if (_IOC_TYPE(cmd) == MD_MAJOR &&
4504 mddev->ro && mddev->pers) {
4505 if (mddev->ro == 2) {
4506 mddev->ro = 0;
4507 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4508 md_wakeup_thread(mddev->thread);
4509
4510 } else {
4511 err = -EROFS;
4512 goto abort_unlock;
4513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 }
4515
4516 switch (cmd)
4517 {
4518 case ADD_NEW_DISK:
4519 {
4520 mdu_disk_info_t info;
4521 if (copy_from_user(&info, argp, sizeof(info)))
4522 err = -EFAULT;
4523 else
4524 err = add_new_disk(mddev, &info);
4525 goto done_unlock;
4526 }
4527
4528 case HOT_REMOVE_DISK:
4529 err = hot_remove_disk(mddev, new_decode_dev(arg));
4530 goto done_unlock;
4531
4532 case HOT_ADD_DISK:
4533 err = hot_add_disk(mddev, new_decode_dev(arg));
4534 goto done_unlock;
4535
4536 case SET_DISK_FAULTY:
4537 err = set_disk_faulty(mddev, new_decode_dev(arg));
4538 goto done_unlock;
4539
4540 case RUN_ARRAY:
4541 err = do_md_run (mddev);
4542 goto done_unlock;
4543
NeilBrown32a76272005-06-21 17:17:14 -07004544 case SET_BITMAP_FILE:
4545 err = set_bitmap_file(mddev, (int)arg);
4546 goto done_unlock;
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 err = -EINVAL;
4550 goto abort_unlock;
4551 }
4552
4553done_unlock:
4554abort_unlock:
4555 mddev_unlock(mddev);
4556
4557 return err;
4558done:
4559 if (err)
4560 MD_BUG();
4561abort:
4562 return err;
4563}
4564
4565static int md_open(struct inode *inode, struct file *file)
4566{
4567 /*
4568 * Succeed if we can lock the mddev, which confirms that
4569 * it isn't being stopped right now.
4570 */
4571 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4572 int err;
4573
NeilBrownd63a5a72006-12-08 02:36:17 -08004574 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 goto out;
4576
4577 err = 0;
4578 mddev_get(mddev);
4579 mddev_unlock(mddev);
4580
4581 check_disk_change(inode->i_bdev);
4582 out:
4583 return err;
4584}
4585
4586static int md_release(struct inode *inode, struct file * file)
4587{
4588 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
4589
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02004590 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 mddev_put(mddev);
4592
4593 return 0;
4594}
4595
Linus Torvalds44ce6292007-05-09 18:51:36 -07004596static int md_media_changed(struct gendisk *disk)
4597{
4598 mddev_t *mddev = disk->private_data;
4599
4600 return mddev->changed;
4601}
4602
4603static int md_revalidate(struct gendisk *disk)
4604{
4605 mddev_t *mddev = disk->private_data;
4606
4607 mddev->changed = 0;
4608 return 0;
4609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610static struct block_device_operations md_fops =
4611{
4612 .owner = THIS_MODULE,
4613 .open = md_open,
4614 .release = md_release,
4615 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004616 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07004617 .media_changed = md_media_changed,
4618 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619};
4620
Adrian Bunk75c96f82005-05-05 16:16:09 -07004621static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
4623 mdk_thread_t *thread = arg;
4624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 /*
4626 * md_thread is a 'system-thread', it's priority should be very
4627 * high. We avoid resource deadlocks individually in each
4628 * raid personality. (RAID5 does preallocation) We also use RR and
4629 * the very same RT priority as kswapd, thus we will never get
4630 * into a priority inversion deadlock.
4631 *
4632 * we definitely have to have equal or higher priority than
4633 * bdflush, otherwise bdflush will deadlock if there are too
4634 * many dirty RAID5 blocks.
4635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Rafael J. Wysocki4b438a22006-11-08 17:44:48 -08004637 current->flags |= PF_NOFREEZE;
NeilBrown6985c432005-10-19 21:23:47 -07004638 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07004639 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
NeilBrown93588e22005-11-15 00:09:12 -08004641 /* We need to wait INTERRUPTIBLE so that
4642 * we don't add to the load-average.
4643 * That means we need to be sure no signals are
4644 * pending
4645 */
4646 if (signal_pending(current))
4647 flush_signals(current);
4648
4649 wait_event_interruptible_timeout
4650 (thread->wqueue,
4651 test_bit(THREAD_WAKEUP, &thread->flags)
4652 || kthread_should_stop(),
4653 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
4655 clear_bit(THREAD_WAKEUP, &thread->flags);
4656
NeilBrown787453c2005-11-08 21:39:43 -08004657 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
NeilBrowna6fb0932005-09-09 16:23:56 -07004659
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 return 0;
4661}
4662
4663void md_wakeup_thread(mdk_thread_t *thread)
4664{
4665 if (thread) {
4666 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
4667 set_bit(THREAD_WAKEUP, &thread->flags);
4668 wake_up(&thread->wqueue);
4669 }
4670}
4671
4672mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
4673 const char *name)
4674{
4675 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676
NeilBrown9ffae0c2006-01-06 00:20:32 -08004677 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 if (!thread)
4679 return NULL;
4680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 init_waitqueue_head(&thread->wqueue);
4682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 thread->run = run;
4684 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07004685 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07004686 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07004687 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 kfree(thread);
4689 return NULL;
4690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 return thread;
4692}
4693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694void md_unregister_thread(mdk_thread_t *thread)
4695{
NeilBrownd28446f2005-04-16 15:26:41 -07004696 dprintk("interrupting MD-thread pid %d\n", thread->tsk->pid);
NeilBrowna6fb0932005-09-09 16:23:56 -07004697
4698 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 kfree(thread);
4700}
4701
4702void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
4703{
4704 if (!mddev) {
4705 MD_BUG();
4706 return;
4707 }
4708
NeilBrownb2d444d2005-11-08 21:39:31 -08004709 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 return;
NeilBrown32a76272005-06-21 17:17:14 -07004711/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
4713 mdname(mddev),
4714 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
4715 __builtin_return_address(0),__builtin_return_address(1),
4716 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07004717*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07004718 if (!mddev->pers)
4719 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 if (!mddev->pers->error_handler)
4721 return;
4722 mddev->pers->error_handler(mddev,rdev);
4723 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4724 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4725 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07004726 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727}
4728
4729/* seq_file implementation /proc/mdstat */
4730
4731static void status_unused(struct seq_file *seq)
4732{
4733 int i = 0;
4734 mdk_rdev_t *rdev;
4735 struct list_head *tmp;
4736
4737 seq_printf(seq, "unused devices: ");
4738
4739 ITERATE_RDEV_PENDING(rdev,tmp) {
4740 char b[BDEVNAME_SIZE];
4741 i++;
4742 seq_printf(seq, "%s ",
4743 bdevname(rdev->bdev,b));
4744 }
4745 if (!i)
4746 seq_printf(seq, "<none>");
4747
4748 seq_printf(seq, "\n");
4749}
4750
4751
4752static void status_resync(struct seq_file *seq, mddev_t * mddev)
4753{
NeilBrown4588b422006-03-27 01:18:04 -08004754 sector_t max_blocks, resync, res;
4755 unsigned long dt, db, rt;
4756 int scale;
4757 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758
4759 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
4760
4761 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
4762 max_blocks = mddev->resync_max_sectors >> 1;
4763 else
4764 max_blocks = mddev->size;
4765
4766 /*
4767 * Should not happen.
4768 */
4769 if (!max_blocks) {
4770 MD_BUG();
4771 return;
4772 }
NeilBrown4588b422006-03-27 01:18:04 -08004773 /* Pick 'scale' such that (resync>>scale)*1000 will fit
4774 * in a sector_t, and (max_blocks>>scale) will fit in a
4775 * u32, as those are the requirements for sector_div.
4776 * Thus 'scale' must be at least 10
4777 */
4778 scale = 10;
4779 if (sizeof(sector_t) > sizeof(unsigned long)) {
4780 while ( max_blocks/2 > (1ULL<<(scale+32)))
4781 scale++;
4782 }
4783 res = (resync>>scale)*1000;
4784 sector_div(res, (u32)((max_blocks>>scale)+1));
4785
4786 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 {
NeilBrown4588b422006-03-27 01:18:04 -08004788 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 seq_printf(seq, "[");
4790 for (i = 0; i < x; i++)
4791 seq_printf(seq, "=");
4792 seq_printf(seq, ">");
4793 for (i = 0; i < y; i++)
4794 seq_printf(seq, ".");
4795 seq_printf(seq, "] ");
4796 }
NeilBrown4588b422006-03-27 01:18:04 -08004797 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08004798 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
4799 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07004800 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
4801 "check" :
4802 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
4803 "resync" : "recovery"))),
4804 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08004805 (unsigned long long) resync,
4806 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
4808 /*
4809 * We do not want to overflow, so the order of operands and
4810 * the * 100 / 100 trick are important. We do a +1 to be
4811 * safe against division by zero. We only estimate anyway.
4812 *
4813 * dt: time from mark until now
4814 * db: blocks written from mark until now
4815 * rt: remaining time
4816 */
4817 dt = ((jiffies - mddev->resync_mark) / HZ);
4818 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07004819 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
4820 - mddev->resync_mark_cnt;
4821 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822
4823 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
4824
NeilBrownff4e8d92006-07-10 04:44:16 -07004825 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827
4828static void *md_seq_start(struct seq_file *seq, loff_t *pos)
4829{
4830 struct list_head *tmp;
4831 loff_t l = *pos;
4832 mddev_t *mddev;
4833
4834 if (l >= 0x10000)
4835 return NULL;
4836 if (!l--)
4837 /* header */
4838 return (void*)1;
4839
4840 spin_lock(&all_mddevs_lock);
4841 list_for_each(tmp,&all_mddevs)
4842 if (!l--) {
4843 mddev = list_entry(tmp, mddev_t, all_mddevs);
4844 mddev_get(mddev);
4845 spin_unlock(&all_mddevs_lock);
4846 return mddev;
4847 }
4848 spin_unlock(&all_mddevs_lock);
4849 if (!l--)
4850 return (void*)2;/* tail */
4851 return NULL;
4852}
4853
4854static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
4855{
4856 struct list_head *tmp;
4857 mddev_t *next_mddev, *mddev = v;
4858
4859 ++*pos;
4860 if (v == (void*)2)
4861 return NULL;
4862
4863 spin_lock(&all_mddevs_lock);
4864 if (v == (void*)1)
4865 tmp = all_mddevs.next;
4866 else
4867 tmp = mddev->all_mddevs.next;
4868 if (tmp != &all_mddevs)
4869 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
4870 else {
4871 next_mddev = (void*)2;
4872 *pos = 0x10000;
4873 }
4874 spin_unlock(&all_mddevs_lock);
4875
4876 if (v != (void*)1)
4877 mddev_put(mddev);
4878 return next_mddev;
4879
4880}
4881
4882static void md_seq_stop(struct seq_file *seq, void *v)
4883{
4884 mddev_t *mddev = v;
4885
4886 if (mddev && v != (void*)1 && v != (void*)2)
4887 mddev_put(mddev);
4888}
4889
NeilBrownd7603b72006-01-06 00:20:30 -08004890struct mdstat_info {
4891 int event;
4892};
4893
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894static int md_seq_show(struct seq_file *seq, void *v)
4895{
4896 mddev_t *mddev = v;
4897 sector_t size;
4898 struct list_head *tmp2;
4899 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08004900 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07004901 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
4903 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08004904 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 seq_printf(seq, "Personalities : ");
4906 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08004907 list_for_each_entry(pers, &pers_list, list)
4908 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909
4910 spin_unlock(&pers_lock);
4911 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08004912 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 return 0;
4914 }
4915 if (v == (void*)2) {
4916 status_unused(seq);
4917 return 0;
4918 }
4919
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004920 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004922
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
4924 seq_printf(seq, "%s : %sactive", mdname(mddev),
4925 mddev->pers ? "" : "in");
4926 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004927 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08004929 if (mddev->ro==2)
4930 seq_printf(seq, "(auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 seq_printf(seq, " %s", mddev->pers->name);
4932 }
4933
4934 size = 0;
4935 ITERATE_RDEV(mddev,rdev,tmp2) {
4936 char b[BDEVNAME_SIZE];
4937 seq_printf(seq, " %s[%d]",
4938 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004939 if (test_bit(WriteMostly, &rdev->flags))
4940 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08004941 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 seq_printf(seq, "(F)");
4943 continue;
NeilBrownb325a322005-09-09 16:24:00 -07004944 } else if (rdev->raid_disk < 0)
4945 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 size += rdev->size;
4947 }
4948
4949 if (!list_empty(&mddev->disks)) {
4950 if (mddev->pers)
4951 seq_printf(seq, "\n %llu blocks",
4952 (unsigned long long)mddev->array_size);
4953 else
4954 seq_printf(seq, "\n %llu blocks",
4955 (unsigned long long)size);
4956 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07004957 if (mddev->persistent) {
4958 if (mddev->major_version != 0 ||
4959 mddev->minor_version != 90) {
4960 seq_printf(seq," super %d.%d",
4961 mddev->major_version,
4962 mddev->minor_version);
4963 }
4964 } else
4965 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
4967 if (mddev->pers) {
4968 mddev->pers->status (seq, mddev);
4969 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08004970 if (mddev->pers->sync_request) {
4971 if (mddev->curr_resync > 2) {
4972 status_resync (seq, mddev);
4973 seq_printf(seq, "\n ");
4974 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
4975 seq_printf(seq, "\tresync=DELAYED\n ");
4976 else if (mddev->recovery_cp < MaxSector)
4977 seq_printf(seq, "\tresync=PENDING\n ");
4978 }
NeilBrown32a76272005-06-21 17:17:14 -07004979 } else
4980 seq_printf(seq, "\n ");
4981
4982 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07004983 unsigned long chunk_kb;
4984 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07004985 spin_lock_irqsave(&bitmap->lock, flags);
4986 chunk_kb = bitmap->chunksize >> 10;
4987 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
4988 "%lu%s chunk",
4989 bitmap->pages - bitmap->missing_pages,
4990 bitmap->pages,
4991 (bitmap->pages - bitmap->missing_pages)
4992 << (PAGE_SHIFT - 10),
4993 chunk_kb ? chunk_kb : bitmap->chunksize,
4994 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07004995 if (bitmap->file) {
4996 seq_printf(seq, ", file: ");
Josef Sipekc649bb92006-12-08 02:37:19 -08004997 seq_path(seq, bitmap->file->f_path.mnt,
4998 bitmap->file->f_path.dentry," \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07004999 }
NeilBrown78d742d2005-06-21 17:17:15 -07005000
NeilBrown32a76272005-06-21 17:17:14 -07005001 seq_printf(seq, "\n");
5002 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 }
5004
5005 seq_printf(seq, "\n");
5006 }
5007 mddev_unlock(mddev);
5008
5009 return 0;
5010}
5011
5012static struct seq_operations md_seq_ops = {
5013 .start = md_seq_start,
5014 .next = md_seq_next,
5015 .stop = md_seq_stop,
5016 .show = md_seq_show,
5017};
5018
5019static int md_seq_open(struct inode *inode, struct file *file)
5020{
5021 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005022 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5023 if (mi == NULL)
5024 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025
5026 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005027 if (error)
5028 kfree(mi);
5029 else {
5030 struct seq_file *p = file->private_data;
5031 p->private = mi;
5032 mi->event = atomic_read(&md_event_count);
5033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 return error;
5035}
5036
NeilBrownd7603b72006-01-06 00:20:30 -08005037static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5038{
5039 struct seq_file *m = filp->private_data;
5040 struct mdstat_info *mi = m->private;
5041 int mask;
5042
5043 poll_wait(filp, &md_event_waiters, wait);
5044
5045 /* always allow read */
5046 mask = POLLIN | POLLRDNORM;
5047
5048 if (mi->event != atomic_read(&md_event_count))
5049 mask |= POLLERR | POLLPRI;
5050 return mask;
5051}
5052
Arjan van de Venfa027c22007-02-12 00:55:33 -08005053static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005054 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 .open = md_seq_open,
5056 .read = seq_read,
5057 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07005058 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005059 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060};
5061
NeilBrown2604b702006-01-06 00:20:36 -08005062int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005065 list_add_tail(&p->list, &pers_list);
5066 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 spin_unlock(&pers_lock);
5068 return 0;
5069}
5070
NeilBrown2604b702006-01-06 00:20:36 -08005071int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072{
NeilBrown2604b702006-01-06 00:20:36 -08005073 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005075 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 spin_unlock(&pers_lock);
5077 return 0;
5078}
5079
5080static int is_mddev_idle(mddev_t *mddev)
5081{
5082 mdk_rdev_t * rdev;
5083 struct list_head *tmp;
5084 int idle;
5085 unsigned long curr_events;
5086
5087 idle = 1;
5088 ITERATE_RDEV(mddev,rdev,tmp) {
5089 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005090 curr_events = disk_stat_read(disk, sectors[0]) +
5091 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 atomic_read(&disk->sync_io);
NeilBrownc0e48522005-11-18 01:11:01 -08005093 /* The difference between curr_events and last_events
5094 * will be affected by any new non-sync IO (making
5095 * curr_events bigger) and any difference in the amount of
5096 * in-flight syncio (making current_events bigger or smaller)
5097 * The amount in-flight is currently limited to
5098 * 32*64K in raid1/10 and 256*PAGE_SIZE in raid5/6
5099 * which is at most 4096 sectors.
5100 * These numbers are fairly fragile and should be made
5101 * more robust, probably by enforcing the
5102 * 'window size' that md_do_sync sort-of uses.
5103 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 * Note: the following is an unsigned comparison.
5105 */
NeilBrown435b71b2007-05-10 22:23:31 -07005106 if ((long)curr_events - (long)rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 rdev->last_events = curr_events;
5108 idle = 0;
5109 }
5110 }
5111 return idle;
5112}
5113
5114void md_done_sync(mddev_t *mddev, int blocks, int ok)
5115{
5116 /* another "blocks" (512byte) blocks have been synced */
5117 atomic_sub(blocks, &mddev->recovery_active);
5118 wake_up(&mddev->recovery_wait);
5119 if (!ok) {
5120 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5121 md_wakeup_thread(mddev->thread);
5122 // stop recovery, signal do_sync ....
5123 }
5124}
5125
5126
NeilBrown06d91a52005-06-21 17:17:12 -07005127/* md_write_start(mddev, bi)
5128 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005129 * in superblock) before writing, schedule a superblock update
5130 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005131 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005132void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133{
NeilBrown06d91a52005-06-21 17:17:12 -07005134 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005135 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005136
NeilBrownf91de922005-11-08 21:39:36 -08005137 BUG_ON(mddev->ro == 1);
5138 if (mddev->ro == 2) {
5139 /* need to switch to read/write */
5140 mddev->ro = 0;
5141 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5142 md_wakeup_thread(mddev->thread);
5143 }
NeilBrown06d91a52005-06-21 17:17:12 -07005144 atomic_inc(&mddev->writes_pending);
NeilBrown06d91a52005-06-21 17:17:12 -07005145 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005146 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005147 if (mddev->in_sync) {
5148 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005149 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005150 md_wakeup_thread(mddev->thread);
5151 }
NeilBrowna9701a32005-11-08 21:39:34 -08005152 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005153 }
NeilBrown850b2b42006-10-03 01:15:46 -07005154 wait_event(mddev->sb_wait, mddev->flags==0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155}
5156
5157void md_write_end(mddev_t *mddev)
5158{
5159 if (atomic_dec_and_test(&mddev->writes_pending)) {
5160 if (mddev->safemode == 2)
5161 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005162 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5164 }
5165}
5166
NeilBrown2a2275d2007-01-26 00:57:11 -08005167/* md_allow_write(mddev)
5168 * Calling this ensures that the array is marked 'active' so that writes
5169 * may proceed without blocking. It is important to call this before
5170 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5171 * Must be called with mddev_lock held.
5172 */
5173void md_allow_write(mddev_t *mddev)
5174{
5175 if (!mddev->pers)
5176 return;
5177 if (mddev->ro)
5178 return;
5179
5180 spin_lock_irq(&mddev->write_lock);
5181 if (mddev->in_sync) {
5182 mddev->in_sync = 0;
5183 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5184 if (mddev->safemode_delay &&
5185 mddev->safemode == 0)
5186 mddev->safemode = 1;
5187 spin_unlock_irq(&mddev->write_lock);
5188 md_update_sb(mddev, 0);
5189 } else
5190 spin_unlock_irq(&mddev->write_lock);
5191}
5192EXPORT_SYMBOL_GPL(md_allow_write);
5193
Adrian Bunk75c96f82005-05-05 16:16:09 -07005194static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195
5196#define SYNC_MARKS 10
5197#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005198void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199{
5200 mddev_t *mddev2;
5201 unsigned int currspeed = 0,
5202 window;
NeilBrown57afd892005-06-21 17:17:13 -07005203 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 unsigned long mark[SYNC_MARKS];
5205 sector_t mark_cnt[SYNC_MARKS];
5206 int last_mark,m;
5207 struct list_head *tmp;
5208 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005209 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005210 struct list_head *rtmp;
5211 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005212 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
5214 /* just incase thread restarts... */
5215 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5216 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005217 if (mddev->ro) /* never try to sync a read-only array */
5218 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219
NeilBrown61df9d92006-10-03 01:15:57 -07005220 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5221 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5222 desc = "data-check";
5223 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5224 desc = "requested-resync";
5225 else
5226 desc = "resync";
5227 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5228 desc = "reshape";
5229 else
5230 desc = "recovery";
5231
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 /* we overload curr_resync somewhat here.
5233 * 0 == not engaged in resync at all
5234 * 2 == checking that there is no conflict with another sync
5235 * 1 == like 2, but have yielded to allow conflicting resync to
5236 * commense
5237 * other == active in resync - this many blocks
5238 *
5239 * Before starting a resync we must have set curr_resync to
5240 * 2, and then checked that every "conflicting" array has curr_resync
5241 * less than ours. When we find one that is the same or higher
5242 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5243 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5244 * This will mean we have to start checking from the beginning again.
5245 *
5246 */
5247
5248 do {
5249 mddev->curr_resync = 2;
5250
5251 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005252 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005253 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 goto skip;
5255 }
5256 ITERATE_MDDEV(mddev2,tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 if (mddev2 == mddev)
5258 continue;
5259 if (mddev2->curr_resync &&
5260 match_mddev_units(mddev,mddev2)) {
5261 DEFINE_WAIT(wq);
5262 if (mddev < mddev2 && mddev->curr_resync == 2) {
5263 /* arbitrarily yield */
5264 mddev->curr_resync = 1;
5265 wake_up(&resync_wait);
5266 }
5267 if (mddev > mddev2 && mddev->curr_resync == 1)
5268 /* no need to wait here, we can wait the next
5269 * time 'round when curr_resync == 2
5270 */
5271 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005272 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5273 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005274 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005275 printk(KERN_INFO "md: delaying %s of %s"
5276 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005278 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 mddev_put(mddev2);
5280 schedule();
5281 finish_wait(&resync_wait, &wq);
5282 goto try_again;
5283 }
5284 finish_wait(&resync_wait, &wq);
5285 }
5286 }
5287 } while (mddev->curr_resync < 2);
5288
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005289 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005290 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005292 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 */
5294 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005295 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005296 /* we don't use the checkpoint if there's a bitmap */
5297 if (!mddev->bitmap &&
5298 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5299 j = mddev->recovery_cp;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005300 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5301 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005302 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 /* recovery follows the physical size of devices */
5304 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005305 j = MaxSector;
5306 ITERATE_RDEV(mddev,rdev,rtmp)
5307 if (rdev->raid_disk >= 0 &&
5308 !test_bit(Faulty, &rdev->flags) &&
5309 !test_bit(In_sync, &rdev->flags) &&
5310 rdev->recovery_offset < j)
5311 j = rdev->recovery_offset;
5312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313
NeilBrown61df9d92006-10-03 01:15:57 -07005314 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5315 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5316 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005317 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005318 "(but not more than %d KB/sec) for %s.\n",
5319 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320
5321 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005322
NeilBrown57afd892005-06-21 17:17:13 -07005323 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 for (m = 0; m < SYNC_MARKS; m++) {
5325 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005326 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 }
5328 last_mark = 0;
5329 mddev->resync_mark = mark[last_mark];
5330 mddev->resync_mark_cnt = mark_cnt[last_mark];
5331
5332 /*
5333 * Tune reconstruction:
5334 */
5335 window = 32*(PAGE_SIZE/512);
5336 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5337 window/2,(unsigned long long) max_sectors/2);
5338
5339 atomic_set(&mddev->recovery_active, 0);
5340 init_waitqueue_head(&mddev->recovery_wait);
5341 last_check = 0;
5342
5343 if (j>2) {
5344 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005345 "md: resuming %s of %s from checkpoint.\n",
5346 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 mddev->curr_resync = j;
5348 }
5349
5350 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005351 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
NeilBrown57afd892005-06-21 17:17:13 -07005353 skipped = 0;
5354 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrown88202a02006-01-06 00:21:36 -08005355 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005356 if (sectors == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
5358 goto out;
5359 }
NeilBrown57afd892005-06-21 17:17:13 -07005360
5361 if (!skipped) { /* actual IO requested */
5362 io_sectors += sectors;
5363 atomic_add(sectors, &mddev->recovery_active);
5364 }
5365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 j += sectors;
5367 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005368 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005369 if (last_check == 0)
5370 /* this is the earliers that rebuilt will be
5371 * visible in /proc/mdstat
5372 */
5373 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005374
5375 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 continue;
5377
NeilBrown57afd892005-06-21 17:17:13 -07005378 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379
5380 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery) ||
5381 test_bit(MD_RECOVERY_ERR, &mddev->recovery))
5382 break;
5383
5384 repeat:
5385 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5386 /* step marks */
5387 int next = (last_mark+1) % SYNC_MARKS;
5388
5389 mddev->resync_mark = mark[next];
5390 mddev->resync_mark_cnt = mark_cnt[next];
5391 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005392 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 last_mark = next;
5394 }
5395
5396
NeilBrown787453c2005-11-08 21:39:43 -08005397 if (kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 /*
5399 * got a signal, exit.
5400 */
5401 printk(KERN_INFO
5402 "md: md_do_sync() got signal ... exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5404 goto out;
5405 }
5406
5407 /*
5408 * this loop exits only if either when we are slower than
5409 * the 'hard' speed limit, or the system was IO-idle for
5410 * a jiffy.
5411 * the system might be non-idle CPU-wise, but we only care
5412 * about not overloading the IO subsystem. (things like an
5413 * e2fsck being done on the RAID array should execute fast)
5414 */
5415 mddev->queue->unplug_fn(mddev->queue);
5416 cond_resched();
5417
NeilBrown57afd892005-06-21 17:17:13 -07005418 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5419 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420
NeilBrown88202a02006-01-06 00:21:36 -08005421 if (currspeed > speed_min(mddev)) {
5422 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005424 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 goto repeat;
5426 }
5427 }
5428 }
NeilBrown61df9d92006-10-03 01:15:57 -07005429 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 /*
5431 * this also signals 'finished resyncing' to md_stop
5432 */
5433 out:
5434 mddev->queue->unplug_fn(mddev->queue);
5435
5436 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5437
5438 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005439 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440
5441 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
NeilBrownccfcc3c2006-03-27 01:18:09 -08005442 !test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005443 mddev->curr_resync > 2) {
5444 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5445 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5446 if (mddev->curr_resync >= mddev->recovery_cp) {
5447 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005448 "md: checkpointing %s of %s.\n",
5449 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005450 mddev->recovery_cp = mddev->curr_resync;
5451 }
5452 } else
5453 mddev->recovery_cp = MaxSector;
5454 } else {
5455 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5456 mddev->curr_resync = MaxSector;
5457 ITERATE_RDEV(mddev,rdev,rtmp)
5458 if (rdev->raid_disk >= 0 &&
5459 !test_bit(Faulty, &rdev->flags) &&
5460 !test_bit(In_sync, &rdev->flags) &&
5461 rdev->recovery_offset < mddev->curr_resync)
5462 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
NeilBrown17571282006-12-10 02:20:52 -08005465 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 skip:
5468 mddev->curr_resync = 0;
5469 wake_up(&resync_wait);
5470 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5471 md_wakeup_thread(mddev->thread);
5472}
NeilBrown29269552006-03-27 01:18:10 -08005473EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005476static int remove_and_add_spares(mddev_t *mddev)
5477{
5478 mdk_rdev_t *rdev;
5479 struct list_head *rtmp;
5480 int spares = 0;
5481
5482 ITERATE_RDEV(mddev,rdev,rtmp)
5483 if (rdev->raid_disk >= 0 &&
5484 (test_bit(Faulty, &rdev->flags) ||
5485 ! test_bit(In_sync, &rdev->flags)) &&
5486 atomic_read(&rdev->nr_pending)==0) {
5487 if (mddev->pers->hot_remove_disk(
5488 mddev, rdev->raid_disk)==0) {
5489 char nm[20];
5490 sprintf(nm,"rd%d", rdev->raid_disk);
5491 sysfs_remove_link(&mddev->kobj, nm);
5492 rdev->raid_disk = -1;
5493 }
5494 }
5495
5496 if (mddev->degraded) {
5497 ITERATE_RDEV(mddev,rdev,rtmp)
5498 if (rdev->raid_disk < 0
5499 && !test_bit(Faulty, &rdev->flags)) {
5500 rdev->recovery_offset = 0;
5501 if (mddev->pers->hot_add_disk(mddev,rdev)) {
5502 char nm[20];
5503 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005504 if (sysfs_create_link(&mddev->kobj,
5505 &rdev->kobj, nm))
5506 printk(KERN_WARNING
5507 "md: cannot register "
5508 "%s for %s\n",
5509 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005510 spares++;
5511 md_new_event(mddev);
5512 } else
5513 break;
5514 }
5515 }
5516 return spares;
5517}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518/*
5519 * This routine is regularly called by all per-raid-array threads to
5520 * deal with generic issues like resync and super-block update.
5521 * Raid personalities that don't have a thread (linear/raid0) do not
5522 * need this as they never do any recovery or update the superblock.
5523 *
5524 * It does not do any resync itself, but rather "forks" off other threads
5525 * to do that as needed.
5526 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
5527 * "->recovery" and create a thread at ->sync_thread.
5528 * When the thread finishes it sets MD_RECOVERY_DONE (and might set MD_RECOVERY_ERR)
5529 * and wakeups up this thread which will reap the thread and finish up.
5530 * This thread also removes any faulty devices (with nr_pending == 0).
5531 *
5532 * The overall approach is:
5533 * 1/ if the superblock needs updating, update it.
5534 * 2/ If a recovery thread is running, don't do anything else.
5535 * 3/ If recovery has finished, clean up, possibly marking spares active.
5536 * 4/ If there are any faulty devices, remove them.
5537 * 5/ If array is degraded, try to add spares devices
5538 * 6/ If array has spares or is not in-sync, start a resync thread.
5539 */
5540void md_check_recovery(mddev_t *mddev)
5541{
5542 mdk_rdev_t *rdev;
5543 struct list_head *rtmp;
5544
5545
NeilBrown5f404022005-06-21 17:17:16 -07005546 if (mddev->bitmap)
5547 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548
5549 if (mddev->ro)
5550 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005551
5552 if (signal_pending(current)) {
5553 if (mddev->pers->sync_request) {
5554 printk(KERN_INFO "md: %s in immediate safe mode\n",
5555 mdname(mddev));
5556 mddev->safemode = 2;
5557 }
5558 flush_signals(current);
5559 }
5560
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 if ( ! (
NeilBrown850b2b42006-10-03 01:15:46 -07005562 mddev->flags ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07005564 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
5565 (mddev->safemode == 1) ||
5566 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
5567 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 ))
5569 return;
NeilBrownfca4d842005-06-21 17:17:11 -07005570
NeilBrowndf5b89b2006-03-27 01:18:20 -08005571 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005572 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07005573
NeilBrowna9701a32005-11-08 21:39:34 -08005574 spin_lock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005575 if (mddev->safemode && !atomic_read(&mddev->writes_pending) &&
5576 !mddev->in_sync && mddev->recovery_cp == MaxSector) {
5577 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07005578 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownfca4d842005-06-21 17:17:11 -07005579 }
5580 if (mddev->safemode == 1)
5581 mddev->safemode = 0;
NeilBrowna9701a32005-11-08 21:39:34 -08005582 spin_unlock_irq(&mddev->write_lock);
NeilBrownfca4d842005-06-21 17:17:11 -07005583
NeilBrown850b2b42006-10-03 01:15:46 -07005584 if (mddev->flags)
5585 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07005586
NeilBrown06d91a52005-06-21 17:17:12 -07005587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
5589 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
5590 /* resync/recovery still happening */
5591 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5592 goto unlock;
5593 }
5594 if (mddev->sync_thread) {
5595 /* resync has finished, collect result */
5596 md_unregister_thread(mddev->sync_thread);
5597 mddev->sync_thread = NULL;
5598 if (!test_bit(MD_RECOVERY_ERR, &mddev->recovery) &&
5599 !test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5600 /* success...*/
5601 /* activate any spares */
5602 mddev->pers->spare_active(mddev);
5603 }
NeilBrown850b2b42006-10-03 01:15:46 -07005604 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07005605
5606 /* if array is no-longer degraded, then any saved_raid_disk
5607 * information must be scrapped
5608 */
5609 if (!mddev->degraded)
5610 ITERATE_RDEV(mddev,rdev,rtmp)
5611 rdev->saved_raid_disk = -1;
5612
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 mddev->recovery = 0;
5614 /* flag recovery needed just to double check */
5615 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrownd7603b72006-01-06 00:20:30 -08005616 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 goto unlock;
5618 }
NeilBrown24dd4692005-11-08 21:39:26 -08005619 /* Clear some bits that don't mean anything, but
5620 * might be left set
5621 */
5622 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5623 clear_bit(MD_RECOVERY_ERR, &mddev->recovery);
5624 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
5625 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005627 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
5628 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 /* no recovery is running.
5630 * remove any failed drives, then
5631 * add spares if possible.
5632 * Spare are also removed and re-added, to allow
5633 * the personality to fail the re-add.
5634 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005636 if (mddev->reshape_position != MaxSector) {
5637 if (mddev->pers->check_reshape(mddev) != 0)
5638 /* Cannot proceed */
5639 goto unlock;
5640 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5641 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08005642 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5643 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5644 } else if (mddev->recovery_cp < MaxSector) {
5645 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5646 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5647 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08005649
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 if (mddev->pers->sync_request) {
5651 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
NeilBrowna654b9d82005-06-21 17:17:27 -07005652 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
5653 /* We are adding a device or devices to an array
5654 * which has the bitmap stored on all devices.
5655 * So make sure all bitmap pages get written
5656 */
5657 bitmap_write_all(mddev->bitmap);
5658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 mddev->sync_thread = md_register_thread(md_do_sync,
5660 mddev,
5661 "%s_resync");
5662 if (!mddev->sync_thread) {
5663 printk(KERN_ERR "%s: could not start resync"
5664 " thread...\n",
5665 mdname(mddev));
5666 /* leave the spares where they are, it shouldn't hurt */
5667 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08005668 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 md_wakeup_thread(mddev->sync_thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005670 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 }
5672 unlock:
5673 mddev_unlock(mddev);
5674 }
5675}
5676
Adrian Bunk75c96f82005-05-05 16:16:09 -07005677static int md_notify_reboot(struct notifier_block *this,
5678 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679{
5680 struct list_head *tmp;
5681 mddev_t *mddev;
5682
5683 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
5684
5685 printk(KERN_INFO "md: stopping all md devices.\n");
5686
5687 ITERATE_MDDEV(mddev,tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10005688 if (mddev_trylock(mddev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 do_md_stop (mddev, 1);
Neil Brownc71d4882006-05-26 10:39:25 +10005690 mddev_unlock(mddev);
5691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 /*
5693 * certain more exotic SCSI devices are known to be
5694 * volatile wrt too early system reboots. While the
5695 * right place to handle this issue is the given
5696 * driver, we do want to have a safe RAID driver ...
5697 */
5698 mdelay(1000*1);
5699 }
5700 return NOTIFY_DONE;
5701}
5702
Adrian Bunk75c96f82005-05-05 16:16:09 -07005703static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 .notifier_call = md_notify_reboot,
5705 .next = NULL,
5706 .priority = INT_MAX, /* before any real devices */
5707};
5708
5709static void md_geninit(void)
5710{
5711 struct proc_dir_entry *p;
5712
5713 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
5714
5715 p = create_proc_entry("mdstat", S_IRUGO, NULL);
5716 if (p)
5717 p->proc_fops = &md_seq_fops;
5718}
5719
Adrian Bunk75c96f82005-05-05 16:16:09 -07005720static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 if (register_blkdev(MAJOR_NR, "md"))
5723 return -1;
5724 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
5725 unregister_blkdev(MAJOR_NR, "md");
5726 return -1;
5727 }
NeilBrowne8703fe2006-10-03 01:15:59 -07005728 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
5729 md_probe, NULL, NULL);
5730 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 md_probe, NULL, NULL);
5732
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08005734 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
5736 md_geninit();
5737 return (0);
5738}
5739
5740
5741#ifndef MODULE
5742
5743/*
5744 * Searches all registered partitions for autorun RAID arrays
5745 * at boot time.
5746 */
5747static dev_t detected_devices[128];
5748static int dev_cnt;
5749
5750void md_autodetect_dev(dev_t dev)
5751{
5752 if (dev_cnt >= 0 && dev_cnt < 127)
5753 detected_devices[dev_cnt++] = dev;
5754}
5755
5756
5757static void autostart_arrays(int part)
5758{
5759 mdk_rdev_t *rdev;
5760 int i;
5761
5762 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
5763
5764 for (i = 0; i < dev_cnt; i++) {
5765 dev_t dev = detected_devices[i];
5766
5767 rdev = md_import_device(dev,0, 0);
5768 if (IS_ERR(rdev))
5769 continue;
5770
NeilBrownb2d444d2005-11-08 21:39:31 -08005771 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 MD_BUG();
5773 continue;
5774 }
5775 list_add(&rdev->same_set, &pending_raid_disks);
5776 }
5777 dev_cnt = 0;
5778
5779 autorun_devices(part);
5780}
5781
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08005782#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783
5784static __exit void md_exit(void)
5785{
5786 mddev_t *mddev;
5787 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07005788
NeilBrowne8703fe2006-10-03 01:15:59 -07005789 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
5790 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791
5792 unregister_blkdev(MAJOR_NR,"md");
5793 unregister_blkdev(mdp_major, "mdp");
5794 unregister_reboot_notifier(&md_notifier);
5795 unregister_sysctl_table(raid_table_header);
5796 remove_proc_entry("mdstat", NULL);
5797 ITERATE_MDDEV(mddev,tmp) {
5798 struct gendisk *disk = mddev->gendisk;
5799 if (!disk)
5800 continue;
5801 export_array(mddev);
5802 del_gendisk(disk);
5803 put_disk(disk);
5804 mddev->gendisk = NULL;
5805 mddev_put(mddev);
5806 }
5807}
5808
5809module_init(md_init)
5810module_exit(md_exit)
5811
NeilBrownf91de922005-11-08 21:39:36 -08005812static int get_ro(char *buffer, struct kernel_param *kp)
5813{
5814 return sprintf(buffer, "%d", start_readonly);
5815}
5816static int set_ro(const char *val, struct kernel_param *kp)
5817{
5818 char *e;
5819 int num = simple_strtoul(val, &e, 10);
5820 if (*val && (*e == '\0' || *e == '\n')) {
5821 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08005822 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08005823 }
5824 return -EINVAL;
5825}
5826
NeilBrown80ca3a42006-07-10 04:44:18 -07005827module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
5828module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08005829
NeilBrownf91de922005-11-08 21:39:36 -08005830
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831EXPORT_SYMBOL(register_md_personality);
5832EXPORT_SYMBOL(unregister_md_personality);
5833EXPORT_SYMBOL(md_error);
5834EXPORT_SYMBOL(md_done_sync);
5835EXPORT_SYMBOL(md_write_start);
5836EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005837EXPORT_SYMBOL(md_register_thread);
5838EXPORT_SYMBOL(md_unregister_thread);
5839EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005840EXPORT_SYMBOL(md_check_recovery);
5841MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07005842MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07005843MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);