blob: 95466bb089ab352ce5c4538431bad3488567b1ff [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
Andre Nollb73df2d2008-07-11 22:02:23 +1000350/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700351static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Andre Nollb73df2d2008-07-11 22:02:23 +1000353 sector_t num_sectors = bdev->bd_inode->i_size / 512;
354 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Andre Nolle7debaa2008-07-11 22:02:23 +1000357static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Andre Noll0f420352008-07-11 22:02:23 +1000359 sector_t num_sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000362 num_sectors &= ~((sector_t)chunk_size/512 - 1);
363 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
366static int alloc_disk_sb(mdk_rdev_t * rdev)
367{
368 if (rdev->sb_page)
369 MD_BUG();
370
371 rdev->sb_page = alloc_page(GFP_KERNEL);
372 if (!rdev->sb_page) {
373 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000374 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 return 0;
378}
379
380static void free_disk_sb(mdk_rdev_t * rdev)
381{
382 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800383 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 rdev->sb_loaded = 0;
385 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000386 rdev->sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 rdev->size = 0;
388 }
389}
390
391
NeilBrown6712ecf2007-09-27 12:47:43 +0200392static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700393{
394 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800395 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700396
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700397 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
398 printk("md: super_written gets error=%d, uptodate=%d\n",
399 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
400 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800401 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700402 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700403
NeilBrowna9701a32005-11-08 21:39:34 -0800404 if (atomic_dec_and_test(&mddev->pending_writes))
405 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700406 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700407}
408
NeilBrown6712ecf2007-09-27 12:47:43 +0200409static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800410{
411 struct bio *bio2 = bio->bi_private;
412 mdk_rdev_t *rdev = bio2->bi_private;
413 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800414
415 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
416 error == -EOPNOTSUPP) {
417 unsigned long flags;
418 /* barriers don't appear to be supported :-( */
419 set_bit(BarriersNotsupp, &rdev->flags);
420 mddev->barriers_work = 0;
421 spin_lock_irqsave(&mddev->write_lock, flags);
422 bio2->bi_next = mddev->biolist;
423 mddev->biolist = bio2;
424 spin_unlock_irqrestore(&mddev->write_lock, flags);
425 wake_up(&mddev->sb_wait);
426 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200427 } else {
428 bio_put(bio2);
429 bio->bi_private = rdev;
430 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800431 }
NeilBrowna9701a32005-11-08 21:39:34 -0800432}
433
NeilBrown7bfa19f2005-06-21 17:17:28 -0700434void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
435 sector_t sector, int size, struct page *page)
436{
437 /* write first size bytes of page to sector of rdev
438 * Increment mddev->pending_writes before returning
439 * and decrement it on completion, waking up sb_wait
440 * if zero is reached.
441 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800442 *
443 * As we might need to resubmit the request if BIO_RW_BARRIER
444 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700445 */
446 struct bio *bio = bio_alloc(GFP_NOIO, 1);
NeilBrowna9701a32005-11-08 21:39:34 -0800447 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNC);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700448
449 bio->bi_bdev = rdev->bdev;
450 bio->bi_sector = sector;
451 bio_add_page(bio, page, size, 0);
452 bio->bi_private = rdev;
453 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800454 bio->bi_rw = rw;
455
NeilBrown7bfa19f2005-06-21 17:17:28 -0700456 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800457 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
458 struct bio *rbio;
459 rw |= (1<<BIO_RW_BARRIER);
460 rbio = bio_clone(bio, GFP_NOIO);
461 rbio->bi_private = bio;
462 rbio->bi_end_io = super_written_barrier;
463 submit_bio(rw, rbio);
464 } else
465 submit_bio(rw, bio);
466}
467
468void md_super_wait(mddev_t *mddev)
469{
470 /* wait for all superblock writes that were scheduled to complete.
471 * if any had to be retried (due to BARRIER problems), retry them
472 */
473 DEFINE_WAIT(wq);
474 for(;;) {
475 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
476 if (atomic_read(&mddev->pending_writes)==0)
477 break;
478 while (mddev->biolist) {
479 struct bio *bio;
480 spin_lock_irq(&mddev->write_lock);
481 bio = mddev->biolist;
482 mddev->biolist = bio->bi_next ;
483 bio->bi_next = NULL;
484 spin_unlock_irq(&mddev->write_lock);
485 submit_bio(bio->bi_rw, bio);
486 }
487 schedule();
488 }
489 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700490}
491
NeilBrown6712ecf2007-09-27 12:47:43 +0200492static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
NeilBrowna654b9d82005-06-21 17:17:27 -0700497int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct page *page, int rw)
499{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700500 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct completion event;
502 int ret;
503
504 rw |= (1 << BIO_RW_SYNC);
505
506 bio->bi_bdev = bdev;
507 bio->bi_sector = sector;
508 bio_add_page(bio, page, size, 0);
509 init_completion(&event);
510 bio->bi_private = &event;
511 bio->bi_end_io = bi_complete;
512 submit_bio(rw, bio);
513 wait_for_completion(&event);
514
515 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
516 bio_put(bio);
517 return ret;
518}
NeilBrowna8745db2006-01-06 00:20:34 -0800519EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
NeilBrown0002b272005-09-09 16:23:53 -0700521static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 char b[BDEVNAME_SIZE];
524 if (!rdev->sb_page) {
525 MD_BUG();
526 return -EINVAL;
527 }
528 if (rdev->sb_loaded)
529 return 0;
530
531
Andre Noll0f420352008-07-11 22:02:23 +1000532 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 goto fail;
534 rdev->sb_loaded = 1;
535 return 0;
536
537fail:
538 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
539 bdevname(rdev->bdev,b));
540 return -EINVAL;
541}
542
543static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
544{
Andre Noll05710462008-07-11 22:02:20 +1000545 return sb1->set_uuid0 == sb2->set_uuid0 &&
546 sb1->set_uuid1 == sb2->set_uuid1 &&
547 sb1->set_uuid2 == sb2->set_uuid2 &&
548 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
552{
553 int ret;
554 mdp_super_t *tmp1, *tmp2;
555
556 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
557 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
558
559 if (!tmp1 || !tmp2) {
560 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100561 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 goto abort;
563 }
564
565 *tmp1 = *sb1;
566 *tmp2 = *sb2;
567
568 /*
569 * nr_disks is not constant
570 */
571 tmp1->nr_disks = 0;
572 tmp2->nr_disks = 0;
573
Andre Nollce0c8e02008-07-11 22:02:20 +1000574 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700576 kfree(tmp1);
577 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return ret;
579}
580
NeilBrown4d167f02007-05-09 02:35:37 -0700581
582static u32 md_csum_fold(u32 csum)
583{
584 csum = (csum & 0xffff) + (csum >> 16);
585 return (csum & 0xffff) + (csum >> 16);
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588static unsigned int calc_sb_csum(mdp_super_t * sb)
589{
NeilBrown4d167f02007-05-09 02:35:37 -0700590 u64 newcsum = 0;
591 u32 *sb32 = (u32*)sb;
592 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 unsigned int disk_csum, csum;
594
595 disk_csum = sb->sb_csum;
596 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700597
598 for (i = 0; i < MD_SB_BYTES/4 ; i++)
599 newcsum += sb32[i];
600 csum = (newcsum & 0xffffffff) + (newcsum>>32);
601
602
603#ifdef CONFIG_ALPHA
604 /* This used to use csum_partial, which was wrong for several
605 * reasons including that different results are returned on
606 * different architectures. It isn't critical that we get exactly
607 * the same return value as before (we always csum_fold before
608 * testing, and that removes any differences). However as we
609 * know that csum_partial always returned a 16bit value on
610 * alphas, do a fold to maximise conformity to previous behaviour.
611 */
612 sb->sb_csum = md_csum_fold(disk_csum);
613#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700615#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return csum;
617}
618
619
620/*
621 * Handle superblock details.
622 * We want to be able to handle multiple superblock formats
623 * so we have a common interface to them all, and an array of
624 * different handlers.
625 * We rely on user-space to write the initial superblock, and support
626 * reading and updating of superblocks.
627 * Interface methods are:
628 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
629 * loads and validates a superblock on dev.
630 * if refdev != NULL, compare superblocks on both devices
631 * Return:
632 * 0 - dev has a superblock that is compatible with refdev
633 * 1 - dev has a superblock that is compatible and newer than refdev
634 * so dev should be used as the refdev in future
635 * -EINVAL superblock incompatible or invalid
636 * -othererror e.g. -EIO
637 *
638 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
639 * Verify that dev is acceptable into mddev.
640 * The first time, mddev->raid_disks will be 0, and data from
641 * dev should be merged in. Subsequent calls check that dev
642 * is new enough. Return 0 or -EINVAL
643 *
644 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
645 * Update the superblock for rdev with data in mddev
646 * This does not write to disc.
647 *
648 */
649
650struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000651 char *name;
652 struct module *owner;
653 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
654 int minor_version);
655 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
656 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
657 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
658 unsigned long long size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659};
660
661/*
662 * load_super for 0.90.0
663 */
664static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
665{
666 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
667 mdp_super_t *sb;
668 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /*
Andre Noll0f420352008-07-11 22:02:23 +1000671 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 * it's at the end of the disk.
673 *
674 * It also happens to be a multiple of 4Kb.
675 */
Andre Noll0f420352008-07-11 22:02:23 +1000676 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
NeilBrown0002b272005-09-09 16:23:53 -0700678 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (ret) return ret;
680
681 ret = -EINVAL;
682
683 bdevname(rdev->bdev, b);
684 sb = (mdp_super_t*)page_address(rdev->sb_page);
685
686 if (sb->md_magic != MD_SB_MAGIC) {
687 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
688 b);
689 goto abort;
690 }
691
692 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800693 sb->minor_version < 90 ||
694 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
696 sb->major_version, sb->minor_version,
697 b);
698 goto abort;
699 }
700
701 if (sb->raid_disks <= 0)
702 goto abort;
703
NeilBrown4d167f02007-05-09 02:35:37 -0700704 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
706 b);
707 goto abort;
708 }
709
710 rdev->preferred_minor = sb->md_minor;
711 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700712 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
NeilBrowne11e93f2007-05-09 02:35:36 -0700714 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
715 if (sb->level != 1 && sb->level != 4
716 && sb->level != 5 && sb->level != 6
717 && sb->level != 10) {
718 /* FIXME use a better test */
719 printk(KERN_WARNING
720 "md: bitmaps not supported for this level.\n");
721 goto abort;
722 }
723 }
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (sb->level == LEVEL_MULTIPATH)
726 rdev->desc_nr = -1;
727 else
728 rdev->desc_nr = sb->this_disk.number;
729
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700730 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700732 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 __u64 ev1, ev2;
734 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
735 if (!uuid_equal(refsb, sb)) {
736 printk(KERN_WARNING "md: %s has different UUID to %s\n",
737 b, bdevname(refdev->bdev,b2));
738 goto abort;
739 }
740 if (!sb_equal(refsb, sb)) {
741 printk(KERN_WARNING "md: %s has same UUID"
742 " but different superblock to %s\n",
743 b, bdevname(refdev->bdev, b2));
744 goto abort;
745 }
746 ev1 = md_event(sb);
747 ev2 = md_event(refsb);
748 if (ev1 > ev2)
749 ret = 1;
750 else
751 ret = 0;
752 }
Andre Nolle7debaa2008-07-11 22:02:23 +1000753 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
NeilBrown2bf071b2006-01-06 00:20:55 -0800755 if (rdev->size < sb->size && sb->level > 1)
756 /* "this cannot possibly happen" ... */
757 ret = -EINVAL;
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 abort:
760 return ret;
761}
762
763/*
764 * validate_super for 0.90.0
765 */
766static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
767{
768 mdp_disk_t *desc;
769 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700770 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
NeilBrown41158c72005-06-21 17:17:25 -0700772 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800773 clear_bit(Faulty, &rdev->flags);
774 clear_bit(In_sync, &rdev->flags);
775 clear_bit(WriteMostly, &rdev->flags);
776 clear_bit(BarriersNotsupp, &rdev->flags);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (mddev->raid_disks == 0) {
779 mddev->major_version = 0;
780 mddev->minor_version = sb->minor_version;
781 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800782 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 mddev->chunk_size = sb->chunk_size;
784 mddev->ctime = sb->ctime;
785 mddev->utime = sb->utime;
786 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800787 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 mddev->layout = sb->layout;
789 mddev->raid_disks = sb->raid_disks;
790 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700791 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700792 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700793 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
NeilBrownf6705572006-03-27 01:18:11 -0800795 if (mddev->minor_version >= 91) {
796 mddev->reshape_position = sb->reshape_position;
797 mddev->delta_disks = sb->delta_disks;
798 mddev->new_level = sb->new_level;
799 mddev->new_layout = sb->new_layout;
800 mddev->new_chunk = sb->new_chunk;
801 } else {
802 mddev->reshape_position = MaxSector;
803 mddev->delta_disks = 0;
804 mddev->new_level = mddev->level;
805 mddev->new_layout = mddev->layout;
806 mddev->new_chunk = mddev->chunk_size;
807 }
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (sb->state & (1<<MD_SB_CLEAN))
810 mddev->recovery_cp = MaxSector;
811 else {
812 if (sb->events_hi == sb->cp_events_hi &&
813 sb->events_lo == sb->cp_events_lo) {
814 mddev->recovery_cp = sb->recovery_cp;
815 } else
816 mddev->recovery_cp = 0;
817 }
818
819 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
820 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
821 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
822 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
823
824 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700825
826 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700827 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700828 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700829
NeilBrown41158c72005-06-21 17:17:25 -0700830 } else if (mddev->pers == NULL) {
831 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 ++ev1;
833 if (ev1 < mddev->events)
834 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700835 } else if (mddev->bitmap) {
836 /* if adding to array with a bitmap, then we can accept an
837 * older device ... but not too old.
838 */
NeilBrown41158c72005-06-21 17:17:25 -0700839 if (ev1 < mddev->bitmap->events_cleared)
840 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700841 } else {
842 if (ev1 < mddev->events)
843 /* just a hot-add of a new device, leave raid_disk at -1 */
844 return 0;
845 }
NeilBrown41158c72005-06-21 17:17:25 -0700846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 desc = sb->disks + rdev->desc_nr;
849
850 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800851 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700852 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
853 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800854 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 rdev->raid_disk = desc->raid_disk;
856 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700857 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
858 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700859 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800860 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return 0;
862}
863
864/*
865 * sync_super for 0.90.0
866 */
867static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
868{
869 mdp_super_t *sb;
870 struct list_head *tmp;
871 mdk_rdev_t *rdev2;
872 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /* make rdev->sb match mddev data..
876 *
877 * 1/ zero out disks
878 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
879 * 3/ any empty disks < next_spare become removed
880 *
881 * disks[0] gets initialised to REMOVED because
882 * we cannot be sure from other fields if it has
883 * been initialised or not.
884 */
885 int i;
886 int active=0, working=0,failed=0,spare=0,nr_disks=0;
887
NeilBrown61181562005-09-09 16:24:02 -0700888 rdev->sb_size = MD_SB_BYTES;
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 sb = (mdp_super_t*)page_address(rdev->sb_page);
891
892 memset(sb, 0, sizeof(*sb));
893
894 sb->md_magic = MD_SB_MAGIC;
895 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 sb->patch_version = mddev->patch_version;
897 sb->gvalid_words = 0; /* ignored */
898 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
899 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
900 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
901 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
902
903 sb->ctime = mddev->ctime;
904 sb->level = mddev->level;
905 sb->size = mddev->size;
906 sb->raid_disks = mddev->raid_disks;
907 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800908 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 sb->utime = mddev->utime;
910 sb->state = 0;
911 sb->events_hi = (mddev->events>>32);
912 sb->events_lo = (u32)mddev->events;
913
NeilBrownf6705572006-03-27 01:18:11 -0800914 if (mddev->reshape_position == MaxSector)
915 sb->minor_version = 90;
916 else {
917 sb->minor_version = 91;
918 sb->reshape_position = mddev->reshape_position;
919 sb->new_level = mddev->new_level;
920 sb->delta_disks = mddev->delta_disks;
921 sb->new_layout = mddev->new_layout;
922 sb->new_chunk = mddev->new_chunk;
923 }
924 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (mddev->in_sync)
926 {
927 sb->recovery_cp = mddev->recovery_cp;
928 sb->cp_events_hi = (mddev->events>>32);
929 sb->cp_events_lo = (u32)mddev->events;
930 if (mddev->recovery_cp == MaxSector)
931 sb->state = (1<< MD_SB_CLEAN);
932 } else
933 sb->recovery_cp = 0;
934
935 sb->layout = mddev->layout;
936 sb->chunk_size = mddev->chunk_size;
937
NeilBrowna654b9d82005-06-21 17:17:27 -0700938 if (mddev->bitmap && mddev->bitmap_file == NULL)
939 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 sb->disks[0].state = (1<<MD_DISK_REMOVED);
NeilBrownd089c6a2008-02-06 01:39:59 -0800942 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800944 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800945 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
946 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800947 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800949 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800950 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 d = &sb->disks[rdev2->desc_nr];
952 nr_disks++;
953 d->number = rdev2->desc_nr;
954 d->major = MAJOR(rdev2->bdev->bd_dev);
955 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800956 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
957 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 d->raid_disk = rdev2->raid_disk;
959 else
960 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800961 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800963 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 d->state = (1<<MD_DISK_ACTIVE);
965 d->state |= (1<<MD_DISK_SYNC);
966 active++;
967 working++;
968 } else {
969 d->state = 0;
970 spare++;
971 working++;
972 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700973 if (test_bit(WriteMostly, &rdev2->flags))
974 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* now set the "removed" and "faulty" bits on any missing devices */
977 for (i=0 ; i < mddev->raid_disks ; i++) {
978 mdp_disk_t *d = &sb->disks[i];
979 if (d->state == 0 && d->number == 0) {
980 d->number = i;
981 d->raid_disk = i;
982 d->state = (1<<MD_DISK_REMOVED);
983 d->state |= (1<<MD_DISK_FAULTY);
984 failed++;
985 }
986 }
987 sb->nr_disks = nr_disks;
988 sb->active_disks = active;
989 sb->working_disks = working;
990 sb->failed_disks = failed;
991 sb->spare_disks = spare;
992
993 sb->this_disk = sb->disks[rdev->desc_nr];
994 sb->sb_csum = calc_sb_csum(sb);
995}
996
997/*
Chris Webb0cd17fe2008-06-28 08:31:46 +1000998 * rdev_size_change for 0.90.0
999 */
1000static unsigned long long
1001super_90_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1002{
1003 if (size && size < rdev->mddev->size)
1004 return 0; /* component must fit device */
1005 size *= 2; /* convert to sectors */
1006 if (rdev->mddev->bitmap_offset)
1007 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001008 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
1009 if (!size || size > rdev->sb_start)
1010 size = rdev->sb_start;
1011 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001012 rdev->sb_page);
1013 md_super_wait(rdev->mddev);
1014 return size/2; /* kB for sysfs */
1015}
1016
1017
1018/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 * version 1 superblock
1020 */
1021
NeilBrown1c05b4b2006-10-21 10:24:08 -07001022static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001024 __le32 disk_csum;
1025 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 unsigned long long newcsum;
1027 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001028 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int i;
1030
1031 disk_csum = sb->sb_csum;
1032 sb->sb_csum = 0;
1033 newcsum = 0;
1034 for (i=0; size>=4; size -= 4 )
1035 newcsum += le32_to_cpu(*isuper++);
1036
1037 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001038 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1041 sb->sb_csum = disk_csum;
1042 return cpu_to_le32(csum);
1043}
1044
1045static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1046{
1047 struct mdp_superblock_1 *sb;
1048 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001049 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001051 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /*
Andre Noll0f420352008-07-11 22:02:23 +10001054 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 * It is always aligned to a 4K boundary and
1056 * depeding on minor_version, it can be:
1057 * 0: At least 8K, but less than 12K, from end of device
1058 * 1: At start of device
1059 * 2: 4K from start of device.
1060 */
1061 switch(minor_version) {
1062 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001063 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1064 sb_start -= 8*2;
1065 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 break;
1067 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001068 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 break;
1070 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001071 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 break;
1073 default:
1074 return -EINVAL;
1075 }
Andre Noll0f420352008-07-11 22:02:23 +10001076 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
NeilBrown0002b272005-09-09 16:23:53 -07001078 /* superblock is rarely larger than 1K, but it can be larger,
1079 * and it is safe to read 4k, so we do that
1080 */
1081 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (ret) return ret;
1083
1084
1085 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1086
1087 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1088 sb->major_version != cpu_to_le32(1) ||
1089 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001090 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001091 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 return -EINVAL;
1093
1094 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1095 printk("md: invalid superblock checksum on %s\n",
1096 bdevname(rdev->bdev,b));
1097 return -EINVAL;
1098 }
1099 if (le64_to_cpu(sb->data_size) < 10) {
1100 printk("md: data_size too small on %s\n",
1101 bdevname(rdev->bdev,b));
1102 return -EINVAL;
1103 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001104 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1105 if (sb->level != cpu_to_le32(1) &&
1106 sb->level != cpu_to_le32(4) &&
1107 sb->level != cpu_to_le32(5) &&
1108 sb->level != cpu_to_le32(6) &&
1109 sb->level != cpu_to_le32(10)) {
1110 printk(KERN_WARNING
1111 "md: bitmaps not supported for this level.\n");
1112 return -EINVAL;
1113 }
1114 }
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 rdev->preferred_minor = 0xffff;
1117 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001118 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
NeilBrown0002b272005-09-09 16:23:53 -07001120 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001121 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001122 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001123 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1124
1125 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001126 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001127 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001128
NeilBrown31b65a02006-07-10 04:44:14 -07001129 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1130 rdev->desc_nr = -1;
1131 else
1132 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1133
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001134 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001135 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001136 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 __u64 ev1, ev2;
1138 struct mdp_superblock_1 *refsb =
1139 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1140
1141 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1142 sb->level != refsb->level ||
1143 sb->layout != refsb->layout ||
1144 sb->chunksize != refsb->chunksize) {
1145 printk(KERN_WARNING "md: %s has strangely different"
1146 " superblock to %s\n",
1147 bdevname(rdev->bdev,b),
1148 bdevname(refdev->bdev,b2));
1149 return -EINVAL;
1150 }
1151 ev1 = le64_to_cpu(sb->events);
1152 ev2 = le64_to_cpu(refsb->events);
1153
1154 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001155 ret = 1;
1156 else
1157 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
NeilBrowna1801f82008-03-04 14:29:31 -08001159 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1161 else
Andre Noll0f420352008-07-11 22:02:23 +10001162 rdev->size = rdev->sb_start / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1164 return -EINVAL;
1165 rdev->size = le64_to_cpu(sb->data_size)/2;
1166 if (le32_to_cpu(sb->chunksize))
1167 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001168
NeilBrown1c05b4b2006-10-21 10:24:08 -07001169 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001170 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001171 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1175{
1176 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001177 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
NeilBrown41158c72005-06-21 17:17:25 -07001179 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001180 clear_bit(Faulty, &rdev->flags);
1181 clear_bit(In_sync, &rdev->flags);
1182 clear_bit(WriteMostly, &rdev->flags);
1183 clear_bit(BarriersNotsupp, &rdev->flags);
1184
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (mddev->raid_disks == 0) {
1186 mddev->major_version = 1;
1187 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001188 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1190 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1191 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1192 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001193 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 mddev->layout = le32_to_cpu(sb->layout);
1195 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1196 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001197 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001198 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001199 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1202 memcpy(mddev->uuid, sb->set_uuid, 16);
1203
1204 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001205
NeilBrown71c08052005-09-09 16:23:51 -07001206 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001207 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001208 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001209
NeilBrownf6705572006-03-27 01:18:11 -08001210 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1211 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1212 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1213 mddev->new_level = le32_to_cpu(sb->new_level);
1214 mddev->new_layout = le32_to_cpu(sb->new_layout);
1215 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1216 } else {
1217 mddev->reshape_position = MaxSector;
1218 mddev->delta_disks = 0;
1219 mddev->new_level = mddev->level;
1220 mddev->new_layout = mddev->layout;
1221 mddev->new_chunk = mddev->chunk_size;
1222 }
1223
NeilBrown41158c72005-06-21 17:17:25 -07001224 } else if (mddev->pers == NULL) {
1225 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 ++ev1;
1227 if (ev1 < mddev->events)
1228 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001229 } else if (mddev->bitmap) {
1230 /* If adding to array with a bitmap, then we can accept an
1231 * older device, but not too old.
1232 */
NeilBrown41158c72005-06-21 17:17:25 -07001233 if (ev1 < mddev->bitmap->events_cleared)
1234 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001235 } else {
1236 if (ev1 < mddev->events)
1237 /* just a hot-add of a new device, leave raid_disk at -1 */
1238 return 0;
1239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (mddev->level != LEVEL_MULTIPATH) {
1241 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1243 switch(role) {
1244 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 break;
1246 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001247 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
1249 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001250 if ((le32_to_cpu(sb->feature_map) &
1251 MD_FEATURE_RECOVERY_OFFSET))
1252 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1253 else
1254 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 rdev->raid_disk = role;
1256 break;
1257 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001258 if (sb->devflags & WriteMostly1)
1259 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001260 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001261 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return 0;
1264}
1265
1266static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1267{
1268 struct mdp_superblock_1 *sb;
1269 struct list_head *tmp;
1270 mdk_rdev_t *rdev2;
1271 int max_dev, i;
1272 /* make rdev->sb match mddev and rdev data. */
1273
1274 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1275
1276 sb->feature_map = 0;
1277 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001278 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 memset(sb->pad1, 0, sizeof(sb->pad1));
1280 memset(sb->pad2, 0, sizeof(sb->pad2));
1281 memset(sb->pad3, 0, sizeof(sb->pad3));
1282
1283 sb->utime = cpu_to_le64((__u64)mddev->utime);
1284 sb->events = cpu_to_le64(mddev->events);
1285 if (mddev->in_sync)
1286 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1287 else
1288 sb->resync_offset = cpu_to_le64(0);
1289
NeilBrown1c05b4b2006-10-21 10:24:08 -07001290 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001291
NeilBrownf0ca3402006-02-02 14:28:04 -08001292 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001293 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001294
NeilBrowna654b9d82005-06-21 17:17:27 -07001295 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1296 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001297 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001298 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001299
1300 if (rdev->raid_disk >= 0 &&
1301 !test_bit(In_sync, &rdev->flags) &&
1302 rdev->recovery_offset > 0) {
1303 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1304 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1305 }
1306
NeilBrownf6705572006-03-27 01:18:11 -08001307 if (mddev->reshape_position != MaxSector) {
1308 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1309 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1310 sb->new_layout = cpu_to_le32(mddev->new_layout);
1311 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1312 sb->new_level = cpu_to_le32(mddev->new_level);
1313 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1314 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 max_dev = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08001317 rdev_for_each(rdev2, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (rdev2->desc_nr+1 > max_dev)
1319 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001320
1321 if (max_dev > le32_to_cpu(sb->max_dev))
1322 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 for (i=0; i<max_dev;i++)
1324 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1325
NeilBrownd089c6a2008-02-06 01:39:59 -08001326 rdev_for_each(rdev2, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001328 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001330 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001332 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1333 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 else
1335 sb->dev_roles[i] = cpu_to_le16(0xffff);
1336 }
1337
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 sb->sb_csum = calc_sb_1_csum(sb);
1339}
1340
Chris Webb0cd17fe2008-06-28 08:31:46 +10001341static unsigned long long
1342super_1_rdev_size_change(mdk_rdev_t *rdev, unsigned long long size)
1343{
1344 struct mdp_superblock_1 *sb;
1345 unsigned long long max_size;
1346 if (size && size < rdev->mddev->size)
1347 return 0; /* component must fit device */
1348 size *= 2; /* convert to sectors */
Andre Noll0f420352008-07-11 22:02:23 +10001349 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001350 /* minor versions 1 and 2; superblock before data */
1351 max_size = (rdev->bdev->bd_inode->i_size >> 9);
1352 max_size -= rdev->data_offset;
1353 if (!size || size > max_size)
1354 size = max_size;
1355 } else if (rdev->mddev->bitmap_offset) {
1356 /* minor version 0 with bitmap we can't move */
1357 return 0;
1358 } else {
1359 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001360 sector_t sb_start;
1361 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1362 sb_start &= ~(sector_t)(4*2 - 1);
1363 max_size = rdev->size*2 + sb_start - rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001364 if (!size || size > max_size)
1365 size = max_size;
Andre Noll0f420352008-07-11 22:02:23 +10001366 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001367 }
1368 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
1369 sb->data_size = cpu_to_le64(size);
Andre Noll0f420352008-07-11 22:02:23 +10001370 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001371 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001372 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001373 rdev->sb_page);
1374 md_super_wait(rdev->mddev);
1375 return size/2; /* kB for sysfs */
1376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Adrian Bunk75c96f82005-05-05 16:16:09 -07001378static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 [0] = {
1380 .name = "0.90.0",
1381 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001382 .load_super = super_90_load,
1383 .validate_super = super_90_validate,
1384 .sync_super = super_90_sync,
1385 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 },
1387 [1] = {
1388 .name = "md-1",
1389 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001390 .load_super = super_1_load,
1391 .validate_super = super_1_validate,
1392 .sync_super = super_1_sync,
1393 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 },
1395};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1398{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001399 struct list_head *tmp, *tmp2;
1400 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
NeilBrownd089c6a2008-02-06 01:39:59 -08001402 rdev_for_each(rdev, tmp, mddev1)
1403 rdev_for_each(rdev2, tmp2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001404 if (rdev->bdev->bd_contains ==
1405 rdev2->bdev->bd_contains)
1406 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 return 0;
1409}
1410
1411static LIST_HEAD(pending_raid_disks);
1412
1413static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1414{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001415 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001416 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001417 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001418 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 if (rdev->mddev) {
1421 MD_BUG();
1422 return -EINVAL;
1423 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001424
1425 /* prevent duplicates */
1426 if (find_rdev(mddev, rdev->bdev->bd_dev))
1427 return -EEXIST;
1428
NeilBrown2bf071b2006-01-06 00:20:55 -08001429 /* make sure rdev->size exceeds mddev->size */
1430 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001431 if (mddev->pers) {
1432 /* Cannot change size, so fail
1433 * If mddev->level <= 0, then we don't care
1434 * about aligning sizes (e.g. linear)
1435 */
1436 if (mddev->level > 0)
1437 return -ENOSPC;
1438 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001439 mddev->size = rdev->size;
1440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /* Verify rdev->desc_nr is unique.
1443 * If it is -1, assign a free number, else
1444 * check number is not in use
1445 */
1446 if (rdev->desc_nr < 0) {
1447 int choice = 0;
1448 if (mddev->pers) choice = mddev->raid_disks;
1449 while (find_rdev_nr(mddev, choice))
1450 choice++;
1451 rdev->desc_nr = choice;
1452 } else {
1453 if (find_rdev_nr(mddev, rdev->desc_nr))
1454 return -EBUSY;
1455 }
NeilBrown19133a42005-11-08 21:39:35 -08001456 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001457 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001458 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001461 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001462
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001463 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001464 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001465
NeilBrownf637b9f2005-11-08 21:39:37 -08001466 if (rdev->bdev->bd_part)
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001467 ko = &rdev->bdev->bd_part->dev.kobj;
NeilBrownf637b9f2005-11-08 21:39:37 -08001468 else
Kay Sieversedfaa7c2007-05-21 22:08:01 +02001469 ko = &rdev->bdev->bd_disk->dev.kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001470 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1471 kobject_del(&rdev->kobj);
1472 goto fail;
1473 }
1474 list_add(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001475 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001477
1478 fail:
1479 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1480 b, mdname(mddev));
1481 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
NeilBrown177a99b2008-02-06 01:39:56 -08001484static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001485{
1486 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1487 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001488 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001489}
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1492{
1493 char b[BDEVNAME_SIZE];
1494 if (!rdev->mddev) {
1495 MD_BUG();
1496 return;
1497 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001498 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 list_del_init(&rdev->same_set);
1500 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1501 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001502 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown5792a282007-04-04 19:08:18 -07001503
1504 /* We need to delay this, otherwise we can deadlock when
1505 * writing to 'remove' to "dev/state"
1506 */
NeilBrown177a99b2008-02-06 01:39:56 -08001507 INIT_WORK(&rdev->del_work, md_delayed_delete);
1508 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001509 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512/*
1513 * prevent the device from being mounted, repartitioned or
1514 * otherwise reused by a RAID array (or any other kernel
1515 * subsystem), by bd_claiming the device.
1516 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001517static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 int err = 0;
1520 struct block_device *bdev;
1521 char b[BDEVNAME_SIZE];
1522
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001523 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (IS_ERR(bdev)) {
1525 printk(KERN_ERR "md: could not open %s.\n",
1526 __bdevname(dev, b));
1527 return PTR_ERR(bdev);
1528 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001529 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 if (err) {
1531 printk(KERN_ERR "md: could not bd_claim %s.\n",
1532 bdevname(bdev, b));
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001533 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return err;
1535 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001536 if (!shared)
1537 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 rdev->bdev = bdev;
1539 return err;
1540}
1541
1542static void unlock_rdev(mdk_rdev_t *rdev)
1543{
1544 struct block_device *bdev = rdev->bdev;
1545 rdev->bdev = NULL;
1546 if (!bdev)
1547 MD_BUG();
1548 bd_release(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001549 blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
1551
1552void md_autodetect_dev(dev_t dev);
1553
1554static void export_rdev(mdk_rdev_t * rdev)
1555{
1556 char b[BDEVNAME_SIZE];
1557 printk(KERN_INFO "md: export_rdev(%s)\n",
1558 bdevname(rdev->bdev,b));
1559 if (rdev->mddev)
1560 MD_BUG();
1561 free_disk_sb(rdev);
1562 list_del_init(&rdev->same_set);
1563#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001564 if (test_bit(AutoDetected, &rdev->flags))
1565 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566#endif
1567 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001568 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571static void kick_rdev_from_array(mdk_rdev_t * rdev)
1572{
1573 unbind_rdev_from_array(rdev);
1574 export_rdev(rdev);
1575}
1576
1577static void export_array(mddev_t *mddev)
1578{
1579 struct list_head *tmp;
1580 mdk_rdev_t *rdev;
1581
NeilBrownd089c6a2008-02-06 01:39:59 -08001582 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if (!rdev->mddev) {
1584 MD_BUG();
1585 continue;
1586 }
1587 kick_rdev_from_array(rdev);
1588 }
1589 if (!list_empty(&mddev->disks))
1590 MD_BUG();
1591 mddev->raid_disks = 0;
1592 mddev->major_version = 0;
1593}
1594
1595static void print_desc(mdp_disk_t *desc)
1596{
1597 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1598 desc->major,desc->minor,desc->raid_disk,desc->state);
1599}
1600
1601static void print_sb(mdp_super_t *sb)
1602{
1603 int i;
1604
1605 printk(KERN_INFO
1606 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1607 sb->major_version, sb->minor_version, sb->patch_version,
1608 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1609 sb->ctime);
1610 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1611 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1612 sb->md_minor, sb->layout, sb->chunk_size);
1613 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1614 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1615 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1616 sb->failed_disks, sb->spare_disks,
1617 sb->sb_csum, (unsigned long)sb->events_lo);
1618
1619 printk(KERN_INFO);
1620 for (i = 0; i < MD_SB_DISKS; i++) {
1621 mdp_disk_t *desc;
1622
1623 desc = sb->disks + i;
1624 if (desc->number || desc->major || desc->minor ||
1625 desc->raid_disk || (desc->state && (desc->state != 4))) {
1626 printk(" D %2d: ", i);
1627 print_desc(desc);
1628 }
1629 }
1630 printk(KERN_INFO "md: THIS: ");
1631 print_desc(&sb->this_disk);
1632
1633}
1634
1635static void print_rdev(mdk_rdev_t *rdev)
1636{
1637 char b[BDEVNAME_SIZE];
1638 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1639 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001640 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1641 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (rdev->sb_loaded) {
1643 printk(KERN_INFO "md: rdev superblock:\n");
1644 print_sb((mdp_super_t*)page_address(rdev->sb_page));
1645 } else
1646 printk(KERN_INFO "md: no rdev superblock!\n");
1647}
1648
Adrian Bunk5e563412006-06-26 00:27:42 -07001649static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct list_head *tmp, *tmp2;
1652 mdk_rdev_t *rdev;
1653 mddev_t *mddev;
1654 char b[BDEVNAME_SIZE];
1655
1656 printk("\n");
1657 printk("md: **********************************\n");
1658 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1659 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001660 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
NeilBrown32a76272005-06-21 17:17:14 -07001662 if (mddev->bitmap)
1663 bitmap_print_sb(mddev->bitmap);
1664 else
1665 printk("%s: ", mdname(mddev));
NeilBrownd089c6a2008-02-06 01:39:59 -08001666 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 printk("<%s>", bdevname(rdev->bdev,b));
1668 printk("\n");
1669
NeilBrownd089c6a2008-02-06 01:39:59 -08001670 rdev_for_each(rdev, tmp2, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 print_rdev(rdev);
1672 }
1673 printk("md: **********************************\n");
1674 printk("\n");
1675}
1676
1677
NeilBrown42543762006-06-26 00:27:57 -07001678static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
NeilBrown42543762006-06-26 00:27:57 -07001680 /* Update each superblock (in-memory image), but
1681 * if we are allowed to, skip spares which already
1682 * have the right event counter, or have one earlier
1683 * (which would mean they aren't being marked as dirty
1684 * with the rest of the array)
1685 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 mdk_rdev_t *rdev;
1687 struct list_head *tmp;
1688
NeilBrownd089c6a2008-02-06 01:39:59 -08001689 rdev_for_each(rdev, tmp, mddev) {
NeilBrown42543762006-06-26 00:27:57 -07001690 if (rdev->sb_events == mddev->events ||
1691 (nospares &&
1692 rdev->raid_disk < 0 &&
1693 (rdev->sb_events&1)==0 &&
1694 rdev->sb_events+1 == mddev->events)) {
1695 /* Don't update this superblock */
1696 rdev->sb_loaded = 2;
1697 } else {
1698 super_types[mddev->major_version].
1699 sync_super(mddev, rdev);
1700 rdev->sb_loaded = 1;
1701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703}
1704
NeilBrown850b2b42006-10-03 01:15:46 -07001705static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 struct list_head *tmp;
1708 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001709 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001710 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
NeilBrown8377bc82008-04-30 00:52:28 -07001712 if (mddev->external)
1713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001715 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001716
NeilBrown850b2b42006-10-03 01:15:46 -07001717 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1718 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1719 force_change = 1;
1720 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1721 /* just a clean<-> dirty transition, possibly leave spares alone,
1722 * though if events isn't the right even/odd, we will have to do
1723 * spares after all
1724 */
1725 nospares = 1;
1726 if (force_change)
1727 nospares = 0;
1728 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001729 /* If the array is degraded, then skipping spares is both
1730 * dangerous and fairly pointless.
1731 * Dangerous because a device that was removed from the array
1732 * might have a event_count that still looks up-to-date,
1733 * so it can be re-added without a resync.
1734 * Pointless because if there are any spares to skip,
1735 * then a recovery will happen and soon that array won't
1736 * be degraded any more and the spare can go back to sleep then.
1737 */
NeilBrown850b2b42006-10-03 01:15:46 -07001738 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001739
NeilBrown06d91a52005-06-21 17:17:12 -07001740 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001742
1743 /* If this is just a dirty<->clean transition, and the array is clean
1744 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001745 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001746 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001747 && (mddev->events & 1)
1748 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001749 mddev->events--;
1750 else {
1751 /* otherwise we have to go forward and ... */
1752 mddev->events ++;
1753 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1754 /* .. if the array isn't clean, insist on an odd 'events' */
1755 if ((mddev->events&1)==0) {
1756 mddev->events++;
1757 nospares = 0;
1758 }
1759 } else {
1760 /* otherwise insist on an even 'events' (for clean states) */
1761 if ((mddev->events&1)) {
1762 mddev->events++;
1763 nospares = 0;
1764 }
1765 }
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 if (!mddev->events) {
1769 /*
1770 * oops, this 64-bit counter should never wrap.
1771 * Either we are in around ~1 trillion A.C., assuming
1772 * 1 reboot per second, or we have a bug:
1773 */
1774 MD_BUG();
1775 mddev->events --;
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 /*
1779 * do not write anything to disk if using
1780 * nonpersistent superblocks
1781 */
NeilBrown06d91a52005-06-21 17:17:12 -07001782 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001783 if (!mddev->external)
1784 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1785
NeilBrowna9701a32005-11-08 21:39:34 -08001786 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001787 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001789 }
NeilBrowne6910632008-02-06 01:39:51 -08001790 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001791 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 dprintk(KERN_INFO
1794 "md: updating %s RAID superblock on device (in sync %d)\n",
1795 mdname(mddev),mddev->in_sync);
1796
NeilBrown4ad13662007-07-17 04:06:13 -07001797 bitmap_update_sb(mddev->bitmap);
NeilBrownd089c6a2008-02-06 01:39:59 -08001798 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 char b[BDEVNAME_SIZE];
1800 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001801 if (rdev->sb_loaded != 1)
1802 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001803 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 dprintk("(skipping faulty ");
1805
1806 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001807 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001808 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001809 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001810 rdev->sb_page);
1811 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1812 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10001813 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07001814 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 } else
1817 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001818 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /* only need to write one superblock... */
1820 break;
1821 }
NeilBrowna9701a32005-11-08 21:39:34 -08001822 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001823 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001824
NeilBrowna9701a32005-11-08 21:39:34 -08001825 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001826 if (mddev->in_sync != sync_req ||
1827 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001828 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001829 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001830 goto repeat;
1831 }
NeilBrown850b2b42006-10-03 01:15:46 -07001832 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001833 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001834 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
Andre Noll7f6ce762008-03-23 18:34:54 +01001838/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08001839 * We want to accept with case. For this we use cmd_match.
1840 */
1841static int cmd_match(const char *cmd, const char *str)
1842{
1843 /* See if cmd, written into a sysfs file, matches
1844 * str. They must either be the same, or cmd can
1845 * have a trailing newline
1846 */
1847 while (*cmd && *str && *cmd == *str) {
1848 cmd++;
1849 str++;
1850 }
1851 if (*cmd == '\n')
1852 cmd++;
1853 if (*str || *cmd)
1854 return 0;
1855 return 1;
1856}
1857
NeilBrown86e6ffd2005-11-08 21:39:24 -08001858struct rdev_sysfs_entry {
1859 struct attribute attr;
1860 ssize_t (*show)(mdk_rdev_t *, char *);
1861 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1862};
1863
1864static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001865state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001866{
1867 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001868 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001869
NeilBrownb2d444d2005-11-08 21:39:31 -08001870 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001871 len+= sprintf(page+len, "%sfaulty",sep);
1872 sep = ",";
1873 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001874 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001875 len += sprintf(page+len, "%sin_sync",sep);
1876 sep = ",";
1877 }
NeilBrownf6556752006-06-26 00:28:01 -07001878 if (test_bit(WriteMostly, &rdev->flags)) {
1879 len += sprintf(page+len, "%swrite_mostly",sep);
1880 sep = ",";
1881 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07001882 if (test_bit(Blocked, &rdev->flags)) {
1883 len += sprintf(page+len, "%sblocked", sep);
1884 sep = ",";
1885 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001886 if (!test_bit(Faulty, &rdev->flags) &&
1887 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001888 len += sprintf(page+len, "%sspare", sep);
1889 sep = ",";
1890 }
1891 return len+sprintf(page+len, "\n");
1892}
1893
NeilBrown45dc2de2006-06-26 00:27:58 -07001894static ssize_t
1895state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1896{
1897 /* can write
1898 * faulty - simulates and error
1899 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07001900 * writemostly - sets write_mostly
1901 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07001902 * blocked - sets the Blocked flag
1903 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07001904 */
1905 int err = -EINVAL;
1906 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
1907 md_error(rdev->mddev, rdev);
1908 err = 0;
1909 } else if (cmd_match(buf, "remove")) {
1910 if (rdev->raid_disk >= 0)
1911 err = -EBUSY;
1912 else {
1913 mddev_t *mddev = rdev->mddev;
1914 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08001915 if (mddev->pers)
1916 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07001917 md_new_event(mddev);
1918 err = 0;
1919 }
NeilBrownf6556752006-06-26 00:28:01 -07001920 } else if (cmd_match(buf, "writemostly")) {
1921 set_bit(WriteMostly, &rdev->flags);
1922 err = 0;
1923 } else if (cmd_match(buf, "-writemostly")) {
1924 clear_bit(WriteMostly, &rdev->flags);
1925 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07001926 } else if (cmd_match(buf, "blocked")) {
1927 set_bit(Blocked, &rdev->flags);
1928 err = 0;
1929 } else if (cmd_match(buf, "-blocked")) {
1930 clear_bit(Blocked, &rdev->flags);
1931 wake_up(&rdev->blocked_wait);
1932 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
1933 md_wakeup_thread(rdev->mddev->thread);
1934
1935 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07001936 }
Neil Brown52664732008-06-28 08:31:44 +10001937 if (!err)
1938 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrown45dc2de2006-06-26 00:27:58 -07001939 return err ? err : len;
1940}
NeilBrown80ca3a42006-07-10 04:44:18 -07001941static struct rdev_sysfs_entry rdev_state =
1942__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001943
1944static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08001945errors_show(mdk_rdev_t *rdev, char *page)
1946{
1947 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
1948}
1949
1950static ssize_t
1951errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1952{
1953 char *e;
1954 unsigned long n = simple_strtoul(buf, &e, 10);
1955 if (*buf && (*e == 0 || *e == '\n')) {
1956 atomic_set(&rdev->corrected_errors, n);
1957 return len;
1958 }
1959 return -EINVAL;
1960}
1961static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07001962__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001963
NeilBrown014236d2006-01-06 00:20:55 -08001964static ssize_t
1965slot_show(mdk_rdev_t *rdev, char *page)
1966{
1967 if (rdev->raid_disk < 0)
1968 return sprintf(page, "none\n");
1969 else
1970 return sprintf(page, "%d\n", rdev->raid_disk);
1971}
1972
1973static ssize_t
1974slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
1975{
1976 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08001977 int err;
1978 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08001979 int slot = simple_strtoul(buf, &e, 10);
1980 if (strncmp(buf, "none", 4)==0)
1981 slot = -1;
1982 else if (e==buf || (*e && *e!= '\n'))
1983 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10001984 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08001985 /* Setting 'slot' on an active array requires also
1986 * updating the 'rd%d' link, and communicating
1987 * with the personality with ->hot_*_disk.
1988 * For now we only support removing
1989 * failed/spare devices. This normally happens automatically,
1990 * but not when the metadata is externally managed.
1991 */
NeilBrownc303da62008-02-06 01:39:51 -08001992 if (rdev->raid_disk == -1)
1993 return -EEXIST;
1994 /* personality does all needed checks */
1995 if (rdev->mddev->pers->hot_add_disk == NULL)
1996 return -EINVAL;
1997 err = rdev->mddev->pers->
1998 hot_remove_disk(rdev->mddev, rdev->raid_disk);
1999 if (err)
2000 return err;
2001 sprintf(nm, "rd%d", rdev->raid_disk);
2002 sysfs_remove_link(&rdev->mddev->kobj, nm);
2003 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2004 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002005 } else if (rdev->mddev->pers) {
2006 mdk_rdev_t *rdev2;
2007 struct list_head *tmp;
2008 /* Activating a spare .. or possibly reactivating
2009 * if we every get bitmaps working here.
2010 */
2011
2012 if (rdev->raid_disk != -1)
2013 return -EBUSY;
2014
2015 if (rdev->mddev->pers->hot_add_disk == NULL)
2016 return -EINVAL;
2017
2018 rdev_for_each(rdev2, tmp, rdev->mddev)
2019 if (rdev2->raid_disk == slot)
2020 return -EEXIST;
2021
2022 rdev->raid_disk = slot;
2023 if (test_bit(In_sync, &rdev->flags))
2024 rdev->saved_raid_disk = slot;
2025 else
2026 rdev->saved_raid_disk = -1;
2027 err = rdev->mddev->pers->
2028 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002029 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002030 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002031 return err;
Neil Brown52664732008-06-28 08:31:44 +10002032 } else
2033 sysfs_notify(&rdev->kobj, NULL, "state");
Neil Brown6c2fce22008-06-28 08:31:31 +10002034 sprintf(nm, "rd%d", rdev->raid_disk);
2035 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2036 printk(KERN_WARNING
2037 "md: cannot register "
2038 "%s for %s\n",
2039 nm, mdname(rdev->mddev));
2040
2041 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002042 } else {
2043 if (slot >= rdev->mddev->raid_disks)
2044 return -ENOSPC;
2045 rdev->raid_disk = slot;
2046 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002047 clear_bit(Faulty, &rdev->flags);
2048 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002049 set_bit(In_sync, &rdev->flags);
Neil Brown52664732008-06-28 08:31:44 +10002050 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc303da62008-02-06 01:39:51 -08002051 }
NeilBrown014236d2006-01-06 00:20:55 -08002052 return len;
2053}
2054
2055
2056static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002057__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002058
NeilBrown93c8cad2006-01-06 00:20:56 -08002059static ssize_t
2060offset_show(mdk_rdev_t *rdev, char *page)
2061{
Andrew Morton6961ece2006-01-06 00:20:59 -08002062 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002063}
2064
2065static ssize_t
2066offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2067{
2068 char *e;
2069 unsigned long long offset = simple_strtoull(buf, &e, 10);
2070 if (e==buf || (*e && *e != '\n'))
2071 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002072 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002073 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002074 if (rdev->size && rdev->mddev->external)
2075 /* Must set offset before size, so overlap checks
2076 * can be sane */
2077 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002078 rdev->data_offset = offset;
2079 return len;
2080}
2081
2082static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002083__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002084
NeilBrown83303b62006-01-06 00:21:06 -08002085static ssize_t
2086rdev_size_show(mdk_rdev_t *rdev, char *page)
2087{
2088 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2089}
2090
NeilBrownc5d79ad2008-02-06 01:39:54 -08002091static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2092{
2093 /* check if two start/length pairs overlap */
2094 if (s1+l1 <= s2)
2095 return 0;
2096 if (s2+l2 <= s1)
2097 return 0;
2098 return 1;
2099}
2100
NeilBrown83303b62006-01-06 00:21:06 -08002101static ssize_t
2102rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2103{
Neil Brownd7027452008-07-12 10:37:50 +10002104 unsigned long long size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002105 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002106 mddev_t *my_mddev = rdev->mddev;
2107
Neil Brownd7027452008-07-12 10:37:50 +10002108 if (strict_strtoull(buf, 10, &size) < 0)
2109 return -EINVAL;
2110 if (size < my_mddev->size)
NeilBrown83303b62006-01-06 00:21:06 -08002111 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002112 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002113 if (my_mddev->persistent) {
2114 size = super_types[my_mddev->major_version].
Chris Webb0cd17fe2008-06-28 08:31:46 +10002115 rdev_size_change(rdev, size);
2116 if (!size)
2117 return -EBUSY;
2118 } else if (!size) {
2119 size = (rdev->bdev->bd_inode->i_size >> 10);
2120 size -= rdev->data_offset/2;
2121 }
Neil Brownd7027452008-07-12 10:37:50 +10002122 if (size < my_mddev->size)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002123 return -EINVAL; /* component must fit device */
2124 }
2125
NeilBrown83303b62006-01-06 00:21:06 -08002126 rdev->size = size;
Neil Brownd7027452008-07-12 10:37:50 +10002127 if (size > oldsize && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002128 /* need to check that all other rdevs with the same ->bdev
2129 * do not overlap. We need to unlock the mddev to avoid
2130 * a deadlock. We have already changed rdev->size, and if
2131 * we have to change it back, we will have the lock again.
2132 */
2133 mddev_t *mddev;
2134 int overlap = 0;
2135 struct list_head *tmp, *tmp2;
2136
NeilBrown27c529b2008-03-04 14:29:33 -08002137 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002138 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002139 mdk_rdev_t *rdev2;
2140
2141 mddev_lock(mddev);
NeilBrownd089c6a2008-02-06 01:39:59 -08002142 rdev_for_each(rdev2, tmp2, mddev)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002143 if (test_bit(AllReserved, &rdev2->flags) ||
2144 (rdev->bdev == rdev2->bdev &&
2145 rdev != rdev2 &&
2146 overlaps(rdev->data_offset, rdev->size,
2147 rdev2->data_offset, rdev2->size))) {
2148 overlap = 1;
2149 break;
2150 }
2151 mddev_unlock(mddev);
2152 if (overlap) {
2153 mddev_put(mddev);
2154 break;
2155 }
2156 }
NeilBrown27c529b2008-03-04 14:29:33 -08002157 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002158 if (overlap) {
2159 /* Someone else could have slipped in a size
2160 * change here, but doing so is just silly.
2161 * We put oldsize back because we *know* it is
2162 * safe, and trust userspace not to race with
2163 * itself
2164 */
2165 rdev->size = oldsize;
2166 return -EBUSY;
2167 }
2168 }
NeilBrown83303b62006-01-06 00:21:06 -08002169 return len;
2170}
2171
2172static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002173__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002174
NeilBrown86e6ffd2005-11-08 21:39:24 -08002175static struct attribute *rdev_default_attrs[] = {
2176 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002177 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002178 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002179 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002180 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002181 NULL,
2182};
2183static ssize_t
2184rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2185{
2186 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2187 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002188 mddev_t *mddev = rdev->mddev;
2189 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002190
2191 if (!entry->show)
2192 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002193
2194 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2195 if (!rv) {
2196 if (rdev->mddev == NULL)
2197 rv = -EBUSY;
2198 else
2199 rv = entry->show(rdev, page);
2200 mddev_unlock(mddev);
2201 }
2202 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002203}
2204
2205static ssize_t
2206rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2207 const char *page, size_t length)
2208{
2209 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2210 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002211 ssize_t rv;
2212 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002213
2214 if (!entry->store)
2215 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002216 if (!capable(CAP_SYS_ADMIN))
2217 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002218 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002219 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002220 if (rdev->mddev == NULL)
2221 rv = -EBUSY;
2222 else
2223 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002224 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002225 }
2226 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002227}
2228
2229static void rdev_free(struct kobject *ko)
2230{
2231 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2232 kfree(rdev);
2233}
2234static struct sysfs_ops rdev_sysfs_ops = {
2235 .show = rdev_attr_show,
2236 .store = rdev_attr_store,
2237};
2238static struct kobj_type rdev_ktype = {
2239 .release = rdev_free,
2240 .sysfs_ops = &rdev_sysfs_ops,
2241 .default_attrs = rdev_default_attrs,
2242};
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244/*
2245 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2246 *
2247 * mark the device faulty if:
2248 *
2249 * - the device is nonexistent (zero size)
2250 * - the device has no valid superblock
2251 *
2252 * a faulty rdev _never_ has rdev->sb set.
2253 */
2254static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2255{
2256 char b[BDEVNAME_SIZE];
2257 int err;
2258 mdk_rdev_t *rdev;
2259 sector_t size;
2260
NeilBrown9ffae0c2006-01-06 00:20:32 -08002261 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (!rdev) {
2263 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2264 return ERR_PTR(-ENOMEM);
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 if ((err = alloc_disk_sb(rdev)))
2268 goto abort_free;
2269
NeilBrownc5d79ad2008-02-06 01:39:54 -08002270 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (err)
2272 goto abort_free;
2273
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002274 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002277 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002278 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002279 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002281 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002283 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002284 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2287 if (!size) {
2288 printk(KERN_WARNING
2289 "md: %s has zero or unknown size, marking faulty!\n",
2290 bdevname(rdev->bdev,b));
2291 err = -EINVAL;
2292 goto abort_free;
2293 }
2294
2295 if (super_format >= 0) {
2296 err = super_types[super_format].
2297 load_super(rdev, NULL, super_minor);
2298 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002299 printk(KERN_WARNING
2300 "md: %s does not have a valid v%d.%d "
2301 "superblock, not importing!\n",
2302 bdevname(rdev->bdev,b),
2303 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto abort_free;
2305 }
2306 if (err < 0) {
2307 printk(KERN_WARNING
2308 "md: could not read %s's sb, not importing!\n",
2309 bdevname(rdev->bdev,b));
2310 goto abort_free;
2311 }
2312 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002315 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 return rdev;
2318
2319abort_free:
2320 if (rdev->sb_page) {
2321 if (rdev->bdev)
2322 unlock_rdev(rdev);
2323 free_disk_sb(rdev);
2324 }
2325 kfree(rdev);
2326 return ERR_PTR(err);
2327}
2328
2329/*
2330 * Check a full RAID array for plausibility
2331 */
2332
2333
NeilBrowna757e642005-04-16 15:26:42 -07002334static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
2336 int i;
2337 struct list_head *tmp;
2338 mdk_rdev_t *rdev, *freshest;
2339 char b[BDEVNAME_SIZE];
2340
2341 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002342 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 switch (super_types[mddev->major_version].
2344 load_super(rdev, freshest, mddev->minor_version)) {
2345 case 1:
2346 freshest = rdev;
2347 break;
2348 case 0:
2349 break;
2350 default:
2351 printk( KERN_ERR \
2352 "md: fatal superblock inconsistency in %s"
2353 " -- removing from array\n",
2354 bdevname(rdev->bdev,b));
2355 kick_rdev_from_array(rdev);
2356 }
2357
2358
2359 super_types[mddev->major_version].
2360 validate_super(mddev, freshest);
2361
2362 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002363 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (rdev != freshest)
2365 if (super_types[mddev->major_version].
2366 validate_super(mddev, rdev)) {
2367 printk(KERN_WARNING "md: kicking non-fresh %s"
2368 " from array!\n",
2369 bdevname(rdev->bdev,b));
2370 kick_rdev_from_array(rdev);
2371 continue;
2372 }
2373 if (mddev->level == LEVEL_MULTIPATH) {
2374 rdev->desc_nr = i++;
2375 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002376 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002377 } else if (rdev->raid_disk >= mddev->raid_disks) {
2378 rdev->raid_disk = -1;
2379 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 }
2381 }
2382
2383
2384
2385 if (mddev->recovery_cp != MaxSector &&
2386 mddev->level >= 1)
2387 printk(KERN_ERR "md: %s: raid array is not clean"
2388 " -- starting background reconstruction\n",
2389 mdname(mddev));
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
NeilBrowneae17012005-11-08 21:39:23 -08002393static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002394safe_delay_show(mddev_t *mddev, char *page)
2395{
2396 int msec = (mddev->safemode_delay*1000)/HZ;
2397 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2398}
2399static ssize_t
2400safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2401{
2402 int scale=1;
2403 int dot=0;
2404 int i;
2405 unsigned long msec;
2406 char buf[30];
2407 char *e;
2408 /* remove a period, and count digits after it */
2409 if (len >= sizeof(buf))
2410 return -EINVAL;
2411 strlcpy(buf, cbuf, len);
2412 buf[len] = 0;
2413 for (i=0; i<len; i++) {
2414 if (dot) {
2415 if (isdigit(buf[i])) {
2416 buf[i-1] = buf[i];
2417 scale *= 10;
2418 }
2419 buf[i] = 0;
2420 } else if (buf[i] == '.') {
2421 dot=1;
2422 buf[i] = 0;
2423 }
2424 }
2425 msec = simple_strtoul(buf, &e, 10);
2426 if (e == buf || (*e && *e != '\n'))
2427 return -EINVAL;
2428 msec = (msec * 1000) / scale;
2429 if (msec == 0)
2430 mddev->safemode_delay = 0;
2431 else {
2432 mddev->safemode_delay = (msec*HZ)/1000;
2433 if (mddev->safemode_delay == 0)
2434 mddev->safemode_delay = 1;
2435 }
2436 return len;
2437}
2438static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002439__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002440
2441static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002442level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002443{
NeilBrown2604b702006-01-06 00:20:36 -08002444 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002445 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002446 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002447 else if (mddev->clevel[0])
2448 return sprintf(page, "%s\n", mddev->clevel);
2449 else if (mddev->level != LEVEL_NONE)
2450 return sprintf(page, "%d\n", mddev->level);
2451 else
2452 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002453}
2454
NeilBrownd9d166c2006-01-06 00:20:51 -08002455static ssize_t
2456level_store(mddev_t *mddev, const char *buf, size_t len)
2457{
NeilBrown20a49ff2008-02-06 01:39:57 -08002458 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002459 if (mddev->pers)
2460 return -EBUSY;
2461 if (len == 0)
2462 return 0;
2463 if (len >= sizeof(mddev->clevel))
2464 return -ENOSPC;
2465 strncpy(mddev->clevel, buf, len);
2466 if (mddev->clevel[len-1] == '\n')
2467 len--;
2468 mddev->clevel[len] = 0;
2469 mddev->level = LEVEL_NONE;
2470 return rv;
2471}
2472
2473static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002474__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002475
NeilBrownd4dbd022006-06-26 00:27:59 -07002476
2477static ssize_t
2478layout_show(mddev_t *mddev, char *page)
2479{
2480 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002481 if (mddev->reshape_position != MaxSector &&
2482 mddev->layout != mddev->new_layout)
2483 return sprintf(page, "%d (%d)\n",
2484 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002485 return sprintf(page, "%d\n", mddev->layout);
2486}
2487
2488static ssize_t
2489layout_store(mddev_t *mddev, const char *buf, size_t len)
2490{
2491 char *e;
2492 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002493
2494 if (!*buf || (*e && *e != '\n'))
2495 return -EINVAL;
2496
NeilBrown08a02ec2007-05-09 02:35:38 -07002497 if (mddev->pers)
2498 return -EBUSY;
2499 if (mddev->reshape_position != MaxSector)
2500 mddev->new_layout = n;
2501 else
2502 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002503 return len;
2504}
2505static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002506__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002507
2508
NeilBrowneae17012005-11-08 21:39:23 -08002509static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002510raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002511{
NeilBrownbb636542005-11-08 21:39:45 -08002512 if (mddev->raid_disks == 0)
2513 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002514 if (mddev->reshape_position != MaxSector &&
2515 mddev->delta_disks != 0)
2516 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2517 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002518 return sprintf(page, "%d\n", mddev->raid_disks);
2519}
2520
NeilBrownda943b992006-01-06 00:20:54 -08002521static int update_raid_disks(mddev_t *mddev, int raid_disks);
2522
2523static ssize_t
2524raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2525{
NeilBrownda943b992006-01-06 00:20:54 -08002526 char *e;
2527 int rv = 0;
2528 unsigned long n = simple_strtoul(buf, &e, 10);
2529
2530 if (!*buf || (*e && *e != '\n'))
2531 return -EINVAL;
2532
2533 if (mddev->pers)
2534 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002535 else if (mddev->reshape_position != MaxSector) {
2536 int olddisks = mddev->raid_disks - mddev->delta_disks;
2537 mddev->delta_disks = n - olddisks;
2538 mddev->raid_disks = n;
2539 } else
NeilBrownda943b992006-01-06 00:20:54 -08002540 mddev->raid_disks = n;
2541 return rv ? rv : len;
2542}
2543static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002544__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002545
NeilBrown24dd4692005-11-08 21:39:26 -08002546static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002547chunk_size_show(mddev_t *mddev, char *page)
2548{
NeilBrown08a02ec2007-05-09 02:35:38 -07002549 if (mddev->reshape_position != MaxSector &&
2550 mddev->chunk_size != mddev->new_chunk)
2551 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2552 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002553 return sprintf(page, "%d\n", mddev->chunk_size);
2554}
2555
2556static ssize_t
2557chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2558{
2559 /* can only set chunk_size if array is not yet active */
2560 char *e;
2561 unsigned long n = simple_strtoul(buf, &e, 10);
2562
NeilBrown3b343802006-01-06 00:20:47 -08002563 if (!*buf || (*e && *e != '\n'))
2564 return -EINVAL;
2565
NeilBrown08a02ec2007-05-09 02:35:38 -07002566 if (mddev->pers)
2567 return -EBUSY;
2568 else if (mddev->reshape_position != MaxSector)
2569 mddev->new_chunk = n;
2570 else
2571 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002572 return len;
2573}
2574static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002575__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002576
NeilBrowna94213b2006-06-26 00:28:00 -07002577static ssize_t
2578resync_start_show(mddev_t *mddev, char *page)
2579{
2580 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2581}
2582
2583static ssize_t
2584resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2585{
NeilBrowna94213b2006-06-26 00:28:00 -07002586 char *e;
2587 unsigned long long n = simple_strtoull(buf, &e, 10);
2588
2589 if (mddev->pers)
2590 return -EBUSY;
2591 if (!*buf || (*e && *e != '\n'))
2592 return -EINVAL;
2593
2594 mddev->recovery_cp = n;
2595 return len;
2596}
2597static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002598__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002599
NeilBrown9e653b62006-06-26 00:27:58 -07002600/*
2601 * The array state can be:
2602 *
2603 * clear
2604 * No devices, no size, no level
2605 * Equivalent to STOP_ARRAY ioctl
2606 * inactive
2607 * May have some settings, but array is not active
2608 * all IO results in error
2609 * When written, doesn't tear down array, but just stops it
2610 * suspended (not supported yet)
2611 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002612 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002613 * readonly
2614 * no resync can happen. no superblocks get written.
2615 * write requests fail
2616 * read-auto
2617 * like readonly, but behaves like 'clean' on a write request.
2618 *
2619 * clean - no pending writes, but otherwise active.
2620 * When written to inactive array, starts without resync
2621 * If a write request arrives then
2622 * if metadata is known, mark 'dirty' and switch to 'active'.
2623 * if not known, block and switch to write-pending
2624 * If written to an active array that has pending writes, then fails.
2625 * active
2626 * fully active: IO and resync can be happening.
2627 * When written to inactive array, starts with resync
2628 *
2629 * write-pending
2630 * clean, but writes are blocked waiting for 'active' to be written.
2631 *
2632 * active-idle
2633 * like active, but no writes have been seen for a while (100msec).
2634 *
2635 */
2636enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2637 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002638static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002639 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2640 "write-pending", "active-idle", NULL };
2641
2642static int match_word(const char *word, char **list)
2643{
2644 int n;
2645 for (n=0; list[n]; n++)
2646 if (cmd_match(word, list[n]))
2647 break;
2648 return n;
2649}
2650
2651static ssize_t
2652array_state_show(mddev_t *mddev, char *page)
2653{
2654 enum array_state st = inactive;
2655
2656 if (mddev->pers)
2657 switch(mddev->ro) {
2658 case 1:
2659 st = readonly;
2660 break;
2661 case 2:
2662 st = read_auto;
2663 break;
2664 case 0:
2665 if (mddev->in_sync)
2666 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002667 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2668 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002669 else if (mddev->safemode)
2670 st = active_idle;
2671 else
2672 st = active;
2673 }
2674 else {
2675 if (list_empty(&mddev->disks) &&
2676 mddev->raid_disks == 0 &&
2677 mddev->size == 0)
2678 st = clear;
2679 else
2680 st = inactive;
2681 }
2682 return sprintf(page, "%s\n", array_states[st]);
2683}
2684
Neil Browndf5b20c2008-07-11 22:02:22 +10002685static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002686static int do_md_run(mddev_t * mddev);
2687static int restart_array(mddev_t *mddev);
2688
2689static ssize_t
2690array_state_store(mddev_t *mddev, const char *buf, size_t len)
2691{
2692 int err = -EINVAL;
2693 enum array_state st = match_word(buf, array_states);
2694 switch(st) {
2695 case bad_word:
2696 break;
2697 case clear:
2698 /* stopping an active array */
NeilBrowne6910632008-02-06 01:39:51 -08002699 if (atomic_read(&mddev->active) > 1)
2700 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002701 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002702 break;
2703 case inactive:
2704 /* stopping an active array */
2705 if (mddev->pers) {
2706 if (atomic_read(&mddev->active) > 1)
2707 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002708 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08002709 } else
2710 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002711 break;
2712 case suspended:
2713 break; /* not supported yet */
2714 case readonly:
2715 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10002716 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002717 else {
2718 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002719 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002720 err = do_md_run(mddev);
2721 }
2722 break;
2723 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002724 if (mddev->pers) {
NeilBrown648b6292008-04-30 00:52:30 -07002725 if (mddev->ro != 1)
Neil Browndf5b20c2008-07-11 22:02:22 +10002726 err = do_md_stop(mddev, 1, 0);
NeilBrown648b6292008-04-30 00:52:30 -07002727 else
2728 err = restart_array(mddev);
2729 if (err == 0) {
2730 mddev->ro = 2;
2731 set_disk_ro(mddev->gendisk, 0);
2732 }
NeilBrown9e653b62006-06-26 00:27:58 -07002733 } else {
2734 mddev->ro = 2;
2735 err = do_md_run(mddev);
2736 }
2737 break;
2738 case clean:
2739 if (mddev->pers) {
2740 restart_array(mddev);
2741 spin_lock_irq(&mddev->write_lock);
2742 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002743 if (mddev->in_sync == 0) {
2744 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002745 if (mddev->safemode == 1)
2746 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002747 if (mddev->persistent)
2748 set_bit(MD_CHANGE_CLEAN,
2749 &mddev->flags);
2750 }
2751 err = 0;
2752 } else
2753 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002754 spin_unlock_irq(&mddev->write_lock);
2755 } else {
2756 mddev->ro = 0;
2757 mddev->recovery_cp = MaxSector;
2758 err = do_md_run(mddev);
2759 }
2760 break;
2761 case active:
2762 if (mddev->pers) {
2763 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002764 if (mddev->external)
2765 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002766 wake_up(&mddev->sb_wait);
2767 err = 0;
2768 } else {
2769 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002770 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002771 err = do_md_run(mddev);
2772 }
2773 break;
2774 case write_pending:
2775 case active_idle:
2776 /* these cannot be set */
2777 break;
2778 }
2779 if (err)
2780 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002781 else {
2782 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown9e653b62006-06-26 00:27:58 -07002783 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002784 }
NeilBrown9e653b62006-06-26 00:27:58 -07002785}
NeilBrown80ca3a42006-07-10 04:44:18 -07002786static struct md_sysfs_entry md_array_state =
2787__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002788
NeilBrown6d7ff732006-01-06 00:21:16 -08002789static ssize_t
2790null_show(mddev_t *mddev, char *page)
2791{
2792 return -EINVAL;
2793}
2794
2795static ssize_t
2796new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2797{
2798 /* buf must be %d:%d\n? giving major and minor numbers */
2799 /* The new device is added to the array.
2800 * If the array has a persistent superblock, we read the
2801 * superblock to initialise info and check validity.
2802 * Otherwise, only checking done is that in bind_rdev_to_array,
2803 * which mainly checks size.
2804 */
2805 char *e;
2806 int major = simple_strtoul(buf, &e, 10);
2807 int minor;
2808 dev_t dev;
2809 mdk_rdev_t *rdev;
2810 int err;
2811
2812 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2813 return -EINVAL;
2814 minor = simple_strtoul(e+1, &e, 10);
2815 if (*e && *e != '\n')
2816 return -EINVAL;
2817 dev = MKDEV(major, minor);
2818 if (major != MAJOR(dev) ||
2819 minor != MINOR(dev))
2820 return -EOVERFLOW;
2821
2822
2823 if (mddev->persistent) {
2824 rdev = md_import_device(dev, mddev->major_version,
2825 mddev->minor_version);
2826 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2827 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2828 mdk_rdev_t, same_set);
2829 err = super_types[mddev->major_version]
2830 .load_super(rdev, rdev0, mddev->minor_version);
2831 if (err < 0)
2832 goto out;
2833 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002834 } else if (mddev->external)
2835 rdev = md_import_device(dev, -2, -1);
2836 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002837 rdev = md_import_device(dev, -1, -1);
2838
2839 if (IS_ERR(rdev))
2840 return PTR_ERR(rdev);
2841 err = bind_rdev_to_array(rdev, mddev);
2842 out:
2843 if (err)
2844 export_rdev(rdev);
2845 return err ? err : len;
2846}
2847
2848static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002849__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002850
2851static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002852bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2853{
2854 char *end;
2855 unsigned long chunk, end_chunk;
2856
2857 if (!mddev->bitmap)
2858 goto out;
2859 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2860 while (*buf) {
2861 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2862 if (buf == end) break;
2863 if (*end == '-') { /* range */
2864 buf = end + 1;
2865 end_chunk = simple_strtoul(buf, &end, 0);
2866 if (buf == end) break;
2867 }
2868 if (*end && !isspace(*end)) break;
2869 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2870 buf = end;
2871 while (isspace(*buf)) buf++;
2872 }
2873 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
2874out:
2875 return len;
2876}
2877
2878static struct md_sysfs_entry md_bitmap =
2879__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
2880
2881static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08002882size_show(mddev_t *mddev, char *page)
2883{
2884 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
2885}
2886
Andre Nolld71f9f82008-07-11 22:02:22 +10002887static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08002888
2889static ssize_t
2890size_store(mddev_t *mddev, const char *buf, size_t len)
2891{
2892 /* If array is inactive, we can reduce the component size, but
2893 * not increase it (except from 0).
2894 * If array is active, we can try an on-line resize
2895 */
2896 char *e;
2897 int err = 0;
2898 unsigned long long size = simple_strtoull(buf, &e, 10);
2899 if (!*buf || *buf == '\n' ||
2900 (*e && *e != '\n'))
2901 return -EINVAL;
2902
2903 if (mddev->pers) {
Andre Nolld71f9f82008-07-11 22:02:22 +10002904 err = update_size(mddev, size * 2);
NeilBrown850b2b42006-10-03 01:15:46 -07002905 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08002906 } else {
2907 if (mddev->size == 0 ||
2908 mddev->size > size)
2909 mddev->size = size;
2910 else
2911 err = -ENOSPC;
2912 }
2913 return err ? err : len;
2914}
2915
2916static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002917__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08002918
NeilBrown8bb93aa2006-01-06 00:20:50 -08002919
2920/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08002921 * This is one of
2922 * 'none' for arrays with no metadata (good luck...)
2923 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08002924 * or N.M for internally known formats
2925 */
2926static ssize_t
2927metadata_show(mddev_t *mddev, char *page)
2928{
2929 if (mddev->persistent)
2930 return sprintf(page, "%d.%d\n",
2931 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08002932 else if (mddev->external)
2933 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002934 else
2935 return sprintf(page, "none\n");
2936}
2937
2938static ssize_t
2939metadata_store(mddev_t *mddev, const char *buf, size_t len)
2940{
2941 int major, minor;
2942 char *e;
2943 if (!list_empty(&mddev->disks))
2944 return -EBUSY;
2945
2946 if (cmd_match(buf, "none")) {
2947 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002948 mddev->external = 0;
2949 mddev->major_version = 0;
2950 mddev->minor_version = 90;
2951 return len;
2952 }
2953 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08002954 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08002955 if (namelen >= sizeof(mddev->metadata_type))
2956 namelen = sizeof(mddev->metadata_type)-1;
2957 strncpy(mddev->metadata_type, buf+9, namelen);
2958 mddev->metadata_type[namelen] = 0;
2959 if (namelen && mddev->metadata_type[namelen-1] == '\n')
2960 mddev->metadata_type[--namelen] = 0;
2961 mddev->persistent = 0;
2962 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002963 mddev->major_version = 0;
2964 mddev->minor_version = 90;
2965 return len;
2966 }
2967 major = simple_strtoul(buf, &e, 10);
2968 if (e==buf || *e != '.')
2969 return -EINVAL;
2970 buf = e+1;
2971 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002972 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08002973 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07002974 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08002975 return -ENOENT;
2976 mddev->major_version = major;
2977 mddev->minor_version = minor;
2978 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08002979 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08002980 return len;
2981}
2982
2983static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07002984__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08002985
NeilBrowna35b0d62006-01-06 00:20:49 -08002986static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08002987action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08002988{
NeilBrown7eec3142005-11-08 21:39:44 -08002989 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08002990 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07002991 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08002992 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2993 type = "reshape";
2994 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08002995 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2996 type = "resync";
2997 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2998 type = "check";
2999 else
3000 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003001 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003002 type = "recover";
3003 }
3004 return sprintf(page, "%s\n", type);
3005}
3006
3007static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003008action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003009{
NeilBrown7eec3142005-11-08 21:39:44 -08003010 if (!mddev->pers || !mddev->pers->sync_request)
3011 return -EINVAL;
3012
NeilBrownbce74da2006-01-06 00:20:41 -08003013 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003014 if (mddev->sync_thread) {
3015 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3016 md_unregister_thread(mddev->sync_thread);
3017 mddev->sync_thread = NULL;
3018 mddev->recovery = 0;
3019 }
NeilBrown03c902e2006-01-06 00:20:46 -08003020 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3021 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003022 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003023 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003024 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003025 else if (cmd_match(page, "recover")) {
3026 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3027 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3028 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003029 int err;
3030 if (mddev->pers->start_reshape == NULL)
3031 return -EINVAL;
3032 err = mddev->pers->start_reshape(mddev);
3033 if (err)
3034 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003035 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003036 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003037 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003038 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003039 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003040 return -EINVAL;
3041 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3042 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003043 }
NeilBrown03c902e2006-01-06 00:20:46 -08003044 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003045 md_wakeup_thread(mddev->thread);
Neil Brown72a23c22008-06-28 08:31:41 +10003046 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrown24dd4692005-11-08 21:39:26 -08003047 return len;
3048}
3049
NeilBrown9d888832005-11-08 21:39:26 -08003050static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003051mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003052{
3053 return sprintf(page, "%llu\n",
3054 (unsigned long long) mddev->resync_mismatches);
3055}
3056
NeilBrown80ca3a42006-07-10 04:44:18 -07003057static struct md_sysfs_entry md_scan_mode =
3058__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003059
NeilBrown96de1e62005-11-08 21:39:39 -08003060
NeilBrown80ca3a42006-07-10 04:44:18 -07003061static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003062
NeilBrown88202a02006-01-06 00:21:36 -08003063static ssize_t
3064sync_min_show(mddev_t *mddev, char *page)
3065{
3066 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3067 mddev->sync_speed_min ? "local": "system");
3068}
3069
3070static ssize_t
3071sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3072{
3073 int min;
3074 char *e;
3075 if (strncmp(buf, "system", 6)==0) {
3076 mddev->sync_speed_min = 0;
3077 return len;
3078 }
3079 min = simple_strtoul(buf, &e, 10);
3080 if (buf == e || (*e && *e != '\n') || min <= 0)
3081 return -EINVAL;
3082 mddev->sync_speed_min = min;
3083 return len;
3084}
3085
3086static struct md_sysfs_entry md_sync_min =
3087__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3088
3089static ssize_t
3090sync_max_show(mddev_t *mddev, char *page)
3091{
3092 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3093 mddev->sync_speed_max ? "local": "system");
3094}
3095
3096static ssize_t
3097sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3098{
3099 int max;
3100 char *e;
3101 if (strncmp(buf, "system", 6)==0) {
3102 mddev->sync_speed_max = 0;
3103 return len;
3104 }
3105 max = simple_strtoul(buf, &e, 10);
3106 if (buf == e || (*e && *e != '\n') || max <= 0)
3107 return -EINVAL;
3108 mddev->sync_speed_max = max;
3109 return len;
3110}
3111
3112static struct md_sysfs_entry md_sync_max =
3113__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3114
Iustin Popd7f3d292007-10-16 23:30:54 -07003115static ssize_t
3116degraded_show(mddev_t *mddev, char *page)
3117{
3118 return sprintf(page, "%d\n", mddev->degraded);
3119}
3120static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003121
3122static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003123sync_force_parallel_show(mddev_t *mddev, char *page)
3124{
3125 return sprintf(page, "%d\n", mddev->parallel_resync);
3126}
3127
3128static ssize_t
3129sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3130{
3131 long n;
3132
3133 if (strict_strtol(buf, 10, &n))
3134 return -EINVAL;
3135
3136 if (n != 0 && n != 1)
3137 return -EINVAL;
3138
3139 mddev->parallel_resync = n;
3140
3141 if (mddev->sync_thread)
3142 wake_up(&resync_wait);
3143
3144 return len;
3145}
3146
3147/* force parallel resync, even with shared block devices */
3148static struct md_sysfs_entry md_sync_force_parallel =
3149__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3150 sync_force_parallel_show, sync_force_parallel_store);
3151
3152static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003153sync_speed_show(mddev_t *mddev, char *page)
3154{
3155 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003156 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3157 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003158 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003159 db = resync - mddev->resync_mark_cnt;
3160 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003161}
3162
NeilBrown80ca3a42006-07-10 04:44:18 -07003163static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003164
3165static ssize_t
3166sync_completed_show(mddev_t *mddev, char *page)
3167{
3168 unsigned long max_blocks, resync;
3169
3170 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3171 max_blocks = mddev->resync_max_sectors;
3172 else
3173 max_blocks = mddev->size << 1;
3174
3175 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3176 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3177}
3178
NeilBrown80ca3a42006-07-10 04:44:18 -07003179static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003180
NeilBrowne464eaf2006-03-27 01:18:14 -08003181static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003182min_sync_show(mddev_t *mddev, char *page)
3183{
3184 return sprintf(page, "%llu\n",
3185 (unsigned long long)mddev->resync_min);
3186}
3187static ssize_t
3188min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3189{
3190 unsigned long long min;
3191 if (strict_strtoull(buf, 10, &min))
3192 return -EINVAL;
3193 if (min > mddev->resync_max)
3194 return -EINVAL;
3195 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3196 return -EBUSY;
3197
3198 /* Must be a multiple of chunk_size */
3199 if (mddev->chunk_size) {
3200 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3201 return -EINVAL;
3202 }
3203 mddev->resync_min = min;
3204
3205 return len;
3206}
3207
3208static struct md_sysfs_entry md_min_sync =
3209__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3210
3211static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003212max_sync_show(mddev_t *mddev, char *page)
3213{
3214 if (mddev->resync_max == MaxSector)
3215 return sprintf(page, "max\n");
3216 else
3217 return sprintf(page, "%llu\n",
3218 (unsigned long long)mddev->resync_max);
3219}
3220static ssize_t
3221max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3222{
3223 if (strncmp(buf, "max", 3) == 0)
3224 mddev->resync_max = MaxSector;
3225 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003226 unsigned long long max;
3227 if (strict_strtoull(buf, 10, &max))
3228 return -EINVAL;
3229 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003230 return -EINVAL;
3231 if (max < mddev->resync_max &&
3232 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3233 return -EBUSY;
3234
3235 /* Must be a multiple of chunk_size */
3236 if (mddev->chunk_size) {
3237 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3238 return -EINVAL;
3239 }
3240 mddev->resync_max = max;
3241 }
3242 wake_up(&mddev->recovery_wait);
3243 return len;
3244}
3245
3246static struct md_sysfs_entry md_max_sync =
3247__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3248
3249static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003250suspend_lo_show(mddev_t *mddev, char *page)
3251{
3252 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3253}
3254
3255static ssize_t
3256suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3257{
3258 char *e;
3259 unsigned long long new = simple_strtoull(buf, &e, 10);
3260
3261 if (mddev->pers->quiesce == NULL)
3262 return -EINVAL;
3263 if (buf == e || (*e && *e != '\n'))
3264 return -EINVAL;
3265 if (new >= mddev->suspend_hi ||
3266 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3267 mddev->suspend_lo = new;
3268 mddev->pers->quiesce(mddev, 2);
3269 return len;
3270 } else
3271 return -EINVAL;
3272}
3273static struct md_sysfs_entry md_suspend_lo =
3274__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3275
3276
3277static ssize_t
3278suspend_hi_show(mddev_t *mddev, char *page)
3279{
3280 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3281}
3282
3283static ssize_t
3284suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3285{
3286 char *e;
3287 unsigned long long new = simple_strtoull(buf, &e, 10);
3288
3289 if (mddev->pers->quiesce == NULL)
3290 return -EINVAL;
3291 if (buf == e || (*e && *e != '\n'))
3292 return -EINVAL;
3293 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3294 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3295 mddev->suspend_hi = new;
3296 mddev->pers->quiesce(mddev, 1);
3297 mddev->pers->quiesce(mddev, 0);
3298 return len;
3299 } else
3300 return -EINVAL;
3301}
3302static struct md_sysfs_entry md_suspend_hi =
3303__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3304
NeilBrown08a02ec2007-05-09 02:35:38 -07003305static ssize_t
3306reshape_position_show(mddev_t *mddev, char *page)
3307{
3308 if (mddev->reshape_position != MaxSector)
3309 return sprintf(page, "%llu\n",
3310 (unsigned long long)mddev->reshape_position);
3311 strcpy(page, "none\n");
3312 return 5;
3313}
3314
3315static ssize_t
3316reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3317{
3318 char *e;
3319 unsigned long long new = simple_strtoull(buf, &e, 10);
3320 if (mddev->pers)
3321 return -EBUSY;
3322 if (buf == e || (*e && *e != '\n'))
3323 return -EINVAL;
3324 mddev->reshape_position = new;
3325 mddev->delta_disks = 0;
3326 mddev->new_level = mddev->level;
3327 mddev->new_layout = mddev->layout;
3328 mddev->new_chunk = mddev->chunk_size;
3329 return len;
3330}
3331
3332static struct md_sysfs_entry md_reshape_position =
3333__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3334 reshape_position_store);
3335
NeilBrowne464eaf2006-03-27 01:18:14 -08003336
NeilBrowneae17012005-11-08 21:39:23 -08003337static struct attribute *md_default_attrs[] = {
3338 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003339 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003340 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003341 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003342 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003343 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003344 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003345 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003346 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003347 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003348 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003349 NULL,
3350};
3351
3352static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003353 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003354 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003355 &md_sync_min.attr,
3356 &md_sync_max.attr,
3357 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003358 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003359 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003360 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003361 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003362 &md_suspend_lo.attr,
3363 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003364 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003365 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003366 NULL,
3367};
NeilBrown411036f2005-11-08 21:39:40 -08003368static struct attribute_group md_redundancy_group = {
3369 .name = NULL,
3370 .attrs = md_redundancy_attrs,
3371};
3372
NeilBrowneae17012005-11-08 21:39:23 -08003373
3374static ssize_t
3375md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3376{
3377 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3378 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003379 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003380
3381 if (!entry->show)
3382 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003383 rv = mddev_lock(mddev);
3384 if (!rv) {
3385 rv = entry->show(mddev, page);
3386 mddev_unlock(mddev);
3387 }
NeilBrown96de1e62005-11-08 21:39:39 -08003388 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003389}
3390
3391static ssize_t
3392md_attr_store(struct kobject *kobj, struct attribute *attr,
3393 const char *page, size_t length)
3394{
3395 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3396 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003397 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003398
3399 if (!entry->store)
3400 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003401 if (!capable(CAP_SYS_ADMIN))
3402 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003403 rv = mddev_lock(mddev);
3404 if (!rv) {
3405 rv = entry->store(mddev, page, length);
3406 mddev_unlock(mddev);
3407 }
NeilBrown96de1e62005-11-08 21:39:39 -08003408 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003409}
3410
3411static void md_free(struct kobject *ko)
3412{
3413 mddev_t *mddev = container_of(ko, mddev_t, kobj);
3414 kfree(mddev);
3415}
3416
3417static struct sysfs_ops md_sysfs_ops = {
3418 .show = md_attr_show,
3419 .store = md_attr_store,
3420};
3421static struct kobj_type md_ktype = {
3422 .release = md_free,
3423 .sysfs_ops = &md_sysfs_ops,
3424 .default_attrs = md_default_attrs,
3425};
3426
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427int mdp_major = 0;
3428
3429static struct kobject *md_probe(dev_t dev, int *part, void *data)
3430{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003431 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 mddev_t *mddev = mddev_find(dev);
3433 struct gendisk *disk;
3434 int partitioned = (MAJOR(dev) != MD_MAJOR);
3435 int shift = partitioned ? MdpMinorShift : 0;
3436 int unit = MINOR(dev) >> shift;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003437 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 if (!mddev)
3440 return NULL;
3441
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003442 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003444 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 mddev_put(mddev);
3446 return NULL;
3447 }
3448 disk = alloc_disk(1 << shift);
3449 if (!disk) {
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->major = MAJOR(dev);
3455 disk->first_minor = unit << shift;
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003456 if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003458 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 disk->fops = &md_fops;
3461 disk->private_data = mddev;
3462 disk->queue = mddev->queue;
3463 add_disk(disk);
3464 mddev->gendisk = disk;
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003465 error = kobject_init_and_add(&mddev->kobj, &md_ktype, &disk->dev.kobj,
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003466 "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003467 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003468 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003469 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3470 disk->disk_name);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003471 else
3472 kobject_uevent(&mddev->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 return NULL;
3474}
3475
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476static void md_safemode_timeout(unsigned long data)
3477{
3478 mddev_t *mddev = (mddev_t *) data;
3479
Neil Brown0fd62b82008-06-28 08:31:36 +10003480 if (!atomic_read(&mddev->writes_pending)) {
3481 mddev->safemode = 1;
3482 if (mddev->external)
3483 sysfs_notify(&mddev->kobj, NULL, "array_state");
3484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 md_wakeup_thread(mddev->thread);
3486}
3487
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003488static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
3490static int do_md_run(mddev_t * mddev)
3491{
NeilBrown2604b702006-01-06 00:20:36 -08003492 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 int chunk_size;
3494 struct list_head *tmp;
3495 mdk_rdev_t *rdev;
3496 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003497 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 char b[BDEVNAME_SIZE];
3499
NeilBrowna757e642005-04-16 15:26:42 -07003500 if (list_empty(&mddev->disks))
3501 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 if (mddev->pers)
3505 return -EBUSY;
3506
3507 /*
3508 * Analyze all RAID superblock(s)
3509 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003510 if (!mddev->raid_disks) {
3511 if (!mddev->persistent)
3512 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003513 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
3516 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
NeilBrown2604b702006-01-06 00:20:36 -08003518 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 if (chunk_size > MAX_CHUNK_SIZE) {
3520 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3521 chunk_size, MAX_CHUNK_SIZE);
3522 return -EINVAL;
3523 }
3524 /*
3525 * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
3526 */
3527 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003528 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 return -EINVAL;
3530 }
3531 if (chunk_size < PAGE_SIZE) {
3532 printk(KERN_ERR "too small chunk_size: %d < %ld\n",
3533 chunk_size, PAGE_SIZE);
3534 return -EINVAL;
3535 }
3536
3537 /* devices must have minimum size of one chunk */
NeilBrownd089c6a2008-02-06 01:39:59 -08003538 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003539 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 continue;
3541 if (rdev->size < chunk_size / 1024) {
3542 printk(KERN_WARNING
3543 "md: Dev %s smaller than chunk_size:"
3544 " %lluk < %dk\n",
3545 bdevname(rdev->bdev,b),
3546 (unsigned long long)rdev->size,
3547 chunk_size / 1024);
3548 return -EINVAL;
3549 }
3550 }
3551 }
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553#ifdef CONFIG_KMOD
NeilBrownd9d166c2006-01-06 00:20:51 -08003554 if (mddev->level != LEVEL_NONE)
3555 request_module("md-level-%d", mddev->level);
3556 else if (mddev->clevel[0])
3557 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558#endif
3559
3560 /*
3561 * Drop all container device buffers, from now on
3562 * the only valid external interface is through the md
3563 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 */
NeilBrownd089c6a2008-02-06 01:39:59 -08003565 rdev_for_each(rdev, tmp, mddev) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003566 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 continue;
3568 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003569 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003570
3571 /* perform some consistency tests on the device.
3572 * We don't want the data to overlap the metadata,
3573 * Internal Bitmap issues has handled elsewhere.
3574 */
Andre Noll0f420352008-07-11 22:02:23 +10003575 if (rdev->data_offset < rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003576 if (mddev->size &&
3577 rdev->data_offset + mddev->size*2
Andre Noll0f420352008-07-11 22:02:23 +10003578 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003579 printk("md: %s: data overlaps metadata\n",
3580 mdname(mddev));
3581 return -EINVAL;
3582 }
3583 } else {
Andre Noll0f420352008-07-11 22:02:23 +10003584 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07003585 > rdev->data_offset) {
3586 printk("md: %s: metadata overlaps data\n",
3587 mdname(mddev));
3588 return -EINVAL;
3589 }
3590 }
Neil Brown52664732008-06-28 08:31:44 +10003591 sysfs_notify(&rdev->kobj, NULL, "state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
3593
3594 md_probe(mddev->unit, NULL, NULL);
3595 disk = mddev->gendisk;
3596 if (!disk)
3597 return -ENOMEM;
3598
3599 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003600 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003601 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003603 if (mddev->level != LEVEL_NONE)
3604 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3605 mddev->level);
3606 else
3607 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3608 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 return -EINVAL;
3610 }
NeilBrown2604b702006-01-06 00:20:36 -08003611 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003613 mddev->level = pers->level;
3614 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615
NeilBrownf6705572006-03-27 01:18:11 -08003616 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003617 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003618 /* This personality cannot handle reshaping... */
3619 mddev->pers = NULL;
3620 module_put(pers->owner);
3621 return -EINVAL;
3622 }
3623
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003624 if (pers->sync_request) {
3625 /* Warn if this is a potentially silly
3626 * configuration.
3627 */
3628 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3629 mdk_rdev_t *rdev2;
3630 struct list_head *tmp2;
3631 int warned = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003632 rdev_for_each(rdev, tmp, mddev) {
3633 rdev_for_each(rdev2, tmp2, mddev) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003634 if (rdev < rdev2 &&
3635 rdev->bdev->bd_contains ==
3636 rdev2->bdev->bd_contains) {
3637 printk(KERN_WARNING
3638 "%s: WARNING: %s appears to be"
3639 " on the same physical disk as"
3640 " %s.\n",
3641 mdname(mddev),
3642 bdevname(rdev->bdev,b),
3643 bdevname(rdev2->bdev,b2));
3644 warned = 1;
3645 }
3646 }
3647 }
3648 if (warned)
3649 printk(KERN_WARNING
3650 "True protection against single-disk"
3651 " failure might be compromised.\n");
3652 }
3653
NeilBrown657390d2005-08-26 18:34:16 -07003654 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003656 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003657 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
NeilBrownf91de922005-11-08 21:39:36 -08003659 if (start_readonly)
3660 mddev->ro = 2; /* read-only, but switch on first write */
3661
NeilBrownb15c2e52006-01-06 00:20:16 -08003662 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01003663 if (err)
3664 printk(KERN_ERR "md: pers->run() failed ...\n");
3665 else if (mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08003666 err = bitmap_create(mddev);
3667 if (err) {
3668 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3669 mdname(mddev), err);
3670 mddev->pers->stop(mddev);
3671 }
3672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 module_put(mddev->pers->owner);
3675 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003676 bitmap_destroy(mddev);
3677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003679 if (mddev->pers->sync_request) {
3680 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3681 printk(KERN_WARNING
3682 "md: cannot register extra attributes for %s\n",
3683 mdname(mddev));
3684 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003685 mddev->ro = 0;
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 atomic_set(&mddev->writes_pending,0);
3688 mddev->safemode = 0;
3689 mddev->safemode_timer.function = md_safemode_timeout;
3690 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003691 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003693
NeilBrownd089c6a2008-02-06 01:39:59 -08003694 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003695 if (rdev->raid_disk >= 0) {
3696 char nm[20];
3697 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003698 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3699 printk("md: cannot register %s for %s\n",
3700 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3704
NeilBrown850b2b42006-10-03 01:15:46 -07003705 if (mddev->flags)
3706 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
3708 set_capacity(disk, mddev->array_size<<1);
3709
3710 /* If we call blk_queue_make_request here, it will
3711 * re-initialise max_sectors etc which may have been
3712 * refined inside -> run. So just set the bits we need to set.
3713 * Most initialisation happended when we called
3714 * blk_queue_make_request(..., md_fail_request)
3715 * earlier.
3716 */
3717 mddev->queue->queuedata = mddev;
3718 mddev->queue->make_request_fn = mddev->pers->make_request;
3719
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003720 /* If there is a partially-recovered drive we need to
3721 * start recovery here. If we leave it to md_check_recovery,
3722 * it will remove the drives and not do the right thing
3723 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003724 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003725 struct list_head *rtmp;
3726 int spares = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08003727 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003728 if (rdev->raid_disk >= 0 &&
3729 !test_bit(In_sync, &rdev->flags) &&
3730 !test_bit(Faulty, &rdev->flags))
3731 /* complete an interrupted recovery */
3732 spares++;
3733 if (spares && mddev->pers->sync_request) {
3734 mddev->recovery = 0;
3735 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3736 mddev->sync_thread = md_register_thread(md_do_sync,
3737 mddev,
3738 "%s_resync");
3739 if (!mddev->sync_thread) {
3740 printk(KERN_ERR "%s: could not start resync"
3741 " thread...\n",
3742 mdname(mddev));
3743 /* leave the spares where they are, it shouldn't hurt */
3744 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003745 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003746 }
3747 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003748 md_wakeup_thread(mddev->thread);
3749 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003750
Linus Torvalds44ce6292007-05-09 18:51:36 -07003751 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003752 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003753 sysfs_notify(&mddev->kobj, NULL, "array_state");
Neil Brown72a23c22008-06-28 08:31:41 +10003754 sysfs_notify(&mddev->kobj, NULL, "sync_action");
Neil Browna99ac972008-06-28 08:31:43 +10003755 sysfs_notify(&mddev->kobj, NULL, "degraded");
Kay Sieversedfaa7c2007-05-21 22:08:01 +02003756 kobject_uevent(&mddev->gendisk->dev.kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 return 0;
3758}
3759
3760static int restart_array(mddev_t *mddev)
3761{
3762 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763
Andre Noll80fab1d2008-07-11 22:02:21 +10003764 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10003766 return -ENXIO;
3767 if (!mddev->pers)
3768 return -EINVAL;
3769 if (!mddev->ro)
3770 return -EBUSY;
3771 mddev->safemode = 0;
3772 mddev->ro = 0;
3773 set_disk_ro(disk, 0);
3774 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3775 mdname(mddev));
3776 /* Kick recovery or resync if necessary */
3777 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3778 md_wakeup_thread(mddev->thread);
3779 md_wakeup_thread(mddev->sync_thread);
3780 sysfs_notify(&mddev->kobj, NULL, "array_state");
3781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782}
3783
NeilBrownacc55e22006-06-26 00:27:47 -07003784/* similar to deny_write_access, but accounts for our holding a reference
3785 * to the file ourselves */
3786static int deny_bitmap_write_access(struct file * file)
3787{
3788 struct inode *inode = file->f_mapping->host;
3789
3790 spin_lock(&inode->i_lock);
3791 if (atomic_read(&inode->i_writecount) > 1) {
3792 spin_unlock(&inode->i_lock);
3793 return -ETXTBSY;
3794 }
3795 atomic_set(&inode->i_writecount, -1);
3796 spin_unlock(&inode->i_lock);
3797
3798 return 0;
3799}
3800
3801static void restore_bitmap_write_access(struct file *file)
3802{
3803 struct inode *inode = file->f_mapping->host;
3804
3805 spin_lock(&inode->i_lock);
3806 atomic_set(&inode->i_writecount, 1);
3807 spin_unlock(&inode->i_lock);
3808}
3809
NeilBrown9e653b62006-06-26 00:27:58 -07003810/* mode:
3811 * 0 - completely stop and dis-assemble array
3812 * 1 - switch to readonly
3813 * 2 - stop but do not disassemble array
3814 */
Neil Browndf5b20c2008-07-11 22:02:22 +10003815static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
3817 int err = 0;
3818 struct gendisk *disk = mddev->gendisk;
3819
Neil Browndf5b20c2008-07-11 22:02:22 +10003820 if (atomic_read(&mddev->active) > 1 + is_open) {
3821 printk("md: %s still in use.\n",mdname(mddev));
3822 return -EBUSY;
3823 }
3824
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
3827 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003828 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3830 md_unregister_thread(mddev->sync_thread);
3831 mddev->sync_thread = NULL;
3832 }
3833
3834 del_timer_sync(&mddev->safemode_timer);
3835
3836 invalidate_partition(disk, 0);
3837
NeilBrown9e653b62006-06-26 00:27:58 -07003838 switch(mode) {
3839 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08003841 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 goto out;
3843 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07003844 break;
3845 case 0: /* disassemble */
3846 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07003847 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08003848 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 if (mddev->ro)
3850 set_disk_ro(disk, 0);
3851 blk_queue_make_request(mddev->queue, md_fail_request);
3852 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08003853 mddev->queue->merge_bvec_fn = NULL;
3854 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08003855 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown411036f2005-11-08 21:39:40 -08003856 if (mddev->pers->sync_request)
3857 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 module_put(mddev->pers->owner);
3860 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07003861 /* tell userspace to handle 'inactive' */
3862 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown0d4ca602006-12-10 02:20:44 -08003863
3864 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07003865 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08003866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 if (mddev->ro)
3868 mddev->ro = 0;
3869 }
NeilBrown850b2b42006-10-03 01:15:46 -07003870 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 /* mark array as shutdown cleanly */
3872 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07003873 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 }
NeilBrown9e653b62006-06-26 00:27:58 -07003875 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003877 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 }
NeilBrown32a76272005-06-21 17:17:14 -07003879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 /*
3881 * Free resources if final stop
3882 */
NeilBrown9e653b62006-06-26 00:27:58 -07003883 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08003884 mdk_rdev_t *rdev;
3885 struct list_head *tmp;
NeilBrown0d4ca602006-12-10 02:20:44 -08003886
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
3888
NeilBrown978f9462006-02-02 14:28:05 -08003889 bitmap_destroy(mddev);
3890 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07003891 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08003892 fput(mddev->bitmap_file);
3893 mddev->bitmap_file = NULL;
3894 }
3895 mddev->bitmap_offset = 0;
3896
NeilBrownd089c6a2008-02-06 01:39:59 -08003897 rdev_for_each(rdev, tmp, mddev)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003898 if (rdev->raid_disk >= 0) {
3899 char nm[20];
3900 sprintf(nm, "rd%d", rdev->raid_disk);
3901 sysfs_remove_link(&mddev->kobj, nm);
3902 }
3903
NeilBrown177a99b2008-02-06 01:39:56 -08003904 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07003905 flush_scheduled_work();
3906
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 export_array(mddev);
3908
3909 mddev->array_size = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003910 mddev->size = 0;
3911 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07003912 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10003913 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08003914 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07003915 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08003916 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08003917 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07003918 mddev->level = LEVEL_NONE;
3919 mddev->clevel[0] = 0;
3920 mddev->flags = 0;
3921 mddev->ro = 0;
3922 mddev->metadata_type[0] = 0;
3923 mddev->chunk_size = 0;
3924 mddev->ctime = mddev->utime = 0;
3925 mddev->layout = 0;
3926 mddev->max_disks = 0;
3927 mddev->events = 0;
3928 mddev->delta_disks = 0;
3929 mddev->new_level = LEVEL_NONE;
3930 mddev->new_layout = 0;
3931 mddev->new_chunk = 0;
3932 mddev->curr_resync = 0;
3933 mddev->resync_mismatches = 0;
3934 mddev->suspend_lo = mddev->suspend_hi = 0;
3935 mddev->sync_speed_min = mddev->sync_speed_max = 0;
3936 mddev->recovery = 0;
3937 mddev->in_sync = 0;
3938 mddev->changed = 0;
3939 mddev->degraded = 0;
3940 mddev->barriers_work = 0;
3941 mddev->safemode = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07003942
NeilBrowna8a55c32006-06-26 00:27:39 -07003943 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 printk(KERN_INFO "md: %s switched to read-only mode.\n",
3945 mdname(mddev));
3946 err = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08003947 md_new_event(mddev);
Neil Brown0fd62b82008-06-28 08:31:36 +10003948 sysfs_notify(&mddev->kobj, NULL, "array_state");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949out:
3950 return err;
3951}
3952
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08003953#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954static void autorun_array(mddev_t *mddev)
3955{
3956 mdk_rdev_t *rdev;
3957 struct list_head *tmp;
3958 int err;
3959
NeilBrowna757e642005-04-16 15:26:42 -07003960 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962
3963 printk(KERN_INFO "md: running: ");
3964
NeilBrownd089c6a2008-02-06 01:39:59 -08003965 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 char b[BDEVNAME_SIZE];
3967 printk("<%s>", bdevname(rdev->bdev,b));
3968 }
3969 printk("\n");
3970
3971 err = do_md_run (mddev);
3972 if (err) {
3973 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
Neil Browndf5b20c2008-07-11 22:02:22 +10003974 do_md_stop (mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
3976}
3977
3978/*
3979 * lets try to run arrays based on all disks that have arrived
3980 * until now. (those are in pending_raid_disks)
3981 *
3982 * the method: pick the first pending disk, collect all disks with
3983 * the same UUID, remove all from the pending list and put them into
3984 * the 'same_array' list. Then order this list based on superblock
3985 * update time (freshest comes first), kick out 'old' disks and
3986 * compare superblocks. If everything's fine then run it.
3987 *
3988 * If "unit" is allocated, then bump its reference count
3989 */
3990static void autorun_devices(int part)
3991{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 struct list_head *tmp;
3993 mdk_rdev_t *rdev0, *rdev;
3994 mddev_t *mddev;
3995 char b[BDEVNAME_SIZE];
3996
3997 printk(KERN_INFO "md: autorun ...\n");
3998 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07003999 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004001 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 rdev0 = list_entry(pending_raid_disks.next,
4003 mdk_rdev_t, same_set);
4004
4005 printk(KERN_INFO "md: considering %s ...\n",
4006 bdevname(rdev0->bdev,b));
4007 INIT_LIST_HEAD(&candidates);
NeilBrown73c34432008-02-06 01:39:59 -08004008 rdev_for_each_list(rdev, tmp, pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 if (super_90_load(rdev, rdev0, 0) >= 0) {
4010 printk(KERN_INFO "md: adding %s ...\n",
4011 bdevname(rdev->bdev,b));
4012 list_move(&rdev->same_set, &candidates);
4013 }
4014 /*
4015 * now we have a set of devices, with all of them having
4016 * mostly sane superblocks. It's time to allocate the
4017 * mddev.
4018 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004019 if (part) {
4020 dev = MKDEV(mdp_major,
4021 rdev0->preferred_minor << MdpMinorShift);
4022 unit = MINOR(dev) >> MdpMinorShift;
4023 } else {
4024 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4025 unit = MINOR(dev);
4026 }
4027 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4029 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4030 break;
4031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
4033 md_probe(dev, NULL, NULL);
4034 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004035 if (!mddev || !mddev->gendisk) {
4036 if (mddev)
4037 mddev_put(mddev);
4038 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 "md: cannot allocate memory for md drive.\n");
4040 break;
4041 }
4042 if (mddev_lock(mddev))
4043 printk(KERN_WARNING "md: %s locked, cannot run\n",
4044 mdname(mddev));
4045 else if (mddev->raid_disks || mddev->major_version
4046 || !list_empty(&mddev->disks)) {
4047 printk(KERN_WARNING
4048 "md: %s already running, cannot run %s\n",
4049 mdname(mddev), bdevname(rdev0->bdev,b));
4050 mddev_unlock(mddev);
4051 } else {
4052 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004053 mddev->persistent = 1;
NeilBrown73c34432008-02-06 01:39:59 -08004054 rdev_for_each_list(rdev, tmp, candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 list_del_init(&rdev->same_set);
4056 if (bind_rdev_to_array(rdev, mddev))
4057 export_rdev(rdev);
4058 }
4059 autorun_array(mddev);
4060 mddev_unlock(mddev);
4061 }
4062 /* on success, candidates will be empty, on error
4063 * it won't...
4064 */
NeilBrown73c34432008-02-06 01:39:59 -08004065 rdev_for_each_list(rdev, tmp, candidates)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 export_rdev(rdev);
4067 mddev_put(mddev);
4068 }
4069 printk(KERN_INFO "md: ... autorun DONE.\n");
4070}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004071#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073static int get_version(void __user * arg)
4074{
4075 mdu_version_t ver;
4076
4077 ver.major = MD_MAJOR_VERSION;
4078 ver.minor = MD_MINOR_VERSION;
4079 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4080
4081 if (copy_to_user(arg, &ver, sizeof(ver)))
4082 return -EFAULT;
4083
4084 return 0;
4085}
4086
4087static int get_array_info(mddev_t * mddev, void __user * arg)
4088{
4089 mdu_array_info_t info;
4090 int nr,working,active,failed,spare;
4091 mdk_rdev_t *rdev;
4092 struct list_head *tmp;
4093
4094 nr=working=active=failed=spare=0;
NeilBrownd089c6a2008-02-06 01:39:59 -08004095 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004097 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 failed++;
4099 else {
4100 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004101 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 active++;
4103 else
4104 spare++;
4105 }
4106 }
4107
4108 info.major_version = mddev->major_version;
4109 info.minor_version = mddev->minor_version;
4110 info.patch_version = MD_PATCHLEVEL_VERSION;
4111 info.ctime = mddev->ctime;
4112 info.level = mddev->level;
4113 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004114 if (info.size != mddev->size) /* overflow */
4115 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 info.nr_disks = nr;
4117 info.raid_disks = mddev->raid_disks;
4118 info.md_minor = mddev->md_minor;
4119 info.not_persistent= !mddev->persistent;
4120
4121 info.utime = mddev->utime;
4122 info.state = 0;
4123 if (mddev->in_sync)
4124 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004125 if (mddev->bitmap && mddev->bitmap_offset)
4126 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 info.active_disks = active;
4128 info.working_disks = working;
4129 info.failed_disks = failed;
4130 info.spare_disks = spare;
4131
4132 info.layout = mddev->layout;
4133 info.chunk_size = mddev->chunk_size;
4134
4135 if (copy_to_user(arg, &info, sizeof(info)))
4136 return -EFAULT;
4137
4138 return 0;
4139}
4140
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004141static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004142{
4143 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4144 char *ptr, *buf = NULL;
4145 int err = -ENOMEM;
4146
Dan Williamsb5470dc2008-06-27 21:44:04 -07004147 if (md_allow_write(mddev))
4148 file = kmalloc(sizeof(*file), GFP_NOIO);
4149 else
4150 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004151
NeilBrown32a76272005-06-21 17:17:14 -07004152 if (!file)
4153 goto out;
4154
4155 /* bitmap disabled, zero the first byte and copy out */
4156 if (!mddev->bitmap || !mddev->bitmap->file) {
4157 file->pathname[0] = '\0';
4158 goto copy_out;
4159 }
4160
4161 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4162 if (!buf)
4163 goto out;
4164
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004165 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4166 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004167 goto out;
4168
4169 strcpy(file->pathname, ptr);
4170
4171copy_out:
4172 err = 0;
4173 if (copy_to_user(arg, file, sizeof(*file)))
4174 err = -EFAULT;
4175out:
4176 kfree(buf);
4177 kfree(file);
4178 return err;
4179}
4180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181static int get_disk_info(mddev_t * mddev, void __user * arg)
4182{
4183 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 mdk_rdev_t *rdev;
4185
4186 if (copy_from_user(&info, arg, sizeof(info)))
4187 return -EFAULT;
4188
Andre Noll26ef3792008-07-11 22:02:21 +10004189 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 if (rdev) {
4191 info.major = MAJOR(rdev->bdev->bd_dev);
4192 info.minor = MINOR(rdev->bdev->bd_dev);
4193 info.raid_disk = rdev->raid_disk;
4194 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004195 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004197 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 info.state |= (1<<MD_DISK_ACTIVE);
4199 info.state |= (1<<MD_DISK_SYNC);
4200 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004201 if (test_bit(WriteMostly, &rdev->flags))
4202 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 } else {
4204 info.major = info.minor = 0;
4205 info.raid_disk = -1;
4206 info.state = (1<<MD_DISK_REMOVED);
4207 }
4208
4209 if (copy_to_user(arg, &info, sizeof(info)))
4210 return -EFAULT;
4211
4212 return 0;
4213}
4214
4215static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4216{
4217 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4218 mdk_rdev_t *rdev;
4219 dev_t dev = MKDEV(info->major,info->minor);
4220
4221 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4222 return -EOVERFLOW;
4223
4224 if (!mddev->raid_disks) {
4225 int err;
4226 /* expecting a device which has a superblock */
4227 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4228 if (IS_ERR(rdev)) {
4229 printk(KERN_WARNING
4230 "md: md_import_device returned %ld\n",
4231 PTR_ERR(rdev));
4232 return PTR_ERR(rdev);
4233 }
4234 if (!list_empty(&mddev->disks)) {
4235 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4236 mdk_rdev_t, same_set);
4237 int err = super_types[mddev->major_version]
4238 .load_super(rdev, rdev0, mddev->minor_version);
4239 if (err < 0) {
4240 printk(KERN_WARNING
4241 "md: %s has different UUID to %s\n",
4242 bdevname(rdev->bdev,b),
4243 bdevname(rdev0->bdev,b2));
4244 export_rdev(rdev);
4245 return -EINVAL;
4246 }
4247 }
4248 err = bind_rdev_to_array(rdev, mddev);
4249 if (err)
4250 export_rdev(rdev);
4251 return err;
4252 }
4253
4254 /*
4255 * add_new_disk can be used once the array is assembled
4256 * to add "hot spares". They must already have a superblock
4257 * written
4258 */
4259 if (mddev->pers) {
4260 int err;
4261 if (!mddev->pers->hot_add_disk) {
4262 printk(KERN_WARNING
4263 "%s: personality does not support diskops!\n",
4264 mdname(mddev));
4265 return -EINVAL;
4266 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004267 if (mddev->persistent)
4268 rdev = md_import_device(dev, mddev->major_version,
4269 mddev->minor_version);
4270 else
4271 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if (IS_ERR(rdev)) {
4273 printk(KERN_WARNING
4274 "md: md_import_device returned %ld\n",
4275 PTR_ERR(rdev));
4276 return PTR_ERR(rdev);
4277 }
NeilBrown41158c72005-06-21 17:17:25 -07004278 /* set save_raid_disk if appropriate */
4279 if (!mddev->persistent) {
4280 if (info->state & (1<<MD_DISK_SYNC) &&
4281 info->raid_disk < mddev->raid_disks)
4282 rdev->raid_disk = info->raid_disk;
4283 else
4284 rdev->raid_disk = -1;
4285 } else
4286 super_types[mddev->major_version].
4287 validate_super(mddev, rdev);
4288 rdev->saved_raid_disk = rdev->raid_disk;
4289
NeilBrownb2d444d2005-11-08 21:39:31 -08004290 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004291 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4292 set_bit(WriteMostly, &rdev->flags);
4293
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 rdev->raid_disk = -1;
4295 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004296 if (!err && !mddev->pers->hot_remove_disk) {
4297 /* If there is hot_add_disk but no hot_remove_disk
4298 * then added disks for geometry changes,
4299 * and should be added immediately.
4300 */
4301 super_types[mddev->major_version].
4302 validate_super(mddev, rdev);
4303 err = mddev->pers->hot_add_disk(mddev, rdev);
4304 if (err)
4305 unbind_rdev_from_array(rdev);
4306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 if (err)
4308 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004309 else
4310 sysfs_notify(&rdev->kobj, NULL, "state");
NeilBrownc3617772005-06-21 17:17:10 -07004311
NeilBrown17571282006-12-10 02:20:52 -08004312 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004313 if (mddev->degraded)
4314 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004315 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004316 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 return err;
4318 }
4319
4320 /* otherwise, add_new_disk is only allowed
4321 * for major_version==0 superblocks
4322 */
4323 if (mddev->major_version != 0) {
4324 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4325 mdname(mddev));
4326 return -EINVAL;
4327 }
4328
4329 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4330 int err;
4331 rdev = md_import_device (dev, -1, 0);
4332 if (IS_ERR(rdev)) {
4333 printk(KERN_WARNING
4334 "md: error, md_import_device() returned %ld\n",
4335 PTR_ERR(rdev));
4336 return PTR_ERR(rdev);
4337 }
4338 rdev->desc_nr = info->number;
4339 if (info->raid_disk < mddev->raid_disks)
4340 rdev->raid_disk = info->raid_disk;
4341 else
4342 rdev->raid_disk = -1;
4343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004345 if (info->state & (1<<MD_DISK_SYNC))
4346 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004348 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4349 set_bit(WriteMostly, &rdev->flags);
4350
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 if (!mddev->persistent) {
4352 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004353 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 } else
Andre Noll0f420352008-07-11 22:02:23 +10004355 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolle7debaa2008-07-11 22:02:23 +10004356 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
NeilBrown2bf071b2006-01-06 00:20:55 -08004358 err = bind_rdev_to_array(rdev, mddev);
4359 if (err) {
4360 export_rdev(rdev);
4361 return err;
4362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 }
4364
4365 return 0;
4366}
4367
4368static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4369{
4370 char b[BDEVNAME_SIZE];
4371 mdk_rdev_t *rdev;
4372
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 rdev = find_rdev(mddev, dev);
4374 if (!rdev)
4375 return -ENXIO;
4376
4377 if (rdev->raid_disk >= 0)
4378 goto busy;
4379
4380 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004381 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004382 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383
4384 return 0;
4385busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004386 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 bdevname(rdev->bdev,b), mdname(mddev));
4388 return -EBUSY;
4389}
4390
4391static int hot_add_disk(mddev_t * mddev, dev_t dev)
4392{
4393 char b[BDEVNAME_SIZE];
4394 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 mdk_rdev_t *rdev;
4396
4397 if (!mddev->pers)
4398 return -ENODEV;
4399
4400 if (mddev->major_version != 0) {
4401 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4402 " version-0 superblocks.\n",
4403 mdname(mddev));
4404 return -EINVAL;
4405 }
4406 if (!mddev->pers->hot_add_disk) {
4407 printk(KERN_WARNING
4408 "%s: personality does not support diskops!\n",
4409 mdname(mddev));
4410 return -EINVAL;
4411 }
4412
4413 rdev = md_import_device (dev, -1, 0);
4414 if (IS_ERR(rdev)) {
4415 printk(KERN_WARNING
4416 "md: error, md_import_device() returned %ld\n",
4417 PTR_ERR(rdev));
4418 return -EINVAL;
4419 }
4420
4421 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004422 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 else
Andre Noll0f420352008-07-11 22:02:23 +10004424 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425
Andre Nolle7debaa2008-07-11 22:02:23 +10004426 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
NeilBrownb2d444d2005-11-08 21:39:31 -08004428 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 printk(KERN_WARNING
4430 "md: can not hot-add faulty %s disk to %s!\n",
4431 bdevname(rdev->bdev,b), mdname(mddev));
4432 err = -EINVAL;
4433 goto abort_export;
4434 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004435 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004437 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004438 err = bind_rdev_to_array(rdev, mddev);
4439 if (err)
4440 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
4442 /*
4443 * The rest should better be atomic, we can have disk failures
4444 * noticed in interrupt contexts ...
4445 */
4446
4447 if (rdev->desc_nr == mddev->max_disks) {
4448 printk(KERN_WARNING "%s: can not hot-add to full array!\n",
4449 mdname(mddev));
4450 err = -EBUSY;
4451 goto abort_unbind_export;
4452 }
4453
4454 rdev->raid_disk = -1;
4455
NeilBrown850b2b42006-10-03 01:15:46 -07004456 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 /*
4459 * Kick recovery, maybe this spare has to be added to the
4460 * array immediately.
4461 */
4462 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4463 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004464 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return 0;
4466
4467abort_unbind_export:
4468 unbind_rdev_from_array(rdev);
4469
4470abort_export:
4471 export_rdev(rdev);
4472 return err;
4473}
4474
NeilBrown32a76272005-06-21 17:17:14 -07004475static int set_bitmap_file(mddev_t *mddev, int fd)
4476{
4477 int err;
4478
NeilBrown36fa3062005-09-09 16:23:45 -07004479 if (mddev->pers) {
4480 if (!mddev->pers->quiesce)
4481 return -EBUSY;
4482 if (mddev->recovery || mddev->sync_thread)
4483 return -EBUSY;
4484 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004485 }
4486
NeilBrown36fa3062005-09-09 16:23:45 -07004487
4488 if (fd >= 0) {
4489 if (mddev->bitmap)
4490 return -EEXIST; /* cannot add when bitmap is present */
4491 mddev->bitmap_file = fget(fd);
4492
4493 if (mddev->bitmap_file == NULL) {
4494 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4495 mdname(mddev));
4496 return -EBADF;
4497 }
4498
4499 err = deny_bitmap_write_access(mddev->bitmap_file);
4500 if (err) {
4501 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4502 mdname(mddev));
4503 fput(mddev->bitmap_file);
4504 mddev->bitmap_file = NULL;
4505 return err;
4506 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004507 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004508 } else if (mddev->bitmap == NULL)
4509 return -ENOENT; /* cannot remove what isn't there */
4510 err = 0;
4511 if (mddev->pers) {
4512 mddev->pers->quiesce(mddev, 1);
4513 if (fd >= 0)
4514 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004515 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004516 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07004517 fd = -1; /* make sure to put the file */
4518 }
NeilBrown36fa3062005-09-09 16:23:45 -07004519 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07004520 }
4521 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004522 if (mddev->bitmap_file) {
4523 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004524 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004525 }
NeilBrown36fa3062005-09-09 16:23:45 -07004526 mddev->bitmap_file = NULL;
4527 }
4528
NeilBrown32a76272005-06-21 17:17:14 -07004529 return err;
4530}
4531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532/*
4533 * set_array_info is used two different ways
4534 * The original usage is when creating a new array.
4535 * In this usage, raid_disks is > 0 and it together with
4536 * level, size, not_persistent,layout,chunksize determine the
4537 * shape of the array.
4538 * This will always create an array with a type-0.90.0 superblock.
4539 * The newer usage is when assembling an array.
4540 * In this case raid_disks will be 0, and the major_version field is
4541 * use to determine which style super-blocks are to be found on the devices.
4542 * The minor and patch _version numbers are also kept incase the
4543 * super_block handler wishes to interpret them.
4544 */
4545static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4546{
4547
4548 if (info->raid_disks == 0) {
4549 /* just setting version number for superblock loading */
4550 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004551 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 super_types[info->major_version].name == NULL) {
4553 /* maybe try to auto-load a module? */
4554 printk(KERN_INFO
4555 "md: superblock version %d not known\n",
4556 info->major_version);
4557 return -EINVAL;
4558 }
4559 mddev->major_version = info->major_version;
4560 mddev->minor_version = info->minor_version;
4561 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004562 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 return 0;
4564 }
4565 mddev->major_version = MD_MAJOR_VERSION;
4566 mddev->minor_version = MD_MINOR_VERSION;
4567 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4568 mddev->ctime = get_seconds();
4569
4570 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004571 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 mddev->size = info->size;
4573 mddev->raid_disks = info->raid_disks;
4574 /* don't set md_minor, it is determined by which /dev/md* was
4575 * openned
4576 */
4577 if (info->state & (1<<MD_SB_CLEAN))
4578 mddev->recovery_cp = MaxSector;
4579 else
4580 mddev->recovery_cp = 0;
4581 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004582 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583
4584 mddev->layout = info->layout;
4585 mddev->chunk_size = info->chunk_size;
4586
4587 mddev->max_disks = MD_SB_DISKS;
4588
NeilBrowne6910632008-02-06 01:39:51 -08004589 if (mddev->persistent)
4590 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004591 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
NeilBrownb2a27032005-11-28 13:44:12 -08004593 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4594 mddev->bitmap_offset = 0;
4595
NeilBrownf6705572006-03-27 01:18:11 -08004596 mddev->reshape_position = MaxSector;
4597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 /*
4599 * Generate a 128 bit UUID
4600 */
4601 get_random_bytes(mddev->uuid, 16);
4602
NeilBrownf6705572006-03-27 01:18:11 -08004603 mddev->new_level = mddev->level;
4604 mddev->new_chunk = mddev->chunk_size;
4605 mddev->new_layout = mddev->layout;
4606 mddev->delta_disks = 0;
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return 0;
4609}
4610
Andre Nolld71f9f82008-07-11 22:02:22 +10004611static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004612{
4613 mdk_rdev_t * rdev;
4614 int rv;
4615 struct list_head *tmp;
Andre Nolld71f9f82008-07-11 22:02:22 +10004616 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004617
4618 if (mddev->pers->resize == NULL)
4619 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10004620 /* The "num_sectors" is the number of sectors of each device that
4621 * is used. This can only make sense for arrays with redundancy.
4622 * linear and raid0 always use whatever space is available. We can only
4623 * consider changing this number if no resync or reconstruction is
4624 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10004625 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10004626 * of each device. If num_sectors is zero, we find the largest size
4627 * that fits.
4628
NeilBrowna35b0d62006-01-06 00:20:49 -08004629 */
4630 if (mddev->sync_thread)
4631 return -EBUSY;
NeilBrownd089c6a2008-02-06 01:39:59 -08004632 rdev_for_each(rdev, tmp, mddev) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004633 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004634 avail = rdev->size * 2;
4635
Andre Nolld71f9f82008-07-11 22:02:22 +10004636 if (fit && (num_sectors == 0 || num_sectors > avail))
4637 num_sectors = avail;
4638 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004639 return -ENOSPC;
4640 }
Andre Nolld71f9f82008-07-11 22:02:22 +10004641 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004642 if (!rv) {
4643 struct block_device *bdev;
4644
4645 bdev = bdget_disk(mddev->gendisk, 0);
4646 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004647 mutex_lock(&bdev->bd_inode->i_mutex);
NeilBrown6d893322006-02-02 14:28:03 -08004648 i_size_write(bdev->bd_inode, (loff_t)mddev->array_size << 10);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004649 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004650 bdput(bdev);
4651 }
4652 }
4653 return rv;
4654}
4655
NeilBrownda943b992006-01-06 00:20:54 -08004656static int update_raid_disks(mddev_t *mddev, int raid_disks)
4657{
4658 int rv;
4659 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004660 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004661 return -EINVAL;
4662 if (raid_disks <= 0 ||
4663 raid_disks >= mddev->max_disks)
4664 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004665 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004666 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004667 mddev->delta_disks = raid_disks - mddev->raid_disks;
4668
4669 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004670 return rv;
4671}
4672
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674/*
4675 * update_array_info is used to change the configuration of an
4676 * on-line array.
4677 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4678 * fields in the info are checked against the array.
4679 * Any differences that cannot be handled will cause an error.
4680 * Normally, only one change can be managed at a time.
4681 */
4682static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4683{
4684 int rv = 0;
4685 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004686 int state = 0;
4687
4688 /* calculate expected state,ignoring low bits */
4689 if (mddev->bitmap && mddev->bitmap_offset)
4690 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
4692 if (mddev->major_version != info->major_version ||
4693 mddev->minor_version != info->minor_version ||
4694/* mddev->patch_version != info->patch_version || */
4695 mddev->ctime != info->ctime ||
4696 mddev->level != info->level ||
4697/* mddev->layout != info->layout || */
4698 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004699 mddev->chunk_size != info->chunk_size ||
4700 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4701 ((state^info->state) & 0xfffffe00)
4702 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 return -EINVAL;
4704 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004705 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 if (mddev->raid_disks != info->raid_disks) cnt++;
4707 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004708 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 if (cnt == 0) return 0;
4710 if (cnt > 1) return -EINVAL;
4711
4712 if (mddev->layout != info->layout) {
4713 /* Change layout
4714 * we don't need to do anything at the md level, the
4715 * personality will take care of it all.
4716 */
4717 if (mddev->pers->reconfig == NULL)
4718 return -EINVAL;
4719 else
4720 return mddev->pers->reconfig(mddev, info->layout, -1);
4721 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004722 if (info->size >= 0 && mddev->size != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10004723 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724
NeilBrownda943b992006-01-06 00:20:54 -08004725 if (mddev->raid_disks != info->raid_disks)
4726 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727
NeilBrown36fa3062005-09-09 16:23:45 -07004728 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4729 if (mddev->pers->quiesce == NULL)
4730 return -EINVAL;
4731 if (mddev->recovery || mddev->sync_thread)
4732 return -EBUSY;
4733 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4734 /* add the bitmap */
4735 if (mddev->bitmap)
4736 return -EEXIST;
4737 if (mddev->default_bitmap_offset == 0)
4738 return -EINVAL;
4739 mddev->bitmap_offset = mddev->default_bitmap_offset;
4740 mddev->pers->quiesce(mddev, 1);
4741 rv = bitmap_create(mddev);
4742 if (rv)
4743 bitmap_destroy(mddev);
4744 mddev->pers->quiesce(mddev, 0);
4745 } else {
4746 /* remove the bitmap */
4747 if (!mddev->bitmap)
4748 return -ENOENT;
4749 if (mddev->bitmap->file)
4750 return -EINVAL;
4751 mddev->pers->quiesce(mddev, 1);
4752 bitmap_destroy(mddev);
4753 mddev->pers->quiesce(mddev, 0);
4754 mddev->bitmap_offset = 0;
4755 }
4756 }
NeilBrown850b2b42006-10-03 01:15:46 -07004757 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 return rv;
4759}
4760
4761static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4762{
4763 mdk_rdev_t *rdev;
4764
4765 if (mddev->pers == NULL)
4766 return -ENODEV;
4767
4768 rdev = find_rdev(mddev, dev);
4769 if (!rdev)
4770 return -ENODEV;
4771
4772 md_error(mddev, rdev);
4773 return 0;
4774}
4775
Andre Noll2f9618c2008-04-25 18:57:58 +02004776/*
4777 * We have a problem here : there is no easy way to give a CHS
4778 * virtual geometry. We currently pretend that we have a 2 heads
4779 * 4 sectors (with a BIG number of cylinders...). This drives
4780 * dosfs just mad... ;-)
4781 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004782static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4783{
4784 mddev_t *mddev = bdev->bd_disk->private_data;
4785
4786 geo->heads = 2;
4787 geo->sectors = 4;
4788 geo->cylinders = get_capacity(mddev->gendisk) / 8;
4789 return 0;
4790}
4791
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792static int md_ioctl(struct inode *inode, struct file *file,
4793 unsigned int cmd, unsigned long arg)
4794{
4795 int err = 0;
4796 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797 mddev_t *mddev = NULL;
4798
4799 if (!capable(CAP_SYS_ADMIN))
4800 return -EACCES;
4801
4802 /*
4803 * Commands dealing with the RAID driver but not any
4804 * particular array:
4805 */
4806 switch (cmd)
4807 {
4808 case RAID_VERSION:
4809 err = get_version(argp);
4810 goto done;
4811
4812 case PRINT_RAID_DEBUG:
4813 err = 0;
4814 md_print_devices();
4815 goto done;
4816
4817#ifndef MODULE
4818 case RAID_AUTORUN:
4819 err = 0;
4820 autostart_arrays(arg);
4821 goto done;
4822#endif
4823 default:;
4824 }
4825
4826 /*
4827 * Commands creating/starting a new array:
4828 */
4829
4830 mddev = inode->i_bdev->bd_disk->private_data;
4831
4832 if (!mddev) {
4833 BUG();
4834 goto abort;
4835 }
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 err = mddev_lock(mddev);
4838 if (err) {
4839 printk(KERN_INFO
4840 "md: ioctl lock interrupted, reason %d, cmd %d\n",
4841 err, cmd);
4842 goto abort;
4843 }
4844
4845 switch (cmd)
4846 {
4847 case SET_ARRAY_INFO:
4848 {
4849 mdu_array_info_t info;
4850 if (!arg)
4851 memset(&info, 0, sizeof(info));
4852 else if (copy_from_user(&info, argp, sizeof(info))) {
4853 err = -EFAULT;
4854 goto abort_unlock;
4855 }
4856 if (mddev->pers) {
4857 err = update_array_info(mddev, &info);
4858 if (err) {
4859 printk(KERN_WARNING "md: couldn't update"
4860 " array info. %d\n", err);
4861 goto abort_unlock;
4862 }
4863 goto done_unlock;
4864 }
4865 if (!list_empty(&mddev->disks)) {
4866 printk(KERN_WARNING
4867 "md: array %s already has disks!\n",
4868 mdname(mddev));
4869 err = -EBUSY;
4870 goto abort_unlock;
4871 }
4872 if (mddev->raid_disks) {
4873 printk(KERN_WARNING
4874 "md: array %s already initialised!\n",
4875 mdname(mddev));
4876 err = -EBUSY;
4877 goto abort_unlock;
4878 }
4879 err = set_array_info(mddev, &info);
4880 if (err) {
4881 printk(KERN_WARNING "md: couldn't set"
4882 " array info. %d\n", err);
4883 goto abort_unlock;
4884 }
4885 }
4886 goto done_unlock;
4887
4888 default:;
4889 }
4890
4891 /*
4892 * Commands querying/configuring an existing array:
4893 */
NeilBrown32a76272005-06-21 17:17:14 -07004894 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08004895 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08004896 if ((!mddev->raid_disks && !mddev->external)
4897 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
4898 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
4899 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900 err = -ENODEV;
4901 goto abort_unlock;
4902 }
4903
4904 /*
4905 * Commands even a read-only array can execute:
4906 */
4907 switch (cmd)
4908 {
4909 case GET_ARRAY_INFO:
4910 err = get_array_info(mddev, argp);
4911 goto done_unlock;
4912
NeilBrown32a76272005-06-21 17:17:14 -07004913 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004914 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07004915 goto done_unlock;
4916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 case GET_DISK_INFO:
4918 err = get_disk_info(mddev, argp);
4919 goto done_unlock;
4920
4921 case RESTART_ARRAY_RW:
4922 err = restart_array(mddev);
4923 goto done_unlock;
4924
4925 case STOP_ARRAY:
Neil Browndf5b20c2008-07-11 22:02:22 +10004926 err = do_md_stop (mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 goto done_unlock;
4928
4929 case STOP_ARRAY_RO:
Neil Browndf5b20c2008-07-11 22:02:22 +10004930 err = do_md_stop (mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 goto done_unlock;
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 }
4934
4935 /*
4936 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08004937 * superblock, so we do not allow them on read-only arrays.
4938 * However non-MD ioctls (e.g. get-size) will still come through
4939 * here and hit the 'default' below, so only disallow
4940 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004941 */
Andre Nollbb57fc62008-04-25 19:06:35 +02004942 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08004943 if (mddev->ro == 2) {
4944 mddev->ro = 0;
Neil Brown0fd62b82008-06-28 08:31:36 +10004945 sysfs_notify(&mddev->kobj, NULL, "array_state");
4946 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4947 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08004948 } else {
4949 err = -EROFS;
4950 goto abort_unlock;
4951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 }
4953
4954 switch (cmd)
4955 {
4956 case ADD_NEW_DISK:
4957 {
4958 mdu_disk_info_t info;
4959 if (copy_from_user(&info, argp, sizeof(info)))
4960 err = -EFAULT;
4961 else
4962 err = add_new_disk(mddev, &info);
4963 goto done_unlock;
4964 }
4965
4966 case HOT_REMOVE_DISK:
4967 err = hot_remove_disk(mddev, new_decode_dev(arg));
4968 goto done_unlock;
4969
4970 case HOT_ADD_DISK:
4971 err = hot_add_disk(mddev, new_decode_dev(arg));
4972 goto done_unlock;
4973
4974 case SET_DISK_FAULTY:
4975 err = set_disk_faulty(mddev, new_decode_dev(arg));
4976 goto done_unlock;
4977
4978 case RUN_ARRAY:
4979 err = do_md_run (mddev);
4980 goto done_unlock;
4981
NeilBrown32a76272005-06-21 17:17:14 -07004982 case SET_BITMAP_FILE:
4983 err = set_bitmap_file(mddev, (int)arg);
4984 goto done_unlock;
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 err = -EINVAL;
4988 goto abort_unlock;
4989 }
4990
4991done_unlock:
4992abort_unlock:
4993 mddev_unlock(mddev);
4994
4995 return err;
4996done:
4997 if (err)
4998 MD_BUG();
4999abort:
5000 return err;
5001}
5002
5003static int md_open(struct inode *inode, struct file *file)
5004{
5005 /*
5006 * Succeed if we can lock the mddev, which confirms that
5007 * it isn't being stopped right now.
5008 */
5009 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5010 int err;
5011
NeilBrownd63a5a72006-12-08 02:36:17 -08005012 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 goto out;
5014
5015 err = 0;
5016 mddev_get(mddev);
5017 mddev_unlock(mddev);
5018
5019 check_disk_change(inode->i_bdev);
5020 out:
5021 return err;
5022}
5023
5024static int md_release(struct inode *inode, struct file * file)
5025{
5026 mddev_t *mddev = inode->i_bdev->bd_disk->private_data;
5027
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005028 BUG_ON(!mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029 mddev_put(mddev);
5030
5031 return 0;
5032}
5033
Linus Torvalds44ce6292007-05-09 18:51:36 -07005034static int md_media_changed(struct gendisk *disk)
5035{
5036 mddev_t *mddev = disk->private_data;
5037
5038 return mddev->changed;
5039}
5040
5041static int md_revalidate(struct gendisk *disk)
5042{
5043 mddev_t *mddev = disk->private_data;
5044
5045 mddev->changed = 0;
5046 return 0;
5047}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048static struct block_device_operations md_fops =
5049{
5050 .owner = THIS_MODULE,
5051 .open = md_open,
5052 .release = md_release,
5053 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005054 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005055 .media_changed = md_media_changed,
5056 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057};
5058
Adrian Bunk75c96f82005-05-05 16:16:09 -07005059static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060{
5061 mdk_thread_t *thread = arg;
5062
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 /*
5064 * md_thread is a 'system-thread', it's priority should be very
5065 * high. We avoid resource deadlocks individually in each
5066 * raid personality. (RAID5 does preallocation) We also use RR and
5067 * the very same RT priority as kswapd, thus we will never get
5068 * into a priority inversion deadlock.
5069 *
5070 * we definitely have to have equal or higher priority than
5071 * bdflush, otherwise bdflush will deadlock if there are too
5072 * many dirty RAID5 blocks.
5073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
NeilBrown6985c432005-10-19 21:23:47 -07005075 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005076 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
NeilBrown93588e22005-11-15 00:09:12 -08005078 /* We need to wait INTERRUPTIBLE so that
5079 * we don't add to the load-average.
5080 * That means we need to be sure no signals are
5081 * pending
5082 */
5083 if (signal_pending(current))
5084 flush_signals(current);
5085
5086 wait_event_interruptible_timeout
5087 (thread->wqueue,
5088 test_bit(THREAD_WAKEUP, &thread->flags)
5089 || kthread_should_stop(),
5090 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091
5092 clear_bit(THREAD_WAKEUP, &thread->flags);
5093
NeilBrown787453c2005-11-08 21:39:43 -08005094 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005096
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 return 0;
5098}
5099
5100void md_wakeup_thread(mdk_thread_t *thread)
5101{
5102 if (thread) {
5103 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5104 set_bit(THREAD_WAKEUP, &thread->flags);
5105 wake_up(&thread->wqueue);
5106 }
5107}
5108
5109mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5110 const char *name)
5111{
5112 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
NeilBrown9ffae0c2006-01-06 00:20:32 -08005114 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 if (!thread)
5116 return NULL;
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 init_waitqueue_head(&thread->wqueue);
5119
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 thread->run = run;
5121 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005122 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005123 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005124 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 kfree(thread);
5126 return NULL;
5127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 return thread;
5129}
5130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131void md_unregister_thread(mdk_thread_t *thread)
5132{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005133 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005134
5135 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 kfree(thread);
5137}
5138
5139void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5140{
5141 if (!mddev) {
5142 MD_BUG();
5143 return;
5144 }
5145
NeilBrownb2d444d2005-11-08 21:39:31 -08005146 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005148
5149 if (mddev->external)
5150 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005151/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5153 mdname(mddev),
5154 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5155 __builtin_return_address(0),__builtin_return_address(1),
5156 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005157*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005158 if (!mddev->pers)
5159 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 if (!mddev->pers->error_handler)
5161 return;
5162 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005163 if (mddev->degraded)
5164 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005165 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5167 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5168 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005169 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170}
5171
5172/* seq_file implementation /proc/mdstat */
5173
5174static void status_unused(struct seq_file *seq)
5175{
5176 int i = 0;
5177 mdk_rdev_t *rdev;
5178 struct list_head *tmp;
5179
5180 seq_printf(seq, "unused devices: ");
5181
NeilBrown73c34432008-02-06 01:39:59 -08005182 rdev_for_each_list(rdev, tmp, pending_raid_disks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 char b[BDEVNAME_SIZE];
5184 i++;
5185 seq_printf(seq, "%s ",
5186 bdevname(rdev->bdev,b));
5187 }
5188 if (!i)
5189 seq_printf(seq, "<none>");
5190
5191 seq_printf(seq, "\n");
5192}
5193
5194
5195static void status_resync(struct seq_file *seq, mddev_t * mddev)
5196{
NeilBrown4588b422006-03-27 01:18:04 -08005197 sector_t max_blocks, resync, res;
5198 unsigned long dt, db, rt;
5199 int scale;
5200 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
5202 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5203
5204 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5205 max_blocks = mddev->resync_max_sectors >> 1;
5206 else
5207 max_blocks = mddev->size;
5208
5209 /*
5210 * Should not happen.
5211 */
5212 if (!max_blocks) {
5213 MD_BUG();
5214 return;
5215 }
NeilBrown4588b422006-03-27 01:18:04 -08005216 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5217 * in a sector_t, and (max_blocks>>scale) will fit in a
5218 * u32, as those are the requirements for sector_div.
5219 * Thus 'scale' must be at least 10
5220 */
5221 scale = 10;
5222 if (sizeof(sector_t) > sizeof(unsigned long)) {
5223 while ( max_blocks/2 > (1ULL<<(scale+32)))
5224 scale++;
5225 }
5226 res = (resync>>scale)*1000;
5227 sector_div(res, (u32)((max_blocks>>scale)+1));
5228
5229 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 {
NeilBrown4588b422006-03-27 01:18:04 -08005231 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 seq_printf(seq, "[");
5233 for (i = 0; i < x; i++)
5234 seq_printf(seq, "=");
5235 seq_printf(seq, ">");
5236 for (i = 0; i < y; i++)
5237 seq_printf(seq, ".");
5238 seq_printf(seq, "] ");
5239 }
NeilBrown4588b422006-03-27 01:18:04 -08005240 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005241 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5242 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005243 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5244 "check" :
5245 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5246 "resync" : "recovery"))),
5247 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005248 (unsigned long long) resync,
5249 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
5251 /*
5252 * We do not want to overflow, so the order of operands and
5253 * the * 100 / 100 trick are important. We do a +1 to be
5254 * safe against division by zero. We only estimate anyway.
5255 *
5256 * dt: time from mark until now
5257 * db: blocks written from mark until now
5258 * rt: remaining time
5259 */
5260 dt = ((jiffies - mddev->resync_mark) / HZ);
5261 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005262 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5263 - mddev->resync_mark_cnt;
5264 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265
5266 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5267
NeilBrownff4e8d92006-07-10 04:44:16 -07005268 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269}
5270
5271static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5272{
5273 struct list_head *tmp;
5274 loff_t l = *pos;
5275 mddev_t *mddev;
5276
5277 if (l >= 0x10000)
5278 return NULL;
5279 if (!l--)
5280 /* header */
5281 return (void*)1;
5282
5283 spin_lock(&all_mddevs_lock);
5284 list_for_each(tmp,&all_mddevs)
5285 if (!l--) {
5286 mddev = list_entry(tmp, mddev_t, all_mddevs);
5287 mddev_get(mddev);
5288 spin_unlock(&all_mddevs_lock);
5289 return mddev;
5290 }
5291 spin_unlock(&all_mddevs_lock);
5292 if (!l--)
5293 return (void*)2;/* tail */
5294 return NULL;
5295}
5296
5297static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5298{
5299 struct list_head *tmp;
5300 mddev_t *next_mddev, *mddev = v;
5301
5302 ++*pos;
5303 if (v == (void*)2)
5304 return NULL;
5305
5306 spin_lock(&all_mddevs_lock);
5307 if (v == (void*)1)
5308 tmp = all_mddevs.next;
5309 else
5310 tmp = mddev->all_mddevs.next;
5311 if (tmp != &all_mddevs)
5312 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5313 else {
5314 next_mddev = (void*)2;
5315 *pos = 0x10000;
5316 }
5317 spin_unlock(&all_mddevs_lock);
5318
5319 if (v != (void*)1)
5320 mddev_put(mddev);
5321 return next_mddev;
5322
5323}
5324
5325static void md_seq_stop(struct seq_file *seq, void *v)
5326{
5327 mddev_t *mddev = v;
5328
5329 if (mddev && v != (void*)1 && v != (void*)2)
5330 mddev_put(mddev);
5331}
5332
NeilBrownd7603b72006-01-06 00:20:30 -08005333struct mdstat_info {
5334 int event;
5335};
5336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337static int md_seq_show(struct seq_file *seq, void *v)
5338{
5339 mddev_t *mddev = v;
5340 sector_t size;
5341 struct list_head *tmp2;
5342 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005343 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005344 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
5346 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005347 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 seq_printf(seq, "Personalities : ");
5349 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005350 list_for_each_entry(pers, &pers_list, list)
5351 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
5353 spin_unlock(&pers_lock);
5354 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005355 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 return 0;
5357 }
5358 if (v == (void*)2) {
5359 status_unused(seq);
5360 return 0;
5361 }
5362
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005363 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005365
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5367 seq_printf(seq, "%s : %sactive", mdname(mddev),
5368 mddev->pers ? "" : "in");
5369 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005370 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005372 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005373 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 seq_printf(seq, " %s", mddev->pers->name);
5375 }
5376
5377 size = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08005378 rdev_for_each(rdev, tmp2, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 char b[BDEVNAME_SIZE];
5380 seq_printf(seq, " %s[%d]",
5381 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005382 if (test_bit(WriteMostly, &rdev->flags))
5383 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005384 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 seq_printf(seq, "(F)");
5386 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005387 } else if (rdev->raid_disk < 0)
5388 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 size += rdev->size;
5390 }
5391
5392 if (!list_empty(&mddev->disks)) {
5393 if (mddev->pers)
5394 seq_printf(seq, "\n %llu blocks",
5395 (unsigned long long)mddev->array_size);
5396 else
5397 seq_printf(seq, "\n %llu blocks",
5398 (unsigned long long)size);
5399 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005400 if (mddev->persistent) {
5401 if (mddev->major_version != 0 ||
5402 mddev->minor_version != 90) {
5403 seq_printf(seq," super %d.%d",
5404 mddev->major_version,
5405 mddev->minor_version);
5406 }
NeilBrowne6910632008-02-06 01:39:51 -08005407 } else if (mddev->external)
5408 seq_printf(seq, " super external:%s",
5409 mddev->metadata_type);
5410 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005411 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412
5413 if (mddev->pers) {
5414 mddev->pers->status (seq, mddev);
5415 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005416 if (mddev->pers->sync_request) {
5417 if (mddev->curr_resync > 2) {
5418 status_resync (seq, mddev);
5419 seq_printf(seq, "\n ");
5420 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5421 seq_printf(seq, "\tresync=DELAYED\n ");
5422 else if (mddev->recovery_cp < MaxSector)
5423 seq_printf(seq, "\tresync=PENDING\n ");
5424 }
NeilBrown32a76272005-06-21 17:17:14 -07005425 } else
5426 seq_printf(seq, "\n ");
5427
5428 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005429 unsigned long chunk_kb;
5430 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005431 spin_lock_irqsave(&bitmap->lock, flags);
5432 chunk_kb = bitmap->chunksize >> 10;
5433 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5434 "%lu%s chunk",
5435 bitmap->pages - bitmap->missing_pages,
5436 bitmap->pages,
5437 (bitmap->pages - bitmap->missing_pages)
5438 << (PAGE_SHIFT - 10),
5439 chunk_kb ? chunk_kb : bitmap->chunksize,
5440 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005441 if (bitmap->file) {
5442 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005443 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005444 }
NeilBrown78d742d2005-06-21 17:17:15 -07005445
NeilBrown32a76272005-06-21 17:17:14 -07005446 seq_printf(seq, "\n");
5447 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 }
5449
5450 seq_printf(seq, "\n");
5451 }
5452 mddev_unlock(mddev);
5453
5454 return 0;
5455}
5456
5457static struct seq_operations md_seq_ops = {
5458 .start = md_seq_start,
5459 .next = md_seq_next,
5460 .stop = md_seq_stop,
5461 .show = md_seq_show,
5462};
5463
5464static int md_seq_open(struct inode *inode, struct file *file)
5465{
5466 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005467 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5468 if (mi == NULL)
5469 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470
5471 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005472 if (error)
5473 kfree(mi);
5474 else {
5475 struct seq_file *p = file->private_data;
5476 p->private = mi;
5477 mi->event = atomic_read(&md_event_count);
5478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 return error;
5480}
5481
NeilBrownd7603b72006-01-06 00:20:30 -08005482static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5483{
5484 struct seq_file *m = filp->private_data;
5485 struct mdstat_info *mi = m->private;
5486 int mask;
5487
5488 poll_wait(filp, &md_event_waiters, wait);
5489
5490 /* always allow read */
5491 mask = POLLIN | POLLRDNORM;
5492
5493 if (mi->event != atomic_read(&md_event_count))
5494 mask |= POLLERR | POLLPRI;
5495 return mask;
5496}
5497
Arjan van de Venfa027c22007-02-12 00:55:33 -08005498static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005499 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 .open = md_seq_open,
5501 .read = seq_read,
5502 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005503 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005504 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505};
5506
NeilBrown2604b702006-01-06 00:20:36 -08005507int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005510 list_add_tail(&p->list, &pers_list);
5511 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 spin_unlock(&pers_lock);
5513 return 0;
5514}
5515
NeilBrown2604b702006-01-06 00:20:36 -08005516int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517{
NeilBrown2604b702006-01-06 00:20:36 -08005518 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005520 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 spin_unlock(&pers_lock);
5522 return 0;
5523}
5524
5525static int is_mddev_idle(mddev_t *mddev)
5526{
5527 mdk_rdev_t * rdev;
5528 struct list_head *tmp;
5529 int idle;
NeilBrown713f6ab2007-07-17 04:06:12 -07005530 long curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
5532 idle = 1;
NeilBrownd089c6a2008-02-06 01:39:59 -08005533 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
Jens Axboea3623572005-11-01 09:26:16 +01005535 curr_events = disk_stat_read(disk, sectors[0]) +
5536 disk_stat_read(disk, sectors[1]) -
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005538 /* sync IO will cause sync_io to increase before the disk_stats
5539 * as sync_io is counted when a request starts, and
5540 * disk_stats is counted when it completes.
5541 * So resync activity will cause curr_events to be smaller than
5542 * when there was no such activity.
5543 * non-sync IO will cause disk_stat to increase without
5544 * increasing sync_io so curr_events will (eventually)
5545 * be larger than it was before. Once it becomes
5546 * substantially larger, the test below will cause
5547 * the array to appear non-idle, and resync will slow
5548 * down.
5549 * If there is a lot of outstanding resync activity when
5550 * we set last_event to curr_events, then all that activity
5551 * completing might cause the array to appear non-idle
5552 * and resync will be slowed down even though there might
5553 * not have been non-resync activity. This will only
5554 * happen once though. 'last_events' will soon reflect
5555 * the state where there is little or no outstanding
5556 * resync requests, and further resync activity will
5557 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005558 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 */
NeilBrown713f6ab2007-07-17 04:06:12 -07005560 if (curr_events - rdev->last_events > 4096) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 rdev->last_events = curr_events;
5562 idle = 0;
5563 }
5564 }
5565 return idle;
5566}
5567
5568void md_done_sync(mddev_t *mddev, int blocks, int ok)
5569{
5570 /* another "blocks" (512byte) blocks have been synced */
5571 atomic_sub(blocks, &mddev->recovery_active);
5572 wake_up(&mddev->recovery_wait);
5573 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005574 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 md_wakeup_thread(mddev->thread);
5576 // stop recovery, signal do_sync ....
5577 }
5578}
5579
5580
NeilBrown06d91a52005-06-21 17:17:12 -07005581/* md_write_start(mddev, bi)
5582 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005583 * in superblock) before writing, schedule a superblock update
5584 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005585 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005586void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005587{
Neil Brown0fd62b82008-06-28 08:31:36 +10005588 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005589 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005590 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005591
NeilBrownf91de922005-11-08 21:39:36 -08005592 BUG_ON(mddev->ro == 1);
5593 if (mddev->ro == 2) {
5594 /* need to switch to read/write */
5595 mddev->ro = 0;
5596 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5597 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005598 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005599 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005600 }
NeilBrown06d91a52005-06-21 17:17:12 -07005601 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005602 if (mddev->safemode == 1)
5603 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005604 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005605 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005606 if (mddev->in_sync) {
5607 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005608 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005609 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005610 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005611 }
NeilBrowna9701a32005-11-08 21:39:34 -08005612 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005613 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005614 if (did_change)
5615 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown09a44cc2008-05-23 13:04:36 -07005616 wait_event(mddev->sb_wait,
5617 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5618 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619}
5620
5621void md_write_end(mddev_t *mddev)
5622{
5623 if (atomic_dec_and_test(&mddev->writes_pending)) {
5624 if (mddev->safemode == 2)
5625 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005626 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5628 }
5629}
5630
NeilBrown2a2275d2007-01-26 00:57:11 -08005631/* md_allow_write(mddev)
5632 * Calling this ensures that the array is marked 'active' so that writes
5633 * may proceed without blocking. It is important to call this before
5634 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5635 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005636 *
5637 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5638 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005639 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005640int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005641{
5642 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005643 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005644 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005645 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005646 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005647 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005648
5649 spin_lock_irq(&mddev->write_lock);
5650 if (mddev->in_sync) {
5651 mddev->in_sync = 0;
5652 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5653 if (mddev->safemode_delay &&
5654 mddev->safemode == 0)
5655 mddev->safemode = 1;
5656 spin_unlock_irq(&mddev->write_lock);
5657 md_update_sb(mddev, 0);
NeilBrown09a44cc2008-05-23 13:04:36 -07005658 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrown2a2275d2007-01-26 00:57:11 -08005659 } else
5660 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005661
5662 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5663 return -EAGAIN;
5664 else
5665 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005666}
5667EXPORT_SYMBOL_GPL(md_allow_write);
5668
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669#define SYNC_MARKS 10
5670#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005671void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672{
5673 mddev_t *mddev2;
5674 unsigned int currspeed = 0,
5675 window;
NeilBrown57afd892005-06-21 17:17:13 -07005676 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 unsigned long mark[SYNC_MARKS];
5678 sector_t mark_cnt[SYNC_MARKS];
5679 int last_mark,m;
5680 struct list_head *tmp;
5681 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005682 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005683 struct list_head *rtmp;
5684 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005685 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686
5687 /* just incase thread restarts... */
5688 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5689 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005690 if (mddev->ro) /* never try to sync a read-only array */
5691 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692
NeilBrown61df9d92006-10-03 01:15:57 -07005693 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5694 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5695 desc = "data-check";
5696 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5697 desc = "requested-resync";
5698 else
5699 desc = "resync";
5700 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5701 desc = "reshape";
5702 else
5703 desc = "recovery";
5704
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 /* we overload curr_resync somewhat here.
5706 * 0 == not engaged in resync at all
5707 * 2 == checking that there is no conflict with another sync
5708 * 1 == like 2, but have yielded to allow conflicting resync to
5709 * commense
5710 * other == active in resync - this many blocks
5711 *
5712 * Before starting a resync we must have set curr_resync to
5713 * 2, and then checked that every "conflicting" array has curr_resync
5714 * less than ours. When we find one that is the same or higher
5715 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5716 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5717 * This will mean we have to start checking from the beginning again.
5718 *
5719 */
5720
5721 do {
5722 mddev->curr_resync = 2;
5723
5724 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005725 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005726 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 goto skip;
5728 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005729 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 if (mddev2 == mddev)
5731 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005732 if (!mddev->parallel_resync
5733 && mddev2->curr_resync
5734 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 DEFINE_WAIT(wq);
5736 if (mddev < mddev2 && mddev->curr_resync == 2) {
5737 /* arbitrarily yield */
5738 mddev->curr_resync = 1;
5739 wake_up(&resync_wait);
5740 }
5741 if (mddev > mddev2 && mddev->curr_resync == 1)
5742 /* no need to wait here, we can wait the next
5743 * time 'round when curr_resync == 2
5744 */
5745 continue;
NeilBrown787453c2005-11-08 21:39:43 -08005746 prepare_to_wait(&resync_wait, &wq, TASK_UNINTERRUPTIBLE);
5747 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005748 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005749 printk(KERN_INFO "md: delaying %s of %s"
5750 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005752 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 mddev_put(mddev2);
5754 schedule();
5755 finish_wait(&resync_wait, &wq);
5756 goto try_again;
5757 }
5758 finish_wait(&resync_wait, &wq);
5759 }
5760 }
5761 } while (mddev->curr_resync < 2);
5762
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005763 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08005764 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07005766 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 */
5768 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08005769 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005770 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10005771 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5772 j = mddev->resync_min;
5773 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005774 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10005775
NeilBrownccfcc3c2006-03-27 01:18:09 -08005776 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5777 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005778 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 /* recovery follows the physical size of devices */
5780 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005781 j = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005782 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005783 if (rdev->raid_disk >= 0 &&
5784 !test_bit(Faulty, &rdev->flags) &&
5785 !test_bit(In_sync, &rdev->flags) &&
5786 rdev->recovery_offset < j)
5787 j = rdev->recovery_offset;
5788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
NeilBrown61df9d92006-10-03 01:15:57 -07005790 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
5791 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
5792 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07005793 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07005794 "(but not more than %d KB/sec) for %s.\n",
5795 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796
5797 is_mddev_idle(mddev); /* this also initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005798
NeilBrown57afd892005-06-21 17:17:13 -07005799 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 for (m = 0; m < SYNC_MARKS; m++) {
5801 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005802 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 }
5804 last_mark = 0;
5805 mddev->resync_mark = mark[last_mark];
5806 mddev->resync_mark_cnt = mark_cnt[last_mark];
5807
5808 /*
5809 * Tune reconstruction:
5810 */
5811 window = 32*(PAGE_SIZE/512);
5812 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
5813 window/2,(unsigned long long) max_sectors/2);
5814
5815 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816 last_check = 0;
5817
5818 if (j>2) {
5819 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005820 "md: resuming %s of %s from checkpoint.\n",
5821 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 mddev->curr_resync = j;
5823 }
5824
5825 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07005826 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005827
NeilBrown57afd892005-06-21 17:17:13 -07005828 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005829 if (j >= mddev->resync_max) {
5830 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
5831 wait_event(mddev->recovery_wait,
5832 mddev->resync_max > j
5833 || kthread_should_stop());
5834 }
5835 if (kthread_should_stop())
5836 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07005837 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08005838 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07005839 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07005840 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841 goto out;
5842 }
NeilBrown57afd892005-06-21 17:17:13 -07005843
5844 if (!skipped) { /* actual IO requested */
5845 io_sectors += sectors;
5846 atomic_add(sectors, &mddev->recovery_active);
5847 }
5848
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849 j += sectors;
5850 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07005851 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08005852 if (last_check == 0)
5853 /* this is the earliers that rebuilt will be
5854 * visible in /proc/mdstat
5855 */
5856 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07005857
5858 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 continue;
5860
NeilBrown57afd892005-06-21 17:17:13 -07005861 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005862
NeilBrowndfc70642008-05-23 13:04:39 -07005863 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 break;
5865
5866 repeat:
5867 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
5868 /* step marks */
5869 int next = (last_mark+1) % SYNC_MARKS;
5870
5871 mddev->resync_mark = mark[next];
5872 mddev->resync_mark_cnt = mark_cnt[next];
5873 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07005874 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 last_mark = next;
5876 }
5877
5878
NeilBrownc6207272008-02-06 01:39:52 -08005879 if (kthread_should_stop())
5880 goto interrupted;
5881
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
5883 /*
5884 * this loop exits only if either when we are slower than
5885 * the 'hard' speed limit, or the system was IO-idle for
5886 * a jiffy.
5887 * the system might be non-idle CPU-wise, but we only care
5888 * about not overloading the IO subsystem. (things like an
5889 * e2fsck being done on the RAID array should execute fast)
5890 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005891 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 cond_resched();
5893
NeilBrown57afd892005-06-21 17:17:13 -07005894 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
5895 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896
NeilBrown88202a02006-01-06 00:21:36 -08005897 if (currspeed > speed_min(mddev)) {
5898 if ((currspeed > speed_max(mddev)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 !is_mddev_idle(mddev)) {
NeilBrownc0e48522005-11-18 01:11:01 -08005900 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 goto repeat;
5902 }
5903 }
5904 }
NeilBrown61df9d92006-10-03 01:15:57 -07005905 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906 /*
5907 * this also signals 'finished resyncing' to md_stop
5908 */
5909 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05005910 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911
5912 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
5913
5914 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07005915 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
NeilBrowndfc70642008-05-23 13:04:39 -07005917 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005918 mddev->curr_resync > 2) {
5919 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5920 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5921 if (mddev->curr_resync >= mddev->recovery_cp) {
5922 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07005923 "md: checkpointing %s of %s.\n",
5924 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005925 mddev->recovery_cp = mddev->curr_resync;
5926 }
5927 } else
5928 mddev->recovery_cp = MaxSector;
5929 } else {
5930 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5931 mddev->curr_resync = MaxSector;
NeilBrownd089c6a2008-02-06 01:39:59 -08005932 rdev_for_each(rdev, rtmp, mddev)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005933 if (rdev->raid_disk >= 0 &&
5934 !test_bit(Faulty, &rdev->flags) &&
5935 !test_bit(In_sync, &rdev->flags) &&
5936 rdev->recovery_offset < mddev->curr_resync)
5937 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939 }
NeilBrown17571282006-12-10 02:20:52 -08005940 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 skip:
5943 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10005944 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08005945 mddev->resync_max = MaxSector;
5946 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 wake_up(&resync_wait);
5948 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
5949 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08005950 return;
5951
5952 interrupted:
5953 /*
5954 * got a signal, exit.
5955 */
5956 printk(KERN_INFO
5957 "md: md_do_sync() got signal ... exiting\n");
5958 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5959 goto out;
5960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961}
NeilBrown29269552006-03-27 01:18:10 -08005962EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005963
5964
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005965static int remove_and_add_spares(mddev_t *mddev)
5966{
5967 mdk_rdev_t *rdev;
5968 struct list_head *rtmp;
5969 int spares = 0;
5970
NeilBrownd089c6a2008-02-06 01:39:59 -08005971 rdev_for_each(rdev, rtmp, mddev)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005972 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07005973 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005974 (test_bit(Faulty, &rdev->flags) ||
5975 ! test_bit(In_sync, &rdev->flags)) &&
5976 atomic_read(&rdev->nr_pending)==0) {
5977 if (mddev->pers->hot_remove_disk(
5978 mddev, rdev->raid_disk)==0) {
5979 char nm[20];
5980 sprintf(nm,"rd%d", rdev->raid_disk);
5981 sysfs_remove_link(&mddev->kobj, nm);
5982 rdev->raid_disk = -1;
5983 }
5984 }
5985
5986 if (mddev->degraded) {
NeilBrowndfc70642008-05-23 13:04:39 -07005987 rdev_for_each(rdev, rtmp, mddev) {
5988 if (rdev->raid_disk >= 0 &&
5989 !test_bit(In_sync, &rdev->flags))
5990 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005991 if (rdev->raid_disk < 0
5992 && !test_bit(Faulty, &rdev->flags)) {
5993 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10005994 if (mddev->pers->
5995 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08005996 char nm[20];
5997 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08005998 if (sysfs_create_link(&mddev->kobj,
5999 &rdev->kobj, nm))
6000 printk(KERN_WARNING
6001 "md: cannot register "
6002 "%s for %s\n",
6003 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006004 spares++;
6005 md_new_event(mddev);
6006 } else
6007 break;
6008 }
NeilBrowndfc70642008-05-23 13:04:39 -07006009 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006010 }
6011 return spares;
6012}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013/*
6014 * This routine is regularly called by all per-raid-array threads to
6015 * deal with generic issues like resync and super-block update.
6016 * Raid personalities that don't have a thread (linear/raid0) do not
6017 * need this as they never do any recovery or update the superblock.
6018 *
6019 * It does not do any resync itself, but rather "forks" off other threads
6020 * to do that as needed.
6021 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6022 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006023 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 * and wakeups up this thread which will reap the thread and finish up.
6025 * This thread also removes any faulty devices (with nr_pending == 0).
6026 *
6027 * The overall approach is:
6028 * 1/ if the superblock needs updating, update it.
6029 * 2/ If a recovery thread is running, don't do anything else.
6030 * 3/ If recovery has finished, clean up, possibly marking spares active.
6031 * 4/ If there are any faulty devices, remove them.
6032 * 5/ If array is degraded, try to add spares devices
6033 * 6/ If array has spares or is not in-sync, start a resync thread.
6034 */
6035void md_check_recovery(mddev_t *mddev)
6036{
6037 mdk_rdev_t *rdev;
6038 struct list_head *rtmp;
6039
6040
NeilBrown5f404022005-06-21 17:17:16 -07006041 if (mddev->bitmap)
6042 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043
6044 if (mddev->ro)
6045 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006046
6047 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006048 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006049 printk(KERN_INFO "md: %s in immediate safe mode\n",
6050 mdname(mddev));
6051 mddev->safemode = 2;
6052 }
6053 flush_signals(current);
6054 }
6055
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006057 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006059 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006060 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006061 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6062 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 ))
6064 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006065
NeilBrowndf5b89b2006-03-27 01:18:20 -08006066 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006067 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006068
NeilBrown31a59e32008-04-30 00:52:30 -07006069 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006070 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006071 spin_lock_irq(&mddev->write_lock);
6072 if (mddev->safemode &&
6073 !atomic_read(&mddev->writes_pending) &&
6074 !mddev->in_sync &&
6075 mddev->recovery_cp == MaxSector) {
6076 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006077 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006078 if (mddev->persistent)
6079 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6080 }
6081 if (mddev->safemode == 1)
6082 mddev->safemode = 0;
6083 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006084 if (did_change)
6085 sysfs_notify(&mddev->kobj, NULL, "array_state");
NeilBrownfca4d842005-06-21 17:17:11 -07006086 }
NeilBrownfca4d842005-06-21 17:17:11 -07006087
NeilBrown850b2b42006-10-03 01:15:46 -07006088 if (mddev->flags)
6089 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006090
Neil Brown52664732008-06-28 08:31:44 +10006091 rdev_for_each(rdev, rtmp, mddev)
6092 if (test_and_clear_bit(StateChanged, &rdev->flags))
6093 sysfs_notify(&rdev->kobj, NULL, "state");
6094
NeilBrown06d91a52005-06-21 17:17:12 -07006095
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6097 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6098 /* resync/recovery still happening */
6099 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6100 goto unlock;
6101 }
6102 if (mddev->sync_thread) {
6103 /* resync has finished, collect result */
6104 md_unregister_thread(mddev->sync_thread);
6105 mddev->sync_thread = NULL;
NeilBrowndfc70642008-05-23 13:04:39 -07006106 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 /* success...*/
6108 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006109 if (mddev->pers->spare_active(mddev))
6110 sysfs_notify(&mddev->kobj, NULL,
6111 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 }
NeilBrown850b2b42006-10-03 01:15:46 -07006113 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006114
6115 /* if array is no-longer degraded, then any saved_raid_disk
6116 * information must be scrapped
6117 */
6118 if (!mddev->degraded)
NeilBrownd089c6a2008-02-06 01:39:59 -08006119 rdev_for_each(rdev, rtmp, mddev)
NeilBrown41158c72005-06-21 17:17:25 -07006120 rdev->saved_raid_disk = -1;
6121
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122 mddev->recovery = 0;
6123 /* flag recovery needed just to double check */
6124 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006125 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006126 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006127 goto unlock;
6128 }
Neil Brown72a23c22008-06-28 08:31:41 +10006129 /* Set RUNNING before clearing NEEDED to avoid
6130 * any transients in the value of "sync_action".
6131 */
6132 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6133 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006134 /* Clear some bits that don't mean anything, but
6135 * might be left set
6136 */
NeilBrown24dd4692005-11-08 21:39:26 -08006137 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6138 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006140 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6141 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142 /* no recovery is running.
6143 * remove any failed drives, then
6144 * add spares if possible.
6145 * Spare are also removed and re-added, to allow
6146 * the personality to fail the re-add.
6147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006149 if (mddev->reshape_position != MaxSector) {
6150 if (mddev->pers->check_reshape(mddev) != 0)
6151 /* Cannot proceed */
6152 goto unlock;
6153 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006154 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006155 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006156 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6157 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006158 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006159 } else if (mddev->recovery_cp < MaxSector) {
6160 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006161 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006162 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6163 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006165
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006167 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6168 /* We are adding a device or devices to an array
6169 * which has the bitmap stored on all devices.
6170 * So make sure all bitmap pages get written
6171 */
6172 bitmap_write_all(mddev->bitmap);
6173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 mddev->sync_thread = md_register_thread(md_do_sync,
6175 mddev,
6176 "%s_resync");
6177 if (!mddev->sync_thread) {
6178 printk(KERN_ERR "%s: could not start resync"
6179 " thread...\n",
6180 mdname(mddev));
6181 /* leave the spares where they are, it shouldn't hurt */
6182 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006183 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 md_wakeup_thread(mddev->sync_thread);
Neil Brown72a23c22008-06-28 08:31:41 +10006185 sysfs_notify(&mddev->kobj, NULL, "sync_action");
NeilBrownd7603b72006-01-06 00:20:30 -08006186 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 }
6188 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006189 if (!mddev->sync_thread) {
6190 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6191 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6192 &mddev->recovery))
6193 sysfs_notify(&mddev->kobj, NULL, "sync_action");
6194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 mddev_unlock(mddev);
6196 }
6197}
6198
Dan Williams6bfe0b42008-04-30 00:52:32 -07006199void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6200{
6201 sysfs_notify(&rdev->kobj, NULL, "state");
6202 wait_event_timeout(rdev->blocked_wait,
6203 !test_bit(Blocked, &rdev->flags),
6204 msecs_to_jiffies(5000));
6205 rdev_dec_pending(rdev, mddev);
6206}
6207EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6208
Adrian Bunk75c96f82005-05-05 16:16:09 -07006209static int md_notify_reboot(struct notifier_block *this,
6210 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211{
6212 struct list_head *tmp;
6213 mddev_t *mddev;
6214
6215 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6216
6217 printk(KERN_INFO "md: stopping all md devices.\n");
6218
NeilBrown29ac4aa2008-02-06 01:39:58 -08006219 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006220 if (mddev_trylock(mddev)) {
Neil Browndf5b20c2008-07-11 22:02:22 +10006221 do_md_stop (mddev, 1, 0);
Neil Brownc71d4882006-05-26 10:39:25 +10006222 mddev_unlock(mddev);
6223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 /*
6225 * certain more exotic SCSI devices are known to be
6226 * volatile wrt too early system reboots. While the
6227 * right place to handle this issue is the given
6228 * driver, we do want to have a safe RAID driver ...
6229 */
6230 mdelay(1000*1);
6231 }
6232 return NOTIFY_DONE;
6233}
6234
Adrian Bunk75c96f82005-05-05 16:16:09 -07006235static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 .notifier_call = md_notify_reboot,
6237 .next = NULL,
6238 .priority = INT_MAX, /* before any real devices */
6239};
6240
6241static void md_geninit(void)
6242{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6244
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006245 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246}
6247
Adrian Bunk75c96f82005-05-05 16:16:09 -07006248static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 if (register_blkdev(MAJOR_NR, "md"))
6251 return -1;
6252 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
6253 unregister_blkdev(MAJOR_NR, "md");
6254 return -1;
6255 }
NeilBrowne8703fe2006-10-03 01:15:59 -07006256 blk_register_region(MKDEV(MAJOR_NR, 0), 1UL<<MINORBITS, THIS_MODULE,
6257 md_probe, NULL, NULL);
6258 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 md_probe, NULL, NULL);
6260
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006262 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263
6264 md_geninit();
6265 return (0);
6266}
6267
6268
6269#ifndef MODULE
6270
6271/*
6272 * Searches all registered partitions for autorun RAID arrays
6273 * at boot time.
6274 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006275
6276static LIST_HEAD(all_detected_devices);
6277struct detected_devices_node {
6278 struct list_head list;
6279 dev_t dev;
6280};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281
6282void md_autodetect_dev(dev_t dev)
6283{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006284 struct detected_devices_node *node_detected_dev;
6285
6286 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6287 if (node_detected_dev) {
6288 node_detected_dev->dev = dev;
6289 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6290 } else {
6291 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6292 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294}
6295
6296
6297static void autostart_arrays(int part)
6298{
6299 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006300 struct detected_devices_node *node_detected_dev;
6301 dev_t dev;
6302 int i_scanned, i_passed;
6303
6304 i_scanned = 0;
6305 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306
6307 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6308
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006309 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6310 i_scanned++;
6311 node_detected_dev = list_entry(all_detected_devices.next,
6312 struct detected_devices_node, list);
6313 list_del(&node_detected_dev->list);
6314 dev = node_detected_dev->dev;
6315 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006316 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 if (IS_ERR(rdev))
6318 continue;
6319
NeilBrownb2d444d2005-11-08 21:39:31 -08006320 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 MD_BUG();
6322 continue;
6323 }
NeilBrownd0fae182008-03-04 14:29:31 -08006324 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006326 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006328
6329 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6330 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
6332 autorun_devices(part);
6333}
6334
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006335#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336
6337static __exit void md_exit(void)
6338{
6339 mddev_t *mddev;
6340 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006341
NeilBrowne8703fe2006-10-03 01:15:59 -07006342 blk_unregister_region(MKDEV(MAJOR_NR,0), 1U << MINORBITS);
6343 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344
6345 unregister_blkdev(MAJOR_NR,"md");
6346 unregister_blkdev(mdp_major, "mdp");
6347 unregister_reboot_notifier(&md_notifier);
6348 unregister_sysctl_table(raid_table_header);
6349 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006350 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 struct gendisk *disk = mddev->gendisk;
6352 if (!disk)
6353 continue;
6354 export_array(mddev);
6355 del_gendisk(disk);
6356 put_disk(disk);
6357 mddev->gendisk = NULL;
6358 mddev_put(mddev);
6359 }
6360}
6361
Dan Williams685784a2007-07-09 11:56:42 -07006362subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363module_exit(md_exit)
6364
NeilBrownf91de922005-11-08 21:39:36 -08006365static int get_ro(char *buffer, struct kernel_param *kp)
6366{
6367 return sprintf(buffer, "%d", start_readonly);
6368}
6369static int set_ro(const char *val, struct kernel_param *kp)
6370{
6371 char *e;
6372 int num = simple_strtoul(val, &e, 10);
6373 if (*val && (*e == '\0' || *e == '\n')) {
6374 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006375 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006376 }
6377 return -EINVAL;
6378}
6379
NeilBrown80ca3a42006-07-10 04:44:18 -07006380module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6381module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006382
NeilBrownf91de922005-11-08 21:39:36 -08006383
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384EXPORT_SYMBOL(register_md_personality);
6385EXPORT_SYMBOL(unregister_md_personality);
6386EXPORT_SYMBOL(md_error);
6387EXPORT_SYMBOL(md_done_sync);
6388EXPORT_SYMBOL(md_write_start);
6389EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390EXPORT_SYMBOL(md_register_thread);
6391EXPORT_SYMBOL(md_unregister_thread);
6392EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393EXPORT_SYMBOL(md_check_recovery);
6394MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006395MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006396MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);