blob: c1a837ca193c26c4c74af8c975b76201005c2533 [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
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070037#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrown32a76272005-06-21 17:17:14 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#define MAJOR_NR MD_MAJOR
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* 63 partitions with the alternate major number (mdp) */
52#define MdpMinorShift 6
53
54#define DEBUG 0
55#define dprintk(x...) ((void)(DEBUG && printk(x)))
56
57
58#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110059static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#endif
61
NeilBrown2604b702006-01-06 00:20:36 -080062static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static DEFINE_SPINLOCK(pers_lock);
64
Adrian Bunk5e563412006-06-26 00:27:42 -070065static void md_print_devices(void);
66
Bernd Schubert90b08712008-05-23 13:04:38 -070067static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
68
Adrian Bunk5e563412006-06-26 00:27:42 -070069#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
73 * is 1000 KB/sec, so the extra system load does not show up that much.
74 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070075 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * subsystem is idle. There is also an 'absolute maximum' reconstruction
77 * speed limit - in case reconstruction slows down your system despite
78 * idle IO detection.
79 *
80 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080081 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
84static int sysctl_speed_limit_min = 1000;
85static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080086static inline int speed_min(mddev_t *mddev)
87{
88 return mddev->sync_speed_min ?
89 mddev->sync_speed_min : sysctl_speed_limit_min;
90}
91
92static inline int speed_max(mddev_t *mddev)
93{
94 return mddev->sync_speed_max ?
95 mddev->sync_speed_max : sysctl_speed_limit_max;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98static struct ctl_table_header *raid_table_header;
99
100static ctl_table raid_table[] = {
101 {
102 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
103 .procname = "speed_limit_min",
104 .data = &sysctl_speed_limit_min,
105 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700106 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .proc_handler = &proc_dointvec,
108 },
109 {
110 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
111 .procname = "speed_limit_max",
112 .data = &sysctl_speed_limit_max,
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 { .ctl_name = 0 }
118};
119
120static ctl_table raid_dir_table[] = {
121 {
122 .ctl_name = DEV_RAID,
123 .procname = "raid",
124 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700125 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 .child = raid_table,
127 },
128 { .ctl_name = 0 }
129};
130
131static ctl_table raid_root_table[] = {
132 {
133 .ctl_name = CTL_DEV,
134 .procname = "dev",
135 .maxlen = 0,
136 .mode = 0555,
137 .child = raid_dir_table,
138 },
139 { .ctl_name = 0 }
140};
141
142static struct block_device_operations md_fops;
143
NeilBrownf91de922005-11-08 21:39:36 -0800144static int start_readonly;
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
NeilBrownd7603b72006-01-06 00:20:30 -0800147 * We have a system wide 'event count' that is incremented
148 * on any 'interesting' event, and readers of /proc/mdstat
149 * can use 'poll' or 'select' to find out when the event
150 * count increases.
151 *
152 * Events are:
153 * start array, stop array, error, add device, remove device,
154 * start build, activate spare
155 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800156static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800157static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800158void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800159{
160 atomic_inc(&md_event_count);
161 wake_up(&md_event_waiters);
162}
NeilBrown29269552006-03-27 01:18:10 -0800163EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800164
NeilBrownc331eb02006-05-30 21:27:13 -0700165/* Alternate version that can be called from interrupts
166 * when calling sysfs_notify isn't needed.
167 */
Adrian Bunk05381952006-06-26 00:28:01 -0700168static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700169{
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
172}
173
NeilBrownd7603b72006-01-06 00:20:30 -0800174/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * Enables to iterate over all existing md arrays
176 * all_mddevs_lock protects this list.
177 */
178static LIST_HEAD(all_mddevs);
179static DEFINE_SPINLOCK(all_mddevs_lock);
180
181
182/*
183 * iterates through all used mddevs in the system.
184 * We take care to grab the all_mddevs_lock whenever navigating
185 * the list, and to always hold a refcount when unlocked.
186 * Any code which breaks out of this loop while own
187 * a reference to the current mddev and must mddev_put it.
188 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800189#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 \
191 for (({ spin_lock(&all_mddevs_lock); \
192 tmp = all_mddevs.next; \
193 mddev = NULL;}); \
194 ({ if (tmp != &all_mddevs) \
195 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
196 spin_unlock(&all_mddevs_lock); \
197 if (mddev) mddev_put(mddev); \
198 mddev = list_entry(tmp, mddev_t, all_mddevs); \
199 tmp != &all_mddevs;}); \
200 ({ spin_lock(&all_mddevs_lock); \
201 tmp = tmp->next;}) \
202 )
203
204
NeilBrownd710e132008-10-13 11:55:12 +1100205static int md_fail_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
NeilBrown6712ecf2007-09-27 12:47:43 +0200207 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return 0;
209}
210
211static inline mddev_t *mddev_get(mddev_t *mddev)
212{
213 atomic_inc(&mddev->active);
214 return mddev;
215}
216
217static void mddev_put(mddev_t *mddev)
218{
219 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
220 return;
221 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
222 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800223 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500224 blk_cleanup_queue(mddev->queue);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800225 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800226 } else
227 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static mddev_t * mddev_find(dev_t unit)
231{
232 mddev_t *mddev, *new = NULL;
233
234 retry:
235 spin_lock(&all_mddevs_lock);
236 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
237 if (mddev->unit == unit) {
238 mddev_get(mddev);
239 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700240 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return mddev;
242 }
243
244 if (new) {
245 list_add(&new->all_mddevs, &all_mddevs);
246 spin_unlock(&all_mddevs_lock);
247 return new;
248 }
249 spin_unlock(&all_mddevs_lock);
250
NeilBrown9ffae0c2006-01-06 00:20:32 -0800251 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!new)
253 return NULL;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 new->unit = unit;
256 if (MAJOR(unit) == MD_MAJOR)
257 new->md_minor = MINOR(unit);
258 else
259 new->md_minor = MINOR(unit) >> MdpMinorShift;
260
NeilBrowndf5b89b2006-03-27 01:18:20 -0800261 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 INIT_LIST_HEAD(&new->disks);
263 INIT_LIST_HEAD(&new->all_mddevs);
264 init_timer(&new->safemode_timer);
265 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000266 atomic_set(&new->openers, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700267 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700268 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700269 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700270 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000271 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800272 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700273 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 new->queue = blk_alloc_queue(GFP_KERNEL);
276 if (!new->queue) {
277 kfree(new);
278 return NULL;
279 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200280 /* Can be unlocked because the queue is new: no concurrency */
281 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 blk_queue_make_request(new->queue, md_fail_request);
284
285 goto retry;
286}
287
288static inline int mddev_lock(mddev_t * mddev)
289{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800290 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static inline int mddev_trylock(mddev_t * mddev)
294{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800295 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static inline void mddev_unlock(mddev_t * mddev)
299{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800300 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
NeilBrown005eca52005-08-22 13:11:08 -0700302 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
NeilBrown2989ddb2006-01-06 00:20:43 -0800305static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 mdk_rdev_t * rdev;
308 struct list_head *tmp;
309
NeilBrownd089c6a2008-02-06 01:39:59 -0800310 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (rdev->desc_nr == nr)
312 return rdev;
313 }
314 return NULL;
315}
316
317static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
318{
319 struct list_head *tmp;
320 mdk_rdev_t *rdev;
321
NeilBrownd089c6a2008-02-06 01:39:59 -0800322 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (rdev->bdev->bd_dev == dev)
324 return rdev;
325 }
326 return NULL;
327}
328
NeilBrownd9d166c2006-01-06 00:20:51 -0800329static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800330{
331 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800332 list_for_each_entry(pers, &pers_list, list) {
333 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800334 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800335 if (strcmp(pers->name, clevel)==0)
336 return pers;
337 }
NeilBrown2604b702006-01-06 00:20:36 -0800338 return NULL;
339}
340
Andre Nollb73df2d2008-07-11 22:02:23 +1000341/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700342static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Andre Nollb73df2d2008-07-11 22:02:23 +1000344 sector_t num_sectors = bdev->bd_inode->i_size / 512;
345 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Andre Nolle7debaa2008-07-11 22:02:23 +1000348static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Andre Noll0f420352008-07-11 22:02:23 +1000350 sector_t num_sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000353 num_sectors &= ~((sector_t)chunk_size/512 - 1);
354 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static int alloc_disk_sb(mdk_rdev_t * rdev)
358{
359 if (rdev->sb_page)
360 MD_BUG();
361
362 rdev->sb_page = alloc_page(GFP_KERNEL);
363 if (!rdev->sb_page) {
364 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000365 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
368 return 0;
369}
370
371static void free_disk_sb(mdk_rdev_t * rdev)
372{
373 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800374 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 rdev->sb_loaded = 0;
376 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000377 rdev->sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 rdev->size = 0;
379 }
380}
381
382
NeilBrown6712ecf2007-09-27 12:47:43 +0200383static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700384{
385 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800386 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700387
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700388 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
389 printk("md: super_written gets error=%d, uptodate=%d\n",
390 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
391 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800392 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700393 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700394
NeilBrowna9701a32005-11-08 21:39:34 -0800395 if (atomic_dec_and_test(&mddev->pending_writes))
396 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700397 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700398}
399
NeilBrown6712ecf2007-09-27 12:47:43 +0200400static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800401{
402 struct bio *bio2 = bio->bi_private;
403 mdk_rdev_t *rdev = bio2->bi_private;
404 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800405
406 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
407 error == -EOPNOTSUPP) {
408 unsigned long flags;
409 /* barriers don't appear to be supported :-( */
410 set_bit(BarriersNotsupp, &rdev->flags);
411 mddev->barriers_work = 0;
412 spin_lock_irqsave(&mddev->write_lock, flags);
413 bio2->bi_next = mddev->biolist;
414 mddev->biolist = bio2;
415 spin_unlock_irqrestore(&mddev->write_lock, flags);
416 wake_up(&mddev->sb_wait);
417 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200418 } else {
419 bio_put(bio2);
420 bio->bi_private = rdev;
421 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800422 }
NeilBrowna9701a32005-11-08 21:39:34 -0800423}
424
NeilBrown7bfa19f2005-06-21 17:17:28 -0700425void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
426 sector_t sector, int size, struct page *page)
427{
428 /* write first size bytes of page to sector of rdev
429 * Increment mddev->pending_writes before returning
430 * and decrement it on completion, waking up sb_wait
431 * if zero is reached.
432 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800433 *
434 * As we might need to resubmit the request if BIO_RW_BARRIER
435 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700436 */
437 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800438 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700439
440 bio->bi_bdev = rdev->bdev;
441 bio->bi_sector = sector;
442 bio_add_page(bio, page, size, 0);
443 bio->bi_private = rdev;
444 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800445 bio->bi_rw = rw;
446
NeilBrown7bfa19f2005-06-21 17:17:28 -0700447 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800448 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
449 struct bio *rbio;
450 rw |= (1<<BIO_RW_BARRIER);
451 rbio = bio_clone(bio, GFP_NOIO);
452 rbio->bi_private = bio;
453 rbio->bi_end_io = super_written_barrier;
454 submit_bio(rw, rbio);
455 } else
456 submit_bio(rw, bio);
457}
458
459void md_super_wait(mddev_t *mddev)
460{
461 /* wait for all superblock writes that were scheduled to complete.
462 * if any had to be retried (due to BARRIER problems), retry them
463 */
464 DEFINE_WAIT(wq);
465 for(;;) {
466 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
467 if (atomic_read(&mddev->pending_writes)==0)
468 break;
469 while (mddev->biolist) {
470 struct bio *bio;
471 spin_lock_irq(&mddev->write_lock);
472 bio = mddev->biolist;
473 mddev->biolist = bio->bi_next ;
474 bio->bi_next = NULL;
475 spin_unlock_irq(&mddev->write_lock);
476 submit_bio(bio->bi_rw, bio);
477 }
478 schedule();
479 }
480 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700481}
482
NeilBrown6712ecf2007-09-27 12:47:43 +0200483static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
NeilBrowna654b9d82005-06-21 17:17:27 -0700488int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 struct page *page, int rw)
490{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700491 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 struct completion event;
493 int ret;
494
495 rw |= (1 << BIO_RW_SYNC);
496
497 bio->bi_bdev = bdev;
498 bio->bi_sector = sector;
499 bio_add_page(bio, page, size, 0);
500 init_completion(&event);
501 bio->bi_private = &event;
502 bio->bi_end_io = bi_complete;
503 submit_bio(rw, bio);
504 wait_for_completion(&event);
505
506 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
507 bio_put(bio);
508 return ret;
509}
NeilBrowna8745db2006-01-06 00:20:34 -0800510EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
NeilBrown0002b272005-09-09 16:23:53 -0700512static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 char b[BDEVNAME_SIZE];
515 if (!rdev->sb_page) {
516 MD_BUG();
517 return -EINVAL;
518 }
519 if (rdev->sb_loaded)
520 return 0;
521
522
Andre Noll0f420352008-07-11 22:02:23 +1000523 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 goto fail;
525 rdev->sb_loaded = 1;
526 return 0;
527
528fail:
529 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
530 bdevname(rdev->bdev,b));
531 return -EINVAL;
532}
533
534static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
535{
Andre Noll05710462008-07-11 22:02:20 +1000536 return sb1->set_uuid0 == sb2->set_uuid0 &&
537 sb1->set_uuid1 == sb2->set_uuid1 &&
538 sb1->set_uuid2 == sb2->set_uuid2 &&
539 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
543{
544 int ret;
545 mdp_super_t *tmp1, *tmp2;
546
547 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
548 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
549
550 if (!tmp1 || !tmp2) {
551 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100552 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto abort;
554 }
555
556 *tmp1 = *sb1;
557 *tmp2 = *sb2;
558
559 /*
560 * nr_disks is not constant
561 */
562 tmp1->nr_disks = 0;
563 tmp2->nr_disks = 0;
564
Andre Nollce0c8e02008-07-11 22:02:20 +1000565 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700567 kfree(tmp1);
568 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return ret;
570}
571
NeilBrown4d167f02007-05-09 02:35:37 -0700572
573static u32 md_csum_fold(u32 csum)
574{
575 csum = (csum & 0xffff) + (csum >> 16);
576 return (csum & 0xffff) + (csum >> 16);
577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static unsigned int calc_sb_csum(mdp_super_t * sb)
580{
NeilBrown4d167f02007-05-09 02:35:37 -0700581 u64 newcsum = 0;
582 u32 *sb32 = (u32*)sb;
583 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 unsigned int disk_csum, csum;
585
586 disk_csum = sb->sb_csum;
587 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700588
589 for (i = 0; i < MD_SB_BYTES/4 ; i++)
590 newcsum += sb32[i];
591 csum = (newcsum & 0xffffffff) + (newcsum>>32);
592
593
594#ifdef CONFIG_ALPHA
595 /* This used to use csum_partial, which was wrong for several
596 * reasons including that different results are returned on
597 * different architectures. It isn't critical that we get exactly
598 * the same return value as before (we always csum_fold before
599 * testing, and that removes any differences). However as we
600 * know that csum_partial always returned a 16bit value on
601 * alphas, do a fold to maximise conformity to previous behaviour.
602 */
603 sb->sb_csum = md_csum_fold(disk_csum);
604#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700606#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return csum;
608}
609
610
611/*
612 * Handle superblock details.
613 * We want to be able to handle multiple superblock formats
614 * so we have a common interface to them all, and an array of
615 * different handlers.
616 * We rely on user-space to write the initial superblock, and support
617 * reading and updating of superblocks.
618 * Interface methods are:
619 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
620 * loads and validates a superblock on dev.
621 * if refdev != NULL, compare superblocks on both devices
622 * Return:
623 * 0 - dev has a superblock that is compatible with refdev
624 * 1 - dev has a superblock that is compatible and newer than refdev
625 * so dev should be used as the refdev in future
626 * -EINVAL superblock incompatible or invalid
627 * -othererror e.g. -EIO
628 *
629 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
630 * Verify that dev is acceptable into mddev.
631 * The first time, mddev->raid_disks will be 0, and data from
632 * dev should be merged in. Subsequent calls check that dev
633 * is new enough. Return 0 or -EINVAL
634 *
635 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
636 * Update the superblock for rdev with data in mddev
637 * This does not write to disc.
638 *
639 */
640
641struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000642 char *name;
643 struct module *owner;
644 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
645 int minor_version);
646 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
647 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
648 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000649 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650};
651
652/*
653 * load_super for 0.90.0
654 */
655static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
656{
657 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
658 mdp_super_t *sb;
659 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 /*
Andre Noll0f420352008-07-11 22:02:23 +1000662 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * it's at the end of the disk.
664 *
665 * It also happens to be a multiple of 4Kb.
666 */
Andre Noll0f420352008-07-11 22:02:23 +1000667 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
NeilBrown0002b272005-09-09 16:23:53 -0700669 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (ret) return ret;
671
672 ret = -EINVAL;
673
674 bdevname(rdev->bdev, b);
675 sb = (mdp_super_t*)page_address(rdev->sb_page);
676
677 if (sb->md_magic != MD_SB_MAGIC) {
678 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
679 b);
680 goto abort;
681 }
682
683 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800684 sb->minor_version < 90 ||
685 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
687 sb->major_version, sb->minor_version,
688 b);
689 goto abort;
690 }
691
692 if (sb->raid_disks <= 0)
693 goto abort;
694
NeilBrown4d167f02007-05-09 02:35:37 -0700695 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
697 b);
698 goto abort;
699 }
700
701 rdev->preferred_minor = sb->md_minor;
702 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700703 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
NeilBrowne11e93f2007-05-09 02:35:36 -0700705 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
706 if (sb->level != 1 && sb->level != 4
707 && sb->level != 5 && sb->level != 6
708 && sb->level != 10) {
709 /* FIXME use a better test */
710 printk(KERN_WARNING
711 "md: bitmaps not supported for this level.\n");
712 goto abort;
713 }
714 }
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (sb->level == LEVEL_MULTIPATH)
717 rdev->desc_nr = -1;
718 else
719 rdev->desc_nr = sb->this_disk.number;
720
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700721 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700723 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 __u64 ev1, ev2;
725 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
726 if (!uuid_equal(refsb, sb)) {
727 printk(KERN_WARNING "md: %s has different UUID to %s\n",
728 b, bdevname(refdev->bdev,b2));
729 goto abort;
730 }
731 if (!sb_equal(refsb, sb)) {
732 printk(KERN_WARNING "md: %s has same UUID"
733 " but different superblock to %s\n",
734 b, bdevname(refdev->bdev, b2));
735 goto abort;
736 }
737 ev1 = md_event(sb);
738 ev2 = md_event(refsb);
739 if (ev1 > ev2)
740 ret = 1;
741 else
742 ret = 0;
743 }
Andre Nolle7debaa2008-07-11 22:02:23 +1000744 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
NeilBrown2bf071b2006-01-06 00:20:55 -0800746 if (rdev->size < sb->size && sb->level > 1)
747 /* "this cannot possibly happen" ... */
748 ret = -EINVAL;
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 abort:
751 return ret;
752}
753
754/*
755 * validate_super for 0.90.0
756 */
757static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
758{
759 mdp_disk_t *desc;
760 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700761 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
NeilBrown41158c72005-06-21 17:17:25 -0700763 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800764 clear_bit(Faulty, &rdev->flags);
765 clear_bit(In_sync, &rdev->flags);
766 clear_bit(WriteMostly, &rdev->flags);
767 clear_bit(BarriersNotsupp, &rdev->flags);
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 if (mddev->raid_disks == 0) {
770 mddev->major_version = 0;
771 mddev->minor_version = sb->minor_version;
772 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800773 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 mddev->chunk_size = sb->chunk_size;
775 mddev->ctime = sb->ctime;
776 mddev->utime = sb->utime;
777 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800778 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 mddev->layout = sb->layout;
780 mddev->raid_disks = sb->raid_disks;
781 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700782 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700783 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700784 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
NeilBrownf6705572006-03-27 01:18:11 -0800786 if (mddev->minor_version >= 91) {
787 mddev->reshape_position = sb->reshape_position;
788 mddev->delta_disks = sb->delta_disks;
789 mddev->new_level = sb->new_level;
790 mddev->new_layout = sb->new_layout;
791 mddev->new_chunk = sb->new_chunk;
792 } else {
793 mddev->reshape_position = MaxSector;
794 mddev->delta_disks = 0;
795 mddev->new_level = mddev->level;
796 mddev->new_layout = mddev->layout;
797 mddev->new_chunk = mddev->chunk_size;
798 }
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (sb->state & (1<<MD_SB_CLEAN))
801 mddev->recovery_cp = MaxSector;
802 else {
803 if (sb->events_hi == sb->cp_events_hi &&
804 sb->events_lo == sb->cp_events_lo) {
805 mddev->recovery_cp = sb->recovery_cp;
806 } else
807 mddev->recovery_cp = 0;
808 }
809
810 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
811 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
812 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
813 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
814
815 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700816
817 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700818 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700819 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700820
NeilBrown41158c72005-06-21 17:17:25 -0700821 } else if (mddev->pers == NULL) {
822 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ++ev1;
824 if (ev1 < mddev->events)
825 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700826 } else if (mddev->bitmap) {
827 /* if adding to array with a bitmap, then we can accept an
828 * older device ... but not too old.
829 */
NeilBrown41158c72005-06-21 17:17:25 -0700830 if (ev1 < mddev->bitmap->events_cleared)
831 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700832 } else {
833 if (ev1 < mddev->events)
834 /* just a hot-add of a new device, leave raid_disk at -1 */
835 return 0;
836 }
NeilBrown41158c72005-06-21 17:17:25 -0700837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 desc = sb->disks + rdev->desc_nr;
840
841 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800842 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700843 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
844 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800845 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 rdev->raid_disk = desc->raid_disk;
847 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700848 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
849 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700850 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800851 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return 0;
853}
854
855/*
856 * sync_super for 0.90.0
857 */
858static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
859{
860 mdp_super_t *sb;
861 struct list_head *tmp;
862 mdk_rdev_t *rdev2;
863 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /* make rdev->sb match mddev data..
867 *
868 * 1/ zero out disks
869 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
870 * 3/ any empty disks < next_spare become removed
871 *
872 * disks[0] gets initialised to REMOVED because
873 * we cannot be sure from other fields if it has
874 * been initialised or not.
875 */
876 int i;
877 int active=0, working=0,failed=0,spare=0,nr_disks=0;
878
NeilBrown61181562005-09-09 16:24:02 -0700879 rdev->sb_size = MD_SB_BYTES;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 sb = (mdp_super_t*)page_address(rdev->sb_page);
882
883 memset(sb, 0, sizeof(*sb));
884
885 sb->md_magic = MD_SB_MAGIC;
886 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 sb->patch_version = mddev->patch_version;
888 sb->gvalid_words = 0; /* ignored */
889 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
890 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
891 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
892 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
893
894 sb->ctime = mddev->ctime;
895 sb->level = mddev->level;
896 sb->size = mddev->size;
897 sb->raid_disks = mddev->raid_disks;
898 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800899 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sb->utime = mddev->utime;
901 sb->state = 0;
902 sb->events_hi = (mddev->events>>32);
903 sb->events_lo = (u32)mddev->events;
904
NeilBrownf6705572006-03-27 01:18:11 -0800905 if (mddev->reshape_position == MaxSector)
906 sb->minor_version = 90;
907 else {
908 sb->minor_version = 91;
909 sb->reshape_position = mddev->reshape_position;
910 sb->new_level = mddev->new_level;
911 sb->delta_disks = mddev->delta_disks;
912 sb->new_layout = mddev->new_layout;
913 sb->new_chunk = mddev->new_chunk;
914 }
915 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (mddev->in_sync)
917 {
918 sb->recovery_cp = mddev->recovery_cp;
919 sb->cp_events_hi = (mddev->events>>32);
920 sb->cp_events_lo = (u32)mddev->events;
921 if (mddev->recovery_cp == MaxSector)
922 sb->state = (1<< MD_SB_CLEAN);
923 } else
924 sb->recovery_cp = 0;
925
926 sb->layout = mddev->layout;
927 sb->chunk_size = mddev->chunk_size;
928
NeilBrowna654b9d82005-06-21 17:17:27 -0700929 if (mddev->bitmap && mddev->bitmap_file == NULL)
930 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800933 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800935 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800936 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
937 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800938 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800940 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800941 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 d = &sb->disks[rdev2->desc_nr];
943 nr_disks++;
944 d->number = rdev2->desc_nr;
945 d->major = MAJOR(rdev2->bdev->bd_dev);
946 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800947 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
948 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 d->raid_disk = rdev2->raid_disk;
950 else
951 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800952 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800954 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 d->state = (1<<MD_DISK_ACTIVE);
956 d->state |= (1<<MD_DISK_SYNC);
957 active++;
958 working++;
959 } else {
960 d->state = 0;
961 spare++;
962 working++;
963 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700964 if (test_bit(WriteMostly, &rdev2->flags))
965 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* now set the "removed" and "faulty" bits on any missing devices */
968 for (i=0 ; i < mddev->raid_disks ; i++) {
969 mdp_disk_t *d = &sb->disks[i];
970 if (d->state == 0 && d->number == 0) {
971 d->number = i;
972 d->raid_disk = i;
973 d->state = (1<<MD_DISK_REMOVED);
974 d->state |= (1<<MD_DISK_FAULTY);
975 failed++;
976 }
977 }
978 sb->nr_disks = nr_disks;
979 sb->active_disks = active;
980 sb->working_disks = working;
981 sb->failed_disks = failed;
982 sb->spare_disks = spare;
983
984 sb->this_disk = sb->disks[rdev->desc_nr];
985 sb->sb_csum = calc_sb_csum(sb);
986}
987
988/*
Chris Webb0cd17fe2008-06-28 08:31:46 +1000989 * rdev_size_change for 0.90.0
990 */
991static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +1000992super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +1000993{
Andre Noll15f4a5f2008-07-21 14:42:12 +1000994 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +1000995 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +1000996 if (rdev->mddev->bitmap_offset)
997 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +1000998 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +1000999 if (!num_sectors || num_sectors > rdev->sb_start)
1000 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001001 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001002 rdev->sb_page);
1003 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001004 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001005}
1006
1007
1008/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 * version 1 superblock
1010 */
1011
NeilBrown1c05b4b2006-10-21 10:24:08 -07001012static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001014 __le32 disk_csum;
1015 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 unsigned long long newcsum;
1017 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001018 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 int i;
1020
1021 disk_csum = sb->sb_csum;
1022 sb->sb_csum = 0;
1023 newcsum = 0;
1024 for (i=0; size>=4; size -= 4 )
1025 newcsum += le32_to_cpu(*isuper++);
1026
1027 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001028 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1031 sb->sb_csum = disk_csum;
1032 return cpu_to_le32(csum);
1033}
1034
1035static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1036{
1037 struct mdp_superblock_1 *sb;
1038 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001039 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001041 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /*
Andre Noll0f420352008-07-11 22:02:23 +10001044 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * It is always aligned to a 4K boundary and
1046 * depeding on minor_version, it can be:
1047 * 0: At least 8K, but less than 12K, from end of device
1048 * 1: At start of device
1049 * 2: 4K from start of device.
1050 */
1051 switch(minor_version) {
1052 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001053 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1054 sb_start -= 8*2;
1055 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 break;
1057 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001058 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 break;
1060 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001061 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 break;
1063 default:
1064 return -EINVAL;
1065 }
Andre Noll0f420352008-07-11 22:02:23 +10001066 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
NeilBrown0002b272005-09-09 16:23:53 -07001068 /* superblock is rarely larger than 1K, but it can be larger,
1069 * and it is safe to read 4k, so we do that
1070 */
1071 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (ret) return ret;
1073
1074
1075 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1076
1077 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1078 sb->major_version != cpu_to_le32(1) ||
1079 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001080 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001081 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -EINVAL;
1083
1084 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1085 printk("md: invalid superblock checksum on %s\n",
1086 bdevname(rdev->bdev,b));
1087 return -EINVAL;
1088 }
1089 if (le64_to_cpu(sb->data_size) < 10) {
1090 printk("md: data_size too small on %s\n",
1091 bdevname(rdev->bdev,b));
1092 return -EINVAL;
1093 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001094 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1095 if (sb->level != cpu_to_le32(1) &&
1096 sb->level != cpu_to_le32(4) &&
1097 sb->level != cpu_to_le32(5) &&
1098 sb->level != cpu_to_le32(6) &&
1099 sb->level != cpu_to_le32(10)) {
1100 printk(KERN_WARNING
1101 "md: bitmaps not supported for this level.\n");
1102 return -EINVAL;
1103 }
1104 }
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 rdev->preferred_minor = 0xffff;
1107 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001108 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
NeilBrown0002b272005-09-09 16:23:53 -07001110 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001111 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001112 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001113 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1114
1115 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001116 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001117 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001118
NeilBrown31b65a02006-07-10 04:44:14 -07001119 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1120 rdev->desc_nr = -1;
1121 else
1122 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1123
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001124 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001125 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001126 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 __u64 ev1, ev2;
1128 struct mdp_superblock_1 *refsb =
1129 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1130
1131 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1132 sb->level != refsb->level ||
1133 sb->layout != refsb->layout ||
1134 sb->chunksize != refsb->chunksize) {
1135 printk(KERN_WARNING "md: %s has strangely different"
1136 " superblock to %s\n",
1137 bdevname(rdev->bdev,b),
1138 bdevname(refdev->bdev,b2));
1139 return -EINVAL;
1140 }
1141 ev1 = le64_to_cpu(sb->events);
1142 ev2 = le64_to_cpu(refsb->events);
1143
1144 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001145 ret = 1;
1146 else
1147 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
NeilBrowna1801f82008-03-04 14:29:31 -08001149 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1151 else
Andre Noll0f420352008-07-11 22:02:23 +10001152 rdev->size = rdev->sb_start / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1154 return -EINVAL;
1155 rdev->size = le64_to_cpu(sb->data_size)/2;
1156 if (le32_to_cpu(sb->chunksize))
1157 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001158
NeilBrown1c05b4b2006-10-21 10:24:08 -07001159 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001160 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001161 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1165{
1166 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001167 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
NeilBrown41158c72005-06-21 17:17:25 -07001169 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001170 clear_bit(Faulty, &rdev->flags);
1171 clear_bit(In_sync, &rdev->flags);
1172 clear_bit(WriteMostly, &rdev->flags);
1173 clear_bit(BarriersNotsupp, &rdev->flags);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (mddev->raid_disks == 0) {
1176 mddev->major_version = 1;
1177 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001178 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1180 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1181 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1182 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001183 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 mddev->layout = le32_to_cpu(sb->layout);
1185 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1186 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001187 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001188 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001189 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1192 memcpy(mddev->uuid, sb->set_uuid, 16);
1193
1194 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001195
NeilBrown71c08052005-09-09 16:23:51 -07001196 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001197 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001198 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001199
NeilBrownf6705572006-03-27 01:18:11 -08001200 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1201 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1202 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1203 mddev->new_level = le32_to_cpu(sb->new_level);
1204 mddev->new_layout = le32_to_cpu(sb->new_layout);
1205 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1206 } else {
1207 mddev->reshape_position = MaxSector;
1208 mddev->delta_disks = 0;
1209 mddev->new_level = mddev->level;
1210 mddev->new_layout = mddev->layout;
1211 mddev->new_chunk = mddev->chunk_size;
1212 }
1213
NeilBrown41158c72005-06-21 17:17:25 -07001214 } else if (mddev->pers == NULL) {
1215 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ++ev1;
1217 if (ev1 < mddev->events)
1218 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001219 } else if (mddev->bitmap) {
1220 /* If adding to array with a bitmap, then we can accept an
1221 * older device, but not too old.
1222 */
NeilBrown41158c72005-06-21 17:17:25 -07001223 if (ev1 < mddev->bitmap->events_cleared)
1224 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001225 } else {
1226 if (ev1 < mddev->events)
1227 /* just a hot-add of a new device, leave raid_disk at -1 */
1228 return 0;
1229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (mddev->level != LEVEL_MULTIPATH) {
1231 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1233 switch(role) {
1234 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 break;
1236 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001237 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 break;
1239 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001240 if ((le32_to_cpu(sb->feature_map) &
1241 MD_FEATURE_RECOVERY_OFFSET))
1242 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1243 else
1244 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 rdev->raid_disk = role;
1246 break;
1247 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001248 if (sb->devflags & WriteMostly1)
1249 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001250 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001251 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return 0;
1254}
1255
1256static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1257{
1258 struct mdp_superblock_1 *sb;
1259 struct list_head *tmp;
1260 mdk_rdev_t *rdev2;
1261 int max_dev, i;
1262 /* make rdev->sb match mddev and rdev data. */
1263
1264 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1265
1266 sb->feature_map = 0;
1267 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001268 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 memset(sb->pad1, 0, sizeof(sb->pad1));
1270 memset(sb->pad2, 0, sizeof(sb->pad2));
1271 memset(sb->pad3, 0, sizeof(sb->pad3));
1272
1273 sb->utime = cpu_to_le64((__u64)mddev->utime);
1274 sb->events = cpu_to_le64(mddev->events);
1275 if (mddev->in_sync)
1276 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1277 else
1278 sb->resync_offset = cpu_to_le64(0);
1279
NeilBrown1c05b4b2006-10-21 10:24:08 -07001280 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001281
NeilBrownf0ca3402006-02-02 14:28:04 -08001282 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001283 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001284
NeilBrowna654b9d82005-06-21 17:17:27 -07001285 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1286 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001287 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001288 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001289
1290 if (rdev->raid_disk >= 0 &&
1291 !test_bit(In_sync, &rdev->flags) &&
1292 rdev->recovery_offset > 0) {
1293 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1294 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1295 }
1296
NeilBrownf6705572006-03-27 01:18:11 -08001297 if (mddev->reshape_position != MaxSector) {
1298 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1299 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1300 sb->new_layout = cpu_to_le32(mddev->new_layout);
1301 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1302 sb->new_level = cpu_to_le32(mddev->new_level);
1303 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1304 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001307 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (rdev2->desc_nr+1 > max_dev)
1309 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001310
1311 if (max_dev > le32_to_cpu(sb->max_dev))
1312 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 for (i=0; i<max_dev;i++)
1314 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1315
NeilBrownd089c6a2008-02-06 01:39:59 -08001316 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001318 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001320 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001322 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1323 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 else
1325 sb->dev_roles[i] = cpu_to_le16(0xffff);
1326 }
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 sb->sb_csum = calc_sb_1_csum(sb);
1329}
1330
Chris Webb0cd17fe2008-06-28 08:31:46 +10001331static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001332super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001333{
1334 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001335 sector_t max_sectors;
1336 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001337 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001338 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001339 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001340 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1341 max_sectors -= rdev->data_offset;
1342 if (!num_sectors || num_sectors > max_sectors)
1343 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001344 } else if (rdev->mddev->bitmap_offset) {
1345 /* minor version 0 with bitmap we can't move */
1346 return 0;
1347 } else {
1348 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001349 sector_t sb_start;
1350 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1351 sb_start &= ~(sector_t)(4*2 - 1);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001352 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1353 if (!num_sectors || num_sectors > max_sectors)
1354 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001355 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001356 }
1357 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001358 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001359 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001360 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001361 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001362 rdev->sb_page);
1363 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001364 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001365}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Adrian Bunk75c96f82005-05-05 16:16:09 -07001367static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 [0] = {
1369 .name = "0.90.0",
1370 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001371 .load_super = super_90_load,
1372 .validate_super = super_90_validate,
1373 .sync_super = super_90_sync,
1374 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 },
1376 [1] = {
1377 .name = "md-1",
1378 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001379 .load_super = super_1_load,
1380 .validate_super = super_1_validate,
1381 .sync_super = super_1_sync,
1382 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 },
1384};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1387{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001388 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
NeilBrown4b809912008-07-21 17:05:25 +10001390 rcu_read_lock();
1391 rdev_for_each_rcu(rdev, mddev1)
1392 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001393 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001394 rdev2->bdev->bd_contains) {
1395 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001396 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001397 }
1398 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return 0;
1400}
1401
1402static LIST_HEAD(pending_raid_disks);
1403
1404static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1405{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001406 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001407 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001408 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001409 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 if (rdev->mddev) {
1412 MD_BUG();
1413 return -EINVAL;
1414 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001415
1416 /* prevent duplicates */
1417 if (find_rdev(mddev, rdev->bdev->bd_dev))
1418 return -EEXIST;
1419
NeilBrown2bf071b2006-01-06 00:20:55 -08001420 /* make sure rdev->size exceeds mddev->size */
1421 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001422 if (mddev->pers) {
1423 /* Cannot change size, so fail
1424 * If mddev->level <= 0, then we don't care
1425 * about aligning sizes (e.g. linear)
1426 */
1427 if (mddev->level > 0)
1428 return -ENOSPC;
1429 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001430 mddev->size = rdev->size;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 /* Verify rdev->desc_nr is unique.
1434 * If it is -1, assign a free number, else
1435 * check number is not in use
1436 */
1437 if (rdev->desc_nr < 0) {
1438 int choice = 0;
1439 if (mddev->pers) choice = mddev->raid_disks;
1440 while (find_rdev_nr(mddev, choice))
1441 choice++;
1442 rdev->desc_nr = choice;
1443 } else {
1444 if (find_rdev_nr(mddev, rdev->desc_nr))
1445 return -EBUSY;
1446 }
NeilBrown19133a42005-11-08 21:39:35 -08001447 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001448 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001449 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001452 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001453
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001454 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001455 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001456
Tejun Heo0762b8b2008-08-25 19:56:12 +09001457 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001458 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1459 kobject_del(&rdev->kobj);
1460 goto fail;
1461 }
NeilBrown4b809912008-07-21 17:05:25 +10001462 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001463 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001465
1466 fail:
1467 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1468 b, mdname(mddev));
1469 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
NeilBrown177a99b2008-02-06 01:39:56 -08001472static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001473{
1474 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1475 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001476 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001477}
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1480{
1481 char b[BDEVNAME_SIZE];
1482 if (!rdev->mddev) {
1483 MD_BUG();
1484 return;
1485 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001486 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001487 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1489 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001490 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001491
1492 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001493 * writing to 'remove' to "dev/state". We also need
1494 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001495 */
NeilBrown4b809912008-07-21 17:05:25 +10001496 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001497 INIT_WORK(&rdev->del_work, md_delayed_delete);
1498 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001499 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
1502/*
1503 * prevent the device from being mounted, repartitioned or
1504 * otherwise reused by a RAID array (or any other kernel
1505 * subsystem), by bd_claiming the device.
1506 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001507static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 int err = 0;
1510 struct block_device *bdev;
1511 char b[BDEVNAME_SIZE];
1512
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001513 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 if (IS_ERR(bdev)) {
1515 printk(KERN_ERR "md: could not open %s.\n",
1516 __bdevname(dev, b));
1517 return PTR_ERR(bdev);
1518 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001519 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 if (err) {
1521 printk(KERN_ERR "md: could not bd_claim %s.\n",
1522 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001523 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return err;
1525 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001526 if (!shared)
1527 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 rdev->bdev = bdev;
1529 return err;
1530}
1531
1532static void unlock_rdev(mdk_rdev_t *rdev)
1533{
1534 struct block_device *bdev = rdev->bdev;
1535 rdev->bdev = NULL;
1536 if (!bdev)
1537 MD_BUG();
1538 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001539 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
1542void md_autodetect_dev(dev_t dev);
1543
1544static void export_rdev(mdk_rdev_t * rdev)
1545{
1546 char b[BDEVNAME_SIZE];
1547 printk(KERN_INFO "md: export_rdev(%s)\n",
1548 bdevname(rdev->bdev,b));
1549 if (rdev->mddev)
1550 MD_BUG();
1551 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001553 if (test_bit(AutoDetected, &rdev->flags))
1554 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555#endif
1556 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001557 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558}
1559
1560static void kick_rdev_from_array(mdk_rdev_t * rdev)
1561{
1562 unbind_rdev_from_array(rdev);
1563 export_rdev(rdev);
1564}
1565
1566static void export_array(mddev_t *mddev)
1567{
1568 struct list_head *tmp;
1569 mdk_rdev_t *rdev;
1570
NeilBrownd089c6a2008-02-06 01:39:59 -08001571 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (!rdev->mddev) {
1573 MD_BUG();
1574 continue;
1575 }
1576 kick_rdev_from_array(rdev);
1577 }
1578 if (!list_empty(&mddev->disks))
1579 MD_BUG();
1580 mddev->raid_disks = 0;
1581 mddev->major_version = 0;
1582}
1583
1584static void print_desc(mdp_disk_t *desc)
1585{
1586 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1587 desc->major,desc->minor,desc->raid_disk,desc->state);
1588}
1589
1590static void print_sb(mdp_super_t *sb)
1591{
1592 int i;
1593
1594 printk(KERN_INFO
1595 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1596 sb->major_version, sb->minor_version, sb->patch_version,
1597 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1598 sb->ctime);
1599 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1600 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1601 sb->md_minor, sb->layout, sb->chunk_size);
1602 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1603 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1604 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1605 sb->failed_disks, sb->spare_disks,
1606 sb->sb_csum, (unsigned long)sb->events_lo);
1607
1608 printk(KERN_INFO);
1609 for (i = 0; i < MD_SB_DISKS; i++) {
1610 mdp_disk_t *desc;
1611
1612 desc = sb->disks + i;
1613 if (desc->number || desc->major || desc->minor ||
1614 desc->raid_disk || (desc->state && (desc->state != 4))) {
1615 printk(" D %2d: ", i);
1616 print_desc(desc);
1617 }
1618 }
1619 printk(KERN_INFO "md: THIS: ");
1620 print_desc(&sb->this_disk);
1621
1622}
1623
1624static void print_rdev(mdk_rdev_t *rdev)
1625{
1626 char b[BDEVNAME_SIZE];
1627 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1628 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001629 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1630 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 if (rdev->sb_loaded) {
1632 printk(KERN_INFO "md: rdev superblock:\n");
1633 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1634 } else
1635 printk(KERN_INFO "md: no rdev superblock!\n");
1636}
1637
Adrian Bunk5e563412006-06-26 00:27:42 -07001638static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
1640 struct list_head *tmp, *tmp2;
1641 mdk_rdev_t *rdev;
1642 mddev_t *mddev;
1643 char b[BDEVNAME_SIZE];
1644
1645 printk("\n");
1646 printk("md: **********************************\n");
1647 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1648 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001649 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
NeilBrown32a76272005-06-21 17:17:14 -07001651 if (mddev->bitmap)
1652 bitmap_print_sb(mddev->bitmap);
1653 else
1654 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001655 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 printk("<%s>", bdevname(rdev->bdev,b));
1657 printk("\n");
1658
NeilBrownd089c6a2008-02-06 01:39:59 -08001659 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 print_rdev(rdev);
1661 }
1662 printk("md: **********************************\n");
1663 printk("\n");
1664}
1665
1666
NeilBrown42543762006-06-26 00:27:57 -07001667static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
NeilBrown42543762006-06-26 00:27:57 -07001669 /* Update each superblock (in-memory image), but
1670 * if we are allowed to, skip spares which already
1671 * have the right event counter, or have one earlier
1672 * (which would mean they aren't being marked as dirty
1673 * with the rest of the array)
1674 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 mdk_rdev_t *rdev;
1676 struct list_head *tmp;
1677
NeilBrownd089c6a2008-02-06 01:39:59 -08001678 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001679 if (rdev->sb_events == mddev->events ||
1680 (nospares &&
1681 rdev->raid_disk < 0 &&
1682 (rdev->sb_events&1)==0 &&
1683 rdev->sb_events+1 == mddev->events)) {
1684 /* Don't update this superblock */
1685 rdev->sb_loaded = 2;
1686 } else {
1687 super_types[mddev->major_version].
1688 sync_super(mddev, rdev);
1689 rdev->sb_loaded = 1;
1690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692}
1693
NeilBrown850b2b42006-10-03 01:15:46 -07001694static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 struct list_head *tmp;
1697 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001698 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001699 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
NeilBrown8377bc82008-04-30 00:52:28 -07001701 if (mddev->external)
1702 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001704 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001705
NeilBrown850b2b42006-10-03 01:15:46 -07001706 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1707 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1708 force_change = 1;
1709 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1710 /* just a clean<-> dirty transition, possibly leave spares alone,
1711 * though if events isn't the right even/odd, we will have to do
1712 * spares after all
1713 */
1714 nospares = 1;
1715 if (force_change)
1716 nospares = 0;
1717 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001718 /* If the array is degraded, then skipping spares is both
1719 * dangerous and fairly pointless.
1720 * Dangerous because a device that was removed from the array
1721 * might have a event_count that still looks up-to-date,
1722 * so it can be re-added without a resync.
1723 * Pointless because if there are any spares to skip,
1724 * then a recovery will happen and soon that array won't
1725 * be degraded any more and the spare can go back to sleep then.
1726 */
NeilBrown850b2b42006-10-03 01:15:46 -07001727 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001728
NeilBrown06d91a52005-06-21 17:17:12 -07001729 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001731
1732 /* If this is just a dirty<->clean transition, and the array is clean
1733 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001734 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001735 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001736 && (mddev->events & 1)
1737 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001738 mddev->events--;
1739 else {
1740 /* otherwise we have to go forward and ... */
1741 mddev->events ++;
1742 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1743 /* .. if the array isn't clean, insist on an odd 'events' */
1744 if ((mddev->events&1)==0) {
1745 mddev->events++;
1746 nospares = 0;
1747 }
1748 } else {
1749 /* otherwise insist on an even 'events' (for clean states) */
1750 if ((mddev->events&1)) {
1751 mddev->events++;
1752 nospares = 0;
1753 }
1754 }
1755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (!mddev->events) {
1758 /*
1759 * oops, this 64-bit counter should never wrap.
1760 * Either we are in around ~1 trillion A.C., assuming
1761 * 1 reboot per second, or we have a bug:
1762 */
1763 MD_BUG();
1764 mddev->events --;
1765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 /*
1768 * do not write anything to disk if using
1769 * nonpersistent superblocks
1770 */
NeilBrown06d91a52005-06-21 17:17:12 -07001771 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001772 if (!mddev->external)
1773 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1774
NeilBrowna9701a32005-11-08 21:39:34 -08001775 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001776 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001778 }
NeilBrowne6910632008-02-06 01:39:51 -08001779 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001780 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 dprintk(KERN_INFO
1783 "md: updating %s RAID superblock on device (in sync %d)\n",
1784 mdname(mddev),mddev->in_sync);
1785
NeilBrown4ad13662007-07-17 04:06:13 -07001786 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001787 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 char b[BDEVNAME_SIZE];
1789 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001790 if (rdev->sb_loaded != 1)
1791 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001792 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 dprintk("(skipping faulty ");
1794
1795 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001796 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001797 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001798 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001799 rdev->sb_page);
1800 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1801 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10001802 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07001803 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 } else
1806 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001807 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 /* only need to write one superblock... */
1809 break;
1810 }
NeilBrowna9701a32005-11-08 21:39:34 -08001811 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001812 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001813
NeilBrowna9701a32005-11-08 21:39:34 -08001814 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001815 if (mddev->in_sync != sync_req ||
1816 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001817 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001818 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001819 goto repeat;
1820 }
NeilBrown850b2b42006-10-03 01:15:46 -07001821 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001822 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001823 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Andre Noll7f6ce762008-03-23 18:34:54 +01001827/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08001828 * We want to accept with case. For this we use cmd_match.
1829 */
1830static int cmd_match(const char *cmd, const char *str)
1831{
1832 /* See if cmd, written into a sysfs file, matches
1833 * str. They must either be the same, or cmd can
1834 * have a trailing newline
1835 */
1836 while (*cmd && *str && *cmd == *str) {
1837 cmd++;
1838 str++;
1839 }
1840 if (*cmd == '\n')
1841 cmd++;
1842 if (*str || *cmd)
1843 return 0;
1844 return 1;
1845}
1846
NeilBrown86e6ffd2005-11-08 21:39:24 -08001847struct rdev_sysfs_entry {
1848 struct attribute attr;
1849 ssize_t (*show)(mdk_rdev_t *, char *);
1850 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1851};
1852
1853static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001854state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001855{
1856 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001857 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001858
NeilBrownb2d444d2005-11-08 21:39:31 -08001859 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001860 len+= sprintf(page+len, "%sfaulty",sep);
1861 sep = ",";
1862 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001863 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001864 len += sprintf(page+len, "%sin_sync",sep);
1865 sep = ",";
1866 }
NeilBrownf6556752006-06-26 00:28:01 -07001867 if (test_bit(WriteMostly, &rdev->flags)) {
1868 len += sprintf(page+len, "%swrite_mostly",sep);
1869 sep = ",";
1870 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001871 if (test_bit(Blocked, &rdev->flags)) {
1872 len += sprintf(page+len, "%sblocked", sep);
1873 sep = ",";
1874 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001875 if (!test_bit(Faulty, &rdev->flags) &&
1876 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001877 len += sprintf(page+len, "%sspare", sep);
1878 sep = ",";
1879 }
1880 return len+sprintf(page+len, "\n");
1881}
1882
NeilBrown45dc2de2006-06-26 00:27:58 -07001883static ssize_t
1884state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1885{
1886 /* can write
1887 * faulty - simulates and error
1888 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001889 * writemostly - sets write_mostly
1890 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001891 * blocked - sets the Blocked flag
1892 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001893 */
1894 int err = -EINVAL;
1895 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1896 md_error(rdev->mddev, rdev);
1897 err = 0;
1898 } else if (cmd_match(buf, "remove")) {
1899 if (rdev->raid_disk >= 0)
1900 err = -EBUSY;
1901 else {
1902 mddev_t *mddev = rdev->mddev;
1903 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001904 if (mddev->pers)
1905 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001906 md_new_event(mddev);
1907 err = 0;
1908 }
NeilBrownf6556752006-06-26 00:28:01 -07001909 } else if (cmd_match(buf, "writemostly")) {
1910 set_bit(WriteMostly, &rdev->flags);
1911 err = 0;
1912 } else if (cmd_match(buf, "-writemostly")) {
1913 clear_bit(WriteMostly, &rdev->flags);
1914 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001915 } else if (cmd_match(buf, "blocked")) {
1916 set_bit(Blocked, &rdev->flags);
1917 err = 0;
1918 } else if (cmd_match(buf, "-blocked")) {
1919 clear_bit(Blocked, &rdev->flags);
1920 wake_up(&rdev->blocked_wait);
1921 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1922 md_wakeup_thread(rdev->mddev->thread);
1923
1924 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001925 }
Neil Brown52664732008-06-28 08:31:44 +10001926 if (!err)
1927 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrown45dc2de2006-06-26 00:27:58 -07001928 return err ? err : len;
1929}
NeilBrown80ca3a42006-07-10 04:44:18 -07001930static struct rdev_sysfs_entry rdev_state =
1931__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001932
1933static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001934errors_show(mdk_rdev_t *rdev, char *page)
1935{
1936 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1937}
1938
1939static ssize_t
1940errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1941{
1942 char *e;
1943 unsigned long n = simple_strtoul(buf, &e, 10);
1944 if (*buf && (*e == 0 || *e == '\n')) {
1945 atomic_set(&rdev->corrected_errors, n);
1946 return len;
1947 }
1948 return -EINVAL;
1949}
1950static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001951__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001952
NeilBrown014236d2006-01-06 00:20:55 -08001953static ssize_t
1954slot_show(mdk_rdev_t *rdev, char *page)
1955{
1956 if (rdev->raid_disk < 0)
1957 return sprintf(page, "none\n");
1958 else
1959 return sprintf(page, "%d\n", rdev->raid_disk);
1960}
1961
1962static ssize_t
1963slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1964{
1965 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001966 int err;
1967 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001968 int slot = simple_strtoul(buf, &e, 10);
1969 if (strncmp(buf, "none", 4)==0)
1970 slot = -1;
1971 else if (e==buf || (*e && *e!= '\n'))
1972 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10001973 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001974 /* Setting 'slot' on an active array requires also
1975 * updating the 'rd%d' link, and communicating
1976 * with the personality with ->hot_*_disk.
1977 * For now we only support removing
1978 * failed/spare devices. This normally happens automatically,
1979 * but not when the metadata is externally managed.
1980 */
NeilBrownc303da62008-02-06 01:39:51 -08001981 if (rdev->raid_disk == -1)
1982 return -EEXIST;
1983 /* personality does all needed checks */
1984 if (rdev->mddev->pers->hot_add_disk == NULL)
1985 return -EINVAL;
1986 err = rdev->mddev->pers->
1987 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1988 if (err)
1989 return err;
1990 sprintf(nm, "rd%d", rdev->raid_disk);
1991 sysfs_remove_link(&rdev->mddev->kobj, nm);
1992 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1993 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10001994 } else if (rdev->mddev->pers) {
1995 mdk_rdev_t *rdev2;
1996 struct list_head *tmp;
1997 /* Activating a spare .. or possibly reactivating
1998 * if we every get bitmaps working here.
1999 */
2000
2001 if (rdev->raid_disk != -1)
2002 return -EBUSY;
2003
2004 if (rdev->mddev->pers->hot_add_disk == NULL)
2005 return -EINVAL;
2006
2007 rdev_for_each(rdev2, tmp, rdev->mddev)
2008 if (rdev2->raid_disk == slot)
2009 return -EEXIST;
2010
2011 rdev->raid_disk = slot;
2012 if (test_bit(In_sync, &rdev->flags))
2013 rdev->saved_raid_disk = slot;
2014 else
2015 rdev->saved_raid_disk = -1;
2016 err = rdev->mddev->pers->
2017 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002018 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002019 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002020 return err;
Neil Brown52664732008-06-28 08:31:44 +10002021 } else
2022 sysfs_notify(&rdev->kobj, NULL, "state");
Neil Brown6c2fce22008-06-28 08:31:31 +10002023 sprintf(nm, "rd%d", rdev->raid_disk);
2024 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2025 printk(KERN_WARNING
2026 "md: cannot register "
2027 "%s for %s\n",
2028 nm, mdname(rdev->mddev));
2029
2030 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002031 } else {
2032 if (slot >= rdev->mddev->raid_disks)
2033 return -ENOSPC;
2034 rdev->raid_disk = slot;
2035 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002036 clear_bit(Faulty, &rdev->flags);
2037 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002038 set_bit(In_sync, &rdev->flags);
Neil Brown52664732008-06-28 08:31:44 +10002039 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc303da62008-02-06 01:39:51 -08002040 }
NeilBrown014236d2006-01-06 00:20:55 -08002041 return len;
2042}
2043
2044
2045static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002046__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002047
NeilBrown93c8cad2006-01-06 00:20:56 -08002048static ssize_t
2049offset_show(mdk_rdev_t *rdev, char *page)
2050{
Andrew Morton6961ece2006-01-06 00:20:59 -08002051 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002052}
2053
2054static ssize_t
2055offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2056{
2057 char *e;
2058 unsigned long long offset = simple_strtoull(buf, &e, 10);
2059 if (e==buf || (*e && *e != '\n'))
2060 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002061 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002062 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002063 if (rdev->size && rdev->mddev->external)
2064 /* Must set offset before size, so overlap checks
2065 * can be sane */
2066 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002067 rdev->data_offset = offset;
2068 return len;
2069}
2070
2071static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002072__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002073
NeilBrown83303b62006-01-06 00:21:06 -08002074static ssize_t
2075rdev_size_show(mdk_rdev_t *rdev, char *page)
2076{
2077 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2078}
2079
NeilBrownc5d79ad2008-02-06 01:39:54 -08002080static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2081{
2082 /* check if two start/length pairs overlap */
2083 if (s1+l1 <= s2)
2084 return 0;
2085 if (s2+l2 <= s1)
2086 return 0;
2087 return 1;
2088}
2089
NeilBrown83303b62006-01-06 00:21:06 -08002090static ssize_t
2091rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2092{
Neil Brownd7027452008-07-12 10:37:50 +10002093 unsigned long long size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002094 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002095 mddev_t *my_mddev = rdev->mddev;
2096
Neil Brownd7027452008-07-12 10:37:50 +10002097 if (strict_strtoull(buf, 10, &size) < 0)
2098 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002099 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002100 if (my_mddev->persistent) {
2101 size = super_types[my_mddev->major_version].
Andre Noll15f4a5f2008-07-21 14:42:12 +10002102 rdev_size_change(rdev, size * 2);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002103 if (!size)
2104 return -EBUSY;
2105 } else if (!size) {
2106 size = (rdev->bdev->bd_inode->i_size >> 10);
2107 size -= rdev->data_offset/2;
2108 }
Chris Webb0cd17fe2008-06-28 08:31:46 +10002109 }
Chris Webb7d3c6f82008-10-13 11:55:11 +11002110 if (size < my_mddev->size)
2111 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002112
NeilBrown83303b62006-01-06 00:21:06 -08002113 rdev->size = size;
Neil Brownd7027452008-07-12 10:37:50 +10002114 if (size > oldsize && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002115 /* need to check that all other rdevs with the same ->bdev
2116 * do not overlap. We need to unlock the mddev to avoid
2117 * a deadlock. We have already changed rdev->size, and if
2118 * we have to change it back, we will have the lock again.
2119 */
2120 mddev_t *mddev;
2121 int overlap = 0;
2122 struct list_head *tmp, *tmp2;
2123
NeilBrown27c529b2008-03-04 14:29:33 -08002124 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002125 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002126 mdk_rdev_t *rdev2;
2127
2128 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002129 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002130 if (test_bit(AllReserved, &rdev2->flags) ||
2131 (rdev->bdev == rdev2->bdev &&
2132 rdev != rdev2 &&
Andre Nolld07bd3b2008-07-21 14:42:07 +10002133 overlaps(rdev->data_offset, rdev->size * 2,
2134 rdev2->data_offset,
2135 rdev2->size * 2))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002136 overlap = 1;
2137 break;
2138 }
2139 mddev_unlock(mddev);
2140 if (overlap) {
2141 mddev_put(mddev);
2142 break;
2143 }
2144 }
NeilBrown27c529b2008-03-04 14:29:33 -08002145 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002146 if (overlap) {
2147 /* Someone else could have slipped in a size
2148 * change here, but doing so is just silly.
2149 * We put oldsize back because we *know* it is
2150 * safe, and trust userspace not to race with
2151 * itself
2152 */
2153 rdev->size = oldsize;
2154 return -EBUSY;
2155 }
2156 }
NeilBrown83303b62006-01-06 00:21:06 -08002157 return len;
2158}
2159
2160static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002161__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002162
NeilBrown86e6ffd2005-11-08 21:39:24 -08002163static struct attribute *rdev_default_attrs[] = {
2164 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002165 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002166 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002167 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002168 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002169 NULL,
2170};
2171static ssize_t
2172rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2173{
2174 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2175 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002176 mddev_t *mddev = rdev->mddev;
2177 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002178
2179 if (!entry->show)
2180 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002181
2182 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2183 if (!rv) {
2184 if (rdev->mddev == NULL)
2185 rv = -EBUSY;
2186 else
2187 rv = entry->show(rdev, page);
2188 mddev_unlock(mddev);
2189 }
2190 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002191}
2192
2193static ssize_t
2194rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2195 const char *page, size_t length)
2196{
2197 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2198 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002199 ssize_t rv;
2200 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002201
2202 if (!entry->store)
2203 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002204 if (!capable(CAP_SYS_ADMIN))
2205 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002206 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002207 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002208 if (rdev->mddev == NULL)
2209 rv = -EBUSY;
2210 else
2211 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002212 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002213 }
2214 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002215}
2216
2217static void rdev_free(struct kobject *ko)
2218{
2219 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2220 kfree(rdev);
2221}
2222static struct sysfs_ops rdev_sysfs_ops = {
2223 .show = rdev_attr_show,
2224 .store = rdev_attr_store,
2225};
2226static struct kobj_type rdev_ktype = {
2227 .release = rdev_free,
2228 .sysfs_ops = &rdev_sysfs_ops,
2229 .default_attrs = rdev_default_attrs,
2230};
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232/*
2233 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2234 *
2235 * mark the device faulty if:
2236 *
2237 * - the device is nonexistent (zero size)
2238 * - the device has no valid superblock
2239 *
2240 * a faulty rdev _never_ has rdev->sb set.
2241 */
2242static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2243{
2244 char b[BDEVNAME_SIZE];
2245 int err;
2246 mdk_rdev_t *rdev;
2247 sector_t size;
2248
NeilBrown9ffae0c2006-01-06 00:20:32 -08002249 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (!rdev) {
2251 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2252 return ERR_PTR(-ENOMEM);
2253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 if ((err = alloc_disk_sb(rdev)))
2256 goto abort_free;
2257
NeilBrownc5d79ad2008-02-06 01:39:54 -08002258 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (err)
2260 goto abort_free;
2261
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002262 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002265 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002266 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002267 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002269 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002271 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002272 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2275 if (!size) {
2276 printk(KERN_WARNING
2277 "md: %s has zero or unknown size, marking faulty!\n",
2278 bdevname(rdev->bdev,b));
2279 err = -EINVAL;
2280 goto abort_free;
2281 }
2282
2283 if (super_format >= 0) {
2284 err = super_types[super_format].
2285 load_super(rdev, NULL, super_minor);
2286 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002287 printk(KERN_WARNING
2288 "md: %s does not have a valid v%d.%d "
2289 "superblock, not importing!\n",
2290 bdevname(rdev->bdev,b),
2291 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto abort_free;
2293 }
2294 if (err < 0) {
2295 printk(KERN_WARNING
2296 "md: could not read %s's sb, not importing!\n",
2297 bdevname(rdev->bdev,b));
2298 goto abort_free;
2299 }
2300 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002303 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 return rdev;
2306
2307abort_free:
2308 if (rdev->sb_page) {
2309 if (rdev->bdev)
2310 unlock_rdev(rdev);
2311 free_disk_sb(rdev);
2312 }
2313 kfree(rdev);
2314 return ERR_PTR(err);
2315}
2316
2317/*
2318 * Check a full RAID array for plausibility
2319 */
2320
2321
NeilBrowna757e642005-04-16 15:26:42 -07002322static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
2324 int i;
2325 struct list_head *tmp;
2326 mdk_rdev_t *rdev, *freshest;
2327 char b[BDEVNAME_SIZE];
2328
2329 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002330 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 switch (super_types[mddev->major_version].
2332 load_super(rdev, freshest, mddev->minor_version)) {
2333 case 1:
2334 freshest = rdev;
2335 break;
2336 case 0:
2337 break;
2338 default:
2339 printk( KERN_ERR \
2340 "md: fatal superblock inconsistency in %s"
2341 " -- removing from array\n",
2342 bdevname(rdev->bdev,b));
2343 kick_rdev_from_array(rdev);
2344 }
2345
2346
2347 super_types[mddev->major_version].
2348 validate_super(mddev, freshest);
2349
2350 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002351 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (rdev != freshest)
2353 if (super_types[mddev->major_version].
2354 validate_super(mddev, rdev)) {
2355 printk(KERN_WARNING "md: kicking non-fresh %s"
2356 " from array!\n",
2357 bdevname(rdev->bdev,b));
2358 kick_rdev_from_array(rdev);
2359 continue;
2360 }
2361 if (mddev->level == LEVEL_MULTIPATH) {
2362 rdev->desc_nr = i++;
2363 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002364 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002365 } else if (rdev->raid_disk >= mddev->raid_disks) {
2366 rdev->raid_disk = -1;
2367 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 }
2369 }
2370
2371
2372
2373 if (mddev->recovery_cp != MaxSector &&
2374 mddev->level >= 1)
2375 printk(KERN_ERR "md: %s: raid array is not clean"
2376 " -- starting background reconstruction\n",
2377 mdname(mddev));
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
NeilBrown19052c02008-08-05 15:54:13 +10002381static void md_safemode_timeout(unsigned long data);
2382
NeilBrowneae17012005-11-08 21:39:23 -08002383static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002384safe_delay_show(mddev_t *mddev, char *page)
2385{
2386 int msec = (mddev->safemode_delay*1000)/HZ;
2387 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2388}
2389static ssize_t
2390safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2391{
2392 int scale=1;
2393 int dot=0;
2394 int i;
2395 unsigned long msec;
2396 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002397
NeilBrown16f17b32006-06-26 00:27:37 -07002398 /* remove a period, and count digits after it */
2399 if (len >= sizeof(buf))
2400 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002401 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002402 for (i=0; i<len; i++) {
2403 if (dot) {
2404 if (isdigit(buf[i])) {
2405 buf[i-1] = buf[i];
2406 scale *= 10;
2407 }
2408 buf[i] = 0;
2409 } else if (buf[i] == '.') {
2410 dot=1;
2411 buf[i] = 0;
2412 }
2413 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002414 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002415 return -EINVAL;
2416 msec = (msec * 1000) / scale;
2417 if (msec == 0)
2418 mddev->safemode_delay = 0;
2419 else {
NeilBrown19052c02008-08-05 15:54:13 +10002420 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002421 mddev->safemode_delay = (msec*HZ)/1000;
2422 if (mddev->safemode_delay == 0)
2423 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002424 if (mddev->safemode_delay < old_delay)
2425 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002426 }
2427 return len;
2428}
2429static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002430__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002431
2432static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002433level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002434{
NeilBrown2604b702006-01-06 00:20:36 -08002435 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002436 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002437 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002438 else if (mddev->clevel[0])
2439 return sprintf(page, "%s\n", mddev->clevel);
2440 else if (mddev->level != LEVEL_NONE)
2441 return sprintf(page, "%d\n", mddev->level);
2442 else
2443 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002444}
2445
NeilBrownd9d166c2006-01-06 00:20:51 -08002446static ssize_t
2447level_store(mddev_t *mddev, const char *buf, size_t len)
2448{
NeilBrown20a49ff2008-02-06 01:39:57 -08002449 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002450 if (mddev->pers)
2451 return -EBUSY;
2452 if (len == 0)
2453 return 0;
2454 if (len >= sizeof(mddev->clevel))
2455 return -ENOSPC;
2456 strncpy(mddev->clevel, buf, len);
2457 if (mddev->clevel[len-1] == '\n')
2458 len--;
2459 mddev->clevel[len] = 0;
2460 mddev->level = LEVEL_NONE;
2461 return rv;
2462}
2463
2464static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002465__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002466
NeilBrownd4dbd022006-06-26 00:27:59 -07002467
2468static ssize_t
2469layout_show(mddev_t *mddev, char *page)
2470{
2471 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002472 if (mddev->reshape_position != MaxSector &&
2473 mddev->layout != mddev->new_layout)
2474 return sprintf(page, "%d (%d)\n",
2475 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002476 return sprintf(page, "%d\n", mddev->layout);
2477}
2478
2479static ssize_t
2480layout_store(mddev_t *mddev, const char *buf, size_t len)
2481{
2482 char *e;
2483 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002484
2485 if (!*buf || (*e && *e != '\n'))
2486 return -EINVAL;
2487
NeilBrown08a02ec2007-05-09 02:35:38 -07002488 if (mddev->pers)
2489 return -EBUSY;
2490 if (mddev->reshape_position != MaxSector)
2491 mddev->new_layout = n;
2492 else
2493 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002494 return len;
2495}
2496static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002497__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002498
2499
NeilBrowneae17012005-11-08 21:39:23 -08002500static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002501raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002502{
NeilBrownbb636542005-11-08 21:39:45 -08002503 if (mddev->raid_disks == 0)
2504 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002505 if (mddev->reshape_position != MaxSector &&
2506 mddev->delta_disks != 0)
2507 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2508 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002509 return sprintf(page, "%d\n", mddev->raid_disks);
2510}
2511
NeilBrownda943b992006-01-06 00:20:54 -08002512static int update_raid_disks(mddev_t *mddev, int raid_disks);
2513
2514static ssize_t
2515raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2516{
NeilBrownda943b992006-01-06 00:20:54 -08002517 char *e;
2518 int rv = 0;
2519 unsigned long n = simple_strtoul(buf, &e, 10);
2520
2521 if (!*buf || (*e && *e != '\n'))
2522 return -EINVAL;
2523
2524 if (mddev->pers)
2525 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002526 else if (mddev->reshape_position != MaxSector) {
2527 int olddisks = mddev->raid_disks - mddev->delta_disks;
2528 mddev->delta_disks = n - olddisks;
2529 mddev->raid_disks = n;
2530 } else
NeilBrownda943b992006-01-06 00:20:54 -08002531 mddev->raid_disks = n;
2532 return rv ? rv : len;
2533}
2534static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002535__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002536
NeilBrown24dd4692005-11-08 21:39:26 -08002537static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002538chunk_size_show(mddev_t *mddev, char *page)
2539{
NeilBrown08a02ec2007-05-09 02:35:38 -07002540 if (mddev->reshape_position != MaxSector &&
2541 mddev->chunk_size != mddev->new_chunk)
2542 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2543 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002544 return sprintf(page, "%d\n", mddev->chunk_size);
2545}
2546
2547static ssize_t
2548chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2549{
2550 /* can only set chunk_size if array is not yet active */
2551 char *e;
2552 unsigned long n = simple_strtoul(buf, &e, 10);
2553
NeilBrown3b343802006-01-06 00:20:47 -08002554 if (!*buf || (*e && *e != '\n'))
2555 return -EINVAL;
2556
NeilBrown08a02ec2007-05-09 02:35:38 -07002557 if (mddev->pers)
2558 return -EBUSY;
2559 else if (mddev->reshape_position != MaxSector)
2560 mddev->new_chunk = n;
2561 else
2562 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002563 return len;
2564}
2565static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002566__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002567
NeilBrowna94213b2006-06-26 00:28:00 -07002568static ssize_t
2569resync_start_show(mddev_t *mddev, char *page)
2570{
2571 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2572}
2573
2574static ssize_t
2575resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2576{
NeilBrowna94213b2006-06-26 00:28:00 -07002577 char *e;
2578 unsigned long long n = simple_strtoull(buf, &e, 10);
2579
2580 if (mddev->pers)
2581 return -EBUSY;
2582 if (!*buf || (*e && *e != '\n'))
2583 return -EINVAL;
2584
2585 mddev->recovery_cp = n;
2586 return len;
2587}
2588static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002589__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002590
NeilBrown9e653b62006-06-26 00:27:58 -07002591/*
2592 * The array state can be:
2593 *
2594 * clear
2595 * No devices, no size, no level
2596 * Equivalent to STOP_ARRAY ioctl
2597 * inactive
2598 * May have some settings, but array is not active
2599 * all IO results in error
2600 * When written, doesn't tear down array, but just stops it
2601 * suspended (not supported yet)
2602 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002603 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002604 * readonly
2605 * no resync can happen. no superblocks get written.
2606 * write requests fail
2607 * read-auto
2608 * like readonly, but behaves like 'clean' on a write request.
2609 *
2610 * clean - no pending writes, but otherwise active.
2611 * When written to inactive array, starts without resync
2612 * If a write request arrives then
2613 * if metadata is known, mark 'dirty' and switch to 'active'.
2614 * if not known, block and switch to write-pending
2615 * If written to an active array that has pending writes, then fails.
2616 * active
2617 * fully active: IO and resync can be happening.
2618 * When written to inactive array, starts with resync
2619 *
2620 * write-pending
2621 * clean, but writes are blocked waiting for 'active' to be written.
2622 *
2623 * active-idle
2624 * like active, but no writes have been seen for a while (100msec).
2625 *
2626 */
2627enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2628 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002629static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002630 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2631 "write-pending", "active-idle", NULL };
2632
2633static int match_word(const char *word, char **list)
2634{
2635 int n;
2636 for (n=0; list[n]; n++)
2637 if (cmd_match(word, list[n]))
2638 break;
2639 return n;
2640}
2641
2642static ssize_t
2643array_state_show(mddev_t *mddev, char *page)
2644{
2645 enum array_state st = inactive;
2646
2647 if (mddev->pers)
2648 switch(mddev->ro) {
2649 case 1:
2650 st = readonly;
2651 break;
2652 case 2:
2653 st = read_auto;
2654 break;
2655 case 0:
2656 if (mddev->in_sync)
2657 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002658 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2659 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002660 else if (mddev->safemode)
2661 st = active_idle;
2662 else
2663 st = active;
2664 }
2665 else {
2666 if (list_empty(&mddev->disks) &&
2667 mddev->raid_disks == 0 &&
2668 mddev->size == 0)
2669 st = clear;
2670 else
2671 st = inactive;
2672 }
2673 return sprintf(page, "%s\n", array_states[st]);
2674}
2675
Neil Browndf5b20c2008-07-11 22:02:22 +10002676static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002677static int do_md_run(mddev_t * mddev);
2678static int restart_array(mddev_t *mddev);
2679
2680static ssize_t
2681array_state_store(mddev_t *mddev, const char *buf, size_t len)
2682{
2683 int err = -EINVAL;
2684 enum array_state st = match_word(buf, array_states);
2685 switch(st) {
2686 case bad_word:
2687 break;
2688 case clear:
2689 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10002690 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08002691 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002692 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002693 break;
2694 case inactive:
2695 /* stopping an active array */
2696 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10002697 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002698 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002699 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08002700 } else
2701 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002702 break;
2703 case suspended:
2704 break; /* not supported yet */
2705 case readonly:
2706 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10002707 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002708 else {
2709 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002710 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002711 err = do_md_run(mddev);
2712 }
2713 break;
2714 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002715 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11002716 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10002717 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11002718 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07002719 err = restart_array(mddev);
2720 if (err == 0) {
2721 mddev->ro = 2;
2722 set_disk_ro(mddev->gendisk, 0);
2723 }
NeilBrown9e653b62006-06-26 00:27:58 -07002724 } else {
2725 mddev->ro = 2;
2726 err = do_md_run(mddev);
2727 }
2728 break;
2729 case clean:
2730 if (mddev->pers) {
2731 restart_array(mddev);
2732 spin_lock_irq(&mddev->write_lock);
2733 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002734 if (mddev->in_sync == 0) {
2735 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002736 if (mddev->safemode == 1)
2737 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002738 if (mddev->persistent)
2739 set_bit(MD_CHANGE_CLEAN,
2740 &mddev->flags);
2741 }
2742 err = 0;
2743 } else
2744 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002745 spin_unlock_irq(&mddev->write_lock);
2746 } else {
2747 mddev->ro = 0;
2748 mddev->recovery_cp = MaxSector;
2749 err = do_md_run(mddev);
2750 }
2751 break;
2752 case active:
2753 if (mddev->pers) {
2754 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002755 if (mddev->external)
2756 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002757 wake_up(&mddev->sb_wait);
2758 err = 0;
2759 } else {
2760 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002761 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002762 err = do_md_run(mddev);
2763 }
2764 break;
2765 case write_pending:
2766 case active_idle:
2767 /* these cannot be set */
2768 break;
2769 }
2770 if (err)
2771 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002772 else {
2773 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown9e653b62006-06-26 00:27:58 -07002774 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002775 }
NeilBrown9e653b62006-06-26 00:27:58 -07002776}
NeilBrown80ca3a42006-07-10 04:44:18 -07002777static struct md_sysfs_entry md_array_state =
2778__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002779
NeilBrown6d7ff732006-01-06 00:21:16 -08002780static ssize_t
2781null_show(mddev_t *mddev, char *page)
2782{
2783 return -EINVAL;
2784}
2785
2786static ssize_t
2787new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2788{
2789 /* buf must be %d:%d\n? giving major and minor numbers */
2790 /* The new device is added to the array.
2791 * If the array has a persistent superblock, we read the
2792 * superblock to initialise info and check validity.
2793 * Otherwise, only checking done is that in bind_rdev_to_array,
2794 * which mainly checks size.
2795 */
2796 char *e;
2797 int major = simple_strtoul(buf, &e, 10);
2798 int minor;
2799 dev_t dev;
2800 mdk_rdev_t *rdev;
2801 int err;
2802
2803 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2804 return -EINVAL;
2805 minor = simple_strtoul(e+1, &e, 10);
2806 if (*e && *e != '\n')
2807 return -EINVAL;
2808 dev = MKDEV(major, minor);
2809 if (major != MAJOR(dev) ||
2810 minor != MINOR(dev))
2811 return -EOVERFLOW;
2812
2813
2814 if (mddev->persistent) {
2815 rdev = md_import_device(dev, mddev->major_version,
2816 mddev->minor_version);
2817 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2818 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2819 mdk_rdev_t, same_set);
2820 err = super_types[mddev->major_version]
2821 .load_super(rdev, rdev0, mddev->minor_version);
2822 if (err < 0)
2823 goto out;
2824 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002825 } else if (mddev->external)
2826 rdev = md_import_device(dev, -2, -1);
2827 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002828 rdev = md_import_device(dev, -1, -1);
2829
2830 if (IS_ERR(rdev))
2831 return PTR_ERR(rdev);
2832 err = bind_rdev_to_array(rdev, mddev);
2833 out:
2834 if (err)
2835 export_rdev(rdev);
2836 return err ? err : len;
2837}
2838
2839static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002840__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002841
2842static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002843bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2844{
2845 char *end;
2846 unsigned long chunk, end_chunk;
2847
2848 if (!mddev->bitmap)
2849 goto out;
2850 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2851 while (*buf) {
2852 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2853 if (buf == end) break;
2854 if (*end == '-') { /* range */
2855 buf = end + 1;
2856 end_chunk = simple_strtoul(buf, &end, 0);
2857 if (buf == end) break;
2858 }
2859 if (*end && !isspace(*end)) break;
2860 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2861 buf = end;
2862 while (isspace(*buf)) buf++;
2863 }
2864 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2865out:
2866 return len;
2867}
2868
2869static struct md_sysfs_entry md_bitmap =
2870__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2871
2872static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002873size_show(mddev_t *mddev, char *page)
2874{
2875 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2876}
2877
Andre Nolld71f9f82008-07-11 22:02:22 +10002878static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08002879
2880static ssize_t
2881size_store(mddev_t *mddev, const char *buf, size_t len)
2882{
2883 /* If array is inactive, we can reduce the component size, but
2884 * not increase it (except from 0).
2885 * If array is active, we can try an on-line resize
2886 */
2887 char *e;
2888 int err = 0;
2889 unsigned long long size = simple_strtoull(buf, &e, 10);
2890 if (!*buf || *buf == '\n' ||
2891 (*e && *e != '\n'))
2892 return -EINVAL;
2893
2894 if (mddev->pers) {
Andre Nolld71f9f82008-07-11 22:02:22 +10002895 err = update_size(mddev, size * 2);
NeilBrown850b2b42006-10-03 01:15:46 -07002896 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002897 } else {
2898 if (mddev->size == 0 ||
2899 mddev->size > size)
2900 mddev->size = size;
2901 else
2902 err = -ENOSPC;
2903 }
2904 return err ? err : len;
2905}
2906
2907static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002908__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002909
NeilBrown8bb93aa2006-01-06 00:20:50 -08002910
2911/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002912 * This is one of
2913 * 'none' for arrays with no metadata (good luck...)
2914 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002915 * or N.M for internally known formats
2916 */
2917static ssize_t
2918metadata_show(mddev_t *mddev, char *page)
2919{
2920 if (mddev->persistent)
2921 return sprintf(page, "%d.%d\n",
2922 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002923 else if (mddev->external)
2924 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002925 else
2926 return sprintf(page, "none\n");
2927}
2928
2929static ssize_t
2930metadata_store(mddev_t *mddev, const char *buf, size_t len)
2931{
2932 int major, minor;
2933 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11002934 /* Changing the details of 'external' metadata is
2935 * always permitted. Otherwise there must be
2936 * no devices attached to the array.
2937 */
2938 if (mddev->external && strncmp(buf, "external:", 9) == 0)
2939 ;
2940 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08002941 return -EBUSY;
2942
2943 if (cmd_match(buf, "none")) {
2944 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002945 mddev->external = 0;
2946 mddev->major_version = 0;
2947 mddev->minor_version = 90;
2948 return len;
2949 }
2950 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002951 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002952 if (namelen >= sizeof(mddev->metadata_type))
2953 namelen = sizeof(mddev->metadata_type)-1;
2954 strncpy(mddev->metadata_type, buf+9, namelen);
2955 mddev->metadata_type[namelen] = 0;
2956 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2957 mddev->metadata_type[--namelen] = 0;
2958 mddev->persistent = 0;
2959 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002960 mddev->major_version = 0;
2961 mddev->minor_version = 90;
2962 return len;
2963 }
2964 major = simple_strtoul(buf, &e, 10);
2965 if (e==buf || *e != '.')
2966 return -EINVAL;
2967 buf = e+1;
2968 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002969 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002970 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002971 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002972 return -ENOENT;
2973 mddev->major_version = major;
2974 mddev->minor_version = minor;
2975 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002976 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002977 return len;
2978}
2979
2980static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002981__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002982
NeilBrowna35b0d62006-01-06 00:20:49 -08002983static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002984action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002985{
NeilBrown7eec3142005-11-08 21:39:44 -08002986 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002987 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002988 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002989 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2990 type = "reshape";
2991 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002992 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2993 type = "resync";
2994 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2995 type = "check";
2996 else
2997 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10002998 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08002999 type = "recover";
3000 }
3001 return sprintf(page, "%s\n", type);
3002}
3003
3004static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003005action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003006{
NeilBrown7eec3142005-11-08 21:39:44 -08003007 if (!mddev->pers || !mddev->pers->sync_request)
3008 return -EINVAL;
3009
NeilBrownbce74da2006-01-06 00:20:41 -08003010 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003011 if (mddev->sync_thread) {
3012 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3013 md_unregister_thread(mddev->sync_thread);
3014 mddev->sync_thread = NULL;
3015 mddev->recovery = 0;
3016 }
NeilBrown03c902e2006-01-06 00:20:46 -08003017 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3018 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003019 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003020 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003021 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003022 else if (cmd_match(page, "recover")) {
3023 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3024 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3025 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003026 int err;
3027 if (mddev->pers->start_reshape == NULL)
3028 return -EINVAL;
3029 err = mddev->pers->start_reshape(mddev);
3030 if (err)
3031 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003032 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003033 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003034 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003035 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003036 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003037 return -EINVAL;
3038 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3039 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003040 }
NeilBrown03c902e2006-01-06 00:20:46 -08003041 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003042 md_wakeup_thread(mddev->thread);
Neil Brown72a23c22008-06-28 08:31:41 +10003043 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrown24dd4692005-11-08 21:39:26 -08003044 return len;
3045}
3046
NeilBrown9d888832005-11-08 21:39:26 -08003047static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003048mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003049{
3050 return sprintf(page, "%llu\n",
3051 (unsigned long long) mddev->resync_mismatches);
3052}
3053
NeilBrown80ca3a42006-07-10 04:44:18 -07003054static struct md_sysfs_entry md_scan_mode =
3055__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003056
NeilBrown96de1e62005-11-08 21:39:39 -08003057
NeilBrown80ca3a42006-07-10 04:44:18 -07003058static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003059
NeilBrown88202a02006-01-06 00:21:36 -08003060static ssize_t
3061sync_min_show(mddev_t *mddev, char *page)
3062{
3063 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3064 mddev->sync_speed_min ? "local": "system");
3065}
3066
3067static ssize_t
3068sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3069{
3070 int min;
3071 char *e;
3072 if (strncmp(buf, "system", 6)==0) {
3073 mddev->sync_speed_min = 0;
3074 return len;
3075 }
3076 min = simple_strtoul(buf, &e, 10);
3077 if (buf == e || (*e && *e != '\n') || min <= 0)
3078 return -EINVAL;
3079 mddev->sync_speed_min = min;
3080 return len;
3081}
3082
3083static struct md_sysfs_entry md_sync_min =
3084__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3085
3086static ssize_t
3087sync_max_show(mddev_t *mddev, char *page)
3088{
3089 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3090 mddev->sync_speed_max ? "local": "system");
3091}
3092
3093static ssize_t
3094sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3095{
3096 int max;
3097 char *e;
3098 if (strncmp(buf, "system", 6)==0) {
3099 mddev->sync_speed_max = 0;
3100 return len;
3101 }
3102 max = simple_strtoul(buf, &e, 10);
3103 if (buf == e || (*e && *e != '\n') || max <= 0)
3104 return -EINVAL;
3105 mddev->sync_speed_max = max;
3106 return len;
3107}
3108
3109static struct md_sysfs_entry md_sync_max =
3110__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3111
Iustin Popd7f3d292007-10-16 23:30:54 -07003112static ssize_t
3113degraded_show(mddev_t *mddev, char *page)
3114{
3115 return sprintf(page, "%d\n", mddev->degraded);
3116}
3117static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003118
3119static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003120sync_force_parallel_show(mddev_t *mddev, char *page)
3121{
3122 return sprintf(page, "%d\n", mddev->parallel_resync);
3123}
3124
3125static ssize_t
3126sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3127{
3128 long n;
3129
3130 if (strict_strtol(buf, 10, &n))
3131 return -EINVAL;
3132
3133 if (n != 0 && n != 1)
3134 return -EINVAL;
3135
3136 mddev->parallel_resync = n;
3137
3138 if (mddev->sync_thread)
3139 wake_up(&resync_wait);
3140
3141 return len;
3142}
3143
3144/* force parallel resync, even with shared block devices */
3145static struct md_sysfs_entry md_sync_force_parallel =
3146__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3147 sync_force_parallel_show, sync_force_parallel_store);
3148
3149static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003150sync_speed_show(mddev_t *mddev, char *page)
3151{
3152 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003153 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3154 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003155 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003156 db = resync - mddev->resync_mark_cnt;
3157 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003158}
3159
NeilBrown80ca3a42006-07-10 04:44:18 -07003160static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003161
3162static ssize_t
3163sync_completed_show(mddev_t *mddev, char *page)
3164{
3165 unsigned long max_blocks, resync;
3166
3167 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3168 max_blocks = mddev->resync_max_sectors;
3169 else
3170 max_blocks = mddev->size << 1;
3171
3172 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3173 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3174}
3175
NeilBrown80ca3a42006-07-10 04:44:18 -07003176static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003177
NeilBrowne464eaf2006-03-27 01:18:14 -08003178static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003179min_sync_show(mddev_t *mddev, char *page)
3180{
3181 return sprintf(page, "%llu\n",
3182 (unsigned long long)mddev->resync_min);
3183}
3184static ssize_t
3185min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3186{
3187 unsigned long long min;
3188 if (strict_strtoull(buf, 10, &min))
3189 return -EINVAL;
3190 if (min > mddev->resync_max)
3191 return -EINVAL;
3192 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3193 return -EBUSY;
3194
3195 /* Must be a multiple of chunk_size */
3196 if (mddev->chunk_size) {
3197 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3198 return -EINVAL;
3199 }
3200 mddev->resync_min = min;
3201
3202 return len;
3203}
3204
3205static struct md_sysfs_entry md_min_sync =
3206__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3207
3208static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003209max_sync_show(mddev_t *mddev, char *page)
3210{
3211 if (mddev->resync_max == MaxSector)
3212 return sprintf(page, "max\n");
3213 else
3214 return sprintf(page, "%llu\n",
3215 (unsigned long long)mddev->resync_max);
3216}
3217static ssize_t
3218max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3219{
3220 if (strncmp(buf, "max", 3) == 0)
3221 mddev->resync_max = MaxSector;
3222 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003223 unsigned long long max;
3224 if (strict_strtoull(buf, 10, &max))
3225 return -EINVAL;
3226 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003227 return -EINVAL;
3228 if (max < mddev->resync_max &&
3229 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3230 return -EBUSY;
3231
3232 /* Must be a multiple of chunk_size */
3233 if (mddev->chunk_size) {
3234 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3235 return -EINVAL;
3236 }
3237 mddev->resync_max = max;
3238 }
3239 wake_up(&mddev->recovery_wait);
3240 return len;
3241}
3242
3243static struct md_sysfs_entry md_max_sync =
3244__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3245
3246static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003247suspend_lo_show(mddev_t *mddev, char *page)
3248{
3249 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3250}
3251
3252static ssize_t
3253suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3254{
3255 char *e;
3256 unsigned long long new = simple_strtoull(buf, &e, 10);
3257
3258 if (mddev->pers->quiesce == NULL)
3259 return -EINVAL;
3260 if (buf == e || (*e && *e != '\n'))
3261 return -EINVAL;
3262 if (new >= mddev->suspend_hi ||
3263 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3264 mddev->suspend_lo = new;
3265 mddev->pers->quiesce(mddev, 2);
3266 return len;
3267 } else
3268 return -EINVAL;
3269}
3270static struct md_sysfs_entry md_suspend_lo =
3271__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3272
3273
3274static ssize_t
3275suspend_hi_show(mddev_t *mddev, char *page)
3276{
3277 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3278}
3279
3280static ssize_t
3281suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3282{
3283 char *e;
3284 unsigned long long new = simple_strtoull(buf, &e, 10);
3285
3286 if (mddev->pers->quiesce == NULL)
3287 return -EINVAL;
3288 if (buf == e || (*e && *e != '\n'))
3289 return -EINVAL;
3290 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3291 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3292 mddev->suspend_hi = new;
3293 mddev->pers->quiesce(mddev, 1);
3294 mddev->pers->quiesce(mddev, 0);
3295 return len;
3296 } else
3297 return -EINVAL;
3298}
3299static struct md_sysfs_entry md_suspend_hi =
3300__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3301
NeilBrown08a02ec2007-05-09 02:35:38 -07003302static ssize_t
3303reshape_position_show(mddev_t *mddev, char *page)
3304{
3305 if (mddev->reshape_position != MaxSector)
3306 return sprintf(page, "%llu\n",
3307 (unsigned long long)mddev->reshape_position);
3308 strcpy(page, "none\n");
3309 return 5;
3310}
3311
3312static ssize_t
3313reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3314{
3315 char *e;
3316 unsigned long long new = simple_strtoull(buf, &e, 10);
3317 if (mddev->pers)
3318 return -EBUSY;
3319 if (buf == e || (*e && *e != '\n'))
3320 return -EINVAL;
3321 mddev->reshape_position = new;
3322 mddev->delta_disks = 0;
3323 mddev->new_level = mddev->level;
3324 mddev->new_layout = mddev->layout;
3325 mddev->new_chunk = mddev->chunk_size;
3326 return len;
3327}
3328
3329static struct md_sysfs_entry md_reshape_position =
3330__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3331 reshape_position_store);
3332
NeilBrowne464eaf2006-03-27 01:18:14 -08003333
NeilBrowneae17012005-11-08 21:39:23 -08003334static struct attribute *md_default_attrs[] = {
3335 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003336 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003337 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003338 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003339 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003340 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003341 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003342 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003343 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003344 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003345 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003346 NULL,
3347};
3348
3349static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003350 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003351 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003352 &md_sync_min.attr,
3353 &md_sync_max.attr,
3354 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003355 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003356 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003357 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003358 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003359 &md_suspend_lo.attr,
3360 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003361 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003362 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003363 NULL,
3364};
NeilBrown411036f2005-11-08 21:39:40 -08003365static struct attribute_group md_redundancy_group = {
3366 .name = NULL,
3367 .attrs = md_redundancy_attrs,
3368};
3369
NeilBrowneae17012005-11-08 21:39:23 -08003370
3371static ssize_t
3372md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3373{
3374 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3375 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003376 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003377
3378 if (!entry->show)
3379 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003380 rv = mddev_lock(mddev);
3381 if (!rv) {
3382 rv = entry->show(mddev, page);
3383 mddev_unlock(mddev);
3384 }
NeilBrown96de1e62005-11-08 21:39:39 -08003385 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003386}
3387
3388static ssize_t
3389md_attr_store(struct kobject *kobj, struct attribute *attr,
3390 const char *page, size_t length)
3391{
3392 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3393 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003394 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003395
3396 if (!entry->store)
3397 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003398 if (!capable(CAP_SYS_ADMIN))
3399 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003400 rv = mddev_lock(mddev);
3401 if (!rv) {
3402 rv = entry->store(mddev, page, length);
3403 mddev_unlock(mddev);
3404 }
NeilBrown96de1e62005-11-08 21:39:39 -08003405 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003406}
3407
3408static void md_free(struct kobject *ko)
3409{
3410 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3411 kfree(mddev);
3412}
3413
3414static struct sysfs_ops md_sysfs_ops = {
3415 .show = md_attr_show,
3416 .store = md_attr_store,
3417};
3418static struct kobj_type md_ktype = {
3419 .release = md_free,
3420 .sysfs_ops = &md_sysfs_ops,
3421 .default_attrs = md_default_attrs,
3422};
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424int mdp_major = 0;
3425
3426static struct kobject *md_probe(dev_t dev, int *part, void *data)
3427{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003428 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 mddev_t *mddev = mddev_find(dev);
3430 struct gendisk *disk;
3431 int partitioned = (MAJOR(dev) != MD_MAJOR);
3432 int shift = partitioned ? MdpMinorShift : 0;
3433 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003434 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
3436 if (!mddev)
3437 return NULL;
3438
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003439 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003441 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 mddev_put(mddev);
3443 return NULL;
3444 }
3445 disk = alloc_disk(1 << shift);
3446 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003447 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 mddev_put(mddev);
3449 return NULL;
3450 }
3451 disk->major = MAJOR(dev);
3452 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003453 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003455 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 disk->fops = &md_fops;
3458 disk->private_data = mddev;
3459 disk->queue = mddev->queue;
3460 add_disk(disk);
3461 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003462 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3463 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003464 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003465 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003466 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3467 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003468 else
3469 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 return NULL;
3471}
3472
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473static void md_safemode_timeout(unsigned long data)
3474{
3475 mddev_t *mddev = (mddev_t *) data;
3476
Neil Brown0fd62b82008-06-28 08:31:36 +10003477 if (!atomic_read(&mddev->writes_pending)) {
3478 mddev->safemode = 1;
3479 if (mddev->external)
Dan Williamsd8e64402008-07-23 13:09:48 -07003480 set_bit(MD_NOTIFY_ARRAY_STATE, &mddev->flags);
Neil Brown0fd62b82008-06-28 08:31:36 +10003481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 md_wakeup_thread(mddev->thread);
3483}
3484
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003485static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486
3487static int do_md_run(mddev_t * mddev)
3488{
NeilBrown2604b702006-01-06 00:20:36 -08003489 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 int chunk_size;
3491 struct list_head *tmp;
3492 mdk_rdev_t *rdev;
3493 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003494 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 char b[BDEVNAME_SIZE];
3496
NeilBrowna757e642005-04-16 15:26:42 -07003497 if (list_empty(&mddev->disks))
3498 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
3501 if (mddev->pers)
3502 return -EBUSY;
3503
3504 /*
3505 * Analyze all RAID superblock(s)
3506 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003507 if (!mddev->raid_disks) {
3508 if (!mddev->persistent)
3509 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003510 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
NeilBrown2604b702006-01-06 00:20:36 -08003515 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 if (chunk_size > MAX_CHUNK_SIZE) {
3517 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3518 chunk_size, MAX_CHUNK_SIZE);
3519 return -EINVAL;
3520 }
3521 /*
NeilBrown4bbf3772008-10-13 11:55:12 +11003522 * chunk-size has to be a power of 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 */
3524 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003525 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 return -EINVAL;
3527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
3529 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003530 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003531 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 continue;
3533 if (rdev->size < chunk_size / 1024) {
3534 printk(KERN_WARNING
3535 "md: Dev %s smaller than chunk_size:"
3536 " %lluk < %dk\n",
3537 bdevname(rdev->bdev,b),
3538 (unsigned long long)rdev->size,
3539 chunk_size / 1024);
3540 return -EINVAL;
3541 }
3542 }
3543 }
3544
NeilBrownd9d166c2006-01-06 00:20:51 -08003545 if (mddev->level != LEVEL_NONE)
3546 request_module("md-level-%d", mddev->level);
3547 else if (mddev->clevel[0])
3548 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549
3550 /*
3551 * Drop all container device buffers, from now on
3552 * the only valid external interface is through the md
3553 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003555 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003556 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 continue;
3558 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003559 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003560
3561 /* perform some consistency tests on the device.
3562 * We don't want the data to overlap the metadata,
3563 * Internal Bitmap issues has handled elsewhere.
3564 */
Andre Noll0f420352008-07-11 22:02:23 +10003565 if (rdev->data_offset < rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003566 if (mddev->size &&
3567 rdev->data_offset + mddev->size*2
Andre Noll0f420352008-07-11 22:02:23 +10003568 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003569 printk("md: %s: data overlaps metadata\n",
3570 mdname(mddev));
3571 return -EINVAL;
3572 }
3573 } else {
Andre Noll0f420352008-07-11 22:02:23 +10003574 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07003575 > rdev->data_offset) {
3576 printk("md: %s: metadata overlaps data\n",
3577 mdname(mddev));
3578 return -EINVAL;
3579 }
3580 }
Neil Brown52664732008-06-28 08:31:44 +10003581 sysfs_notify(&rdev->kobj, NULL, "state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
3583
3584 md_probe(mddev->unit, NULL, NULL);
3585 disk = mddev->gendisk;
3586 if (!disk)
3587 return -ENOMEM;
3588
3589 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003590 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003591 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003593 if (mddev->level != LEVEL_NONE)
3594 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3595 mddev->level);
3596 else
3597 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3598 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 return -EINVAL;
3600 }
NeilBrown2604b702006-01-06 00:20:36 -08003601 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003603 mddev->level = pers->level;
3604 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605
NeilBrownf6705572006-03-27 01:18:11 -08003606 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003607 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003608 /* This personality cannot handle reshaping... */
3609 mddev->pers = NULL;
3610 module_put(pers->owner);
3611 return -EINVAL;
3612 }
3613
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003614 if (pers->sync_request) {
3615 /* Warn if this is a potentially silly
3616 * configuration.
3617 */
3618 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3619 mdk_rdev_t *rdev2;
3620 struct list_head *tmp2;
3621 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003622 rdev_for_each(rdev, tmp, mddev) {
3623 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003624 if (rdev < rdev2 &&
3625 rdev->bdev->bd_contains ==
3626 rdev2->bdev->bd_contains) {
3627 printk(KERN_WARNING
3628 "%s: WARNING: %s appears to be"
3629 " on the same physical disk as"
3630 " %s.\n",
3631 mdname(mddev),
3632 bdevname(rdev->bdev,b),
3633 bdevname(rdev2->bdev,b2));
3634 warned = 1;
3635 }
3636 }
3637 }
3638 if (warned)
3639 printk(KERN_WARNING
3640 "True protection against single-disk"
3641 " failure might be compromised.\n");
3642 }
3643
NeilBrown657390d2005-08-26 18:34:16 -07003644 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003646 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003647 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648
NeilBrownf91de922005-11-08 21:39:36 -08003649 if (start_readonly)
3650 mddev->ro = 2; /* read-only, but switch on first write */
3651
NeilBrownb15c2e52006-01-06 00:20:16 -08003652 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01003653 if (err)
3654 printk(KERN_ERR "md: pers->run() failed ...\n");
3655 else if (mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08003656 err = bitmap_create(mddev);
3657 if (err) {
3658 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3659 mdname(mddev), err);
3660 mddev->pers->stop(mddev);
3661 }
3662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 module_put(mddev->pers->owner);
3665 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003666 bitmap_destroy(mddev);
3667 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003669 if (mddev->pers->sync_request) {
3670 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3671 printk(KERN_WARNING
3672 "md: cannot register extra attributes for %s\n",
3673 mdname(mddev));
3674 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003675 mddev->ro = 0;
3676
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 atomic_set(&mddev->writes_pending,0);
3678 mddev->safemode = 0;
3679 mddev->safemode_timer.function = md_safemode_timeout;
3680 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003681 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003683
NeilBrownd089c6a2008-02-06 01:39:59 -08003684 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003685 if (rdev->raid_disk >= 0) {
3686 char nm[20];
3687 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003688 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3689 printk("md: cannot register %s for %s\n",
3690 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
3693 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3694
NeilBrown850b2b42006-10-03 01:15:46 -07003695 if (mddev->flags)
3696 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Andre Nollf233ea52008-07-21 17:05:22 +10003698 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699
3700 /* If we call blk_queue_make_request here, it will
3701 * re-initialise max_sectors etc which may have been
3702 * refined inside -> run. So just set the bits we need to set.
3703 * Most initialisation happended when we called
3704 * blk_queue_make_request(..., md_fail_request)
3705 * earlier.
3706 */
3707 mddev->queue->queuedata = mddev;
3708 mddev->queue->make_request_fn = mddev->pers->make_request;
3709
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003710 /* If there is a partially-recovered drive we need to
3711 * start recovery here. If we leave it to md_check_recovery,
3712 * it will remove the drives and not do the right thing
3713 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003714 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003715 struct list_head *rtmp;
3716 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003717 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003718 if (rdev->raid_disk >= 0 &&
3719 !test_bit(In_sync, &rdev->flags) &&
3720 !test_bit(Faulty, &rdev->flags))
3721 /* complete an interrupted recovery */
3722 spares++;
3723 if (spares && mddev->pers->sync_request) {
3724 mddev->recovery = 0;
3725 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3726 mddev->sync_thread = md_register_thread(md_do_sync,
3727 mddev,
3728 "%s_resync");
3729 if (!mddev->sync_thread) {
3730 printk(KERN_ERR "%s: could not start resync"
3731 " thread...\n",
3732 mdname(mddev));
3733 /* leave the spares where they are, it shouldn't hurt */
3734 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003735 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003736 }
3737 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003738 md_wakeup_thread(mddev->thread);
3739 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003740
Linus Torvalds44ce62942007-05-09 18:51:36 -07003741 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003742 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003743 sysfs_notify(&mddev->kobj, NULL, "array_state");
Neil Brown72a23c22008-06-28 08:31:41 +10003744 sysfs_notify(&mddev->kobj, NULL, "sync_action");
Neil Browna99ac972008-06-28 08:31:43 +10003745 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09003746 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 return 0;
3748}
3749
3750static int restart_array(mddev_t *mddev)
3751{
3752 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753
Andre Noll80fab1d2008-07-11 22:02:21 +10003754 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10003756 return -ENXIO;
3757 if (!mddev->pers)
3758 return -EINVAL;
3759 if (!mddev->ro)
3760 return -EBUSY;
3761 mddev->safemode = 0;
3762 mddev->ro = 0;
3763 set_disk_ro(disk, 0);
3764 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3765 mdname(mddev));
3766 /* Kick recovery or resync if necessary */
3767 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3768 md_wakeup_thread(mddev->thread);
3769 md_wakeup_thread(mddev->sync_thread);
3770 sysfs_notify(&mddev->kobj, NULL, "array_state");
3771 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772}
3773
NeilBrownacc55e22006-06-26 00:27:47 -07003774/* similar to deny_write_access, but accounts for our holding a reference
3775 * to the file ourselves */
3776static int deny_bitmap_write_access(struct file * file)
3777{
3778 struct inode *inode = file->f_mapping->host;
3779
3780 spin_lock(&inode->i_lock);
3781 if (atomic_read(&inode->i_writecount) > 1) {
3782 spin_unlock(&inode->i_lock);
3783 return -ETXTBSY;
3784 }
3785 atomic_set(&inode->i_writecount, -1);
3786 spin_unlock(&inode->i_lock);
3787
3788 return 0;
3789}
3790
3791static void restore_bitmap_write_access(struct file *file)
3792{
3793 struct inode *inode = file->f_mapping->host;
3794
3795 spin_lock(&inode->i_lock);
3796 atomic_set(&inode->i_writecount, 1);
3797 spin_unlock(&inode->i_lock);
3798}
3799
NeilBrown9e653b62006-06-26 00:27:58 -07003800/* mode:
3801 * 0 - completely stop and dis-assemble array
3802 * 1 - switch to readonly
3803 * 2 - stop but do not disassemble array
3804 */
Neil Browndf5b20c2008-07-11 22:02:22 +10003805static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
3807 int err = 0;
3808 struct gendisk *disk = mddev->gendisk;
3809
NeilBrownf2ea68c2008-07-21 17:05:25 +10003810 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10003811 printk("md: %s still in use.\n",mdname(mddev));
3812 return -EBUSY;
3813 }
3814
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
3817 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003818 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3820 md_unregister_thread(mddev->sync_thread);
3821 mddev->sync_thread = NULL;
3822 }
3823
3824 del_timer_sync(&mddev->safemode_timer);
3825
NeilBrown9e653b62006-06-26 00:27:58 -07003826 switch(mode) {
3827 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003829 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 goto out;
3831 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003832 break;
3833 case 0: /* disassemble */
3834 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003835 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003836 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 if (mddev->ro)
3838 set_disk_ro(disk, 0);
3839 blk_queue_make_request(mddev->queue, md_fail_request);
3840 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003841 mddev->queue->merge_bvec_fn = NULL;
3842 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003843 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003844 if (mddev->pers->sync_request)
3845 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3846
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 module_put(mddev->pers->owner);
3848 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003849 /* tell userspace to handle 'inactive' */
3850 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003851
3852 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003853 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 if (mddev->ro)
3856 mddev->ro = 0;
3857 }
NeilBrown850b2b42006-10-03 01:15:46 -07003858 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 /* mark array as shutdown cleanly */
3860 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003861 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 }
NeilBrown9e653b62006-06-26 00:27:58 -07003863 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003865 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 }
NeilBrown32a76272005-06-21 17:17:14 -07003867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 /*
3869 * Free resources if final stop
3870 */
NeilBrown9e653b62006-06-26 00:27:58 -07003871 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003872 mdk_rdev_t *rdev;
3873 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3876
NeilBrown978f9462006-02-02 14:28:05 -08003877 bitmap_destroy(mddev);
3878 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003879 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003880 fput(mddev->bitmap_file);
3881 mddev->bitmap_file = NULL;
3882 }
3883 mddev->bitmap_offset = 0;
3884
NeilBrownd089c6a2008-02-06 01:39:59 -08003885 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003886 if (rdev->raid_disk >= 0) {
3887 char nm[20];
3888 sprintf(nm, "rd%d", rdev->raid_disk);
3889 sysfs_remove_link(&mddev->kobj, nm);
3890 }
3891
NeilBrown177a99b2008-02-06 01:39:56 -08003892 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003893 flush_scheduled_work();
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 export_array(mddev);
3896
Andre Nollf233ea52008-07-21 17:05:22 +10003897 mddev->array_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003898 mddev->size = 0;
3899 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003900 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003901 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003902 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003903 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003904 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003905 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003906 mddev->level = LEVEL_NONE;
3907 mddev->clevel[0] = 0;
3908 mddev->flags = 0;
3909 mddev->ro = 0;
3910 mddev->metadata_type[0] = 0;
3911 mddev->chunk_size = 0;
3912 mddev->ctime = mddev->utime = 0;
3913 mddev->layout = 0;
3914 mddev->max_disks = 0;
3915 mddev->events = 0;
3916 mddev->delta_disks = 0;
3917 mddev->new_level = LEVEL_NONE;
3918 mddev->new_layout = 0;
3919 mddev->new_chunk = 0;
3920 mddev->curr_resync = 0;
3921 mddev->resync_mismatches = 0;
3922 mddev->suspend_lo = mddev->suspend_hi = 0;
3923 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3924 mddev->recovery = 0;
3925 mddev->in_sync = 0;
3926 mddev->changed = 0;
3927 mddev->degraded = 0;
3928 mddev->barriers_work = 0;
3929 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003930
NeilBrowna8a55c32006-06-26 00:27:39 -07003931 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3933 mdname(mddev));
3934 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003935 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003936 sysfs_notify(&mddev->kobj, NULL, "array_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937out:
3938 return err;
3939}
3940
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003941#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942static void autorun_array(mddev_t *mddev)
3943{
3944 mdk_rdev_t *rdev;
3945 struct list_head *tmp;
3946 int err;
3947
NeilBrowna757e642005-04-16 15:26:42 -07003948 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
3951 printk(KERN_INFO "md: running: ");
3952
NeilBrownd089c6a2008-02-06 01:39:59 -08003953 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 char b[BDEVNAME_SIZE];
3955 printk("<%s>", bdevname(rdev->bdev,b));
3956 }
3957 printk("\n");
3958
NeilBrownd710e132008-10-13 11:55:12 +11003959 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 if (err) {
3961 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11003962 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
3964}
3965
3966/*
3967 * lets try to run arrays based on all disks that have arrived
3968 * until now. (those are in pending_raid_disks)
3969 *
3970 * the method: pick the first pending disk, collect all disks with
3971 * the same UUID, remove all from the pending list and put them into
3972 * the 'same_array' list. Then order this list based on superblock
3973 * update time (freshest comes first), kick out 'old' disks and
3974 * compare superblocks. If everything's fine then run it.
3975 *
3976 * If "unit" is allocated, then bump its reference count
3977 */
3978static void autorun_devices(int part)
3979{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 struct list_head *tmp;
3981 mdk_rdev_t *rdev0, *rdev;
3982 mddev_t *mddev;
3983 char b[BDEVNAME_SIZE];
3984
3985 printk(KERN_INFO "md: autorun ...\n");
3986 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003987 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08003989 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 rdev0 = list_entry(pending_raid_disks.next,
3991 mdk_rdev_t, same_set);
3992
3993 printk(KERN_INFO "md: considering %s ...\n",
3994 bdevname(rdev0->bdev,b));
3995 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08003996 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 if (super_90_load(rdev, rdev0, 0) >= 0) {
3998 printk(KERN_INFO "md: adding %s ...\n",
3999 bdevname(rdev->bdev,b));
4000 list_move(&rdev->same_set, &candidates);
4001 }
4002 /*
4003 * now we have a set of devices, with all of them having
4004 * mostly sane superblocks. It's time to allocate the
4005 * mddev.
4006 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004007 if (part) {
4008 dev = MKDEV(mdp_major,
4009 rdev0->preferred_minor << MdpMinorShift);
4010 unit = MINOR(dev) >> MdpMinorShift;
4011 } else {
4012 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4013 unit = MINOR(dev);
4014 }
4015 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4017 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4018 break;
4019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 md_probe(dev, NULL, NULL);
4022 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004023 if (!mddev || !mddev->gendisk) {
4024 if (mddev)
4025 mddev_put(mddev);
4026 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 "md: cannot allocate memory for md drive.\n");
4028 break;
4029 }
4030 if (mddev_lock(mddev))
4031 printk(KERN_WARNING "md: %s locked, cannot run\n",
4032 mdname(mddev));
4033 else if (mddev->raid_disks || mddev->major_version
4034 || !list_empty(&mddev->disks)) {
4035 printk(KERN_WARNING
4036 "md: %s already running, cannot run %s\n",
4037 mdname(mddev), bdevname(rdev0->bdev,b));
4038 mddev_unlock(mddev);
4039 } else {
4040 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004041 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08004042 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 list_del_init(&rdev->same_set);
4044 if (bind_rdev_to_array(rdev, mddev))
4045 export_rdev(rdev);
4046 }
4047 autorun_array(mddev);
4048 mddev_unlock(mddev);
4049 }
4050 /* on success, candidates will be empty, on error
4051 * it won't...
4052 */
NeilBrown4b809912008-07-21 17:05:25 +10004053 rdev_for_each_list(rdev, tmp, candidates) {
4054 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 mddev_put(mddev);
4058 }
4059 printk(KERN_INFO "md: ... autorun DONE.\n");
4060}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004061#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063static int get_version(void __user * arg)
4064{
4065 mdu_version_t ver;
4066
4067 ver.major = MD_MAJOR_VERSION;
4068 ver.minor = MD_MINOR_VERSION;
4069 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4070
4071 if (copy_to_user(arg, &ver, sizeof(ver)))
4072 return -EFAULT;
4073
4074 return 0;
4075}
4076
4077static int get_array_info(mddev_t * mddev, void __user * arg)
4078{
4079 mdu_array_info_t info;
4080 int nr,working,active,failed,spare;
4081 mdk_rdev_t *rdev;
4082 struct list_head *tmp;
4083
4084 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004085 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004087 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 failed++;
4089 else {
4090 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004091 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 active++;
4093 else
4094 spare++;
4095 }
4096 }
4097
4098 info.major_version = mddev->major_version;
4099 info.minor_version = mddev->minor_version;
4100 info.patch_version = MD_PATCHLEVEL_VERSION;
4101 info.ctime = mddev->ctime;
4102 info.level = mddev->level;
4103 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004104 if (info.size != mddev->size) /* overflow */
4105 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 info.nr_disks = nr;
4107 info.raid_disks = mddev->raid_disks;
4108 info.md_minor = mddev->md_minor;
4109 info.not_persistent= !mddev->persistent;
4110
4111 info.utime = mddev->utime;
4112 info.state = 0;
4113 if (mddev->in_sync)
4114 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004115 if (mddev->bitmap && mddev->bitmap_offset)
4116 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 info.active_disks = active;
4118 info.working_disks = working;
4119 info.failed_disks = failed;
4120 info.spare_disks = spare;
4121
4122 info.layout = mddev->layout;
4123 info.chunk_size = mddev->chunk_size;
4124
4125 if (copy_to_user(arg, &info, sizeof(info)))
4126 return -EFAULT;
4127
4128 return 0;
4129}
4130
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004131static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004132{
4133 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4134 char *ptr, *buf = NULL;
4135 int err = -ENOMEM;
4136
Dan Williamsb5470dc2008-06-27 21:44:04 -07004137 if (md_allow_write(mddev))
4138 file = kmalloc(sizeof(*file), GFP_NOIO);
4139 else
4140 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004141
NeilBrown32a76272005-06-21 17:17:14 -07004142 if (!file)
4143 goto out;
4144
4145 /* bitmap disabled, zero the first byte and copy out */
4146 if (!mddev->bitmap || !mddev->bitmap->file) {
4147 file->pathname[0] = '\0';
4148 goto copy_out;
4149 }
4150
4151 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4152 if (!buf)
4153 goto out;
4154
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004155 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4156 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004157 goto out;
4158
4159 strcpy(file->pathname, ptr);
4160
4161copy_out:
4162 err = 0;
4163 if (copy_to_user(arg, file, sizeof(*file)))
4164 err = -EFAULT;
4165out:
4166 kfree(buf);
4167 kfree(file);
4168 return err;
4169}
4170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171static int get_disk_info(mddev_t * mddev, void __user * arg)
4172{
4173 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 mdk_rdev_t *rdev;
4175
4176 if (copy_from_user(&info, arg, sizeof(info)))
4177 return -EFAULT;
4178
Andre Noll26ef3792008-07-11 22:02:21 +10004179 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 if (rdev) {
4181 info.major = MAJOR(rdev->bdev->bd_dev);
4182 info.minor = MINOR(rdev->bdev->bd_dev);
4183 info.raid_disk = rdev->raid_disk;
4184 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004185 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004187 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 info.state |= (1<<MD_DISK_ACTIVE);
4189 info.state |= (1<<MD_DISK_SYNC);
4190 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004191 if (test_bit(WriteMostly, &rdev->flags))
4192 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 } else {
4194 info.major = info.minor = 0;
4195 info.raid_disk = -1;
4196 info.state = (1<<MD_DISK_REMOVED);
4197 }
4198
4199 if (copy_to_user(arg, &info, sizeof(info)))
4200 return -EFAULT;
4201
4202 return 0;
4203}
4204
4205static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4206{
4207 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4208 mdk_rdev_t *rdev;
4209 dev_t dev = MKDEV(info->major,info->minor);
4210
4211 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4212 return -EOVERFLOW;
4213
4214 if (!mddev->raid_disks) {
4215 int err;
4216 /* expecting a device which has a superblock */
4217 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4218 if (IS_ERR(rdev)) {
4219 printk(KERN_WARNING
4220 "md: md_import_device returned %ld\n",
4221 PTR_ERR(rdev));
4222 return PTR_ERR(rdev);
4223 }
4224 if (!list_empty(&mddev->disks)) {
4225 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4226 mdk_rdev_t, same_set);
4227 int err = super_types[mddev->major_version]
4228 .load_super(rdev, rdev0, mddev->minor_version);
4229 if (err < 0) {
4230 printk(KERN_WARNING
4231 "md: %s has different UUID to %s\n",
4232 bdevname(rdev->bdev,b),
4233 bdevname(rdev0->bdev,b2));
4234 export_rdev(rdev);
4235 return -EINVAL;
4236 }
4237 }
4238 err = bind_rdev_to_array(rdev, mddev);
4239 if (err)
4240 export_rdev(rdev);
4241 return err;
4242 }
4243
4244 /*
4245 * add_new_disk can be used once the array is assembled
4246 * to add "hot spares". They must already have a superblock
4247 * written
4248 */
4249 if (mddev->pers) {
4250 int err;
4251 if (!mddev->pers->hot_add_disk) {
4252 printk(KERN_WARNING
4253 "%s: personality does not support diskops!\n",
4254 mdname(mddev));
4255 return -EINVAL;
4256 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004257 if (mddev->persistent)
4258 rdev = md_import_device(dev, mddev->major_version,
4259 mddev->minor_version);
4260 else
4261 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 if (IS_ERR(rdev)) {
4263 printk(KERN_WARNING
4264 "md: md_import_device returned %ld\n",
4265 PTR_ERR(rdev));
4266 return PTR_ERR(rdev);
4267 }
NeilBrown41158c72005-06-21 17:17:25 -07004268 /* set save_raid_disk if appropriate */
4269 if (!mddev->persistent) {
4270 if (info->state & (1<<MD_DISK_SYNC) &&
4271 info->raid_disk < mddev->raid_disks)
4272 rdev->raid_disk = info->raid_disk;
4273 else
4274 rdev->raid_disk = -1;
4275 } else
4276 super_types[mddev->major_version].
4277 validate_super(mddev, rdev);
4278 rdev->saved_raid_disk = rdev->raid_disk;
4279
NeilBrownb2d444d2005-11-08 21:39:31 -08004280 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004281 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4282 set_bit(WriteMostly, &rdev->flags);
4283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 rdev->raid_disk = -1;
4285 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004286 if (!err && !mddev->pers->hot_remove_disk) {
4287 /* If there is hot_add_disk but no hot_remove_disk
4288 * then added disks for geometry changes,
4289 * and should be added immediately.
4290 */
4291 super_types[mddev->major_version].
4292 validate_super(mddev, rdev);
4293 err = mddev->pers->hot_add_disk(mddev, rdev);
4294 if (err)
4295 unbind_rdev_from_array(rdev);
4296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (err)
4298 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004299 else
4300 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc3617772005-06-21 17:17:10 -07004301
NeilBrown17571282006-12-10 02:20:52 -08004302 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004303 if (mddev->degraded)
4304 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004305 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004306 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return err;
4308 }
4309
4310 /* otherwise, add_new_disk is only allowed
4311 * for major_version==0 superblocks
4312 */
4313 if (mddev->major_version != 0) {
4314 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4315 mdname(mddev));
4316 return -EINVAL;
4317 }
4318
4319 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4320 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004321 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 if (IS_ERR(rdev)) {
4323 printk(KERN_WARNING
4324 "md: error, md_import_device() returned %ld\n",
4325 PTR_ERR(rdev));
4326 return PTR_ERR(rdev);
4327 }
4328 rdev->desc_nr = info->number;
4329 if (info->raid_disk < mddev->raid_disks)
4330 rdev->raid_disk = info->raid_disk;
4331 else
4332 rdev->raid_disk = -1;
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004335 if (info->state & (1<<MD_DISK_SYNC))
4336 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004338 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4339 set_bit(WriteMostly, &rdev->flags);
4340
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 if (!mddev->persistent) {
4342 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004343 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 } else
Andre Noll0f420352008-07-11 22:02:23 +10004345 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolle7debaa2008-07-11 22:02:23 +10004346 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
NeilBrown2bf071b2006-01-06 00:20:55 -08004348 err = bind_rdev_to_array(rdev, mddev);
4349 if (err) {
4350 export_rdev(rdev);
4351 return err;
4352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 }
4354
4355 return 0;
4356}
4357
4358static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4359{
4360 char b[BDEVNAME_SIZE];
4361 mdk_rdev_t *rdev;
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 rdev = find_rdev(mddev, dev);
4364 if (!rdev)
4365 return -ENXIO;
4366
4367 if (rdev->raid_disk >= 0)
4368 goto busy;
4369
4370 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004371 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004372 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373
4374 return 0;
4375busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004376 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 bdevname(rdev->bdev,b), mdname(mddev));
4378 return -EBUSY;
4379}
4380
4381static int hot_add_disk(mddev_t * mddev, dev_t dev)
4382{
4383 char b[BDEVNAME_SIZE];
4384 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 mdk_rdev_t *rdev;
4386
4387 if (!mddev->pers)
4388 return -ENODEV;
4389
4390 if (mddev->major_version != 0) {
4391 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4392 " version-0 superblocks.\n",
4393 mdname(mddev));
4394 return -EINVAL;
4395 }
4396 if (!mddev->pers->hot_add_disk) {
4397 printk(KERN_WARNING
4398 "%s: personality does not support diskops!\n",
4399 mdname(mddev));
4400 return -EINVAL;
4401 }
4402
NeilBrownd710e132008-10-13 11:55:12 +11004403 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 if (IS_ERR(rdev)) {
4405 printk(KERN_WARNING
4406 "md: error, md_import_device() returned %ld\n",
4407 PTR_ERR(rdev));
4408 return -EINVAL;
4409 }
4410
4411 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004412 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 else
Andre Noll0f420352008-07-11 22:02:23 +10004414 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
Andre Nolle7debaa2008-07-11 22:02:23 +10004416 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
NeilBrownb2d444d2005-11-08 21:39:31 -08004418 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 printk(KERN_WARNING
4420 "md: can not hot-add faulty %s disk to %s!\n",
4421 bdevname(rdev->bdev,b), mdname(mddev));
4422 err = -EINVAL;
4423 goto abort_export;
4424 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004425 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004427 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004428 err = bind_rdev_to_array(rdev, mddev);
4429 if (err)
4430 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
4432 /*
4433 * The rest should better be atomic, we can have disk failures
4434 * noticed in interrupt contexts ...
4435 */
4436
4437 if (rdev->desc_nr == mddev->max_disks) {
4438 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4439 mdname(mddev));
4440 err = -EBUSY;
4441 goto abort_unbind_export;
4442 }
4443
4444 rdev->raid_disk = -1;
4445
NeilBrown850b2b42006-10-03 01:15:46 -07004446 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
4448 /*
4449 * Kick recovery, maybe this spare has to be added to the
4450 * array immediately.
4451 */
4452 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4453 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004454 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 return 0;
4456
4457abort_unbind_export:
4458 unbind_rdev_from_array(rdev);
4459
4460abort_export:
4461 export_rdev(rdev);
4462 return err;
4463}
4464
NeilBrown32a76272005-06-21 17:17:14 -07004465static int set_bitmap_file(mddev_t *mddev, int fd)
4466{
4467 int err;
4468
NeilBrown36fa3062005-09-09 16:23:45 -07004469 if (mddev->pers) {
4470 if (!mddev->pers->quiesce)
4471 return -EBUSY;
4472 if (mddev->recovery || mddev->sync_thread)
4473 return -EBUSY;
4474 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004475 }
4476
NeilBrown36fa3062005-09-09 16:23:45 -07004477
4478 if (fd >= 0) {
4479 if (mddev->bitmap)
4480 return -EEXIST; /* cannot add when bitmap is present */
4481 mddev->bitmap_file = fget(fd);
4482
4483 if (mddev->bitmap_file == NULL) {
4484 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4485 mdname(mddev));
4486 return -EBADF;
4487 }
4488
4489 err = deny_bitmap_write_access(mddev->bitmap_file);
4490 if (err) {
4491 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4492 mdname(mddev));
4493 fput(mddev->bitmap_file);
4494 mddev->bitmap_file = NULL;
4495 return err;
4496 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004497 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004498 } else if (mddev->bitmap == NULL)
4499 return -ENOENT; /* cannot remove what isn't there */
4500 err = 0;
4501 if (mddev->pers) {
4502 mddev->pers->quiesce(mddev, 1);
4503 if (fd >= 0)
4504 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004505 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004506 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004507 fd = -1; /* make sure to put the file */
4508 }
NeilBrown36fa3062005-09-09 16:23:45 -07004509 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004510 }
4511 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004512 if (mddev->bitmap_file) {
4513 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004514 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004515 }
NeilBrown36fa3062005-09-09 16:23:45 -07004516 mddev->bitmap_file = NULL;
4517 }
4518
NeilBrown32a76272005-06-21 17:17:14 -07004519 return err;
4520}
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522/*
4523 * set_array_info is used two different ways
4524 * The original usage is when creating a new array.
4525 * In this usage, raid_disks is > 0 and it together with
4526 * level, size, not_persistent,layout,chunksize determine the
4527 * shape of the array.
4528 * This will always create an array with a type-0.90.0 superblock.
4529 * The newer usage is when assembling an array.
4530 * In this case raid_disks will be 0, and the major_version field is
4531 * use to determine which style super-blocks are to be found on the devices.
4532 * The minor and patch _version numbers are also kept incase the
4533 * super_block handler wishes to interpret them.
4534 */
4535static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4536{
4537
4538 if (info->raid_disks == 0) {
4539 /* just setting version number for superblock loading */
4540 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004541 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 super_types[info->major_version].name == NULL) {
4543 /* maybe try to auto-load a module? */
4544 printk(KERN_INFO
4545 "md: superblock version %d not known\n",
4546 info->major_version);
4547 return -EINVAL;
4548 }
4549 mddev->major_version = info->major_version;
4550 mddev->minor_version = info->minor_version;
4551 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004552 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 return 0;
4554 }
4555 mddev->major_version = MD_MAJOR_VERSION;
4556 mddev->minor_version = MD_MINOR_VERSION;
4557 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4558 mddev->ctime = get_seconds();
4559
4560 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004561 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 mddev->size = info->size;
4563 mddev->raid_disks = info->raid_disks;
4564 /* don't set md_minor, it is determined by which /dev/md* was
4565 * openned
4566 */
4567 if (info->state & (1<<MD_SB_CLEAN))
4568 mddev->recovery_cp = MaxSector;
4569 else
4570 mddev->recovery_cp = 0;
4571 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004572 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
4574 mddev->layout = info->layout;
4575 mddev->chunk_size = info->chunk_size;
4576
4577 mddev->max_disks = MD_SB_DISKS;
4578
NeilBrowne6910632008-02-06 01:39:51 -08004579 if (mddev->persistent)
4580 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004581 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
NeilBrownb2a27032005-11-28 13:44:12 -08004583 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4584 mddev->bitmap_offset = 0;
4585
NeilBrownf6705572006-03-27 01:18:11 -08004586 mddev->reshape_position = MaxSector;
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 /*
4589 * Generate a 128 bit UUID
4590 */
4591 get_random_bytes(mddev->uuid, 16);
4592
NeilBrownf6705572006-03-27 01:18:11 -08004593 mddev->new_level = mddev->level;
4594 mddev->new_chunk = mddev->chunk_size;
4595 mddev->new_layout = mddev->layout;
4596 mddev->delta_disks = 0;
4597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 return 0;
4599}
4600
Andre Nolld71f9f82008-07-11 22:02:22 +10004601static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004602{
4603 mdk_rdev_t * rdev;
4604 int rv;
4605 struct list_head *tmp;
Andre Nolld71f9f82008-07-11 22:02:22 +10004606 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004607
4608 if (mddev->pers->resize == NULL)
4609 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10004610 /* The "num_sectors" is the number of sectors of each device that
4611 * is used. This can only make sense for arrays with redundancy.
4612 * linear and raid0 always use whatever space is available. We can only
4613 * consider changing this number if no resync or reconstruction is
4614 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10004615 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10004616 * of each device. If num_sectors is zero, we find the largest size
4617 * that fits.
4618
NeilBrowna35b0d62006-01-06 00:20:49 -08004619 */
4620 if (mddev->sync_thread)
4621 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10004622 if (mddev->bitmap)
4623 /* Sorry, cannot grow a bitmap yet, just remove it,
4624 * grow, and re-add.
4625 */
4626 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004627 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004628 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004629 avail = rdev->size * 2;
4630
Andre Nolld71f9f82008-07-11 22:02:22 +10004631 if (fit && (num_sectors == 0 || num_sectors > avail))
4632 num_sectors = avail;
4633 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004634 return -ENOSPC;
4635 }
Andre Nolld71f9f82008-07-11 22:02:22 +10004636 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004637 if (!rv) {
4638 struct block_device *bdev;
4639
4640 bdev = bdget_disk(mddev->gendisk, 0);
4641 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004642 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10004643 i_size_write(bdev->bd_inode,
4644 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004645 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004646 bdput(bdev);
4647 }
4648 }
4649 return rv;
4650}
4651
NeilBrownda943b992006-01-06 00:20:54 -08004652static int update_raid_disks(mddev_t *mddev, int raid_disks)
4653{
4654 int rv;
4655 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004656 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004657 return -EINVAL;
4658 if (raid_disks <= 0 ||
4659 raid_disks >= mddev->max_disks)
4660 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004661 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004662 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004663 mddev->delta_disks = raid_disks - mddev->raid_disks;
4664
4665 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004666 return rv;
4667}
4668
4669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670/*
4671 * update_array_info is used to change the configuration of an
4672 * on-line array.
4673 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4674 * fields in the info are checked against the array.
4675 * Any differences that cannot be handled will cause an error.
4676 * Normally, only one change can be managed at a time.
4677 */
4678static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4679{
4680 int rv = 0;
4681 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004682 int state = 0;
4683
4684 /* calculate expected state,ignoring low bits */
4685 if (mddev->bitmap && mddev->bitmap_offset)
4686 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
4688 if (mddev->major_version != info->major_version ||
4689 mddev->minor_version != info->minor_version ||
4690/* mddev->patch_version != info->patch_version || */
4691 mddev->ctime != info->ctime ||
4692 mddev->level != info->level ||
4693/* mddev->layout != info->layout || */
4694 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004695 mddev->chunk_size != info->chunk_size ||
4696 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4697 ((state^info->state) & 0xfffffe00)
4698 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 return -EINVAL;
4700 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004701 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 if (mddev->raid_disks != info->raid_disks) cnt++;
4703 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004704 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 if (cnt == 0) return 0;
4706 if (cnt > 1) return -EINVAL;
4707
4708 if (mddev->layout != info->layout) {
4709 /* Change layout
4710 * we don't need to do anything at the md level, the
4711 * personality will take care of it all.
4712 */
4713 if (mddev->pers->reconfig == NULL)
4714 return -EINVAL;
4715 else
4716 return mddev->pers->reconfig(mddev, info->layout, -1);
4717 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004718 if (info->size >= 0 && mddev->size != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10004719 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
NeilBrownda943b992006-01-06 00:20:54 -08004721 if (mddev->raid_disks != info->raid_disks)
4722 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723
NeilBrown36fa3062005-09-09 16:23:45 -07004724 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4725 if (mddev->pers->quiesce == NULL)
4726 return -EINVAL;
4727 if (mddev->recovery || mddev->sync_thread)
4728 return -EBUSY;
4729 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4730 /* add the bitmap */
4731 if (mddev->bitmap)
4732 return -EEXIST;
4733 if (mddev->default_bitmap_offset == 0)
4734 return -EINVAL;
4735 mddev->bitmap_offset = mddev->default_bitmap_offset;
4736 mddev->pers->quiesce(mddev, 1);
4737 rv = bitmap_create(mddev);
4738 if (rv)
4739 bitmap_destroy(mddev);
4740 mddev->pers->quiesce(mddev, 0);
4741 } else {
4742 /* remove the bitmap */
4743 if (!mddev->bitmap)
4744 return -ENOENT;
4745 if (mddev->bitmap->file)
4746 return -EINVAL;
4747 mddev->pers->quiesce(mddev, 1);
4748 bitmap_destroy(mddev);
4749 mddev->pers->quiesce(mddev, 0);
4750 mddev->bitmap_offset = 0;
4751 }
4752 }
NeilBrown850b2b42006-10-03 01:15:46 -07004753 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 return rv;
4755}
4756
4757static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4758{
4759 mdk_rdev_t *rdev;
4760
4761 if (mddev->pers == NULL)
4762 return -ENODEV;
4763
4764 rdev = find_rdev(mddev, dev);
4765 if (!rdev)
4766 return -ENODEV;
4767
4768 md_error(mddev, rdev);
4769 return 0;
4770}
4771
Andre Noll2f9618c2008-04-25 18:57:58 +02004772/*
4773 * We have a problem here : there is no easy way to give a CHS
4774 * virtual geometry. We currently pretend that we have a 2 heads
4775 * 4 sectors (with a BIG number of cylinders...). This drives
4776 * dosfs just mad... ;-)
4777 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004778static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4779{
4780 mddev_t *mddev = bdev->bd_disk->private_data;
4781
4782 geo->heads = 2;
4783 geo->sectors = 4;
4784 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4785 return 0;
4786}
4787
Al Viroa39907f2008-03-02 10:31:15 -05004788static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 unsigned int cmd, unsigned long arg)
4790{
4791 int err = 0;
4792 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 mddev_t *mddev = NULL;
4794
4795 if (!capable(CAP_SYS_ADMIN))
4796 return -EACCES;
4797
4798 /*
4799 * Commands dealing with the RAID driver but not any
4800 * particular array:
4801 */
4802 switch (cmd)
4803 {
4804 case RAID_VERSION:
4805 err = get_version(argp);
4806 goto done;
4807
4808 case PRINT_RAID_DEBUG:
4809 err = 0;
4810 md_print_devices();
4811 goto done;
4812
4813#ifndef MODULE
4814 case RAID_AUTORUN:
4815 err = 0;
4816 autostart_arrays(arg);
4817 goto done;
4818#endif
4819 default:;
4820 }
4821
4822 /*
4823 * Commands creating/starting a new array:
4824 */
4825
Al Viroa39907f2008-03-02 10:31:15 -05004826 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
4828 if (!mddev) {
4829 BUG();
4830 goto abort;
4831 }
4832
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 err = mddev_lock(mddev);
4834 if (err) {
4835 printk(KERN_INFO
4836 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4837 err, cmd);
4838 goto abort;
4839 }
4840
4841 switch (cmd)
4842 {
4843 case SET_ARRAY_INFO:
4844 {
4845 mdu_array_info_t info;
4846 if (!arg)
4847 memset(&info, 0, sizeof(info));
4848 else if (copy_from_user(&info, argp, sizeof(info))) {
4849 err = -EFAULT;
4850 goto abort_unlock;
4851 }
4852 if (mddev->pers) {
4853 err = update_array_info(mddev, &info);
4854 if (err) {
4855 printk(KERN_WARNING "md: couldn't update"
4856 " array info. %d\n", err);
4857 goto abort_unlock;
4858 }
4859 goto done_unlock;
4860 }
4861 if (!list_empty(&mddev->disks)) {
4862 printk(KERN_WARNING
4863 "md: array %s already has disks!\n",
4864 mdname(mddev));
4865 err = -EBUSY;
4866 goto abort_unlock;
4867 }
4868 if (mddev->raid_disks) {
4869 printk(KERN_WARNING
4870 "md: array %s already initialised!\n",
4871 mdname(mddev));
4872 err = -EBUSY;
4873 goto abort_unlock;
4874 }
4875 err = set_array_info(mddev, &info);
4876 if (err) {
4877 printk(KERN_WARNING "md: couldn't set"
4878 " array info. %d\n", err);
4879 goto abort_unlock;
4880 }
4881 }
4882 goto done_unlock;
4883
4884 default:;
4885 }
4886
4887 /*
4888 * Commands querying/configuring an existing array:
4889 */
NeilBrown32a76272005-06-21 17:17:14 -07004890 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004891 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004892 if ((!mddev->raid_disks && !mddev->external)
4893 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4894 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4895 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 err = -ENODEV;
4897 goto abort_unlock;
4898 }
4899
4900 /*
4901 * Commands even a read-only array can execute:
4902 */
4903 switch (cmd)
4904 {
4905 case GET_ARRAY_INFO:
4906 err = get_array_info(mddev, argp);
4907 goto done_unlock;
4908
NeilBrown32a76272005-06-21 17:17:14 -07004909 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004910 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004911 goto done_unlock;
4912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 case GET_DISK_INFO:
4914 err = get_disk_info(mddev, argp);
4915 goto done_unlock;
4916
4917 case RESTART_ARRAY_RW:
4918 err = restart_array(mddev);
4919 goto done_unlock;
4920
4921 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11004922 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 goto done_unlock;
4924
4925 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11004926 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 goto done_unlock;
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 }
4930
4931 /*
4932 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004933 * superblock, so we do not allow them on read-only arrays.
4934 * However non-MD ioctls (e.g. get-size) will still come through
4935 * here and hit the 'default' below, so only disallow
4936 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 */
Andre Nollbb57fc62008-04-25 19:06:35 +02004938 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004939 if (mddev->ro == 2) {
4940 mddev->ro = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10004941 sysfs_notify(&mddev->kobj, NULL, "array_state");
4942 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4943 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08004944 } else {
4945 err = -EROFS;
4946 goto abort_unlock;
4947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 }
4949
4950 switch (cmd)
4951 {
4952 case ADD_NEW_DISK:
4953 {
4954 mdu_disk_info_t info;
4955 if (copy_from_user(&info, argp, sizeof(info)))
4956 err = -EFAULT;
4957 else
4958 err = add_new_disk(mddev, &info);
4959 goto done_unlock;
4960 }
4961
4962 case HOT_REMOVE_DISK:
4963 err = hot_remove_disk(mddev, new_decode_dev(arg));
4964 goto done_unlock;
4965
4966 case HOT_ADD_DISK:
4967 err = hot_add_disk(mddev, new_decode_dev(arg));
4968 goto done_unlock;
4969
4970 case SET_DISK_FAULTY:
4971 err = set_disk_faulty(mddev, new_decode_dev(arg));
4972 goto done_unlock;
4973
4974 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11004975 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 goto done_unlock;
4977
NeilBrown32a76272005-06-21 17:17:14 -07004978 case SET_BITMAP_FILE:
4979 err = set_bitmap_file(mddev, (int)arg);
4980 goto done_unlock;
4981
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 err = -EINVAL;
4984 goto abort_unlock;
4985 }
4986
4987done_unlock:
4988abort_unlock:
4989 mddev_unlock(mddev);
4990
4991 return err;
4992done:
4993 if (err)
4994 MD_BUG();
4995abort:
4996 return err;
4997}
4998
Al Viroa39907f2008-03-02 10:31:15 -05004999static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
5001 /*
5002 * Succeed if we can lock the mddev, which confirms that
5003 * it isn't being stopped right now.
5004 */
Al Viroa39907f2008-03-02 10:31:15 -05005005 mddev_t *mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 int err;
5007
NeilBrownd63a5a72006-12-08 02:36:17 -08005008 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 goto out;
5010
5011 err = 0;
5012 mddev_get(mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005013 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 mddev_unlock(mddev);
5015
Al Viroa39907f2008-03-02 10:31:15 -05005016 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 out:
5018 return err;
5019}
5020
Al Viroa39907f2008-03-02 10:31:15 -05005021static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022{
Al Viroa39907f2008-03-02 10:31:15 -05005023 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005025 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005026 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 mddev_put(mddev);
5028
5029 return 0;
5030}
5031
Linus Torvalds44ce62942007-05-09 18:51:36 -07005032static int md_media_changed(struct gendisk *disk)
5033{
5034 mddev_t *mddev = disk->private_data;
5035
5036 return mddev->changed;
5037}
5038
5039static int md_revalidate(struct gendisk *disk)
5040{
5041 mddev_t *mddev = disk->private_data;
5042
5043 mddev->changed = 0;
5044 return 0;
5045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046static struct block_device_operations md_fops =
5047{
5048 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005049 .open = md_open,
5050 .release = md_release,
5051 .locked_ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005052 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005053 .media_changed = md_media_changed,
5054 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055};
5056
Adrian Bunk75c96f82005-05-05 16:16:09 -07005057static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
5059 mdk_thread_t *thread = arg;
5060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 /*
5062 * md_thread is a 'system-thread', it's priority should be very
5063 * high. We avoid resource deadlocks individually in each
5064 * raid personality. (RAID5 does preallocation) We also use RR and
5065 * the very same RT priority as kswapd, thus we will never get
5066 * into a priority inversion deadlock.
5067 *
5068 * we definitely have to have equal or higher priority than
5069 * bdflush, otherwise bdflush will deadlock if there are too
5070 * many dirty RAID5 blocks.
5071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
NeilBrown6985c432005-10-19 21:23:47 -07005073 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005074 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
NeilBrown93588e22005-11-15 00:09:12 -08005076 /* We need to wait INTERRUPTIBLE so that
5077 * we don't add to the load-average.
5078 * That means we need to be sure no signals are
5079 * pending
5080 */
5081 if (signal_pending(current))
5082 flush_signals(current);
5083
5084 wait_event_interruptible_timeout
5085 (thread->wqueue,
5086 test_bit(THREAD_WAKEUP, &thread->flags)
5087 || kthread_should_stop(),
5088 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
5090 clear_bit(THREAD_WAKEUP, &thread->flags);
5091
NeilBrown787453c2005-11-08 21:39:43 -08005092 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 return 0;
5096}
5097
5098void md_wakeup_thread(mdk_thread_t *thread)
5099{
5100 if (thread) {
5101 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5102 set_bit(THREAD_WAKEUP, &thread->flags);
5103 wake_up(&thread->wqueue);
5104 }
5105}
5106
5107mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5108 const char *name)
5109{
5110 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111
NeilBrown9ffae0c2006-01-06 00:20:32 -08005112 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113 if (!thread)
5114 return NULL;
5115
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 init_waitqueue_head(&thread->wqueue);
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 thread->run = run;
5119 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005120 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005121 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005122 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 kfree(thread);
5124 return NULL;
5125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 return thread;
5127}
5128
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129void md_unregister_thread(mdk_thread_t *thread)
5130{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005131 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005132
5133 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 kfree(thread);
5135}
5136
5137void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5138{
5139 if (!mddev) {
5140 MD_BUG();
5141 return;
5142 }
5143
NeilBrownb2d444d2005-11-08 21:39:31 -08005144 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005146
5147 if (mddev->external)
5148 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005149/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5151 mdname(mddev),
5152 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5153 __builtin_return_address(0),__builtin_return_address(1),
5154 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005155*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005156 if (!mddev->pers)
5157 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 if (!mddev->pers->error_handler)
5159 return;
5160 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005161 if (mddev->degraded)
5162 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005163 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5165 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5166 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005167 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168}
5169
5170/* seq_file implementation /proc/mdstat */
5171
5172static void status_unused(struct seq_file *seq)
5173{
5174 int i = 0;
5175 mdk_rdev_t *rdev;
5176 struct list_head *tmp;
5177
5178 seq_printf(seq, "unused devices: ");
5179
NeilBrown73c34432008-02-06 01:39:59 -08005180 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 char b[BDEVNAME_SIZE];
5182 i++;
5183 seq_printf(seq, "%s ",
5184 bdevname(rdev->bdev,b));
5185 }
5186 if (!i)
5187 seq_printf(seq, "<none>");
5188
5189 seq_printf(seq, "\n");
5190}
5191
5192
5193static void status_resync(struct seq_file *seq, mddev_t * mddev)
5194{
NeilBrown4588b422006-03-27 01:18:04 -08005195 sector_t max_blocks, resync, res;
5196 unsigned long dt, db, rt;
5197 int scale;
5198 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
5200 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5201
5202 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5203 max_blocks = mddev->resync_max_sectors >> 1;
5204 else
5205 max_blocks = mddev->size;
5206
5207 /*
5208 * Should not happen.
5209 */
5210 if (!max_blocks) {
5211 MD_BUG();
5212 return;
5213 }
NeilBrown4588b422006-03-27 01:18:04 -08005214 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5215 * in a sector_t, and (max_blocks>>scale) will fit in a
5216 * u32, as those are the requirements for sector_div.
5217 * Thus 'scale' must be at least 10
5218 */
5219 scale = 10;
5220 if (sizeof(sector_t) > sizeof(unsigned long)) {
5221 while ( max_blocks/2 > (1ULL<<(scale+32)))
5222 scale++;
5223 }
5224 res = (resync>>scale)*1000;
5225 sector_div(res, (u32)((max_blocks>>scale)+1));
5226
5227 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 {
NeilBrown4588b422006-03-27 01:18:04 -08005229 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 seq_printf(seq, "[");
5231 for (i = 0; i < x; i++)
5232 seq_printf(seq, "=");
5233 seq_printf(seq, ">");
5234 for (i = 0; i < y; i++)
5235 seq_printf(seq, ".");
5236 seq_printf(seq, "] ");
5237 }
NeilBrown4588b422006-03-27 01:18:04 -08005238 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005239 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5240 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005241 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5242 "check" :
5243 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5244 "resync" : "recovery"))),
5245 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005246 (unsigned long long) resync,
5247 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248
5249 /*
5250 * We do not want to overflow, so the order of operands and
5251 * the * 100 / 100 trick are important. We do a +1 to be
5252 * safe against division by zero. We only estimate anyway.
5253 *
5254 * dt: time from mark until now
5255 * db: blocks written from mark until now
5256 * rt: remaining time
5257 */
5258 dt = ((jiffies - mddev->resync_mark) / HZ);
5259 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005260 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5261 - mddev->resync_mark_cnt;
5262 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263
5264 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5265
NeilBrownff4e8d92006-07-10 04:44:16 -07005266 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267}
5268
5269static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5270{
5271 struct list_head *tmp;
5272 loff_t l = *pos;
5273 mddev_t *mddev;
5274
5275 if (l >= 0x10000)
5276 return NULL;
5277 if (!l--)
5278 /* header */
5279 return (void*)1;
5280
5281 spin_lock(&all_mddevs_lock);
5282 list_for_each(tmp,&all_mddevs)
5283 if (!l--) {
5284 mddev = list_entry(tmp, mddev_t, all_mddevs);
5285 mddev_get(mddev);
5286 spin_unlock(&all_mddevs_lock);
5287 return mddev;
5288 }
5289 spin_unlock(&all_mddevs_lock);
5290 if (!l--)
5291 return (void*)2;/* tail */
5292 return NULL;
5293}
5294
5295static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5296{
5297 struct list_head *tmp;
5298 mddev_t *next_mddev, *mddev = v;
5299
5300 ++*pos;
5301 if (v == (void*)2)
5302 return NULL;
5303
5304 spin_lock(&all_mddevs_lock);
5305 if (v == (void*)1)
5306 tmp = all_mddevs.next;
5307 else
5308 tmp = mddev->all_mddevs.next;
5309 if (tmp != &all_mddevs)
5310 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5311 else {
5312 next_mddev = (void*)2;
5313 *pos = 0x10000;
5314 }
5315 spin_unlock(&all_mddevs_lock);
5316
5317 if (v != (void*)1)
5318 mddev_put(mddev);
5319 return next_mddev;
5320
5321}
5322
5323static void md_seq_stop(struct seq_file *seq, void *v)
5324{
5325 mddev_t *mddev = v;
5326
5327 if (mddev && v != (void*)1 && v != (void*)2)
5328 mddev_put(mddev);
5329}
5330
NeilBrownd7603b72006-01-06 00:20:30 -08005331struct mdstat_info {
5332 int event;
5333};
5334
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335static int md_seq_show(struct seq_file *seq, void *v)
5336{
5337 mddev_t *mddev = v;
5338 sector_t size;
5339 struct list_head *tmp2;
5340 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005341 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005342 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
5344 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005345 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 seq_printf(seq, "Personalities : ");
5347 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005348 list_for_each_entry(pers, &pers_list, list)
5349 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 spin_unlock(&pers_lock);
5352 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005353 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 return 0;
5355 }
5356 if (v == (void*)2) {
5357 status_unused(seq);
5358 return 0;
5359 }
5360
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005361 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005363
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5365 seq_printf(seq, "%s : %sactive", mdname(mddev),
5366 mddev->pers ? "" : "in");
5367 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005368 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005370 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005371 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 seq_printf(seq, " %s", mddev->pers->name);
5373 }
5374
5375 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005376 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 char b[BDEVNAME_SIZE];
5378 seq_printf(seq, " %s[%d]",
5379 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005380 if (test_bit(WriteMostly, &rdev->flags))
5381 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005382 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 seq_printf(seq, "(F)");
5384 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005385 } else if (rdev->raid_disk < 0)
5386 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 size += rdev->size;
5388 }
5389
5390 if (!list_empty(&mddev->disks)) {
5391 if (mddev->pers)
5392 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005393 (unsigned long long)
5394 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 else
5396 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005397 (unsigned long long)size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005399 if (mddev->persistent) {
5400 if (mddev->major_version != 0 ||
5401 mddev->minor_version != 90) {
5402 seq_printf(seq," super %d.%d",
5403 mddev->major_version,
5404 mddev->minor_version);
5405 }
NeilBrowne6910632008-02-06 01:39:51 -08005406 } else if (mddev->external)
5407 seq_printf(seq, " super external:%s",
5408 mddev->metadata_type);
5409 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005410 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411
5412 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005413 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005415 if (mddev->pers->sync_request) {
5416 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005417 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005418 seq_printf(seq, "\n ");
5419 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5420 seq_printf(seq, "\tresync=DELAYED\n ");
5421 else if (mddev->recovery_cp < MaxSector)
5422 seq_printf(seq, "\tresync=PENDING\n ");
5423 }
NeilBrown32a76272005-06-21 17:17:14 -07005424 } else
5425 seq_printf(seq, "\n ");
5426
5427 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005428 unsigned long chunk_kb;
5429 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005430 spin_lock_irqsave(&bitmap->lock, flags);
5431 chunk_kb = bitmap->chunksize >> 10;
5432 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5433 "%lu%s chunk",
5434 bitmap->pages - bitmap->missing_pages,
5435 bitmap->pages,
5436 (bitmap->pages - bitmap->missing_pages)
5437 << (PAGE_SHIFT - 10),
5438 chunk_kb ? chunk_kb : bitmap->chunksize,
5439 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005440 if (bitmap->file) {
5441 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005442 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005443 }
NeilBrown78d742d2005-06-21 17:17:15 -07005444
NeilBrown32a76272005-06-21 17:17:14 -07005445 seq_printf(seq, "\n");
5446 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 }
5448
5449 seq_printf(seq, "\n");
5450 }
5451 mddev_unlock(mddev);
5452
5453 return 0;
5454}
5455
5456static struct seq_operations md_seq_ops = {
5457 .start = md_seq_start,
5458 .next = md_seq_next,
5459 .stop = md_seq_stop,
5460 .show = md_seq_show,
5461};
5462
5463static int md_seq_open(struct inode *inode, struct file *file)
5464{
5465 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005466 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5467 if (mi == NULL)
5468 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
5470 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005471 if (error)
5472 kfree(mi);
5473 else {
5474 struct seq_file *p = file->private_data;
5475 p->private = mi;
5476 mi->event = atomic_read(&md_event_count);
5477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return error;
5479}
5480
NeilBrownd7603b72006-01-06 00:20:30 -08005481static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5482{
5483 struct seq_file *m = filp->private_data;
5484 struct mdstat_info *mi = m->private;
5485 int mask;
5486
5487 poll_wait(filp, &md_event_waiters, wait);
5488
5489 /* always allow read */
5490 mask = POLLIN | POLLRDNORM;
5491
5492 if (mi->event != atomic_read(&md_event_count))
5493 mask |= POLLERR | POLLPRI;
5494 return mask;
5495}
5496
Arjan van de Venfa027c22007-02-12 00:55:33 -08005497static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005498 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 .open = md_seq_open,
5500 .read = seq_read,
5501 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005502 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005503 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504};
5505
NeilBrown2604b702006-01-06 00:20:36 -08005506int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005509 list_add_tail(&p->list, &pers_list);
5510 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 spin_unlock(&pers_lock);
5512 return 0;
5513}
5514
NeilBrown2604b702006-01-06 00:20:36 -08005515int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516{
NeilBrown2604b702006-01-06 00:20:36 -08005517 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005519 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 spin_unlock(&pers_lock);
5521 return 0;
5522}
5523
5524static int is_mddev_idle(mddev_t *mddev)
5525{
5526 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005528 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529
5530 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10005531 rcu_read_lock();
5532 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Tejun Heo074a7ac2008-08-25 19:56:14 +09005534 curr_events = part_stat_read(&disk->part0, sectors[0]) +
5535 part_stat_read(&disk->part0, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005537 /* sync IO will cause sync_io to increase before the disk_stats
5538 * as sync_io is counted when a request starts, and
5539 * disk_stats is counted when it completes.
5540 * So resync activity will cause curr_events to be smaller than
5541 * when there was no such activity.
5542 * non-sync IO will cause disk_stat to increase without
5543 * increasing sync_io so curr_events will (eventually)
5544 * be larger than it was before. Once it becomes
5545 * substantially larger, the test below will cause
5546 * the array to appear non-idle, and resync will slow
5547 * down.
5548 * If there is a lot of outstanding resync activity when
5549 * we set last_event to curr_events, then all that activity
5550 * completing might cause the array to appear non-idle
5551 * and resync will be slowed down even though there might
5552 * not have been non-resync activity. This will only
5553 * happen once though. 'last_events' will soon reflect
5554 * the state where there is little or no outstanding
5555 * resync requests, and further resync activity will
5556 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005557 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005559 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 rdev->last_events = curr_events;
5561 idle = 0;
5562 }
5563 }
NeilBrown4b809912008-07-21 17:05:25 +10005564 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 return idle;
5566}
5567
5568void md_done_sync(mddev_t *mddev, int blocks, int ok)
5569{
5570 /* another "blocks" (512byte) blocks have been synced */
5571 atomic_sub(blocks, &mddev->recovery_active);
5572 wake_up(&mddev->recovery_wait);
5573 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005574 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 md_wakeup_thread(mddev->thread);
5576 // stop recovery, signal do_sync ....
5577 }
5578}
5579
5580
NeilBrown06d91a52005-06-21 17:17:12 -07005581/* md_write_start(mddev, bi)
5582 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005583 * in superblock) before writing, schedule a superblock update
5584 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005585 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005586void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
Neil Brown0fd62b82008-06-28 08:31:36 +10005588 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005589 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005590 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005591
NeilBrownf91de922005-11-08 21:39:36 -08005592 BUG_ON(mddev->ro == 1);
5593 if (mddev->ro == 2) {
5594 /* need to switch to read/write */
5595 mddev->ro = 0;
5596 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5597 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005598 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005599 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005600 }
NeilBrown06d91a52005-06-21 17:17:12 -07005601 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005602 if (mddev->safemode == 1)
5603 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005604 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005605 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005606 if (mddev->in_sync) {
5607 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005608 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005609 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005610 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005611 }
NeilBrowna9701a32005-11-08 21:39:34 -08005612 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005613 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005614 if (did_change)
5615 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown09a44cc2008-05-23 13:04:36 -07005616 wait_event(mddev->sb_wait,
5617 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5618 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
5621void md_write_end(mddev_t *mddev)
5622{
5623 if (atomic_dec_and_test(&mddev->writes_pending)) {
5624 if (mddev->safemode == 2)
5625 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005626 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5628 }
5629}
5630
NeilBrown2a2275d2007-01-26 00:57:11 -08005631/* md_allow_write(mddev)
5632 * Calling this ensures that the array is marked 'active' so that writes
5633 * may proceed without blocking. It is important to call this before
5634 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5635 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005636 *
5637 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5638 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005639 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005640int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005641{
5642 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005643 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005644 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005645 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005646 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005647 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005648
5649 spin_lock_irq(&mddev->write_lock);
5650 if (mddev->in_sync) {
5651 mddev->in_sync = 0;
5652 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5653 if (mddev->safemode_delay &&
5654 mddev->safemode == 0)
5655 mddev->safemode = 1;
5656 spin_unlock_irq(&mddev->write_lock);
5657 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005658 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown2a2275d2007-01-26 00:57:11 -08005659 } else
5660 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005661
5662 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5663 return -EAGAIN;
5664 else
5665 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005666}
5667EXPORT_SYMBOL_GPL(md_allow_write);
5668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669#define SYNC_MARKS 10
5670#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005671void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672{
5673 mddev_t *mddev2;
5674 unsigned int currspeed = 0,
5675 window;
NeilBrown57afd892005-06-21 17:17:13 -07005676 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 unsigned long mark[SYNC_MARKS];
5678 sector_t mark_cnt[SYNC_MARKS];
5679 int last_mark,m;
5680 struct list_head *tmp;
5681 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005682 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005683 struct list_head *rtmp;
5684 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005685 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 /* just incase thread restarts... */
5688 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5689 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005690 if (mddev->ro) /* never try to sync a read-only array */
5691 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
NeilBrown61df9d92006-10-03 01:15:57 -07005693 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5694 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5695 desc = "data-check";
5696 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5697 desc = "requested-resync";
5698 else
5699 desc = "resync";
5700 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5701 desc = "reshape";
5702 else
5703 desc = "recovery";
5704
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 /* we overload curr_resync somewhat here.
5706 * 0 == not engaged in resync at all
5707 * 2 == checking that there is no conflict with another sync
5708 * 1 == like 2, but have yielded to allow conflicting resync to
5709 * commense
5710 * other == active in resync - this many blocks
5711 *
5712 * Before starting a resync we must have set curr_resync to
5713 * 2, and then checked that every "conflicting" array has curr_resync
5714 * less than ours. When we find one that is the same or higher
5715 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5716 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5717 * This will mean we have to start checking from the beginning again.
5718 *
5719 */
5720
5721 do {
5722 mddev->curr_resync = 2;
5723
5724 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005725 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005726 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 goto skip;
5728 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005729 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 if (mddev2 == mddev)
5731 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005732 if (!mddev->parallel_resync
5733 && mddev2->curr_resync
5734 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 DEFINE_WAIT(wq);
5736 if (mddev < mddev2 && mddev->curr_resync == 2) {
5737 /* arbitrarily yield */
5738 mddev->curr_resync = 1;
5739 wake_up(&resync_wait);
5740 }
5741 if (mddev > mddev2 && mddev->curr_resync == 1)
5742 /* no need to wait here, we can wait the next
5743 * time 'round when curr_resync == 2
5744 */
5745 continue;
NeilBrown97441972008-09-19 11:49:54 +10005746 /* We need to wait 'interruptible' so as not to
5747 * contribute to the load average, and not to
5748 * be caught by 'softlockup'
5749 */
5750 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08005751 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005752 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005753 printk(KERN_INFO "md: delaying %s of %s"
5754 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005756 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10005758 if (signal_pending(current))
5759 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 schedule();
5761 finish_wait(&resync_wait, &wq);
5762 goto try_again;
5763 }
5764 finish_wait(&resync_wait, &wq);
5765 }
5766 }
5767 } while (mddev->curr_resync < 2);
5768
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005769 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005770 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005772 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 */
5774 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005775 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005776 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005777 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5778 j = mddev->resync_min;
5779 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005780 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005781
NeilBrownccfcc3c2006-03-27 01:18:09 -08005782 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5783 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005784 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 /* recovery follows the physical size of devices */
5786 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005787 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005788 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005789 if (rdev->raid_disk >= 0 &&
5790 !test_bit(Faulty, &rdev->flags) &&
5791 !test_bit(In_sync, &rdev->flags) &&
5792 rdev->recovery_offset < j)
5793 j = rdev->recovery_offset;
5794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795
NeilBrown61df9d92006-10-03 01:15:57 -07005796 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5797 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5798 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005799 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005800 "(but not more than %d KB/sec) for %s.\n",
5801 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
5803 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005804
NeilBrown57afd892005-06-21 17:17:13 -07005805 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 for (m = 0; m < SYNC_MARKS; m++) {
5807 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005808 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 }
5810 last_mark = 0;
5811 mddev->resync_mark = mark[last_mark];
5812 mddev->resync_mark_cnt = mark_cnt[last_mark];
5813
5814 /*
5815 * Tune reconstruction:
5816 */
5817 window = 32*(PAGE_SIZE/512);
5818 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5819 window/2,(unsigned long long) max_sectors/2);
5820
5821 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 last_check = 0;
5823
5824 if (j>2) {
5825 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005826 "md: resuming %s of %s from checkpoint.\n",
5827 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828 mddev->curr_resync = j;
5829 }
5830
5831 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005832 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833
NeilBrown57afd892005-06-21 17:17:13 -07005834 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005835 if (j >= mddev->resync_max) {
5836 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5837 wait_event(mddev->recovery_wait,
5838 mddev->resync_max > j
5839 || kthread_should_stop());
5840 }
5841 if (kthread_should_stop())
5842 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005843 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005844 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005845 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005846 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 goto out;
5848 }
NeilBrown57afd892005-06-21 17:17:13 -07005849
5850 if (!skipped) { /* actual IO requested */
5851 io_sectors += sectors;
5852 atomic_add(sectors, &mddev->recovery_active);
5853 }
5854
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 j += sectors;
5856 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005857 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005858 if (last_check == 0)
5859 /* this is the earliers that rebuilt will be
5860 * visible in /proc/mdstat
5861 */
5862 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005863
5864 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005865 continue;
5866
NeilBrown57afd892005-06-21 17:17:13 -07005867 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868
NeilBrowndfc70642008-05-23 13:04:39 -07005869 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 break;
5871
5872 repeat:
5873 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5874 /* step marks */
5875 int next = (last_mark+1) % SYNC_MARKS;
5876
5877 mddev->resync_mark = mark[next];
5878 mddev->resync_mark_cnt = mark_cnt[next];
5879 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005880 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 last_mark = next;
5882 }
5883
5884
NeilBrownc6207272008-02-06 01:39:52 -08005885 if (kthread_should_stop())
5886 goto interrupted;
5887
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888
5889 /*
5890 * this loop exits only if either when we are slower than
5891 * the 'hard' speed limit, or the system was IO-idle for
5892 * a jiffy.
5893 * the system might be non-idle CPU-wise, but we only care
5894 * about not overloading the IO subsystem. (things like an
5895 * e2fsck being done on the RAID array should execute fast)
5896 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005897 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 cond_resched();
5899
NeilBrown57afd892005-06-21 17:17:13 -07005900 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5901 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
NeilBrown88202a02006-01-06 00:21:36 -08005903 if (currspeed > speed_min(mddev)) {
5904 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005906 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 goto repeat;
5908 }
5909 }
5910 }
NeilBrown61df9d92006-10-03 01:15:57 -07005911 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 /*
5913 * this also signals 'finished resyncing' to md_stop
5914 */
5915 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005916 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
5918 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5919
5920 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005921 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922
NeilBrowndfc70642008-05-23 13:04:39 -07005923 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005924 mddev->curr_resync > 2) {
5925 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5926 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5927 if (mddev->curr_resync >= mddev->recovery_cp) {
5928 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005929 "md: checkpointing %s of %s.\n",
5930 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005931 mddev->recovery_cp = mddev->curr_resync;
5932 }
5933 } else
5934 mddev->recovery_cp = MaxSector;
5935 } else {
5936 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5937 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005938 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005939 if (rdev->raid_disk >= 0 &&
5940 !test_bit(Faulty, &rdev->flags) &&
5941 !test_bit(In_sync, &rdev->flags) &&
5942 rdev->recovery_offset < mddev->curr_resync)
5943 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 }
NeilBrown17571282006-12-10 02:20:52 -08005946 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 skip:
5949 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005950 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005951 mddev->resync_max = MaxSector;
5952 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 wake_up(&resync_wait);
5954 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5955 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005956 return;
5957
5958 interrupted:
5959 /*
5960 * got a signal, exit.
5961 */
5962 printk(KERN_INFO
5963 "md: md_do_sync() got signal ... exiting\n");
5964 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5965 goto out;
5966
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967}
NeilBrown29269552006-03-27 01:18:10 -08005968EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
5970
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005971static int remove_and_add_spares(mddev_t *mddev)
5972{
5973 mdk_rdev_t *rdev;
5974 struct list_head *rtmp;
5975 int spares = 0;
5976
NeilBrownd089c6a2008-02-06 01:39:59 -08005977 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005978 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005979 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005980 (test_bit(Faulty, &rdev->flags) ||
5981 ! test_bit(In_sync, &rdev->flags)) &&
5982 atomic_read(&rdev->nr_pending)==0) {
5983 if (mddev->pers->hot_remove_disk(
5984 mddev, rdev->raid_disk)==0) {
5985 char nm[20];
5986 sprintf(nm,"rd%d", rdev->raid_disk);
5987 sysfs_remove_link(&mddev->kobj, nm);
5988 rdev->raid_disk = -1;
5989 }
5990 }
5991
NeilBrownc89a8ee2008-08-05 15:54:13 +10005992 if (mddev->degraded && ! mddev->ro) {
NeilBrowndfc70642008-05-23 13:04:39 -07005993 rdev_for_each(rdev, rtmp, mddev) {
5994 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07005995 !test_bit(In_sync, &rdev->flags) &&
5996 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07005997 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005998 if (rdev->raid_disk < 0
5999 && !test_bit(Faulty, &rdev->flags)) {
6000 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006001 if (mddev->pers->
6002 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006003 char nm[20];
6004 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006005 if (sysfs_create_link(&mddev->kobj,
6006 &rdev->kobj, nm))
6007 printk(KERN_WARNING
6008 "md: cannot register "
6009 "%s for %s\n",
6010 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006011 spares++;
6012 md_new_event(mddev);
6013 } else
6014 break;
6015 }
NeilBrowndfc70642008-05-23 13:04:39 -07006016 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006017 }
6018 return spares;
6019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020/*
6021 * This routine is regularly called by all per-raid-array threads to
6022 * deal with generic issues like resync and super-block update.
6023 * Raid personalities that don't have a thread (linear/raid0) do not
6024 * need this as they never do any recovery or update the superblock.
6025 *
6026 * It does not do any resync itself, but rather "forks" off other threads
6027 * to do that as needed.
6028 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6029 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006030 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 * and wakeups up this thread which will reap the thread and finish up.
6032 * This thread also removes any faulty devices (with nr_pending == 0).
6033 *
6034 * The overall approach is:
6035 * 1/ if the superblock needs updating, update it.
6036 * 2/ If a recovery thread is running, don't do anything else.
6037 * 3/ If recovery has finished, clean up, possibly marking spares active.
6038 * 4/ If there are any faulty devices, remove them.
6039 * 5/ If array is degraded, try to add spares devices
6040 * 6/ If array has spares or is not in-sync, start a resync thread.
6041 */
6042void md_check_recovery(mddev_t *mddev)
6043{
6044 mdk_rdev_t *rdev;
6045 struct list_head *rtmp;
6046
6047
NeilBrown5f404022005-06-21 17:17:16 -07006048 if (mddev->bitmap)
6049 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050
Dan Williamsd8e64402008-07-23 13:09:48 -07006051 if (test_and_clear_bit(MD_NOTIFY_ARRAY_STATE, &mddev->flags))
6052 sysfs_notify(&mddev->kobj, NULL, "array_state");
6053
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 if (mddev->ro)
6055 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006056
6057 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006058 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006059 printk(KERN_INFO "md: %s in immediate safe mode\n",
6060 mdname(mddev));
6061 mddev->safemode = 2;
6062 }
6063 flush_signals(current);
6064 }
6065
NeilBrownc89a8ee2008-08-05 15:54:13 +10006066 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006069 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006071 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006072 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006073 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6074 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 ))
6076 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006077
NeilBrowndf5b89b2006-03-27 01:18:20 -08006078 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006079 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006080
NeilBrownc89a8ee2008-08-05 15:54:13 +10006081 if (mddev->ro) {
6082 /* Only thing we do on a ro array is remove
6083 * failed devices.
6084 */
6085 remove_and_add_spares(mddev);
6086 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6087 goto unlock;
6088 }
6089
NeilBrown31a59e32008-04-30 00:52:30 -07006090 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006091 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006092 spin_lock_irq(&mddev->write_lock);
6093 if (mddev->safemode &&
6094 !atomic_read(&mddev->writes_pending) &&
6095 !mddev->in_sync &&
6096 mddev->recovery_cp == MaxSector) {
6097 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006098 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006099 if (mddev->persistent)
6100 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6101 }
6102 if (mddev->safemode == 1)
6103 mddev->safemode = 0;
6104 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006105 if (did_change)
6106 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrownfca4d842005-06-21 17:17:11 -07006107 }
NeilBrownfca4d842005-06-21 17:17:11 -07006108
NeilBrown850b2b42006-10-03 01:15:46 -07006109 if (mddev->flags)
6110 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006111
Neil Brown52664732008-06-28 08:31:44 +10006112 rdev_for_each(rdev, rtmp, mddev)
6113 if (test_and_clear_bit(StateChanged, &rdev->flags))
6114 sysfs_notify(&rdev->kobj, NULL, "state");
6115
NeilBrown06d91a52005-06-21 17:17:12 -07006116
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6118 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6119 /* resync/recovery still happening */
6120 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6121 goto unlock;
6122 }
6123 if (mddev->sync_thread) {
6124 /* resync has finished, collect result */
6125 md_unregister_thread(mddev->sync_thread);
6126 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006127 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6128 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 /* success...*/
6130 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006131 if (mddev->pers->spare_active(mddev))
6132 sysfs_notify(&mddev->kobj, NULL,
6133 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 }
NeilBrown850b2b42006-10-03 01:15:46 -07006135 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006136
6137 /* if array is no-longer degraded, then any saved_raid_disk
6138 * information must be scrapped
6139 */
6140 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006141 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006142 rdev->saved_raid_disk = -1;
6143
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144 mddev->recovery = 0;
6145 /* flag recovery needed just to double check */
6146 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006147 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006148 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006149 goto unlock;
6150 }
Neil Brown72a23c22008-06-28 08:31:41 +10006151 /* Set RUNNING before clearing NEEDED to avoid
6152 * any transients in the value of "sync_action".
6153 */
6154 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6155 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006156 /* Clear some bits that don't mean anything, but
6157 * might be left set
6158 */
NeilBrown24dd4692005-11-08 21:39:26 -08006159 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6160 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006162 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6163 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 /* no recovery is running.
6165 * remove any failed drives, then
6166 * add spares if possible.
6167 * Spare are also removed and re-added, to allow
6168 * the personality to fail the re-add.
6169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006171 if (mddev->reshape_position != MaxSector) {
6172 if (mddev->pers->check_reshape(mddev) != 0)
6173 /* Cannot proceed */
6174 goto unlock;
6175 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006176 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006177 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006178 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6179 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006180 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006181 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006182 } else if (mddev->recovery_cp < MaxSector) {
6183 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006184 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006185 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6186 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006188
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006190 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6191 /* We are adding a device or devices to an array
6192 * which has the bitmap stored on all devices.
6193 * So make sure all bitmap pages get written
6194 */
6195 bitmap_write_all(mddev->bitmap);
6196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 mddev->sync_thread = md_register_thread(md_do_sync,
6198 mddev,
6199 "%s_resync");
6200 if (!mddev->sync_thread) {
6201 printk(KERN_ERR "%s: could not start resync"
6202 " thread...\n",
6203 mdname(mddev));
6204 /* leave the spares where they are, it shouldn't hurt */
6205 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006206 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 md_wakeup_thread(mddev->sync_thread);
Neil Brown72a23c22008-06-28 08:31:41 +10006208 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006209 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 }
6211 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006212 if (!mddev->sync_thread) {
6213 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6214 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6215 &mddev->recovery))
6216 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 mddev_unlock(mddev);
6219 }
6220}
6221
Dan Williams6bfe0b42008-04-30 00:52:32 -07006222void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6223{
6224 sysfs_notify(&rdev->kobj, NULL, "state");
6225 wait_event_timeout(rdev->blocked_wait,
6226 !test_bit(Blocked, &rdev->flags),
6227 msecs_to_jiffies(5000));
6228 rdev_dec_pending(rdev, mddev);
6229}
6230EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6231
Adrian Bunk75c96f82005-05-05 16:16:09 -07006232static int md_notify_reboot(struct notifier_block *this,
6233 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234{
6235 struct list_head *tmp;
6236 mddev_t *mddev;
6237
6238 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6239
6240 printk(KERN_INFO "md: stopping all md devices.\n");
6241
NeilBrown29ac4aa2008-02-06 01:39:58 -08006242 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006243 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006244 /* Force a switch to readonly even array
6245 * appears to still be in use. Hence
6246 * the '100'.
6247 */
NeilBrownd710e132008-10-13 11:55:12 +11006248 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006249 mddev_unlock(mddev);
6250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 /*
6252 * certain more exotic SCSI devices are known to be
6253 * volatile wrt too early system reboots. While the
6254 * right place to handle this issue is the given
6255 * driver, we do want to have a safe RAID driver ...
6256 */
6257 mdelay(1000*1);
6258 }
6259 return NOTIFY_DONE;
6260}
6261
Adrian Bunk75c96f82005-05-05 16:16:09 -07006262static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 .notifier_call = md_notify_reboot,
6264 .next = NULL,
6265 .priority = INT_MAX, /* before any real devices */
6266};
6267
6268static void md_geninit(void)
6269{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6271
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006272 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273}
6274
Adrian Bunk75c96f82005-05-05 16:16:09 -07006275static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 if (register_blkdev(MAJOR_NR, "md"))
6278 return -1;
6279 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6280 unregister_blkdev(MAJOR_NR, "md");
6281 return -1;
6282 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006283 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6284 md_probe, NULL, NULL);
6285 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 md_probe, NULL, NULL);
6287
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006289 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
6291 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293}
6294
6295
6296#ifndef MODULE
6297
6298/*
6299 * Searches all registered partitions for autorun RAID arrays
6300 * at boot time.
6301 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006302
6303static LIST_HEAD(all_detected_devices);
6304struct detected_devices_node {
6305 struct list_head list;
6306 dev_t dev;
6307};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006308
6309void md_autodetect_dev(dev_t dev)
6310{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006311 struct detected_devices_node *node_detected_dev;
6312
6313 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6314 if (node_detected_dev) {
6315 node_detected_dev->dev = dev;
6316 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6317 } else {
6318 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6319 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321}
6322
6323
6324static void autostart_arrays(int part)
6325{
6326 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006327 struct detected_devices_node *node_detected_dev;
6328 dev_t dev;
6329 int i_scanned, i_passed;
6330
6331 i_scanned = 0;
6332 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
6334 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6335
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006336 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6337 i_scanned++;
6338 node_detected_dev = list_entry(all_detected_devices.next,
6339 struct detected_devices_node, list);
6340 list_del(&node_detected_dev->list);
6341 dev = node_detected_dev->dev;
6342 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006343 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 if (IS_ERR(rdev))
6345 continue;
6346
NeilBrownb2d444d2005-11-08 21:39:31 -08006347 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 MD_BUG();
6349 continue;
6350 }
NeilBrownd0fae182008-03-04 14:29:31 -08006351 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006353 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006355
6356 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6357 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
6359 autorun_devices(part);
6360}
6361
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006362#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
6364static __exit void md_exit(void)
6365{
6366 mddev_t *mddev;
6367 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006368
NeilBrowne8703fe2006-10-03 01:15:59 -07006369 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6370 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 unregister_blkdev(MAJOR_NR,"md");
6373 unregister_blkdev(mdp_major, "mdp");
6374 unregister_reboot_notifier(&md_notifier);
6375 unregister_sysctl_table(raid_table_header);
6376 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006377 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378 struct gendisk *disk = mddev->gendisk;
6379 if (!disk)
6380 continue;
6381 export_array(mddev);
6382 del_gendisk(disk);
6383 put_disk(disk);
6384 mddev->gendisk = NULL;
6385 mddev_put(mddev);
6386 }
6387}
6388
Dan Williams685784a2007-07-09 11:56:42 -07006389subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390module_exit(md_exit)
6391
NeilBrownf91de922005-11-08 21:39:36 -08006392static int get_ro(char *buffer, struct kernel_param *kp)
6393{
6394 return sprintf(buffer, "%d", start_readonly);
6395}
6396static int set_ro(const char *val, struct kernel_param *kp)
6397{
6398 char *e;
6399 int num = simple_strtoul(val, &e, 10);
6400 if (*val && (*e == '\0' || *e == '\n')) {
6401 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006402 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006403 }
6404 return -EINVAL;
6405}
6406
NeilBrown80ca3a42006-07-10 04:44:18 -07006407module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6408module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006409
NeilBrownf91de922005-11-08 21:39:36 -08006410
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411EXPORT_SYMBOL(register_md_personality);
6412EXPORT_SYMBOL(unregister_md_personality);
6413EXPORT_SYMBOL(md_error);
6414EXPORT_SYMBOL(md_done_sync);
6415EXPORT_SYMBOL(md_write_start);
6416EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417EXPORT_SYMBOL(md_register_thread);
6418EXPORT_SYMBOL(md_unregister_thread);
6419EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420EXPORT_SYMBOL(md_check_recovery);
6421MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006422MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006423MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);