blob: 19f646a7640243b8049635c12e9dce1eba7966b9 [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
Andre Nolle7debaa2008-07-11 22:02:23 +1000356static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Andre Nolle7debaa2008-07-11 22:02:23 +1000358 sector_t num_sectors = rdev->sb_offset * 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000361 num_sectors &= ~((sector_t)chunk_size/512 - 1);
362 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
365static int alloc_disk_sb(mdk_rdev_t * rdev)
366{
367 if (rdev->sb_page)
368 MD_BUG();
369
370 rdev->sb_page = alloc_page(GFP_KERNEL);
371 if (!rdev->sb_page) {
372 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000373 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 return 0;
377}
378
379static void free_disk_sb(mdk_rdev_t * rdev)
380{
381 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800382 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 rdev->sb_loaded = 0;
384 rdev->sb_page = NULL;
385 rdev->sb_offset = 0;
386 rdev->size = 0;
387 }
388}
389
390
NeilBrown6712ecf2007-09-27 12:47:43 +0200391static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700392{
393 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800394 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700395
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700396 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
397 printk("md: super_written gets error=%d, uptodate=%d\n",
398 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
399 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800400 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700401 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700402
NeilBrowna9701a32005-11-08 21:39:34 -0800403 if (atomic_dec_and_test(&mddev->pending_writes))
404 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700405 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700406}
407
NeilBrown6712ecf2007-09-27 12:47:43 +0200408static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800409{
410 struct bio *bio2 = bio->bi_private;
411 mdk_rdev_t *rdev = bio2->bi_private;
412 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800413
414 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
415 error == -EOPNOTSUPP) {
416 unsigned long flags;
417 /* barriers don't appear to be supported :-( */
418 set_bit(BarriersNotsupp, &rdev->flags);
419 mddev->barriers_work = 0;
420 spin_lock_irqsave(&mddev->write_lock, flags);
421 bio2->bi_next = mddev->biolist;
422 mddev->biolist = bio2;
423 spin_unlock_irqrestore(&mddev->write_lock, flags);
424 wake_up(&mddev->sb_wait);
425 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200426 } else {
427 bio_put(bio2);
428 bio->bi_private = rdev;
429 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800430 }
NeilBrowna9701a32005-11-08 21:39:34 -0800431}
432
NeilBrown7bfa19f2005-06-21 17:17:28 -0700433void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
434 sector_t sector, int size, struct page *page)
435{
436 /* write first size bytes of page to sector of rdev
437 * Increment mddev->pending_writes before returning
438 * and decrement it on completion, waking up sb_wait
439 * if zero is reached.
440 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800441 *
442 * As we might need to resubmit the request if BIO_RW_BARRIER
443 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700444 */
445 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800446 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700447
448 bio->bi_bdev = rdev->bdev;
449 bio->bi_sector = sector;
450 bio_add_page(bio, page, size, 0);
451 bio->bi_private = rdev;
452 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800453 bio->bi_rw = rw;
454
NeilBrown7bfa19f2005-06-21 17:17:28 -0700455 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800456 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
457 struct bio *rbio;
458 rw |= (1<<BIO_RW_BARRIER);
459 rbio = bio_clone(bio, GFP_NOIO);
460 rbio->bi_private = bio;
461 rbio->bi_end_io = super_written_barrier;
462 submit_bio(rw, rbio);
463 } else
464 submit_bio(rw, bio);
465}
466
467void md_super_wait(mddev_t *mddev)
468{
469 /* wait for all superblock writes that were scheduled to complete.
470 * if any had to be retried (due to BARRIER problems), retry them
471 */
472 DEFINE_WAIT(wq);
473 for(;;) {
474 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
475 if (atomic_read(&mddev->pending_writes)==0)
476 break;
477 while (mddev->biolist) {
478 struct bio *bio;
479 spin_lock_irq(&mddev->write_lock);
480 bio = mddev->biolist;
481 mddev->biolist = bio->bi_next ;
482 bio->bi_next = NULL;
483 spin_unlock_irq(&mddev->write_lock);
484 submit_bio(bio->bi_rw, bio);
485 }
486 schedule();
487 }
488 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700489}
490
NeilBrown6712ecf2007-09-27 12:47:43 +0200491static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
NeilBrowna654b9d82005-06-21 17:17:27 -0700496int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 struct page *page, int rw)
498{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700499 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 struct completion event;
501 int ret;
502
503 rw |= (1 << BIO_RW_SYNC);
504
505 bio->bi_bdev = bdev;
506 bio->bi_sector = sector;
507 bio_add_page(bio, page, size, 0);
508 init_completion(&event);
509 bio->bi_private = &event;
510 bio->bi_end_io = bi_complete;
511 submit_bio(rw, bio);
512 wait_for_completion(&event);
513
514 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
515 bio_put(bio);
516 return ret;
517}
NeilBrowna8745db2006-01-06 00:20:34 -0800518EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
NeilBrown0002b272005-09-09 16:23:53 -0700520static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 char b[BDEVNAME_SIZE];
523 if (!rdev->sb_page) {
524 MD_BUG();
525 return -EINVAL;
526 }
527 if (rdev->sb_loaded)
528 return 0;
529
530
NeilBrown0002b272005-09-09 16:23:53 -0700531 if (!sync_page_io(rdev->bdev, rdev->sb_offset<<1, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 goto fail;
533 rdev->sb_loaded = 1;
534 return 0;
535
536fail:
537 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
538 bdevname(rdev->bdev,b));
539 return -EINVAL;
540}
541
542static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
543{
Andre Noll05710462008-07-11 22:02:20 +1000544 return sb1->set_uuid0 == sb2->set_uuid0 &&
545 sb1->set_uuid1 == sb2->set_uuid1 &&
546 sb1->set_uuid2 == sb2->set_uuid2 &&
547 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
551{
552 int ret;
553 mdp_super_t *tmp1, *tmp2;
554
555 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
556 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
557
558 if (!tmp1 || !tmp2) {
559 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100560 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto abort;
562 }
563
564 *tmp1 = *sb1;
565 *tmp2 = *sb2;
566
567 /*
568 * nr_disks is not constant
569 */
570 tmp1->nr_disks = 0;
571 tmp2->nr_disks = 0;
572
Andre Nollce0c8e02008-07-11 22:02:20 +1000573 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700575 kfree(tmp1);
576 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return ret;
578}
579
NeilBrown4d167f02007-05-09 02:35:37 -0700580
581static u32 md_csum_fold(u32 csum)
582{
583 csum = (csum & 0xffff) + (csum >> 16);
584 return (csum & 0xffff) + (csum >> 16);
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587static unsigned int calc_sb_csum(mdp_super_t * sb)
588{
NeilBrown4d167f02007-05-09 02:35:37 -0700589 u64 newcsum = 0;
590 u32 *sb32 = (u32*)sb;
591 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 unsigned int disk_csum, csum;
593
594 disk_csum = sb->sb_csum;
595 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700596
597 for (i = 0; i < MD_SB_BYTES/4 ; i++)
598 newcsum += sb32[i];
599 csum = (newcsum & 0xffffffff) + (newcsum>>32);
600
601
602#ifdef CONFIG_ALPHA
603 /* This used to use csum_partial, which was wrong for several
604 * reasons including that different results are returned on
605 * different architectures. It isn't critical that we get exactly
606 * the same return value as before (we always csum_fold before
607 * testing, and that removes any differences). However as we
608 * know that csum_partial always returned a 16bit value on
609 * alphas, do a fold to maximise conformity to previous behaviour.
610 */
611 sb->sb_csum = md_csum_fold(disk_csum);
612#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700614#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return csum;
616}
617
618
619/*
620 * Handle superblock details.
621 * We want to be able to handle multiple superblock formats
622 * so we have a common interface to them all, and an array of
623 * different handlers.
624 * We rely on user-space to write the initial superblock, and support
625 * reading and updating of superblocks.
626 * Interface methods are:
627 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
628 * loads and validates a superblock on dev.
629 * if refdev != NULL, compare superblocks on both devices
630 * Return:
631 * 0 - dev has a superblock that is compatible with refdev
632 * 1 - dev has a superblock that is compatible and newer than refdev
633 * so dev should be used as the refdev in future
634 * -EINVAL superblock incompatible or invalid
635 * -othererror e.g. -EIO
636 *
637 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
638 * Verify that dev is acceptable into mddev.
639 * The first time, mddev->raid_disks will be 0, and data from
640 * dev should be merged in. Subsequent calls check that dev
641 * is new enough. Return 0 or -EINVAL
642 *
643 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
644 * Update the superblock for rdev with data in mddev
645 * This does not write to disc.
646 *
647 */
648
649struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000650 char *name;
651 struct module *owner;
652 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
653 int minor_version);
654 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
655 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
656 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
657 unsigned long long size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658};
659
660/*
661 * load_super for 0.90.0
662 */
663static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
664{
665 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
666 mdp_super_t *sb;
667 int ret;
668 sector_t sb_offset;
669
670 /*
671 * Calculate the position of the superblock,
672 * it's at the end of the disk.
673 *
674 * It also happens to be a multiple of 4Kb.
675 */
676 sb_offset = calc_dev_sboffset(rdev->bdev);
677 rdev->sb_offset = sb_offset;
678
NeilBrown0002b272005-09-09 16:23:53 -0700679 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (ret) return ret;
681
682 ret = -EINVAL;
683
684 bdevname(rdev->bdev, b);
685 sb = (mdp_super_t*)page_address(rdev->sb_page);
686
687 if (sb->md_magic != MD_SB_MAGIC) {
688 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
689 b);
690 goto abort;
691 }
692
693 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800694 sb->minor_version < 90 ||
695 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
697 sb->major_version, sb->minor_version,
698 b);
699 goto abort;
700 }
701
702 if (sb->raid_disks <= 0)
703 goto abort;
704
NeilBrown4d167f02007-05-09 02:35:37 -0700705 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
707 b);
708 goto abort;
709 }
710
711 rdev->preferred_minor = sb->md_minor;
712 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700713 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
NeilBrowne11e93f2007-05-09 02:35:36 -0700715 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
716 if (sb->level != 1 && sb->level != 4
717 && sb->level != 5 && sb->level != 6
718 && sb->level != 10) {
719 /* FIXME use a better test */
720 printk(KERN_WARNING
721 "md: bitmaps not supported for this level.\n");
722 goto abort;
723 }
724 }
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (sb->level == LEVEL_MULTIPATH)
727 rdev->desc_nr = -1;
728 else
729 rdev->desc_nr = sb->this_disk.number;
730
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700731 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700733 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 __u64 ev1, ev2;
735 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
736 if (!uuid_equal(refsb, sb)) {
737 printk(KERN_WARNING "md: %s has different UUID to %s\n",
738 b, bdevname(refdev->bdev,b2));
739 goto abort;
740 }
741 if (!sb_equal(refsb, sb)) {
742 printk(KERN_WARNING "md: %s has same UUID"
743 " but different superblock to %s\n",
744 b, bdevname(refdev->bdev, b2));
745 goto abort;
746 }
747 ev1 = md_event(sb);
748 ev2 = md_event(refsb);
749 if (ev1 > ev2)
750 ret = 1;
751 else
752 ret = 0;
753 }
Andre Nolle7debaa2008-07-11 22:02:23 +1000754 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
NeilBrown2bf071b2006-01-06 00:20:55 -0800756 if (rdev->size < sb->size && sb->level > 1)
757 /* "this cannot possibly happen" ... */
758 ret = -EINVAL;
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 abort:
761 return ret;
762}
763
764/*
765 * validate_super for 0.90.0
766 */
767static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
768{
769 mdp_disk_t *desc;
770 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700771 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
NeilBrown41158c72005-06-21 17:17:25 -0700773 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800774 clear_bit(Faulty, &rdev->flags);
775 clear_bit(In_sync, &rdev->flags);
776 clear_bit(WriteMostly, &rdev->flags);
777 clear_bit(BarriersNotsupp, &rdev->flags);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (mddev->raid_disks == 0) {
780 mddev->major_version = 0;
781 mddev->minor_version = sb->minor_version;
782 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800783 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 mddev->chunk_size = sb->chunk_size;
785 mddev->ctime = sb->ctime;
786 mddev->utime = sb->utime;
787 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800788 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 mddev->layout = sb->layout;
790 mddev->raid_disks = sb->raid_disks;
791 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700792 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700793 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700794 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
NeilBrownf6705572006-03-27 01:18:11 -0800796 if (mddev->minor_version >= 91) {
797 mddev->reshape_position = sb->reshape_position;
798 mddev->delta_disks = sb->delta_disks;
799 mddev->new_level = sb->new_level;
800 mddev->new_layout = sb->new_layout;
801 mddev->new_chunk = sb->new_chunk;
802 } else {
803 mddev->reshape_position = MaxSector;
804 mddev->delta_disks = 0;
805 mddev->new_level = mddev->level;
806 mddev->new_layout = mddev->layout;
807 mddev->new_chunk = mddev->chunk_size;
808 }
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 if (sb->state & (1<<MD_SB_CLEAN))
811 mddev->recovery_cp = MaxSector;
812 else {
813 if (sb->events_hi == sb->cp_events_hi &&
814 sb->events_lo == sb->cp_events_lo) {
815 mddev->recovery_cp = sb->recovery_cp;
816 } else
817 mddev->recovery_cp = 0;
818 }
819
820 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
821 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
822 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
823 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
824
825 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700826
827 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700828 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700829 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700830
NeilBrown41158c72005-06-21 17:17:25 -0700831 } else if (mddev->pers == NULL) {
832 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 ++ev1;
834 if (ev1 < mddev->events)
835 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700836 } else if (mddev->bitmap) {
837 /* if adding to array with a bitmap, then we can accept an
838 * older device ... but not too old.
839 */
NeilBrown41158c72005-06-21 17:17:25 -0700840 if (ev1 < mddev->bitmap->events_cleared)
841 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700842 } else {
843 if (ev1 < mddev->events)
844 /* just a hot-add of a new device, leave raid_disk at -1 */
845 return 0;
846 }
NeilBrown41158c72005-06-21 17:17:25 -0700847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 desc = sb->disks + rdev->desc_nr;
850
851 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800852 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700853 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
854 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800855 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 rdev->raid_disk = desc->raid_disk;
857 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700858 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
859 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700860 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800861 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 return 0;
863}
864
865/*
866 * sync_super for 0.90.0
867 */
868static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
869{
870 mdp_super_t *sb;
871 struct list_head *tmp;
872 mdk_rdev_t *rdev2;
873 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 /* make rdev->sb match mddev data..
877 *
878 * 1/ zero out disks
879 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
880 * 3/ any empty disks < next_spare become removed
881 *
882 * disks[0] gets initialised to REMOVED because
883 * we cannot be sure from other fields if it has
884 * been initialised or not.
885 */
886 int i;
887 int active=0, working=0,failed=0,spare=0,nr_disks=0;
888
NeilBrown61181562005-09-09 16:24:02 -0700889 rdev->sb_size = MD_SB_BYTES;
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 sb = (mdp_super_t*)page_address(rdev->sb_page);
892
893 memset(sb, 0, sizeof(*sb));
894
895 sb->md_magic = MD_SB_MAGIC;
896 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 sb->patch_version = mddev->patch_version;
898 sb->gvalid_words = 0; /* ignored */
899 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
900 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
901 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
902 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
903
904 sb->ctime = mddev->ctime;
905 sb->level = mddev->level;
906 sb->size = mddev->size;
907 sb->raid_disks = mddev->raid_disks;
908 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800909 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 sb->utime = mddev->utime;
911 sb->state = 0;
912 sb->events_hi = (mddev->events>>32);
913 sb->events_lo = (u32)mddev->events;
914
NeilBrownf6705572006-03-27 01:18:11 -0800915 if (mddev->reshape_position == MaxSector)
916 sb->minor_version = 90;
917 else {
918 sb->minor_version = 91;
919 sb->reshape_position = mddev->reshape_position;
920 sb->new_level = mddev->new_level;
921 sb->delta_disks = mddev->delta_disks;
922 sb->new_layout = mddev->new_layout;
923 sb->new_chunk = mddev->new_chunk;
924 }
925 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (mddev->in_sync)
927 {
928 sb->recovery_cp = mddev->recovery_cp;
929 sb->cp_events_hi = (mddev->events>>32);
930 sb->cp_events_lo = (u32)mddev->events;
931 if (mddev->recovery_cp == MaxSector)
932 sb->state = (1<< MD_SB_CLEAN);
933 } else
934 sb->recovery_cp = 0;
935
936 sb->layout = mddev->layout;
937 sb->chunk_size = mddev->chunk_size;
938
NeilBrowna654b9d82005-06-21 17:17:27 -0700939 if (mddev->bitmap && mddev->bitmap_file == NULL)
940 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800943 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800945 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800946 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
947 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800948 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800950 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800951 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 d = &sb->disks[rdev2->desc_nr];
953 nr_disks++;
954 d->number = rdev2->desc_nr;
955 d->major = MAJOR(rdev2->bdev->bd_dev);
956 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800957 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
958 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 d->raid_disk = rdev2->raid_disk;
960 else
961 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800962 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800964 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 d->state = (1<<MD_DISK_ACTIVE);
966 d->state |= (1<<MD_DISK_SYNC);
967 active++;
968 working++;
969 } else {
970 d->state = 0;
971 spare++;
972 working++;
973 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700974 if (test_bit(WriteMostly, &rdev2->flags))
975 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 /* now set the "removed" and "faulty" bits on any missing devices */
978 for (i=0 ; i < mddev->raid_disks ; i++) {
979 mdp_disk_t *d = &sb->disks[i];
980 if (d->state == 0 && d->number == 0) {
981 d->number = i;
982 d->raid_disk = i;
983 d->state = (1<<MD_DISK_REMOVED);
984 d->state |= (1<<MD_DISK_FAULTY);
985 failed++;
986 }
987 }
988 sb->nr_disks = nr_disks;
989 sb->active_disks = active;
990 sb->working_disks = working;
991 sb->failed_disks = failed;
992 sb->spare_disks = spare;
993
994 sb->this_disk = sb->disks[rdev->desc_nr];
995 sb->sb_csum = calc_sb_csum(sb);
996}
997
998/*
Chris Webb0cd17fe2008-06-28 08:31:46 +1000999 * rdev_size_change for 0.90.0
1000 */
1001static unsigned long long
1002super_90_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1003{
1004 if (size && size < rdev->mddev->size)
1005 return 0; /* component must fit device */
1006 size *= 2; /* convert to sectors */
1007 if (rdev->mddev->bitmap_offset)
1008 return 0; /* can't move bitmap */
1009 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
1010 if (!size || size > rdev->sb_offset*2)
1011 size = rdev->sb_offset*2;
1012 md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size,
1013 rdev->sb_page);
1014 md_super_wait(rdev->mddev);
1015 return size/2; /* kB for sysfs */
1016}
1017
1018
1019/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * version 1 superblock
1021 */
1022
NeilBrown1c05b4b2006-10-21 10:24:08 -07001023static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001025 __le32 disk_csum;
1026 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 unsigned long long newcsum;
1028 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001029 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 int i;
1031
1032 disk_csum = sb->sb_csum;
1033 sb->sb_csum = 0;
1034 newcsum = 0;
1035 for (i=0; size>=4; size -= 4 )
1036 newcsum += le32_to_cpu(*isuper++);
1037
1038 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001039 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1042 sb->sb_csum = disk_csum;
1043 return cpu_to_le32(csum);
1044}
1045
1046static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1047{
1048 struct mdp_superblock_1 *sb;
1049 int ret;
1050 sector_t sb_offset;
1051 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001052 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 /*
1055 * Calculate the position of the superblock.
1056 * It is always aligned to a 4K boundary and
1057 * depeding on minor_version, it can be:
1058 * 0: At least 8K, but less than 12K, from end of device
1059 * 1: At start of device
1060 * 2: 4K from start of device.
1061 */
1062 switch(minor_version) {
1063 case 0:
1064 sb_offset = rdev->bdev->bd_inode->i_size >> 9;
1065 sb_offset -= 8*2;
NeilBrown39730962005-06-21 17:17:28 -07001066 sb_offset &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /* convert from sectors to K */
1068 sb_offset /= 2;
1069 break;
1070 case 1:
1071 sb_offset = 0;
1072 break;
1073 case 2:
1074 sb_offset = 4;
1075 break;
1076 default:
1077 return -EINVAL;
1078 }
1079 rdev->sb_offset = sb_offset;
1080
NeilBrown0002b272005-09-09 16:23:53 -07001081 /* superblock is rarely larger than 1K, but it can be larger,
1082 * and it is safe to read 4k, so we do that
1083 */
1084 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (ret) return ret;
1086
1087
1088 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1089
1090 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1091 sb->major_version != cpu_to_le32(1) ||
1092 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
1093 le64_to_cpu(sb->super_offset) != (rdev->sb_offset<<1) ||
NeilBrown71c08052005-09-09 16:23:51 -07001094 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return -EINVAL;
1096
1097 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1098 printk("md: invalid superblock checksum on %s\n",
1099 bdevname(rdev->bdev,b));
1100 return -EINVAL;
1101 }
1102 if (le64_to_cpu(sb->data_size) < 10) {
1103 printk("md: data_size too small on %s\n",
1104 bdevname(rdev->bdev,b));
1105 return -EINVAL;
1106 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001107 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1108 if (sb->level != cpu_to_le32(1) &&
1109 sb->level != cpu_to_le32(4) &&
1110 sb->level != cpu_to_le32(5) &&
1111 sb->level != cpu_to_le32(6) &&
1112 sb->level != cpu_to_le32(10)) {
1113 printk(KERN_WARNING
1114 "md: bitmaps not supported for this level.\n");
1115 return -EINVAL;
1116 }
1117 }
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 rdev->preferred_minor = 0xffff;
1120 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001121 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
NeilBrown0002b272005-09-09 16:23:53 -07001123 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001124 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001125 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001126 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1127
1128 if (minor_version
1129 && rdev->data_offset < sb_offset + (rdev->sb_size/512))
1130 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001131
NeilBrown31b65a02006-07-10 04:44:14 -07001132 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1133 rdev->desc_nr = -1;
1134 else
1135 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1136
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001137 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001138 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001139 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 __u64 ev1, ev2;
1141 struct mdp_superblock_1 *refsb =
1142 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1143
1144 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1145 sb->level != refsb->level ||
1146 sb->layout != refsb->layout ||
1147 sb->chunksize != refsb->chunksize) {
1148 printk(KERN_WARNING "md: %s has strangely different"
1149 " superblock to %s\n",
1150 bdevname(rdev->bdev,b),
1151 bdevname(refdev->bdev,b2));
1152 return -EINVAL;
1153 }
1154 ev1 = le64_to_cpu(sb->events);
1155 ev2 = le64_to_cpu(refsb->events);
1156
1157 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001158 ret = 1;
1159 else
1160 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
NeilBrowna1801f82008-03-04 14:29:31 -08001162 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1164 else
1165 rdev->size = rdev->sb_offset;
1166 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1167 return -EINVAL;
1168 rdev->size = le64_to_cpu(sb->data_size)/2;
1169 if (le32_to_cpu(sb->chunksize))
1170 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001171
NeilBrown1c05b4b2006-10-21 10:24:08 -07001172 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001173 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001174 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1178{
1179 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001180 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
NeilBrown41158c72005-06-21 17:17:25 -07001182 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001183 clear_bit(Faulty, &rdev->flags);
1184 clear_bit(In_sync, &rdev->flags);
1185 clear_bit(WriteMostly, &rdev->flags);
1186 clear_bit(BarriersNotsupp, &rdev->flags);
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (mddev->raid_disks == 0) {
1189 mddev->major_version = 1;
1190 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001191 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1193 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1194 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1195 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001196 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 mddev->layout = le32_to_cpu(sb->layout);
1198 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1199 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001200 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001201 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001202 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1205 memcpy(mddev->uuid, sb->set_uuid, 16);
1206
1207 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001208
NeilBrown71c08052005-09-09 16:23:51 -07001209 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001210 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001211 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001212
NeilBrownf6705572006-03-27 01:18:11 -08001213 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1214 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1215 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1216 mddev->new_level = le32_to_cpu(sb->new_level);
1217 mddev->new_layout = le32_to_cpu(sb->new_layout);
1218 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1219 } else {
1220 mddev->reshape_position = MaxSector;
1221 mddev->delta_disks = 0;
1222 mddev->new_level = mddev->level;
1223 mddev->new_layout = mddev->layout;
1224 mddev->new_chunk = mddev->chunk_size;
1225 }
1226
NeilBrown41158c72005-06-21 17:17:25 -07001227 } else if (mddev->pers == NULL) {
1228 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 ++ev1;
1230 if (ev1 < mddev->events)
1231 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001232 } else if (mddev->bitmap) {
1233 /* If adding to array with a bitmap, then we can accept an
1234 * older device, but not too old.
1235 */
NeilBrown41158c72005-06-21 17:17:25 -07001236 if (ev1 < mddev->bitmap->events_cleared)
1237 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001238 } else {
1239 if (ev1 < mddev->events)
1240 /* just a hot-add of a new device, leave raid_disk at -1 */
1241 return 0;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (mddev->level != LEVEL_MULTIPATH) {
1244 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1246 switch(role) {
1247 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
1249 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001250 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 break;
1252 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001253 if ((le32_to_cpu(sb->feature_map) &
1254 MD_FEATURE_RECOVERY_OFFSET))
1255 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1256 else
1257 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 rdev->raid_disk = role;
1259 break;
1260 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001261 if (sb->devflags & WriteMostly1)
1262 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001263 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001264 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return 0;
1267}
1268
1269static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1270{
1271 struct mdp_superblock_1 *sb;
1272 struct list_head *tmp;
1273 mdk_rdev_t *rdev2;
1274 int max_dev, i;
1275 /* make rdev->sb match mddev and rdev data. */
1276
1277 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1278
1279 sb->feature_map = 0;
1280 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001281 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 memset(sb->pad1, 0, sizeof(sb->pad1));
1283 memset(sb->pad2, 0, sizeof(sb->pad2));
1284 memset(sb->pad3, 0, sizeof(sb->pad3));
1285
1286 sb->utime = cpu_to_le64((__u64)mddev->utime);
1287 sb->events = cpu_to_le64(mddev->events);
1288 if (mddev->in_sync)
1289 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1290 else
1291 sb->resync_offset = cpu_to_le64(0);
1292
NeilBrown1c05b4b2006-10-21 10:24:08 -07001293 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001294
NeilBrownf0ca3402006-02-02 14:28:04 -08001295 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001296 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001297
NeilBrowna654b9d82005-06-21 17:17:27 -07001298 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1299 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001300 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001301 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001302
1303 if (rdev->raid_disk >= 0 &&
1304 !test_bit(In_sync, &rdev->flags) &&
1305 rdev->recovery_offset > 0) {
1306 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1307 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1308 }
1309
NeilBrownf6705572006-03-27 01:18:11 -08001310 if (mddev->reshape_position != MaxSector) {
1311 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1312 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1313 sb->new_layout = cpu_to_le32(mddev->new_layout);
1314 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1315 sb->new_level = cpu_to_le32(mddev->new_level);
1316 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1317 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001320 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (rdev2->desc_nr+1 > max_dev)
1322 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001323
1324 if (max_dev > le32_to_cpu(sb->max_dev))
1325 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 for (i=0; i<max_dev;i++)
1327 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1328
NeilBrownd089c6a2008-02-06 01:39:59 -08001329 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001331 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001333 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001335 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1336 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 else
1338 sb->dev_roles[i] = cpu_to_le16(0xffff);
1339 }
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 sb->sb_csum = calc_sb_1_csum(sb);
1342}
1343
Chris Webb0cd17fe2008-06-28 08:31:46 +10001344static unsigned long long
1345super_1_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1346{
1347 struct mdp_superblock_1 *sb;
1348 unsigned long long max_size;
1349 if (size && size < rdev->mddev->size)
1350 return 0; /* component must fit device */
1351 size *= 2; /* convert to sectors */
1352 if (rdev->sb_offset < rdev->data_offset/2) {
1353 /* minor versions 1 and 2; superblock before data */
1354 max_size = (rdev->bdev->bd_inode->i_size >> 9);
1355 max_size -= rdev->data_offset;
1356 if (!size || size > max_size)
1357 size = max_size;
1358 } else if (rdev->mddev->bitmap_offset) {
1359 /* minor version 0 with bitmap we can't move */
1360 return 0;
1361 } else {
1362 /* minor version 0; superblock after data */
1363 sector_t sb_offset;
1364 sb_offset = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1365 sb_offset &= ~(sector_t)(4*2 - 1);
1366 max_size = rdev->size*2 + sb_offset - rdev->sb_offset*2;
1367 if (!size || size > max_size)
1368 size = max_size;
1369 rdev->sb_offset = sb_offset/2;
1370 }
1371 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1372 sb->data_size = cpu_to_le64(size);
1373 sb->super_offset = rdev->sb_offset*2;
1374 sb->sb_csum = calc_sb_1_csum(sb);
1375 md_super_write(rdev->mddev, rdev, rdev->sb_offset << 1, rdev->sb_size,
1376 rdev->sb_page);
1377 md_super_wait(rdev->mddev);
1378 return size/2; /* kB for sysfs */
1379}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Adrian Bunk75c96f82005-05-05 16:16:09 -07001381static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 [0] = {
1383 .name = "0.90.0",
1384 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001385 .load_super = super_90_load,
1386 .validate_super = super_90_validate,
1387 .sync_super = super_90_sync,
1388 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 },
1390 [1] = {
1391 .name = "md-1",
1392 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001393 .load_super = super_1_load,
1394 .validate_super = super_1_validate,
1395 .sync_super = super_1_sync,
1396 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 },
1398};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1401{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001402 struct list_head *tmp, *tmp2;
1403 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
NeilBrownd089c6a2008-02-06 01:39:59 -08001405 rdev_for_each(rdev, tmp, mddev1)
1406 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001407 if (rdev->bdev->bd_contains ==
1408 rdev2->bdev->bd_contains)
1409 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 return 0;
1412}
1413
1414static LIST_HEAD(pending_raid_disks);
1415
1416static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1417{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001418 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001419 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001420 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001421 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 if (rdev->mddev) {
1424 MD_BUG();
1425 return -EINVAL;
1426 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001427
1428 /* prevent duplicates */
1429 if (find_rdev(mddev, rdev->bdev->bd_dev))
1430 return -EEXIST;
1431
NeilBrown2bf071b2006-01-06 00:20:55 -08001432 /* make sure rdev->size exceeds mddev->size */
1433 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001434 if (mddev->pers) {
1435 /* Cannot change size, so fail
1436 * If mddev->level <= 0, then we don't care
1437 * about aligning sizes (e.g. linear)
1438 */
1439 if (mddev->level > 0)
1440 return -ENOSPC;
1441 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001442 mddev->size = rdev->size;
1443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
1445 /* Verify rdev->desc_nr is unique.
1446 * If it is -1, assign a free number, else
1447 * check number is not in use
1448 */
1449 if (rdev->desc_nr < 0) {
1450 int choice = 0;
1451 if (mddev->pers) choice = mddev->raid_disks;
1452 while (find_rdev_nr(mddev, choice))
1453 choice++;
1454 rdev->desc_nr = choice;
1455 } else {
1456 if (find_rdev_nr(mddev, rdev->desc_nr))
1457 return -EBUSY;
1458 }
NeilBrown19133a42005-11-08 21:39:35 -08001459 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001460 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001461 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001464 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001465
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001466 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001467 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001468
NeilBrownf637b9f2005-11-08 21:39:37 -08001469 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001470 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001471 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001472 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001473 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1474 kobject_del(&rdev->kobj);
1475 goto fail;
1476 }
1477 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001478 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001480
1481 fail:
1482 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1483 b, mdname(mddev));
1484 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485}
1486
NeilBrown177a99b2008-02-06 01:39:56 -08001487static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001488{
1489 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1490 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001491 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1495{
1496 char b[BDEVNAME_SIZE];
1497 if (!rdev->mddev) {
1498 MD_BUG();
1499 return;
1500 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001501 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 list_del_init(&rdev->same_set);
1503 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1504 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001505 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001506
1507 /* We need to delay this, otherwise we can deadlock when
1508 * writing to 'remove' to "dev/state"
1509 */
NeilBrown177a99b2008-02-06 01:39:56 -08001510 INIT_WORK(&rdev->del_work, md_delayed_delete);
1511 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001512 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515/*
1516 * prevent the device from being mounted, repartitioned or
1517 * otherwise reused by a RAID array (or any other kernel
1518 * subsystem), by bd_claiming the device.
1519 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001520static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 int err = 0;
1523 struct block_device *bdev;
1524 char b[BDEVNAME_SIZE];
1525
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001526 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (IS_ERR(bdev)) {
1528 printk(KERN_ERR "md: could not open %s.\n",
1529 __bdevname(dev, b));
1530 return PTR_ERR(bdev);
1531 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001532 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (err) {
1534 printk(KERN_ERR "md: could not bd_claim %s.\n",
1535 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001536 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return err;
1538 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001539 if (!shared)
1540 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 rdev->bdev = bdev;
1542 return err;
1543}
1544
1545static void unlock_rdev(mdk_rdev_t *rdev)
1546{
1547 struct block_device *bdev = rdev->bdev;
1548 rdev->bdev = NULL;
1549 if (!bdev)
1550 MD_BUG();
1551 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001552 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
1555void md_autodetect_dev(dev_t dev);
1556
1557static void export_rdev(mdk_rdev_t * rdev)
1558{
1559 char b[BDEVNAME_SIZE];
1560 printk(KERN_INFO "md: export_rdev(%s)\n",
1561 bdevname(rdev->bdev,b));
1562 if (rdev->mddev)
1563 MD_BUG();
1564 free_disk_sb(rdev);
1565 list_del_init(&rdev->same_set);
1566#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001567 if (test_bit(AutoDetected, &rdev->flags))
1568 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569#endif
1570 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001571 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
1574static void kick_rdev_from_array(mdk_rdev_t * rdev)
1575{
1576 unbind_rdev_from_array(rdev);
1577 export_rdev(rdev);
1578}
1579
1580static void export_array(mddev_t *mddev)
1581{
1582 struct list_head *tmp;
1583 mdk_rdev_t *rdev;
1584
NeilBrownd089c6a2008-02-06 01:39:59 -08001585 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 if (!rdev->mddev) {
1587 MD_BUG();
1588 continue;
1589 }
1590 kick_rdev_from_array(rdev);
1591 }
1592 if (!list_empty(&mddev->disks))
1593 MD_BUG();
1594 mddev->raid_disks = 0;
1595 mddev->major_version = 0;
1596}
1597
1598static void print_desc(mdp_disk_t *desc)
1599{
1600 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1601 desc->major,desc->minor,desc->raid_disk,desc->state);
1602}
1603
1604static void print_sb(mdp_super_t *sb)
1605{
1606 int i;
1607
1608 printk(KERN_INFO
1609 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1610 sb->major_version, sb->minor_version, sb->patch_version,
1611 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1612 sb->ctime);
1613 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1614 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1615 sb->md_minor, sb->layout, sb->chunk_size);
1616 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1617 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1618 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1619 sb->failed_disks, sb->spare_disks,
1620 sb->sb_csum, (unsigned long)sb->events_lo);
1621
1622 printk(KERN_INFO);
1623 for (i = 0; i < MD_SB_DISKS; i++) {
1624 mdp_disk_t *desc;
1625
1626 desc = sb->disks + i;
1627 if (desc->number || desc->major || desc->minor ||
1628 desc->raid_disk || (desc->state && (desc->state != 4))) {
1629 printk(" D %2d: ", i);
1630 print_desc(desc);
1631 }
1632 }
1633 printk(KERN_INFO "md: THIS: ");
1634 print_desc(&sb->this_disk);
1635
1636}
1637
1638static void print_rdev(mdk_rdev_t *rdev)
1639{
1640 char b[BDEVNAME_SIZE];
1641 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1642 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001643 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1644 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (rdev->sb_loaded) {
1646 printk(KERN_INFO "md: rdev superblock:\n");
1647 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1648 } else
1649 printk(KERN_INFO "md: no rdev superblock!\n");
1650}
1651
Adrian Bunk5e563412006-06-26 00:27:42 -07001652static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct list_head *tmp, *tmp2;
1655 mdk_rdev_t *rdev;
1656 mddev_t *mddev;
1657 char b[BDEVNAME_SIZE];
1658
1659 printk("\n");
1660 printk("md: **********************************\n");
1661 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1662 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001663 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
NeilBrown32a76272005-06-21 17:17:14 -07001665 if (mddev->bitmap)
1666 bitmap_print_sb(mddev->bitmap);
1667 else
1668 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001669 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 printk("<%s>", bdevname(rdev->bdev,b));
1671 printk("\n");
1672
NeilBrownd089c6a2008-02-06 01:39:59 -08001673 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 print_rdev(rdev);
1675 }
1676 printk("md: **********************************\n");
1677 printk("\n");
1678}
1679
1680
NeilBrown42543762006-06-26 00:27:57 -07001681static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
NeilBrown42543762006-06-26 00:27:57 -07001683 /* Update each superblock (in-memory image), but
1684 * if we are allowed to, skip spares which already
1685 * have the right event counter, or have one earlier
1686 * (which would mean they aren't being marked as dirty
1687 * with the rest of the array)
1688 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 mdk_rdev_t *rdev;
1690 struct list_head *tmp;
1691
NeilBrownd089c6a2008-02-06 01:39:59 -08001692 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001693 if (rdev->sb_events == mddev->events ||
1694 (nospares &&
1695 rdev->raid_disk < 0 &&
1696 (rdev->sb_events&1)==0 &&
1697 rdev->sb_events+1 == mddev->events)) {
1698 /* Don't update this superblock */
1699 rdev->sb_loaded = 2;
1700 } else {
1701 super_types[mddev->major_version].
1702 sync_super(mddev, rdev);
1703 rdev->sb_loaded = 1;
1704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
1706}
1707
NeilBrown850b2b42006-10-03 01:15:46 -07001708static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 struct list_head *tmp;
1711 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001712 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001713 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
NeilBrown8377bc82008-04-30 00:52:28 -07001715 if (mddev->external)
1716 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001718 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001719
NeilBrown850b2b42006-10-03 01:15:46 -07001720 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1721 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1722 force_change = 1;
1723 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1724 /* just a clean<-> dirty transition, possibly leave spares alone,
1725 * though if events isn't the right even/odd, we will have to do
1726 * spares after all
1727 */
1728 nospares = 1;
1729 if (force_change)
1730 nospares = 0;
1731 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001732 /* If the array is degraded, then skipping spares is both
1733 * dangerous and fairly pointless.
1734 * Dangerous because a device that was removed from the array
1735 * might have a event_count that still looks up-to-date,
1736 * so it can be re-added without a resync.
1737 * Pointless because if there are any spares to skip,
1738 * then a recovery will happen and soon that array won't
1739 * be degraded any more and the spare can go back to sleep then.
1740 */
NeilBrown850b2b42006-10-03 01:15:46 -07001741 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001742
NeilBrown06d91a52005-06-21 17:17:12 -07001743 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001745
1746 /* If this is just a dirty<->clean transition, and the array is clean
1747 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001748 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001749 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001750 && (mddev->events & 1)
1751 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001752 mddev->events--;
1753 else {
1754 /* otherwise we have to go forward and ... */
1755 mddev->events ++;
1756 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1757 /* .. if the array isn't clean, insist on an odd 'events' */
1758 if ((mddev->events&1)==0) {
1759 mddev->events++;
1760 nospares = 0;
1761 }
1762 } else {
1763 /* otherwise insist on an even 'events' (for clean states) */
1764 if ((mddev->events&1)) {
1765 mddev->events++;
1766 nospares = 0;
1767 }
1768 }
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771 if (!mddev->events) {
1772 /*
1773 * oops, this 64-bit counter should never wrap.
1774 * Either we are in around ~1 trillion A.C., assuming
1775 * 1 reboot per second, or we have a bug:
1776 */
1777 MD_BUG();
1778 mddev->events --;
1779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /*
1782 * do not write anything to disk if using
1783 * nonpersistent superblocks
1784 */
NeilBrown06d91a52005-06-21 17:17:12 -07001785 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001786 if (!mddev->external)
1787 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1788
NeilBrowna9701a32005-11-08 21:39:34 -08001789 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001790 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001792 }
NeilBrowne6910632008-02-06 01:39:51 -08001793 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001794 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 dprintk(KERN_INFO
1797 "md: updating %s RAID superblock on device (in sync %d)\n",
1798 mdname(mddev),mddev->in_sync);
1799
NeilBrown4ad13662007-07-17 04:06:13 -07001800 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001801 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 char b[BDEVNAME_SIZE];
1803 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001804 if (rdev->sb_loaded != 1)
1805 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001806 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 dprintk("(skipping faulty ");
1808
1809 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001810 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001811 md_super_write(mddev,rdev,
NeilBrown0002b272005-09-09 16:23:53 -07001812 rdev->sb_offset<<1, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001813 rdev->sb_page);
1814 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1815 bdevname(rdev->bdev,b),
1816 (unsigned long long)rdev->sb_offset);
NeilBrown42543762006-06-26 00:27:57 -07001817 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001818
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 } else
1820 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001821 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 /* only need to write one superblock... */
1823 break;
1824 }
NeilBrowna9701a32005-11-08 21:39:34 -08001825 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001826 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001827
NeilBrowna9701a32005-11-08 21:39:34 -08001828 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001829 if (mddev->in_sync != sync_req ||
1830 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001831 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001832 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001833 goto repeat;
1834 }
NeilBrown850b2b42006-10-03 01:15:46 -07001835 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001836 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001837 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839}
1840
Andre Noll7f6ce762008-03-23 18:34:54 +01001841/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08001842 * We want to accept with case. For this we use cmd_match.
1843 */
1844static int cmd_match(const char *cmd, const char *str)
1845{
1846 /* See if cmd, written into a sysfs file, matches
1847 * str. They must either be the same, or cmd can
1848 * have a trailing newline
1849 */
1850 while (*cmd && *str && *cmd == *str) {
1851 cmd++;
1852 str++;
1853 }
1854 if (*cmd == '\n')
1855 cmd++;
1856 if (*str || *cmd)
1857 return 0;
1858 return 1;
1859}
1860
NeilBrown86e6ffd2005-11-08 21:39:24 -08001861struct rdev_sysfs_entry {
1862 struct attribute attr;
1863 ssize_t (*show)(mdk_rdev_t *, char *);
1864 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1865};
1866
1867static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001868state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001869{
1870 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001871 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001872
NeilBrownb2d444d2005-11-08 21:39:31 -08001873 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001874 len+= sprintf(page+len, "%sfaulty",sep);
1875 sep = ",";
1876 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001877 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001878 len += sprintf(page+len, "%sin_sync",sep);
1879 sep = ",";
1880 }
NeilBrownf6556752006-06-26 00:28:01 -07001881 if (test_bit(WriteMostly, &rdev->flags)) {
1882 len += sprintf(page+len, "%swrite_mostly",sep);
1883 sep = ",";
1884 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001885 if (test_bit(Blocked, &rdev->flags)) {
1886 len += sprintf(page+len, "%sblocked", sep);
1887 sep = ",";
1888 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001889 if (!test_bit(Faulty, &rdev->flags) &&
1890 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001891 len += sprintf(page+len, "%sspare", sep);
1892 sep = ",";
1893 }
1894 return len+sprintf(page+len, "\n");
1895}
1896
NeilBrown45dc2de2006-06-26 00:27:58 -07001897static ssize_t
1898state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1899{
1900 /* can write
1901 * faulty - simulates and error
1902 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001903 * writemostly - sets write_mostly
1904 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001905 * blocked - sets the Blocked flag
1906 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001907 */
1908 int err = -EINVAL;
1909 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1910 md_error(rdev->mddev, rdev);
1911 err = 0;
1912 } else if (cmd_match(buf, "remove")) {
1913 if (rdev->raid_disk >= 0)
1914 err = -EBUSY;
1915 else {
1916 mddev_t *mddev = rdev->mddev;
1917 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001918 if (mddev->pers)
1919 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001920 md_new_event(mddev);
1921 err = 0;
1922 }
NeilBrownf6556752006-06-26 00:28:01 -07001923 } else if (cmd_match(buf, "writemostly")) {
1924 set_bit(WriteMostly, &rdev->flags);
1925 err = 0;
1926 } else if (cmd_match(buf, "-writemostly")) {
1927 clear_bit(WriteMostly, &rdev->flags);
1928 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001929 } else if (cmd_match(buf, "blocked")) {
1930 set_bit(Blocked, &rdev->flags);
1931 err = 0;
1932 } else if (cmd_match(buf, "-blocked")) {
1933 clear_bit(Blocked, &rdev->flags);
1934 wake_up(&rdev->blocked_wait);
1935 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1936 md_wakeup_thread(rdev->mddev->thread);
1937
1938 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001939 }
Neil Brown52664732008-06-28 08:31:44 +10001940 if (!err)
1941 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrown45dc2de2006-06-26 00:27:58 -07001942 return err ? err : len;
1943}
NeilBrown80ca3a42006-07-10 04:44:18 -07001944static struct rdev_sysfs_entry rdev_state =
1945__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001946
1947static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001948errors_show(mdk_rdev_t *rdev, char *page)
1949{
1950 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1951}
1952
1953static ssize_t
1954errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1955{
1956 char *e;
1957 unsigned long n = simple_strtoul(buf, &e, 10);
1958 if (*buf && (*e == 0 || *e == '\n')) {
1959 atomic_set(&rdev->corrected_errors, n);
1960 return len;
1961 }
1962 return -EINVAL;
1963}
1964static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001965__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001966
NeilBrown014236d2006-01-06 00:20:55 -08001967static ssize_t
1968slot_show(mdk_rdev_t *rdev, char *page)
1969{
1970 if (rdev->raid_disk < 0)
1971 return sprintf(page, "none\n");
1972 else
1973 return sprintf(page, "%d\n", rdev->raid_disk);
1974}
1975
1976static ssize_t
1977slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1978{
1979 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001980 int err;
1981 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001982 int slot = simple_strtoul(buf, &e, 10);
1983 if (strncmp(buf, "none", 4)==0)
1984 slot = -1;
1985 else if (e==buf || (*e && *e!= '\n'))
1986 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10001987 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001988 /* Setting 'slot' on an active array requires also
1989 * updating the 'rd%d' link, and communicating
1990 * with the personality with ->hot_*_disk.
1991 * For now we only support removing
1992 * failed/spare devices. This normally happens automatically,
1993 * but not when the metadata is externally managed.
1994 */
NeilBrownc303da62008-02-06 01:39:51 -08001995 if (rdev->raid_disk == -1)
1996 return -EEXIST;
1997 /* personality does all needed checks */
1998 if (rdev->mddev->pers->hot_add_disk == NULL)
1999 return -EINVAL;
2000 err = rdev->mddev->pers->
2001 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2002 if (err)
2003 return err;
2004 sprintf(nm, "rd%d", rdev->raid_disk);
2005 sysfs_remove_link(&rdev->mddev->kobj, nm);
2006 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2007 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002008 } else if (rdev->mddev->pers) {
2009 mdk_rdev_t *rdev2;
2010 struct list_head *tmp;
2011 /* Activating a spare .. or possibly reactivating
2012 * if we every get bitmaps working here.
2013 */
2014
2015 if (rdev->raid_disk != -1)
2016 return -EBUSY;
2017
2018 if (rdev->mddev->pers->hot_add_disk == NULL)
2019 return -EINVAL;
2020
2021 rdev_for_each(rdev2, tmp, rdev->mddev)
2022 if (rdev2->raid_disk == slot)
2023 return -EEXIST;
2024
2025 rdev->raid_disk = slot;
2026 if (test_bit(In_sync, &rdev->flags))
2027 rdev->saved_raid_disk = slot;
2028 else
2029 rdev->saved_raid_disk = -1;
2030 err = rdev->mddev->pers->
2031 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002032 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002033 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002034 return err;
Neil Brown52664732008-06-28 08:31:44 +10002035 } else
2036 sysfs_notify(&rdev->kobj, NULL, "state");
Neil Brown6c2fce22008-06-28 08:31:31 +10002037 sprintf(nm, "rd%d", rdev->raid_disk);
2038 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2039 printk(KERN_WARNING
2040 "md: cannot register "
2041 "%s for %s\n",
2042 nm, mdname(rdev->mddev));
2043
2044 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002045 } else {
2046 if (slot >= rdev->mddev->raid_disks)
2047 return -ENOSPC;
2048 rdev->raid_disk = slot;
2049 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002050 clear_bit(Faulty, &rdev->flags);
2051 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002052 set_bit(In_sync, &rdev->flags);
Neil Brown52664732008-06-28 08:31:44 +10002053 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc303da62008-02-06 01:39:51 -08002054 }
NeilBrown014236d2006-01-06 00:20:55 -08002055 return len;
2056}
2057
2058
2059static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002060__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002061
NeilBrown93c8cad2006-01-06 00:20:56 -08002062static ssize_t
2063offset_show(mdk_rdev_t *rdev, char *page)
2064{
Andrew Morton6961ece2006-01-06 00:20:59 -08002065 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002066}
2067
2068static ssize_t
2069offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2070{
2071 char *e;
2072 unsigned long long offset = simple_strtoull(buf, &e, 10);
2073 if (e==buf || (*e && *e != '\n'))
2074 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002075 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002076 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002077 if (rdev->size && rdev->mddev->external)
2078 /* Must set offset before size, so overlap checks
2079 * can be sane */
2080 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002081 rdev->data_offset = offset;
2082 return len;
2083}
2084
2085static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002086__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002087
NeilBrown83303b62006-01-06 00:21:06 -08002088static ssize_t
2089rdev_size_show(mdk_rdev_t *rdev, char *page)
2090{
2091 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2092}
2093
NeilBrownc5d79ad2008-02-06 01:39:54 -08002094static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2095{
2096 /* check if two start/length pairs overlap */
2097 if (s1+l1 <= s2)
2098 return 0;
2099 if (s2+l2 <= s1)
2100 return 0;
2101 return 1;
2102}
2103
NeilBrown83303b62006-01-06 00:21:06 -08002104static ssize_t
2105rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2106{
2107 char *e;
2108 unsigned long long size = simple_strtoull(buf, &e, 10);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002109 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002110 mddev_t *my_mddev = rdev->mddev;
2111
NeilBrown83303b62006-01-06 00:21:06 -08002112 if (e==buf || (*e && *e != '\n'))
2113 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002114 if (my_mddev->pers && rdev->raid_disk >= 0) {
2115 if (rdev->mddev->persistent) {
2116 size = super_types[rdev->mddev->major_version].
2117 rdev_size_change(rdev, size);
2118 if (!size)
2119 return -EBUSY;
2120 } else if (!size) {
2121 size = (rdev->bdev->bd_inode->i_size >> 10);
2122 size -= rdev->data_offset/2;
2123 }
2124 if (size < rdev->mddev->size)
2125 return -EINVAL; /* component must fit device */
2126 }
2127
NeilBrown83303b62006-01-06 00:21:06 -08002128 rdev->size = size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002129 if (size > oldsize && rdev->mddev->external) {
2130 /* need to check that all other rdevs with the same ->bdev
2131 * do not overlap. We need to unlock the mddev to avoid
2132 * a deadlock. We have already changed rdev->size, and if
2133 * we have to change it back, we will have the lock again.
2134 */
2135 mddev_t *mddev;
2136 int overlap = 0;
2137 struct list_head *tmp, *tmp2;
2138
NeilBrown27c529b2008-03-04 14:29:33 -08002139 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002140 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002141 mdk_rdev_t *rdev2;
2142
2143 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002144 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002145 if (test_bit(AllReserved, &rdev2->flags) ||
2146 (rdev->bdev == rdev2->bdev &&
2147 rdev != rdev2 &&
2148 overlaps(rdev->data_offset, rdev->size,
2149 rdev2->data_offset, rdev2->size))) {
2150 overlap = 1;
2151 break;
2152 }
2153 mddev_unlock(mddev);
2154 if (overlap) {
2155 mddev_put(mddev);
2156 break;
2157 }
2158 }
NeilBrown27c529b2008-03-04 14:29:33 -08002159 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002160 if (overlap) {
2161 /* Someone else could have slipped in a size
2162 * change here, but doing so is just silly.
2163 * We put oldsize back because we *know* it is
2164 * safe, and trust userspace not to race with
2165 * itself
2166 */
2167 rdev->size = oldsize;
2168 return -EBUSY;
2169 }
2170 }
NeilBrown27c529b2008-03-04 14:29:33 -08002171 if (size < my_mddev->size || my_mddev->size == 0)
2172 my_mddev->size = size;
NeilBrown83303b62006-01-06 00:21:06 -08002173 return len;
2174}
2175
2176static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002177__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002178
NeilBrown86e6ffd2005-11-08 21:39:24 -08002179static struct attribute *rdev_default_attrs[] = {
2180 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002181 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002182 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002183 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002184 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002185 NULL,
2186};
2187static ssize_t
2188rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2189{
2190 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2191 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002192 mddev_t *mddev = rdev->mddev;
2193 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002194
2195 if (!entry->show)
2196 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002197
2198 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2199 if (!rv) {
2200 if (rdev->mddev == NULL)
2201 rv = -EBUSY;
2202 else
2203 rv = entry->show(rdev, page);
2204 mddev_unlock(mddev);
2205 }
2206 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002207}
2208
2209static ssize_t
2210rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2211 const char *page, size_t length)
2212{
2213 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2214 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002215 ssize_t rv;
2216 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002217
2218 if (!entry->store)
2219 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002220 if (!capable(CAP_SYS_ADMIN))
2221 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002222 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002223 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002224 if (rdev->mddev == NULL)
2225 rv = -EBUSY;
2226 else
2227 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002228 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002229 }
2230 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002231}
2232
2233static void rdev_free(struct kobject *ko)
2234{
2235 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2236 kfree(rdev);
2237}
2238static struct sysfs_ops rdev_sysfs_ops = {
2239 .show = rdev_attr_show,
2240 .store = rdev_attr_store,
2241};
2242static struct kobj_type rdev_ktype = {
2243 .release = rdev_free,
2244 .sysfs_ops = &rdev_sysfs_ops,
2245 .default_attrs = rdev_default_attrs,
2246};
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248/*
2249 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2250 *
2251 * mark the device faulty if:
2252 *
2253 * - the device is nonexistent (zero size)
2254 * - the device has no valid superblock
2255 *
2256 * a faulty rdev _never_ has rdev->sb set.
2257 */
2258static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2259{
2260 char b[BDEVNAME_SIZE];
2261 int err;
2262 mdk_rdev_t *rdev;
2263 sector_t size;
2264
NeilBrown9ffae0c2006-01-06 00:20:32 -08002265 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (!rdev) {
2267 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2268 return ERR_PTR(-ENOMEM);
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 if ((err = alloc_disk_sb(rdev)))
2272 goto abort_free;
2273
NeilBrownc5d79ad2008-02-06 01:39:54 -08002274 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (err)
2276 goto abort_free;
2277
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002278 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002281 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002282 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002283 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002285 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002287 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002288 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2291 if (!size) {
2292 printk(KERN_WARNING
2293 "md: %s has zero or unknown size, marking faulty!\n",
2294 bdevname(rdev->bdev,b));
2295 err = -EINVAL;
2296 goto abort_free;
2297 }
2298
2299 if (super_format >= 0) {
2300 err = super_types[super_format].
2301 load_super(rdev, NULL, super_minor);
2302 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002303 printk(KERN_WARNING
2304 "md: %s does not have a valid v%d.%d "
2305 "superblock, not importing!\n",
2306 bdevname(rdev->bdev,b),
2307 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 goto abort_free;
2309 }
2310 if (err < 0) {
2311 printk(KERN_WARNING
2312 "md: could not read %s's sb, not importing!\n",
2313 bdevname(rdev->bdev,b));
2314 goto abort_free;
2315 }
2316 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002319 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 return rdev;
2322
2323abort_free:
2324 if (rdev->sb_page) {
2325 if (rdev->bdev)
2326 unlock_rdev(rdev);
2327 free_disk_sb(rdev);
2328 }
2329 kfree(rdev);
2330 return ERR_PTR(err);
2331}
2332
2333/*
2334 * Check a full RAID array for plausibility
2335 */
2336
2337
NeilBrowna757e642005-04-16 15:26:42 -07002338static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
2340 int i;
2341 struct list_head *tmp;
2342 mdk_rdev_t *rdev, *freshest;
2343 char b[BDEVNAME_SIZE];
2344
2345 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002346 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 switch (super_types[mddev->major_version].
2348 load_super(rdev, freshest, mddev->minor_version)) {
2349 case 1:
2350 freshest = rdev;
2351 break;
2352 case 0:
2353 break;
2354 default:
2355 printk( KERN_ERR \
2356 "md: fatal superblock inconsistency in %s"
2357 " -- removing from array\n",
2358 bdevname(rdev->bdev,b));
2359 kick_rdev_from_array(rdev);
2360 }
2361
2362
2363 super_types[mddev->major_version].
2364 validate_super(mddev, freshest);
2365
2366 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002367 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 if (rdev != freshest)
2369 if (super_types[mddev->major_version].
2370 validate_super(mddev, rdev)) {
2371 printk(KERN_WARNING "md: kicking non-fresh %s"
2372 " from array!\n",
2373 bdevname(rdev->bdev,b));
2374 kick_rdev_from_array(rdev);
2375 continue;
2376 }
2377 if (mddev->level == LEVEL_MULTIPATH) {
2378 rdev->desc_nr = i++;
2379 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002380 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002381 } else if (rdev->raid_disk >= mddev->raid_disks) {
2382 rdev->raid_disk = -1;
2383 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385 }
2386
2387
2388
2389 if (mddev->recovery_cp != MaxSector &&
2390 mddev->level >= 1)
2391 printk(KERN_ERR "md: %s: raid array is not clean"
2392 " -- starting background reconstruction\n",
2393 mdname(mddev));
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395}
2396
NeilBrowneae17012005-11-08 21:39:23 -08002397static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002398safe_delay_show(mddev_t *mddev, char *page)
2399{
2400 int msec = (mddev->safemode_delay*1000)/HZ;
2401 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2402}
2403static ssize_t
2404safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2405{
2406 int scale=1;
2407 int dot=0;
2408 int i;
2409 unsigned long msec;
2410 char buf[30];
2411 char *e;
2412 /* remove a period, and count digits after it */
2413 if (len >= sizeof(buf))
2414 return -EINVAL;
2415 strlcpy(buf, cbuf, len);
2416 buf[len] = 0;
2417 for (i=0; i<len; i++) {
2418 if (dot) {
2419 if (isdigit(buf[i])) {
2420 buf[i-1] = buf[i];
2421 scale *= 10;
2422 }
2423 buf[i] = 0;
2424 } else if (buf[i] == '.') {
2425 dot=1;
2426 buf[i] = 0;
2427 }
2428 }
2429 msec = simple_strtoul(buf, &e, 10);
2430 if (e == buf || (*e && *e != '\n'))
2431 return -EINVAL;
2432 msec = (msec * 1000) / scale;
2433 if (msec == 0)
2434 mddev->safemode_delay = 0;
2435 else {
2436 mddev->safemode_delay = (msec*HZ)/1000;
2437 if (mddev->safemode_delay == 0)
2438 mddev->safemode_delay = 1;
2439 }
2440 return len;
2441}
2442static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002443__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002444
2445static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002446level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002447{
NeilBrown2604b702006-01-06 00:20:36 -08002448 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002449 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002450 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002451 else if (mddev->clevel[0])
2452 return sprintf(page, "%s\n", mddev->clevel);
2453 else if (mddev->level != LEVEL_NONE)
2454 return sprintf(page, "%d\n", mddev->level);
2455 else
2456 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002457}
2458
NeilBrownd9d166c2006-01-06 00:20:51 -08002459static ssize_t
2460level_store(mddev_t *mddev, const char *buf, size_t len)
2461{
NeilBrown20a49ff2008-02-06 01:39:57 -08002462 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002463 if (mddev->pers)
2464 return -EBUSY;
2465 if (len == 0)
2466 return 0;
2467 if (len >= sizeof(mddev->clevel))
2468 return -ENOSPC;
2469 strncpy(mddev->clevel, buf, len);
2470 if (mddev->clevel[len-1] == '\n')
2471 len--;
2472 mddev->clevel[len] = 0;
2473 mddev->level = LEVEL_NONE;
2474 return rv;
2475}
2476
2477static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002478__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002479
NeilBrownd4dbd022006-06-26 00:27:59 -07002480
2481static ssize_t
2482layout_show(mddev_t *mddev, char *page)
2483{
2484 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002485 if (mddev->reshape_position != MaxSector &&
2486 mddev->layout != mddev->new_layout)
2487 return sprintf(page, "%d (%d)\n",
2488 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002489 return sprintf(page, "%d\n", mddev->layout);
2490}
2491
2492static ssize_t
2493layout_store(mddev_t *mddev, const char *buf, size_t len)
2494{
2495 char *e;
2496 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002497
2498 if (!*buf || (*e && *e != '\n'))
2499 return -EINVAL;
2500
NeilBrown08a02ec2007-05-09 02:35:38 -07002501 if (mddev->pers)
2502 return -EBUSY;
2503 if (mddev->reshape_position != MaxSector)
2504 mddev->new_layout = n;
2505 else
2506 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002507 return len;
2508}
2509static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002510__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002511
2512
NeilBrowneae17012005-11-08 21:39:23 -08002513static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002514raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002515{
NeilBrownbb636542005-11-08 21:39:45 -08002516 if (mddev->raid_disks == 0)
2517 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002518 if (mddev->reshape_position != MaxSector &&
2519 mddev->delta_disks != 0)
2520 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2521 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002522 return sprintf(page, "%d\n", mddev->raid_disks);
2523}
2524
NeilBrownda943b992006-01-06 00:20:54 -08002525static int update_raid_disks(mddev_t *mddev, int raid_disks);
2526
2527static ssize_t
2528raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2529{
NeilBrownda943b992006-01-06 00:20:54 -08002530 char *e;
2531 int rv = 0;
2532 unsigned long n = simple_strtoul(buf, &e, 10);
2533
2534 if (!*buf || (*e && *e != '\n'))
2535 return -EINVAL;
2536
2537 if (mddev->pers)
2538 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002539 else if (mddev->reshape_position != MaxSector) {
2540 int olddisks = mddev->raid_disks - mddev->delta_disks;
2541 mddev->delta_disks = n - olddisks;
2542 mddev->raid_disks = n;
2543 } else
NeilBrownda943b992006-01-06 00:20:54 -08002544 mddev->raid_disks = n;
2545 return rv ? rv : len;
2546}
2547static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002548__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002549
NeilBrown24dd4692005-11-08 21:39:26 -08002550static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002551chunk_size_show(mddev_t *mddev, char *page)
2552{
NeilBrown08a02ec2007-05-09 02:35:38 -07002553 if (mddev->reshape_position != MaxSector &&
2554 mddev->chunk_size != mddev->new_chunk)
2555 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2556 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002557 return sprintf(page, "%d\n", mddev->chunk_size);
2558}
2559
2560static ssize_t
2561chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2562{
2563 /* can only set chunk_size if array is not yet active */
2564 char *e;
2565 unsigned long n = simple_strtoul(buf, &e, 10);
2566
NeilBrown3b343802006-01-06 00:20:47 -08002567 if (!*buf || (*e && *e != '\n'))
2568 return -EINVAL;
2569
NeilBrown08a02ec2007-05-09 02:35:38 -07002570 if (mddev->pers)
2571 return -EBUSY;
2572 else if (mddev->reshape_position != MaxSector)
2573 mddev->new_chunk = n;
2574 else
2575 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002576 return len;
2577}
2578static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002579__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002580
NeilBrowna94213b2006-06-26 00:28:00 -07002581static ssize_t
2582resync_start_show(mddev_t *mddev, char *page)
2583{
2584 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2585}
2586
2587static ssize_t
2588resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2589{
NeilBrowna94213b2006-06-26 00:28:00 -07002590 char *e;
2591 unsigned long long n = simple_strtoull(buf, &e, 10);
2592
2593 if (mddev->pers)
2594 return -EBUSY;
2595 if (!*buf || (*e && *e != '\n'))
2596 return -EINVAL;
2597
2598 mddev->recovery_cp = n;
2599 return len;
2600}
2601static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002602__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002603
NeilBrown9e653b62006-06-26 00:27:58 -07002604/*
2605 * The array state can be:
2606 *
2607 * clear
2608 * No devices, no size, no level
2609 * Equivalent to STOP_ARRAY ioctl
2610 * inactive
2611 * May have some settings, but array is not active
2612 * all IO results in error
2613 * When written, doesn't tear down array, but just stops it
2614 * suspended (not supported yet)
2615 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002616 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002617 * readonly
2618 * no resync can happen. no superblocks get written.
2619 * write requests fail
2620 * read-auto
2621 * like readonly, but behaves like 'clean' on a write request.
2622 *
2623 * clean - no pending writes, but otherwise active.
2624 * When written to inactive array, starts without resync
2625 * If a write request arrives then
2626 * if metadata is known, mark 'dirty' and switch to 'active'.
2627 * if not known, block and switch to write-pending
2628 * If written to an active array that has pending writes, then fails.
2629 * active
2630 * fully active: IO and resync can be happening.
2631 * When written to inactive array, starts with resync
2632 *
2633 * write-pending
2634 * clean, but writes are blocked waiting for 'active' to be written.
2635 *
2636 * active-idle
2637 * like active, but no writes have been seen for a while (100msec).
2638 *
2639 */
2640enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2641 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002642static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002643 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2644 "write-pending", "active-idle", NULL };
2645
2646static int match_word(const char *word, char **list)
2647{
2648 int n;
2649 for (n=0; list[n]; n++)
2650 if (cmd_match(word, list[n]))
2651 break;
2652 return n;
2653}
2654
2655static ssize_t
2656array_state_show(mddev_t *mddev, char *page)
2657{
2658 enum array_state st = inactive;
2659
2660 if (mddev->pers)
2661 switch(mddev->ro) {
2662 case 1:
2663 st = readonly;
2664 break;
2665 case 2:
2666 st = read_auto;
2667 break;
2668 case 0:
2669 if (mddev->in_sync)
2670 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002671 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2672 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002673 else if (mddev->safemode)
2674 st = active_idle;
2675 else
2676 st = active;
2677 }
2678 else {
2679 if (list_empty(&mddev->disks) &&
2680 mddev->raid_disks == 0 &&
2681 mddev->size == 0)
2682 st = clear;
2683 else
2684 st = inactive;
2685 }
2686 return sprintf(page, "%s\n", array_states[st]);
2687}
2688
Neil Browndf5b20c2008-07-11 22:02:22 +10002689static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002690static int do_md_run(mddev_t * mddev);
2691static int restart_array(mddev_t *mddev);
2692
2693static ssize_t
2694array_state_store(mddev_t *mddev, const char *buf, size_t len)
2695{
2696 int err = -EINVAL;
2697 enum array_state st = match_word(buf, array_states);
2698 switch(st) {
2699 case bad_word:
2700 break;
2701 case clear:
2702 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002703 if (atomic_read(&mddev->active) > 1)
2704 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002705 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002706 break;
2707 case inactive:
2708 /* stopping an active array */
2709 if (mddev->pers) {
2710 if (atomic_read(&mddev->active) > 1)
2711 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002712 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08002713 } else
2714 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002715 break;
2716 case suspended:
2717 break; /* not supported yet */
2718 case readonly:
2719 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10002720 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002721 else {
2722 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002723 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002724 err = do_md_run(mddev);
2725 }
2726 break;
2727 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002728 if (mddev->pers) {
NeilBrown648b6292008-04-30 00:52:30 -07002729 if (mddev->ro != 1)
Neil Browndf5b20c2008-07-11 22:02:22 +10002730 err = do_md_stop(mddev, 1, 0);
NeilBrown648b6292008-04-30 00:52:30 -07002731 else
2732 err = restart_array(mddev);
2733 if (err == 0) {
2734 mddev->ro = 2;
2735 set_disk_ro(mddev->gendisk, 0);
2736 }
NeilBrown9e653b62006-06-26 00:27:58 -07002737 } else {
2738 mddev->ro = 2;
2739 err = do_md_run(mddev);
2740 }
2741 break;
2742 case clean:
2743 if (mddev->pers) {
2744 restart_array(mddev);
2745 spin_lock_irq(&mddev->write_lock);
2746 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002747 if (mddev->in_sync == 0) {
2748 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002749 if (mddev->safemode == 1)
2750 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002751 if (mddev->persistent)
2752 set_bit(MD_CHANGE_CLEAN,
2753 &mddev->flags);
2754 }
2755 err = 0;
2756 } else
2757 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002758 spin_unlock_irq(&mddev->write_lock);
2759 } else {
2760 mddev->ro = 0;
2761 mddev->recovery_cp = MaxSector;
2762 err = do_md_run(mddev);
2763 }
2764 break;
2765 case active:
2766 if (mddev->pers) {
2767 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002768 if (mddev->external)
2769 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002770 wake_up(&mddev->sb_wait);
2771 err = 0;
2772 } else {
2773 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002774 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002775 err = do_md_run(mddev);
2776 }
2777 break;
2778 case write_pending:
2779 case active_idle:
2780 /* these cannot be set */
2781 break;
2782 }
2783 if (err)
2784 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002785 else {
2786 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown9e653b62006-06-26 00:27:58 -07002787 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002788 }
NeilBrown9e653b62006-06-26 00:27:58 -07002789}
NeilBrown80ca3a42006-07-10 04:44:18 -07002790static struct md_sysfs_entry md_array_state =
2791__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002792
NeilBrown6d7ff732006-01-06 00:21:16 -08002793static ssize_t
2794null_show(mddev_t *mddev, char *page)
2795{
2796 return -EINVAL;
2797}
2798
2799static ssize_t
2800new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2801{
2802 /* buf must be %d:%d\n? giving major and minor numbers */
2803 /* The new device is added to the array.
2804 * If the array has a persistent superblock, we read the
2805 * superblock to initialise info and check validity.
2806 * Otherwise, only checking done is that in bind_rdev_to_array,
2807 * which mainly checks size.
2808 */
2809 char *e;
2810 int major = simple_strtoul(buf, &e, 10);
2811 int minor;
2812 dev_t dev;
2813 mdk_rdev_t *rdev;
2814 int err;
2815
2816 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2817 return -EINVAL;
2818 minor = simple_strtoul(e+1, &e, 10);
2819 if (*e && *e != '\n')
2820 return -EINVAL;
2821 dev = MKDEV(major, minor);
2822 if (major != MAJOR(dev) ||
2823 minor != MINOR(dev))
2824 return -EOVERFLOW;
2825
2826
2827 if (mddev->persistent) {
2828 rdev = md_import_device(dev, mddev->major_version,
2829 mddev->minor_version);
2830 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2831 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2832 mdk_rdev_t, same_set);
2833 err = super_types[mddev->major_version]
2834 .load_super(rdev, rdev0, mddev->minor_version);
2835 if (err < 0)
2836 goto out;
2837 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002838 } else if (mddev->external)
2839 rdev = md_import_device(dev, -2, -1);
2840 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002841 rdev = md_import_device(dev, -1, -1);
2842
2843 if (IS_ERR(rdev))
2844 return PTR_ERR(rdev);
2845 err = bind_rdev_to_array(rdev, mddev);
2846 out:
2847 if (err)
2848 export_rdev(rdev);
2849 return err ? err : len;
2850}
2851
2852static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002853__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002854
2855static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002856bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2857{
2858 char *end;
2859 unsigned long chunk, end_chunk;
2860
2861 if (!mddev->bitmap)
2862 goto out;
2863 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2864 while (*buf) {
2865 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2866 if (buf == end) break;
2867 if (*end == '-') { /* range */
2868 buf = end + 1;
2869 end_chunk = simple_strtoul(buf, &end, 0);
2870 if (buf == end) break;
2871 }
2872 if (*end && !isspace(*end)) break;
2873 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2874 buf = end;
2875 while (isspace(*buf)) buf++;
2876 }
2877 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2878out:
2879 return len;
2880}
2881
2882static struct md_sysfs_entry md_bitmap =
2883__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2884
2885static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002886size_show(mddev_t *mddev, char *page)
2887{
2888 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2889}
2890
Andre Nolld71f9f82008-07-11 22:02:22 +10002891static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08002892
2893static ssize_t
2894size_store(mddev_t *mddev, const char *buf, size_t len)
2895{
2896 /* If array is inactive, we can reduce the component size, but
2897 * not increase it (except from 0).
2898 * If array is active, we can try an on-line resize
2899 */
2900 char *e;
2901 int err = 0;
2902 unsigned long long size = simple_strtoull(buf, &e, 10);
2903 if (!*buf || *buf == '\n' ||
2904 (*e && *e != '\n'))
2905 return -EINVAL;
2906
2907 if (mddev->pers) {
Andre Nolld71f9f82008-07-11 22:02:22 +10002908 err = update_size(mddev, size * 2);
NeilBrown850b2b42006-10-03 01:15:46 -07002909 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002910 } else {
2911 if (mddev->size == 0 ||
2912 mddev->size > size)
2913 mddev->size = size;
2914 else
2915 err = -ENOSPC;
2916 }
2917 return err ? err : len;
2918}
2919
2920static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002921__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002922
NeilBrown8bb93aa2006-01-06 00:20:50 -08002923
2924/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002925 * This is one of
2926 * 'none' for arrays with no metadata (good luck...)
2927 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002928 * or N.M for internally known formats
2929 */
2930static ssize_t
2931metadata_show(mddev_t *mddev, char *page)
2932{
2933 if (mddev->persistent)
2934 return sprintf(page, "%d.%d\n",
2935 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002936 else if (mddev->external)
2937 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002938 else
2939 return sprintf(page, "none\n");
2940}
2941
2942static ssize_t
2943metadata_store(mddev_t *mddev, const char *buf, size_t len)
2944{
2945 int major, minor;
2946 char *e;
2947 if (!list_empty(&mddev->disks))
2948 return -EBUSY;
2949
2950 if (cmd_match(buf, "none")) {
2951 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002952 mddev->external = 0;
2953 mddev->major_version = 0;
2954 mddev->minor_version = 90;
2955 return len;
2956 }
2957 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002958 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002959 if (namelen >= sizeof(mddev->metadata_type))
2960 namelen = sizeof(mddev->metadata_type)-1;
2961 strncpy(mddev->metadata_type, buf+9, namelen);
2962 mddev->metadata_type[namelen] = 0;
2963 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2964 mddev->metadata_type[--namelen] = 0;
2965 mddev->persistent = 0;
2966 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002967 mddev->major_version = 0;
2968 mddev->minor_version = 90;
2969 return len;
2970 }
2971 major = simple_strtoul(buf, &e, 10);
2972 if (e==buf || *e != '.')
2973 return -EINVAL;
2974 buf = e+1;
2975 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002976 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002977 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002978 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002979 return -ENOENT;
2980 mddev->major_version = major;
2981 mddev->minor_version = minor;
2982 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002983 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002984 return len;
2985}
2986
2987static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002988__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002989
NeilBrowna35b0d62006-01-06 00:20:49 -08002990static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002991action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002992{
NeilBrown7eec3142005-11-08 21:39:44 -08002993 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002994 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002995 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002996 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2997 type = "reshape";
2998 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002999 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3000 type = "resync";
3001 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3002 type = "check";
3003 else
3004 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003005 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003006 type = "recover";
3007 }
3008 return sprintf(page, "%s\n", type);
3009}
3010
3011static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003012action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003013{
NeilBrown7eec3142005-11-08 21:39:44 -08003014 if (!mddev->pers || !mddev->pers->sync_request)
3015 return -EINVAL;
3016
NeilBrownbce74da2006-01-06 00:20:41 -08003017 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003018 if (mddev->sync_thread) {
3019 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3020 md_unregister_thread(mddev->sync_thread);
3021 mddev->sync_thread = NULL;
3022 mddev->recovery = 0;
3023 }
NeilBrown03c902e2006-01-06 00:20:46 -08003024 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3025 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003026 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003027 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003028 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003029 else if (cmd_match(page, "recover")) {
3030 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3031 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3032 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003033 int err;
3034 if (mddev->pers->start_reshape == NULL)
3035 return -EINVAL;
3036 err = mddev->pers->start_reshape(mddev);
3037 if (err)
3038 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003039 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003040 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003041 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003042 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003043 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003044 return -EINVAL;
3045 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3046 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003047 }
NeilBrown03c902e2006-01-06 00:20:46 -08003048 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003049 md_wakeup_thread(mddev->thread);
Neil Brown72a23c22008-06-28 08:31:41 +10003050 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrown24dd4692005-11-08 21:39:26 -08003051 return len;
3052}
3053
NeilBrown9d888832005-11-08 21:39:26 -08003054static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003055mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003056{
3057 return sprintf(page, "%llu\n",
3058 (unsigned long long) mddev->resync_mismatches);
3059}
3060
NeilBrown80ca3a42006-07-10 04:44:18 -07003061static struct md_sysfs_entry md_scan_mode =
3062__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003063
NeilBrown96de1e62005-11-08 21:39:39 -08003064
NeilBrown80ca3a42006-07-10 04:44:18 -07003065static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003066
NeilBrown88202a02006-01-06 00:21:36 -08003067static ssize_t
3068sync_min_show(mddev_t *mddev, char *page)
3069{
3070 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3071 mddev->sync_speed_min ? "local": "system");
3072}
3073
3074static ssize_t
3075sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3076{
3077 int min;
3078 char *e;
3079 if (strncmp(buf, "system", 6)==0) {
3080 mddev->sync_speed_min = 0;
3081 return len;
3082 }
3083 min = simple_strtoul(buf, &e, 10);
3084 if (buf == e || (*e && *e != '\n') || min <= 0)
3085 return -EINVAL;
3086 mddev->sync_speed_min = min;
3087 return len;
3088}
3089
3090static struct md_sysfs_entry md_sync_min =
3091__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3092
3093static ssize_t
3094sync_max_show(mddev_t *mddev, char *page)
3095{
3096 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3097 mddev->sync_speed_max ? "local": "system");
3098}
3099
3100static ssize_t
3101sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3102{
3103 int max;
3104 char *e;
3105 if (strncmp(buf, "system", 6)==0) {
3106 mddev->sync_speed_max = 0;
3107 return len;
3108 }
3109 max = simple_strtoul(buf, &e, 10);
3110 if (buf == e || (*e && *e != '\n') || max <= 0)
3111 return -EINVAL;
3112 mddev->sync_speed_max = max;
3113 return len;
3114}
3115
3116static struct md_sysfs_entry md_sync_max =
3117__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3118
Iustin Popd7f3d292007-10-16 23:30:54 -07003119static ssize_t
3120degraded_show(mddev_t *mddev, char *page)
3121{
3122 return sprintf(page, "%d\n", mddev->degraded);
3123}
3124static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003125
3126static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003127sync_force_parallel_show(mddev_t *mddev, char *page)
3128{
3129 return sprintf(page, "%d\n", mddev->parallel_resync);
3130}
3131
3132static ssize_t
3133sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3134{
3135 long n;
3136
3137 if (strict_strtol(buf, 10, &n))
3138 return -EINVAL;
3139
3140 if (n != 0 && n != 1)
3141 return -EINVAL;
3142
3143 mddev->parallel_resync = n;
3144
3145 if (mddev->sync_thread)
3146 wake_up(&resync_wait);
3147
3148 return len;
3149}
3150
3151/* force parallel resync, even with shared block devices */
3152static struct md_sysfs_entry md_sync_force_parallel =
3153__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3154 sync_force_parallel_show, sync_force_parallel_store);
3155
3156static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003157sync_speed_show(mddev_t *mddev, char *page)
3158{
3159 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003160 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3161 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003162 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003163 db = resync - mddev->resync_mark_cnt;
3164 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003165}
3166
NeilBrown80ca3a42006-07-10 04:44:18 -07003167static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003168
3169static ssize_t
3170sync_completed_show(mddev_t *mddev, char *page)
3171{
3172 unsigned long max_blocks, resync;
3173
3174 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3175 max_blocks = mddev->resync_max_sectors;
3176 else
3177 max_blocks = mddev->size << 1;
3178
3179 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3180 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3181}
3182
NeilBrown80ca3a42006-07-10 04:44:18 -07003183static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003184
NeilBrowne464eaf2006-03-27 01:18:14 -08003185static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003186min_sync_show(mddev_t *mddev, char *page)
3187{
3188 return sprintf(page, "%llu\n",
3189 (unsigned long long)mddev->resync_min);
3190}
3191static ssize_t
3192min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3193{
3194 unsigned long long min;
3195 if (strict_strtoull(buf, 10, &min))
3196 return -EINVAL;
3197 if (min > mddev->resync_max)
3198 return -EINVAL;
3199 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3200 return -EBUSY;
3201
3202 /* Must be a multiple of chunk_size */
3203 if (mddev->chunk_size) {
3204 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3205 return -EINVAL;
3206 }
3207 mddev->resync_min = min;
3208
3209 return len;
3210}
3211
3212static struct md_sysfs_entry md_min_sync =
3213__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3214
3215static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003216max_sync_show(mddev_t *mddev, char *page)
3217{
3218 if (mddev->resync_max == MaxSector)
3219 return sprintf(page, "max\n");
3220 else
3221 return sprintf(page, "%llu\n",
3222 (unsigned long long)mddev->resync_max);
3223}
3224static ssize_t
3225max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3226{
3227 if (strncmp(buf, "max", 3) == 0)
3228 mddev->resync_max = MaxSector;
3229 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003230 unsigned long long max;
3231 if (strict_strtoull(buf, 10, &max))
3232 return -EINVAL;
3233 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003234 return -EINVAL;
3235 if (max < mddev->resync_max &&
3236 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3237 return -EBUSY;
3238
3239 /* Must be a multiple of chunk_size */
3240 if (mddev->chunk_size) {
3241 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3242 return -EINVAL;
3243 }
3244 mddev->resync_max = max;
3245 }
3246 wake_up(&mddev->recovery_wait);
3247 return len;
3248}
3249
3250static struct md_sysfs_entry md_max_sync =
3251__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3252
3253static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003254suspend_lo_show(mddev_t *mddev, char *page)
3255{
3256 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3257}
3258
3259static ssize_t
3260suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3261{
3262 char *e;
3263 unsigned long long new = simple_strtoull(buf, &e, 10);
3264
3265 if (mddev->pers->quiesce == NULL)
3266 return -EINVAL;
3267 if (buf == e || (*e && *e != '\n'))
3268 return -EINVAL;
3269 if (new >= mddev->suspend_hi ||
3270 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3271 mddev->suspend_lo = new;
3272 mddev->pers->quiesce(mddev, 2);
3273 return len;
3274 } else
3275 return -EINVAL;
3276}
3277static struct md_sysfs_entry md_suspend_lo =
3278__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3279
3280
3281static ssize_t
3282suspend_hi_show(mddev_t *mddev, char *page)
3283{
3284 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3285}
3286
3287static ssize_t
3288suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3289{
3290 char *e;
3291 unsigned long long new = simple_strtoull(buf, &e, 10);
3292
3293 if (mddev->pers->quiesce == NULL)
3294 return -EINVAL;
3295 if (buf == e || (*e && *e != '\n'))
3296 return -EINVAL;
3297 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3298 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3299 mddev->suspend_hi = new;
3300 mddev->pers->quiesce(mddev, 1);
3301 mddev->pers->quiesce(mddev, 0);
3302 return len;
3303 } else
3304 return -EINVAL;
3305}
3306static struct md_sysfs_entry md_suspend_hi =
3307__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3308
NeilBrown08a02ec2007-05-09 02:35:38 -07003309static ssize_t
3310reshape_position_show(mddev_t *mddev, char *page)
3311{
3312 if (mddev->reshape_position != MaxSector)
3313 return sprintf(page, "%llu\n",
3314 (unsigned long long)mddev->reshape_position);
3315 strcpy(page, "none\n");
3316 return 5;
3317}
3318
3319static ssize_t
3320reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3321{
3322 char *e;
3323 unsigned long long new = simple_strtoull(buf, &e, 10);
3324 if (mddev->pers)
3325 return -EBUSY;
3326 if (buf == e || (*e && *e != '\n'))
3327 return -EINVAL;
3328 mddev->reshape_position = new;
3329 mddev->delta_disks = 0;
3330 mddev->new_level = mddev->level;
3331 mddev->new_layout = mddev->layout;
3332 mddev->new_chunk = mddev->chunk_size;
3333 return len;
3334}
3335
3336static struct md_sysfs_entry md_reshape_position =
3337__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3338 reshape_position_store);
3339
NeilBrowne464eaf2006-03-27 01:18:14 -08003340
NeilBrowneae17012005-11-08 21:39:23 -08003341static struct attribute *md_default_attrs[] = {
3342 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003343 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003344 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003345 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003346 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003347 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003348 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003349 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003350 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003351 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003352 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003353 NULL,
3354};
3355
3356static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003357 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003358 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003359 &md_sync_min.attr,
3360 &md_sync_max.attr,
3361 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003362 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003363 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003364 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003365 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003366 &md_suspend_lo.attr,
3367 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003368 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003369 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003370 NULL,
3371};
NeilBrown411036f2005-11-08 21:39:40 -08003372static struct attribute_group md_redundancy_group = {
3373 .name = NULL,
3374 .attrs = md_redundancy_attrs,
3375};
3376
NeilBrowneae17012005-11-08 21:39:23 -08003377
3378static ssize_t
3379md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3380{
3381 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3382 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003383 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003384
3385 if (!entry->show)
3386 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003387 rv = mddev_lock(mddev);
3388 if (!rv) {
3389 rv = entry->show(mddev, page);
3390 mddev_unlock(mddev);
3391 }
NeilBrown96de1e62005-11-08 21:39:39 -08003392 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003393}
3394
3395static ssize_t
3396md_attr_store(struct kobject *kobj, struct attribute *attr,
3397 const char *page, size_t length)
3398{
3399 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3400 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003401 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003402
3403 if (!entry->store)
3404 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003405 if (!capable(CAP_SYS_ADMIN))
3406 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003407 rv = mddev_lock(mddev);
3408 if (!rv) {
3409 rv = entry->store(mddev, page, length);
3410 mddev_unlock(mddev);
3411 }
NeilBrown96de1e62005-11-08 21:39:39 -08003412 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003413}
3414
3415static void md_free(struct kobject *ko)
3416{
3417 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3418 kfree(mddev);
3419}
3420
3421static struct sysfs_ops md_sysfs_ops = {
3422 .show = md_attr_show,
3423 .store = md_attr_store,
3424};
3425static struct kobj_type md_ktype = {
3426 .release = md_free,
3427 .sysfs_ops = &md_sysfs_ops,
3428 .default_attrs = md_default_attrs,
3429};
3430
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431int mdp_major = 0;
3432
3433static struct kobject *md_probe(dev_t dev, int *part, void *data)
3434{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003435 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 mddev_t *mddev = mddev_find(dev);
3437 struct gendisk *disk;
3438 int partitioned = (MAJOR(dev) != MD_MAJOR);
3439 int shift = partitioned ? MdpMinorShift : 0;
3440 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003441 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
3443 if (!mddev)
3444 return NULL;
3445
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003446 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003448 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 mddev_put(mddev);
3450 return NULL;
3451 }
3452 disk = alloc_disk(1 << shift);
3453 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003454 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 mddev_put(mddev);
3456 return NULL;
3457 }
3458 disk->major = MAJOR(dev);
3459 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003460 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003462 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 disk->fops = &md_fops;
3465 disk->private_data = mddev;
3466 disk->queue = mddev->queue;
3467 add_disk(disk);
3468 mddev->gendisk = disk;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003469 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003470 "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003471 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003472 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003473 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3474 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003475 else
3476 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 return NULL;
3478}
3479
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480static void md_safemode_timeout(unsigned long data)
3481{
3482 mddev_t *mddev = (mddev_t *) data;
3483
Neil Brown0fd62b82008-06-28 08:31:36 +10003484 if (!atomic_read(&mddev->writes_pending)) {
3485 mddev->safemode = 1;
3486 if (mddev->external)
3487 sysfs_notify(&mddev->kobj, NULL, "array_state");
3488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 md_wakeup_thread(mddev->thread);
3490}
3491
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003492static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494static int do_md_run(mddev_t * mddev)
3495{
NeilBrown2604b702006-01-06 00:20:36 -08003496 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 int chunk_size;
3498 struct list_head *tmp;
3499 mdk_rdev_t *rdev;
3500 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003501 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 char b[BDEVNAME_SIZE];
3503
NeilBrowna757e642005-04-16 15:26:42 -07003504 if (list_empty(&mddev->disks))
3505 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
3508 if (mddev->pers)
3509 return -EBUSY;
3510
3511 /*
3512 * Analyze all RAID superblock(s)
3513 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003514 if (!mddev->raid_disks) {
3515 if (!mddev->persistent)
3516 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003517 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
3520 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521
NeilBrown2604b702006-01-06 00:20:36 -08003522 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 if (chunk_size > MAX_CHUNK_SIZE) {
3524 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3525 chunk_size, MAX_CHUNK_SIZE);
3526 return -EINVAL;
3527 }
3528 /*
3529 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3530 */
3531 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003532 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 return -EINVAL;
3534 }
3535 if (chunk_size < PAGE_SIZE) {
3536 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3537 chunk_size, PAGE_SIZE);
3538 return -EINVAL;
3539 }
3540
3541 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003542 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003543 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 continue;
3545 if (rdev->size < chunk_size / 1024) {
3546 printk(KERN_WARNING
3547 "md: Dev %s smaller than chunk_size:"
3548 " %lluk < %dk\n",
3549 bdevname(rdev->bdev,b),
3550 (unsigned long long)rdev->size,
3551 chunk_size / 1024);
3552 return -EINVAL;
3553 }
3554 }
3555 }
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003558 if (mddev->level != LEVEL_NONE)
3559 request_module("md-level-%d", mddev->level);
3560 else if (mddev->clevel[0])
3561 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562#endif
3563
3564 /*
3565 * Drop all container device buffers, from now on
3566 * the only valid external interface is through the md
3567 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003569 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003570 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 continue;
3572 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003573 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003574
3575 /* perform some consistency tests on the device.
3576 * We don't want the data to overlap the metadata,
3577 * Internal Bitmap issues has handled elsewhere.
3578 */
3579 if (rdev->data_offset < rdev->sb_offset) {
3580 if (mddev->size &&
3581 rdev->data_offset + mddev->size*2
3582 > rdev->sb_offset*2) {
3583 printk("md: %s: data overlaps metadata\n",
3584 mdname(mddev));
3585 return -EINVAL;
3586 }
3587 } else {
3588 if (rdev->sb_offset*2 + rdev->sb_size/512
3589 > rdev->data_offset) {
3590 printk("md: %s: metadata overlaps data\n",
3591 mdname(mddev));
3592 return -EINVAL;
3593 }
3594 }
Neil Brown52664732008-06-28 08:31:44 +10003595 sysfs_notify(&rdev->kobj, NULL, "state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 }
3597
3598 md_probe(mddev->unit, NULL, NULL);
3599 disk = mddev->gendisk;
3600 if (!disk)
3601 return -ENOMEM;
3602
3603 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003604 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003605 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003607 if (mddev->level != LEVEL_NONE)
3608 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3609 mddev->level);
3610 else
3611 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3612 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return -EINVAL;
3614 }
NeilBrown2604b702006-01-06 00:20:36 -08003615 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003617 mddev->level = pers->level;
3618 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
NeilBrownf6705572006-03-27 01:18:11 -08003620 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003621 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003622 /* This personality cannot handle reshaping... */
3623 mddev->pers = NULL;
3624 module_put(pers->owner);
3625 return -EINVAL;
3626 }
3627
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003628 if (pers->sync_request) {
3629 /* Warn if this is a potentially silly
3630 * configuration.
3631 */
3632 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3633 mdk_rdev_t *rdev2;
3634 struct list_head *tmp2;
3635 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003636 rdev_for_each(rdev, tmp, mddev) {
3637 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003638 if (rdev < rdev2 &&
3639 rdev->bdev->bd_contains ==
3640 rdev2->bdev->bd_contains) {
3641 printk(KERN_WARNING
3642 "%s: WARNING: %s appears to be"
3643 " on the same physical disk as"
3644 " %s.\n",
3645 mdname(mddev),
3646 bdevname(rdev->bdev,b),
3647 bdevname(rdev2->bdev,b2));
3648 warned = 1;
3649 }
3650 }
3651 }
3652 if (warned)
3653 printk(KERN_WARNING
3654 "True protection against single-disk"
3655 " failure might be compromised.\n");
3656 }
3657
NeilBrown657390d2005-08-26 18:34:16 -07003658 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003660 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003661 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
NeilBrownf91de922005-11-08 21:39:36 -08003663 if (start_readonly)
3664 mddev->ro = 2; /* read-only, but switch on first write */
3665
NeilBrownb15c2e52006-01-06 00:20:16 -08003666 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01003667 if (err)
3668 printk(KERN_ERR "md: pers->run() failed ...\n");
3669 else if (mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08003670 err = bitmap_create(mddev);
3671 if (err) {
3672 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3673 mdname(mddev), err);
3674 mddev->pers->stop(mddev);
3675 }
3676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 module_put(mddev->pers->owner);
3679 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003680 bitmap_destroy(mddev);
3681 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003683 if (mddev->pers->sync_request) {
3684 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3685 printk(KERN_WARNING
3686 "md: cannot register extra attributes for %s\n",
3687 mdname(mddev));
3688 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003689 mddev->ro = 0;
3690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 atomic_set(&mddev->writes_pending,0);
3692 mddev->safemode = 0;
3693 mddev->safemode_timer.function = md_safemode_timeout;
3694 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003695 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003697
NeilBrownd089c6a2008-02-06 01:39:59 -08003698 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003699 if (rdev->raid_disk >= 0) {
3700 char nm[20];
3701 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003702 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3703 printk("md: cannot register %s for %s\n",
3704 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706
3707 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3708
NeilBrown850b2b42006-10-03 01:15:46 -07003709 if (mddev->flags)
3710 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712 set_capacity(disk, mddev->array_size<<1);
3713
3714 /* If we call blk_queue_make_request here, it will
3715 * re-initialise max_sectors etc which may have been
3716 * refined inside -> run. So just set the bits we need to set.
3717 * Most initialisation happended when we called
3718 * blk_queue_make_request(..., md_fail_request)
3719 * earlier.
3720 */
3721 mddev->queue->queuedata = mddev;
3722 mddev->queue->make_request_fn = mddev->pers->make_request;
3723
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003724 /* If there is a partially-recovered drive we need to
3725 * start recovery here. If we leave it to md_check_recovery,
3726 * it will remove the drives and not do the right thing
3727 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003728 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003729 struct list_head *rtmp;
3730 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003731 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003732 if (rdev->raid_disk >= 0 &&
3733 !test_bit(In_sync, &rdev->flags) &&
3734 !test_bit(Faulty, &rdev->flags))
3735 /* complete an interrupted recovery */
3736 spares++;
3737 if (spares && mddev->pers->sync_request) {
3738 mddev->recovery = 0;
3739 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3740 mddev->sync_thread = md_register_thread(md_do_sync,
3741 mddev,
3742 "%s_resync");
3743 if (!mddev->sync_thread) {
3744 printk(KERN_ERR "%s: could not start resync"
3745 " thread...\n",
3746 mdname(mddev));
3747 /* leave the spares where they are, it shouldn't hurt */
3748 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003749 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003750 }
3751 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003752 md_wakeup_thread(mddev->thread);
3753 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003754
Linus Torvalds44ce62942007-05-09 18:51:36 -07003755 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003756 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003757 sysfs_notify(&mddev->kobj, NULL, "array_state");
Neil Brown72a23c22008-06-28 08:31:41 +10003758 sysfs_notify(&mddev->kobj, NULL, "sync_action");
Neil Browna99ac972008-06-28 08:31:43 +10003759 sysfs_notify(&mddev->kobj, NULL, "degraded");
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003760 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return 0;
3762}
3763
3764static int restart_array(mddev_t *mddev)
3765{
3766 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Andre Noll80fab1d2008-07-11 22:02:21 +10003768 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10003770 return -ENXIO;
3771 if (!mddev->pers)
3772 return -EINVAL;
3773 if (!mddev->ro)
3774 return -EBUSY;
3775 mddev->safemode = 0;
3776 mddev->ro = 0;
3777 set_disk_ro(disk, 0);
3778 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3779 mdname(mddev));
3780 /* Kick recovery or resync if necessary */
3781 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3782 md_wakeup_thread(mddev->thread);
3783 md_wakeup_thread(mddev->sync_thread);
3784 sysfs_notify(&mddev->kobj, NULL, "array_state");
3785 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786}
3787
NeilBrownacc55e22006-06-26 00:27:47 -07003788/* similar to deny_write_access, but accounts for our holding a reference
3789 * to the file ourselves */
3790static int deny_bitmap_write_access(struct file * file)
3791{
3792 struct inode *inode = file->f_mapping->host;
3793
3794 spin_lock(&inode->i_lock);
3795 if (atomic_read(&inode->i_writecount) > 1) {
3796 spin_unlock(&inode->i_lock);
3797 return -ETXTBSY;
3798 }
3799 atomic_set(&inode->i_writecount, -1);
3800 spin_unlock(&inode->i_lock);
3801
3802 return 0;
3803}
3804
3805static void restore_bitmap_write_access(struct file *file)
3806{
3807 struct inode *inode = file->f_mapping->host;
3808
3809 spin_lock(&inode->i_lock);
3810 atomic_set(&inode->i_writecount, 1);
3811 spin_unlock(&inode->i_lock);
3812}
3813
NeilBrown9e653b62006-06-26 00:27:58 -07003814/* mode:
3815 * 0 - completely stop and dis-assemble array
3816 * 1 - switch to readonly
3817 * 2 - stop but do not disassemble array
3818 */
Neil Browndf5b20c2008-07-11 22:02:22 +10003819static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
3821 int err = 0;
3822 struct gendisk *disk = mddev->gendisk;
3823
Neil Browndf5b20c2008-07-11 22:02:22 +10003824 if (atomic_read(&mddev->active) > 1 + is_open) {
3825 printk("md: %s still in use.\n",mdname(mddev));
3826 return -EBUSY;
3827 }
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
3831 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003832 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3834 md_unregister_thread(mddev->sync_thread);
3835 mddev->sync_thread = NULL;
3836 }
3837
3838 del_timer_sync(&mddev->safemode_timer);
3839
3840 invalidate_partition(disk, 0);
3841
NeilBrown9e653b62006-06-26 00:27:58 -07003842 switch(mode) {
3843 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003845 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 goto out;
3847 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003848 break;
3849 case 0: /* disassemble */
3850 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003851 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003852 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 if (mddev->ro)
3854 set_disk_ro(disk, 0);
3855 blk_queue_make_request(mddev->queue, md_fail_request);
3856 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003857 mddev->queue->merge_bvec_fn = NULL;
3858 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003859 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003860 if (mddev->pers->sync_request)
3861 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 module_put(mddev->pers->owner);
3864 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003865 /* tell userspace to handle 'inactive' */
3866 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003867
3868 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07003869 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 if (mddev->ro)
3872 mddev->ro = 0;
3873 }
NeilBrown850b2b42006-10-03 01:15:46 -07003874 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 /* mark array as shutdown cleanly */
3876 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003877 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 }
NeilBrown9e653b62006-06-26 00:27:58 -07003879 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003881 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 }
NeilBrown32a76272005-06-21 17:17:14 -07003883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 /*
3885 * Free resources if final stop
3886 */
NeilBrown9e653b62006-06-26 00:27:58 -07003887 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003888 mdk_rdev_t *rdev;
3889 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3892
NeilBrown978f9462006-02-02 14:28:05 -08003893 bitmap_destroy(mddev);
3894 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003895 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003896 fput(mddev->bitmap_file);
3897 mddev->bitmap_file = NULL;
3898 }
3899 mddev->bitmap_offset = 0;
3900
NeilBrownd089c6a2008-02-06 01:39:59 -08003901 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003902 if (rdev->raid_disk >= 0) {
3903 char nm[20];
3904 sprintf(nm, "rd%d", rdev->raid_disk);
3905 sysfs_remove_link(&mddev->kobj, nm);
3906 }
3907
NeilBrown177a99b2008-02-06 01:39:56 -08003908 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003909 flush_scheduled_work();
3910
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 export_array(mddev);
3912
3913 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003914 mddev->size = 0;
3915 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003916 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003917 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003918 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003919 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003920 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003921 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003922 mddev->level = LEVEL_NONE;
3923 mddev->clevel[0] = 0;
3924 mddev->flags = 0;
3925 mddev->ro = 0;
3926 mddev->metadata_type[0] = 0;
3927 mddev->chunk_size = 0;
3928 mddev->ctime = mddev->utime = 0;
3929 mddev->layout = 0;
3930 mddev->max_disks = 0;
3931 mddev->events = 0;
3932 mddev->delta_disks = 0;
3933 mddev->new_level = LEVEL_NONE;
3934 mddev->new_layout = 0;
3935 mddev->new_chunk = 0;
3936 mddev->curr_resync = 0;
3937 mddev->resync_mismatches = 0;
3938 mddev->suspend_lo = mddev->suspend_hi = 0;
3939 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3940 mddev->recovery = 0;
3941 mddev->in_sync = 0;
3942 mddev->changed = 0;
3943 mddev->degraded = 0;
3944 mddev->barriers_work = 0;
3945 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003946
NeilBrowna8a55c32006-06-26 00:27:39 -07003947 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3949 mdname(mddev));
3950 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003951 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003952 sysfs_notify(&mddev->kobj, NULL, "array_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953out:
3954 return err;
3955}
3956
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003957#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958static void autorun_array(mddev_t *mddev)
3959{
3960 mdk_rdev_t *rdev;
3961 struct list_head *tmp;
3962 int err;
3963
NeilBrowna757e642005-04-16 15:26:42 -07003964 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
3967 printk(KERN_INFO "md: running: ");
3968
NeilBrownd089c6a2008-02-06 01:39:59 -08003969 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 char b[BDEVNAME_SIZE];
3971 printk("<%s>", bdevname(rdev->bdev,b));
3972 }
3973 printk("\n");
3974
3975 err = do_md_run (mddev);
3976 if (err) {
3977 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
Neil Browndf5b20c2008-07-11 22:02:22 +10003978 do_md_stop (mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 }
3980}
3981
3982/*
3983 * lets try to run arrays based on all disks that have arrived
3984 * until now. (those are in pending_raid_disks)
3985 *
3986 * the method: pick the first pending disk, collect all disks with
3987 * the same UUID, remove all from the pending list and put them into
3988 * the 'same_array' list. Then order this list based on superblock
3989 * update time (freshest comes first), kick out 'old' disks and
3990 * compare superblocks. If everything's fine then run it.
3991 *
3992 * If "unit" is allocated, then bump its reference count
3993 */
3994static void autorun_devices(int part)
3995{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 struct list_head *tmp;
3997 mdk_rdev_t *rdev0, *rdev;
3998 mddev_t *mddev;
3999 char b[BDEVNAME_SIZE];
4000
4001 printk(KERN_INFO "md: autorun ...\n");
4002 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004003 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004005 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 rdev0 = list_entry(pending_raid_disks.next,
4007 mdk_rdev_t, same_set);
4008
4009 printk(KERN_INFO "md: considering %s ...\n",
4010 bdevname(rdev0->bdev,b));
4011 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08004012 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 if (super_90_load(rdev, rdev0, 0) >= 0) {
4014 printk(KERN_INFO "md: adding %s ...\n",
4015 bdevname(rdev->bdev,b));
4016 list_move(&rdev->same_set, &candidates);
4017 }
4018 /*
4019 * now we have a set of devices, with all of them having
4020 * mostly sane superblocks. It's time to allocate the
4021 * mddev.
4022 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004023 if (part) {
4024 dev = MKDEV(mdp_major,
4025 rdev0->preferred_minor << MdpMinorShift);
4026 unit = MINOR(dev) >> MdpMinorShift;
4027 } else {
4028 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4029 unit = MINOR(dev);
4030 }
4031 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4033 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4034 break;
4035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
4037 md_probe(dev, NULL, NULL);
4038 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004039 if (!mddev || !mddev->gendisk) {
4040 if (mddev)
4041 mddev_put(mddev);
4042 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 "md: cannot allocate memory for md drive.\n");
4044 break;
4045 }
4046 if (mddev_lock(mddev))
4047 printk(KERN_WARNING "md: %s locked, cannot run\n",
4048 mdname(mddev));
4049 else if (mddev->raid_disks || mddev->major_version
4050 || !list_empty(&mddev->disks)) {
4051 printk(KERN_WARNING
4052 "md: %s already running, cannot run %s\n",
4053 mdname(mddev), bdevname(rdev0->bdev,b));
4054 mddev_unlock(mddev);
4055 } else {
4056 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004057 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08004058 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 list_del_init(&rdev->same_set);
4060 if (bind_rdev_to_array(rdev, mddev))
4061 export_rdev(rdev);
4062 }
4063 autorun_array(mddev);
4064 mddev_unlock(mddev);
4065 }
4066 /* on success, candidates will be empty, on error
4067 * it won't...
4068 */
NeilBrown73c34432008-02-06 01:39:59 -08004069 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 export_rdev(rdev);
4071 mddev_put(mddev);
4072 }
4073 printk(KERN_INFO "md: ... autorun DONE.\n");
4074}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004075#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077static int get_version(void __user * arg)
4078{
4079 mdu_version_t ver;
4080
4081 ver.major = MD_MAJOR_VERSION;
4082 ver.minor = MD_MINOR_VERSION;
4083 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4084
4085 if (copy_to_user(arg, &ver, sizeof(ver)))
4086 return -EFAULT;
4087
4088 return 0;
4089}
4090
4091static int get_array_info(mddev_t * mddev, void __user * arg)
4092{
4093 mdu_array_info_t info;
4094 int nr,working,active,failed,spare;
4095 mdk_rdev_t *rdev;
4096 struct list_head *tmp;
4097
4098 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004099 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004101 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 failed++;
4103 else {
4104 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004105 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 active++;
4107 else
4108 spare++;
4109 }
4110 }
4111
4112 info.major_version = mddev->major_version;
4113 info.minor_version = mddev->minor_version;
4114 info.patch_version = MD_PATCHLEVEL_VERSION;
4115 info.ctime = mddev->ctime;
4116 info.level = mddev->level;
4117 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004118 if (info.size != mddev->size) /* overflow */
4119 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 info.nr_disks = nr;
4121 info.raid_disks = mddev->raid_disks;
4122 info.md_minor = mddev->md_minor;
4123 info.not_persistent= !mddev->persistent;
4124
4125 info.utime = mddev->utime;
4126 info.state = 0;
4127 if (mddev->in_sync)
4128 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004129 if (mddev->bitmap && mddev->bitmap_offset)
4130 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 info.active_disks = active;
4132 info.working_disks = working;
4133 info.failed_disks = failed;
4134 info.spare_disks = spare;
4135
4136 info.layout = mddev->layout;
4137 info.chunk_size = mddev->chunk_size;
4138
4139 if (copy_to_user(arg, &info, sizeof(info)))
4140 return -EFAULT;
4141
4142 return 0;
4143}
4144
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004145static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004146{
4147 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4148 char *ptr, *buf = NULL;
4149 int err = -ENOMEM;
4150
Dan Williamsb5470dc2008-06-27 21:44:04 -07004151 if (md_allow_write(mddev))
4152 file = kmalloc(sizeof(*file), GFP_NOIO);
4153 else
4154 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004155
NeilBrown32a76272005-06-21 17:17:14 -07004156 if (!file)
4157 goto out;
4158
4159 /* bitmap disabled, zero the first byte and copy out */
4160 if (!mddev->bitmap || !mddev->bitmap->file) {
4161 file->pathname[0] = '\0';
4162 goto copy_out;
4163 }
4164
4165 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4166 if (!buf)
4167 goto out;
4168
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004169 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4170 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004171 goto out;
4172
4173 strcpy(file->pathname, ptr);
4174
4175copy_out:
4176 err = 0;
4177 if (copy_to_user(arg, file, sizeof(*file)))
4178 err = -EFAULT;
4179out:
4180 kfree(buf);
4181 kfree(file);
4182 return err;
4183}
4184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185static int get_disk_info(mddev_t * mddev, void __user * arg)
4186{
4187 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 mdk_rdev_t *rdev;
4189
4190 if (copy_from_user(&info, arg, sizeof(info)))
4191 return -EFAULT;
4192
Andre Noll26ef3792008-07-11 22:02:21 +10004193 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 if (rdev) {
4195 info.major = MAJOR(rdev->bdev->bd_dev);
4196 info.minor = MINOR(rdev->bdev->bd_dev);
4197 info.raid_disk = rdev->raid_disk;
4198 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004199 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004201 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 info.state |= (1<<MD_DISK_ACTIVE);
4203 info.state |= (1<<MD_DISK_SYNC);
4204 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004205 if (test_bit(WriteMostly, &rdev->flags))
4206 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 } else {
4208 info.major = info.minor = 0;
4209 info.raid_disk = -1;
4210 info.state = (1<<MD_DISK_REMOVED);
4211 }
4212
4213 if (copy_to_user(arg, &info, sizeof(info)))
4214 return -EFAULT;
4215
4216 return 0;
4217}
4218
4219static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4220{
4221 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4222 mdk_rdev_t *rdev;
4223 dev_t dev = MKDEV(info->major,info->minor);
4224
4225 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4226 return -EOVERFLOW;
4227
4228 if (!mddev->raid_disks) {
4229 int err;
4230 /* expecting a device which has a superblock */
4231 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4232 if (IS_ERR(rdev)) {
4233 printk(KERN_WARNING
4234 "md: md_import_device returned %ld\n",
4235 PTR_ERR(rdev));
4236 return PTR_ERR(rdev);
4237 }
4238 if (!list_empty(&mddev->disks)) {
4239 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4240 mdk_rdev_t, same_set);
4241 int err = super_types[mddev->major_version]
4242 .load_super(rdev, rdev0, mddev->minor_version);
4243 if (err < 0) {
4244 printk(KERN_WARNING
4245 "md: %s has different UUID to %s\n",
4246 bdevname(rdev->bdev,b),
4247 bdevname(rdev0->bdev,b2));
4248 export_rdev(rdev);
4249 return -EINVAL;
4250 }
4251 }
4252 err = bind_rdev_to_array(rdev, mddev);
4253 if (err)
4254 export_rdev(rdev);
4255 return err;
4256 }
4257
4258 /*
4259 * add_new_disk can be used once the array is assembled
4260 * to add "hot spares". They must already have a superblock
4261 * written
4262 */
4263 if (mddev->pers) {
4264 int err;
4265 if (!mddev->pers->hot_add_disk) {
4266 printk(KERN_WARNING
4267 "%s: personality does not support diskops!\n",
4268 mdname(mddev));
4269 return -EINVAL;
4270 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004271 if (mddev->persistent)
4272 rdev = md_import_device(dev, mddev->major_version,
4273 mddev->minor_version);
4274 else
4275 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 if (IS_ERR(rdev)) {
4277 printk(KERN_WARNING
4278 "md: md_import_device returned %ld\n",
4279 PTR_ERR(rdev));
4280 return PTR_ERR(rdev);
4281 }
NeilBrown41158c72005-06-21 17:17:25 -07004282 /* set save_raid_disk if appropriate */
4283 if (!mddev->persistent) {
4284 if (info->state & (1<<MD_DISK_SYNC) &&
4285 info->raid_disk < mddev->raid_disks)
4286 rdev->raid_disk = info->raid_disk;
4287 else
4288 rdev->raid_disk = -1;
4289 } else
4290 super_types[mddev->major_version].
4291 validate_super(mddev, rdev);
4292 rdev->saved_raid_disk = rdev->raid_disk;
4293
NeilBrownb2d444d2005-11-08 21:39:31 -08004294 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004295 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4296 set_bit(WriteMostly, &rdev->flags);
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 rdev->raid_disk = -1;
4299 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004300 if (!err && !mddev->pers->hot_remove_disk) {
4301 /* If there is hot_add_disk but no hot_remove_disk
4302 * then added disks for geometry changes,
4303 * and should be added immediately.
4304 */
4305 super_types[mddev->major_version].
4306 validate_super(mddev, rdev);
4307 err = mddev->pers->hot_add_disk(mddev, rdev);
4308 if (err)
4309 unbind_rdev_from_array(rdev);
4310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 if (err)
4312 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004313 else
4314 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc3617772005-06-21 17:17:10 -07004315
NeilBrown17571282006-12-10 02:20:52 -08004316 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004317 if (mddev->degraded)
4318 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004319 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004320 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 return err;
4322 }
4323
4324 /* otherwise, add_new_disk is only allowed
4325 * for major_version==0 superblocks
4326 */
4327 if (mddev->major_version != 0) {
4328 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4329 mdname(mddev));
4330 return -EINVAL;
4331 }
4332
4333 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4334 int err;
4335 rdev = md_import_device (dev, -1, 0);
4336 if (IS_ERR(rdev)) {
4337 printk(KERN_WARNING
4338 "md: error, md_import_device() returned %ld\n",
4339 PTR_ERR(rdev));
4340 return PTR_ERR(rdev);
4341 }
4342 rdev->desc_nr = info->number;
4343 if (info->raid_disk < mddev->raid_disks)
4344 rdev->raid_disk = info->raid_disk;
4345 else
4346 rdev->raid_disk = -1;
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004349 if (info->state & (1<<MD_DISK_SYNC))
4350 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004352 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4353 set_bit(WriteMostly, &rdev->flags);
4354
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 if (!mddev->persistent) {
4356 printk(KERN_INFO "md: nonpersistent superblock ...\n");
4357 rdev->sb_offset = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4358 } else
4359 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
Andre Nolle7debaa2008-07-11 22:02:23 +10004360 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
NeilBrown2bf071b2006-01-06 00:20:55 -08004362 err = bind_rdev_to_array(rdev, mddev);
4363 if (err) {
4364 export_rdev(rdev);
4365 return err;
4366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 }
4368
4369 return 0;
4370}
4371
4372static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4373{
4374 char b[BDEVNAME_SIZE];
4375 mdk_rdev_t *rdev;
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 rdev = find_rdev(mddev, dev);
4378 if (!rdev)
4379 return -ENXIO;
4380
4381 if (rdev->raid_disk >= 0)
4382 goto busy;
4383
4384 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004385 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004386 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
4388 return 0;
4389busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004390 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 bdevname(rdev->bdev,b), mdname(mddev));
4392 return -EBUSY;
4393}
4394
4395static int hot_add_disk(mddev_t * mddev, dev_t dev)
4396{
4397 char b[BDEVNAME_SIZE];
4398 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 mdk_rdev_t *rdev;
4400
4401 if (!mddev->pers)
4402 return -ENODEV;
4403
4404 if (mddev->major_version != 0) {
4405 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4406 " version-0 superblocks.\n",
4407 mdname(mddev));
4408 return -EINVAL;
4409 }
4410 if (!mddev->pers->hot_add_disk) {
4411 printk(KERN_WARNING
4412 "%s: personality does not support diskops!\n",
4413 mdname(mddev));
4414 return -EINVAL;
4415 }
4416
4417 rdev = md_import_device (dev, -1, 0);
4418 if (IS_ERR(rdev)) {
4419 printk(KERN_WARNING
4420 "md: error, md_import_device() returned %ld\n",
4421 PTR_ERR(rdev));
4422 return -EINVAL;
4423 }
4424
4425 if (mddev->persistent)
4426 rdev->sb_offset = calc_dev_sboffset(rdev->bdev);
4427 else
4428 rdev->sb_offset =
4429 rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
4430
Andre Nolle7debaa2008-07-11 22:02:23 +10004431 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
NeilBrownb2d444d2005-11-08 21:39:31 -08004433 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 printk(KERN_WARNING
4435 "md: can not hot-add faulty %s disk to %s!\n",
4436 bdevname(rdev->bdev,b), mdname(mddev));
4437 err = -EINVAL;
4438 goto abort_export;
4439 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004440 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004442 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004443 err = bind_rdev_to_array(rdev, mddev);
4444 if (err)
4445 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
4447 /*
4448 * The rest should better be atomic, we can have disk failures
4449 * noticed in interrupt contexts ...
4450 */
4451
4452 if (rdev->desc_nr == mddev->max_disks) {
4453 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4454 mdname(mddev));
4455 err = -EBUSY;
4456 goto abort_unbind_export;
4457 }
4458
4459 rdev->raid_disk = -1;
4460
NeilBrown850b2b42006-10-03 01:15:46 -07004461 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
4463 /*
4464 * Kick recovery, maybe this spare has to be added to the
4465 * array immediately.
4466 */
4467 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4468 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004469 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 return 0;
4471
4472abort_unbind_export:
4473 unbind_rdev_from_array(rdev);
4474
4475abort_export:
4476 export_rdev(rdev);
4477 return err;
4478}
4479
NeilBrown32a76272005-06-21 17:17:14 -07004480static int set_bitmap_file(mddev_t *mddev, int fd)
4481{
4482 int err;
4483
NeilBrown36fa3062005-09-09 16:23:45 -07004484 if (mddev->pers) {
4485 if (!mddev->pers->quiesce)
4486 return -EBUSY;
4487 if (mddev->recovery || mddev->sync_thread)
4488 return -EBUSY;
4489 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004490 }
4491
NeilBrown36fa3062005-09-09 16:23:45 -07004492
4493 if (fd >= 0) {
4494 if (mddev->bitmap)
4495 return -EEXIST; /* cannot add when bitmap is present */
4496 mddev->bitmap_file = fget(fd);
4497
4498 if (mddev->bitmap_file == NULL) {
4499 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4500 mdname(mddev));
4501 return -EBADF;
4502 }
4503
4504 err = deny_bitmap_write_access(mddev->bitmap_file);
4505 if (err) {
4506 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4507 mdname(mddev));
4508 fput(mddev->bitmap_file);
4509 mddev->bitmap_file = NULL;
4510 return err;
4511 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004512 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004513 } else if (mddev->bitmap == NULL)
4514 return -ENOENT; /* cannot remove what isn't there */
4515 err = 0;
4516 if (mddev->pers) {
4517 mddev->pers->quiesce(mddev, 1);
4518 if (fd >= 0)
4519 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004520 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004521 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004522 fd = -1; /* make sure to put the file */
4523 }
NeilBrown36fa3062005-09-09 16:23:45 -07004524 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004525 }
4526 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004527 if (mddev->bitmap_file) {
4528 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004529 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004530 }
NeilBrown36fa3062005-09-09 16:23:45 -07004531 mddev->bitmap_file = NULL;
4532 }
4533
NeilBrown32a76272005-06-21 17:17:14 -07004534 return err;
4535}
4536
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537/*
4538 * set_array_info is used two different ways
4539 * The original usage is when creating a new array.
4540 * In this usage, raid_disks is > 0 and it together with
4541 * level, size, not_persistent,layout,chunksize determine the
4542 * shape of the array.
4543 * This will always create an array with a type-0.90.0 superblock.
4544 * The newer usage is when assembling an array.
4545 * In this case raid_disks will be 0, and the major_version field is
4546 * use to determine which style super-blocks are to be found on the devices.
4547 * The minor and patch _version numbers are also kept incase the
4548 * super_block handler wishes to interpret them.
4549 */
4550static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4551{
4552
4553 if (info->raid_disks == 0) {
4554 /* just setting version number for superblock loading */
4555 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004556 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 super_types[info->major_version].name == NULL) {
4558 /* maybe try to auto-load a module? */
4559 printk(KERN_INFO
4560 "md: superblock version %d not known\n",
4561 info->major_version);
4562 return -EINVAL;
4563 }
4564 mddev->major_version = info->major_version;
4565 mddev->minor_version = info->minor_version;
4566 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004567 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 return 0;
4569 }
4570 mddev->major_version = MD_MAJOR_VERSION;
4571 mddev->minor_version = MD_MINOR_VERSION;
4572 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4573 mddev->ctime = get_seconds();
4574
4575 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004576 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 mddev->size = info->size;
4578 mddev->raid_disks = info->raid_disks;
4579 /* don't set md_minor, it is determined by which /dev/md* was
4580 * openned
4581 */
4582 if (info->state & (1<<MD_SB_CLEAN))
4583 mddev->recovery_cp = MaxSector;
4584 else
4585 mddev->recovery_cp = 0;
4586 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004587 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
4589 mddev->layout = info->layout;
4590 mddev->chunk_size = info->chunk_size;
4591
4592 mddev->max_disks = MD_SB_DISKS;
4593
NeilBrowne6910632008-02-06 01:39:51 -08004594 if (mddev->persistent)
4595 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004596 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
NeilBrownb2a27032005-11-28 13:44:12 -08004598 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4599 mddev->bitmap_offset = 0;
4600
NeilBrownf6705572006-03-27 01:18:11 -08004601 mddev->reshape_position = MaxSector;
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 /*
4604 * Generate a 128 bit UUID
4605 */
4606 get_random_bytes(mddev->uuid, 16);
4607
NeilBrownf6705572006-03-27 01:18:11 -08004608 mddev->new_level = mddev->level;
4609 mddev->new_chunk = mddev->chunk_size;
4610 mddev->new_layout = mddev->layout;
4611 mddev->delta_disks = 0;
4612
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 return 0;
4614}
4615
Andre Nolld71f9f82008-07-11 22:02:22 +10004616static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004617{
4618 mdk_rdev_t * rdev;
4619 int rv;
4620 struct list_head *tmp;
Andre Nolld71f9f82008-07-11 22:02:22 +10004621 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004622
4623 if (mddev->pers->resize == NULL)
4624 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10004625 /* The "num_sectors" is the number of sectors of each device that
4626 * is used. This can only make sense for arrays with redundancy.
4627 * linear and raid0 always use whatever space is available. We can only
4628 * consider changing this number if no resync or reconstruction is
4629 * happening, and if the new size is acceptable. It must fit before the
4630 * sb_offset or, if that is <data_offset, it must fit before the size
4631 * of each device. If num_sectors is zero, we find the largest size
4632 * that fits.
4633
NeilBrowna35b0d62006-01-06 00:20:49 -08004634 */
4635 if (mddev->sync_thread)
4636 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004637 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004638 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004639 avail = rdev->size * 2;
4640
Andre Nolld71f9f82008-07-11 22:02:22 +10004641 if (fit && (num_sectors == 0 || num_sectors > avail))
4642 num_sectors = avail;
4643 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004644 return -ENOSPC;
4645 }
Andre Nolld71f9f82008-07-11 22:02:22 +10004646 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004647 if (!rv) {
4648 struct block_device *bdev;
4649
4650 bdev = bdget_disk(mddev->gendisk, 0);
4651 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004652 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004653 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004654 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004655 bdput(bdev);
4656 }
4657 }
4658 return rv;
4659}
4660
NeilBrownda943b992006-01-06 00:20:54 -08004661static int update_raid_disks(mddev_t *mddev, int raid_disks)
4662{
4663 int rv;
4664 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004665 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004666 return -EINVAL;
4667 if (raid_disks <= 0 ||
4668 raid_disks >= mddev->max_disks)
4669 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004670 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004671 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004672 mddev->delta_disks = raid_disks - mddev->raid_disks;
4673
4674 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004675 return rv;
4676}
4677
4678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679/*
4680 * update_array_info is used to change the configuration of an
4681 * on-line array.
4682 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4683 * fields in the info are checked against the array.
4684 * Any differences that cannot be handled will cause an error.
4685 * Normally, only one change can be managed at a time.
4686 */
4687static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4688{
4689 int rv = 0;
4690 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004691 int state = 0;
4692
4693 /* calculate expected state,ignoring low bits */
4694 if (mddev->bitmap && mddev->bitmap_offset)
4695 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
4697 if (mddev->major_version != info->major_version ||
4698 mddev->minor_version != info->minor_version ||
4699/* mddev->patch_version != info->patch_version || */
4700 mddev->ctime != info->ctime ||
4701 mddev->level != info->level ||
4702/* mddev->layout != info->layout || */
4703 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004704 mddev->chunk_size != info->chunk_size ||
4705 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4706 ((state^info->state) & 0xfffffe00)
4707 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 return -EINVAL;
4709 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004710 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 if (mddev->raid_disks != info->raid_disks) cnt++;
4712 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004713 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 if (cnt == 0) return 0;
4715 if (cnt > 1) return -EINVAL;
4716
4717 if (mddev->layout != info->layout) {
4718 /* Change layout
4719 * we don't need to do anything at the md level, the
4720 * personality will take care of it all.
4721 */
4722 if (mddev->pers->reconfig == NULL)
4723 return -EINVAL;
4724 else
4725 return mddev->pers->reconfig(mddev, info->layout, -1);
4726 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004727 if (info->size >= 0 && mddev->size != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10004728 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729
NeilBrownda943b992006-01-06 00:20:54 -08004730 if (mddev->raid_disks != info->raid_disks)
4731 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732
NeilBrown36fa3062005-09-09 16:23:45 -07004733 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4734 if (mddev->pers->quiesce == NULL)
4735 return -EINVAL;
4736 if (mddev->recovery || mddev->sync_thread)
4737 return -EBUSY;
4738 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4739 /* add the bitmap */
4740 if (mddev->bitmap)
4741 return -EEXIST;
4742 if (mddev->default_bitmap_offset == 0)
4743 return -EINVAL;
4744 mddev->bitmap_offset = mddev->default_bitmap_offset;
4745 mddev->pers->quiesce(mddev, 1);
4746 rv = bitmap_create(mddev);
4747 if (rv)
4748 bitmap_destroy(mddev);
4749 mddev->pers->quiesce(mddev, 0);
4750 } else {
4751 /* remove the bitmap */
4752 if (!mddev->bitmap)
4753 return -ENOENT;
4754 if (mddev->bitmap->file)
4755 return -EINVAL;
4756 mddev->pers->quiesce(mddev, 1);
4757 bitmap_destroy(mddev);
4758 mddev->pers->quiesce(mddev, 0);
4759 mddev->bitmap_offset = 0;
4760 }
4761 }
NeilBrown850b2b42006-10-03 01:15:46 -07004762 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 return rv;
4764}
4765
4766static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4767{
4768 mdk_rdev_t *rdev;
4769
4770 if (mddev->pers == NULL)
4771 return -ENODEV;
4772
4773 rdev = find_rdev(mddev, dev);
4774 if (!rdev)
4775 return -ENODEV;
4776
4777 md_error(mddev, rdev);
4778 return 0;
4779}
4780
Andre Noll2f9618c2008-04-25 18:57:58 +02004781/*
4782 * We have a problem here : there is no easy way to give a CHS
4783 * virtual geometry. We currently pretend that we have a 2 heads
4784 * 4 sectors (with a BIG number of cylinders...). This drives
4785 * dosfs just mad... ;-)
4786 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004787static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4788{
4789 mddev_t *mddev = bdev->bd_disk->private_data;
4790
4791 geo->heads = 2;
4792 geo->sectors = 4;
4793 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4794 return 0;
4795}
4796
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797static int md_ioctl(struct inode *inode, struct file *file,
4798 unsigned int cmd, unsigned long arg)
4799{
4800 int err = 0;
4801 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 mddev_t *mddev = NULL;
4803
4804 if (!capable(CAP_SYS_ADMIN))
4805 return -EACCES;
4806
4807 /*
4808 * Commands dealing with the RAID driver but not any
4809 * particular array:
4810 */
4811 switch (cmd)
4812 {
4813 case RAID_VERSION:
4814 err = get_version(argp);
4815 goto done;
4816
4817 case PRINT_RAID_DEBUG:
4818 err = 0;
4819 md_print_devices();
4820 goto done;
4821
4822#ifndef MODULE
4823 case RAID_AUTORUN:
4824 err = 0;
4825 autostart_arrays(arg);
4826 goto done;
4827#endif
4828 default:;
4829 }
4830
4831 /*
4832 * Commands creating/starting a new array:
4833 */
4834
4835 mddev = inode->i_bdev->bd_disk->private_data;
4836
4837 if (!mddev) {
4838 BUG();
4839 goto abort;
4840 }
4841
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 err = mddev_lock(mddev);
4843 if (err) {
4844 printk(KERN_INFO
4845 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4846 err, cmd);
4847 goto abort;
4848 }
4849
4850 switch (cmd)
4851 {
4852 case SET_ARRAY_INFO:
4853 {
4854 mdu_array_info_t info;
4855 if (!arg)
4856 memset(&info, 0, sizeof(info));
4857 else if (copy_from_user(&info, argp, sizeof(info))) {
4858 err = -EFAULT;
4859 goto abort_unlock;
4860 }
4861 if (mddev->pers) {
4862 err = update_array_info(mddev, &info);
4863 if (err) {
4864 printk(KERN_WARNING "md: couldn't update"
4865 " array info. %d\n", err);
4866 goto abort_unlock;
4867 }
4868 goto done_unlock;
4869 }
4870 if (!list_empty(&mddev->disks)) {
4871 printk(KERN_WARNING
4872 "md: array %s already has disks!\n",
4873 mdname(mddev));
4874 err = -EBUSY;
4875 goto abort_unlock;
4876 }
4877 if (mddev->raid_disks) {
4878 printk(KERN_WARNING
4879 "md: array %s already initialised!\n",
4880 mdname(mddev));
4881 err = -EBUSY;
4882 goto abort_unlock;
4883 }
4884 err = set_array_info(mddev, &info);
4885 if (err) {
4886 printk(KERN_WARNING "md: couldn't set"
4887 " array info. %d\n", err);
4888 goto abort_unlock;
4889 }
4890 }
4891 goto done_unlock;
4892
4893 default:;
4894 }
4895
4896 /*
4897 * Commands querying/configuring an existing array:
4898 */
NeilBrown32a76272005-06-21 17:17:14 -07004899 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004900 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004901 if ((!mddev->raid_disks && !mddev->external)
4902 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4903 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4904 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905 err = -ENODEV;
4906 goto abort_unlock;
4907 }
4908
4909 /*
4910 * Commands even a read-only array can execute:
4911 */
4912 switch (cmd)
4913 {
4914 case GET_ARRAY_INFO:
4915 err = get_array_info(mddev, argp);
4916 goto done_unlock;
4917
NeilBrown32a76272005-06-21 17:17:14 -07004918 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004919 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004920 goto done_unlock;
4921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 case GET_DISK_INFO:
4923 err = get_disk_info(mddev, argp);
4924 goto done_unlock;
4925
4926 case RESTART_ARRAY_RW:
4927 err = restart_array(mddev);
4928 goto done_unlock;
4929
4930 case STOP_ARRAY:
Neil Browndf5b20c2008-07-11 22:02:22 +10004931 err = do_md_stop (mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 goto done_unlock;
4933
4934 case STOP_ARRAY_RO:
Neil Browndf5b20c2008-07-11 22:02:22 +10004935 err = do_md_stop (mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 goto done_unlock;
4937
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 }
4939
4940 /*
4941 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004942 * superblock, so we do not allow them on read-only arrays.
4943 * However non-MD ioctls (e.g. get-size) will still come through
4944 * here and hit the 'default' below, so only disallow
4945 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 */
Andre Nollbb57fc62008-04-25 19:06:35 +02004947 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004948 if (mddev->ro == 2) {
4949 mddev->ro = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10004950 sysfs_notify(&mddev->kobj, NULL, "array_state");
4951 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4952 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08004953 } else {
4954 err = -EROFS;
4955 goto abort_unlock;
4956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 }
4958
4959 switch (cmd)
4960 {
4961 case ADD_NEW_DISK:
4962 {
4963 mdu_disk_info_t info;
4964 if (copy_from_user(&info, argp, sizeof(info)))
4965 err = -EFAULT;
4966 else
4967 err = add_new_disk(mddev, &info);
4968 goto done_unlock;
4969 }
4970
4971 case HOT_REMOVE_DISK:
4972 err = hot_remove_disk(mddev, new_decode_dev(arg));
4973 goto done_unlock;
4974
4975 case HOT_ADD_DISK:
4976 err = hot_add_disk(mddev, new_decode_dev(arg));
4977 goto done_unlock;
4978
4979 case SET_DISK_FAULTY:
4980 err = set_disk_faulty(mddev, new_decode_dev(arg));
4981 goto done_unlock;
4982
4983 case RUN_ARRAY:
4984 err = do_md_run (mddev);
4985 goto done_unlock;
4986
NeilBrown32a76272005-06-21 17:17:14 -07004987 case SET_BITMAP_FILE:
4988 err = set_bitmap_file(mddev, (int)arg);
4989 goto done_unlock;
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 err = -EINVAL;
4993 goto abort_unlock;
4994 }
4995
4996done_unlock:
4997abort_unlock:
4998 mddev_unlock(mddev);
4999
5000 return err;
5001done:
5002 if (err)
5003 MD_BUG();
5004abort:
5005 return err;
5006}
5007
5008static int md_open(struct inode *inode, struct file *file)
5009{
5010 /*
5011 * Succeed if we can lock the mddev, which confirms that
5012 * it isn't being stopped right now.
5013 */
5014 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5015 int err;
5016
NeilBrownd63a5a72006-12-08 02:36:17 -08005017 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 goto out;
5019
5020 err = 0;
5021 mddev_get(mddev);
5022 mddev_unlock(mddev);
5023
5024 check_disk_change(inode->i_bdev);
5025 out:
5026 return err;
5027}
5028
5029static int md_release(struct inode *inode, struct file * file)
5030{
5031 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5032
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005033 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 mddev_put(mddev);
5035
5036 return 0;
5037}
5038
Linus Torvalds44ce62942007-05-09 18:51:36 -07005039static int md_media_changed(struct gendisk *disk)
5040{
5041 mddev_t *mddev = disk->private_data;
5042
5043 return mddev->changed;
5044}
5045
5046static int md_revalidate(struct gendisk *disk)
5047{
5048 mddev_t *mddev = disk->private_data;
5049
5050 mddev->changed = 0;
5051 return 0;
5052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053static struct block_device_operations md_fops =
5054{
5055 .owner = THIS_MODULE,
5056 .open = md_open,
5057 .release = md_release,
5058 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005059 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005060 .media_changed = md_media_changed,
5061 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062};
5063
Adrian Bunk75c96f82005-05-05 16:16:09 -07005064static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065{
5066 mdk_thread_t *thread = arg;
5067
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 /*
5069 * md_thread is a 'system-thread', it's priority should be very
5070 * high. We avoid resource deadlocks individually in each
5071 * raid personality. (RAID5 does preallocation) We also use RR and
5072 * the very same RT priority as kswapd, thus we will never get
5073 * into a priority inversion deadlock.
5074 *
5075 * we definitely have to have equal or higher priority than
5076 * bdflush, otherwise bdflush will deadlock if there are too
5077 * many dirty RAID5 blocks.
5078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079
NeilBrown6985c432005-10-19 21:23:47 -07005080 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005081 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
NeilBrown93588e22005-11-15 00:09:12 -08005083 /* We need to wait INTERRUPTIBLE so that
5084 * we don't add to the load-average.
5085 * That means we need to be sure no signals are
5086 * pending
5087 */
5088 if (signal_pending(current))
5089 flush_signals(current);
5090
5091 wait_event_interruptible_timeout
5092 (thread->wqueue,
5093 test_bit(THREAD_WAKEUP, &thread->flags)
5094 || kthread_should_stop(),
5095 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
5097 clear_bit(THREAD_WAKEUP, &thread->flags);
5098
NeilBrown787453c2005-11-08 21:39:43 -08005099 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005101
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 return 0;
5103}
5104
5105void md_wakeup_thread(mdk_thread_t *thread)
5106{
5107 if (thread) {
5108 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5109 set_bit(THREAD_WAKEUP, &thread->flags);
5110 wake_up(&thread->wqueue);
5111 }
5112}
5113
5114mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5115 const char *name)
5116{
5117 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118
NeilBrown9ffae0c2006-01-06 00:20:32 -08005119 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 if (!thread)
5121 return NULL;
5122
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 init_waitqueue_head(&thread->wqueue);
5124
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 thread->run = run;
5126 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005127 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005128 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005129 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 kfree(thread);
5131 return NULL;
5132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 return thread;
5134}
5135
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136void md_unregister_thread(mdk_thread_t *thread)
5137{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005138 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005139
5140 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 kfree(thread);
5142}
5143
5144void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5145{
5146 if (!mddev) {
5147 MD_BUG();
5148 return;
5149 }
5150
NeilBrownb2d444d2005-11-08 21:39:31 -08005151 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005153
5154 if (mddev->external)
5155 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5158 mdname(mddev),
5159 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5160 __builtin_return_address(0),__builtin_return_address(1),
5161 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005162*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005163 if (!mddev->pers)
5164 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 if (!mddev->pers->error_handler)
5166 return;
5167 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005168 if (mddev->degraded)
5169 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005170 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5172 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5173 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005174 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175}
5176
5177/* seq_file implementation /proc/mdstat */
5178
5179static void status_unused(struct seq_file *seq)
5180{
5181 int i = 0;
5182 mdk_rdev_t *rdev;
5183 struct list_head *tmp;
5184
5185 seq_printf(seq, "unused devices: ");
5186
NeilBrown73c34432008-02-06 01:39:59 -08005187 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 char b[BDEVNAME_SIZE];
5189 i++;
5190 seq_printf(seq, "%s ",
5191 bdevname(rdev->bdev,b));
5192 }
5193 if (!i)
5194 seq_printf(seq, "<none>");
5195
5196 seq_printf(seq, "\n");
5197}
5198
5199
5200static void status_resync(struct seq_file *seq, mddev_t * mddev)
5201{
NeilBrown4588b422006-03-27 01:18:04 -08005202 sector_t max_blocks, resync, res;
5203 unsigned long dt, db, rt;
5204 int scale;
5205 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
5207 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5208
5209 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5210 max_blocks = mddev->resync_max_sectors >> 1;
5211 else
5212 max_blocks = mddev->size;
5213
5214 /*
5215 * Should not happen.
5216 */
5217 if (!max_blocks) {
5218 MD_BUG();
5219 return;
5220 }
NeilBrown4588b422006-03-27 01:18:04 -08005221 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5222 * in a sector_t, and (max_blocks>>scale) will fit in a
5223 * u32, as those are the requirements for sector_div.
5224 * Thus 'scale' must be at least 10
5225 */
5226 scale = 10;
5227 if (sizeof(sector_t) > sizeof(unsigned long)) {
5228 while ( max_blocks/2 > (1ULL<<(scale+32)))
5229 scale++;
5230 }
5231 res = (resync>>scale)*1000;
5232 sector_div(res, (u32)((max_blocks>>scale)+1));
5233
5234 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 {
NeilBrown4588b422006-03-27 01:18:04 -08005236 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 seq_printf(seq, "[");
5238 for (i = 0; i < x; i++)
5239 seq_printf(seq, "=");
5240 seq_printf(seq, ">");
5241 for (i = 0; i < y; i++)
5242 seq_printf(seq, ".");
5243 seq_printf(seq, "] ");
5244 }
NeilBrown4588b422006-03-27 01:18:04 -08005245 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005246 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5247 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005248 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5249 "check" :
5250 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5251 "resync" : "recovery"))),
5252 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005253 (unsigned long long) resync,
5254 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255
5256 /*
5257 * We do not want to overflow, so the order of operands and
5258 * the * 100 / 100 trick are important. We do a +1 to be
5259 * safe against division by zero. We only estimate anyway.
5260 *
5261 * dt: time from mark until now
5262 * db: blocks written from mark until now
5263 * rt: remaining time
5264 */
5265 dt = ((jiffies - mddev->resync_mark) / HZ);
5266 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005267 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5268 - mddev->resync_mark_cnt;
5269 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270
5271 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5272
NeilBrownff4e8d92006-07-10 04:44:16 -07005273 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274}
5275
5276static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5277{
5278 struct list_head *tmp;
5279 loff_t l = *pos;
5280 mddev_t *mddev;
5281
5282 if (l >= 0x10000)
5283 return NULL;
5284 if (!l--)
5285 /* header */
5286 return (void*)1;
5287
5288 spin_lock(&all_mddevs_lock);
5289 list_for_each(tmp,&all_mddevs)
5290 if (!l--) {
5291 mddev = list_entry(tmp, mddev_t, all_mddevs);
5292 mddev_get(mddev);
5293 spin_unlock(&all_mddevs_lock);
5294 return mddev;
5295 }
5296 spin_unlock(&all_mddevs_lock);
5297 if (!l--)
5298 return (void*)2;/* tail */
5299 return NULL;
5300}
5301
5302static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5303{
5304 struct list_head *tmp;
5305 mddev_t *next_mddev, *mddev = v;
5306
5307 ++*pos;
5308 if (v == (void*)2)
5309 return NULL;
5310
5311 spin_lock(&all_mddevs_lock);
5312 if (v == (void*)1)
5313 tmp = all_mddevs.next;
5314 else
5315 tmp = mddev->all_mddevs.next;
5316 if (tmp != &all_mddevs)
5317 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5318 else {
5319 next_mddev = (void*)2;
5320 *pos = 0x10000;
5321 }
5322 spin_unlock(&all_mddevs_lock);
5323
5324 if (v != (void*)1)
5325 mddev_put(mddev);
5326 return next_mddev;
5327
5328}
5329
5330static void md_seq_stop(struct seq_file *seq, void *v)
5331{
5332 mddev_t *mddev = v;
5333
5334 if (mddev && v != (void*)1 && v != (void*)2)
5335 mddev_put(mddev);
5336}
5337
NeilBrownd7603b72006-01-06 00:20:30 -08005338struct mdstat_info {
5339 int event;
5340};
5341
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342static int md_seq_show(struct seq_file *seq, void *v)
5343{
5344 mddev_t *mddev = v;
5345 sector_t size;
5346 struct list_head *tmp2;
5347 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005348 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005349 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350
5351 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005352 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 seq_printf(seq, "Personalities : ");
5354 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005355 list_for_each_entry(pers, &pers_list, list)
5356 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
5358 spin_unlock(&pers_lock);
5359 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005360 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 return 0;
5362 }
5363 if (v == (void*)2) {
5364 status_unused(seq);
5365 return 0;
5366 }
5367
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005368 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005370
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5372 seq_printf(seq, "%s : %sactive", mdname(mddev),
5373 mddev->pers ? "" : "in");
5374 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005375 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005377 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005378 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 seq_printf(seq, " %s", mddev->pers->name);
5380 }
5381
5382 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005383 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 char b[BDEVNAME_SIZE];
5385 seq_printf(seq, " %s[%d]",
5386 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005387 if (test_bit(WriteMostly, &rdev->flags))
5388 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005389 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 seq_printf(seq, "(F)");
5391 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005392 } else if (rdev->raid_disk < 0)
5393 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 size += rdev->size;
5395 }
5396
5397 if (!list_empty(&mddev->disks)) {
5398 if (mddev->pers)
5399 seq_printf(seq, "\n %llu blocks",
5400 (unsigned long long)mddev->array_size);
5401 else
5402 seq_printf(seq, "\n %llu blocks",
5403 (unsigned long long)size);
5404 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005405 if (mddev->persistent) {
5406 if (mddev->major_version != 0 ||
5407 mddev->minor_version != 90) {
5408 seq_printf(seq," super %d.%d",
5409 mddev->major_version,
5410 mddev->minor_version);
5411 }
NeilBrowne6910632008-02-06 01:39:51 -08005412 } else if (mddev->external)
5413 seq_printf(seq, " super external:%s",
5414 mddev->metadata_type);
5415 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005416 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417
5418 if (mddev->pers) {
5419 mddev->pers->status (seq, mddev);
5420 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005421 if (mddev->pers->sync_request) {
5422 if (mddev->curr_resync > 2) {
5423 status_resync (seq, mddev);
5424 seq_printf(seq, "\n ");
5425 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5426 seq_printf(seq, "\tresync=DELAYED\n ");
5427 else if (mddev->recovery_cp < MaxSector)
5428 seq_printf(seq, "\tresync=PENDING\n ");
5429 }
NeilBrown32a76272005-06-21 17:17:14 -07005430 } else
5431 seq_printf(seq, "\n ");
5432
5433 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005434 unsigned long chunk_kb;
5435 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005436 spin_lock_irqsave(&bitmap->lock, flags);
5437 chunk_kb = bitmap->chunksize >> 10;
5438 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5439 "%lu%s chunk",
5440 bitmap->pages - bitmap->missing_pages,
5441 bitmap->pages,
5442 (bitmap->pages - bitmap->missing_pages)
5443 << (PAGE_SHIFT - 10),
5444 chunk_kb ? chunk_kb : bitmap->chunksize,
5445 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005446 if (bitmap->file) {
5447 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005448 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005449 }
NeilBrown78d742d2005-06-21 17:17:15 -07005450
NeilBrown32a76272005-06-21 17:17:14 -07005451 seq_printf(seq, "\n");
5452 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 }
5454
5455 seq_printf(seq, "\n");
5456 }
5457 mddev_unlock(mddev);
5458
5459 return 0;
5460}
5461
5462static struct seq_operations md_seq_ops = {
5463 .start = md_seq_start,
5464 .next = md_seq_next,
5465 .stop = md_seq_stop,
5466 .show = md_seq_show,
5467};
5468
5469static int md_seq_open(struct inode *inode, struct file *file)
5470{
5471 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005472 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5473 if (mi == NULL)
5474 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
5476 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005477 if (error)
5478 kfree(mi);
5479 else {
5480 struct seq_file *p = file->private_data;
5481 p->private = mi;
5482 mi->event = atomic_read(&md_event_count);
5483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 return error;
5485}
5486
NeilBrownd7603b72006-01-06 00:20:30 -08005487static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5488{
5489 struct seq_file *m = filp->private_data;
5490 struct mdstat_info *mi = m->private;
5491 int mask;
5492
5493 poll_wait(filp, &md_event_waiters, wait);
5494
5495 /* always allow read */
5496 mask = POLLIN | POLLRDNORM;
5497
5498 if (mi->event != atomic_read(&md_event_count))
5499 mask |= POLLERR | POLLPRI;
5500 return mask;
5501}
5502
Arjan van de Venfa027c22007-02-12 00:55:33 -08005503static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005504 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 .open = md_seq_open,
5506 .read = seq_read,
5507 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005508 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005509 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510};
5511
NeilBrown2604b702006-01-06 00:20:36 -08005512int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005515 list_add_tail(&p->list, &pers_list);
5516 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 spin_unlock(&pers_lock);
5518 return 0;
5519}
5520
NeilBrown2604b702006-01-06 00:20:36 -08005521int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522{
NeilBrown2604b702006-01-06 00:20:36 -08005523 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005525 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 spin_unlock(&pers_lock);
5527 return 0;
5528}
5529
5530static int is_mddev_idle(mddev_t *mddev)
5531{
5532 mdk_rdev_t * rdev;
5533 struct list_head *tmp;
5534 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005535 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536
5537 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005538 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005540 curr_events = disk_stat_read(disk, sectors[0]) +
5541 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005543 /* sync IO will cause sync_io to increase before the disk_stats
5544 * as sync_io is counted when a request starts, and
5545 * disk_stats is counted when it completes.
5546 * So resync activity will cause curr_events to be smaller than
5547 * when there was no such activity.
5548 * non-sync IO will cause disk_stat to increase without
5549 * increasing sync_io so curr_events will (eventually)
5550 * be larger than it was before. Once it becomes
5551 * substantially larger, the test below will cause
5552 * the array to appear non-idle, and resync will slow
5553 * down.
5554 * If there is a lot of outstanding resync activity when
5555 * we set last_event to curr_events, then all that activity
5556 * completing might cause the array to appear non-idle
5557 * and resync will be slowed down even though there might
5558 * not have been non-resync activity. This will only
5559 * happen once though. 'last_events' will soon reflect
5560 * the state where there is little or no outstanding
5561 * resync requests, and further resync activity will
5562 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005563 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005565 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 rdev->last_events = curr_events;
5567 idle = 0;
5568 }
5569 }
5570 return idle;
5571}
5572
5573void md_done_sync(mddev_t *mddev, int blocks, int ok)
5574{
5575 /* another "blocks" (512byte) blocks have been synced */
5576 atomic_sub(blocks, &mddev->recovery_active);
5577 wake_up(&mddev->recovery_wait);
5578 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005579 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 md_wakeup_thread(mddev->thread);
5581 // stop recovery, signal do_sync ....
5582 }
5583}
5584
5585
NeilBrown06d91a52005-06-21 17:17:12 -07005586/* md_write_start(mddev, bi)
5587 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005588 * in superblock) before writing, schedule a superblock update
5589 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005590 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005591void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592{
Neil Brown0fd62b82008-06-28 08:31:36 +10005593 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005594 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005595 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005596
NeilBrownf91de922005-11-08 21:39:36 -08005597 BUG_ON(mddev->ro == 1);
5598 if (mddev->ro == 2) {
5599 /* need to switch to read/write */
5600 mddev->ro = 0;
5601 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5602 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005603 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005604 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005605 }
NeilBrown06d91a52005-06-21 17:17:12 -07005606 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005607 if (mddev->safemode == 1)
5608 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005609 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005610 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005611 if (mddev->in_sync) {
5612 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005613 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005614 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005615 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005616 }
NeilBrowna9701a32005-11-08 21:39:34 -08005617 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005618 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005619 if (did_change)
5620 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown09a44cc2008-05-23 13:04:36 -07005621 wait_event(mddev->sb_wait,
5622 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5623 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624}
5625
5626void md_write_end(mddev_t *mddev)
5627{
5628 if (atomic_dec_and_test(&mddev->writes_pending)) {
5629 if (mddev->safemode == 2)
5630 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005631 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5633 }
5634}
5635
NeilBrown2a2275d2007-01-26 00:57:11 -08005636/* md_allow_write(mddev)
5637 * Calling this ensures that the array is marked 'active' so that writes
5638 * may proceed without blocking. It is important to call this before
5639 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5640 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005641 *
5642 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5643 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005644 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005645int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005646{
5647 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005648 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005649 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005650 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005651 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005652 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005653
5654 spin_lock_irq(&mddev->write_lock);
5655 if (mddev->in_sync) {
5656 mddev->in_sync = 0;
5657 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5658 if (mddev->safemode_delay &&
5659 mddev->safemode == 0)
5660 mddev->safemode = 1;
5661 spin_unlock_irq(&mddev->write_lock);
5662 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005663 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown2a2275d2007-01-26 00:57:11 -08005664 } else
5665 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005666
5667 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5668 return -EAGAIN;
5669 else
5670 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005671}
5672EXPORT_SYMBOL_GPL(md_allow_write);
5673
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674#define SYNC_MARKS 10
5675#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005676void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677{
5678 mddev_t *mddev2;
5679 unsigned int currspeed = 0,
5680 window;
NeilBrown57afd892005-06-21 17:17:13 -07005681 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 unsigned long mark[SYNC_MARKS];
5683 sector_t mark_cnt[SYNC_MARKS];
5684 int last_mark,m;
5685 struct list_head *tmp;
5686 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005687 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005688 struct list_head *rtmp;
5689 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005690 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691
5692 /* just incase thread restarts... */
5693 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5694 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005695 if (mddev->ro) /* never try to sync a read-only array */
5696 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
NeilBrown61df9d92006-10-03 01:15:57 -07005698 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5699 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5700 desc = "data-check";
5701 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5702 desc = "requested-resync";
5703 else
5704 desc = "resync";
5705 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5706 desc = "reshape";
5707 else
5708 desc = "recovery";
5709
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 /* we overload curr_resync somewhat here.
5711 * 0 == not engaged in resync at all
5712 * 2 == checking that there is no conflict with another sync
5713 * 1 == like 2, but have yielded to allow conflicting resync to
5714 * commense
5715 * other == active in resync - this many blocks
5716 *
5717 * Before starting a resync we must have set curr_resync to
5718 * 2, and then checked that every "conflicting" array has curr_resync
5719 * less than ours. When we find one that is the same or higher
5720 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5721 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5722 * This will mean we have to start checking from the beginning again.
5723 *
5724 */
5725
5726 do {
5727 mddev->curr_resync = 2;
5728
5729 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005730 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005731 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 goto skip;
5733 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005734 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 if (mddev2 == mddev)
5736 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005737 if (!mddev->parallel_resync
5738 && mddev2->curr_resync
5739 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 DEFINE_WAIT(wq);
5741 if (mddev < mddev2 && mddev->curr_resync == 2) {
5742 /* arbitrarily yield */
5743 mddev->curr_resync = 1;
5744 wake_up(&resync_wait);
5745 }
5746 if (mddev > mddev2 && mddev->curr_resync == 1)
5747 /* no need to wait here, we can wait the next
5748 * time 'round when curr_resync == 2
5749 */
5750 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005751 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5752 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005753 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005754 printk(KERN_INFO "md: delaying %s of %s"
5755 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005757 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 mddev_put(mddev2);
5759 schedule();
5760 finish_wait(&resync_wait, &wq);
5761 goto try_again;
5762 }
5763 finish_wait(&resync_wait, &wq);
5764 }
5765 }
5766 } while (mddev->curr_resync < 2);
5767
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005768 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005769 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005771 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 */
5773 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005774 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005775 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005776 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5777 j = mddev->resync_min;
5778 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005779 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005780
NeilBrownccfcc3c2006-03-27 01:18:09 -08005781 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5782 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005783 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 /* recovery follows the physical size of devices */
5785 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005786 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005787 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005788 if (rdev->raid_disk >= 0 &&
5789 !test_bit(Faulty, &rdev->flags) &&
5790 !test_bit(In_sync, &rdev->flags) &&
5791 rdev->recovery_offset < j)
5792 j = rdev->recovery_offset;
5793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
NeilBrown61df9d92006-10-03 01:15:57 -07005795 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5796 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5797 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005798 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005799 "(but not more than %d KB/sec) for %s.\n",
5800 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801
5802 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005803
NeilBrown57afd892005-06-21 17:17:13 -07005804 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 for (m = 0; m < SYNC_MARKS; m++) {
5806 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005807 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 }
5809 last_mark = 0;
5810 mddev->resync_mark = mark[last_mark];
5811 mddev->resync_mark_cnt = mark_cnt[last_mark];
5812
5813 /*
5814 * Tune reconstruction:
5815 */
5816 window = 32*(PAGE_SIZE/512);
5817 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5818 window/2,(unsigned long long) max_sectors/2);
5819
5820 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 last_check = 0;
5822
5823 if (j>2) {
5824 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005825 "md: resuming %s of %s from checkpoint.\n",
5826 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827 mddev->curr_resync = j;
5828 }
5829
5830 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005831 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
NeilBrown57afd892005-06-21 17:17:13 -07005833 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005834 if (j >= mddev->resync_max) {
5835 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5836 wait_event(mddev->recovery_wait,
5837 mddev->resync_max > j
5838 || kthread_should_stop());
5839 }
5840 if (kthread_should_stop())
5841 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005842 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005843 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005844 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005845 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 goto out;
5847 }
NeilBrown57afd892005-06-21 17:17:13 -07005848
5849 if (!skipped) { /* actual IO requested */
5850 io_sectors += sectors;
5851 atomic_add(sectors, &mddev->recovery_active);
5852 }
5853
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 j += sectors;
5855 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005856 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005857 if (last_check == 0)
5858 /* this is the earliers that rebuilt will be
5859 * visible in /proc/mdstat
5860 */
5861 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005862
5863 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 continue;
5865
NeilBrown57afd892005-06-21 17:17:13 -07005866 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867
NeilBrowndfc70642008-05-23 13:04:39 -07005868 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 break;
5870
5871 repeat:
5872 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5873 /* step marks */
5874 int next = (last_mark+1) % SYNC_MARKS;
5875
5876 mddev->resync_mark = mark[next];
5877 mddev->resync_mark_cnt = mark_cnt[next];
5878 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005879 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 last_mark = next;
5881 }
5882
5883
NeilBrownc6207272008-02-06 01:39:52 -08005884 if (kthread_should_stop())
5885 goto interrupted;
5886
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887
5888 /*
5889 * this loop exits only if either when we are slower than
5890 * the 'hard' speed limit, or the system was IO-idle for
5891 * a jiffy.
5892 * the system might be non-idle CPU-wise, but we only care
5893 * about not overloading the IO subsystem. (things like an
5894 * e2fsck being done on the RAID array should execute fast)
5895 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005896 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 cond_resched();
5898
NeilBrown57afd892005-06-21 17:17:13 -07005899 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5900 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
NeilBrown88202a02006-01-06 00:21:36 -08005902 if (currspeed > speed_min(mddev)) {
5903 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005905 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 goto repeat;
5907 }
5908 }
5909 }
NeilBrown61df9d92006-10-03 01:15:57 -07005910 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 /*
5912 * this also signals 'finished resyncing' to md_stop
5913 */
5914 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005915 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
5917 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5918
5919 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005920 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921
NeilBrowndfc70642008-05-23 13:04:39 -07005922 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005923 mddev->curr_resync > 2) {
5924 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5925 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5926 if (mddev->curr_resync >= mddev->recovery_cp) {
5927 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005928 "md: checkpointing %s of %s.\n",
5929 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005930 mddev->recovery_cp = mddev->curr_resync;
5931 }
5932 } else
5933 mddev->recovery_cp = MaxSector;
5934 } else {
5935 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5936 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005937 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005938 if (rdev->raid_disk >= 0 &&
5939 !test_bit(Faulty, &rdev->flags) &&
5940 !test_bit(In_sync, &rdev->flags) &&
5941 rdev->recovery_offset < mddev->curr_resync)
5942 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 }
NeilBrown17571282006-12-10 02:20:52 -08005945 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 skip:
5948 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005949 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005950 mddev->resync_max = MaxSector;
5951 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 wake_up(&resync_wait);
5953 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5954 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005955 return;
5956
5957 interrupted:
5958 /*
5959 * got a signal, exit.
5960 */
5961 printk(KERN_INFO
5962 "md: md_do_sync() got signal ... exiting\n");
5963 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5964 goto out;
5965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966}
NeilBrown29269552006-03-27 01:18:10 -08005967EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968
5969
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005970static int remove_and_add_spares(mddev_t *mddev)
5971{
5972 mdk_rdev_t *rdev;
5973 struct list_head *rtmp;
5974 int spares = 0;
5975
NeilBrownd089c6a2008-02-06 01:39:59 -08005976 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005977 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005978 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005979 (test_bit(Faulty, &rdev->flags) ||
5980 ! test_bit(In_sync, &rdev->flags)) &&
5981 atomic_read(&rdev->nr_pending)==0) {
5982 if (mddev->pers->hot_remove_disk(
5983 mddev, rdev->raid_disk)==0) {
5984 char nm[20];
5985 sprintf(nm,"rd%d", rdev->raid_disk);
5986 sysfs_remove_link(&mddev->kobj, nm);
5987 rdev->raid_disk = -1;
5988 }
5989 }
5990
5991 if (mddev->degraded) {
NeilBrowndfc70642008-05-23 13:04:39 -07005992 rdev_for_each(rdev, rtmp, mddev) {
5993 if (rdev->raid_disk >= 0 &&
5994 !test_bit(In_sync, &rdev->flags))
5995 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005996 if (rdev->raid_disk < 0
5997 && !test_bit(Faulty, &rdev->flags)) {
5998 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10005999 if (mddev->pers->
6000 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006001 char nm[20];
6002 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006003 if (sysfs_create_link(&mddev->kobj,
6004 &rdev->kobj, nm))
6005 printk(KERN_WARNING
6006 "md: cannot register "
6007 "%s for %s\n",
6008 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006009 spares++;
6010 md_new_event(mddev);
6011 } else
6012 break;
6013 }
NeilBrowndfc70642008-05-23 13:04:39 -07006014 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006015 }
6016 return spares;
6017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018/*
6019 * This routine is regularly called by all per-raid-array threads to
6020 * deal with generic issues like resync and super-block update.
6021 * Raid personalities that don't have a thread (linear/raid0) do not
6022 * need this as they never do any recovery or update the superblock.
6023 *
6024 * It does not do any resync itself, but rather "forks" off other threads
6025 * to do that as needed.
6026 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6027 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006028 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 * and wakeups up this thread which will reap the thread and finish up.
6030 * This thread also removes any faulty devices (with nr_pending == 0).
6031 *
6032 * The overall approach is:
6033 * 1/ if the superblock needs updating, update it.
6034 * 2/ If a recovery thread is running, don't do anything else.
6035 * 3/ If recovery has finished, clean up, possibly marking spares active.
6036 * 4/ If there are any faulty devices, remove them.
6037 * 5/ If array is degraded, try to add spares devices
6038 * 6/ If array has spares or is not in-sync, start a resync thread.
6039 */
6040void md_check_recovery(mddev_t *mddev)
6041{
6042 mdk_rdev_t *rdev;
6043 struct list_head *rtmp;
6044
6045
NeilBrown5f404022005-06-21 17:17:16 -07006046 if (mddev->bitmap)
6047 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048
6049 if (mddev->ro)
6050 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006051
6052 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006053 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006054 printk(KERN_INFO "md: %s in immediate safe mode\n",
6055 mdname(mddev));
6056 mddev->safemode = 2;
6057 }
6058 flush_signals(current);
6059 }
6060
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006062 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006064 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006065 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006066 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6067 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 ))
6069 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006070
NeilBrowndf5b89b2006-03-27 01:18:20 -08006071 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006072 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006073
NeilBrown31a59e32008-04-30 00:52:30 -07006074 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006075 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006076 spin_lock_irq(&mddev->write_lock);
6077 if (mddev->safemode &&
6078 !atomic_read(&mddev->writes_pending) &&
6079 !mddev->in_sync &&
6080 mddev->recovery_cp == MaxSector) {
6081 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006082 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006083 if (mddev->persistent)
6084 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6085 }
6086 if (mddev->safemode == 1)
6087 mddev->safemode = 0;
6088 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006089 if (did_change)
6090 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrownfca4d842005-06-21 17:17:11 -07006091 }
NeilBrownfca4d842005-06-21 17:17:11 -07006092
NeilBrown850b2b42006-10-03 01:15:46 -07006093 if (mddev->flags)
6094 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006095
Neil Brown52664732008-06-28 08:31:44 +10006096 rdev_for_each(rdev, rtmp, mddev)
6097 if (test_and_clear_bit(StateChanged, &rdev->flags))
6098 sysfs_notify(&rdev->kobj, NULL, "state");
6099
NeilBrown06d91a52005-06-21 17:17:12 -07006100
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6102 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6103 /* resync/recovery still happening */
6104 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6105 goto unlock;
6106 }
6107 if (mddev->sync_thread) {
6108 /* resync has finished, collect result */
6109 md_unregister_thread(mddev->sync_thread);
6110 mddev->sync_thread = NULL;
NeilBrowndfc70642008-05-23 13:04:39 -07006111 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 /* success...*/
6113 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006114 if (mddev->pers->spare_active(mddev))
6115 sysfs_notify(&mddev->kobj, NULL,
6116 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006117 }
NeilBrown850b2b42006-10-03 01:15:46 -07006118 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006119
6120 /* if array is no-longer degraded, then any saved_raid_disk
6121 * information must be scrapped
6122 */
6123 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006124 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006125 rdev->saved_raid_disk = -1;
6126
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 mddev->recovery = 0;
6128 /* flag recovery needed just to double check */
6129 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006130 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006131 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 goto unlock;
6133 }
Neil Brown72a23c22008-06-28 08:31:41 +10006134 /* Set RUNNING before clearing NEEDED to avoid
6135 * any transients in the value of "sync_action".
6136 */
6137 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6138 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006139 /* Clear some bits that don't mean anything, but
6140 * might be left set
6141 */
NeilBrown24dd4692005-11-08 21:39:26 -08006142 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6143 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006144
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006145 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6146 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 /* no recovery is running.
6148 * remove any failed drives, then
6149 * add spares if possible.
6150 * Spare are also removed and re-added, to allow
6151 * the personality to fail the re-add.
6152 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006154 if (mddev->reshape_position != MaxSector) {
6155 if (mddev->pers->check_reshape(mddev) != 0)
6156 /* Cannot proceed */
6157 goto unlock;
6158 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006159 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006160 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006161 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6162 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006163 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006164 } else if (mddev->recovery_cp < MaxSector) {
6165 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006166 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006167 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6168 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006170
Linus Torvalds1da177e2005-04-16 15:20:36 -07006171 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006172 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6173 /* We are adding a device or devices to an array
6174 * which has the bitmap stored on all devices.
6175 * So make sure all bitmap pages get written
6176 */
6177 bitmap_write_all(mddev->bitmap);
6178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 mddev->sync_thread = md_register_thread(md_do_sync,
6180 mddev,
6181 "%s_resync");
6182 if (!mddev->sync_thread) {
6183 printk(KERN_ERR "%s: could not start resync"
6184 " thread...\n",
6185 mdname(mddev));
6186 /* leave the spares where they are, it shouldn't hurt */
6187 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006188 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 md_wakeup_thread(mddev->sync_thread);
Neil Brown72a23c22008-06-28 08:31:41 +10006190 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006191 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192 }
6193 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006194 if (!mddev->sync_thread) {
6195 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6196 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6197 &mddev->recovery))
6198 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 mddev_unlock(mddev);
6201 }
6202}
6203
Dan Williams6bfe0b42008-04-30 00:52:32 -07006204void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6205{
6206 sysfs_notify(&rdev->kobj, NULL, "state");
6207 wait_event_timeout(rdev->blocked_wait,
6208 !test_bit(Blocked, &rdev->flags),
6209 msecs_to_jiffies(5000));
6210 rdev_dec_pending(rdev, mddev);
6211}
6212EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6213
Adrian Bunk75c96f82005-05-05 16:16:09 -07006214static int md_notify_reboot(struct notifier_block *this,
6215 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216{
6217 struct list_head *tmp;
6218 mddev_t *mddev;
6219
6220 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6221
6222 printk(KERN_INFO "md: stopping all md devices.\n");
6223
NeilBrown29ac4aa2008-02-06 01:39:58 -08006224 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006225 if (mddev_trylock(mddev)) {
Neil Browndf5b20c2008-07-11 22:02:22 +10006226 do_md_stop (mddev, 1, 0);
Neil Brownc71d4882006-05-26 10:39:25 +10006227 mddev_unlock(mddev);
6228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 /*
6230 * certain more exotic SCSI devices are known to be
6231 * volatile wrt too early system reboots. While the
6232 * right place to handle this issue is the given
6233 * driver, we do want to have a safe RAID driver ...
6234 */
6235 mdelay(1000*1);
6236 }
6237 return NOTIFY_DONE;
6238}
6239
Adrian Bunk75c96f82005-05-05 16:16:09 -07006240static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 .notifier_call = md_notify_reboot,
6242 .next = NULL,
6243 .priority = INT_MAX, /* before any real devices */
6244};
6245
6246static void md_geninit(void)
6247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6249
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006250 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251}
6252
Adrian Bunk75c96f82005-05-05 16:16:09 -07006253static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 if (register_blkdev(MAJOR_NR, "md"))
6256 return -1;
6257 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6258 unregister_blkdev(MAJOR_NR, "md");
6259 return -1;
6260 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006261 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6262 md_probe, NULL, NULL);
6263 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006264 md_probe, NULL, NULL);
6265
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006267 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268
6269 md_geninit();
6270 return (0);
6271}
6272
6273
6274#ifndef MODULE
6275
6276/*
6277 * Searches all registered partitions for autorun RAID arrays
6278 * at boot time.
6279 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006280
6281static LIST_HEAD(all_detected_devices);
6282struct detected_devices_node {
6283 struct list_head list;
6284 dev_t dev;
6285};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286
6287void md_autodetect_dev(dev_t dev)
6288{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006289 struct detected_devices_node *node_detected_dev;
6290
6291 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6292 if (node_detected_dev) {
6293 node_detected_dev->dev = dev;
6294 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6295 } else {
6296 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6297 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299}
6300
6301
6302static void autostart_arrays(int part)
6303{
6304 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006305 struct detected_devices_node *node_detected_dev;
6306 dev_t dev;
6307 int i_scanned, i_passed;
6308
6309 i_scanned = 0;
6310 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
6312 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6313
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006314 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6315 i_scanned++;
6316 node_detected_dev = list_entry(all_detected_devices.next,
6317 struct detected_devices_node, list);
6318 list_del(&node_detected_dev->list);
6319 dev = node_detected_dev->dev;
6320 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006321 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 if (IS_ERR(rdev))
6323 continue;
6324
NeilBrownb2d444d2005-11-08 21:39:31 -08006325 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326 MD_BUG();
6327 continue;
6328 }
NeilBrownd0fae182008-03-04 14:29:31 -08006329 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006331 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006333
6334 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6335 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
6337 autorun_devices(part);
6338}
6339
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006340#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
6342static __exit void md_exit(void)
6343{
6344 mddev_t *mddev;
6345 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006346
NeilBrowne8703fe2006-10-03 01:15:59 -07006347 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6348 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
6350 unregister_blkdev(MAJOR_NR,"md");
6351 unregister_blkdev(mdp_major, "mdp");
6352 unregister_reboot_notifier(&md_notifier);
6353 unregister_sysctl_table(raid_table_header);
6354 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006355 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356 struct gendisk *disk = mddev->gendisk;
6357 if (!disk)
6358 continue;
6359 export_array(mddev);
6360 del_gendisk(disk);
6361 put_disk(disk);
6362 mddev->gendisk = NULL;
6363 mddev_put(mddev);
6364 }
6365}
6366
Dan Williams685784a2007-07-09 11:56:42 -07006367subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368module_exit(md_exit)
6369
NeilBrownf91de922005-11-08 21:39:36 -08006370static int get_ro(char *buffer, struct kernel_param *kp)
6371{
6372 return sprintf(buffer, "%d", start_readonly);
6373}
6374static int set_ro(const char *val, struct kernel_param *kp)
6375{
6376 char *e;
6377 int num = simple_strtoul(val, &e, 10);
6378 if (*val && (*e == '\0' || *e == '\n')) {
6379 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006380 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006381 }
6382 return -EINVAL;
6383}
6384
NeilBrown80ca3a42006-07-10 04:44:18 -07006385module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6386module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006387
NeilBrownf91de922005-11-08 21:39:36 -08006388
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389EXPORT_SYMBOL(register_md_personality);
6390EXPORT_SYMBOL(unregister_md_personality);
6391EXPORT_SYMBOL(md_error);
6392EXPORT_SYMBOL(md_done_sync);
6393EXPORT_SYMBOL(md_write_start);
6394EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395EXPORT_SYMBOL(md_register_thread);
6396EXPORT_SYMBOL(md_unregister_thread);
6397EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398EXPORT_SYMBOL(md_check_recovery);
6399MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006400MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006401MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);