blob: 4c4c79da72b97ca8fb787b0e3e8bfef3841bbb85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
35#include <linux/module.h>
Ahmed S. Darwish50511da2007-05-09 02:35:34 -070036#include <linux/kernel.h>
NeilBrowna6fb0932005-09-09 16:23:56 -070037#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/linkage.h>
39#include <linux/raid/md.h>
NeilBrown32a76272005-06-21 17:17:14 -070040#include <linux/raid/bitmap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080043#include <linux/poll.h>
Arjan van de Ven48c9c272006-03-27 01:18:20 -080044#include <linux/mutex.h>
NeilBrown16f17b32006-06-26 00:27:37 -070045#include <linux/ctype.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080046#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <linux/init.h>
49
NeilBrown32a76272005-06-21 17:17:14 -070050#include <linux/file.h>
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#ifdef CONFIG_KMOD
53#include <linux/kmod.h>
54#endif
55
56#include <asm/unaligned.h>
57
58#define MAJOR_NR MD_MAJOR
59#define MD_DRIVER
60
61/* 63 partitions with the alternate major number (mdp) */
62#define MdpMinorShift 6
63
64#define DEBUG 0
65#define dprintk(x...) ((void)(DEBUG && printk(x)))
66
67
68#ifndef MODULE
69static void autostart_arrays (int part);
70#endif
71
NeilBrown2604b702006-01-06 00:20:36 -080072static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static DEFINE_SPINLOCK(pers_lock);
74
Adrian Bunk5e563412006-06-26 00:27:42 -070075static void md_print_devices(void);
76
Bernd Schubert90b08712008-05-23 13:04:38 -070077static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
78
Adrian Bunk5e563412006-06-26 00:27:42 -070079#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
82 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
83 * is 1000 KB/sec, so the extra system load does not show up that much.
84 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070085 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * subsystem is idle. There is also an 'absolute maximum' reconstruction
87 * speed limit - in case reconstruction slows down your system despite
88 * idle IO detection.
89 *
90 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080091 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93
94static int sysctl_speed_limit_min = 1000;
95static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080096static inline int speed_min(mddev_t *mddev)
97{
98 return mddev->sync_speed_min ?
99 mddev->sync_speed_min : sysctl_speed_limit_min;
100}
101
102static inline int speed_max(mddev_t *mddev)
103{
104 return mddev->sync_speed_max ?
105 mddev->sync_speed_max : sysctl_speed_limit_max;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108static struct ctl_table_header *raid_table_header;
109
110static ctl_table raid_table[] = {
111 {
112 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
113 .procname = "speed_limit_min",
114 .data = &sysctl_speed_limit_min,
115 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700116 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .proc_handler = &proc_dointvec,
118 },
119 {
120 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
121 .procname = "speed_limit_max",
122 .data = &sysctl_speed_limit_max,
123 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .proc_handler = &proc_dointvec,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_dir_table[] = {
131 {
132 .ctl_name = DEV_RAID,
133 .procname = "raid",
134 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700135 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 .child = raid_table,
137 },
138 { .ctl_name = 0 }
139};
140
141static ctl_table raid_root_table[] = {
142 {
143 .ctl_name = CTL_DEV,
144 .procname = "dev",
145 .maxlen = 0,
146 .mode = 0555,
147 .child = raid_dir_table,
148 },
149 { .ctl_name = 0 }
150};
151
152static struct block_device_operations md_fops;
153
NeilBrownf91de922005-11-08 21:39:36 -0800154static int start_readonly;
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/*
NeilBrownd7603b72006-01-06 00:20:30 -0800157 * We have a system wide 'event count' that is incremented
158 * on any 'interesting' event, and readers of /proc/mdstat
159 * can use 'poll' or 'select' to find out when the event
160 * count increases.
161 *
162 * Events are:
163 * start array, stop array, error, add device, remove device,
164 * start build, activate spare
165 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800166static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800167static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800168void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800169{
170 atomic_inc(&md_event_count);
171 wake_up(&md_event_waiters);
172}
NeilBrown29269552006-03-27 01:18:10 -0800173EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800174
NeilBrownc331eb02006-05-30 21:27:13 -0700175/* Alternate version that can be called from interrupts
176 * when calling sysfs_notify isn't needed.
177 */
Adrian Bunk05381952006-06-26 00:28:01 -0700178static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700179{
180 atomic_inc(&md_event_count);
181 wake_up(&md_event_waiters);
182}
183
NeilBrownd7603b72006-01-06 00:20:30 -0800184/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * Enables to iterate over all existing md arrays
186 * all_mddevs_lock protects this list.
187 */
188static LIST_HEAD(all_mddevs);
189static DEFINE_SPINLOCK(all_mddevs_lock);
190
191
192/*
193 * iterates through all used mddevs in the system.
194 * We take care to grab the all_mddevs_lock whenever navigating
195 * the list, and to always hold a refcount when unlocked.
196 * Any code which breaks out of this loop while own
197 * a reference to the current mddev and must mddev_put it.
198 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800199#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 \
201 for (({ spin_lock(&all_mddevs_lock); \
202 tmp = all_mddevs.next; \
203 mddev = NULL;}); \
204 ({ if (tmp != &all_mddevs) \
205 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
206 spin_unlock(&all_mddevs_lock); \
207 if (mddev) mddev_put(mddev); \
208 mddev = list_entry(tmp, mddev_t, all_mddevs); \
209 tmp != &all_mddevs;}); \
210 ({ spin_lock(&all_mddevs_lock); \
211 tmp = tmp->next;}) \
212 )
213
214
Jens Axboe165125e2007-07-24 09:28:11 +0200215static int md_fail_request (struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
NeilBrown6712ecf2007-09-27 12:47:43 +0200217 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219}
220
221static inline mddev_t *mddev_get(mddev_t *mddev)
222{
223 atomic_inc(&mddev->active);
224 return mddev;
225}
226
227static void mddev_put(mddev_t *mddev)
228{
229 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
230 return;
231 if (!mddev->raid_disks && list_empty(&mddev->disks)) {
232 list_del(&mddev->all_mddevs);
NeilBrown926ce2d2006-03-31 02:32:02 -0800233 spin_unlock(&all_mddevs_lock);
Al Viro1312f402006-03-12 11:02:03 -0500234 blk_cleanup_queue(mddev->queue);
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -0800235 kobject_put(&mddev->kobj);
NeilBrown926ce2d2006-03-31 02:32:02 -0800236 } else
237 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static mddev_t * mddev_find(dev_t unit)
241{
242 mddev_t *mddev, *new = NULL;
243
244 retry:
245 spin_lock(&all_mddevs_lock);
246 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
247 if (mddev->unit == unit) {
248 mddev_get(mddev);
249 spin_unlock(&all_mddevs_lock);
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700250 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return mddev;
252 }
253
254 if (new) {
255 list_add(&new->all_mddevs, &all_mddevs);
256 spin_unlock(&all_mddevs_lock);
257 return new;
258 }
259 spin_unlock(&all_mddevs_lock);
260
NeilBrown9ffae0c2006-01-06 00:20:32 -0800261 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!new)
263 return NULL;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 new->unit = unit;
266 if (MAJOR(unit) == MD_MAJOR)
267 new->md_minor = MINOR(unit);
268 else
269 new->md_minor = MINOR(unit) >> MdpMinorShift;
270
NeilBrowndf5b89b2006-03-27 01:18:20 -0800271 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 INIT_LIST_HEAD(&new->disks);
273 INIT_LIST_HEAD(&new->all_mddevs);
274 init_timer(&new->safemode_timer);
275 atomic_set(&new->active, 1);
NeilBrown06d91a52005-06-21 17:17:12 -0700276 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700277 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700278 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700279 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000280 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800281 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700282 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 new->queue = blk_alloc_queue(GFP_KERNEL);
285 if (!new->queue) {
286 kfree(new);
287 return NULL;
288 }
Nick Piggin75ad23b2008-04-29 14:48:33 +0200289 /* Can be unlocked because the queue is new: no concurrency */
290 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, new->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 blk_queue_make_request(new->queue, md_fail_request);
293
294 goto retry;
295}
296
297static inline int mddev_lock(mddev_t * mddev)
298{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800299 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static inline int mddev_trylock(mddev_t * mddev)
303{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800304 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307static inline void mddev_unlock(mddev_t * mddev)
308{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800309 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
NeilBrown005eca52005-08-22 13:11:08 -0700311 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
NeilBrown2989ddb2006-01-06 00:20:43 -0800314static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 mdk_rdev_t * rdev;
317 struct list_head *tmp;
318
NeilBrownd089c6a2008-02-06 01:39:59 -0800319 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (rdev->desc_nr == nr)
321 return rdev;
322 }
323 return NULL;
324}
325
326static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
327{
328 struct list_head *tmp;
329 mdk_rdev_t *rdev;
330
NeilBrownd089c6a2008-02-06 01:39:59 -0800331 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (rdev->bdev->bd_dev == dev)
333 return rdev;
334 }
335 return NULL;
336}
337
NeilBrownd9d166c2006-01-06 00:20:51 -0800338static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800339{
340 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800341 list_for_each_entry(pers, &pers_list, list) {
342 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800343 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800344 if (strcmp(pers->name, clevel)==0)
345 return pers;
346 }
NeilBrown2604b702006-01-06 00:20:36 -0800347 return NULL;
348}
349
Jesper Juhl77933d72005-07-27 11:46:09 -0700350static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 sector_t size = bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
353 return MD_NEW_SIZE_BLOCKS(size);
354}
355
356static sector_t calc_dev_size(mdk_rdev_t *rdev, unsigned chunk_size)
357{
358 sector_t size;
359
360 size = rdev->sb_offset;
361
362 if (chunk_size)
363 size &= ~((sector_t)chunk_size/1024 - 1);
364 return size;
365}
366
367static int alloc_disk_sb(mdk_rdev_t * rdev)
368{
369 if (rdev->sb_page)
370 MD_BUG();
371
372 rdev->sb_page = alloc_page(GFP_KERNEL);
373 if (!rdev->sb_page) {
374 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000375 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377
378 return 0;
379}
380
381static void free_disk_sb(mdk_rdev_t * rdev)
382{
383 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800384 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 rdev->sb_loaded = 0;
386 rdev->sb_page = NULL;
387 rdev->sb_offset = 0;
388 rdev->size = 0;
389 }
390}
391
392
NeilBrown6712ecf2007-09-27 12:47:43 +0200393static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700394{
395 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800396 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700397
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700398 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
399 printk("md: super_written gets error=%d, uptodate=%d\n",
400 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
401 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800402 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700403 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700404
NeilBrowna9701a32005-11-08 21:39:34 -0800405 if (atomic_dec_and_test(&mddev->pending_writes))
406 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700407 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700408}
409
NeilBrown6712ecf2007-09-27 12:47:43 +0200410static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800411{
412 struct bio *bio2 = bio->bi_private;
413 mdk_rdev_t *rdev = bio2->bi_private;
414 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800415
416 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
417 error == -EOPNOTSUPP) {
418 unsigned long flags;
419 /* barriers don't appear to be supported :-( */
420 set_bit(BarriersNotsupp, &rdev->flags);
421 mddev->barriers_work = 0;
422 spin_lock_irqsave(&mddev->write_lock, flags);
423 bio2->bi_next = mddev->biolist;
424 mddev->biolist = bio2;
425 spin_unlock_irqrestore(&mddev->write_lock, flags);
426 wake_up(&mddev->sb_wait);
427 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200428 } else {
429 bio_put(bio2);
430 bio->bi_private = rdev;
431 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800432 }
NeilBrowna9701a32005-11-08 21:39:34 -0800433}
434
NeilBrown7bfa19f2005-06-21 17:17:28 -0700435void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
436 sector_t sector, int size, struct page *page)
437{
438 /* write first size bytes of page to sector of rdev
439 * Increment mddev->pending_writes before returning
440 * and decrement it on completion, waking up sb_wait
441 * if zero is reached.
442 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800443 *
444 * As we might need to resubmit the request if BIO_RW_BARRIER
445 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700446 */
447 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800448 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700449
450 bio->bi_bdev = rdev->bdev;
451 bio->bi_sector = sector;
452 bio_add_page(bio, page, size, 0);
453 bio->bi_private = rdev;
454 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800455 bio->bi_rw = rw;
456
NeilBrown7bfa19f2005-06-21 17:17:28 -0700457 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800458 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
459 struct bio *rbio;
460 rw |= (1<<BIO_RW_BARRIER);
461 rbio = bio_clone(bio, GFP_NOIO);
462 rbio->bi_private = bio;
463 rbio->bi_end_io = super_written_barrier;
464 submit_bio(rw, rbio);
465 } else
466 submit_bio(rw, bio);
467}
468
469void md_super_wait(mddev_t *mddev)
470{
471 /* wait for all superblock writes that were scheduled to complete.
472 * if any had to be retried (due to BARRIER problems), retry them
473 */
474 DEFINE_WAIT(wq);
475 for(;;) {
476 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
477 if (atomic_read(&mddev->pending_writes)==0)
478 break;
479 while (mddev->biolist) {
480 struct bio *bio;
481 spin_lock_irq(&mddev->write_lock);
482 bio = mddev->biolist;
483 mddev->biolist = bio->bi_next ;
484 bio->bi_next = NULL;
485 spin_unlock_irq(&mddev->write_lock);
486 submit_bio(bio->bi_rw, bio);
487 }
488 schedule();
489 }
490 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700491}
492
NeilBrown6712ecf2007-09-27 12:47:43 +0200493static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
NeilBrowna654b9d82005-06-21 17:17:27 -0700498int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 struct page *page, int rw)
500{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700501 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 struct completion event;
503 int ret;
504
505 rw |= (1 << BIO_RW_SYNC);
506
507 bio->bi_bdev = bdev;
508 bio->bi_sector = sector;
509 bio_add_page(bio, page, size, 0);
510 init_completion(&event);
511 bio->bi_private = &event;
512 bio->bi_end_io = bi_complete;
513 submit_bio(rw, bio);
514 wait_for_completion(&event);
515
516 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
517 bio_put(bio);
518 return ret;
519}
NeilBrowna8745db2006-01-06 00:20:34 -0800520EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
NeilBrown0002b272005-09-09 16:23:53 -0700522static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
524 char b[BDEVNAME_SIZE];
525 if (!rdev->sb_page) {
526 MD_BUG();
527 return -EINVAL;
528 }
529 if (rdev->sb_loaded)
530 return 0;
531
532
NeilBrown0002b272005-09-09 16:23:53 -0700533 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto fail;
535 rdev->sb_loaded = 1;
536 return 0;
537
538fail:
539 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
540 bdevname(rdev->bdev,b));
541 return -EINVAL;
542}
543
544static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
545{
Andre Noll05710462008-07-11 22:02:20 +1000546 return sb1->set_uuid0 == sb2->set_uuid0 &&
547 sb1->set_uuid1 == sb2->set_uuid1 &&
548 sb1->set_uuid2 == sb2->set_uuid2 &&
549 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
553{
554 int ret;
555 mdp_super_t *tmp1, *tmp2;
556
557 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
558 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
559
560 if (!tmp1 || !tmp2) {
561 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100562 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto abort;
564 }
565
566 *tmp1 = *sb1;
567 *tmp2 = *sb2;
568
569 /*
570 * nr_disks is not constant
571 */
572 tmp1->nr_disks = 0;
573 tmp2->nr_disks = 0;
574
Andre Nollce0c8e02008-07-11 22:02:20 +1000575 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700577 kfree(tmp1);
578 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return ret;
580}
581
NeilBrown4d167f02007-05-09 02:35:37 -0700582
583static u32 md_csum_fold(u32 csum)
584{
585 csum = (csum & 0xffff) + (csum >> 16);
586 return (csum & 0xffff) + (csum >> 16);
587}
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589static unsigned int calc_sb_csum(mdp_super_t * sb)
590{
NeilBrown4d167f02007-05-09 02:35:37 -0700591 u64 newcsum = 0;
592 u32 *sb32 = (u32*)sb;
593 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 unsigned int disk_csum, csum;
595
596 disk_csum = sb->sb_csum;
597 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700598
599 for (i = 0; i < MD_SB_BYTES/4 ; i++)
600 newcsum += sb32[i];
601 csum = (newcsum & 0xffffffff) + (newcsum>>32);
602
603
604#ifdef CONFIG_ALPHA
605 /* This used to use csum_partial, which was wrong for several
606 * reasons including that different results are returned on
607 * different architectures. It isn't critical that we get exactly
608 * the same return value as before (we always csum_fold before
609 * testing, and that removes any differences). However as we
610 * know that csum_partial always returned a 16bit value on
611 * alphas, do a fold to maximise conformity to previous behaviour.
612 */
613 sb->sb_csum = md_csum_fold(disk_csum);
614#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700616#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return csum;
618}
619
620
621/*
622 * Handle superblock details.
623 * We want to be able to handle multiple superblock formats
624 * so we have a common interface to them all, and an array of
625 * different handlers.
626 * We rely on user-space to write the initial superblock, and support
627 * reading and updating of superblocks.
628 * Interface methods are:
629 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
630 * loads and validates a superblock on dev.
631 * if refdev != NULL, compare superblocks on both devices
632 * Return:
633 * 0 - dev has a superblock that is compatible with refdev
634 * 1 - dev has a superblock that is compatible and newer than refdev
635 * so dev should be used as the refdev in future
636 * -EINVAL superblock incompatible or invalid
637 * -othererror e.g. -EIO
638 *
639 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
640 * Verify that dev is acceptable into mddev.
641 * The first time, mddev->raid_disks will be 0, and data from
642 * dev should be merged in. Subsequent calls check that dev
643 * is new enough. Return 0 or -EINVAL
644 *
645 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
646 * Update the superblock for rdev with data in mddev
647 * This does not write to disc.
648 *
649 */
650
651struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000652 char *name;
653 struct module *owner;
654 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
655 int minor_version);
656 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
657 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
658 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
659 unsigned long long size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660};
661
662/*
663 * load_super for 0.90.0
664 */
665static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
666{
667 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
668 mdp_super_t *sb;
669 int ret;
670 sector_t sb_offset;
671
672 /*
673 * Calculate the position of the superblock,
674 * it's at the end of the disk.
675 *
676 * It also happens to be a multiple of 4Kb.
677 */
678 sb_offset = calc_dev_sboffset(rdev->bdev);
679 rdev->sb_offset = sb_offset;
680
NeilBrown0002b272005-09-09 16:23:53 -0700681 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (ret) return ret;
683
684 ret = -EINVAL;
685
686 bdevname(rdev->bdev, b);
687 sb = (mdp_super_t*)page_address(rdev->sb_page);
688
689 if (sb->md_magic != MD_SB_MAGIC) {
690 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
691 b);
692 goto abort;
693 }
694
695 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800696 sb->minor_version < 90 ||
697 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
699 sb->major_version, sb->minor_version,
700 b);
701 goto abort;
702 }
703
704 if (sb->raid_disks <= 0)
705 goto abort;
706
NeilBrown4d167f02007-05-09 02:35:37 -0700707 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
709 b);
710 goto abort;
711 }
712
713 rdev->preferred_minor = sb->md_minor;
714 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700715 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
NeilBrowne11e93f2007-05-09 02:35:36 -0700717 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
718 if (sb->level != 1 && sb->level != 4
719 && sb->level != 5 && sb->level != 6
720 && sb->level != 10) {
721 /* FIXME use a better test */
722 printk(KERN_WARNING
723 "md: bitmaps not supported for this level.\n");
724 goto abort;
725 }
726 }
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (sb->level == LEVEL_MULTIPATH)
729 rdev->desc_nr = -1;
730 else
731 rdev->desc_nr = sb->this_disk.number;
732
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700733 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700735 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 __u64 ev1, ev2;
737 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
738 if (!uuid_equal(refsb, sb)) {
739 printk(KERN_WARNING "md: %s has different UUID to %s\n",
740 b, bdevname(refdev->bdev,b2));
741 goto abort;
742 }
743 if (!sb_equal(refsb, sb)) {
744 printk(KERN_WARNING "md: %s has same UUID"
745 " but different superblock to %s\n",
746 b, bdevname(refdev->bdev, b2));
747 goto abort;
748 }
749 ev1 = md_event(sb);
750 ev2 = md_event(refsb);
751 if (ev1 > ev2)
752 ret = 1;
753 else
754 ret = 0;
755 }
756 rdev->size = calc_dev_size(rdev, sb->chunk_size);
757
NeilBrown2bf071b2006-01-06 00:20:55 -0800758 if (rdev->size < sb->size && sb->level > 1)
759 /* "this cannot possibly happen" ... */
760 ret = -EINVAL;
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 abort:
763 return ret;
764}
765
766/*
767 * validate_super for 0.90.0
768 */
769static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
770{
771 mdp_disk_t *desc;
772 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700773 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
NeilBrown41158c72005-06-21 17:17:25 -0700775 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800776 clear_bit(Faulty, &rdev->flags);
777 clear_bit(In_sync, &rdev->flags);
778 clear_bit(WriteMostly, &rdev->flags);
779 clear_bit(BarriersNotsupp, &rdev->flags);
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (mddev->raid_disks == 0) {
782 mddev->major_version = 0;
783 mddev->minor_version = sb->minor_version;
784 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800785 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 mddev->chunk_size = sb->chunk_size;
787 mddev->ctime = sb->ctime;
788 mddev->utime = sb->utime;
789 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800790 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 mddev->layout = sb->layout;
792 mddev->raid_disks = sb->raid_disks;
793 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700794 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700795 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700796 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
NeilBrownf6705572006-03-27 01:18:11 -0800798 if (mddev->minor_version >= 91) {
799 mddev->reshape_position = sb->reshape_position;
800 mddev->delta_disks = sb->delta_disks;
801 mddev->new_level = sb->new_level;
802 mddev->new_layout = sb->new_layout;
803 mddev->new_chunk = sb->new_chunk;
804 } else {
805 mddev->reshape_position = MaxSector;
806 mddev->delta_disks = 0;
807 mddev->new_level = mddev->level;
808 mddev->new_layout = mddev->layout;
809 mddev->new_chunk = mddev->chunk_size;
810 }
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (sb->state & (1<<MD_SB_CLEAN))
813 mddev->recovery_cp = MaxSector;
814 else {
815 if (sb->events_hi == sb->cp_events_hi &&
816 sb->events_lo == sb->cp_events_lo) {
817 mddev->recovery_cp = sb->recovery_cp;
818 } else
819 mddev->recovery_cp = 0;
820 }
821
822 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
823 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
824 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
825 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
826
827 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700828
829 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700830 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700831 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700832
NeilBrown41158c72005-06-21 17:17:25 -0700833 } else if (mddev->pers == NULL) {
834 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 ++ev1;
836 if (ev1 < mddev->events)
837 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700838 } else if (mddev->bitmap) {
839 /* if adding to array with a bitmap, then we can accept an
840 * older device ... but not too old.
841 */
NeilBrown41158c72005-06-21 17:17:25 -0700842 if (ev1 < mddev->bitmap->events_cleared)
843 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700844 } else {
845 if (ev1 < mddev->events)
846 /* just a hot-add of a new device, leave raid_disk at -1 */
847 return 0;
848 }
NeilBrown41158c72005-06-21 17:17:25 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 desc = sb->disks + rdev->desc_nr;
852
853 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800854 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700855 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
856 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800857 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 rdev->raid_disk = desc->raid_disk;
859 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700860 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
861 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700862 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800863 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return 0;
865}
866
867/*
868 * sync_super for 0.90.0
869 */
870static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
871{
872 mdp_super_t *sb;
873 struct list_head *tmp;
874 mdk_rdev_t *rdev2;
875 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /* make rdev->sb match mddev data..
879 *
880 * 1/ zero out disks
881 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
882 * 3/ any empty disks < next_spare become removed
883 *
884 * disks[0] gets initialised to REMOVED because
885 * we cannot be sure from other fields if it has
886 * been initialised or not.
887 */
888 int i;
889 int active=0, working=0,failed=0,spare=0,nr_disks=0;
890
NeilBrown61181562005-09-09 16:24:02 -0700891 rdev->sb_size = MD_SB_BYTES;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 sb = (mdp_super_t*)page_address(rdev->sb_page);
894
895 memset(sb, 0, sizeof(*sb));
896
897 sb->md_magic = MD_SB_MAGIC;
898 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 sb->patch_version = mddev->patch_version;
900 sb->gvalid_words = 0; /* ignored */
901 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
902 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
903 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
904 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
905
906 sb->ctime = mddev->ctime;
907 sb->level = mddev->level;
908 sb->size = mddev->size;
909 sb->raid_disks = mddev->raid_disks;
910 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800911 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 sb->utime = mddev->utime;
913 sb->state = 0;
914 sb->events_hi = (mddev->events>>32);
915 sb->events_lo = (u32)mddev->events;
916
NeilBrownf6705572006-03-27 01:18:11 -0800917 if (mddev->reshape_position == MaxSector)
918 sb->minor_version = 90;
919 else {
920 sb->minor_version = 91;
921 sb->reshape_position = mddev->reshape_position;
922 sb->new_level = mddev->new_level;
923 sb->delta_disks = mddev->delta_disks;
924 sb->new_layout = mddev->new_layout;
925 sb->new_chunk = mddev->new_chunk;
926 }
927 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (mddev->in_sync)
929 {
930 sb->recovery_cp = mddev->recovery_cp;
931 sb->cp_events_hi = (mddev->events>>32);
932 sb->cp_events_lo = (u32)mddev->events;
933 if (mddev->recovery_cp == MaxSector)
934 sb->state = (1<< MD_SB_CLEAN);
935 } else
936 sb->recovery_cp = 0;
937
938 sb->layout = mddev->layout;
939 sb->chunk_size = mddev->chunk_size;
940
NeilBrowna654b9d82005-06-21 17:17:27 -0700941 if (mddev->bitmap && mddev->bitmap_file == NULL)
942 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800945 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800947 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800948 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
949 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800950 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800952 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800953 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 d = &sb->disks[rdev2->desc_nr];
955 nr_disks++;
956 d->number = rdev2->desc_nr;
957 d->major = MAJOR(rdev2->bdev->bd_dev);
958 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800959 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
960 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 d->raid_disk = rdev2->raid_disk;
962 else
963 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800964 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800966 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 d->state = (1<<MD_DISK_ACTIVE);
968 d->state |= (1<<MD_DISK_SYNC);
969 active++;
970 working++;
971 } else {
972 d->state = 0;
973 spare++;
974 working++;
975 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700976 if (test_bit(WriteMostly, &rdev2->flags))
977 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* now set the "removed" and "faulty" bits on any missing devices */
980 for (i=0 ; i < mddev->raid_disks ; i++) {
981 mdp_disk_t *d = &sb->disks[i];
982 if (d->state == 0 && d->number == 0) {
983 d->number = i;
984 d->raid_disk = i;
985 d->state = (1<<MD_DISK_REMOVED);
986 d->state |= (1<<MD_DISK_FAULTY);
987 failed++;
988 }
989 }
990 sb->nr_disks = nr_disks;
991 sb->active_disks = active;
992 sb->working_disks = working;
993 sb->failed_disks = failed;
994 sb->spare_disks = spare;
995
996 sb->this_disk = sb->disks[rdev->desc_nr];
997 sb->sb_csum = calc_sb_csum(sb);
998}
999
1000/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001001 * rdev_size_change for 0.90.0
1002 */
1003static unsigned long long
1004super_90_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1005{
1006 if (size && size < rdev->mddev->size)
1007 return 0; /* component must fit device */
1008 size *= 2; /* convert to sectors */
1009 if (rdev->mddev->bitmap_offset)
1010 return 0; /* can't move bitmap */
1011 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
1012 if (!size || size > rdev->sb_offset*2)
1013 size = rdev->sb_offset*2;
1014 md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size,
1015 rdev->sb_page);
1016 md_super_wait(rdev->mddev);
1017 return size/2; /* kB for sysfs */
1018}
1019
1020
1021/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * version 1 superblock
1023 */
1024
NeilBrown1c05b4b2006-10-21 10:24:08 -07001025static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001027 __le32 disk_csum;
1028 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 unsigned long long newcsum;
1030 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001031 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int i;
1033
1034 disk_csum = sb->sb_csum;
1035 sb->sb_csum = 0;
1036 newcsum = 0;
1037 for (i=0; size>=4; size -= 4 )
1038 newcsum += le32_to_cpu(*isuper++);
1039
1040 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001041 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1044 sb->sb_csum = disk_csum;
1045 return cpu_to_le32(csum);
1046}
1047
1048static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1049{
1050 struct mdp_superblock_1 *sb;
1051 int ret;
1052 sector_t sb_offset;
1053 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001054 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 /*
1057 * Calculate the position of the superblock.
1058 * It is always aligned to a 4K boundary and
1059 * depeding on minor_version, it can be:
1060 * 0: At least 8K, but less than 12K, from end of device
1061 * 1: At start of device
1062 * 2: 4K from start of device.
1063 */
1064 switch(minor_version) {
1065 case 0:
1066 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1067 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001068 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 /* convert from sectors to K */
1070 sb_offset /= 2;
1071 break;
1072 case 1:
1073 sb_offset = 0;
1074 break;
1075 case 2:
1076 sb_offset = 4;
1077 break;
1078 default:
1079 return -EINVAL;
1080 }
1081 rdev->sb_offset = sb_offset;
1082
NeilBrown0002b272005-09-09 16:23:53 -07001083 /* superblock is rarely larger than 1K, but it can be larger,
1084 * and it is safe to read 4k, so we do that
1085 */
1086 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 if (ret) return ret;
1088
1089
1090 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1091
1092 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1093 sb->major_version != cpu_to_le32(1) ||
1094 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1095 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001096 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 return -EINVAL;
1098
1099 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1100 printk("md: invalid superblock checksum on %s\n",
1101 bdevname(rdev->bdev,b));
1102 return -EINVAL;
1103 }
1104 if (le64_to_cpu(sb->data_size) < 10) {
1105 printk("md: data_size too small on %s\n",
1106 bdevname(rdev->bdev,b));
1107 return -EINVAL;
1108 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001109 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1110 if (sb->level != cpu_to_le32(1) &&
1111 sb->level != cpu_to_le32(4) &&
1112 sb->level != cpu_to_le32(5) &&
1113 sb->level != cpu_to_le32(6) &&
1114 sb->level != cpu_to_le32(10)) {
1115 printk(KERN_WARNING
1116 "md: bitmaps not supported for this level.\n");
1117 return -EINVAL;
1118 }
1119 }
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 rdev->preferred_minor = 0xffff;
1122 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001123 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
NeilBrown0002b272005-09-09 16:23:53 -07001125 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001126 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001127 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001128 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1129
1130 if (minor_version
1131 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1132 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001133
NeilBrown31b65a02006-07-10 04:44:14 -07001134 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1135 rdev->desc_nr = -1;
1136 else
1137 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1138
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001139 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001140 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001141 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 __u64 ev1, ev2;
1143 struct mdp_superblock_1 *refsb =
1144 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1145
1146 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1147 sb->level != refsb->level ||
1148 sb->layout != refsb->layout ||
1149 sb->chunksize != refsb->chunksize) {
1150 printk(KERN_WARNING "md: %s has strangely different"
1151 " superblock to %s\n",
1152 bdevname(rdev->bdev,b),
1153 bdevname(refdev->bdev,b2));
1154 return -EINVAL;
1155 }
1156 ev1 = le64_to_cpu(sb->events);
1157 ev2 = le64_to_cpu(refsb->events);
1158
1159 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001160 ret = 1;
1161 else
1162 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
NeilBrowna1801f82008-03-04 14:29:31 -08001164 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1166 else
1167 rdev->size = rdev->sb_offset;
1168 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1169 return -EINVAL;
1170 rdev->size = le64_to_cpu(sb->data_size)/2;
1171 if (le32_to_cpu(sb->chunksize))
1172 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001173
NeilBrown1c05b4b2006-10-21 10:24:08 -07001174 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001175 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001176 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
1179static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1180{
1181 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001182 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
NeilBrown41158c72005-06-21 17:17:25 -07001184 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001185 clear_bit(Faulty, &rdev->flags);
1186 clear_bit(In_sync, &rdev->flags);
1187 clear_bit(WriteMostly, &rdev->flags);
1188 clear_bit(BarriersNotsupp, &rdev->flags);
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (mddev->raid_disks == 0) {
1191 mddev->major_version = 1;
1192 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001193 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1195 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1196 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1197 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001198 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 mddev->layout = le32_to_cpu(sb->layout);
1200 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1201 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001202 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001203 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001204 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1207 memcpy(mddev->uuid, sb->set_uuid, 16);
1208
1209 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001210
NeilBrown71c08052005-09-09 16:23:51 -07001211 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001212 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001213 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001214
NeilBrownf6705572006-03-27 01:18:11 -08001215 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1216 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1217 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1218 mddev->new_level = le32_to_cpu(sb->new_level);
1219 mddev->new_layout = le32_to_cpu(sb->new_layout);
1220 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1221 } else {
1222 mddev->reshape_position = MaxSector;
1223 mddev->delta_disks = 0;
1224 mddev->new_level = mddev->level;
1225 mddev->new_layout = mddev->layout;
1226 mddev->new_chunk = mddev->chunk_size;
1227 }
1228
NeilBrown41158c72005-06-21 17:17:25 -07001229 } else if (mddev->pers == NULL) {
1230 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 ++ev1;
1232 if (ev1 < mddev->events)
1233 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001234 } else if (mddev->bitmap) {
1235 /* If adding to array with a bitmap, then we can accept an
1236 * older device, but not too old.
1237 */
NeilBrown41158c72005-06-21 17:17:25 -07001238 if (ev1 < mddev->bitmap->events_cleared)
1239 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001240 } else {
1241 if (ev1 < mddev->events)
1242 /* just a hot-add of a new device, leave raid_disk at -1 */
1243 return 0;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (mddev->level != LEVEL_MULTIPATH) {
1246 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1248 switch(role) {
1249 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
1251 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001252 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001255 if ((le32_to_cpu(sb->feature_map) &
1256 MD_FEATURE_RECOVERY_OFFSET))
1257 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1258 else
1259 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 rdev->raid_disk = role;
1261 break;
1262 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001263 if (sb->devflags & WriteMostly1)
1264 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001265 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001266 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return 0;
1269}
1270
1271static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1272{
1273 struct mdp_superblock_1 *sb;
1274 struct list_head *tmp;
1275 mdk_rdev_t *rdev2;
1276 int max_dev, i;
1277 /* make rdev->sb match mddev and rdev data. */
1278
1279 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1280
1281 sb->feature_map = 0;
1282 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001283 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 memset(sb->pad1, 0, sizeof(sb->pad1));
1285 memset(sb->pad2, 0, sizeof(sb->pad2));
1286 memset(sb->pad3, 0, sizeof(sb->pad3));
1287
1288 sb->utime = cpu_to_le64((__u64)mddev->utime);
1289 sb->events = cpu_to_le64(mddev->events);
1290 if (mddev->in_sync)
1291 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1292 else
1293 sb->resync_offset = cpu_to_le64(0);
1294
NeilBrown1c05b4b2006-10-21 10:24:08 -07001295 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001296
NeilBrownf0ca3402006-02-02 14:28:04 -08001297 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001298 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001299
NeilBrowna654b9d82005-06-21 17:17:27 -07001300 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1301 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001302 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001303 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001304
1305 if (rdev->raid_disk >= 0 &&
1306 !test_bit(In_sync, &rdev->flags) &&
1307 rdev->recovery_offset > 0) {
1308 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1309 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1310 }
1311
NeilBrownf6705572006-03-27 01:18:11 -08001312 if (mddev->reshape_position != MaxSector) {
1313 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1314 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1315 sb->new_layout = cpu_to_le32(mddev->new_layout);
1316 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1317 sb->new_level = cpu_to_le32(mddev->new_level);
1318 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1319 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001320
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001322 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (rdev2->desc_nr+1 > max_dev)
1324 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001325
1326 if (max_dev > le32_to_cpu(sb->max_dev))
1327 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 for (i=0; i<max_dev;i++)
1329 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1330
NeilBrownd089c6a2008-02-06 01:39:59 -08001331 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001333 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001335 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001337 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1338 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 else
1340 sb->dev_roles[i] = cpu_to_le16(0xffff);
1341 }
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 sb->sb_csum = calc_sb_1_csum(sb);
1344}
1345
Chris Webb0cd17fe2008-06-28 08:31:46 +10001346static unsigned long long
1347super_1_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1348{
1349 struct mdp_superblock_1 *sb;
1350 unsigned long long max_size;
1351 if (size && size < rdev->mddev->size)
1352 return 0; /* component must fit device */
1353 size *= 2; /* convert to sectors */
1354 if (rdev->sb_offset < rdev->data_offset/2) {
1355 /* minor versions 1 and 2; superblock before data */
1356 max_size = (rdev->bdev->bd_inode->i_size >> 9);
1357 max_size -= rdev->data_offset;
1358 if (!size || size > max_size)
1359 size = max_size;
1360 } else if (rdev->mddev->bitmap_offset) {
1361 /* minor version 0 with bitmap we can't move */
1362 return 0;
1363 } else {
1364 /* minor version 0; superblock after data */
1365 sector_t sb_offset;
1366 sb_offset = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1367 sb_offset &= ~(sector_t)(4*2 - 1);
1368 max_size = rdev->size*2 + sb_offset - rdev->sb_offset*2;
1369 if (!size || size > max_size)
1370 size = max_size;
1371 rdev->sb_offset = sb_offset/2;
1372 }
1373 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1374 sb->data_size = cpu_to_le64(size);
1375 sb->super_offset = rdev->sb_offset*2;
1376 sb->sb_csum = calc_sb_1_csum(sb);
1377 md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size,
1378 rdev->sb_page);
1379 md_super_wait(rdev->mddev);
1380 return size/2; /* kB for sysfs */
1381}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Adrian Bunk75c96f82005-05-05 16:16:09 -07001383static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 [0] = {
1385 .name = "0.90.0",
1386 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001387 .load_super = super_90_load,
1388 .validate_super = super_90_validate,
1389 .sync_super = super_90_sync,
1390 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 },
1392 [1] = {
1393 .name = "md-1",
1394 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001395 .load_super = super_1_load,
1396 .validate_super = super_1_validate,
1397 .sync_super = super_1_sync,
1398 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 },
1400};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1403{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001404 struct list_head *tmp, *tmp2;
1405 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
NeilBrownd089c6a2008-02-06 01:39:59 -08001407 rdev_for_each(rdev, tmp, mddev1)
1408 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001409 if (rdev->bdev->bd_contains ==
1410 rdev2->bdev->bd_contains)
1411 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
1413 return 0;
1414}
1415
1416static LIST_HEAD(pending_raid_disks);
1417
1418static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1419{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001420 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001421 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001422 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001423 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (rdev->mddev) {
1426 MD_BUG();
1427 return -EINVAL;
1428 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001429
1430 /* prevent duplicates */
1431 if (find_rdev(mddev, rdev->bdev->bd_dev))
1432 return -EEXIST;
1433
NeilBrown2bf071b2006-01-06 00:20:55 -08001434 /* make sure rdev->size exceeds mddev->size */
1435 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001436 if (mddev->pers) {
1437 /* Cannot change size, so fail
1438 * If mddev->level <= 0, then we don't care
1439 * about aligning sizes (e.g. linear)
1440 */
1441 if (mddev->level > 0)
1442 return -ENOSPC;
1443 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001444 mddev->size = rdev->size;
1445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /* Verify rdev->desc_nr is unique.
1448 * If it is -1, assign a free number, else
1449 * check number is not in use
1450 */
1451 if (rdev->desc_nr < 0) {
1452 int choice = 0;
1453 if (mddev->pers) choice = mddev->raid_disks;
1454 while (find_rdev_nr(mddev, choice))
1455 choice++;
1456 rdev->desc_nr = choice;
1457 } else {
1458 if (find_rdev_nr(mddev, rdev->desc_nr))
1459 return -EBUSY;
1460 }
NeilBrown19133a42005-11-08 21:39:35 -08001461 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001462 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001463 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001466 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001467
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001468 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001469 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001470
NeilBrownf637b9f2005-11-08 21:39:37 -08001471 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001472 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001473 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001474 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001475 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1476 kobject_del(&rdev->kobj);
1477 goto fail;
1478 }
1479 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001480 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001482
1483 fail:
1484 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1485 b, mdname(mddev));
1486 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
NeilBrown177a99b2008-02-06 01:39:56 -08001489static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001490{
1491 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1492 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001493 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1497{
1498 char b[BDEVNAME_SIZE];
1499 if (!rdev->mddev) {
1500 MD_BUG();
1501 return;
1502 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001503 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 list_del_init(&rdev->same_set);
1505 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1506 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001507 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001508
1509 /* We need to delay this, otherwise we can deadlock when
1510 * writing to 'remove' to "dev/state"
1511 */
NeilBrown177a99b2008-02-06 01:39:56 -08001512 INIT_WORK(&rdev->del_work, md_delayed_delete);
1513 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001514 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
1517/*
1518 * prevent the device from being mounted, repartitioned or
1519 * otherwise reused by a RAID array (or any other kernel
1520 * subsystem), by bd_claiming the device.
1521 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001522static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
1524 int err = 0;
1525 struct block_device *bdev;
1526 char b[BDEVNAME_SIZE];
1527
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001528 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (IS_ERR(bdev)) {
1530 printk(KERN_ERR "md: could not open %s.\n",
1531 __bdevname(dev, b));
1532 return PTR_ERR(bdev);
1533 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001534 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (err) {
1536 printk(KERN_ERR "md: could not bd_claim %s.\n",
1537 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001538 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return err;
1540 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001541 if (!shared)
1542 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 rdev->bdev = bdev;
1544 return err;
1545}
1546
1547static void unlock_rdev(mdk_rdev_t *rdev)
1548{
1549 struct block_device *bdev = rdev->bdev;
1550 rdev->bdev = NULL;
1551 if (!bdev)
1552 MD_BUG();
1553 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001554 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
1557void md_autodetect_dev(dev_t dev);
1558
1559static void export_rdev(mdk_rdev_t * rdev)
1560{
1561 char b[BDEVNAME_SIZE];
1562 printk(KERN_INFO "md: export_rdev(%s)\n",
1563 bdevname(rdev->bdev,b));
1564 if (rdev->mddev)
1565 MD_BUG();
1566 free_disk_sb(rdev);
1567 list_del_init(&rdev->same_set);
1568#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001569 if (test_bit(AutoDetected, &rdev->flags))
1570 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571#endif
1572 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001573 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
1576static void kick_rdev_from_array(mdk_rdev_t * rdev)
1577{
1578 unbind_rdev_from_array(rdev);
1579 export_rdev(rdev);
1580}
1581
1582static void export_array(mddev_t *mddev)
1583{
1584 struct list_head *tmp;
1585 mdk_rdev_t *rdev;
1586
NeilBrownd089c6a2008-02-06 01:39:59 -08001587 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (!rdev->mddev) {
1589 MD_BUG();
1590 continue;
1591 }
1592 kick_rdev_from_array(rdev);
1593 }
1594 if (!list_empty(&mddev->disks))
1595 MD_BUG();
1596 mddev->raid_disks = 0;
1597 mddev->major_version = 0;
1598}
1599
1600static void print_desc(mdp_disk_t *desc)
1601{
1602 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1603 desc->major,desc->minor,desc->raid_disk,desc->state);
1604}
1605
1606static void print_sb(mdp_super_t *sb)
1607{
1608 int i;
1609
1610 printk(KERN_INFO
1611 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1612 sb->major_version, sb->minor_version, sb->patch_version,
1613 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1614 sb->ctime);
1615 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1616 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1617 sb->md_minor, sb->layout, sb->chunk_size);
1618 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1619 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1620 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1621 sb->failed_disks, sb->spare_disks,
1622 sb->sb_csum, (unsigned long)sb->events_lo);
1623
1624 printk(KERN_INFO);
1625 for (i = 0; i < MD_SB_DISKS; i++) {
1626 mdp_disk_t *desc;
1627
1628 desc = sb->disks + i;
1629 if (desc->number || desc->major || desc->minor ||
1630 desc->raid_disk || (desc->state && (desc->state != 4))) {
1631 printk(" D %2d: ", i);
1632 print_desc(desc);
1633 }
1634 }
1635 printk(KERN_INFO "md: THIS: ");
1636 print_desc(&sb->this_disk);
1637
1638}
1639
1640static void print_rdev(mdk_rdev_t *rdev)
1641{
1642 char b[BDEVNAME_SIZE];
1643 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1644 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001645 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1646 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (rdev->sb_loaded) {
1648 printk(KERN_INFO "md: rdev superblock:\n");
1649 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1650 } else
1651 printk(KERN_INFO "md: no rdev superblock!\n");
1652}
1653
Adrian Bunk5e563412006-06-26 00:27:42 -07001654static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
1656 struct list_head *tmp, *tmp2;
1657 mdk_rdev_t *rdev;
1658 mddev_t *mddev;
1659 char b[BDEVNAME_SIZE];
1660
1661 printk("\n");
1662 printk("md: **********************************\n");
1663 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1664 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001665 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
NeilBrown32a76272005-06-21 17:17:14 -07001667 if (mddev->bitmap)
1668 bitmap_print_sb(mddev->bitmap);
1669 else
1670 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001671 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 printk("<%s>", bdevname(rdev->bdev,b));
1673 printk("\n");
1674
NeilBrownd089c6a2008-02-06 01:39:59 -08001675 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 print_rdev(rdev);
1677 }
1678 printk("md: **********************************\n");
1679 printk("\n");
1680}
1681
1682
NeilBrown42543762006-06-26 00:27:57 -07001683static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
NeilBrown42543762006-06-26 00:27:57 -07001685 /* Update each superblock (in-memory image), but
1686 * if we are allowed to, skip spares which already
1687 * have the right event counter, or have one earlier
1688 * (which would mean they aren't being marked as dirty
1689 * with the rest of the array)
1690 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 mdk_rdev_t *rdev;
1692 struct list_head *tmp;
1693
NeilBrownd089c6a2008-02-06 01:39:59 -08001694 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001695 if (rdev->sb_events == mddev->events ||
1696 (nospares &&
1697 rdev->raid_disk < 0 &&
1698 (rdev->sb_events&1)==0 &&
1699 rdev->sb_events+1 == mddev->events)) {
1700 /* Don't update this superblock */
1701 rdev->sb_loaded = 2;
1702 } else {
1703 super_types[mddev->major_version].
1704 sync_super(mddev, rdev);
1705 rdev->sb_loaded = 1;
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708}
1709
NeilBrown850b2b422006-10-03 01:15:46 -07001710static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 struct list_head *tmp;
1713 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001714 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001715 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
NeilBrown8377bc82008-04-30 00:52:28 -07001717 if (mddev->external)
1718 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001720 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001721
NeilBrown850b2b422006-10-03 01:15:46 -07001722 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1723 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1724 force_change = 1;
1725 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1726 /* just a clean<-> dirty transition, possibly leave spares alone,
1727 * though if events isn't the right even/odd, we will have to do
1728 * spares after all
1729 */
1730 nospares = 1;
1731 if (force_change)
1732 nospares = 0;
1733 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001734 /* If the array is degraded, then skipping spares is both
1735 * dangerous and fairly pointless.
1736 * Dangerous because a device that was removed from the array
1737 * might have a event_count that still looks up-to-date,
1738 * so it can be re-added without a resync.
1739 * Pointless because if there are any spares to skip,
1740 * then a recovery will happen and soon that array won't
1741 * be degraded any more and the spare can go back to sleep then.
1742 */
NeilBrown850b2b422006-10-03 01:15:46 -07001743 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001744
NeilBrown06d91a52005-06-21 17:17:12 -07001745 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001747
1748 /* If this is just a dirty<->clean transition, and the array is clean
1749 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07001750 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001751 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001752 && (mddev->events & 1)
1753 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001754 mddev->events--;
1755 else {
1756 /* otherwise we have to go forward and ... */
1757 mddev->events ++;
1758 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1759 /* .. if the array isn't clean, insist on an odd 'events' */
1760 if ((mddev->events&1)==0) {
1761 mddev->events++;
1762 nospares = 0;
1763 }
1764 } else {
1765 /* otherwise insist on an even 'events' (for clean states) */
1766 if ((mddev->events&1)) {
1767 mddev->events++;
1768 nospares = 0;
1769 }
1770 }
1771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 if (!mddev->events) {
1774 /*
1775 * oops, this 64-bit counter should never wrap.
1776 * Either we are in around ~1 trillion A.C., assuming
1777 * 1 reboot per second, or we have a bug:
1778 */
1779 MD_BUG();
1780 mddev->events --;
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 /*
1784 * do not write anything to disk if using
1785 * nonpersistent superblocks
1786 */
NeilBrown06d91a52005-06-21 17:17:12 -07001787 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001788 if (!mddev->external)
1789 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1790
NeilBrowna9701a32005-11-08 21:39:34 -08001791 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001792 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001794 }
NeilBrowne6910632008-02-06 01:39:51 -08001795 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001796 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 dprintk(KERN_INFO
1799 "md: updating %s RAID superblock on device (in sync %d)\n",
1800 mdname(mddev),mddev->in_sync);
1801
NeilBrown4ad13662007-07-17 04:06:13 -07001802 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001803 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 char b[BDEVNAME_SIZE];
1805 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001806 if (rdev->sb_loaded != 1)
1807 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001808 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 dprintk("(skipping faulty ");
1810
1811 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001812 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001813 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001814 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001815 rdev->sb_page);
1816 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1817 bdevname(rdev->bdev,b),
1818 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001819 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 } else
1822 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001823 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /* only need to write one superblock... */
1825 break;
1826 }
NeilBrowna9701a32005-11-08 21:39:34 -08001827 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07001828 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001829
NeilBrowna9701a32005-11-08 21:39:34 -08001830 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07001831 if (mddev->in_sync != sync_req ||
1832 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001833 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001834 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001835 goto repeat;
1836 }
NeilBrown850b2b422006-10-03 01:15:46 -07001837 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001838 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001839 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Andre Noll7f6ce762008-03-23 18:34:54 +01001843/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08001844 * We want to accept with case. For this we use cmd_match.
1845 */
1846static int cmd_match(const char *cmd, const char *str)
1847{
1848 /* See if cmd, written into a sysfs file, matches
1849 * str. They must either be the same, or cmd can
1850 * have a trailing newline
1851 */
1852 while (*cmd && *str && *cmd == *str) {
1853 cmd++;
1854 str++;
1855 }
1856 if (*cmd == '\n')
1857 cmd++;
1858 if (*str || *cmd)
1859 return 0;
1860 return 1;
1861}
1862
NeilBrown86e6ffd2005-11-08 21:39:24 -08001863struct rdev_sysfs_entry {
1864 struct attribute attr;
1865 ssize_t (*show)(mdk_rdev_t *, char *);
1866 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1867};
1868
1869static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001870state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001871{
1872 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001873 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001874
NeilBrownb2d444d2005-11-08 21:39:31 -08001875 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001876 len+= sprintf(page+len, "%sfaulty",sep);
1877 sep = ",";
1878 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001879 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001880 len += sprintf(page+len, "%sin_sync",sep);
1881 sep = ",";
1882 }
NeilBrownf6556752006-06-26 00:28:01 -07001883 if (test_bit(WriteMostly, &rdev->flags)) {
1884 len += sprintf(page+len, "%swrite_mostly",sep);
1885 sep = ",";
1886 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001887 if (test_bit(Blocked, &rdev->flags)) {
1888 len += sprintf(page+len, "%sblocked", sep);
1889 sep = ",";
1890 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001891 if (!test_bit(Faulty, &rdev->flags) &&
1892 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001893 len += sprintf(page+len, "%sspare", sep);
1894 sep = ",";
1895 }
1896 return len+sprintf(page+len, "\n");
1897}
1898
NeilBrown45dc2de2006-06-26 00:27:58 -07001899static ssize_t
1900state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1901{
1902 /* can write
1903 * faulty - simulates and error
1904 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001905 * writemostly - sets write_mostly
1906 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001907 * blocked - sets the Blocked flag
1908 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001909 */
1910 int err = -EINVAL;
1911 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1912 md_error(rdev->mddev, rdev);
1913 err = 0;
1914 } else if (cmd_match(buf, "remove")) {
1915 if (rdev->raid_disk >= 0)
1916 err = -EBUSY;
1917 else {
1918 mddev_t *mddev = rdev->mddev;
1919 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001920 if (mddev->pers)
1921 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001922 md_new_event(mddev);
1923 err = 0;
1924 }
NeilBrownf6556752006-06-26 00:28:01 -07001925 } else if (cmd_match(buf, "writemostly")) {
1926 set_bit(WriteMostly, &rdev->flags);
1927 err = 0;
1928 } else if (cmd_match(buf, "-writemostly")) {
1929 clear_bit(WriteMostly, &rdev->flags);
1930 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001931 } else if (cmd_match(buf, "blocked")) {
1932 set_bit(Blocked, &rdev->flags);
1933 err = 0;
1934 } else if (cmd_match(buf, "-blocked")) {
1935 clear_bit(Blocked, &rdev->flags);
1936 wake_up(&rdev->blocked_wait);
1937 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1938 md_wakeup_thread(rdev->mddev->thread);
1939
1940 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001941 }
Neil Brown52664732008-06-28 08:31:44 +10001942 if (!err)
1943 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrown45dc2de2006-06-26 00:27:58 -07001944 return err ? err : len;
1945}
NeilBrown80ca3a42006-07-10 04:44:18 -07001946static struct rdev_sysfs_entry rdev_state =
1947__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001948
1949static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001950errors_show(mdk_rdev_t *rdev, char *page)
1951{
1952 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1953}
1954
1955static ssize_t
1956errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1957{
1958 char *e;
1959 unsigned long n = simple_strtoul(buf, &e, 10);
1960 if (*buf && (*e == 0 || *e == '\n')) {
1961 atomic_set(&rdev->corrected_errors, n);
1962 return len;
1963 }
1964 return -EINVAL;
1965}
1966static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001967__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001968
NeilBrown014236d2006-01-06 00:20:55 -08001969static ssize_t
1970slot_show(mdk_rdev_t *rdev, char *page)
1971{
1972 if (rdev->raid_disk < 0)
1973 return sprintf(page, "none\n");
1974 else
1975 return sprintf(page, "%d\n", rdev->raid_disk);
1976}
1977
1978static ssize_t
1979slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1980{
1981 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001982 int err;
1983 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001984 int slot = simple_strtoul(buf, &e, 10);
1985 if (strncmp(buf, "none", 4)==0)
1986 slot = -1;
1987 else if (e==buf || (*e && *e!= '\n'))
1988 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10001989 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001990 /* Setting 'slot' on an active array requires also
1991 * updating the 'rd%d' link, and communicating
1992 * with the personality with ->hot_*_disk.
1993 * For now we only support removing
1994 * failed/spare devices. This normally happens automatically,
1995 * but not when the metadata is externally managed.
1996 */
NeilBrownc303da62008-02-06 01:39:51 -08001997 if (rdev->raid_disk == -1)
1998 return -EEXIST;
1999 /* personality does all needed checks */
2000 if (rdev->mddev->pers->hot_add_disk == NULL)
2001 return -EINVAL;
2002 err = rdev->mddev->pers->
2003 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2004 if (err)
2005 return err;
2006 sprintf(nm, "rd%d", rdev->raid_disk);
2007 sysfs_remove_link(&rdev->mddev->kobj, nm);
2008 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2009 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002010 } else if (rdev->mddev->pers) {
2011 mdk_rdev_t *rdev2;
2012 struct list_head *tmp;
2013 /* Activating a spare .. or possibly reactivating
2014 * if we every get bitmaps working here.
2015 */
2016
2017 if (rdev->raid_disk != -1)
2018 return -EBUSY;
2019
2020 if (rdev->mddev->pers->hot_add_disk == NULL)
2021 return -EINVAL;
2022
2023 rdev_for_each(rdev2, tmp, rdev->mddev)
2024 if (rdev2->raid_disk == slot)
2025 return -EEXIST;
2026
2027 rdev->raid_disk = slot;
2028 if (test_bit(In_sync, &rdev->flags))
2029 rdev->saved_raid_disk = slot;
2030 else
2031 rdev->saved_raid_disk = -1;
2032 err = rdev->mddev->pers->
2033 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002034 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002035 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002036 return err;
Neil Brown52664732008-06-28 08:31:44 +10002037 } else
2038 sysfs_notify(&rdev->kobj, NULL, "state");
Neil Brown6c2fce22008-06-28 08:31:31 +10002039 sprintf(nm, "rd%d", rdev->raid_disk);
2040 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2041 printk(KERN_WARNING
2042 "md: cannot register "
2043 "%s for %s\n",
2044 nm, mdname(rdev->mddev));
2045
2046 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002047 } else {
2048 if (slot >= rdev->mddev->raid_disks)
2049 return -ENOSPC;
2050 rdev->raid_disk = slot;
2051 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002052 clear_bit(Faulty, &rdev->flags);
2053 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002054 set_bit(In_sync, &rdev->flags);
Neil Brown52664732008-06-28 08:31:44 +10002055 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc303da62008-02-06 01:39:51 -08002056 }
NeilBrown014236d2006-01-06 00:20:55 -08002057 return len;
2058}
2059
2060
2061static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002062__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002063
NeilBrown93c8cad2006-01-06 00:20:56 -08002064static ssize_t
2065offset_show(mdk_rdev_t *rdev, char *page)
2066{
Andrew Morton6961ece2006-01-06 00:20:59 -08002067 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002068}
2069
2070static ssize_t
2071offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2072{
2073 char *e;
2074 unsigned long long offset = simple_strtoull(buf, &e, 10);
2075 if (e==buf || (*e && *e != '\n'))
2076 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002077 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002078 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002079 if (rdev->size && rdev->mddev->external)
2080 /* Must set offset before size, so overlap checks
2081 * can be sane */
2082 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002083 rdev->data_offset = offset;
2084 return len;
2085}
2086
2087static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002088__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002089
NeilBrown83303b62006-01-06 00:21:06 -08002090static ssize_t
2091rdev_size_show(mdk_rdev_t *rdev, char *page)
2092{
2093 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2094}
2095
NeilBrownc5d79ad2008-02-06 01:39:54 -08002096static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2097{
2098 /* check if two start/length pairs overlap */
2099 if (s1+l1 <= s2)
2100 return 0;
2101 if (s2+l2 <= s1)
2102 return 0;
2103 return 1;
2104}
2105
NeilBrown83303b62006-01-06 00:21:06 -08002106static ssize_t
2107rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2108{
2109 char *e;
2110 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002111 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002112 mddev_t *my_mddev = rdev->mddev;
2113
NeilBrown83303b62006-01-06 00:21:06 -08002114 if (e==buf || (*e && *e != '\n'))
2115 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002116 if (my_mddev->pers && rdev->raid_disk >= 0) {
2117 if (rdev->mddev->persistent) {
2118 size = super_types[rdev->mddev->major_version].
2119 rdev_size_change(rdev, size);
2120 if (!size)
2121 return -EBUSY;
2122 } else if (!size) {
2123 size = (rdev->bdev->bd_inode->i_size >> 10);
2124 size -= rdev->data_offset/2;
2125 }
2126 if (size < rdev->mddev->size)
2127 return -EINVAL; /* component must fit device */
2128 }
2129
NeilBrown83303b62006-01-06 00:21:06 -08002130 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002131 if (size > oldsize && rdev->mddev->external) {
2132 /* need to check that all other rdevs with the same ->bdev
2133 * do not overlap. We need to unlock the mddev to avoid
2134 * a deadlock. We have already changed rdev->size, and if
2135 * we have to change it back, we will have the lock again.
2136 */
2137 mddev_t *mddev;
2138 int overlap = 0;
2139 struct list_head *tmp, *tmp2;
2140
NeilBrown27c529b2008-03-04 14:29:33 -08002141 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002142 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002143 mdk_rdev_t *rdev2;
2144
2145 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002146 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002147 if (test_bit(AllReserved, &rdev2->flags) ||
2148 (rdev->bdev == rdev2->bdev &&
2149 rdev != rdev2 &&
2150 overlaps(rdev->data_offset, rdev->size,
2151 rdev2->data_offset, rdev2->size))) {
2152 overlap = 1;
2153 break;
2154 }
2155 mddev_unlock(mddev);
2156 if (overlap) {
2157 mddev_put(mddev);
2158 break;
2159 }
2160 }
NeilBrown27c529b2008-03-04 14:29:33 -08002161 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002162 if (overlap) {
2163 /* Someone else could have slipped in a size
2164 * change here, but doing so is just silly.
2165 * We put oldsize back because we *know* it is
2166 * safe, and trust userspace not to race with
2167 * itself
2168 */
2169 rdev->size = oldsize;
2170 return -EBUSY;
2171 }
2172 }
NeilBrown27c529b2008-03-04 14:29:33 -08002173 if (size < my_mddev->size || my_mddev->size == 0)
2174 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002175 return len;
2176}
2177
2178static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002179__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002180
NeilBrown86e6ffd2005-11-08 21:39:24 -08002181static struct attribute *rdev_default_attrs[] = {
2182 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002183 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002184 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002185 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002186 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002187 NULL,
2188};
2189static ssize_t
2190rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2191{
2192 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2193 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002194 mddev_t *mddev = rdev->mddev;
2195 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002196
2197 if (!entry->show)
2198 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002199
2200 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2201 if (!rv) {
2202 if (rdev->mddev == NULL)
2203 rv = -EBUSY;
2204 else
2205 rv = entry->show(rdev, page);
2206 mddev_unlock(mddev);
2207 }
2208 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002209}
2210
2211static ssize_t
2212rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2213 const char *page, size_t length)
2214{
2215 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2216 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002217 ssize_t rv;
2218 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002219
2220 if (!entry->store)
2221 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002222 if (!capable(CAP_SYS_ADMIN))
2223 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002224 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002225 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002226 if (rdev->mddev == NULL)
2227 rv = -EBUSY;
2228 else
2229 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002230 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002231 }
2232 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002233}
2234
2235static void rdev_free(struct kobject *ko)
2236{
2237 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2238 kfree(rdev);
2239}
2240static struct sysfs_ops rdev_sysfs_ops = {
2241 .show = rdev_attr_show,
2242 .store = rdev_attr_store,
2243};
2244static struct kobj_type rdev_ktype = {
2245 .release = rdev_free,
2246 .sysfs_ops = &rdev_sysfs_ops,
2247 .default_attrs = rdev_default_attrs,
2248};
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250/*
2251 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2252 *
2253 * mark the device faulty if:
2254 *
2255 * - the device is nonexistent (zero size)
2256 * - the device has no valid superblock
2257 *
2258 * a faulty rdev _never_ has rdev->sb set.
2259 */
2260static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2261{
2262 char b[BDEVNAME_SIZE];
2263 int err;
2264 mdk_rdev_t *rdev;
2265 sector_t size;
2266
NeilBrown9ffae0c2006-01-06 00:20:32 -08002267 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (!rdev) {
2269 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2270 return ERR_PTR(-ENOMEM);
2271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 if ((err = alloc_disk_sb(rdev)))
2274 goto abort_free;
2275
NeilBrownc5d79ad2008-02-06 01:39:54 -08002276 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (err)
2278 goto abort_free;
2279
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002280 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002283 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002284 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002285 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002287 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002289 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002290 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2293 if (!size) {
2294 printk(KERN_WARNING
2295 "md: %s has zero or unknown size, marking faulty!\n",
2296 bdevname(rdev->bdev,b));
2297 err = -EINVAL;
2298 goto abort_free;
2299 }
2300
2301 if (super_format >= 0) {
2302 err = super_types[super_format].
2303 load_super(rdev, NULL, super_minor);
2304 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002305 printk(KERN_WARNING
2306 "md: %s does not have a valid v%d.%d "
2307 "superblock, not importing!\n",
2308 bdevname(rdev->bdev,b),
2309 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 goto abort_free;
2311 }
2312 if (err < 0) {
2313 printk(KERN_WARNING
2314 "md: could not read %s's sb, not importing!\n",
2315 bdevname(rdev->bdev,b));
2316 goto abort_free;
2317 }
2318 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002321 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 return rdev;
2324
2325abort_free:
2326 if (rdev->sb_page) {
2327 if (rdev->bdev)
2328 unlock_rdev(rdev);
2329 free_disk_sb(rdev);
2330 }
2331 kfree(rdev);
2332 return ERR_PTR(err);
2333}
2334
2335/*
2336 * Check a full RAID array for plausibility
2337 */
2338
2339
NeilBrowna757e642005-04-16 15:26:42 -07002340static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341{
2342 int i;
2343 struct list_head *tmp;
2344 mdk_rdev_t *rdev, *freshest;
2345 char b[BDEVNAME_SIZE];
2346
2347 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002348 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 switch (super_types[mddev->major_version].
2350 load_super(rdev, freshest, mddev->minor_version)) {
2351 case 1:
2352 freshest = rdev;
2353 break;
2354 case 0:
2355 break;
2356 default:
2357 printk( KERN_ERR \
2358 "md: fatal superblock inconsistency in %s"
2359 " -- removing from array\n",
2360 bdevname(rdev->bdev,b));
2361 kick_rdev_from_array(rdev);
2362 }
2363
2364
2365 super_types[mddev->major_version].
2366 validate_super(mddev, freshest);
2367
2368 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002369 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (rdev != freshest)
2371 if (super_types[mddev->major_version].
2372 validate_super(mddev, rdev)) {
2373 printk(KERN_WARNING "md: kicking non-fresh %s"
2374 " from array!\n",
2375 bdevname(rdev->bdev,b));
2376 kick_rdev_from_array(rdev);
2377 continue;
2378 }
2379 if (mddev->level == LEVEL_MULTIPATH) {
2380 rdev->desc_nr = i++;
2381 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002382 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002383 } else if (rdev->raid_disk >= mddev->raid_disks) {
2384 rdev->raid_disk = -1;
2385 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387 }
2388
2389
2390
2391 if (mddev->recovery_cp != MaxSector &&
2392 mddev->level >= 1)
2393 printk(KERN_ERR "md: %s: raid array is not clean"
2394 " -- starting background reconstruction\n",
2395 mdname(mddev));
2396
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397}
2398
NeilBrowneae17012005-11-08 21:39:23 -08002399static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002400safe_delay_show(mddev_t *mddev, char *page)
2401{
2402 int msec = (mddev->safemode_delay*1000)/HZ;
2403 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2404}
2405static ssize_t
2406safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2407{
2408 int scale=1;
2409 int dot=0;
2410 int i;
2411 unsigned long msec;
2412 char buf[30];
2413 char *e;
2414 /* remove a period, and count digits after it */
2415 if (len >= sizeof(buf))
2416 return -EINVAL;
2417 strlcpy(buf, cbuf, len);
2418 buf[len] = 0;
2419 for (i=0; i<len; i++) {
2420 if (dot) {
2421 if (isdigit(buf[i])) {
2422 buf[i-1] = buf[i];
2423 scale *= 10;
2424 }
2425 buf[i] = 0;
2426 } else if (buf[i] == '.') {
2427 dot=1;
2428 buf[i] = 0;
2429 }
2430 }
2431 msec = simple_strtoul(buf, &e, 10);
2432 if (e == buf || (*e && *e != '\n'))
2433 return -EINVAL;
2434 msec = (msec * 1000) / scale;
2435 if (msec == 0)
2436 mddev->safemode_delay = 0;
2437 else {
2438 mddev->safemode_delay = (msec*HZ)/1000;
2439 if (mddev->safemode_delay == 0)
2440 mddev->safemode_delay = 1;
2441 }
2442 return len;
2443}
2444static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002445__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002446
2447static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002448level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002449{
NeilBrown2604b702006-01-06 00:20:36 -08002450 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002451 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002452 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002453 else if (mddev->clevel[0])
2454 return sprintf(page, "%s\n", mddev->clevel);
2455 else if (mddev->level != LEVEL_NONE)
2456 return sprintf(page, "%d\n", mddev->level);
2457 else
2458 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002459}
2460
NeilBrownd9d166c2006-01-06 00:20:51 -08002461static ssize_t
2462level_store(mddev_t *mddev, const char *buf, size_t len)
2463{
NeilBrown20a49ff2008-02-06 01:39:57 -08002464 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002465 if (mddev->pers)
2466 return -EBUSY;
2467 if (len == 0)
2468 return 0;
2469 if (len >= sizeof(mddev->clevel))
2470 return -ENOSPC;
2471 strncpy(mddev->clevel, buf, len);
2472 if (mddev->clevel[len-1] == '\n')
2473 len--;
2474 mddev->clevel[len] = 0;
2475 mddev->level = LEVEL_NONE;
2476 return rv;
2477}
2478
2479static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002480__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002481
NeilBrownd4dbd022006-06-26 00:27:59 -07002482
2483static ssize_t
2484layout_show(mddev_t *mddev, char *page)
2485{
2486 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002487 if (mddev->reshape_position != MaxSector &&
2488 mddev->layout != mddev->new_layout)
2489 return sprintf(page, "%d (%d)\n",
2490 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002491 return sprintf(page, "%d\n", mddev->layout);
2492}
2493
2494static ssize_t
2495layout_store(mddev_t *mddev, const char *buf, size_t len)
2496{
2497 char *e;
2498 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002499
2500 if (!*buf || (*e && *e != '\n'))
2501 return -EINVAL;
2502
NeilBrown08a02ec2007-05-09 02:35:38 -07002503 if (mddev->pers)
2504 return -EBUSY;
2505 if (mddev->reshape_position != MaxSector)
2506 mddev->new_layout = n;
2507 else
2508 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002509 return len;
2510}
2511static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002512__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002513
2514
NeilBrowneae17012005-11-08 21:39:23 -08002515static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002516raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002517{
NeilBrownbb636542005-11-08 21:39:45 -08002518 if (mddev->raid_disks == 0)
2519 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002520 if (mddev->reshape_position != MaxSector &&
2521 mddev->delta_disks != 0)
2522 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2523 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002524 return sprintf(page, "%d\n", mddev->raid_disks);
2525}
2526
NeilBrownda943b992006-01-06 00:20:54 -08002527static int update_raid_disks(mddev_t *mddev, int raid_disks);
2528
2529static ssize_t
2530raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2531{
NeilBrownda943b992006-01-06 00:20:54 -08002532 char *e;
2533 int rv = 0;
2534 unsigned long n = simple_strtoul(buf, &e, 10);
2535
2536 if (!*buf || (*e && *e != '\n'))
2537 return -EINVAL;
2538
2539 if (mddev->pers)
2540 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002541 else if (mddev->reshape_position != MaxSector) {
2542 int olddisks = mddev->raid_disks - mddev->delta_disks;
2543 mddev->delta_disks = n - olddisks;
2544 mddev->raid_disks = n;
2545 } else
NeilBrownda943b992006-01-06 00:20:54 -08002546 mddev->raid_disks = n;
2547 return rv ? rv : len;
2548}
2549static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002550__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002551
NeilBrown24dd4692005-11-08 21:39:26 -08002552static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002553chunk_size_show(mddev_t *mddev, char *page)
2554{
NeilBrown08a02ec2007-05-09 02:35:38 -07002555 if (mddev->reshape_position != MaxSector &&
2556 mddev->chunk_size != mddev->new_chunk)
2557 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2558 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002559 return sprintf(page, "%d\n", mddev->chunk_size);
2560}
2561
2562static ssize_t
2563chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2564{
2565 /* can only set chunk_size if array is not yet active */
2566 char *e;
2567 unsigned long n = simple_strtoul(buf, &e, 10);
2568
NeilBrown3b343802006-01-06 00:20:47 -08002569 if (!*buf || (*e && *e != '\n'))
2570 return -EINVAL;
2571
NeilBrown08a02ec2007-05-09 02:35:38 -07002572 if (mddev->pers)
2573 return -EBUSY;
2574 else if (mddev->reshape_position != MaxSector)
2575 mddev->new_chunk = n;
2576 else
2577 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002578 return len;
2579}
2580static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002581__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002582
NeilBrowna94213b2006-06-26 00:28:00 -07002583static ssize_t
2584resync_start_show(mddev_t *mddev, char *page)
2585{
2586 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2587}
2588
2589static ssize_t
2590resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2591{
NeilBrowna94213b2006-06-26 00:28:00 -07002592 char *e;
2593 unsigned long long n = simple_strtoull(buf, &e, 10);
2594
2595 if (mddev->pers)
2596 return -EBUSY;
2597 if (!*buf || (*e && *e != '\n'))
2598 return -EINVAL;
2599
2600 mddev->recovery_cp = n;
2601 return len;
2602}
2603static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002604__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002605
NeilBrown9e653b62006-06-26 00:27:58 -07002606/*
2607 * The array state can be:
2608 *
2609 * clear
2610 * No devices, no size, no level
2611 * Equivalent to STOP_ARRAY ioctl
2612 * inactive
2613 * May have some settings, but array is not active
2614 * all IO results in error
2615 * When written, doesn't tear down array, but just stops it
2616 * suspended (not supported yet)
2617 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002618 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002619 * readonly
2620 * no resync can happen. no superblocks get written.
2621 * write requests fail
2622 * read-auto
2623 * like readonly, but behaves like 'clean' on a write request.
2624 *
2625 * clean - no pending writes, but otherwise active.
2626 * When written to inactive array, starts without resync
2627 * If a write request arrives then
2628 * if metadata is known, mark 'dirty' and switch to 'active'.
2629 * if not known, block and switch to write-pending
2630 * If written to an active array that has pending writes, then fails.
2631 * active
2632 * fully active: IO and resync can be happening.
2633 * When written to inactive array, starts with resync
2634 *
2635 * write-pending
2636 * clean, but writes are blocked waiting for 'active' to be written.
2637 *
2638 * active-idle
2639 * like active, but no writes have been seen for a while (100msec).
2640 *
2641 */
2642enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2643 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002644static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002645 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2646 "write-pending", "active-idle", NULL };
2647
2648static int match_word(const char *word, char **list)
2649{
2650 int n;
2651 for (n=0; list[n]; n++)
2652 if (cmd_match(word, list[n]))
2653 break;
2654 return n;
2655}
2656
2657static ssize_t
2658array_state_show(mddev_t *mddev, char *page)
2659{
2660 enum array_state st = inactive;
2661
2662 if (mddev->pers)
2663 switch(mddev->ro) {
2664 case 1:
2665 st = readonly;
2666 break;
2667 case 2:
2668 st = read_auto;
2669 break;
2670 case 0:
2671 if (mddev->in_sync)
2672 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002673 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2674 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002675 else if (mddev->safemode)
2676 st = active_idle;
2677 else
2678 st = active;
2679 }
2680 else {
2681 if (list_empty(&mddev->disks) &&
2682 mddev->raid_disks == 0 &&
2683 mddev->size == 0)
2684 st = clear;
2685 else
2686 st = inactive;
2687 }
2688 return sprintf(page, "%s\n", array_states[st]);
2689}
2690
Neil Browndf5b20c2008-07-11 22:02:22 +10002691static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002692static int do_md_run(mddev_t * mddev);
2693static int restart_array(mddev_t *mddev);
2694
2695static ssize_t
2696array_state_store(mddev_t *mddev, const char *buf, size_t len)
2697{
2698 int err = -EINVAL;
2699 enum array_state st = match_word(buf, array_states);
2700 switch(st) {
2701 case bad_word:
2702 break;
2703 case clear:
2704 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002705 if (atomic_read(&mddev->active) > 1)
2706 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002707 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002708 break;
2709 case inactive:
2710 /* stopping an active array */
2711 if (mddev->pers) {
2712 if (atomic_read(&mddev->active) > 1)
2713 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002714 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08002715 } else
2716 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002717 break;
2718 case suspended:
2719 break; /* not supported yet */
2720 case readonly:
2721 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10002722 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002723 else {
2724 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002725 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002726 err = do_md_run(mddev);
2727 }
2728 break;
2729 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002730 if (mddev->pers) {
NeilBrown648b6292008-04-30 00:52:30 -07002731 if (mddev->ro != 1)
Neil Browndf5b20c2008-07-11 22:02:22 +10002732 err = do_md_stop(mddev, 1, 0);
NeilBrown648b6292008-04-30 00:52:30 -07002733 else
2734 err = restart_array(mddev);
2735 if (err == 0) {
2736 mddev->ro = 2;
2737 set_disk_ro(mddev->gendisk, 0);
2738 }
NeilBrown9e653b62006-06-26 00:27:58 -07002739 } else {
2740 mddev->ro = 2;
2741 err = do_md_run(mddev);
2742 }
2743 break;
2744 case clean:
2745 if (mddev->pers) {
2746 restart_array(mddev);
2747 spin_lock_irq(&mddev->write_lock);
2748 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002749 if (mddev->in_sync == 0) {
2750 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002751 if (mddev->safemode == 1)
2752 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002753 if (mddev->persistent)
2754 set_bit(MD_CHANGE_CLEAN,
2755 &mddev->flags);
2756 }
2757 err = 0;
2758 } else
2759 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002760 spin_unlock_irq(&mddev->write_lock);
2761 } else {
2762 mddev->ro = 0;
2763 mddev->recovery_cp = MaxSector;
2764 err = do_md_run(mddev);
2765 }
2766 break;
2767 case active:
2768 if (mddev->pers) {
2769 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002770 if (mddev->external)
2771 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002772 wake_up(&mddev->sb_wait);
2773 err = 0;
2774 } else {
2775 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002776 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002777 err = do_md_run(mddev);
2778 }
2779 break;
2780 case write_pending:
2781 case active_idle:
2782 /* these cannot be set */
2783 break;
2784 }
2785 if (err)
2786 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002787 else {
2788 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown9e653b62006-06-26 00:27:58 -07002789 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002790 }
NeilBrown9e653b62006-06-26 00:27:58 -07002791}
NeilBrown80ca3a42006-07-10 04:44:18 -07002792static struct md_sysfs_entry md_array_state =
2793__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002794
NeilBrown6d7ff7382006-01-06 00:21:16 -08002795static ssize_t
2796null_show(mddev_t *mddev, char *page)
2797{
2798 return -EINVAL;
2799}
2800
2801static ssize_t
2802new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2803{
2804 /* buf must be %d:%d\n? giving major and minor numbers */
2805 /* The new device is added to the array.
2806 * If the array has a persistent superblock, we read the
2807 * superblock to initialise info and check validity.
2808 * Otherwise, only checking done is that in bind_rdev_to_array,
2809 * which mainly checks size.
2810 */
2811 char *e;
2812 int major = simple_strtoul(buf, &e, 10);
2813 int minor;
2814 dev_t dev;
2815 mdk_rdev_t *rdev;
2816 int err;
2817
2818 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2819 return -EINVAL;
2820 minor = simple_strtoul(e+1, &e, 10);
2821 if (*e && *e != '\n')
2822 return -EINVAL;
2823 dev = MKDEV(major, minor);
2824 if (major != MAJOR(dev) ||
2825 minor != MINOR(dev))
2826 return -EOVERFLOW;
2827
2828
2829 if (mddev->persistent) {
2830 rdev = md_import_device(dev, mddev->major_version,
2831 mddev->minor_version);
2832 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2833 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2834 mdk_rdev_t, same_set);
2835 err = super_types[mddev->major_version]
2836 .load_super(rdev, rdev0, mddev->minor_version);
2837 if (err < 0)
2838 goto out;
2839 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002840 } else if (mddev->external)
2841 rdev = md_import_device(dev, -2, -1);
2842 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08002843 rdev = md_import_device(dev, -1, -1);
2844
2845 if (IS_ERR(rdev))
2846 return PTR_ERR(rdev);
2847 err = bind_rdev_to_array(rdev, mddev);
2848 out:
2849 if (err)
2850 export_rdev(rdev);
2851 return err ? err : len;
2852}
2853
2854static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002855__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002856
2857static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002858bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2859{
2860 char *end;
2861 unsigned long chunk, end_chunk;
2862
2863 if (!mddev->bitmap)
2864 goto out;
2865 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2866 while (*buf) {
2867 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2868 if (buf == end) break;
2869 if (*end == '-') { /* range */
2870 buf = end + 1;
2871 end_chunk = simple_strtoul(buf, &end, 0);
2872 if (buf == end) break;
2873 }
2874 if (*end && !isspace(*end)) break;
2875 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2876 buf = end;
2877 while (isspace(*buf)) buf++;
2878 }
2879 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2880out:
2881 return len;
2882}
2883
2884static struct md_sysfs_entry md_bitmap =
2885__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2886
2887static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002888size_show(mddev_t *mddev, char *page)
2889{
2890 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2891}
2892
2893static int update_size(mddev_t *mddev, unsigned long size);
2894
2895static ssize_t
2896size_store(mddev_t *mddev, const char *buf, size_t len)
2897{
2898 /* If array is inactive, we can reduce the component size, but
2899 * not increase it (except from 0).
2900 * If array is active, we can try an on-line resize
2901 */
2902 char *e;
2903 int err = 0;
2904 unsigned long long size = simple_strtoull(buf, &e, 10);
2905 if (!*buf || *buf == '\n' ||
2906 (*e && *e != '\n'))
2907 return -EINVAL;
2908
2909 if (mddev->pers) {
2910 err = update_size(mddev, size);
NeilBrown850b2b422006-10-03 01:15:46 -07002911 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002912 } else {
2913 if (mddev->size == 0 ||
2914 mddev->size > size)
2915 mddev->size = size;
2916 else
2917 err = -ENOSPC;
2918 }
2919 return err ? err : len;
2920}
2921
2922static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002923__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002924
NeilBrown8bb93aa2006-01-06 00:20:50 -08002925
2926/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002927 * This is one of
2928 * 'none' for arrays with no metadata (good luck...)
2929 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002930 * or N.M for internally known formats
2931 */
2932static ssize_t
2933metadata_show(mddev_t *mddev, char *page)
2934{
2935 if (mddev->persistent)
2936 return sprintf(page, "%d.%d\n",
2937 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002938 else if (mddev->external)
2939 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002940 else
2941 return sprintf(page, "none\n");
2942}
2943
2944static ssize_t
2945metadata_store(mddev_t *mddev, const char *buf, size_t len)
2946{
2947 int major, minor;
2948 char *e;
2949 if (!list_empty(&mddev->disks))
2950 return -EBUSY;
2951
2952 if (cmd_match(buf, "none")) {
2953 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002954 mddev->external = 0;
2955 mddev->major_version = 0;
2956 mddev->minor_version = 90;
2957 return len;
2958 }
2959 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002960 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002961 if (namelen >= sizeof(mddev->metadata_type))
2962 namelen = sizeof(mddev->metadata_type)-1;
2963 strncpy(mddev->metadata_type, buf+9, namelen);
2964 mddev->metadata_type[namelen] = 0;
2965 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2966 mddev->metadata_type[--namelen] = 0;
2967 mddev->persistent = 0;
2968 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002969 mddev->major_version = 0;
2970 mddev->minor_version = 90;
2971 return len;
2972 }
2973 major = simple_strtoul(buf, &e, 10);
2974 if (e==buf || *e != '.')
2975 return -EINVAL;
2976 buf = e+1;
2977 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002978 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002979 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002980 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002981 return -ENOENT;
2982 mddev->major_version = major;
2983 mddev->minor_version = minor;
2984 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002985 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002986 return len;
2987}
2988
2989static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002990__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002991
NeilBrowna35b0d62006-01-06 00:20:49 -08002992static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002993action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002994{
NeilBrown7eec3142005-11-08 21:39:44 -08002995 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002996 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002997 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002998 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2999 type = "reshape";
3000 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003001 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3002 type = "resync";
3003 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3004 type = "check";
3005 else
3006 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003007 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003008 type = "recover";
3009 }
3010 return sprintf(page, "%s\n", type);
3011}
3012
3013static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003014action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003015{
NeilBrown7eec3142005-11-08 21:39:44 -08003016 if (!mddev->pers || !mddev->pers->sync_request)
3017 return -EINVAL;
3018
NeilBrownbce74da2006-01-06 00:20:41 -08003019 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003020 if (mddev->sync_thread) {
3021 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3022 md_unregister_thread(mddev->sync_thread);
3023 mddev->sync_thread = NULL;
3024 mddev->recovery = 0;
3025 }
NeilBrown03c902e2006-01-06 00:20:46 -08003026 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3027 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003028 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003029 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003030 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003031 else if (cmd_match(page, "recover")) {
3032 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3033 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3034 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003035 int err;
3036 if (mddev->pers->start_reshape == NULL)
3037 return -EINVAL;
3038 err = mddev->pers->start_reshape(mddev);
3039 if (err)
3040 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003041 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003042 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003043 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003044 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003045 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003046 return -EINVAL;
3047 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3048 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003049 }
NeilBrown03c902e2006-01-06 00:20:46 -08003050 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003051 md_wakeup_thread(mddev->thread);
Neil Brown72a23c22008-06-28 08:31:41 +10003052 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrown24dd4692005-11-08 21:39:26 -08003053 return len;
3054}
3055
NeilBrown9d888832005-11-08 21:39:26 -08003056static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003057mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003058{
3059 return sprintf(page, "%llu\n",
3060 (unsigned long long) mddev->resync_mismatches);
3061}
3062
NeilBrown80ca3a42006-07-10 04:44:18 -07003063static struct md_sysfs_entry md_scan_mode =
3064__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003065
NeilBrown96de1e62005-11-08 21:39:39 -08003066
NeilBrown80ca3a42006-07-10 04:44:18 -07003067static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003068
NeilBrown88202a02006-01-06 00:21:36 -08003069static ssize_t
3070sync_min_show(mddev_t *mddev, char *page)
3071{
3072 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3073 mddev->sync_speed_min ? "local": "system");
3074}
3075
3076static ssize_t
3077sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3078{
3079 int min;
3080 char *e;
3081 if (strncmp(buf, "system", 6)==0) {
3082 mddev->sync_speed_min = 0;
3083 return len;
3084 }
3085 min = simple_strtoul(buf, &e, 10);
3086 if (buf == e || (*e && *e != '\n') || min <= 0)
3087 return -EINVAL;
3088 mddev->sync_speed_min = min;
3089 return len;
3090}
3091
3092static struct md_sysfs_entry md_sync_min =
3093__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3094
3095static ssize_t
3096sync_max_show(mddev_t *mddev, char *page)
3097{
3098 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3099 mddev->sync_speed_max ? "local": "system");
3100}
3101
3102static ssize_t
3103sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3104{
3105 int max;
3106 char *e;
3107 if (strncmp(buf, "system", 6)==0) {
3108 mddev->sync_speed_max = 0;
3109 return len;
3110 }
3111 max = simple_strtoul(buf, &e, 10);
3112 if (buf == e || (*e && *e != '\n') || max <= 0)
3113 return -EINVAL;
3114 mddev->sync_speed_max = max;
3115 return len;
3116}
3117
3118static struct md_sysfs_entry md_sync_max =
3119__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3120
Iustin Popd7f3d292007-10-16 23:30:54 -07003121static ssize_t
3122degraded_show(mddev_t *mddev, char *page)
3123{
3124 return sprintf(page, "%d\n", mddev->degraded);
3125}
3126static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003127
3128static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003129sync_force_parallel_show(mddev_t *mddev, char *page)
3130{
3131 return sprintf(page, "%d\n", mddev->parallel_resync);
3132}
3133
3134static ssize_t
3135sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3136{
3137 long n;
3138
3139 if (strict_strtol(buf, 10, &n))
3140 return -EINVAL;
3141
3142 if (n != 0 && n != 1)
3143 return -EINVAL;
3144
3145 mddev->parallel_resync = n;
3146
3147 if (mddev->sync_thread)
3148 wake_up(&resync_wait);
3149
3150 return len;
3151}
3152
3153/* force parallel resync, even with shared block devices */
3154static struct md_sysfs_entry md_sync_force_parallel =
3155__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3156 sync_force_parallel_show, sync_force_parallel_store);
3157
3158static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003159sync_speed_show(mddev_t *mddev, char *page)
3160{
3161 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003162 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3163 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003164 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003165 db = resync - mddev->resync_mark_cnt;
3166 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003167}
3168
NeilBrown80ca3a42006-07-10 04:44:18 -07003169static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003170
3171static ssize_t
3172sync_completed_show(mddev_t *mddev, char *page)
3173{
3174 unsigned long max_blocks, resync;
3175
3176 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3177 max_blocks = mddev->resync_max_sectors;
3178 else
3179 max_blocks = mddev->size << 1;
3180
3181 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3182 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3183}
3184
NeilBrown80ca3a42006-07-10 04:44:18 -07003185static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003186
NeilBrowne464eaf2006-03-27 01:18:14 -08003187static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003188min_sync_show(mddev_t *mddev, char *page)
3189{
3190 return sprintf(page, "%llu\n",
3191 (unsigned long long)mddev->resync_min);
3192}
3193static ssize_t
3194min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3195{
3196 unsigned long long min;
3197 if (strict_strtoull(buf, 10, &min))
3198 return -EINVAL;
3199 if (min > mddev->resync_max)
3200 return -EINVAL;
3201 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3202 return -EBUSY;
3203
3204 /* Must be a multiple of chunk_size */
3205 if (mddev->chunk_size) {
3206 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3207 return -EINVAL;
3208 }
3209 mddev->resync_min = min;
3210
3211 return len;
3212}
3213
3214static struct md_sysfs_entry md_min_sync =
3215__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3216
3217static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003218max_sync_show(mddev_t *mddev, char *page)
3219{
3220 if (mddev->resync_max == MaxSector)
3221 return sprintf(page, "max\n");
3222 else
3223 return sprintf(page, "%llu\n",
3224 (unsigned long long)mddev->resync_max);
3225}
3226static ssize_t
3227max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3228{
3229 if (strncmp(buf, "max", 3) == 0)
3230 mddev->resync_max = MaxSector;
3231 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003232 unsigned long long max;
3233 if (strict_strtoull(buf, 10, &max))
3234 return -EINVAL;
3235 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003236 return -EINVAL;
3237 if (max < mddev->resync_max &&
3238 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3239 return -EBUSY;
3240
3241 /* Must be a multiple of chunk_size */
3242 if (mddev->chunk_size) {
3243 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3244 return -EINVAL;
3245 }
3246 mddev->resync_max = max;
3247 }
3248 wake_up(&mddev->recovery_wait);
3249 return len;
3250}
3251
3252static struct md_sysfs_entry md_max_sync =
3253__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3254
3255static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003256suspend_lo_show(mddev_t *mddev, char *page)
3257{
3258 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3259}
3260
3261static ssize_t
3262suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3263{
3264 char *e;
3265 unsigned long long new = simple_strtoull(buf, &e, 10);
3266
3267 if (mddev->pers->quiesce == NULL)
3268 return -EINVAL;
3269 if (buf == e || (*e && *e != '\n'))
3270 return -EINVAL;
3271 if (new >= mddev->suspend_hi ||
3272 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3273 mddev->suspend_lo = new;
3274 mddev->pers->quiesce(mddev, 2);
3275 return len;
3276 } else
3277 return -EINVAL;
3278}
3279static struct md_sysfs_entry md_suspend_lo =
3280__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3281
3282
3283static ssize_t
3284suspend_hi_show(mddev_t *mddev, char *page)
3285{
3286 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3287}
3288
3289static ssize_t
3290suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3291{
3292 char *e;
3293 unsigned long long new = simple_strtoull(buf, &e, 10);
3294
3295 if (mddev->pers->quiesce == NULL)
3296 return -EINVAL;
3297 if (buf == e || (*e && *e != '\n'))
3298 return -EINVAL;
3299 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3300 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3301 mddev->suspend_hi = new;
3302 mddev->pers->quiesce(mddev, 1);
3303 mddev->pers->quiesce(mddev, 0);
3304 return len;
3305 } else
3306 return -EINVAL;
3307}
3308static struct md_sysfs_entry md_suspend_hi =
3309__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3310
NeilBrown08a02ec2007-05-09 02:35:38 -07003311static ssize_t
3312reshape_position_show(mddev_t *mddev, char *page)
3313{
3314 if (mddev->reshape_position != MaxSector)
3315 return sprintf(page, "%llu\n",
3316 (unsigned long long)mddev->reshape_position);
3317 strcpy(page, "none\n");
3318 return 5;
3319}
3320
3321static ssize_t
3322reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3323{
3324 char *e;
3325 unsigned long long new = simple_strtoull(buf, &e, 10);
3326 if (mddev->pers)
3327 return -EBUSY;
3328 if (buf == e || (*e && *e != '\n'))
3329 return -EINVAL;
3330 mddev->reshape_position = new;
3331 mddev->delta_disks = 0;
3332 mddev->new_level = mddev->level;
3333 mddev->new_layout = mddev->layout;
3334 mddev->new_chunk = mddev->chunk_size;
3335 return len;
3336}
3337
3338static struct md_sysfs_entry md_reshape_position =
3339__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3340 reshape_position_store);
3341
NeilBrowne464eaf2006-03-27 01:18:14 -08003342
NeilBrowneae17012005-11-08 21:39:23 -08003343static struct attribute *md_default_attrs[] = {
3344 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003345 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003346 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003347 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003348 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003349 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003350 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003351 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003352 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003353 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003354 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003355 NULL,
3356};
3357
3358static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003359 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003360 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003361 &md_sync_min.attr,
3362 &md_sync_max.attr,
3363 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003364 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003365 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003366 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003367 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003368 &md_suspend_lo.attr,
3369 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003370 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003371 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003372 NULL,
3373};
NeilBrown411036f2005-11-08 21:39:40 -08003374static struct attribute_group md_redundancy_group = {
3375 .name = NULL,
3376 .attrs = md_redundancy_attrs,
3377};
3378
NeilBrowneae17012005-11-08 21:39:23 -08003379
3380static ssize_t
3381md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3382{
3383 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3384 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003385 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003386
3387 if (!entry->show)
3388 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003389 rv = mddev_lock(mddev);
3390 if (!rv) {
3391 rv = entry->show(mddev, page);
3392 mddev_unlock(mddev);
3393 }
NeilBrown96de1e62005-11-08 21:39:39 -08003394 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003395}
3396
3397static ssize_t
3398md_attr_store(struct kobject *kobj, struct attribute *attr,
3399 const char *page, size_t length)
3400{
3401 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3402 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003403 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003404
3405 if (!entry->store)
3406 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003407 if (!capable(CAP_SYS_ADMIN))
3408 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003409 rv = mddev_lock(mddev);
3410 if (!rv) {
3411 rv = entry->store(mddev, page, length);
3412 mddev_unlock(mddev);
3413 }
NeilBrown96de1e62005-11-08 21:39:39 -08003414 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003415}
3416
3417static void md_free(struct kobject *ko)
3418{
3419 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3420 kfree(mddev);
3421}
3422
3423static struct sysfs_ops md_sysfs_ops = {
3424 .show = md_attr_show,
3425 .store = md_attr_store,
3426};
3427static struct kobj_type md_ktype = {
3428 .release = md_free,
3429 .sysfs_ops = &md_sysfs_ops,
3430 .default_attrs = md_default_attrs,
3431};
3432
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433int mdp_major = 0;
3434
3435static struct kobject *md_probe(dev_t dev, int *part, void *data)
3436{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003437 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 mddev_t *mddev = mddev_find(dev);
3439 struct gendisk *disk;
3440 int partitioned = (MAJOR(dev) != MD_MAJOR);
3441 int shift = partitioned ? MdpMinorShift : 0;
3442 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003443 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445 if (!mddev)
3446 return NULL;
3447
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003448 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003450 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 mddev_put(mddev);
3452 return NULL;
3453 }
3454 disk = alloc_disk(1 << shift);
3455 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003456 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 mddev_put(mddev);
3458 return NULL;
3459 }
3460 disk->major = MAJOR(dev);
3461 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003462 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07003464 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 disk->fops = &md_fops;
3467 disk->private_data = mddev;
3468 disk->queue = mddev->queue;
3469 add_disk(disk);
3470 mddev->gendisk = disk;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003471 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003472 "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003473 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003474 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003475 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3476 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003477 else
3478 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 return NULL;
3480}
3481
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482static void md_safemode_timeout(unsigned long data)
3483{
3484 mddev_t *mddev = (mddev_t *) data;
3485
Neil Brown0fd62b82008-06-28 08:31:36 +10003486 if (!atomic_read(&mddev->writes_pending)) {
3487 mddev->safemode = 1;
3488 if (mddev->external)
3489 sysfs_notify(&mddev->kobj, NULL, "array_state");
3490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 md_wakeup_thread(mddev->thread);
3492}
3493
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003494static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496static int do_md_run(mddev_t * mddev)
3497{
NeilBrown2604b702006-01-06 00:20:36 -08003498 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 int chunk_size;
3500 struct list_head *tmp;
3501 mdk_rdev_t *rdev;
3502 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003503 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 char b[BDEVNAME_SIZE];
3505
NeilBrowna757e642005-04-16 15:26:42 -07003506 if (list_empty(&mddev->disks))
3507 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509
3510 if (mddev->pers)
3511 return -EBUSY;
3512
3513 /*
3514 * Analyze all RAID superblock(s)
3515 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003516 if (!mddev->raid_disks) {
3517 if (!mddev->persistent)
3518 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003519 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
3522 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523
NeilBrown2604b702006-01-06 00:20:36 -08003524 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 if (chunk_size > MAX_CHUNK_SIZE) {
3526 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3527 chunk_size, MAX_CHUNK_SIZE);
3528 return -EINVAL;
3529 }
3530 /*
3531 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3532 */
3533 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003534 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 return -EINVAL;
3536 }
3537 if (chunk_size < PAGE_SIZE) {
3538 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3539 chunk_size, PAGE_SIZE);
3540 return -EINVAL;
3541 }
3542
3543 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003544 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003545 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 continue;
3547 if (rdev->size < chunk_size / 1024) {
3548 printk(KERN_WARNING
3549 "md: Dev %s smaller than chunk_size:"
3550 " %lluk < %dk\n",
3551 bdevname(rdev->bdev,b),
3552 (unsigned long long)rdev->size,
3553 chunk_size / 1024);
3554 return -EINVAL;
3555 }
3556 }
3557 }
3558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003560 if (mddev->level != LEVEL_NONE)
3561 request_module("md-level-%d", mddev->level);
3562 else if (mddev->clevel[0])
3563 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564#endif
3565
3566 /*
3567 * Drop all container device buffers, from now on
3568 * the only valid external interface is through the md
3569 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003571 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003572 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 continue;
3574 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003575 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003576
3577 /* perform some consistency tests on the device.
3578 * We don't want the data to overlap the metadata,
3579 * Internal Bitmap issues has handled elsewhere.
3580 */
3581 if (rdev->data_offset < rdev->sb_offset) {
3582 if (mddev->size &&
3583 rdev->data_offset + mddev->size*2
3584 > rdev->sb_offset*2) {
3585 printk("md: %s: data overlaps metadata\n",
3586 mdname(mddev));
3587 return -EINVAL;
3588 }
3589 } else {
3590 if (rdev->sb_offset*2 + rdev->sb_size/512
3591 > rdev->data_offset) {
3592 printk("md: %s: metadata overlaps data\n",
3593 mdname(mddev));
3594 return -EINVAL;
3595 }
3596 }
Neil Brown52664732008-06-28 08:31:44 +10003597 sysfs_notify(&rdev->kobj, NULL, "state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
3599
3600 md_probe(mddev->unit, NULL, NULL);
3601 disk = mddev->gendisk;
3602 if (!disk)
3603 return -ENOMEM;
3604
3605 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003606 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003607 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003609 if (mddev->level != LEVEL_NONE)
3610 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3611 mddev->level);
3612 else
3613 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3614 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 return -EINVAL;
3616 }
NeilBrown2604b702006-01-06 00:20:36 -08003617 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003619 mddev->level = pers->level;
3620 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621
NeilBrownf6705572006-03-27 01:18:11 -08003622 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003623 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003624 /* This personality cannot handle reshaping... */
3625 mddev->pers = NULL;
3626 module_put(pers->owner);
3627 return -EINVAL;
3628 }
3629
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003630 if (pers->sync_request) {
3631 /* Warn if this is a potentially silly
3632 * configuration.
3633 */
3634 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3635 mdk_rdev_t *rdev2;
3636 struct list_head *tmp2;
3637 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003638 rdev_for_each(rdev, tmp, mddev) {
3639 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003640 if (rdev < rdev2 &&
3641 rdev->bdev->bd_contains ==
3642 rdev2->bdev->bd_contains) {
3643 printk(KERN_WARNING
3644 "%s: WARNING: %s appears to be"
3645 " on the same physical disk as"
3646 " %s.\n",
3647 mdname(mddev),
3648 bdevname(rdev->bdev,b),
3649 bdevname(rdev2->bdev,b2));
3650 warned = 1;
3651 }
3652 }
3653 }
3654 if (warned)
3655 printk(KERN_WARNING
3656 "True protection against single-disk"
3657 " failure might be compromised.\n");
3658 }
3659
NeilBrown657390d2005-08-26 18:34:16 -07003660 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003662 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003663 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664
NeilBrownf91de922005-11-08 21:39:36 -08003665 if (start_readonly)
3666 mddev->ro = 2; /* read-only, but switch on first write */
3667
NeilBrownb15c2e52006-01-06 00:20:16 -08003668 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01003669 if (err)
3670 printk(KERN_ERR "md: pers->run() failed ...\n");
3671 else if (mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08003672 err = bitmap_create(mddev);
3673 if (err) {
3674 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3675 mdname(mddev), err);
3676 mddev->pers->stop(mddev);
3677 }
3678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 module_put(mddev->pers->owner);
3681 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003682 bitmap_destroy(mddev);
3683 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003685 if (mddev->pers->sync_request) {
3686 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3687 printk(KERN_WARNING
3688 "md: cannot register extra attributes for %s\n",
3689 mdname(mddev));
3690 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003691 mddev->ro = 0;
3692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 atomic_set(&mddev->writes_pending,0);
3694 mddev->safemode = 0;
3695 mddev->safemode_timer.function = md_safemode_timeout;
3696 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003697 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003699
NeilBrownd089c6a2008-02-06 01:39:59 -08003700 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003701 if (rdev->raid_disk >= 0) {
3702 char nm[20];
3703 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003704 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3705 printk("md: cannot register %s for %s\n",
3706 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3710
NeilBrown850b2b422006-10-03 01:15:46 -07003711 if (mddev->flags)
3712 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
3714 set_capacity(disk, mddev->array_size<<1);
3715
3716 /* If we call blk_queue_make_request here, it will
3717 * re-initialise max_sectors etc which may have been
3718 * refined inside -> run. So just set the bits we need to set.
3719 * Most initialisation happended when we called
3720 * blk_queue_make_request(..., md_fail_request)
3721 * earlier.
3722 */
3723 mddev->queue->queuedata = mddev;
3724 mddev->queue->make_request_fn = mddev->pers->make_request;
3725
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003726 /* If there is a partially-recovered drive we need to
3727 * start recovery here. If we leave it to md_check_recovery,
3728 * it will remove the drives and not do the right thing
3729 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003730 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003731 struct list_head *rtmp;
3732 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003733 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003734 if (rdev->raid_disk >= 0 &&
3735 !test_bit(In_sync, &rdev->flags) &&
3736 !test_bit(Faulty, &rdev->flags))
3737 /* complete an interrupted recovery */
3738 spares++;
3739 if (spares && mddev->pers->sync_request) {
3740 mddev->recovery = 0;
3741 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3742 mddev->sync_thread = md_register_thread(md_do_sync,
3743 mddev,
3744 "%s_resync");
3745 if (!mddev->sync_thread) {
3746 printk(KERN_ERR "%s: could not start resync"
3747 " thread...\n",
3748 mdname(mddev));
3749 /* leave the spares where they are, it shouldn't hurt */
3750 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003751 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003752 }
3753 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003754 md_wakeup_thread(mddev->thread);
3755 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003756
Linus Torvalds44ce62942007-05-09 18:51:36 -07003757 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003758 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003759 sysfs_notify(&mddev->kobj, NULL, "array_state");
Neil Brown72a23c22008-06-28 08:31:41 +10003760 sysfs_notify(&mddev->kobj, NULL, "sync_action");
Neil Browna99ac972008-06-28 08:31:43 +10003761 sysfs_notify(&mddev->kobj, NULL, "degraded");
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003762 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 return 0;
3764}
3765
3766static int restart_array(mddev_t *mddev)
3767{
3768 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Andre Noll80fab1d2008-07-11 22:02:21 +10003770 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10003772 return -ENXIO;
3773 if (!mddev->pers)
3774 return -EINVAL;
3775 if (!mddev->ro)
3776 return -EBUSY;
3777 mddev->safemode = 0;
3778 mddev->ro = 0;
3779 set_disk_ro(disk, 0);
3780 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3781 mdname(mddev));
3782 /* Kick recovery or resync if necessary */
3783 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3784 md_wakeup_thread(mddev->thread);
3785 md_wakeup_thread(mddev->sync_thread);
3786 sysfs_notify(&mddev->kobj, NULL, "array_state");
3787 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788}
3789
NeilBrownacc55e22006-06-26 00:27:47 -07003790/* similar to deny_write_access, but accounts for our holding a reference
3791 * to the file ourselves */
3792static int deny_bitmap_write_access(struct file * file)
3793{
3794 struct inode *inode = file->f_mapping->host;
3795
3796 spin_lock(&inode->i_lock);
3797 if (atomic_read(&inode->i_writecount) > 1) {
3798 spin_unlock(&inode->i_lock);
3799 return -ETXTBSY;
3800 }
3801 atomic_set(&inode->i_writecount, -1);
3802 spin_unlock(&inode->i_lock);
3803
3804 return 0;
3805}
3806
3807static void restore_bitmap_write_access(struct file *file)
3808{
3809 struct inode *inode = file->f_mapping->host;
3810
3811 spin_lock(&inode->i_lock);
3812 atomic_set(&inode->i_writecount, 1);
3813 spin_unlock(&inode->i_lock);
3814}
3815
NeilBrown9e653b62006-06-26 00:27:58 -07003816/* mode:
3817 * 0 - completely stop and dis-assemble array
3818 * 1 - switch to readonly
3819 * 2 - stop but do not disassemble array
3820 */
Neil Browndf5b20c2008-07-11 22:02:22 +10003821static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822{
3823 int err = 0;
3824 struct gendisk *disk = mddev->gendisk;
3825
Neil Browndf5b20c2008-07-11 22:02:22 +10003826 if (atomic_read(&mddev->active) > 1 + is_open) {
3827 printk("md: %s still in use.\n",mdname(mddev));
3828 return -EBUSY;
3829 }
3830
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
3833 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003834 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3836 md_unregister_thread(mddev->sync_thread);
3837 mddev->sync_thread = NULL;
3838 }
3839
3840 del_timer_sync(&mddev->safemode_timer);
3841
3842 invalidate_partition(disk, 0);
3843
NeilBrown9e653b62006-06-26 00:27:58 -07003844 switch(mode) {
3845 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003847 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 goto out;
3849 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003850 break;
3851 case 0: /* disassemble */
3852 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003853 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003854 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 if (mddev->ro)
3856 set_disk_ro(disk, 0);
3857 blk_queue_make_request(mddev->queue, md_fail_request);
3858 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003859 mddev->queue->merge_bvec_fn = NULL;
3860 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003861 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003862 if (mddev->pers->sync_request)
3863 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 module_put(mddev->pers->owner);
3866 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003867 /* tell userspace to handle 'inactive' */
3868 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003869
3870 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003871 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 if (mddev->ro)
3874 mddev->ro = 0;
3875 }
NeilBrown850b2b422006-10-03 01:15:46 -07003876 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 /* mark array as shutdown cleanly */
3878 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07003879 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 }
NeilBrown9e653b62006-06-26 00:27:58 -07003881 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003883 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 }
NeilBrown32a76272005-06-21 17:17:14 -07003885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 /*
3887 * Free resources if final stop
3888 */
NeilBrown9e653b62006-06-26 00:27:58 -07003889 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003890 mdk_rdev_t *rdev;
3891 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003892
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3894
NeilBrown978f9462006-02-02 14:28:05 -08003895 bitmap_destroy(mddev);
3896 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003897 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003898 fput(mddev->bitmap_file);
3899 mddev->bitmap_file = NULL;
3900 }
3901 mddev->bitmap_offset = 0;
3902
NeilBrownd089c6a2008-02-06 01:39:59 -08003903 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003904 if (rdev->raid_disk >= 0) {
3905 char nm[20];
3906 sprintf(nm, "rd%d", rdev->raid_disk);
3907 sysfs_remove_link(&mddev->kobj, nm);
3908 }
3909
NeilBrown177a99b2008-02-06 01:39:56 -08003910 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003911 flush_scheduled_work();
3912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 export_array(mddev);
3914
3915 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003916 mddev->size = 0;
3917 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003918 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003919 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003920 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003921 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003922 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003923 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003924 mddev->level = LEVEL_NONE;
3925 mddev->clevel[0] = 0;
3926 mddev->flags = 0;
3927 mddev->ro = 0;
3928 mddev->metadata_type[0] = 0;
3929 mddev->chunk_size = 0;
3930 mddev->ctime = mddev->utime = 0;
3931 mddev->layout = 0;
3932 mddev->max_disks = 0;
3933 mddev->events = 0;
3934 mddev->delta_disks = 0;
3935 mddev->new_level = LEVEL_NONE;
3936 mddev->new_layout = 0;
3937 mddev->new_chunk = 0;
3938 mddev->curr_resync = 0;
3939 mddev->resync_mismatches = 0;
3940 mddev->suspend_lo = mddev->suspend_hi = 0;
3941 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3942 mddev->recovery = 0;
3943 mddev->in_sync = 0;
3944 mddev->changed = 0;
3945 mddev->degraded = 0;
3946 mddev->barriers_work = 0;
3947 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003948
NeilBrowna8a55c32006-06-26 00:27:39 -07003949 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3951 mdname(mddev));
3952 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003953 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003954 sysfs_notify(&mddev->kobj, NULL, "array_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955out:
3956 return err;
3957}
3958
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003959#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960static void autorun_array(mddev_t *mddev)
3961{
3962 mdk_rdev_t *rdev;
3963 struct list_head *tmp;
3964 int err;
3965
NeilBrowna757e642005-04-16 15:26:42 -07003966 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
3969 printk(KERN_INFO "md: running: ");
3970
NeilBrownd089c6a2008-02-06 01:39:59 -08003971 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 char b[BDEVNAME_SIZE];
3973 printk("<%s>", bdevname(rdev->bdev,b));
3974 }
3975 printk("\n");
3976
3977 err = do_md_run (mddev);
3978 if (err) {
3979 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
Neil Browndf5b20c2008-07-11 22:02:22 +10003980 do_md_stop (mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
3982}
3983
3984/*
3985 * lets try to run arrays based on all disks that have arrived
3986 * until now. (those are in pending_raid_disks)
3987 *
3988 * the method: pick the first pending disk, collect all disks with
3989 * the same UUID, remove all from the pending list and put them into
3990 * the 'same_array' list. Then order this list based on superblock
3991 * update time (freshest comes first), kick out 'old' disks and
3992 * compare superblocks. If everything's fine then run it.
3993 *
3994 * If "unit" is allocated, then bump its reference count
3995 */
3996static void autorun_devices(int part)
3997{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 struct list_head *tmp;
3999 mdk_rdev_t *rdev0, *rdev;
4000 mddev_t *mddev;
4001 char b[BDEVNAME_SIZE];
4002
4003 printk(KERN_INFO "md: autorun ...\n");
4004 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004005 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004007 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 rdev0 = list_entry(pending_raid_disks.next,
4009 mdk_rdev_t, same_set);
4010
4011 printk(KERN_INFO "md: considering %s ...\n",
4012 bdevname(rdev0->bdev,b));
4013 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08004014 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 if (super_90_load(rdev, rdev0, 0) >= 0) {
4016 printk(KERN_INFO "md: adding %s ...\n",
4017 bdevname(rdev->bdev,b));
4018 list_move(&rdev->same_set, &candidates);
4019 }
4020 /*
4021 * now we have a set of devices, with all of them having
4022 * mostly sane superblocks. It's time to allocate the
4023 * mddev.
4024 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004025 if (part) {
4026 dev = MKDEV(mdp_major,
4027 rdev0->preferred_minor << MdpMinorShift);
4028 unit = MINOR(dev) >> MdpMinorShift;
4029 } else {
4030 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4031 unit = MINOR(dev);
4032 }
4033 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4035 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4036 break;
4037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
4039 md_probe(dev, NULL, NULL);
4040 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004041 if (!mddev || !mddev->gendisk) {
4042 if (mddev)
4043 mddev_put(mddev);
4044 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 "md: cannot allocate memory for md drive.\n");
4046 break;
4047 }
4048 if (mddev_lock(mddev))
4049 printk(KERN_WARNING "md: %s locked, cannot run\n",
4050 mdname(mddev));
4051 else if (mddev->raid_disks || mddev->major_version
4052 || !list_empty(&mddev->disks)) {
4053 printk(KERN_WARNING
4054 "md: %s already running, cannot run %s\n",
4055 mdname(mddev), bdevname(rdev0->bdev,b));
4056 mddev_unlock(mddev);
4057 } else {
4058 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004059 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08004060 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 list_del_init(&rdev->same_set);
4062 if (bind_rdev_to_array(rdev, mddev))
4063 export_rdev(rdev);
4064 }
4065 autorun_array(mddev);
4066 mddev_unlock(mddev);
4067 }
4068 /* on success, candidates will be empty, on error
4069 * it won't...
4070 */
NeilBrown73c34432008-02-06 01:39:59 -08004071 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 export_rdev(rdev);
4073 mddev_put(mddev);
4074 }
4075 printk(KERN_INFO "md: ... autorun DONE.\n");
4076}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004077#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079static int get_version(void __user * arg)
4080{
4081 mdu_version_t ver;
4082
4083 ver.major = MD_MAJOR_VERSION;
4084 ver.minor = MD_MINOR_VERSION;
4085 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4086
4087 if (copy_to_user(arg, &ver, sizeof(ver)))
4088 return -EFAULT;
4089
4090 return 0;
4091}
4092
4093static int get_array_info(mddev_t * mddev, void __user * arg)
4094{
4095 mdu_array_info_t info;
4096 int nr,working,active,failed,spare;
4097 mdk_rdev_t *rdev;
4098 struct list_head *tmp;
4099
4100 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004101 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004103 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 failed++;
4105 else {
4106 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004107 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 active++;
4109 else
4110 spare++;
4111 }
4112 }
4113
4114 info.major_version = mddev->major_version;
4115 info.minor_version = mddev->minor_version;
4116 info.patch_version = MD_PATCHLEVEL_VERSION;
4117 info.ctime = mddev->ctime;
4118 info.level = mddev->level;
4119 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004120 if (info.size != mddev->size) /* overflow */
4121 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 info.nr_disks = nr;
4123 info.raid_disks = mddev->raid_disks;
4124 info.md_minor = mddev->md_minor;
4125 info.not_persistent= !mddev->persistent;
4126
4127 info.utime = mddev->utime;
4128 info.state = 0;
4129 if (mddev->in_sync)
4130 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004131 if (mddev->bitmap && mddev->bitmap_offset)
4132 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 info.active_disks = active;
4134 info.working_disks = working;
4135 info.failed_disks = failed;
4136 info.spare_disks = spare;
4137
4138 info.layout = mddev->layout;
4139 info.chunk_size = mddev->chunk_size;
4140
4141 if (copy_to_user(arg, &info, sizeof(info)))
4142 return -EFAULT;
4143
4144 return 0;
4145}
4146
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004147static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004148{
4149 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4150 char *ptr, *buf = NULL;
4151 int err = -ENOMEM;
4152
Dan Williamsb5470dc2008-06-27 21:44:04 -07004153 if (md_allow_write(mddev))
4154 file = kmalloc(sizeof(*file), GFP_NOIO);
4155 else
4156 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004157
NeilBrown32a76272005-06-21 17:17:14 -07004158 if (!file)
4159 goto out;
4160
4161 /* bitmap disabled, zero the first byte and copy out */
4162 if (!mddev->bitmap || !mddev->bitmap->file) {
4163 file->pathname[0] = '\0';
4164 goto copy_out;
4165 }
4166
4167 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4168 if (!buf)
4169 goto out;
4170
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004171 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4172 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004173 goto out;
4174
4175 strcpy(file->pathname, ptr);
4176
4177copy_out:
4178 err = 0;
4179 if (copy_to_user(arg, file, sizeof(*file)))
4180 err = -EFAULT;
4181out:
4182 kfree(buf);
4183 kfree(file);
4184 return err;
4185}
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187static int get_disk_info(mddev_t * mddev, void __user * arg)
4188{
4189 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 mdk_rdev_t *rdev;
4191
4192 if (copy_from_user(&info, arg, sizeof(info)))
4193 return -EFAULT;
4194
Andre Noll26ef3792008-07-11 22:02:21 +10004195 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 if (rdev) {
4197 info.major = MAJOR(rdev->bdev->bd_dev);
4198 info.minor = MINOR(rdev->bdev->bd_dev);
4199 info.raid_disk = rdev->raid_disk;
4200 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004201 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004203 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 info.state |= (1<<MD_DISK_ACTIVE);
4205 info.state |= (1<<MD_DISK_SYNC);
4206 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004207 if (test_bit(WriteMostly, &rdev->flags))
4208 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 } else {
4210 info.major = info.minor = 0;
4211 info.raid_disk = -1;
4212 info.state = (1<<MD_DISK_REMOVED);
4213 }
4214
4215 if (copy_to_user(arg, &info, sizeof(info)))
4216 return -EFAULT;
4217
4218 return 0;
4219}
4220
4221static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4222{
4223 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4224 mdk_rdev_t *rdev;
4225 dev_t dev = MKDEV(info->major,info->minor);
4226
4227 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4228 return -EOVERFLOW;
4229
4230 if (!mddev->raid_disks) {
4231 int err;
4232 /* expecting a device which has a superblock */
4233 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4234 if (IS_ERR(rdev)) {
4235 printk(KERN_WARNING
4236 "md: md_import_device returned %ld\n",
4237 PTR_ERR(rdev));
4238 return PTR_ERR(rdev);
4239 }
4240 if (!list_empty(&mddev->disks)) {
4241 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4242 mdk_rdev_t, same_set);
4243 int err = super_types[mddev->major_version]
4244 .load_super(rdev, rdev0, mddev->minor_version);
4245 if (err < 0) {
4246 printk(KERN_WARNING
4247 "md: %s has different UUID to %s\n",
4248 bdevname(rdev->bdev,b),
4249 bdevname(rdev0->bdev,b2));
4250 export_rdev(rdev);
4251 return -EINVAL;
4252 }
4253 }
4254 err = bind_rdev_to_array(rdev, mddev);
4255 if (err)
4256 export_rdev(rdev);
4257 return err;
4258 }
4259
4260 /*
4261 * add_new_disk can be used once the array is assembled
4262 * to add "hot spares". They must already have a superblock
4263 * written
4264 */
4265 if (mddev->pers) {
4266 int err;
4267 if (!mddev->pers->hot_add_disk) {
4268 printk(KERN_WARNING
4269 "%s: personality does not support diskops!\n",
4270 mdname(mddev));
4271 return -EINVAL;
4272 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004273 if (mddev->persistent)
4274 rdev = md_import_device(dev, mddev->major_version,
4275 mddev->minor_version);
4276 else
4277 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 if (IS_ERR(rdev)) {
4279 printk(KERN_WARNING
4280 "md: md_import_device returned %ld\n",
4281 PTR_ERR(rdev));
4282 return PTR_ERR(rdev);
4283 }
NeilBrown41158c72005-06-21 17:17:25 -07004284 /* set save_raid_disk if appropriate */
4285 if (!mddev->persistent) {
4286 if (info->state & (1<<MD_DISK_SYNC) &&
4287 info->raid_disk < mddev->raid_disks)
4288 rdev->raid_disk = info->raid_disk;
4289 else
4290 rdev->raid_disk = -1;
4291 } else
4292 super_types[mddev->major_version].
4293 validate_super(mddev, rdev);
4294 rdev->saved_raid_disk = rdev->raid_disk;
4295
NeilBrownb2d444d2005-11-08 21:39:31 -08004296 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004297 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4298 set_bit(WriteMostly, &rdev->flags);
4299
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 rdev->raid_disk = -1;
4301 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004302 if (!err && !mddev->pers->hot_remove_disk) {
4303 /* If there is hot_add_disk but no hot_remove_disk
4304 * then added disks for geometry changes,
4305 * and should be added immediately.
4306 */
4307 super_types[mddev->major_version].
4308 validate_super(mddev, rdev);
4309 err = mddev->pers->hot_add_disk(mddev, rdev);
4310 if (err)
4311 unbind_rdev_from_array(rdev);
4312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 if (err)
4314 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004315 else
4316 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc3617772005-06-21 17:17:10 -07004317
NeilBrown17571282006-12-10 02:20:52 -08004318 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004319 if (mddev->degraded)
4320 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004321 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004322 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return err;
4324 }
4325
4326 /* otherwise, add_new_disk is only allowed
4327 * for major_version==0 superblocks
4328 */
4329 if (mddev->major_version != 0) {
4330 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4331 mdname(mddev));
4332 return -EINVAL;
4333 }
4334
4335 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4336 int err;
4337 rdev = md_import_device (dev, -1, 0);
4338 if (IS_ERR(rdev)) {
4339 printk(KERN_WARNING
4340 "md: error, md_import_device() returned %ld\n",
4341 PTR_ERR(rdev));
4342 return PTR_ERR(rdev);
4343 }
4344 rdev->desc_nr = info->number;
4345 if (info->raid_disk < mddev->raid_disks)
4346 rdev->raid_disk = info->raid_disk;
4347 else
4348 rdev->raid_disk = -1;
4349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004351 if (info->state & (1<<MD_DISK_SYNC))
4352 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004354 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4355 set_bit(WriteMostly, &rdev->flags);
4356
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 if (!mddev->persistent) {
4358 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4359 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4360 } else
4361 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4362 rdev->size = calc_dev_size(rdev, mddev->chunk_size);
4363
NeilBrown2bf071b2006-01-06 00:20:55 -08004364 err = bind_rdev_to_array(rdev, mddev);
4365 if (err) {
4366 export_rdev(rdev);
4367 return err;
4368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 }
4370
4371 return 0;
4372}
4373
4374static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4375{
4376 char b[BDEVNAME_SIZE];
4377 mdk_rdev_t *rdev;
4378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 rdev = find_rdev(mddev, dev);
4380 if (!rdev)
4381 return -ENXIO;
4382
4383 if (rdev->raid_disk >= 0)
4384 goto busy;
4385
4386 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07004387 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004388 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 return 0;
4391busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004392 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 bdevname(rdev->bdev,b), mdname(mddev));
4394 return -EBUSY;
4395}
4396
4397static int hot_add_disk(mddev_t * mddev, dev_t dev)
4398{
4399 char b[BDEVNAME_SIZE];
4400 int err;
4401 unsigned int size;
4402 mdk_rdev_t *rdev;
4403
4404 if (!mddev->pers)
4405 return -ENODEV;
4406
4407 if (mddev->major_version != 0) {
4408 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4409 " version-0 superblocks.\n",
4410 mdname(mddev));
4411 return -EINVAL;
4412 }
4413 if (!mddev->pers->hot_add_disk) {
4414 printk(KERN_WARNING
4415 "%s: personality does not support diskops!\n",
4416 mdname(mddev));
4417 return -EINVAL;
4418 }
4419
4420 rdev = md_import_device (dev, -1, 0);
4421 if (IS_ERR(rdev)) {
4422 printk(KERN_WARNING
4423 "md: error, md_import_device() returned %ld\n",
4424 PTR_ERR(rdev));
4425 return -EINVAL;
4426 }
4427
4428 if (mddev->persistent)
4429 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4430 else
4431 rdev->sb_offset =
4432 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4433
4434 size = calc_dev_size(rdev, mddev->chunk_size);
4435 rdev->size = size;
4436
NeilBrownb2d444d2005-11-08 21:39:31 -08004437 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 printk(KERN_WARNING
4439 "md: can not hot-add faulty %s disk to %s!\n",
4440 bdevname(rdev->bdev,b), mdname(mddev));
4441 err = -EINVAL;
4442 goto abort_export;
4443 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004444 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004446 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004447 err = bind_rdev_to_array(rdev, mddev);
4448 if (err)
4449 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
4451 /*
4452 * The rest should better be atomic, we can have disk failures
4453 * noticed in interrupt contexts ...
4454 */
4455
4456 if (rdev->desc_nr == mddev->max_disks) {
4457 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4458 mdname(mddev));
4459 err = -EBUSY;
4460 goto abort_unbind_export;
4461 }
4462
4463 rdev->raid_disk = -1;
4464
NeilBrown850b2b422006-10-03 01:15:46 -07004465 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
4467 /*
4468 * Kick recovery, maybe this spare has to be added to the
4469 * array immediately.
4470 */
4471 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4472 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004473 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 return 0;
4475
4476abort_unbind_export:
4477 unbind_rdev_from_array(rdev);
4478
4479abort_export:
4480 export_rdev(rdev);
4481 return err;
4482}
4483
NeilBrown32a76272005-06-21 17:17:14 -07004484static int set_bitmap_file(mddev_t *mddev, int fd)
4485{
4486 int err;
4487
NeilBrown36fa3062005-09-09 16:23:45 -07004488 if (mddev->pers) {
4489 if (!mddev->pers->quiesce)
4490 return -EBUSY;
4491 if (mddev->recovery || mddev->sync_thread)
4492 return -EBUSY;
4493 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004494 }
4495
NeilBrown36fa3062005-09-09 16:23:45 -07004496
4497 if (fd >= 0) {
4498 if (mddev->bitmap)
4499 return -EEXIST; /* cannot add when bitmap is present */
4500 mddev->bitmap_file = fget(fd);
4501
4502 if (mddev->bitmap_file == NULL) {
4503 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4504 mdname(mddev));
4505 return -EBADF;
4506 }
4507
4508 err = deny_bitmap_write_access(mddev->bitmap_file);
4509 if (err) {
4510 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4511 mdname(mddev));
4512 fput(mddev->bitmap_file);
4513 mddev->bitmap_file = NULL;
4514 return err;
4515 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004516 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004517 } else if (mddev->bitmap == NULL)
4518 return -ENOENT; /* cannot remove what isn't there */
4519 err = 0;
4520 if (mddev->pers) {
4521 mddev->pers->quiesce(mddev, 1);
4522 if (fd >= 0)
4523 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004524 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004525 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004526 fd = -1; /* make sure to put the file */
4527 }
NeilBrown36fa3062005-09-09 16:23:45 -07004528 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004529 }
4530 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004531 if (mddev->bitmap_file) {
4532 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004533 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004534 }
NeilBrown36fa3062005-09-09 16:23:45 -07004535 mddev->bitmap_file = NULL;
4536 }
4537
NeilBrown32a76272005-06-21 17:17:14 -07004538 return err;
4539}
4540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541/*
4542 * set_array_info is used two different ways
4543 * The original usage is when creating a new array.
4544 * In this usage, raid_disks is > 0 and it together with
4545 * level, size, not_persistent,layout,chunksize determine the
4546 * shape of the array.
4547 * This will always create an array with a type-0.90.0 superblock.
4548 * The newer usage is when assembling an array.
4549 * In this case raid_disks will be 0, and the major_version field is
4550 * use to determine which style super-blocks are to be found on the devices.
4551 * The minor and patch _version numbers are also kept incase the
4552 * super_block handler wishes to interpret them.
4553 */
4554static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4555{
4556
4557 if (info->raid_disks == 0) {
4558 /* just setting version number for superblock loading */
4559 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004560 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 super_types[info->major_version].name == NULL) {
4562 /* maybe try to auto-load a module? */
4563 printk(KERN_INFO
4564 "md: superblock version %d not known\n",
4565 info->major_version);
4566 return -EINVAL;
4567 }
4568 mddev->major_version = info->major_version;
4569 mddev->minor_version = info->minor_version;
4570 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004571 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return 0;
4573 }
4574 mddev->major_version = MD_MAJOR_VERSION;
4575 mddev->minor_version = MD_MINOR_VERSION;
4576 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4577 mddev->ctime = get_seconds();
4578
4579 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004580 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 mddev->size = info->size;
4582 mddev->raid_disks = info->raid_disks;
4583 /* don't set md_minor, it is determined by which /dev/md* was
4584 * openned
4585 */
4586 if (info->state & (1<<MD_SB_CLEAN))
4587 mddev->recovery_cp = MaxSector;
4588 else
4589 mddev->recovery_cp = 0;
4590 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004591 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
4593 mddev->layout = info->layout;
4594 mddev->chunk_size = info->chunk_size;
4595
4596 mddev->max_disks = MD_SB_DISKS;
4597
NeilBrowne6910632008-02-06 01:39:51 -08004598 if (mddev->persistent)
4599 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07004600 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
NeilBrownb2a27032005-11-28 13:44:12 -08004602 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4603 mddev->bitmap_offset = 0;
4604
NeilBrownf6705572006-03-27 01:18:11 -08004605 mddev->reshape_position = MaxSector;
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 /*
4608 * Generate a 128 bit UUID
4609 */
4610 get_random_bytes(mddev->uuid, 16);
4611
NeilBrownf6705572006-03-27 01:18:11 -08004612 mddev->new_level = mddev->level;
4613 mddev->new_chunk = mddev->chunk_size;
4614 mddev->new_layout = mddev->layout;
4615 mddev->delta_disks = 0;
4616
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 return 0;
4618}
4619
NeilBrowna35b0d62006-01-06 00:20:49 -08004620static int update_size(mddev_t *mddev, unsigned long size)
4621{
4622 mdk_rdev_t * rdev;
4623 int rv;
4624 struct list_head *tmp;
NeilBrown8ddeeae2006-03-27 01:18:18 -08004625 int fit = (size == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004626
4627 if (mddev->pers->resize == NULL)
4628 return -EINVAL;
4629 /* The "size" is the amount of each device that is used.
4630 * This can only make sense for arrays with redundancy.
4631 * linear and raid0 always use whatever space is available
4632 * We can only consider changing the size if no resync
4633 * or reconstruction is happening, and if the new size
4634 * is acceptable. It must fit before the sb_offset or,
4635 * if that is <data_offset, it must fit before the
4636 * size of each device.
4637 * If size is zero, we find the largest size that fits.
4638 */
4639 if (mddev->sync_thread)
4640 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004641 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004642 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004643 avail = rdev->size * 2;
4644
NeilBrowna35b0d62006-01-06 00:20:49 -08004645 if (fit && (size == 0 || size > avail/2))
4646 size = avail/2;
4647 if (avail < ((sector_t)size << 1))
4648 return -ENOSPC;
4649 }
4650 rv = mddev->pers->resize(mddev, (sector_t)size *2);
4651 if (!rv) {
4652 struct block_device *bdev;
4653
4654 bdev = bdget_disk(mddev->gendisk, 0);
4655 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004656 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004657 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004658 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004659 bdput(bdev);
4660 }
4661 }
4662 return rv;
4663}
4664
NeilBrownda943b992006-01-06 00:20:54 -08004665static int update_raid_disks(mddev_t *mddev, int raid_disks)
4666{
4667 int rv;
4668 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004669 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004670 return -EINVAL;
4671 if (raid_disks <= 0 ||
4672 raid_disks >= mddev->max_disks)
4673 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004674 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004675 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004676 mddev->delta_disks = raid_disks - mddev->raid_disks;
4677
4678 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004679 return rv;
4680}
4681
4682
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683/*
4684 * update_array_info is used to change the configuration of an
4685 * on-line array.
4686 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4687 * fields in the info are checked against the array.
4688 * Any differences that cannot be handled will cause an error.
4689 * Normally, only one change can be managed at a time.
4690 */
4691static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4692{
4693 int rv = 0;
4694 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004695 int state = 0;
4696
4697 /* calculate expected state,ignoring low bits */
4698 if (mddev->bitmap && mddev->bitmap_offset)
4699 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700
4701 if (mddev->major_version != info->major_version ||
4702 mddev->minor_version != info->minor_version ||
4703/* mddev->patch_version != info->patch_version || */
4704 mddev->ctime != info->ctime ||
4705 mddev->level != info->level ||
4706/* mddev->layout != info->layout || */
4707 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004708 mddev->chunk_size != info->chunk_size ||
4709 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4710 ((state^info->state) & 0xfffffe00)
4711 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 return -EINVAL;
4713 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004714 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 if (mddev->raid_disks != info->raid_disks) cnt++;
4716 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004717 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 if (cnt == 0) return 0;
4719 if (cnt > 1) return -EINVAL;
4720
4721 if (mddev->layout != info->layout) {
4722 /* Change layout
4723 * we don't need to do anything at the md level, the
4724 * personality will take care of it all.
4725 */
4726 if (mddev->pers->reconfig == NULL)
4727 return -EINVAL;
4728 else
4729 return mddev->pers->reconfig(mddev, info->layout, -1);
4730 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004731 if (info->size >= 0 && mddev->size != info->size)
NeilBrowna35b0d62006-01-06 00:20:49 -08004732 rv = update_size(mddev, info->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
NeilBrownda943b992006-01-06 00:20:54 -08004734 if (mddev->raid_disks != info->raid_disks)
4735 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736
NeilBrown36fa3062005-09-09 16:23:45 -07004737 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4738 if (mddev->pers->quiesce == NULL)
4739 return -EINVAL;
4740 if (mddev->recovery || mddev->sync_thread)
4741 return -EBUSY;
4742 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4743 /* add the bitmap */
4744 if (mddev->bitmap)
4745 return -EEXIST;
4746 if (mddev->default_bitmap_offset == 0)
4747 return -EINVAL;
4748 mddev->bitmap_offset = mddev->default_bitmap_offset;
4749 mddev->pers->quiesce(mddev, 1);
4750 rv = bitmap_create(mddev);
4751 if (rv)
4752 bitmap_destroy(mddev);
4753 mddev->pers->quiesce(mddev, 0);
4754 } else {
4755 /* remove the bitmap */
4756 if (!mddev->bitmap)
4757 return -ENOENT;
4758 if (mddev->bitmap->file)
4759 return -EINVAL;
4760 mddev->pers->quiesce(mddev, 1);
4761 bitmap_destroy(mddev);
4762 mddev->pers->quiesce(mddev, 0);
4763 mddev->bitmap_offset = 0;
4764 }
4765 }
NeilBrown850b2b422006-10-03 01:15:46 -07004766 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 return rv;
4768}
4769
4770static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4771{
4772 mdk_rdev_t *rdev;
4773
4774 if (mddev->pers == NULL)
4775 return -ENODEV;
4776
4777 rdev = find_rdev(mddev, dev);
4778 if (!rdev)
4779 return -ENODEV;
4780
4781 md_error(mddev, rdev);
4782 return 0;
4783}
4784
Andre Noll2f9618c2008-04-25 18:57:58 +02004785/*
4786 * We have a problem here : there is no easy way to give a CHS
4787 * virtual geometry. We currently pretend that we have a 2 heads
4788 * 4 sectors (with a BIG number of cylinders...). This drives
4789 * dosfs just mad... ;-)
4790 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004791static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4792{
4793 mddev_t *mddev = bdev->bd_disk->private_data;
4794
4795 geo->heads = 2;
4796 geo->sectors = 4;
4797 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4798 return 0;
4799}
4800
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801static int md_ioctl(struct inode *inode, struct file *file,
4802 unsigned int cmd, unsigned long arg)
4803{
4804 int err = 0;
4805 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806 mddev_t *mddev = NULL;
4807
4808 if (!capable(CAP_SYS_ADMIN))
4809 return -EACCES;
4810
4811 /*
4812 * Commands dealing with the RAID driver but not any
4813 * particular array:
4814 */
4815 switch (cmd)
4816 {
4817 case RAID_VERSION:
4818 err = get_version(argp);
4819 goto done;
4820
4821 case PRINT_RAID_DEBUG:
4822 err = 0;
4823 md_print_devices();
4824 goto done;
4825
4826#ifndef MODULE
4827 case RAID_AUTORUN:
4828 err = 0;
4829 autostart_arrays(arg);
4830 goto done;
4831#endif
4832 default:;
4833 }
4834
4835 /*
4836 * Commands creating/starting a new array:
4837 */
4838
4839 mddev = inode->i_bdev->bd_disk->private_data;
4840
4841 if (!mddev) {
4842 BUG();
4843 goto abort;
4844 }
4845
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 err = mddev_lock(mddev);
4847 if (err) {
4848 printk(KERN_INFO
4849 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4850 err, cmd);
4851 goto abort;
4852 }
4853
4854 switch (cmd)
4855 {
4856 case SET_ARRAY_INFO:
4857 {
4858 mdu_array_info_t info;
4859 if (!arg)
4860 memset(&info, 0, sizeof(info));
4861 else if (copy_from_user(&info, argp, sizeof(info))) {
4862 err = -EFAULT;
4863 goto abort_unlock;
4864 }
4865 if (mddev->pers) {
4866 err = update_array_info(mddev, &info);
4867 if (err) {
4868 printk(KERN_WARNING "md: couldn't update"
4869 " array info. %d\n", err);
4870 goto abort_unlock;
4871 }
4872 goto done_unlock;
4873 }
4874 if (!list_empty(&mddev->disks)) {
4875 printk(KERN_WARNING
4876 "md: array %s already has disks!\n",
4877 mdname(mddev));
4878 err = -EBUSY;
4879 goto abort_unlock;
4880 }
4881 if (mddev->raid_disks) {
4882 printk(KERN_WARNING
4883 "md: array %s already initialised!\n",
4884 mdname(mddev));
4885 err = -EBUSY;
4886 goto abort_unlock;
4887 }
4888 err = set_array_info(mddev, &info);
4889 if (err) {
4890 printk(KERN_WARNING "md: couldn't set"
4891 " array info. %d\n", err);
4892 goto abort_unlock;
4893 }
4894 }
4895 goto done_unlock;
4896
4897 default:;
4898 }
4899
4900 /*
4901 * Commands querying/configuring an existing array:
4902 */
NeilBrown32a76272005-06-21 17:17:14 -07004903 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004904 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004905 if ((!mddev->raid_disks && !mddev->external)
4906 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4907 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4908 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909 err = -ENODEV;
4910 goto abort_unlock;
4911 }
4912
4913 /*
4914 * Commands even a read-only array can execute:
4915 */
4916 switch (cmd)
4917 {
4918 case GET_ARRAY_INFO:
4919 err = get_array_info(mddev, argp);
4920 goto done_unlock;
4921
NeilBrown32a76272005-06-21 17:17:14 -07004922 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004923 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004924 goto done_unlock;
4925
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 case GET_DISK_INFO:
4927 err = get_disk_info(mddev, argp);
4928 goto done_unlock;
4929
4930 case RESTART_ARRAY_RW:
4931 err = restart_array(mddev);
4932 goto done_unlock;
4933
4934 case STOP_ARRAY:
Neil Browndf5b20c2008-07-11 22:02:22 +10004935 err = do_md_stop (mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 goto done_unlock;
4937
4938 case STOP_ARRAY_RO:
Neil Browndf5b20c2008-07-11 22:02:22 +10004939 err = do_md_stop (mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 goto done_unlock;
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 }
4943
4944 /*
4945 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004946 * superblock, so we do not allow them on read-only arrays.
4947 * However non-MD ioctls (e.g. get-size) will still come through
4948 * here and hit the 'default' below, so only disallow
4949 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 */
Andre Nollbb57fc62008-04-25 19:06:35 +02004951 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004952 if (mddev->ro == 2) {
4953 mddev->ro = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10004954 sysfs_notify(&mddev->kobj, NULL, "array_state");
4955 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4956 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08004957 } else {
4958 err = -EROFS;
4959 goto abort_unlock;
4960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962
4963 switch (cmd)
4964 {
4965 case ADD_NEW_DISK:
4966 {
4967 mdu_disk_info_t info;
4968 if (copy_from_user(&info, argp, sizeof(info)))
4969 err = -EFAULT;
4970 else
4971 err = add_new_disk(mddev, &info);
4972 goto done_unlock;
4973 }
4974
4975 case HOT_REMOVE_DISK:
4976 err = hot_remove_disk(mddev, new_decode_dev(arg));
4977 goto done_unlock;
4978
4979 case HOT_ADD_DISK:
4980 err = hot_add_disk(mddev, new_decode_dev(arg));
4981 goto done_unlock;
4982
4983 case SET_DISK_FAULTY:
4984 err = set_disk_faulty(mddev, new_decode_dev(arg));
4985 goto done_unlock;
4986
4987 case RUN_ARRAY:
4988 err = do_md_run (mddev);
4989 goto done_unlock;
4990
NeilBrown32a76272005-06-21 17:17:14 -07004991 case SET_BITMAP_FILE:
4992 err = set_bitmap_file(mddev, (int)arg);
4993 goto done_unlock;
4994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 err = -EINVAL;
4997 goto abort_unlock;
4998 }
4999
5000done_unlock:
5001abort_unlock:
5002 mddev_unlock(mddev);
5003
5004 return err;
5005done:
5006 if (err)
5007 MD_BUG();
5008abort:
5009 return err;
5010}
5011
5012static int md_open(struct inode *inode, struct file *file)
5013{
5014 /*
5015 * Succeed if we can lock the mddev, which confirms that
5016 * it isn't being stopped right now.
5017 */
5018 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5019 int err;
5020
NeilBrownd63a5a72006-12-08 02:36:17 -08005021 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 goto out;
5023
5024 err = 0;
5025 mddev_get(mddev);
5026 mddev_unlock(mddev);
5027
5028 check_disk_change(inode->i_bdev);
5029 out:
5030 return err;
5031}
5032
5033static int md_release(struct inode *inode, struct file * file)
5034{
5035 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5036
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005037 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 mddev_put(mddev);
5039
5040 return 0;
5041}
5042
Linus Torvalds44ce62942007-05-09 18:51:36 -07005043static int md_media_changed(struct gendisk *disk)
5044{
5045 mddev_t *mddev = disk->private_data;
5046
5047 return mddev->changed;
5048}
5049
5050static int md_revalidate(struct gendisk *disk)
5051{
5052 mddev_t *mddev = disk->private_data;
5053
5054 mddev->changed = 0;
5055 return 0;
5056}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057static struct block_device_operations md_fops =
5058{
5059 .owner = THIS_MODULE,
5060 .open = md_open,
5061 .release = md_release,
5062 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005063 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005064 .media_changed = md_media_changed,
5065 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066};
5067
Adrian Bunk75c96f82005-05-05 16:16:09 -07005068static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069{
5070 mdk_thread_t *thread = arg;
5071
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 /*
5073 * md_thread is a 'system-thread', it's priority should be very
5074 * high. We avoid resource deadlocks individually in each
5075 * raid personality. (RAID5 does preallocation) We also use RR and
5076 * the very same RT priority as kswapd, thus we will never get
5077 * into a priority inversion deadlock.
5078 *
5079 * we definitely have to have equal or higher priority than
5080 * bdflush, otherwise bdflush will deadlock if there are too
5081 * many dirty RAID5 blocks.
5082 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
NeilBrown6985c432005-10-19 21:23:47 -07005084 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005085 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086
NeilBrown93588e22005-11-15 00:09:12 -08005087 /* We need to wait INTERRUPTIBLE so that
5088 * we don't add to the load-average.
5089 * That means we need to be sure no signals are
5090 * pending
5091 */
5092 if (signal_pending(current))
5093 flush_signals(current);
5094
5095 wait_event_interruptible_timeout
5096 (thread->wqueue,
5097 test_bit(THREAD_WAKEUP, &thread->flags)
5098 || kthread_should_stop(),
5099 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100
5101 clear_bit(THREAD_WAKEUP, &thread->flags);
5102
NeilBrown787453c2005-11-08 21:39:43 -08005103 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005105
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 return 0;
5107}
5108
5109void md_wakeup_thread(mdk_thread_t *thread)
5110{
5111 if (thread) {
5112 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5113 set_bit(THREAD_WAKEUP, &thread->flags);
5114 wake_up(&thread->wqueue);
5115 }
5116}
5117
5118mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5119 const char *name)
5120{
5121 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122
NeilBrown9ffae0c2006-01-06 00:20:32 -08005123 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 if (!thread)
5125 return NULL;
5126
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 init_waitqueue_head(&thread->wqueue);
5128
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 thread->run = run;
5130 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005131 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005132 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005133 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134 kfree(thread);
5135 return NULL;
5136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 return thread;
5138}
5139
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140void md_unregister_thread(mdk_thread_t *thread)
5141{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005142 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005143
5144 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 kfree(thread);
5146}
5147
5148void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5149{
5150 if (!mddev) {
5151 MD_BUG();
5152 return;
5153 }
5154
NeilBrownb2d444d2005-11-08 21:39:31 -08005155 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005157
5158 if (mddev->external)
5159 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5162 mdname(mddev),
5163 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5164 __builtin_return_address(0),__builtin_return_address(1),
5165 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005166*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005167 if (!mddev->pers)
5168 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 if (!mddev->pers->error_handler)
5170 return;
5171 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005172 if (mddev->degraded)
5173 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005174 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5176 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5177 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005178 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179}
5180
5181/* seq_file implementation /proc/mdstat */
5182
5183static void status_unused(struct seq_file *seq)
5184{
5185 int i = 0;
5186 mdk_rdev_t *rdev;
5187 struct list_head *tmp;
5188
5189 seq_printf(seq, "unused devices: ");
5190
NeilBrown73c34432008-02-06 01:39:59 -08005191 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 char b[BDEVNAME_SIZE];
5193 i++;
5194 seq_printf(seq, "%s ",
5195 bdevname(rdev->bdev,b));
5196 }
5197 if (!i)
5198 seq_printf(seq, "<none>");
5199
5200 seq_printf(seq, "\n");
5201}
5202
5203
5204static void status_resync(struct seq_file *seq, mddev_t * mddev)
5205{
NeilBrown4588b422006-03-27 01:18:04 -08005206 sector_t max_blocks, resync, res;
5207 unsigned long dt, db, rt;
5208 int scale;
5209 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
5211 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5212
5213 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5214 max_blocks = mddev->resync_max_sectors >> 1;
5215 else
5216 max_blocks = mddev->size;
5217
5218 /*
5219 * Should not happen.
5220 */
5221 if (!max_blocks) {
5222 MD_BUG();
5223 return;
5224 }
NeilBrown4588b422006-03-27 01:18:04 -08005225 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5226 * in a sector_t, and (max_blocks>>scale) will fit in a
5227 * u32, as those are the requirements for sector_div.
5228 * Thus 'scale' must be at least 10
5229 */
5230 scale = 10;
5231 if (sizeof(sector_t) > sizeof(unsigned long)) {
5232 while ( max_blocks/2 > (1ULL<<(scale+32)))
5233 scale++;
5234 }
5235 res = (resync>>scale)*1000;
5236 sector_div(res, (u32)((max_blocks>>scale)+1));
5237
5238 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 {
NeilBrown4588b422006-03-27 01:18:04 -08005240 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005241 seq_printf(seq, "[");
5242 for (i = 0; i < x; i++)
5243 seq_printf(seq, "=");
5244 seq_printf(seq, ">");
5245 for (i = 0; i < y; i++)
5246 seq_printf(seq, ".");
5247 seq_printf(seq, "] ");
5248 }
NeilBrown4588b422006-03-27 01:18:04 -08005249 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005250 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5251 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005252 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5253 "check" :
5254 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5255 "resync" : "recovery"))),
5256 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005257 (unsigned long long) resync,
5258 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
5260 /*
5261 * We do not want to overflow, so the order of operands and
5262 * the * 100 / 100 trick are important. We do a +1 to be
5263 * safe against division by zero. We only estimate anyway.
5264 *
5265 * dt: time from mark until now
5266 * db: blocks written from mark until now
5267 * rt: remaining time
5268 */
5269 dt = ((jiffies - mddev->resync_mark) / HZ);
5270 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005271 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5272 - mddev->resync_mark_cnt;
5273 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274
5275 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5276
NeilBrownff4e8d92006-07-10 04:44:16 -07005277 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278}
5279
5280static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5281{
5282 struct list_head *tmp;
5283 loff_t l = *pos;
5284 mddev_t *mddev;
5285
5286 if (l >= 0x10000)
5287 return NULL;
5288 if (!l--)
5289 /* header */
5290 return (void*)1;
5291
5292 spin_lock(&all_mddevs_lock);
5293 list_for_each(tmp,&all_mddevs)
5294 if (!l--) {
5295 mddev = list_entry(tmp, mddev_t, all_mddevs);
5296 mddev_get(mddev);
5297 spin_unlock(&all_mddevs_lock);
5298 return mddev;
5299 }
5300 spin_unlock(&all_mddevs_lock);
5301 if (!l--)
5302 return (void*)2;/* tail */
5303 return NULL;
5304}
5305
5306static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5307{
5308 struct list_head *tmp;
5309 mddev_t *next_mddev, *mddev = v;
5310
5311 ++*pos;
5312 if (v == (void*)2)
5313 return NULL;
5314
5315 spin_lock(&all_mddevs_lock);
5316 if (v == (void*)1)
5317 tmp = all_mddevs.next;
5318 else
5319 tmp = mddev->all_mddevs.next;
5320 if (tmp != &all_mddevs)
5321 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5322 else {
5323 next_mddev = (void*)2;
5324 *pos = 0x10000;
5325 }
5326 spin_unlock(&all_mddevs_lock);
5327
5328 if (v != (void*)1)
5329 mddev_put(mddev);
5330 return next_mddev;
5331
5332}
5333
5334static void md_seq_stop(struct seq_file *seq, void *v)
5335{
5336 mddev_t *mddev = v;
5337
5338 if (mddev && v != (void*)1 && v != (void*)2)
5339 mddev_put(mddev);
5340}
5341
NeilBrownd7603b72006-01-06 00:20:30 -08005342struct mdstat_info {
5343 int event;
5344};
5345
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346static int md_seq_show(struct seq_file *seq, void *v)
5347{
5348 mddev_t *mddev = v;
5349 sector_t size;
5350 struct list_head *tmp2;
5351 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005352 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005353 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
5355 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005356 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 seq_printf(seq, "Personalities : ");
5358 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005359 list_for_each_entry(pers, &pers_list, list)
5360 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361
5362 spin_unlock(&pers_lock);
5363 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005364 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 return 0;
5366 }
5367 if (v == (void*)2) {
5368 status_unused(seq);
5369 return 0;
5370 }
5371
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005372 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5376 seq_printf(seq, "%s : %sactive", mdname(mddev),
5377 mddev->pers ? "" : "in");
5378 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005379 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005381 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005382 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 seq_printf(seq, " %s", mddev->pers->name);
5384 }
5385
5386 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005387 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 char b[BDEVNAME_SIZE];
5389 seq_printf(seq, " %s[%d]",
5390 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005391 if (test_bit(WriteMostly, &rdev->flags))
5392 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005393 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 seq_printf(seq, "(F)");
5395 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005396 } else if (rdev->raid_disk < 0)
5397 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 size += rdev->size;
5399 }
5400
5401 if (!list_empty(&mddev->disks)) {
5402 if (mddev->pers)
5403 seq_printf(seq, "\n %llu blocks",
5404 (unsigned long long)mddev->array_size);
5405 else
5406 seq_printf(seq, "\n %llu blocks",
5407 (unsigned long long)size);
5408 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005409 if (mddev->persistent) {
5410 if (mddev->major_version != 0 ||
5411 mddev->minor_version != 90) {
5412 seq_printf(seq," super %d.%d",
5413 mddev->major_version,
5414 mddev->minor_version);
5415 }
NeilBrowne6910632008-02-06 01:39:51 -08005416 } else if (mddev->external)
5417 seq_printf(seq, " super external:%s",
5418 mddev->metadata_type);
5419 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005420 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421
5422 if (mddev->pers) {
5423 mddev->pers->status (seq, mddev);
5424 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005425 if (mddev->pers->sync_request) {
5426 if (mddev->curr_resync > 2) {
5427 status_resync (seq, mddev);
5428 seq_printf(seq, "\n ");
5429 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5430 seq_printf(seq, "\tresync=DELAYED\n ");
5431 else if (mddev->recovery_cp < MaxSector)
5432 seq_printf(seq, "\tresync=PENDING\n ");
5433 }
NeilBrown32a76272005-06-21 17:17:14 -07005434 } else
5435 seq_printf(seq, "\n ");
5436
5437 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005438 unsigned long chunk_kb;
5439 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005440 spin_lock_irqsave(&bitmap->lock, flags);
5441 chunk_kb = bitmap->chunksize >> 10;
5442 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5443 "%lu%s chunk",
5444 bitmap->pages - bitmap->missing_pages,
5445 bitmap->pages,
5446 (bitmap->pages - bitmap->missing_pages)
5447 << (PAGE_SHIFT - 10),
5448 chunk_kb ? chunk_kb : bitmap->chunksize,
5449 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005450 if (bitmap->file) {
5451 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005452 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005453 }
NeilBrown78d742d2005-06-21 17:17:15 -07005454
NeilBrown32a76272005-06-21 17:17:14 -07005455 seq_printf(seq, "\n");
5456 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 }
5458
5459 seq_printf(seq, "\n");
5460 }
5461 mddev_unlock(mddev);
5462
5463 return 0;
5464}
5465
5466static struct seq_operations md_seq_ops = {
5467 .start = md_seq_start,
5468 .next = md_seq_next,
5469 .stop = md_seq_stop,
5470 .show = md_seq_show,
5471};
5472
5473static int md_seq_open(struct inode *inode, struct file *file)
5474{
5475 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005476 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5477 if (mi == NULL)
5478 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479
5480 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005481 if (error)
5482 kfree(mi);
5483 else {
5484 struct seq_file *p = file->private_data;
5485 p->private = mi;
5486 mi->event = atomic_read(&md_event_count);
5487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 return error;
5489}
5490
NeilBrownd7603b72006-01-06 00:20:30 -08005491static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5492{
5493 struct seq_file *m = filp->private_data;
5494 struct mdstat_info *mi = m->private;
5495 int mask;
5496
5497 poll_wait(filp, &md_event_waiters, wait);
5498
5499 /* always allow read */
5500 mask = POLLIN | POLLRDNORM;
5501
5502 if (mi->event != atomic_read(&md_event_count))
5503 mask |= POLLERR | POLLPRI;
5504 return mask;
5505}
5506
Arjan van de Venfa027c22007-02-12 00:55:33 -08005507static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005508 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 .open = md_seq_open,
5510 .read = seq_read,
5511 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005512 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005513 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514};
5515
NeilBrown2604b702006-01-06 00:20:36 -08005516int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005519 list_add_tail(&p->list, &pers_list);
5520 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 spin_unlock(&pers_lock);
5522 return 0;
5523}
5524
NeilBrown2604b702006-01-06 00:20:36 -08005525int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526{
NeilBrown2604b702006-01-06 00:20:36 -08005527 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005529 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 spin_unlock(&pers_lock);
5531 return 0;
5532}
5533
5534static int is_mddev_idle(mddev_t *mddev)
5535{
5536 mdk_rdev_t * rdev;
5537 struct list_head *tmp;
5538 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005539 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
5541 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005542 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005544 curr_events = disk_stat_read(disk, sectors[0]) +
5545 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005547 /* sync IO will cause sync_io to increase before the disk_stats
5548 * as sync_io is counted when a request starts, and
5549 * disk_stats is counted when it completes.
5550 * So resync activity will cause curr_events to be smaller than
5551 * when there was no such activity.
5552 * non-sync IO will cause disk_stat to increase without
5553 * increasing sync_io so curr_events will (eventually)
5554 * be larger than it was before. Once it becomes
5555 * substantially larger, the test below will cause
5556 * the array to appear non-idle, and resync will slow
5557 * down.
5558 * If there is a lot of outstanding resync activity when
5559 * we set last_event to curr_events, then all that activity
5560 * completing might cause the array to appear non-idle
5561 * and resync will be slowed down even though there might
5562 * not have been non-resync activity. This will only
5563 * happen once though. 'last_events' will soon reflect
5564 * the state where there is little or no outstanding
5565 * resync requests, and further resync activity will
5566 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005567 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005569 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 rdev->last_events = curr_events;
5571 idle = 0;
5572 }
5573 }
5574 return idle;
5575}
5576
5577void md_done_sync(mddev_t *mddev, int blocks, int ok)
5578{
5579 /* another "blocks" (512byte) blocks have been synced */
5580 atomic_sub(blocks, &mddev->recovery_active);
5581 wake_up(&mddev->recovery_wait);
5582 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005583 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 md_wakeup_thread(mddev->thread);
5585 // stop recovery, signal do_sync ....
5586 }
5587}
5588
5589
NeilBrown06d91a52005-06-21 17:17:12 -07005590/* md_write_start(mddev, bi)
5591 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005592 * in superblock) before writing, schedule a superblock update
5593 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005594 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005595void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596{
Neil Brown0fd62b82008-06-28 08:31:36 +10005597 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005598 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005599 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005600
NeilBrownf91de922005-11-08 21:39:36 -08005601 BUG_ON(mddev->ro == 1);
5602 if (mddev->ro == 2) {
5603 /* need to switch to read/write */
5604 mddev->ro = 0;
5605 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5606 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005607 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005608 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005609 }
NeilBrown06d91a52005-06-21 17:17:12 -07005610 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005611 if (mddev->safemode == 1)
5612 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005613 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005614 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005615 if (mddev->in_sync) {
5616 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005617 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005618 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005619 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005620 }
NeilBrowna9701a32005-11-08 21:39:34 -08005621 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005622 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005623 if (did_change)
5624 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown09a44cc2008-05-23 13:04:36 -07005625 wait_event(mddev->sb_wait,
5626 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5627 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628}
5629
5630void md_write_end(mddev_t *mddev)
5631{
5632 if (atomic_dec_and_test(&mddev->writes_pending)) {
5633 if (mddev->safemode == 2)
5634 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005635 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5637 }
5638}
5639
NeilBrown2a2275d2007-01-26 00:57:11 -08005640/* md_allow_write(mddev)
5641 * Calling this ensures that the array is marked 'active' so that writes
5642 * may proceed without blocking. It is important to call this before
5643 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5644 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005645 *
5646 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5647 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005648 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005649int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005650{
5651 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005652 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005653 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005654 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005655 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005656 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005657
5658 spin_lock_irq(&mddev->write_lock);
5659 if (mddev->in_sync) {
5660 mddev->in_sync = 0;
5661 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5662 if (mddev->safemode_delay &&
5663 mddev->safemode == 0)
5664 mddev->safemode = 1;
5665 spin_unlock_irq(&mddev->write_lock);
5666 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005667 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown2a2275d2007-01-26 00:57:11 -08005668 } else
5669 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005670
5671 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5672 return -EAGAIN;
5673 else
5674 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005675}
5676EXPORT_SYMBOL_GPL(md_allow_write);
5677
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678#define SYNC_MARKS 10
5679#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005680void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681{
5682 mddev_t *mddev2;
5683 unsigned int currspeed = 0,
5684 window;
NeilBrown57afd892005-06-21 17:17:13 -07005685 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 unsigned long mark[SYNC_MARKS];
5687 sector_t mark_cnt[SYNC_MARKS];
5688 int last_mark,m;
5689 struct list_head *tmp;
5690 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005691 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005692 struct list_head *rtmp;
5693 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005694 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695
5696 /* just incase thread restarts... */
5697 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5698 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005699 if (mddev->ro) /* never try to sync a read-only array */
5700 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
NeilBrown61df9d92006-10-03 01:15:57 -07005702 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5703 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5704 desc = "data-check";
5705 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5706 desc = "requested-resync";
5707 else
5708 desc = "resync";
5709 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5710 desc = "reshape";
5711 else
5712 desc = "recovery";
5713
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 /* we overload curr_resync somewhat here.
5715 * 0 == not engaged in resync at all
5716 * 2 == checking that there is no conflict with another sync
5717 * 1 == like 2, but have yielded to allow conflicting resync to
5718 * commense
5719 * other == active in resync - this many blocks
5720 *
5721 * Before starting a resync we must have set curr_resync to
5722 * 2, and then checked that every "conflicting" array has curr_resync
5723 * less than ours. When we find one that is the same or higher
5724 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5725 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5726 * This will mean we have to start checking from the beginning again.
5727 *
5728 */
5729
5730 do {
5731 mddev->curr_resync = 2;
5732
5733 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005734 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005735 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 goto skip;
5737 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005738 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 if (mddev2 == mddev)
5740 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005741 if (!mddev->parallel_resync
5742 && mddev2->curr_resync
5743 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 DEFINE_WAIT(wq);
5745 if (mddev < mddev2 && mddev->curr_resync == 2) {
5746 /* arbitrarily yield */
5747 mddev->curr_resync = 1;
5748 wake_up(&resync_wait);
5749 }
5750 if (mddev > mddev2 && mddev->curr_resync == 1)
5751 /* no need to wait here, we can wait the next
5752 * time 'round when curr_resync == 2
5753 */
5754 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005755 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5756 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005757 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005758 printk(KERN_INFO "md: delaying %s of %s"
5759 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005761 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 mddev_put(mddev2);
5763 schedule();
5764 finish_wait(&resync_wait, &wq);
5765 goto try_again;
5766 }
5767 finish_wait(&resync_wait, &wq);
5768 }
5769 }
5770 } while (mddev->curr_resync < 2);
5771
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005772 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005773 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005775 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 */
5777 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005778 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005779 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005780 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5781 j = mddev->resync_min;
5782 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005783 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005784
NeilBrownccfcc3c2006-03-27 01:18:09 -08005785 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5786 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005787 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 /* recovery follows the physical size of devices */
5789 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005790 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005791 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005792 if (rdev->raid_disk >= 0 &&
5793 !test_bit(Faulty, &rdev->flags) &&
5794 !test_bit(In_sync, &rdev->flags) &&
5795 rdev->recovery_offset < j)
5796 j = rdev->recovery_offset;
5797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798
NeilBrown61df9d92006-10-03 01:15:57 -07005799 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5800 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5801 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005802 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005803 "(but not more than %d KB/sec) for %s.\n",
5804 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
5806 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005807
NeilBrown57afd892005-06-21 17:17:13 -07005808 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 for (m = 0; m < SYNC_MARKS; m++) {
5810 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005811 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 }
5813 last_mark = 0;
5814 mddev->resync_mark = mark[last_mark];
5815 mddev->resync_mark_cnt = mark_cnt[last_mark];
5816
5817 /*
5818 * Tune reconstruction:
5819 */
5820 window = 32*(PAGE_SIZE/512);
5821 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5822 window/2,(unsigned long long) max_sectors/2);
5823
5824 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005825 last_check = 0;
5826
5827 if (j>2) {
5828 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005829 "md: resuming %s of %s from checkpoint.\n",
5830 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 mddev->curr_resync = j;
5832 }
5833
5834 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005835 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
NeilBrown57afd892005-06-21 17:17:13 -07005837 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005838 if (j >= mddev->resync_max) {
5839 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5840 wait_event(mddev->recovery_wait,
5841 mddev->resync_max > j
5842 || kthread_should_stop());
5843 }
5844 if (kthread_should_stop())
5845 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005846 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005847 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005848 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005849 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850 goto out;
5851 }
NeilBrown57afd892005-06-21 17:17:13 -07005852
5853 if (!skipped) { /* actual IO requested */
5854 io_sectors += sectors;
5855 atomic_add(sectors, &mddev->recovery_active);
5856 }
5857
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858 j += sectors;
5859 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005860 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005861 if (last_check == 0)
5862 /* this is the earliers that rebuilt will be
5863 * visible in /proc/mdstat
5864 */
5865 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005866
5867 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 continue;
5869
NeilBrown57afd892005-06-21 17:17:13 -07005870 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871
NeilBrowndfc70642008-05-23 13:04:39 -07005872 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873 break;
5874
5875 repeat:
5876 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5877 /* step marks */
5878 int next = (last_mark+1) % SYNC_MARKS;
5879
5880 mddev->resync_mark = mark[next];
5881 mddev->resync_mark_cnt = mark_cnt[next];
5882 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005883 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 last_mark = next;
5885 }
5886
5887
NeilBrownc6207272008-02-06 01:39:52 -08005888 if (kthread_should_stop())
5889 goto interrupted;
5890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891
5892 /*
5893 * this loop exits only if either when we are slower than
5894 * the 'hard' speed limit, or the system was IO-idle for
5895 * a jiffy.
5896 * the system might be non-idle CPU-wise, but we only care
5897 * about not overloading the IO subsystem. (things like an
5898 * e2fsck being done on the RAID array should execute fast)
5899 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005900 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 cond_resched();
5902
NeilBrown57afd892005-06-21 17:17:13 -07005903 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5904 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
NeilBrown88202a02006-01-06 00:21:36 -08005906 if (currspeed > speed_min(mddev)) {
5907 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005909 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 goto repeat;
5911 }
5912 }
5913 }
NeilBrown61df9d92006-10-03 01:15:57 -07005914 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 /*
5916 * this also signals 'finished resyncing' to md_stop
5917 */
5918 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005919 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920
5921 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5922
5923 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005924 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
NeilBrowndfc70642008-05-23 13:04:39 -07005926 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005927 mddev->curr_resync > 2) {
5928 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5929 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5930 if (mddev->curr_resync >= mddev->recovery_cp) {
5931 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005932 "md: checkpointing %s of %s.\n",
5933 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005934 mddev->recovery_cp = mddev->curr_resync;
5935 }
5936 } else
5937 mddev->recovery_cp = MaxSector;
5938 } else {
5939 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5940 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005941 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005942 if (rdev->raid_disk >= 0 &&
5943 !test_bit(Faulty, &rdev->flags) &&
5944 !test_bit(In_sync, &rdev->flags) &&
5945 rdev->recovery_offset < mddev->curr_resync)
5946 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 }
NeilBrown17571282006-12-10 02:20:52 -08005949 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951 skip:
5952 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005953 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005954 mddev->resync_max = MaxSector;
5955 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 wake_up(&resync_wait);
5957 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5958 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005959 return;
5960
5961 interrupted:
5962 /*
5963 * got a signal, exit.
5964 */
5965 printk(KERN_INFO
5966 "md: md_do_sync() got signal ... exiting\n");
5967 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5968 goto out;
5969
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970}
NeilBrown29269552006-03-27 01:18:10 -08005971EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972
5973
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005974static int remove_and_add_spares(mddev_t *mddev)
5975{
5976 mdk_rdev_t *rdev;
5977 struct list_head *rtmp;
5978 int spares = 0;
5979
NeilBrownd089c6a2008-02-06 01:39:59 -08005980 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005981 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005982 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005983 (test_bit(Faulty, &rdev->flags) ||
5984 ! test_bit(In_sync, &rdev->flags)) &&
5985 atomic_read(&rdev->nr_pending)==0) {
5986 if (mddev->pers->hot_remove_disk(
5987 mddev, rdev->raid_disk)==0) {
5988 char nm[20];
5989 sprintf(nm,"rd%d", rdev->raid_disk);
5990 sysfs_remove_link(&mddev->kobj, nm);
5991 rdev->raid_disk = -1;
5992 }
5993 }
5994
5995 if (mddev->degraded) {
NeilBrowndfc70642008-05-23 13:04:39 -07005996 rdev_for_each(rdev, rtmp, mddev) {
5997 if (rdev->raid_disk >= 0 &&
5998 !test_bit(In_sync, &rdev->flags))
5999 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006000 if (rdev->raid_disk < 0
6001 && !test_bit(Faulty, &rdev->flags)) {
6002 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006003 if (mddev->pers->
6004 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006005 char nm[20];
6006 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006007 if (sysfs_create_link(&mddev->kobj,
6008 &rdev->kobj, nm))
6009 printk(KERN_WARNING
6010 "md: cannot register "
6011 "%s for %s\n",
6012 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006013 spares++;
6014 md_new_event(mddev);
6015 } else
6016 break;
6017 }
NeilBrowndfc70642008-05-23 13:04:39 -07006018 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006019 }
6020 return spares;
6021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022/*
6023 * This routine is regularly called by all per-raid-array threads to
6024 * deal with generic issues like resync and super-block update.
6025 * Raid personalities that don't have a thread (linear/raid0) do not
6026 * need this as they never do any recovery or update the superblock.
6027 *
6028 * It does not do any resync itself, but rather "forks" off other threads
6029 * to do that as needed.
6030 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6031 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006032 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 * and wakeups up this thread which will reap the thread and finish up.
6034 * This thread also removes any faulty devices (with nr_pending == 0).
6035 *
6036 * The overall approach is:
6037 * 1/ if the superblock needs updating, update it.
6038 * 2/ If a recovery thread is running, don't do anything else.
6039 * 3/ If recovery has finished, clean up, possibly marking spares active.
6040 * 4/ If there are any faulty devices, remove them.
6041 * 5/ If array is degraded, try to add spares devices
6042 * 6/ If array has spares or is not in-sync, start a resync thread.
6043 */
6044void md_check_recovery(mddev_t *mddev)
6045{
6046 mdk_rdev_t *rdev;
6047 struct list_head *rtmp;
6048
6049
NeilBrown5f404022005-06-21 17:17:16 -07006050 if (mddev->bitmap)
6051 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052
6053 if (mddev->ro)
6054 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006055
6056 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006057 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006058 printk(KERN_INFO "md: %s in immediate safe mode\n",
6059 mdname(mddev));
6060 mddev->safemode = 2;
6061 }
6062 flush_signals(current);
6063 }
6064
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006066 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006067 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006068 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006069 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006070 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6071 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 ))
6073 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006074
NeilBrowndf5b89b2006-03-27 01:18:20 -08006075 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006076 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006077
NeilBrown31a59e32008-04-30 00:52:30 -07006078 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006079 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006080 spin_lock_irq(&mddev->write_lock);
6081 if (mddev->safemode &&
6082 !atomic_read(&mddev->writes_pending) &&
6083 !mddev->in_sync &&
6084 mddev->recovery_cp == MaxSector) {
6085 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006086 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006087 if (mddev->persistent)
6088 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6089 }
6090 if (mddev->safemode == 1)
6091 mddev->safemode = 0;
6092 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006093 if (did_change)
6094 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrownfca4d842005-06-21 17:17:11 -07006095 }
NeilBrownfca4d842005-06-21 17:17:11 -07006096
NeilBrown850b2b422006-10-03 01:15:46 -07006097 if (mddev->flags)
6098 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006099
Neil Brown52664732008-06-28 08:31:44 +10006100 rdev_for_each(rdev, rtmp, mddev)
6101 if (test_and_clear_bit(StateChanged, &rdev->flags))
6102 sysfs_notify(&rdev->kobj, NULL, "state");
6103
NeilBrown06d91a52005-06-21 17:17:12 -07006104
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6106 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6107 /* resync/recovery still happening */
6108 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6109 goto unlock;
6110 }
6111 if (mddev->sync_thread) {
6112 /* resync has finished, collect result */
6113 md_unregister_thread(mddev->sync_thread);
6114 mddev->sync_thread = NULL;
NeilBrowndfc70642008-05-23 13:04:39 -07006115 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 /* success...*/
6117 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006118 if (mddev->pers->spare_active(mddev))
6119 sysfs_notify(&mddev->kobj, NULL,
6120 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006121 }
NeilBrown850b2b422006-10-03 01:15:46 -07006122 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006123
6124 /* if array is no-longer degraded, then any saved_raid_disk
6125 * information must be scrapped
6126 */
6127 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006128 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006129 rdev->saved_raid_disk = -1;
6130
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131 mddev->recovery = 0;
6132 /* flag recovery needed just to double check */
6133 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006134 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006135 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006136 goto unlock;
6137 }
Neil Brown72a23c22008-06-28 08:31:41 +10006138 /* Set RUNNING before clearing NEEDED to avoid
6139 * any transients in the value of "sync_action".
6140 */
6141 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6142 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006143 /* Clear some bits that don't mean anything, but
6144 * might be left set
6145 */
NeilBrown24dd4692005-11-08 21:39:26 -08006146 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6147 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006149 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6150 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 /* no recovery is running.
6152 * remove any failed drives, then
6153 * add spares if possible.
6154 * Spare are also removed and re-added, to allow
6155 * the personality to fail the re-add.
6156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006157
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006158 if (mddev->reshape_position != MaxSector) {
6159 if (mddev->pers->check_reshape(mddev) != 0)
6160 /* Cannot proceed */
6161 goto unlock;
6162 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006163 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006164 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006165 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6166 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006167 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006168 } else if (mddev->recovery_cp < MaxSector) {
6169 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006170 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006171 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6172 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006174
Linus Torvalds1da177e2005-04-16 15:20:36 -07006175 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006176 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6177 /* We are adding a device or devices to an array
6178 * which has the bitmap stored on all devices.
6179 * So make sure all bitmap pages get written
6180 */
6181 bitmap_write_all(mddev->bitmap);
6182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 mddev->sync_thread = md_register_thread(md_do_sync,
6184 mddev,
6185 "%s_resync");
6186 if (!mddev->sync_thread) {
6187 printk(KERN_ERR "%s: could not start resync"
6188 " thread...\n",
6189 mdname(mddev));
6190 /* leave the spares where they are, it shouldn't hurt */
6191 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006192 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 md_wakeup_thread(mddev->sync_thread);
Neil Brown72a23c22008-06-28 08:31:41 +10006194 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006195 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 }
6197 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006198 if (!mddev->sync_thread) {
6199 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6200 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6201 &mddev->recovery))
6202 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 mddev_unlock(mddev);
6205 }
6206}
6207
Dan Williams6bfe0b42008-04-30 00:52:32 -07006208void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6209{
6210 sysfs_notify(&rdev->kobj, NULL, "state");
6211 wait_event_timeout(rdev->blocked_wait,
6212 !test_bit(Blocked, &rdev->flags),
6213 msecs_to_jiffies(5000));
6214 rdev_dec_pending(rdev, mddev);
6215}
6216EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6217
Adrian Bunk75c96f82005-05-05 16:16:09 -07006218static int md_notify_reboot(struct notifier_block *this,
6219 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220{
6221 struct list_head *tmp;
6222 mddev_t *mddev;
6223
6224 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6225
6226 printk(KERN_INFO "md: stopping all md devices.\n");
6227
NeilBrown29ac4aa2008-02-06 01:39:58 -08006228 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006229 if (mddev_trylock(mddev)) {
Neil Browndf5b20c2008-07-11 22:02:22 +10006230 do_md_stop (mddev, 1, 0);
Neil Brownc71d4882006-05-26 10:39:25 +10006231 mddev_unlock(mddev);
6232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 /*
6234 * certain more exotic SCSI devices are known to be
6235 * volatile wrt too early system reboots. While the
6236 * right place to handle this issue is the given
6237 * driver, we do want to have a safe RAID driver ...
6238 */
6239 mdelay(1000*1);
6240 }
6241 return NOTIFY_DONE;
6242}
6243
Adrian Bunk75c96f82005-05-05 16:16:09 -07006244static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 .notifier_call = md_notify_reboot,
6246 .next = NULL,
6247 .priority = INT_MAX, /* before any real devices */
6248};
6249
6250static void md_geninit(void)
6251{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6253
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006254 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255}
6256
Adrian Bunk75c96f82005-05-05 16:16:09 -07006257static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 if (register_blkdev(MAJOR_NR, "md"))
6260 return -1;
6261 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6262 unregister_blkdev(MAJOR_NR, "md");
6263 return -1;
6264 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006265 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6266 md_probe, NULL, NULL);
6267 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 md_probe, NULL, NULL);
6269
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006271 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272
6273 md_geninit();
6274 return (0);
6275}
6276
6277
6278#ifndef MODULE
6279
6280/*
6281 * Searches all registered partitions for autorun RAID arrays
6282 * at boot time.
6283 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006284
6285static LIST_HEAD(all_detected_devices);
6286struct detected_devices_node {
6287 struct list_head list;
6288 dev_t dev;
6289};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
6291void md_autodetect_dev(dev_t dev)
6292{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006293 struct detected_devices_node *node_detected_dev;
6294
6295 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6296 if (node_detected_dev) {
6297 node_detected_dev->dev = dev;
6298 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6299 } else {
6300 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6301 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303}
6304
6305
6306static void autostart_arrays(int part)
6307{
6308 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006309 struct detected_devices_node *node_detected_dev;
6310 dev_t dev;
6311 int i_scanned, i_passed;
6312
6313 i_scanned = 0;
6314 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315
6316 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6317
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006318 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6319 i_scanned++;
6320 node_detected_dev = list_entry(all_detected_devices.next,
6321 struct detected_devices_node, list);
6322 list_del(&node_detected_dev->list);
6323 dev = node_detected_dev->dev;
6324 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006325 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 if (IS_ERR(rdev))
6327 continue;
6328
NeilBrownb2d444d2005-11-08 21:39:31 -08006329 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 MD_BUG();
6331 continue;
6332 }
NeilBrownd0fae182008-03-04 14:29:31 -08006333 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006335 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006337
6338 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6339 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
6341 autorun_devices(part);
6342}
6343
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006344#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
6346static __exit void md_exit(void)
6347{
6348 mddev_t *mddev;
6349 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006350
NeilBrowne8703fe2006-10-03 01:15:59 -07006351 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6352 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353
6354 unregister_blkdev(MAJOR_NR,"md");
6355 unregister_blkdev(mdp_major, "mdp");
6356 unregister_reboot_notifier(&md_notifier);
6357 unregister_sysctl_table(raid_table_header);
6358 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006359 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 struct gendisk *disk = mddev->gendisk;
6361 if (!disk)
6362 continue;
6363 export_array(mddev);
6364 del_gendisk(disk);
6365 put_disk(disk);
6366 mddev->gendisk = NULL;
6367 mddev_put(mddev);
6368 }
6369}
6370
Dan Williams685784a2007-07-09 11:56:42 -07006371subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372module_exit(md_exit)
6373
NeilBrownf91de922005-11-08 21:39:36 -08006374static int get_ro(char *buffer, struct kernel_param *kp)
6375{
6376 return sprintf(buffer, "%d", start_readonly);
6377}
6378static int set_ro(const char *val, struct kernel_param *kp)
6379{
6380 char *e;
6381 int num = simple_strtoul(val, &e, 10);
6382 if (*val && (*e == '\0' || *e == '\n')) {
6383 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006384 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006385 }
6386 return -EINVAL;
6387}
6388
NeilBrown80ca3a42006-07-10 04:44:18 -07006389module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6390module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006391
NeilBrownf91de922005-11-08 21:39:36 -08006392
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393EXPORT_SYMBOL(register_md_personality);
6394EXPORT_SYMBOL(unregister_md_personality);
6395EXPORT_SYMBOL(md_error);
6396EXPORT_SYMBOL(md_done_sync);
6397EXPORT_SYMBOL(md_write_start);
6398EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399EXPORT_SYMBOL(md_register_thread);
6400EXPORT_SYMBOL(md_unregister_thread);
6401EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402EXPORT_SYMBOL(md_check_recovery);
6403MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006404MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006405MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);