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