blob: 9a3214c8585f286ba8c743f3763065733a8289cc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 md.c : Multiple Devices driver for Linux
3 Copyright (C) 1998, 1999, 2000 Ingo Molnar
4
5 completely rewritten, based on the MD driver code from Marc Zyngier
6
7 Changes:
8
9 - RAID-1/RAID-5 extensions by Miguel de Icaza, Gadi Oxman, Ingo Molnar
10 - RAID-6 extensions by H. Peter Anvin <hpa@zytor.com>
11 - boot support for linear and striped mode by Harald Hoyer <HarryH@Royal.Net>
12 - kerneld support by Boris Tobotras <boris@xtalk.msk.su>
13 - kmod support by: Cyrus Durgin
14 - RAID0 bugfixes: Mark Anthony Lisher <markal@iname.com>
15 - Devfs support by Richard Gooch <rgooch@atnf.csiro.au>
16
17 - lots of fixes and improvements to the RAID1/RAID5 and generic
18 RAID code (such as request based resynchronization):
19
20 Neil Brown <neilb@cse.unsw.edu.au>.
21
NeilBrown32a76272005-06-21 17:17:14 -070022 - persistent bitmap code
23 Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 This program is free software; you can redistribute it and/or modify
26 it under the terms of the GNU General Public License as published by
27 the Free Software Foundation; either version 2, or (at your option)
28 any later version.
29
30 You should have received a copy of the GNU General Public License
31 (for example /usr/src/linux/COPYING); if not, write to the Free
32 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33*/
34
NeilBrowna6fb0932005-09-09 16:23:56 -070035#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/raid/md.h>
37#include <linux/sysctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080039#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070040#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110041#include <linux/hdreg.h>
42#include <linux/proc_fs.h>
43#include <linux/random.h>
44#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070045#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110046#include <linux/delay.h>
Christoph Hellwigef740c32009-03-31 14:27:03 +110047#include "bitmap.h"
NeilBrown32a76272005-06-21 17:17:14 -070048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* 63 partitions with the alternate major number (mdp) */
50#define MdpMinorShift 6
51
52#define DEBUG 0
53#define dprintk(x...) ((void)(DEBUG && printk(x)))
54
55
56#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110057static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#endif
59
NeilBrown2604b702006-01-06 00:20:36 -080060static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static DEFINE_SPINLOCK(pers_lock);
62
Adrian Bunk5e563412006-06-26 00:27:42 -070063static void md_print_devices(void);
64
Bernd Schubert90b08712008-05-23 13:04:38 -070065static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
66
Adrian Bunk5e563412006-06-26 00:27:42 -070067#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
71 * is 1000 KB/sec, so the extra system load does not show up that much.
72 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070073 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * subsystem is idle. There is also an 'absolute maximum' reconstruction
75 * speed limit - in case reconstruction slows down your system despite
76 * idle IO detection.
77 *
78 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080079 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81
82static int sysctl_speed_limit_min = 1000;
83static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080084static inline int speed_min(mddev_t *mddev)
85{
86 return mddev->sync_speed_min ?
87 mddev->sync_speed_min : sysctl_speed_limit_min;
88}
89
90static inline int speed_max(mddev_t *mddev)
91{
92 return mddev->sync_speed_max ?
93 mddev->sync_speed_max : sysctl_speed_limit_max;
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96static struct ctl_table_header *raid_table_header;
97
98static ctl_table raid_table[] = {
99 {
100 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
101 .procname = "speed_limit_min",
102 .data = &sysctl_speed_limit_min,
103 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700104 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .proc_handler = &proc_dointvec,
106 },
107 {
108 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
109 .procname = "speed_limit_max",
110 .data = &sysctl_speed_limit_max,
111 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700112 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 .proc_handler = &proc_dointvec,
114 },
115 { .ctl_name = 0 }
116};
117
118static ctl_table raid_dir_table[] = {
119 {
120 .ctl_name = DEV_RAID,
121 .procname = "raid",
122 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700123 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .child = raid_table,
125 },
126 { .ctl_name = 0 }
127};
128
129static ctl_table raid_root_table[] = {
130 {
131 .ctl_name = CTL_DEV,
132 .procname = "dev",
133 .maxlen = 0,
134 .mode = 0555,
135 .child = raid_dir_table,
136 },
137 { .ctl_name = 0 }
138};
139
140static struct block_device_operations md_fops;
141
NeilBrownf91de922005-11-08 21:39:36 -0800142static int start_readonly;
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/*
NeilBrownd7603b72006-01-06 00:20:30 -0800145 * We have a system wide 'event count' that is incremented
146 * on any 'interesting' event, and readers of /proc/mdstat
147 * can use 'poll' or 'select' to find out when the event
148 * count increases.
149 *
150 * Events are:
151 * start array, stop array, error, add device, remove device,
152 * start build, activate spare
153 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800154static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800155static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800156void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800157{
158 atomic_inc(&md_event_count);
159 wake_up(&md_event_waiters);
160}
NeilBrown29269552006-03-27 01:18:10 -0800161EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800162
NeilBrownc331eb02006-05-30 21:27:13 -0700163/* Alternate version that can be called from interrupts
164 * when calling sysfs_notify isn't needed.
165 */
Adrian Bunk05381952006-06-26 00:28:01 -0700166static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700167{
168 atomic_inc(&md_event_count);
169 wake_up(&md_event_waiters);
170}
171
NeilBrownd7603b72006-01-06 00:20:30 -0800172/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 * Enables to iterate over all existing md arrays
174 * all_mddevs_lock protects this list.
175 */
176static LIST_HEAD(all_mddevs);
177static DEFINE_SPINLOCK(all_mddevs_lock);
178
179
180/*
181 * iterates through all used mddevs in the system.
182 * We take care to grab the all_mddevs_lock whenever navigating
183 * the list, and to always hold a refcount when unlocked.
184 * Any code which breaks out of this loop while own
185 * a reference to the current mddev and must mddev_put it.
186 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800187#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 \
189 for (({ spin_lock(&all_mddevs_lock); \
190 tmp = all_mddevs.next; \
191 mddev = NULL;}); \
192 ({ if (tmp != &all_mddevs) \
193 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
194 spin_unlock(&all_mddevs_lock); \
195 if (mddev) mddev_put(mddev); \
196 mddev = list_entry(tmp, mddev_t, all_mddevs); \
197 tmp != &all_mddevs;}); \
198 ({ spin_lock(&all_mddevs_lock); \
199 tmp = tmp->next;}) \
200 )
201
202
NeilBrownd710e132008-10-13 11:55:12 +1100203static int md_fail_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
NeilBrown6712ecf2007-09-27 12:47:43 +0200205 bio_io_error(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207}
208
209static inline mddev_t *mddev_get(mddev_t *mddev)
210{
211 atomic_inc(&mddev->active);
212 return mddev;
213}
214
NeilBrownd3374822009-01-09 08:31:10 +1100215static void mddev_delayed_delete(struct work_struct *ws)
216{
217 mddev_t *mddev = container_of(ws, mddev_t, del_work);
218 kobject_del(&mddev->kobj);
219 kobject_put(&mddev->kobj);
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222static void mddev_put(mddev_t *mddev)
223{
224 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
225 return;
NeilBrownd3374822009-01-09 08:31:10 +1100226 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
227 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100229 if (mddev->gendisk) {
230 /* we did a probe so need to clean up.
231 * Call schedule_work inside the spinlock
232 * so that flush_scheduled_work() after
233 * mddev_find will succeed in waiting for the
234 * work to be done.
235 */
236 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
237 schedule_work(&mddev->del_work);
238 } else
239 kfree(mddev);
240 }
241 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
244static mddev_t * mddev_find(dev_t unit)
245{
246 mddev_t *mddev, *new = NULL;
247
248 retry:
249 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
NeilBrownefeb53c2009-01-09 08:31:10 +1100251 if (unit) {
252 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
253 if (mddev->unit == unit) {
254 mddev_get(mddev);
255 spin_unlock(&all_mddevs_lock);
256 kfree(new);
257 return mddev;
258 }
259
260 if (new) {
261 list_add(&new->all_mddevs, &all_mddevs);
262 spin_unlock(&all_mddevs_lock);
263 new->hold_active = UNTIL_IOCTL;
264 return new;
265 }
266 } else if (new) {
267 /* find an unused unit number */
268 static int next_minor = 512;
269 int start = next_minor;
270 int is_free = 0;
271 int dev = 0;
272 while (!is_free) {
273 dev = MKDEV(MD_MAJOR, next_minor);
274 next_minor++;
275 if (next_minor > MINORMASK)
276 next_minor = 0;
277 if (next_minor == start) {
278 /* Oh dear, all in use. */
279 spin_unlock(&all_mddevs_lock);
280 kfree(new);
281 return NULL;
282 }
283
284 is_free = 1;
285 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
286 if (mddev->unit == dev) {
287 is_free = 0;
288 break;
289 }
290 }
291 new->unit = dev;
292 new->md_minor = MINOR(dev);
293 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 list_add(&new->all_mddevs, &all_mddevs);
295 spin_unlock(&all_mddevs_lock);
296 return new;
297 }
298 spin_unlock(&all_mddevs_lock);
299
NeilBrown9ffae0c2006-01-06 00:20:32 -0800300 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!new)
302 return NULL;
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 new->unit = unit;
305 if (MAJOR(unit) == MD_MAJOR)
306 new->md_minor = MINOR(unit);
307 else
308 new->md_minor = MINOR(unit) >> MdpMinorShift;
309
NeilBrowndf5b89b2006-03-27 01:18:20 -0800310 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 INIT_LIST_HEAD(&new->disks);
312 INIT_LIST_HEAD(&new->all_mddevs);
313 init_timer(&new->safemode_timer);
314 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000315 atomic_set(&new->openers, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700316 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700317 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700318 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700319 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000320 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800321 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700322 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 goto retry;
325}
326
327static inline int mddev_lock(mddev_t * mddev)
328{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800329 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static inline int mddev_trylock(mddev_t * mddev)
333{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800334 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
337static inline void mddev_unlock(mddev_t * mddev)
338{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800339 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
NeilBrown005eca52005-08-22 13:11:08 -0700341 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
NeilBrown2989ddb2006-01-06 00:20:43 -0800344static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100346 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100348 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (rdev->desc_nr == nr)
350 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return NULL;
353}
354
355static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
356{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 mdk_rdev_t *rdev;
358
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100359 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (rdev->bdev->bd_dev == dev)
361 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return NULL;
364}
365
NeilBrownd9d166c2006-01-06 00:20:51 -0800366static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800367{
368 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800369 list_for_each_entry(pers, &pers_list, list) {
370 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800371 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800372 if (strcmp(pers->name, clevel)==0)
373 return pers;
374 }
NeilBrown2604b702006-01-06 00:20:36 -0800375 return NULL;
376}
377
Andre Nollb73df2d2008-07-11 22:02:23 +1000378/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700379static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Andre Nollb73df2d2008-07-11 22:02:23 +1000381 sector_t num_sectors = bdev->bd_inode->i_size / 512;
382 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Andre Nolle7debaa2008-07-11 22:02:23 +1000385static sector_t calc_num_sectors(mdk_rdev_t *rdev, unsigned chunk_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Andre Noll0f420352008-07-11 22:02:23 +1000387 sector_t num_sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (chunk_size)
Andre Nolle7debaa2008-07-11 22:02:23 +1000390 num_sectors &= ~((sector_t)chunk_size/512 - 1);
391 return num_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394static int alloc_disk_sb(mdk_rdev_t * rdev)
395{
396 if (rdev->sb_page)
397 MD_BUG();
398
399 rdev->sb_page = alloc_page(GFP_KERNEL);
400 if (!rdev->sb_page) {
401 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000402 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404
405 return 0;
406}
407
408static void free_disk_sb(mdk_rdev_t * rdev)
409{
410 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800411 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 rdev->sb_loaded = 0;
413 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000414 rdev->sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 rdev->size = 0;
416 }
417}
418
419
NeilBrown6712ecf2007-09-27 12:47:43 +0200420static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700421{
422 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800423 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700424
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700425 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
426 printk("md: super_written gets error=%d, uptodate=%d\n",
427 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
428 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800429 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700430 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700431
NeilBrowna9701a32005-11-08 21:39:34 -0800432 if (atomic_dec_and_test(&mddev->pending_writes))
433 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700434 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700435}
436
NeilBrown6712ecf2007-09-27 12:47:43 +0200437static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800438{
439 struct bio *bio2 = bio->bi_private;
440 mdk_rdev_t *rdev = bio2->bi_private;
441 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800442
443 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
444 error == -EOPNOTSUPP) {
445 unsigned long flags;
446 /* barriers don't appear to be supported :-( */
447 set_bit(BarriersNotsupp, &rdev->flags);
448 mddev->barriers_work = 0;
449 spin_lock_irqsave(&mddev->write_lock, flags);
450 bio2->bi_next = mddev->biolist;
451 mddev->biolist = bio2;
452 spin_unlock_irqrestore(&mddev->write_lock, flags);
453 wake_up(&mddev->sb_wait);
454 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200455 } else {
456 bio_put(bio2);
457 bio->bi_private = rdev;
458 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800459 }
NeilBrowna9701a32005-11-08 21:39:34 -0800460}
461
NeilBrown7bfa19f2005-06-21 17:17:28 -0700462void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
463 sector_t sector, int size, struct page *page)
464{
465 /* write first size bytes of page to sector of rdev
466 * Increment mddev->pending_writes before returning
467 * and decrement it on completion, waking up sb_wait
468 * if zero is reached.
469 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800470 *
471 * As we might need to resubmit the request if BIO_RW_BARRIER
472 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700473 */
474 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100475 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700476
477 bio->bi_bdev = rdev->bdev;
478 bio->bi_sector = sector;
479 bio_add_page(bio, page, size, 0);
480 bio->bi_private = rdev;
481 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800482 bio->bi_rw = rw;
483
NeilBrown7bfa19f2005-06-21 17:17:28 -0700484 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800485 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
486 struct bio *rbio;
487 rw |= (1<<BIO_RW_BARRIER);
488 rbio = bio_clone(bio, GFP_NOIO);
489 rbio->bi_private = bio;
490 rbio->bi_end_io = super_written_barrier;
491 submit_bio(rw, rbio);
492 } else
493 submit_bio(rw, bio);
494}
495
496void md_super_wait(mddev_t *mddev)
497{
498 /* wait for all superblock writes that were scheduled to complete.
499 * if any had to be retried (due to BARRIER problems), retry them
500 */
501 DEFINE_WAIT(wq);
502 for(;;) {
503 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
504 if (atomic_read(&mddev->pending_writes)==0)
505 break;
506 while (mddev->biolist) {
507 struct bio *bio;
508 spin_lock_irq(&mddev->write_lock);
509 bio = mddev->biolist;
510 mddev->biolist = bio->bi_next ;
511 bio->bi_next = NULL;
512 spin_unlock_irq(&mddev->write_lock);
513 submit_bio(bio->bi_rw, bio);
514 }
515 schedule();
516 }
517 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700518}
519
NeilBrown6712ecf2007-09-27 12:47:43 +0200520static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
NeilBrowna654b9d82005-06-21 17:17:27 -0700525int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 struct page *page, int rw)
527{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700528 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct completion event;
530 int ret;
531
Jens Axboe93dbb392009-02-16 10:25:40 +0100532 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 bio->bi_bdev = bdev;
535 bio->bi_sector = sector;
536 bio_add_page(bio, page, size, 0);
537 init_completion(&event);
538 bio->bi_private = &event;
539 bio->bi_end_io = bi_complete;
540 submit_bio(rw, bio);
541 wait_for_completion(&event);
542
543 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
544 bio_put(bio);
545 return ret;
546}
NeilBrowna8745db2006-01-06 00:20:34 -0800547EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
NeilBrown0002b272005-09-09 16:23:53 -0700549static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 char b[BDEVNAME_SIZE];
552 if (!rdev->sb_page) {
553 MD_BUG();
554 return -EINVAL;
555 }
556 if (rdev->sb_loaded)
557 return 0;
558
559
Andre Noll0f420352008-07-11 22:02:23 +1000560 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto fail;
562 rdev->sb_loaded = 1;
563 return 0;
564
565fail:
566 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
567 bdevname(rdev->bdev,b));
568 return -EINVAL;
569}
570
571static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
572{
Andre Noll05710462008-07-11 22:02:20 +1000573 return sb1->set_uuid0 == sb2->set_uuid0 &&
574 sb1->set_uuid1 == sb2->set_uuid1 &&
575 sb1->set_uuid2 == sb2->set_uuid2 &&
576 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
580{
581 int ret;
582 mdp_super_t *tmp1, *tmp2;
583
584 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
585 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
586
587 if (!tmp1 || !tmp2) {
588 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100589 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto abort;
591 }
592
593 *tmp1 = *sb1;
594 *tmp2 = *sb2;
595
596 /*
597 * nr_disks is not constant
598 */
599 tmp1->nr_disks = 0;
600 tmp2->nr_disks = 0;
601
Andre Nollce0c8e02008-07-11 22:02:20 +1000602 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700604 kfree(tmp1);
605 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return ret;
607}
608
NeilBrown4d167f02007-05-09 02:35:37 -0700609
610static u32 md_csum_fold(u32 csum)
611{
612 csum = (csum & 0xffff) + (csum >> 16);
613 return (csum & 0xffff) + (csum >> 16);
614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616static unsigned int calc_sb_csum(mdp_super_t * sb)
617{
NeilBrown4d167f02007-05-09 02:35:37 -0700618 u64 newcsum = 0;
619 u32 *sb32 = (u32*)sb;
620 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned int disk_csum, csum;
622
623 disk_csum = sb->sb_csum;
624 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700625
626 for (i = 0; i < MD_SB_BYTES/4 ; i++)
627 newcsum += sb32[i];
628 csum = (newcsum & 0xffffffff) + (newcsum>>32);
629
630
631#ifdef CONFIG_ALPHA
632 /* This used to use csum_partial, which was wrong for several
633 * reasons including that different results are returned on
634 * different architectures. It isn't critical that we get exactly
635 * the same return value as before (we always csum_fold before
636 * testing, and that removes any differences). However as we
637 * know that csum_partial always returned a 16bit value on
638 * alphas, do a fold to maximise conformity to previous behaviour.
639 */
640 sb->sb_csum = md_csum_fold(disk_csum);
641#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700643#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return csum;
645}
646
647
648/*
649 * Handle superblock details.
650 * We want to be able to handle multiple superblock formats
651 * so we have a common interface to them all, and an array of
652 * different handlers.
653 * We rely on user-space to write the initial superblock, and support
654 * reading and updating of superblocks.
655 * Interface methods are:
656 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
657 * loads and validates a superblock on dev.
658 * if refdev != NULL, compare superblocks on both devices
659 * Return:
660 * 0 - dev has a superblock that is compatible with refdev
661 * 1 - dev has a superblock that is compatible and newer than refdev
662 * so dev should be used as the refdev in future
663 * -EINVAL superblock incompatible or invalid
664 * -othererror e.g. -EIO
665 *
666 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
667 * Verify that dev is acceptable into mddev.
668 * The first time, mddev->raid_disks will be 0, and data from
669 * dev should be merged in. Subsequent calls check that dev
670 * is new enough. Return 0 or -EINVAL
671 *
672 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
673 * Update the superblock for rdev with data in mddev
674 * This does not write to disc.
675 *
676 */
677
678struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000679 char *name;
680 struct module *owner;
681 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
682 int minor_version);
683 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
684 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
685 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000686 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687};
688
689/*
690 * load_super for 0.90.0
691 */
692static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
693{
694 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
695 mdp_super_t *sb;
696 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /*
Andre Noll0f420352008-07-11 22:02:23 +1000699 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 * it's at the end of the disk.
701 *
702 * It also happens to be a multiple of 4Kb.
703 */
Andre Noll0f420352008-07-11 22:02:23 +1000704 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
NeilBrown0002b272005-09-09 16:23:53 -0700706 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (ret) return ret;
708
709 ret = -EINVAL;
710
711 bdevname(rdev->bdev, b);
712 sb = (mdp_super_t*)page_address(rdev->sb_page);
713
714 if (sb->md_magic != MD_SB_MAGIC) {
715 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
716 b);
717 goto abort;
718 }
719
720 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800721 sb->minor_version < 90 ||
722 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
724 sb->major_version, sb->minor_version,
725 b);
726 goto abort;
727 }
728
729 if (sb->raid_disks <= 0)
730 goto abort;
731
NeilBrown4d167f02007-05-09 02:35:37 -0700732 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
734 b);
735 goto abort;
736 }
737
738 rdev->preferred_minor = sb->md_minor;
739 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700740 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
NeilBrowne11e93f2007-05-09 02:35:36 -0700742 if (sb->state & (1<<MD_SB_BITMAP_PRESENT)) {
743 if (sb->level != 1 && sb->level != 4
744 && sb->level != 5 && sb->level != 6
745 && sb->level != 10) {
746 /* FIXME use a better test */
747 printk(KERN_WARNING
748 "md: bitmaps not supported for this level.\n");
749 goto abort;
750 }
751 }
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (sb->level == LEVEL_MULTIPATH)
754 rdev->desc_nr = -1;
755 else
756 rdev->desc_nr = sb->this_disk.number;
757
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700758 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700760 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 __u64 ev1, ev2;
762 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
763 if (!uuid_equal(refsb, sb)) {
764 printk(KERN_WARNING "md: %s has different UUID to %s\n",
765 b, bdevname(refdev->bdev,b2));
766 goto abort;
767 }
768 if (!sb_equal(refsb, sb)) {
769 printk(KERN_WARNING "md: %s has same UUID"
770 " but different superblock to %s\n",
771 b, bdevname(refdev->bdev, b2));
772 goto abort;
773 }
774 ev1 = md_event(sb);
775 ev2 = md_event(refsb);
776 if (ev1 > ev2)
777 ret = 1;
778 else
779 ret = 0;
780 }
Andre Nolle7debaa2008-07-11 22:02:23 +1000781 rdev->size = calc_num_sectors(rdev, sb->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
NeilBrown2bf071b2006-01-06 00:20:55 -0800783 if (rdev->size < sb->size && sb->level > 1)
784 /* "this cannot possibly happen" ... */
785 ret = -EINVAL;
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 abort:
788 return ret;
789}
790
791/*
792 * validate_super for 0.90.0
793 */
794static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
795{
796 mdp_disk_t *desc;
797 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700798 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
NeilBrown41158c72005-06-21 17:17:25 -0700800 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800801 clear_bit(Faulty, &rdev->flags);
802 clear_bit(In_sync, &rdev->flags);
803 clear_bit(WriteMostly, &rdev->flags);
804 clear_bit(BarriersNotsupp, &rdev->flags);
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (mddev->raid_disks == 0) {
807 mddev->major_version = 0;
808 mddev->minor_version = sb->minor_version;
809 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800810 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 mddev->chunk_size = sb->chunk_size;
812 mddev->ctime = sb->ctime;
813 mddev->utime = sb->utime;
814 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800815 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 mddev->layout = sb->layout;
817 mddev->raid_disks = sb->raid_disks;
818 mddev->size = sb->size;
NeilBrown07d84d102006-06-26 00:27:56 -0700819 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700820 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700821 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
NeilBrownf6705572006-03-27 01:18:11 -0800823 if (mddev->minor_version >= 91) {
824 mddev->reshape_position = sb->reshape_position;
825 mddev->delta_disks = sb->delta_disks;
826 mddev->new_level = sb->new_level;
827 mddev->new_layout = sb->new_layout;
828 mddev->new_chunk = sb->new_chunk;
829 } else {
830 mddev->reshape_position = MaxSector;
831 mddev->delta_disks = 0;
832 mddev->new_level = mddev->level;
833 mddev->new_layout = mddev->layout;
834 mddev->new_chunk = mddev->chunk_size;
835 }
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (sb->state & (1<<MD_SB_CLEAN))
838 mddev->recovery_cp = MaxSector;
839 else {
840 if (sb->events_hi == sb->cp_events_hi &&
841 sb->events_lo == sb->cp_events_lo) {
842 mddev->recovery_cp = sb->recovery_cp;
843 } else
844 mddev->recovery_cp = 0;
845 }
846
847 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
848 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
849 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
850 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
851
852 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700853
854 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700855 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700856 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700857
NeilBrown41158c72005-06-21 17:17:25 -0700858 } else if (mddev->pers == NULL) {
859 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 ++ev1;
861 if (ev1 < mddev->events)
862 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700863 } else if (mddev->bitmap) {
864 /* if adding to array with a bitmap, then we can accept an
865 * older device ... but not too old.
866 */
NeilBrown41158c72005-06-21 17:17:25 -0700867 if (ev1 < mddev->bitmap->events_cleared)
868 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700869 } else {
870 if (ev1 < mddev->events)
871 /* just a hot-add of a new device, leave raid_disk at -1 */
872 return 0;
873 }
NeilBrown41158c72005-06-21 17:17:25 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 desc = sb->disks + rdev->desc_nr;
877
878 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800879 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700880 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
881 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800882 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 rdev->raid_disk = desc->raid_disk;
884 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700885 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
886 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700887 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800888 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return 0;
890}
891
892/*
893 * sync_super for 0.90.0
894 */
895static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
896{
897 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 mdk_rdev_t *rdev2;
899 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* make rdev->sb match mddev data..
903 *
904 * 1/ zero out disks
905 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
906 * 3/ any empty disks < next_spare become removed
907 *
908 * disks[0] gets initialised to REMOVED because
909 * we cannot be sure from other fields if it has
910 * been initialised or not.
911 */
912 int i;
913 int active=0, working=0,failed=0,spare=0,nr_disks=0;
914
NeilBrown61181562005-09-09 16:24:02 -0700915 rdev->sb_size = MD_SB_BYTES;
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sb = (mdp_super_t*)page_address(rdev->sb_page);
918
919 memset(sb, 0, sizeof(*sb));
920
921 sb->md_magic = MD_SB_MAGIC;
922 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 sb->patch_version = mddev->patch_version;
924 sb->gvalid_words = 0; /* ignored */
925 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
926 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
927 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
928 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
929
930 sb->ctime = mddev->ctime;
931 sb->level = mddev->level;
932 sb->size = mddev->size;
933 sb->raid_disks = mddev->raid_disks;
934 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800935 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 sb->utime = mddev->utime;
937 sb->state = 0;
938 sb->events_hi = (mddev->events>>32);
939 sb->events_lo = (u32)mddev->events;
940
NeilBrownf6705572006-03-27 01:18:11 -0800941 if (mddev->reshape_position == MaxSector)
942 sb->minor_version = 90;
943 else {
944 sb->minor_version = 91;
945 sb->reshape_position = mddev->reshape_position;
946 sb->new_level = mddev->new_level;
947 sb->delta_disks = mddev->delta_disks;
948 sb->new_layout = mddev->new_layout;
949 sb->new_chunk = mddev->new_chunk;
950 }
951 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (mddev->in_sync)
953 {
954 sb->recovery_cp = mddev->recovery_cp;
955 sb->cp_events_hi = (mddev->events>>32);
956 sb->cp_events_lo = (u32)mddev->events;
957 if (mddev->recovery_cp == MaxSector)
958 sb->state = (1<< MD_SB_CLEAN);
959 } else
960 sb->recovery_cp = 0;
961
962 sb->layout = mddev->layout;
963 sb->chunk_size = mddev->chunk_size;
964
NeilBrowna654b9d82005-06-21 17:17:27 -0700965 if (mddev->bitmap && mddev->bitmap_file == NULL)
966 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100969 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -0800971 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -0800972 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
973 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -0800974 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 else
NeilBrown86e6ffd2005-11-08 21:39:24 -0800976 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -0800977 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 d = &sb->disks[rdev2->desc_nr];
979 nr_disks++;
980 d->number = rdev2->desc_nr;
981 d->major = MAJOR(rdev2->bdev->bd_dev);
982 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -0800983 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
984 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 d->raid_disk = rdev2->raid_disk;
986 else
987 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -0800988 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -0800990 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 d->state = (1<<MD_DISK_ACTIVE);
992 d->state |= (1<<MD_DISK_SYNC);
993 active++;
994 working++;
995 } else {
996 d->state = 0;
997 spare++;
998 working++;
999 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001000 if (test_bit(WriteMostly, &rdev2->flags))
1001 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /* now set the "removed" and "faulty" bits on any missing devices */
1004 for (i=0 ; i < mddev->raid_disks ; i++) {
1005 mdp_disk_t *d = &sb->disks[i];
1006 if (d->state == 0 && d->number == 0) {
1007 d->number = i;
1008 d->raid_disk = i;
1009 d->state = (1<<MD_DISK_REMOVED);
1010 d->state |= (1<<MD_DISK_FAULTY);
1011 failed++;
1012 }
1013 }
1014 sb->nr_disks = nr_disks;
1015 sb->active_disks = active;
1016 sb->working_disks = working;
1017 sb->failed_disks = failed;
1018 sb->spare_disks = spare;
1019
1020 sb->this_disk = sb->disks[rdev->desc_nr];
1021 sb->sb_csum = calc_sb_csum(sb);
1022}
1023
1024/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001025 * rdev_size_change for 0.90.0
1026 */
1027static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001028super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001029{
Andre Noll15f4a5f2008-07-21 14:42:12 +10001030 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001031 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001032 if (rdev->mddev->bitmap_offset)
1033 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001034 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001035 if (!num_sectors || num_sectors > rdev->sb_start)
1036 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001037 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001038 rdev->sb_page);
1039 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001040 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001041}
1042
1043
1044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * version 1 superblock
1046 */
1047
NeilBrown1c05b4b2006-10-21 10:24:08 -07001048static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001050 __le32 disk_csum;
1051 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 unsigned long long newcsum;
1053 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001054 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 int i;
1056
1057 disk_csum = sb->sb_csum;
1058 sb->sb_csum = 0;
1059 newcsum = 0;
1060 for (i=0; size>=4; size -= 4 )
1061 newcsum += le32_to_cpu(*isuper++);
1062
1063 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001064 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1067 sb->sb_csum = disk_csum;
1068 return cpu_to_le32(csum);
1069}
1070
1071static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1072{
1073 struct mdp_superblock_1 *sb;
1074 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001075 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001077 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /*
Andre Noll0f420352008-07-11 22:02:23 +10001080 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 * It is always aligned to a 4K boundary and
1082 * depeding on minor_version, it can be:
1083 * 0: At least 8K, but less than 12K, from end of device
1084 * 1: At start of device
1085 * 2: 4K from start of device.
1086 */
1087 switch(minor_version) {
1088 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001089 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1090 sb_start -= 8*2;
1091 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 break;
1093 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001094 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 break;
1096 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001097 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
1099 default:
1100 return -EINVAL;
1101 }
Andre Noll0f420352008-07-11 22:02:23 +10001102 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
NeilBrown0002b272005-09-09 16:23:53 -07001104 /* superblock is rarely larger than 1K, but it can be larger,
1105 * and it is safe to read 4k, so we do that
1106 */
1107 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (ret) return ret;
1109
1110
1111 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1112
1113 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1114 sb->major_version != cpu_to_le32(1) ||
1115 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001116 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001117 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return -EINVAL;
1119
1120 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1121 printk("md: invalid superblock checksum on %s\n",
1122 bdevname(rdev->bdev,b));
1123 return -EINVAL;
1124 }
1125 if (le64_to_cpu(sb->data_size) < 10) {
1126 printk("md: data_size too small on %s\n",
1127 bdevname(rdev->bdev,b));
1128 return -EINVAL;
1129 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001130 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET)) {
1131 if (sb->level != cpu_to_le32(1) &&
1132 sb->level != cpu_to_le32(4) &&
1133 sb->level != cpu_to_le32(5) &&
1134 sb->level != cpu_to_le32(6) &&
1135 sb->level != cpu_to_le32(10)) {
1136 printk(KERN_WARNING
1137 "md: bitmaps not supported for this level.\n");
1138 return -EINVAL;
1139 }
1140 }
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 rdev->preferred_minor = 0xffff;
1143 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001144 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
NeilBrown0002b272005-09-09 16:23:53 -07001146 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
NeilBrown720a3dc2005-09-09 16:23:59 -07001147 bmask = queue_hardsect_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001148 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001149 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1150
1151 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001152 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001153 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001154
NeilBrown31b65a02006-07-10 04:44:14 -07001155 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1156 rdev->desc_nr = -1;
1157 else
1158 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1159
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001160 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001161 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001162 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 __u64 ev1, ev2;
1164 struct mdp_superblock_1 *refsb =
1165 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1166
1167 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1168 sb->level != refsb->level ||
1169 sb->layout != refsb->layout ||
1170 sb->chunksize != refsb->chunksize) {
1171 printk(KERN_WARNING "md: %s has strangely different"
1172 " superblock to %s\n",
1173 bdevname(rdev->bdev,b),
1174 bdevname(refdev->bdev,b2));
1175 return -EINVAL;
1176 }
1177 ev1 = le64_to_cpu(sb->events);
1178 ev2 = le64_to_cpu(refsb->events);
1179
1180 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001181 ret = 1;
1182 else
1183 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
NeilBrowna1801f82008-03-04 14:29:31 -08001185 if (minor_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 rdev->size = ((rdev->bdev->bd_inode->i_size>>9) - le64_to_cpu(sb->data_offset)) / 2;
1187 else
Andre Noll0f420352008-07-11 22:02:23 +10001188 rdev->size = rdev->sb_start / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (rdev->size < le64_to_cpu(sb->data_size)/2)
1190 return -EINVAL;
1191 rdev->size = le64_to_cpu(sb->data_size)/2;
1192 if (le32_to_cpu(sb->chunksize))
1193 rdev->size &= ~((sector_t)le32_to_cpu(sb->chunksize)/2 - 1);
NeilBrown2bf071b2006-01-06 00:20:55 -08001194
NeilBrown1c05b4b2006-10-21 10:24:08 -07001195 if (le64_to_cpu(sb->size) > rdev->size*2)
NeilBrown2bf071b2006-01-06 00:20:55 -08001196 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001197 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198}
1199
1200static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1201{
1202 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001203 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
NeilBrown41158c72005-06-21 17:17:25 -07001205 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001206 clear_bit(Faulty, &rdev->flags);
1207 clear_bit(In_sync, &rdev->flags);
1208 clear_bit(WriteMostly, &rdev->flags);
1209 clear_bit(BarriersNotsupp, &rdev->flags);
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (mddev->raid_disks == 0) {
1212 mddev->major_version = 1;
1213 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001214 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 mddev->chunk_size = le32_to_cpu(sb->chunksize) << 9;
1216 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1217 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1218 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001219 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 mddev->layout = le32_to_cpu(sb->layout);
1221 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
1222 mddev->size = le64_to_cpu(sb->size)/2;
NeilBrown07d84d102006-06-26 00:27:56 -07001223 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001224 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001225 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1228 memcpy(mddev->uuid, sb->set_uuid, 16);
1229
1230 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001231
NeilBrown71c08052005-09-09 16:23:51 -07001232 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001233 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001234 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001235
NeilBrownf6705572006-03-27 01:18:11 -08001236 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1237 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1238 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1239 mddev->new_level = le32_to_cpu(sb->new_level);
1240 mddev->new_layout = le32_to_cpu(sb->new_layout);
1241 mddev->new_chunk = le32_to_cpu(sb->new_chunk)<<9;
1242 } else {
1243 mddev->reshape_position = MaxSector;
1244 mddev->delta_disks = 0;
1245 mddev->new_level = mddev->level;
1246 mddev->new_layout = mddev->layout;
1247 mddev->new_chunk = mddev->chunk_size;
1248 }
1249
NeilBrown41158c72005-06-21 17:17:25 -07001250 } else if (mddev->pers == NULL) {
1251 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 ++ev1;
1253 if (ev1 < mddev->events)
1254 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001255 } else if (mddev->bitmap) {
1256 /* If adding to array with a bitmap, then we can accept an
1257 * older device, but not too old.
1258 */
NeilBrown41158c72005-06-21 17:17:25 -07001259 if (ev1 < mddev->bitmap->events_cleared)
1260 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001261 } else {
1262 if (ev1 < mddev->events)
1263 /* just a hot-add of a new device, leave raid_disk at -1 */
1264 return 0;
1265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (mddev->level != LEVEL_MULTIPATH) {
1267 int role;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
1269 switch(role) {
1270 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001273 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
1275 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001276 if ((le32_to_cpu(sb->feature_map) &
1277 MD_FEATURE_RECOVERY_OFFSET))
1278 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1279 else
1280 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 rdev->raid_disk = role;
1282 break;
1283 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001284 if (sb->devflags & WriteMostly1)
1285 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001286 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001287 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return 0;
1290}
1291
1292static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1293{
1294 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 mdk_rdev_t *rdev2;
1296 int max_dev, i;
1297 /* make rdev->sb match mddev and rdev data. */
1298
1299 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1300
1301 sb->feature_map = 0;
1302 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001303 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 memset(sb->pad1, 0, sizeof(sb->pad1));
1305 memset(sb->pad2, 0, sizeof(sb->pad2));
1306 memset(sb->pad3, 0, sizeof(sb->pad3));
1307
1308 sb->utime = cpu_to_le64((__u64)mddev->utime);
1309 sb->events = cpu_to_le64(mddev->events);
1310 if (mddev->in_sync)
1311 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1312 else
1313 sb->resync_offset = cpu_to_le64(0);
1314
NeilBrown1c05b4b2006-10-21 10:24:08 -07001315 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001316
NeilBrownf0ca3402006-02-02 14:28:04 -08001317 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
NeilBrown29fc7e32006-02-03 03:03:41 -08001318 sb->size = cpu_to_le64(mddev->size<<1);
NeilBrownf0ca3402006-02-02 14:28:04 -08001319
NeilBrowna654b9d82005-06-21 17:17:27 -07001320 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1321 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001322 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001323 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001324
1325 if (rdev->raid_disk >= 0 &&
1326 !test_bit(In_sync, &rdev->flags) &&
1327 rdev->recovery_offset > 0) {
1328 sb->feature_map |= cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1329 sb->recovery_offset = cpu_to_le64(rdev->recovery_offset);
1330 }
1331
NeilBrownf6705572006-03-27 01:18:11 -08001332 if (mddev->reshape_position != MaxSector) {
1333 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1334 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1335 sb->new_layout = cpu_to_le32(mddev->new_layout);
1336 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1337 sb->new_level = cpu_to_le32(mddev->new_level);
1338 sb->new_chunk = cpu_to_le32(mddev->new_chunk>>9);
1339 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001342 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (rdev2->desc_nr+1 > max_dev)
1344 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001345
1346 if (max_dev > le32_to_cpu(sb->max_dev))
1347 sb->max_dev = cpu_to_le32(max_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 for (i=0; i<max_dev;i++)
1349 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1350
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001351 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001353 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001355 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001357 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1358 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 else
1360 sb->dev_roles[i] = cpu_to_le16(0xffff);
1361 }
1362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 sb->sb_csum = calc_sb_1_csum(sb);
1364}
1365
Chris Webb0cd17fe2008-06-28 08:31:46 +10001366static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001367super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001368{
1369 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001370 sector_t max_sectors;
1371 if (num_sectors && num_sectors < rdev->mddev->size * 2)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001372 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001373 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001374 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001375 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1376 max_sectors -= rdev->data_offset;
1377 if (!num_sectors || num_sectors > max_sectors)
1378 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001379 } else if (rdev->mddev->bitmap_offset) {
1380 /* minor version 0 with bitmap we can't move */
1381 return 0;
1382 } else {
1383 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001384 sector_t sb_start;
1385 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1386 sb_start &= ~(sector_t)(4*2 - 1);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001387 max_sectors = rdev->size * 2 + sb_start - rdev->sb_start;
1388 if (!num_sectors || num_sectors > max_sectors)
1389 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001390 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001391 }
1392 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001393 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001394 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001395 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001396 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001397 rdev->sb_page);
1398 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001399 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Adrian Bunk75c96f82005-05-05 16:16:09 -07001402static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 [0] = {
1404 .name = "0.90.0",
1405 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001406 .load_super = super_90_load,
1407 .validate_super = super_90_validate,
1408 .sync_super = super_90_sync,
1409 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 },
1411 [1] = {
1412 .name = "md-1",
1413 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001414 .load_super = super_1_load,
1415 .validate_super = super_1_validate,
1416 .sync_super = super_1_sync,
1417 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 },
1419};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1422{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001423 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
NeilBrown4b809912008-07-21 17:05:25 +10001425 rcu_read_lock();
1426 rdev_for_each_rcu(rdev, mddev1)
1427 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001428 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001429 rdev2->bdev->bd_contains) {
1430 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001431 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001432 }
1433 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return 0;
1435}
1436
1437static LIST_HEAD(pending_raid_disks);
1438
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001439static void md_integrity_check(mdk_rdev_t *rdev, mddev_t *mddev)
1440{
1441 struct mdk_personality *pers = mddev->pers;
1442 struct gendisk *disk = mddev->gendisk;
1443 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1444 struct blk_integrity *bi_mddev = blk_get_integrity(disk);
1445
1446 /* Data integrity passthrough not supported on RAID 4, 5 and 6 */
1447 if (pers && pers->level >= 4 && pers->level <= 6)
1448 return;
1449
1450 /* If rdev is integrity capable, register profile for mddev */
1451 if (!bi_mddev && bi_rdev) {
1452 if (blk_integrity_register(disk, bi_rdev))
1453 printk(KERN_ERR "%s: %s Could not register integrity!\n",
1454 __func__, disk->disk_name);
1455 else
1456 printk(KERN_NOTICE "Enabling data integrity on %s\n",
1457 disk->disk_name);
1458 return;
1459 }
1460
1461 /* Check that mddev and rdev have matching profiles */
1462 if (blk_integrity_compare(disk, rdev->bdev->bd_disk) < 0) {
1463 printk(KERN_ERR "%s: %s/%s integrity mismatch!\n", __func__,
1464 disk->disk_name, rdev->bdev->bd_disk->disk_name);
1465 printk(KERN_NOTICE "Disabling data integrity on %s\n",
1466 disk->disk_name);
1467 blk_integrity_unregister(disk);
1468 }
1469}
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1472{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001473 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001474 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001475 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001476 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 if (rdev->mddev) {
1479 MD_BUG();
1480 return -EINVAL;
1481 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001482
1483 /* prevent duplicates */
1484 if (find_rdev(mddev, rdev->bdev->bd_dev))
1485 return -EEXIST;
1486
NeilBrown2bf071b2006-01-06 00:20:55 -08001487 /* make sure rdev->size exceeds mddev->size */
1488 if (rdev->size && (mddev->size == 0 || rdev->size < mddev->size)) {
NeilBrowna778b732007-05-23 13:58:10 -07001489 if (mddev->pers) {
1490 /* Cannot change size, so fail
1491 * If mddev->level <= 0, then we don't care
1492 * about aligning sizes (e.g. linear)
1493 */
1494 if (mddev->level > 0)
1495 return -ENOSPC;
1496 } else
NeilBrown2bf071b2006-01-06 00:20:55 -08001497 mddev->size = rdev->size;
1498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 /* Verify rdev->desc_nr is unique.
1501 * If it is -1, assign a free number, else
1502 * check number is not in use
1503 */
1504 if (rdev->desc_nr < 0) {
1505 int choice = 0;
1506 if (mddev->pers) choice = mddev->raid_disks;
1507 while (find_rdev_nr(mddev, choice))
1508 choice++;
1509 rdev->desc_nr = choice;
1510 } else {
1511 if (find_rdev_nr(mddev, rdev->desc_nr))
1512 return -EBUSY;
1513 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001514 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1515 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1516 mdname(mddev), mddev->max_disks);
1517 return -EBUSY;
1518 }
NeilBrown19133a42005-11-08 21:39:35 -08001519 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001520 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001521 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001524 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001525
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001526 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001527 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001528
Tejun Heo0762b8b2008-08-25 19:56:12 +09001529 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001530 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1531 kobject_del(&rdev->kobj);
1532 goto fail;
1533 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001534 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1535
NeilBrown4b809912008-07-21 17:05:25 +10001536 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001537 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001538
1539 /* May as well allow recovery to be retried once */
1540 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001541
1542 md_integrity_check(rdev, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001544
1545 fail:
1546 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1547 b, mdname(mddev));
1548 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
NeilBrown177a99b2008-02-06 01:39:56 -08001551static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001552{
1553 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1554 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001555 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001556}
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1559{
1560 char b[BDEVNAME_SIZE];
1561 if (!rdev->mddev) {
1562 MD_BUG();
1563 return;
1564 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001565 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001566 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1568 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001569 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001570 sysfs_put(rdev->sysfs_state);
1571 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001572 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001573 * writing to 'remove' to "dev/state". We also need
1574 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001575 */
NeilBrown4b809912008-07-21 17:05:25 +10001576 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001577 INIT_WORK(&rdev->del_work, md_delayed_delete);
1578 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001579 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
1582/*
1583 * prevent the device from being mounted, repartitioned or
1584 * otherwise reused by a RAID array (or any other kernel
1585 * subsystem), by bd_claiming the device.
1586 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001587static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
1589 int err = 0;
1590 struct block_device *bdev;
1591 char b[BDEVNAME_SIZE];
1592
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001593 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (IS_ERR(bdev)) {
1595 printk(KERN_ERR "md: could not open %s.\n",
1596 __bdevname(dev, b));
1597 return PTR_ERR(bdev);
1598 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001599 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (err) {
1601 printk(KERN_ERR "md: could not bd_claim %s.\n",
1602 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001603 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 return err;
1605 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001606 if (!shared)
1607 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 rdev->bdev = bdev;
1609 return err;
1610}
1611
1612static void unlock_rdev(mdk_rdev_t *rdev)
1613{
1614 struct block_device *bdev = rdev->bdev;
1615 rdev->bdev = NULL;
1616 if (!bdev)
1617 MD_BUG();
1618 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001619 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622void md_autodetect_dev(dev_t dev);
1623
1624static void export_rdev(mdk_rdev_t * rdev)
1625{
1626 char b[BDEVNAME_SIZE];
1627 printk(KERN_INFO "md: export_rdev(%s)\n",
1628 bdevname(rdev->bdev,b));
1629 if (rdev->mddev)
1630 MD_BUG();
1631 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001633 if (test_bit(AutoDetected, &rdev->flags))
1634 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635#endif
1636 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001637 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640static void kick_rdev_from_array(mdk_rdev_t * rdev)
1641{
1642 unbind_rdev_from_array(rdev);
1643 export_rdev(rdev);
1644}
1645
1646static void export_array(mddev_t *mddev)
1647{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001648 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
NeilBrownd089c6a2008-02-06 01:39:59 -08001650 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (!rdev->mddev) {
1652 MD_BUG();
1653 continue;
1654 }
1655 kick_rdev_from_array(rdev);
1656 }
1657 if (!list_empty(&mddev->disks))
1658 MD_BUG();
1659 mddev->raid_disks = 0;
1660 mddev->major_version = 0;
1661}
1662
1663static void print_desc(mdp_disk_t *desc)
1664{
1665 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1666 desc->major,desc->minor,desc->raid_disk,desc->state);
1667}
1668
Cheng Renquancd2ac932009-01-09 08:31:08 +11001669static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 int i;
1672
1673 printk(KERN_INFO
1674 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1675 sb->major_version, sb->minor_version, sb->patch_version,
1676 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1677 sb->ctime);
1678 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1679 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1680 sb->md_minor, sb->layout, sb->chunk_size);
1681 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1682 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1683 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1684 sb->failed_disks, sb->spare_disks,
1685 sb->sb_csum, (unsigned long)sb->events_lo);
1686
1687 printk(KERN_INFO);
1688 for (i = 0; i < MD_SB_DISKS; i++) {
1689 mdp_disk_t *desc;
1690
1691 desc = sb->disks + i;
1692 if (desc->number || desc->major || desc->minor ||
1693 desc->raid_disk || (desc->state && (desc->state != 4))) {
1694 printk(" D %2d: ", i);
1695 print_desc(desc);
1696 }
1697 }
1698 printk(KERN_INFO "md: THIS: ");
1699 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
Cheng Renquancd2ac932009-01-09 08:31:08 +11001702static void print_sb_1(struct mdp_superblock_1 *sb)
1703{
1704 __u8 *uuid;
1705
1706 uuid = sb->set_uuid;
1707 printk(KERN_INFO "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1708 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1709 KERN_INFO "md: Name: \"%s\" CT:%llu\n",
1710 le32_to_cpu(sb->major_version),
1711 le32_to_cpu(sb->feature_map),
1712 uuid[0], uuid[1], uuid[2], uuid[3],
1713 uuid[4], uuid[5], uuid[6], uuid[7],
1714 uuid[8], uuid[9], uuid[10], uuid[11],
1715 uuid[12], uuid[13], uuid[14], uuid[15],
1716 sb->set_name,
1717 (unsigned long long)le64_to_cpu(sb->ctime)
1718 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1719
1720 uuid = sb->device_uuid;
1721 printk(KERN_INFO "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
1722 " RO:%llu\n"
1723 KERN_INFO "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1724 ":%02x%02x%02x%02x%02x%02x\n"
1725 KERN_INFO "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1726 KERN_INFO "md: (MaxDev:%u) \n",
1727 le32_to_cpu(sb->level),
1728 (unsigned long long)le64_to_cpu(sb->size),
1729 le32_to_cpu(sb->raid_disks),
1730 le32_to_cpu(sb->layout),
1731 le32_to_cpu(sb->chunksize),
1732 (unsigned long long)le64_to_cpu(sb->data_offset),
1733 (unsigned long long)le64_to_cpu(sb->data_size),
1734 (unsigned long long)le64_to_cpu(sb->super_offset),
1735 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1736 le32_to_cpu(sb->dev_number),
1737 uuid[0], uuid[1], uuid[2], uuid[3],
1738 uuid[4], uuid[5], uuid[6], uuid[7],
1739 uuid[8], uuid[9], uuid[10], uuid[11],
1740 uuid[12], uuid[13], uuid[14], uuid[15],
1741 sb->devflags,
1742 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1743 (unsigned long long)le64_to_cpu(sb->events),
1744 (unsigned long long)le64_to_cpu(sb->resync_offset),
1745 le32_to_cpu(sb->sb_csum),
1746 le32_to_cpu(sb->max_dev)
1747 );
1748}
1749
1750static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 char b[BDEVNAME_SIZE];
1753 printk(KERN_INFO "md: rdev %s, SZ:%08llu F:%d S:%d DN:%u\n",
1754 bdevname(rdev->bdev,b), (unsigned long long)rdev->size,
NeilBrownb2d444d2005-11-08 21:39:31 -08001755 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1756 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001758 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1759 switch (major_version) {
1760 case 0:
1761 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1762 break;
1763 case 1:
1764 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1765 break;
1766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 } else
1768 printk(KERN_INFO "md: no rdev superblock!\n");
1769}
1770
Adrian Bunk5e563412006-06-26 00:27:42 -07001771static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001773 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 mdk_rdev_t *rdev;
1775 mddev_t *mddev;
1776 char b[BDEVNAME_SIZE];
1777
1778 printk("\n");
1779 printk("md: **********************************\n");
1780 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1781 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001782 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
NeilBrown32a76272005-06-21 17:17:14 -07001784 if (mddev->bitmap)
1785 bitmap_print_sb(mddev->bitmap);
1786 else
1787 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001788 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 printk("<%s>", bdevname(rdev->bdev,b));
1790 printk("\n");
1791
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001792 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001793 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795 printk("md: **********************************\n");
1796 printk("\n");
1797}
1798
1799
NeilBrown42543762006-06-26 00:27:57 -07001800static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
NeilBrown42543762006-06-26 00:27:57 -07001802 /* Update each superblock (in-memory image), but
1803 * if we are allowed to, skip spares which already
1804 * have the right event counter, or have one earlier
1805 * (which would mean they aren't being marked as dirty
1806 * with the rest of the array)
1807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001810 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001811 if (rdev->sb_events == mddev->events ||
1812 (nospares &&
1813 rdev->raid_disk < 0 &&
1814 (rdev->sb_events&1)==0 &&
1815 rdev->sb_events+1 == mddev->events)) {
1816 /* Don't update this superblock */
1817 rdev->sb_loaded = 2;
1818 } else {
1819 super_types[mddev->major_version].
1820 sync_super(mddev, rdev);
1821 rdev->sb_loaded = 1;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824}
1825
NeilBrown850b2b42006-10-03 01:15:46 -07001826static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001829 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001830 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
NeilBrown8377bc82008-04-30 00:52:28 -07001832 if (mddev->external)
1833 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001835 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001836
NeilBrown850b2b42006-10-03 01:15:46 -07001837 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1838 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1839 force_change = 1;
1840 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1841 /* just a clean<-> dirty transition, possibly leave spares alone,
1842 * though if events isn't the right even/odd, we will have to do
1843 * spares after all
1844 */
1845 nospares = 1;
1846 if (force_change)
1847 nospares = 0;
1848 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001849 /* If the array is degraded, then skipping spares is both
1850 * dangerous and fairly pointless.
1851 * Dangerous because a device that was removed from the array
1852 * might have a event_count that still looks up-to-date,
1853 * so it can be re-added without a resync.
1854 * Pointless because if there are any spares to skip,
1855 * then a recovery will happen and soon that array won't
1856 * be degraded any more and the spare can go back to sleep then.
1857 */
NeilBrown850b2b42006-10-03 01:15:46 -07001858 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001859
NeilBrown06d91a52005-06-21 17:17:12 -07001860 sync_req = mddev->in_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 mddev->utime = get_seconds();
NeilBrown42543762006-06-26 00:27:57 -07001862
1863 /* If this is just a dirty<->clean transition, and the array is clean
1864 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001865 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001866 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001867 && (mddev->events & 1)
1868 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001869 mddev->events--;
1870 else {
1871 /* otherwise we have to go forward and ... */
1872 mddev->events ++;
1873 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
1874 /* .. if the array isn't clean, insist on an odd 'events' */
1875 if ((mddev->events&1)==0) {
1876 mddev->events++;
1877 nospares = 0;
1878 }
1879 } else {
1880 /* otherwise insist on an even 'events' (for clean states) */
1881 if ((mddev->events&1)) {
1882 mddev->events++;
1883 nospares = 0;
1884 }
1885 }
1886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 if (!mddev->events) {
1889 /*
1890 * oops, this 64-bit counter should never wrap.
1891 * Either we are in around ~1 trillion A.C., assuming
1892 * 1 reboot per second, or we have a bug:
1893 */
1894 MD_BUG();
1895 mddev->events --;
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /*
1899 * do not write anything to disk if using
1900 * nonpersistent superblocks
1901 */
NeilBrown06d91a52005-06-21 17:17:12 -07001902 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08001903 if (!mddev->external)
1904 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
1905
NeilBrowna9701a32005-11-08 21:39:34 -08001906 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001907 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 return;
NeilBrown06d91a52005-06-21 17:17:12 -07001909 }
NeilBrowne6910632008-02-06 01:39:51 -08001910 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08001911 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 dprintk(KERN_INFO
1914 "md: updating %s RAID superblock on device (in sync %d)\n",
1915 mdname(mddev),mddev->in_sync);
1916
NeilBrown4ad13662007-07-17 04:06:13 -07001917 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001918 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 char b[BDEVNAME_SIZE];
1920 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07001921 if (rdev->sb_loaded != 1)
1922 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08001923 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 dprintk("(skipping faulty ");
1925
1926 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08001927 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07001928 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10001929 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07001930 rdev->sb_page);
1931 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
1932 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10001933 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07001934 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 } else
1937 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07001938 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 /* only need to write one superblock... */
1940 break;
1941 }
NeilBrowna9701a32005-11-08 21:39:34 -08001942 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07001943 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07001944
NeilBrowna9701a32005-11-08 21:39:34 -08001945 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07001946 if (mddev->in_sync != sync_req ||
1947 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07001948 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08001949 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07001950 goto repeat;
1951 }
NeilBrown850b2b42006-10-03 01:15:46 -07001952 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08001953 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07001954 wake_up(&mddev->sb_wait);
NeilBrown06d91a52005-06-21 17:17:12 -07001955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}
1957
Andre Noll7f6ce762008-03-23 18:34:54 +01001958/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08001959 * We want to accept with case. For this we use cmd_match.
1960 */
1961static int cmd_match(const char *cmd, const char *str)
1962{
1963 /* See if cmd, written into a sysfs file, matches
1964 * str. They must either be the same, or cmd can
1965 * have a trailing newline
1966 */
1967 while (*cmd && *str && *cmd == *str) {
1968 cmd++;
1969 str++;
1970 }
1971 if (*cmd == '\n')
1972 cmd++;
1973 if (*str || *cmd)
1974 return 0;
1975 return 1;
1976}
1977
NeilBrown86e6ffd2005-11-08 21:39:24 -08001978struct rdev_sysfs_entry {
1979 struct attribute attr;
1980 ssize_t (*show)(mdk_rdev_t *, char *);
1981 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
1982};
1983
1984static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08001985state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001986{
1987 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08001988 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001989
NeilBrownb2d444d2005-11-08 21:39:31 -08001990 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001991 len+= sprintf(page+len, "%sfaulty",sep);
1992 sep = ",";
1993 }
NeilBrownb2d444d2005-11-08 21:39:31 -08001994 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08001995 len += sprintf(page+len, "%sin_sync",sep);
1996 sep = ",";
1997 }
NeilBrownf6556752006-06-26 00:28:01 -07001998 if (test_bit(WriteMostly, &rdev->flags)) {
1999 len += sprintf(page+len, "%swrite_mostly",sep);
2000 sep = ",";
2001 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002002 if (test_bit(Blocked, &rdev->flags)) {
2003 len += sprintf(page+len, "%sblocked", sep);
2004 sep = ",";
2005 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002006 if (!test_bit(Faulty, &rdev->flags) &&
2007 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002008 len += sprintf(page+len, "%sspare", sep);
2009 sep = ",";
2010 }
2011 return len+sprintf(page+len, "\n");
2012}
2013
NeilBrown45dc2de2006-06-26 00:27:58 -07002014static ssize_t
2015state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2016{
2017 /* can write
2018 * faulty - simulates and error
2019 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002020 * writemostly - sets write_mostly
2021 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002022 * blocked - sets the Blocked flag
2023 * -blocked - clears the Blocked flag
NeilBrown45dc2de2006-06-26 00:27:58 -07002024 */
2025 int err = -EINVAL;
2026 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2027 md_error(rdev->mddev, rdev);
2028 err = 0;
2029 } else if (cmd_match(buf, "remove")) {
2030 if (rdev->raid_disk >= 0)
2031 err = -EBUSY;
2032 else {
2033 mddev_t *mddev = rdev->mddev;
2034 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002035 if (mddev->pers)
2036 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002037 md_new_event(mddev);
2038 err = 0;
2039 }
NeilBrownf6556752006-06-26 00:28:01 -07002040 } else if (cmd_match(buf, "writemostly")) {
2041 set_bit(WriteMostly, &rdev->flags);
2042 err = 0;
2043 } else if (cmd_match(buf, "-writemostly")) {
2044 clear_bit(WriteMostly, &rdev->flags);
2045 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002046 } else if (cmd_match(buf, "blocked")) {
2047 set_bit(Blocked, &rdev->flags);
2048 err = 0;
2049 } else if (cmd_match(buf, "-blocked")) {
2050 clear_bit(Blocked, &rdev->flags);
2051 wake_up(&rdev->blocked_wait);
2052 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2053 md_wakeup_thread(rdev->mddev->thread);
2054
2055 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002056 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002057 if (!err && rdev->sysfs_state)
2058 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002059 return err ? err : len;
2060}
NeilBrown80ca3a42006-07-10 04:44:18 -07002061static struct rdev_sysfs_entry rdev_state =
2062__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002063
2064static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002065errors_show(mdk_rdev_t *rdev, char *page)
2066{
2067 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2068}
2069
2070static ssize_t
2071errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2072{
2073 char *e;
2074 unsigned long n = simple_strtoul(buf, &e, 10);
2075 if (*buf && (*e == 0 || *e == '\n')) {
2076 atomic_set(&rdev->corrected_errors, n);
2077 return len;
2078 }
2079 return -EINVAL;
2080}
2081static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002082__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002083
NeilBrown014236d2006-01-06 00:20:55 -08002084static ssize_t
2085slot_show(mdk_rdev_t *rdev, char *page)
2086{
2087 if (rdev->raid_disk < 0)
2088 return sprintf(page, "none\n");
2089 else
2090 return sprintf(page, "%d\n", rdev->raid_disk);
2091}
2092
2093static ssize_t
2094slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2095{
2096 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002097 int err;
2098 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002099 int slot = simple_strtoul(buf, &e, 10);
2100 if (strncmp(buf, "none", 4)==0)
2101 slot = -1;
2102 else if (e==buf || (*e && *e!= '\n'))
2103 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002104 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002105 /* Setting 'slot' on an active array requires also
2106 * updating the 'rd%d' link, and communicating
2107 * with the personality with ->hot_*_disk.
2108 * For now we only support removing
2109 * failed/spare devices. This normally happens automatically,
2110 * but not when the metadata is externally managed.
2111 */
NeilBrownc303da62008-02-06 01:39:51 -08002112 if (rdev->raid_disk == -1)
2113 return -EEXIST;
2114 /* personality does all needed checks */
2115 if (rdev->mddev->pers->hot_add_disk == NULL)
2116 return -EINVAL;
2117 err = rdev->mddev->pers->
2118 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2119 if (err)
2120 return err;
2121 sprintf(nm, "rd%d", rdev->raid_disk);
2122 sysfs_remove_link(&rdev->mddev->kobj, nm);
2123 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2124 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002125 } else if (rdev->mddev->pers) {
2126 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002127 /* Activating a spare .. or possibly reactivating
2128 * if we every get bitmaps working here.
2129 */
2130
2131 if (rdev->raid_disk != -1)
2132 return -EBUSY;
2133
2134 if (rdev->mddev->pers->hot_add_disk == NULL)
2135 return -EINVAL;
2136
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002137 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002138 if (rdev2->raid_disk == slot)
2139 return -EEXIST;
2140
2141 rdev->raid_disk = slot;
2142 if (test_bit(In_sync, &rdev->flags))
2143 rdev->saved_raid_disk = slot;
2144 else
2145 rdev->saved_raid_disk = -1;
2146 err = rdev->mddev->pers->
2147 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002148 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002149 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002150 return err;
Neil Brown52664732008-06-28 08:31:44 +10002151 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002152 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002153 sprintf(nm, "rd%d", rdev->raid_disk);
2154 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2155 printk(KERN_WARNING
2156 "md: cannot register "
2157 "%s for %s\n",
2158 nm, mdname(rdev->mddev));
2159
2160 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002161 } else {
2162 if (slot >= rdev->mddev->raid_disks)
2163 return -ENOSPC;
2164 rdev->raid_disk = slot;
2165 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002166 clear_bit(Faulty, &rdev->flags);
2167 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002168 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002169 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002170 }
NeilBrown014236d2006-01-06 00:20:55 -08002171 return len;
2172}
2173
2174
2175static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002176__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002177
NeilBrown93c8cad2006-01-06 00:20:56 -08002178static ssize_t
2179offset_show(mdk_rdev_t *rdev, char *page)
2180{
Andrew Morton6961ece2006-01-06 00:20:59 -08002181 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002182}
2183
2184static ssize_t
2185offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2186{
2187 char *e;
2188 unsigned long long offset = simple_strtoull(buf, &e, 10);
2189 if (e==buf || (*e && *e != '\n'))
2190 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002191 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002192 return -EBUSY;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002193 if (rdev->size && rdev->mddev->external)
2194 /* Must set offset before size, so overlap checks
2195 * can be sane */
2196 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002197 rdev->data_offset = offset;
2198 return len;
2199}
2200
2201static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002202__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002203
NeilBrown83303b62006-01-06 00:21:06 -08002204static ssize_t
2205rdev_size_show(mdk_rdev_t *rdev, char *page)
2206{
2207 return sprintf(page, "%llu\n", (unsigned long long)rdev->size);
2208}
2209
NeilBrownc5d79ad2008-02-06 01:39:54 -08002210static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2211{
2212 /* check if two start/length pairs overlap */
2213 if (s1+l1 <= s2)
2214 return 0;
2215 if (s2+l2 <= s1)
2216 return 0;
2217 return 1;
2218}
2219
NeilBrown83303b62006-01-06 00:21:06 -08002220static ssize_t
2221rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2222{
Neil Brownd7027452008-07-12 10:37:50 +10002223 unsigned long long size;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002224 unsigned long long oldsize = rdev->size;
NeilBrown27c529b2008-03-04 14:29:33 -08002225 mddev_t *my_mddev = rdev->mddev;
2226
Neil Brownd7027452008-07-12 10:37:50 +10002227 if (strict_strtoull(buf, 10, &size) < 0)
2228 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002229 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002230 if (my_mddev->persistent) {
2231 size = super_types[my_mddev->major_version].
Andre Noll15f4a5f2008-07-21 14:42:12 +10002232 rdev_size_change(rdev, size * 2);
Chris Webb0cd17fe2008-06-28 08:31:46 +10002233 if (!size)
2234 return -EBUSY;
2235 } else if (!size) {
2236 size = (rdev->bdev->bd_inode->i_size >> 10);
2237 size -= rdev->data_offset/2;
2238 }
Chris Webb0cd17fe2008-06-28 08:31:46 +10002239 }
Chris Webb7d3c6f82008-10-13 11:55:11 +11002240 if (size < my_mddev->size)
2241 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002242
NeilBrown83303b62006-01-06 00:21:06 -08002243 rdev->size = size;
Neil Brownd7027452008-07-12 10:37:50 +10002244 if (size > oldsize && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002245 /* need to check that all other rdevs with the same ->bdev
2246 * do not overlap. We need to unlock the mddev to avoid
2247 * a deadlock. We have already changed rdev->size, and if
2248 * we have to change it back, we will have the lock again.
2249 */
2250 mddev_t *mddev;
2251 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002252 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002253
NeilBrown27c529b2008-03-04 14:29:33 -08002254 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002255 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002256 mdk_rdev_t *rdev2;
2257
2258 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002259 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002260 if (test_bit(AllReserved, &rdev2->flags) ||
2261 (rdev->bdev == rdev2->bdev &&
2262 rdev != rdev2 &&
Andre Nolld07bd3b2008-07-21 14:42:07 +10002263 overlaps(rdev->data_offset, rdev->size * 2,
2264 rdev2->data_offset,
2265 rdev2->size * 2))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002266 overlap = 1;
2267 break;
2268 }
2269 mddev_unlock(mddev);
2270 if (overlap) {
2271 mddev_put(mddev);
2272 break;
2273 }
2274 }
NeilBrown27c529b2008-03-04 14:29:33 -08002275 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002276 if (overlap) {
2277 /* Someone else could have slipped in a size
2278 * change here, but doing so is just silly.
2279 * We put oldsize back because we *know* it is
2280 * safe, and trust userspace not to race with
2281 * itself
2282 */
2283 rdev->size = oldsize;
2284 return -EBUSY;
2285 }
2286 }
NeilBrown83303b62006-01-06 00:21:06 -08002287 return len;
2288}
2289
2290static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002291__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002292
NeilBrown86e6ffd2005-11-08 21:39:24 -08002293static struct attribute *rdev_default_attrs[] = {
2294 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002295 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002296 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002297 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002298 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002299 NULL,
2300};
2301static ssize_t
2302rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2303{
2304 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2305 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002306 mddev_t *mddev = rdev->mddev;
2307 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002308
2309 if (!entry->show)
2310 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002311
2312 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2313 if (!rv) {
2314 if (rdev->mddev == NULL)
2315 rv = -EBUSY;
2316 else
2317 rv = entry->show(rdev, page);
2318 mddev_unlock(mddev);
2319 }
2320 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002321}
2322
2323static ssize_t
2324rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2325 const char *page, size_t length)
2326{
2327 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2328 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002329 ssize_t rv;
2330 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002331
2332 if (!entry->store)
2333 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002334 if (!capable(CAP_SYS_ADMIN))
2335 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002336 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002337 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002338 if (rdev->mddev == NULL)
2339 rv = -EBUSY;
2340 else
2341 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002342 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002343 }
2344 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002345}
2346
2347static void rdev_free(struct kobject *ko)
2348{
2349 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2350 kfree(rdev);
2351}
2352static struct sysfs_ops rdev_sysfs_ops = {
2353 .show = rdev_attr_show,
2354 .store = rdev_attr_store,
2355};
2356static struct kobj_type rdev_ktype = {
2357 .release = rdev_free,
2358 .sysfs_ops = &rdev_sysfs_ops,
2359 .default_attrs = rdev_default_attrs,
2360};
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362/*
2363 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2364 *
2365 * mark the device faulty if:
2366 *
2367 * - the device is nonexistent (zero size)
2368 * - the device has no valid superblock
2369 *
2370 * a faulty rdev _never_ has rdev->sb set.
2371 */
2372static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2373{
2374 char b[BDEVNAME_SIZE];
2375 int err;
2376 mdk_rdev_t *rdev;
2377 sector_t size;
2378
NeilBrown9ffae0c2006-01-06 00:20:32 -08002379 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (!rdev) {
2381 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2382 return ERR_PTR(-ENOMEM);
2383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 if ((err = alloc_disk_sb(rdev)))
2386 goto abort_free;
2387
NeilBrownc5d79ad2008-02-06 01:39:54 -08002388 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (err)
2390 goto abort_free;
2391
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002392 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002395 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002396 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002397 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002399 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002401 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002402 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2405 if (!size) {
2406 printk(KERN_WARNING
2407 "md: %s has zero or unknown size, marking faulty!\n",
2408 bdevname(rdev->bdev,b));
2409 err = -EINVAL;
2410 goto abort_free;
2411 }
2412
2413 if (super_format >= 0) {
2414 err = super_types[super_format].
2415 load_super(rdev, NULL, super_minor);
2416 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002417 printk(KERN_WARNING
2418 "md: %s does not have a valid v%d.%d "
2419 "superblock, not importing!\n",
2420 bdevname(rdev->bdev,b),
2421 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto abort_free;
2423 }
2424 if (err < 0) {
2425 printk(KERN_WARNING
2426 "md: could not read %s's sb, not importing!\n",
2427 bdevname(rdev->bdev,b));
2428 goto abort_free;
2429 }
2430 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002433 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 return rdev;
2436
2437abort_free:
2438 if (rdev->sb_page) {
2439 if (rdev->bdev)
2440 unlock_rdev(rdev);
2441 free_disk_sb(rdev);
2442 }
2443 kfree(rdev);
2444 return ERR_PTR(err);
2445}
2446
2447/*
2448 * Check a full RAID array for plausibility
2449 */
2450
2451
NeilBrowna757e642005-04-16 15:26:42 -07002452static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453{
2454 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002455 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 char b[BDEVNAME_SIZE];
2457
2458 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002459 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 switch (super_types[mddev->major_version].
2461 load_super(rdev, freshest, mddev->minor_version)) {
2462 case 1:
2463 freshest = rdev;
2464 break;
2465 case 0:
2466 break;
2467 default:
2468 printk( KERN_ERR \
2469 "md: fatal superblock inconsistency in %s"
2470 " -- removing from array\n",
2471 bdevname(rdev->bdev,b));
2472 kick_rdev_from_array(rdev);
2473 }
2474
2475
2476 super_types[mddev->major_version].
2477 validate_super(mddev, freshest);
2478
2479 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002480 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002481 if (rdev->desc_nr >= mddev->max_disks ||
2482 i > mddev->max_disks) {
2483 printk(KERN_WARNING
2484 "md: %s: %s: only %d devices permitted\n",
2485 mdname(mddev), bdevname(rdev->bdev, b),
2486 mddev->max_disks);
2487 kick_rdev_from_array(rdev);
2488 continue;
2489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (rdev != freshest)
2491 if (super_types[mddev->major_version].
2492 validate_super(mddev, rdev)) {
2493 printk(KERN_WARNING "md: kicking non-fresh %s"
2494 " from array!\n",
2495 bdevname(rdev->bdev,b));
2496 kick_rdev_from_array(rdev);
2497 continue;
2498 }
2499 if (mddev->level == LEVEL_MULTIPATH) {
2500 rdev->desc_nr = i++;
2501 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002502 set_bit(In_sync, &rdev->flags);
NeilBrowna778b732007-05-23 13:58:10 -07002503 } else if (rdev->raid_disk >= mddev->raid_disks) {
2504 rdev->raid_disk = -1;
2505 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507 }
2508
2509
2510
2511 if (mddev->recovery_cp != MaxSector &&
2512 mddev->level >= 1)
2513 printk(KERN_ERR "md: %s: raid array is not clean"
2514 " -- starting background reconstruction\n",
2515 mdname(mddev));
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
NeilBrown19052c02008-08-05 15:54:13 +10002519static void md_safemode_timeout(unsigned long data);
2520
NeilBrowneae17012005-11-08 21:39:23 -08002521static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002522safe_delay_show(mddev_t *mddev, char *page)
2523{
2524 int msec = (mddev->safemode_delay*1000)/HZ;
2525 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2526}
2527static ssize_t
2528safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2529{
2530 int scale=1;
2531 int dot=0;
2532 int i;
2533 unsigned long msec;
2534 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002535
NeilBrown16f17b32006-06-26 00:27:37 -07002536 /* remove a period, and count digits after it */
2537 if (len >= sizeof(buf))
2538 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002539 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002540 for (i=0; i<len; i++) {
2541 if (dot) {
2542 if (isdigit(buf[i])) {
2543 buf[i-1] = buf[i];
2544 scale *= 10;
2545 }
2546 buf[i] = 0;
2547 } else if (buf[i] == '.') {
2548 dot=1;
2549 buf[i] = 0;
2550 }
2551 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002552 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002553 return -EINVAL;
2554 msec = (msec * 1000) / scale;
2555 if (msec == 0)
2556 mddev->safemode_delay = 0;
2557 else {
NeilBrown19052c02008-08-05 15:54:13 +10002558 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002559 mddev->safemode_delay = (msec*HZ)/1000;
2560 if (mddev->safemode_delay == 0)
2561 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002562 if (mddev->safemode_delay < old_delay)
2563 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002564 }
2565 return len;
2566}
2567static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002568__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002569
2570static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002571level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002572{
NeilBrown2604b702006-01-06 00:20:36 -08002573 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002574 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002575 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002576 else if (mddev->clevel[0])
2577 return sprintf(page, "%s\n", mddev->clevel);
2578 else if (mddev->level != LEVEL_NONE)
2579 return sprintf(page, "%d\n", mddev->level);
2580 else
2581 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002582}
2583
NeilBrownd9d166c2006-01-06 00:20:51 -08002584static ssize_t
2585level_store(mddev_t *mddev, const char *buf, size_t len)
2586{
NeilBrown20a49ff2008-02-06 01:39:57 -08002587 ssize_t rv = len;
NeilBrownd9d166c2006-01-06 00:20:51 -08002588 if (mddev->pers)
2589 return -EBUSY;
2590 if (len == 0)
2591 return 0;
2592 if (len >= sizeof(mddev->clevel))
2593 return -ENOSPC;
2594 strncpy(mddev->clevel, buf, len);
2595 if (mddev->clevel[len-1] == '\n')
2596 len--;
2597 mddev->clevel[len] = 0;
2598 mddev->level = LEVEL_NONE;
2599 return rv;
2600}
2601
2602static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002603__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002604
NeilBrownd4dbd022006-06-26 00:27:59 -07002605
2606static ssize_t
2607layout_show(mddev_t *mddev, char *page)
2608{
2609 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002610 if (mddev->reshape_position != MaxSector &&
2611 mddev->layout != mddev->new_layout)
2612 return sprintf(page, "%d (%d)\n",
2613 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002614 return sprintf(page, "%d\n", mddev->layout);
2615}
2616
2617static ssize_t
2618layout_store(mddev_t *mddev, const char *buf, size_t len)
2619{
2620 char *e;
2621 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002622
2623 if (!*buf || (*e && *e != '\n'))
2624 return -EINVAL;
2625
NeilBrown08a02ec2007-05-09 02:35:38 -07002626 if (mddev->pers)
2627 return -EBUSY;
2628 if (mddev->reshape_position != MaxSector)
2629 mddev->new_layout = n;
2630 else
2631 mddev->layout = n;
NeilBrownd4dbd022006-06-26 00:27:59 -07002632 return len;
2633}
2634static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002635__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002636
2637
NeilBrowneae17012005-11-08 21:39:23 -08002638static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002639raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002640{
NeilBrownbb636542005-11-08 21:39:45 -08002641 if (mddev->raid_disks == 0)
2642 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002643 if (mddev->reshape_position != MaxSector &&
2644 mddev->delta_disks != 0)
2645 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2646 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002647 return sprintf(page, "%d\n", mddev->raid_disks);
2648}
2649
NeilBrownda943b992006-01-06 00:20:54 -08002650static int update_raid_disks(mddev_t *mddev, int raid_disks);
2651
2652static ssize_t
2653raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2654{
NeilBrownda943b992006-01-06 00:20:54 -08002655 char *e;
2656 int rv = 0;
2657 unsigned long n = simple_strtoul(buf, &e, 10);
2658
2659 if (!*buf || (*e && *e != '\n'))
2660 return -EINVAL;
2661
2662 if (mddev->pers)
2663 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002664 else if (mddev->reshape_position != MaxSector) {
2665 int olddisks = mddev->raid_disks - mddev->delta_disks;
2666 mddev->delta_disks = n - olddisks;
2667 mddev->raid_disks = n;
2668 } else
NeilBrownda943b992006-01-06 00:20:54 -08002669 mddev->raid_disks = n;
2670 return rv ? rv : len;
2671}
2672static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002673__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002674
NeilBrown24dd4692005-11-08 21:39:26 -08002675static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002676chunk_size_show(mddev_t *mddev, char *page)
2677{
NeilBrown08a02ec2007-05-09 02:35:38 -07002678 if (mddev->reshape_position != MaxSector &&
2679 mddev->chunk_size != mddev->new_chunk)
2680 return sprintf(page, "%d (%d)\n", mddev->new_chunk,
2681 mddev->chunk_size);
NeilBrown3b343802006-01-06 00:20:47 -08002682 return sprintf(page, "%d\n", mddev->chunk_size);
2683}
2684
2685static ssize_t
2686chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2687{
2688 /* can only set chunk_size if array is not yet active */
2689 char *e;
2690 unsigned long n = simple_strtoul(buf, &e, 10);
2691
NeilBrown3b343802006-01-06 00:20:47 -08002692 if (!*buf || (*e && *e != '\n'))
2693 return -EINVAL;
2694
NeilBrown08a02ec2007-05-09 02:35:38 -07002695 if (mddev->pers)
2696 return -EBUSY;
2697 else if (mddev->reshape_position != MaxSector)
2698 mddev->new_chunk = n;
2699 else
2700 mddev->chunk_size = n;
NeilBrown3b343802006-01-06 00:20:47 -08002701 return len;
2702}
2703static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002704__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002705
NeilBrowna94213b2006-06-26 00:28:00 -07002706static ssize_t
2707resync_start_show(mddev_t *mddev, char *page)
2708{
2709 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2710}
2711
2712static ssize_t
2713resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2714{
NeilBrowna94213b2006-06-26 00:28:00 -07002715 char *e;
2716 unsigned long long n = simple_strtoull(buf, &e, 10);
2717
2718 if (mddev->pers)
2719 return -EBUSY;
2720 if (!*buf || (*e && *e != '\n'))
2721 return -EINVAL;
2722
2723 mddev->recovery_cp = n;
2724 return len;
2725}
2726static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002727__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002728
NeilBrown9e653b62006-06-26 00:27:58 -07002729/*
2730 * The array state can be:
2731 *
2732 * clear
2733 * No devices, no size, no level
2734 * Equivalent to STOP_ARRAY ioctl
2735 * inactive
2736 * May have some settings, but array is not active
2737 * all IO results in error
2738 * When written, doesn't tear down array, but just stops it
2739 * suspended (not supported yet)
2740 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002741 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002742 * readonly
2743 * no resync can happen. no superblocks get written.
2744 * write requests fail
2745 * read-auto
2746 * like readonly, but behaves like 'clean' on a write request.
2747 *
2748 * clean - no pending writes, but otherwise active.
2749 * When written to inactive array, starts without resync
2750 * If a write request arrives then
2751 * if metadata is known, mark 'dirty' and switch to 'active'.
2752 * if not known, block and switch to write-pending
2753 * If written to an active array that has pending writes, then fails.
2754 * active
2755 * fully active: IO and resync can be happening.
2756 * When written to inactive array, starts with resync
2757 *
2758 * write-pending
2759 * clean, but writes are blocked waiting for 'active' to be written.
2760 *
2761 * active-idle
2762 * like active, but no writes have been seen for a while (100msec).
2763 *
2764 */
2765enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
2766 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07002767static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07002768 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
2769 "write-pending", "active-idle", NULL };
2770
2771static int match_word(const char *word, char **list)
2772{
2773 int n;
2774 for (n=0; list[n]; n++)
2775 if (cmd_match(word, list[n]))
2776 break;
2777 return n;
2778}
2779
2780static ssize_t
2781array_state_show(mddev_t *mddev, char *page)
2782{
2783 enum array_state st = inactive;
2784
2785 if (mddev->pers)
2786 switch(mddev->ro) {
2787 case 1:
2788 st = readonly;
2789 break;
2790 case 2:
2791 st = read_auto;
2792 break;
2793 case 0:
2794 if (mddev->in_sync)
2795 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08002796 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
2797 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07002798 else if (mddev->safemode)
2799 st = active_idle;
2800 else
2801 st = active;
2802 }
2803 else {
2804 if (list_empty(&mddev->disks) &&
2805 mddev->raid_disks == 0 &&
2806 mddev->size == 0)
2807 st = clear;
2808 else
2809 st = inactive;
2810 }
2811 return sprintf(page, "%s\n", array_states[st]);
2812}
2813
Neil Browndf5b20c2008-07-11 22:02:22 +10002814static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07002815static int do_md_run(mddev_t * mddev);
2816static int restart_array(mddev_t *mddev);
2817
2818static ssize_t
2819array_state_store(mddev_t *mddev, const char *buf, size_t len)
2820{
2821 int err = -EINVAL;
2822 enum array_state st = match_word(buf, array_states);
2823 switch(st) {
2824 case bad_word:
2825 break;
2826 case clear:
2827 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10002828 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08002829 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002830 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002831 break;
2832 case inactive:
2833 /* stopping an active array */
2834 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10002835 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07002836 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10002837 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08002838 } else
2839 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07002840 break;
2841 case suspended:
2842 break; /* not supported yet */
2843 case readonly:
2844 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10002845 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002846 else {
2847 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07002848 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07002849 err = do_md_run(mddev);
2850 }
2851 break;
2852 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07002853 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11002854 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10002855 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11002856 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07002857 err = restart_array(mddev);
2858 if (err == 0) {
2859 mddev->ro = 2;
2860 set_disk_ro(mddev->gendisk, 0);
2861 }
NeilBrown9e653b62006-06-26 00:27:58 -07002862 } else {
2863 mddev->ro = 2;
2864 err = do_md_run(mddev);
2865 }
2866 break;
2867 case clean:
2868 if (mddev->pers) {
2869 restart_array(mddev);
2870 spin_lock_irq(&mddev->write_lock);
2871 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08002872 if (mddev->in_sync == 0) {
2873 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07002874 if (mddev->safemode == 1)
2875 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08002876 if (mddev->persistent)
2877 set_bit(MD_CHANGE_CLEAN,
2878 &mddev->flags);
2879 }
2880 err = 0;
2881 } else
2882 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07002883 spin_unlock_irq(&mddev->write_lock);
2884 } else {
2885 mddev->ro = 0;
2886 mddev->recovery_cp = MaxSector;
2887 err = do_md_run(mddev);
2888 }
2889 break;
2890 case active:
2891 if (mddev->pers) {
2892 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08002893 if (mddev->external)
2894 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07002895 wake_up(&mddev->sb_wait);
2896 err = 0;
2897 } else {
2898 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07002899 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07002900 err = do_md_run(mddev);
2901 }
2902 break;
2903 case write_pending:
2904 case active_idle:
2905 /* these cannot be set */
2906 break;
2907 }
2908 if (err)
2909 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10002910 else {
NeilBrownb62b7592008-10-21 13:25:21 +11002911 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07002912 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10002913 }
NeilBrown9e653b62006-06-26 00:27:58 -07002914}
NeilBrown80ca3a42006-07-10 04:44:18 -07002915static struct md_sysfs_entry md_array_state =
2916__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07002917
NeilBrown6d7ff732006-01-06 00:21:16 -08002918static ssize_t
2919null_show(mddev_t *mddev, char *page)
2920{
2921 return -EINVAL;
2922}
2923
2924static ssize_t
2925new_dev_store(mddev_t *mddev, const char *buf, size_t len)
2926{
2927 /* buf must be %d:%d\n? giving major and minor numbers */
2928 /* The new device is added to the array.
2929 * If the array has a persistent superblock, we read the
2930 * superblock to initialise info and check validity.
2931 * Otherwise, only checking done is that in bind_rdev_to_array,
2932 * which mainly checks size.
2933 */
2934 char *e;
2935 int major = simple_strtoul(buf, &e, 10);
2936 int minor;
2937 dev_t dev;
2938 mdk_rdev_t *rdev;
2939 int err;
2940
2941 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
2942 return -EINVAL;
2943 minor = simple_strtoul(e+1, &e, 10);
2944 if (*e && *e != '\n')
2945 return -EINVAL;
2946 dev = MKDEV(major, minor);
2947 if (major != MAJOR(dev) ||
2948 minor != MINOR(dev))
2949 return -EOVERFLOW;
2950
2951
2952 if (mddev->persistent) {
2953 rdev = md_import_device(dev, mddev->major_version,
2954 mddev->minor_version);
2955 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
2956 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
2957 mdk_rdev_t, same_set);
2958 err = super_types[mddev->major_version]
2959 .load_super(rdev, rdev0, mddev->minor_version);
2960 if (err < 0)
2961 goto out;
2962 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08002963 } else if (mddev->external)
2964 rdev = md_import_device(dev, -2, -1);
2965 else
NeilBrown6d7ff732006-01-06 00:21:16 -08002966 rdev = md_import_device(dev, -1, -1);
2967
2968 if (IS_ERR(rdev))
2969 return PTR_ERR(rdev);
2970 err = bind_rdev_to_array(rdev, mddev);
2971 out:
2972 if (err)
2973 export_rdev(rdev);
2974 return err ? err : len;
2975}
2976
2977static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07002978__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08002979
2980static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07002981bitmap_store(mddev_t *mddev, const char *buf, size_t len)
2982{
2983 char *end;
2984 unsigned long chunk, end_chunk;
2985
2986 if (!mddev->bitmap)
2987 goto out;
2988 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
2989 while (*buf) {
2990 chunk = end_chunk = simple_strtoul(buf, &end, 0);
2991 if (buf == end) break;
2992 if (*end == '-') { /* range */
2993 buf = end + 1;
2994 end_chunk = simple_strtoul(buf, &end, 0);
2995 if (buf == end) break;
2996 }
2997 if (*end && !isspace(*end)) break;
2998 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
2999 buf = end;
3000 while (isspace(*buf)) buf++;
3001 }
3002 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3003out:
3004 return len;
3005}
3006
3007static struct md_sysfs_entry md_bitmap =
3008__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3009
3010static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003011size_show(mddev_t *mddev, char *page)
3012{
3013 return sprintf(page, "%llu\n", (unsigned long long)mddev->size);
3014}
3015
Andre Nolld71f9f82008-07-11 22:02:22 +10003016static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003017
3018static ssize_t
3019size_store(mddev_t *mddev, const char *buf, size_t len)
3020{
3021 /* If array is inactive, we can reduce the component size, but
3022 * not increase it (except from 0).
3023 * If array is active, we can try an on-line resize
3024 */
3025 char *e;
3026 int err = 0;
3027 unsigned long long size = simple_strtoull(buf, &e, 10);
3028 if (!*buf || *buf == '\n' ||
3029 (*e && *e != '\n'))
3030 return -EINVAL;
3031
3032 if (mddev->pers) {
Andre Nolld71f9f82008-07-11 22:02:22 +10003033 err = update_size(mddev, size * 2);
NeilBrown850b2b42006-10-03 01:15:46 -07003034 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003035 } else {
3036 if (mddev->size == 0 ||
3037 mddev->size > size)
3038 mddev->size = size;
3039 else
3040 err = -ENOSPC;
3041 }
3042 return err ? err : len;
3043}
3044
3045static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003046__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003047
NeilBrown8bb93aa2006-01-06 00:20:50 -08003048
3049/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003050 * This is one of
3051 * 'none' for arrays with no metadata (good luck...)
3052 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003053 * or N.M for internally known formats
3054 */
3055static ssize_t
3056metadata_show(mddev_t *mddev, char *page)
3057{
3058 if (mddev->persistent)
3059 return sprintf(page, "%d.%d\n",
3060 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003061 else if (mddev->external)
3062 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003063 else
3064 return sprintf(page, "none\n");
3065}
3066
3067static ssize_t
3068metadata_store(mddev_t *mddev, const char *buf, size_t len)
3069{
3070 int major, minor;
3071 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003072 /* Changing the details of 'external' metadata is
3073 * always permitted. Otherwise there must be
3074 * no devices attached to the array.
3075 */
3076 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3077 ;
3078 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003079 return -EBUSY;
3080
3081 if (cmd_match(buf, "none")) {
3082 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003083 mddev->external = 0;
3084 mddev->major_version = 0;
3085 mddev->minor_version = 90;
3086 return len;
3087 }
3088 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003089 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003090 if (namelen >= sizeof(mddev->metadata_type))
3091 namelen = sizeof(mddev->metadata_type)-1;
3092 strncpy(mddev->metadata_type, buf+9, namelen);
3093 mddev->metadata_type[namelen] = 0;
3094 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3095 mddev->metadata_type[--namelen] = 0;
3096 mddev->persistent = 0;
3097 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003098 mddev->major_version = 0;
3099 mddev->minor_version = 90;
3100 return len;
3101 }
3102 major = simple_strtoul(buf, &e, 10);
3103 if (e==buf || *e != '.')
3104 return -EINVAL;
3105 buf = e+1;
3106 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003107 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003108 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003109 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003110 return -ENOENT;
3111 mddev->major_version = major;
3112 mddev->minor_version = minor;
3113 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003114 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003115 return len;
3116}
3117
3118static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003119__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003120
NeilBrowna35b0d62006-01-06 00:20:49 -08003121static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003122action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003123{
NeilBrown7eec3142005-11-08 21:39:44 -08003124 char *type = "idle";
NeilBrown31399d92005-11-08 21:39:29 -08003125 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003126 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003127 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3128 type = "reshape";
3129 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003130 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3131 type = "resync";
3132 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3133 type = "check";
3134 else
3135 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003136 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003137 type = "recover";
3138 }
3139 return sprintf(page, "%s\n", type);
3140}
3141
3142static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003143action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003144{
NeilBrown7eec3142005-11-08 21:39:44 -08003145 if (!mddev->pers || !mddev->pers->sync_request)
3146 return -EINVAL;
3147
NeilBrownbce74da2006-01-06 00:20:41 -08003148 if (cmd_match(page, "idle")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003149 if (mddev->sync_thread) {
3150 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3151 md_unregister_thread(mddev->sync_thread);
3152 mddev->sync_thread = NULL;
3153 mddev->recovery = 0;
3154 }
NeilBrown03c902e2006-01-06 00:20:46 -08003155 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3156 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003157 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003158 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003159 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003160 else if (cmd_match(page, "recover")) {
3161 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3162 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3163 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003164 int err;
3165 if (mddev->pers->start_reshape == NULL)
3166 return -EINVAL;
3167 err = mddev->pers->start_reshape(mddev);
3168 if (err)
3169 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003170 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003171 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003172 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003173 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003174 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003175 return -EINVAL;
3176 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3177 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003178 }
NeilBrown03c902e2006-01-06 00:20:46 -08003179 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003180 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003181 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003182 return len;
3183}
3184
NeilBrown9d888832005-11-08 21:39:26 -08003185static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003186mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003187{
3188 return sprintf(page, "%llu\n",
3189 (unsigned long long) mddev->resync_mismatches);
3190}
3191
NeilBrown80ca3a42006-07-10 04:44:18 -07003192static struct md_sysfs_entry md_scan_mode =
3193__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003194
NeilBrown96de1e62005-11-08 21:39:39 -08003195
NeilBrown80ca3a42006-07-10 04:44:18 -07003196static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003197
NeilBrown88202a02006-01-06 00:21:36 -08003198static ssize_t
3199sync_min_show(mddev_t *mddev, char *page)
3200{
3201 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3202 mddev->sync_speed_min ? "local": "system");
3203}
3204
3205static ssize_t
3206sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3207{
3208 int min;
3209 char *e;
3210 if (strncmp(buf, "system", 6)==0) {
3211 mddev->sync_speed_min = 0;
3212 return len;
3213 }
3214 min = simple_strtoul(buf, &e, 10);
3215 if (buf == e || (*e && *e != '\n') || min <= 0)
3216 return -EINVAL;
3217 mddev->sync_speed_min = min;
3218 return len;
3219}
3220
3221static struct md_sysfs_entry md_sync_min =
3222__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3223
3224static ssize_t
3225sync_max_show(mddev_t *mddev, char *page)
3226{
3227 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3228 mddev->sync_speed_max ? "local": "system");
3229}
3230
3231static ssize_t
3232sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3233{
3234 int max;
3235 char *e;
3236 if (strncmp(buf, "system", 6)==0) {
3237 mddev->sync_speed_max = 0;
3238 return len;
3239 }
3240 max = simple_strtoul(buf, &e, 10);
3241 if (buf == e || (*e && *e != '\n') || max <= 0)
3242 return -EINVAL;
3243 mddev->sync_speed_max = max;
3244 return len;
3245}
3246
3247static struct md_sysfs_entry md_sync_max =
3248__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3249
Iustin Popd7f3d292007-10-16 23:30:54 -07003250static ssize_t
3251degraded_show(mddev_t *mddev, char *page)
3252{
3253 return sprintf(page, "%d\n", mddev->degraded);
3254}
3255static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003256
3257static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003258sync_force_parallel_show(mddev_t *mddev, char *page)
3259{
3260 return sprintf(page, "%d\n", mddev->parallel_resync);
3261}
3262
3263static ssize_t
3264sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3265{
3266 long n;
3267
3268 if (strict_strtol(buf, 10, &n))
3269 return -EINVAL;
3270
3271 if (n != 0 && n != 1)
3272 return -EINVAL;
3273
3274 mddev->parallel_resync = n;
3275
3276 if (mddev->sync_thread)
3277 wake_up(&resync_wait);
3278
3279 return len;
3280}
3281
3282/* force parallel resync, even with shared block devices */
3283static struct md_sysfs_entry md_sync_force_parallel =
3284__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3285 sync_force_parallel_show, sync_force_parallel_store);
3286
3287static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003288sync_speed_show(mddev_t *mddev, char *page)
3289{
3290 unsigned long resync, dt, db;
Andre Noll9687a602008-03-25 22:24:09 +01003291 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3292 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003293 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003294 db = resync - mddev->resync_mark_cnt;
3295 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003296}
3297
NeilBrown80ca3a42006-07-10 04:44:18 -07003298static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003299
3300static ssize_t
3301sync_completed_show(mddev_t *mddev, char *page)
3302{
3303 unsigned long max_blocks, resync;
3304
3305 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3306 max_blocks = mddev->resync_max_sectors;
3307 else
3308 max_blocks = mddev->size << 1;
3309
3310 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active));
3311 return sprintf(page, "%lu / %lu\n", resync, max_blocks);
3312}
3313
NeilBrown80ca3a42006-07-10 04:44:18 -07003314static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003315
NeilBrowne464eaf2006-03-27 01:18:14 -08003316static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003317min_sync_show(mddev_t *mddev, char *page)
3318{
3319 return sprintf(page, "%llu\n",
3320 (unsigned long long)mddev->resync_min);
3321}
3322static ssize_t
3323min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3324{
3325 unsigned long long min;
3326 if (strict_strtoull(buf, 10, &min))
3327 return -EINVAL;
3328 if (min > mddev->resync_max)
3329 return -EINVAL;
3330 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3331 return -EBUSY;
3332
3333 /* Must be a multiple of chunk_size */
3334 if (mddev->chunk_size) {
3335 if (min & (sector_t)((mddev->chunk_size>>9)-1))
3336 return -EINVAL;
3337 }
3338 mddev->resync_min = min;
3339
3340 return len;
3341}
3342
3343static struct md_sysfs_entry md_min_sync =
3344__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3345
3346static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003347max_sync_show(mddev_t *mddev, char *page)
3348{
3349 if (mddev->resync_max == MaxSector)
3350 return sprintf(page, "max\n");
3351 else
3352 return sprintf(page, "%llu\n",
3353 (unsigned long long)mddev->resync_max);
3354}
3355static ssize_t
3356max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3357{
3358 if (strncmp(buf, "max", 3) == 0)
3359 mddev->resync_max = MaxSector;
3360 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003361 unsigned long long max;
3362 if (strict_strtoull(buf, 10, &max))
3363 return -EINVAL;
3364 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003365 return -EINVAL;
3366 if (max < mddev->resync_max &&
3367 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3368 return -EBUSY;
3369
3370 /* Must be a multiple of chunk_size */
3371 if (mddev->chunk_size) {
3372 if (max & (sector_t)((mddev->chunk_size>>9)-1))
3373 return -EINVAL;
3374 }
3375 mddev->resync_max = max;
3376 }
3377 wake_up(&mddev->recovery_wait);
3378 return len;
3379}
3380
3381static struct md_sysfs_entry md_max_sync =
3382__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3383
3384static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003385suspend_lo_show(mddev_t *mddev, char *page)
3386{
3387 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3388}
3389
3390static ssize_t
3391suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3392{
3393 char *e;
3394 unsigned long long new = simple_strtoull(buf, &e, 10);
3395
3396 if (mddev->pers->quiesce == NULL)
3397 return -EINVAL;
3398 if (buf == e || (*e && *e != '\n'))
3399 return -EINVAL;
3400 if (new >= mddev->suspend_hi ||
3401 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3402 mddev->suspend_lo = new;
3403 mddev->pers->quiesce(mddev, 2);
3404 return len;
3405 } else
3406 return -EINVAL;
3407}
3408static struct md_sysfs_entry md_suspend_lo =
3409__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3410
3411
3412static ssize_t
3413suspend_hi_show(mddev_t *mddev, char *page)
3414{
3415 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3416}
3417
3418static ssize_t
3419suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3420{
3421 char *e;
3422 unsigned long long new = simple_strtoull(buf, &e, 10);
3423
3424 if (mddev->pers->quiesce == NULL)
3425 return -EINVAL;
3426 if (buf == e || (*e && *e != '\n'))
3427 return -EINVAL;
3428 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3429 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3430 mddev->suspend_hi = new;
3431 mddev->pers->quiesce(mddev, 1);
3432 mddev->pers->quiesce(mddev, 0);
3433 return len;
3434 } else
3435 return -EINVAL;
3436}
3437static struct md_sysfs_entry md_suspend_hi =
3438__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3439
NeilBrown08a02ec2007-05-09 02:35:38 -07003440static ssize_t
3441reshape_position_show(mddev_t *mddev, char *page)
3442{
3443 if (mddev->reshape_position != MaxSector)
3444 return sprintf(page, "%llu\n",
3445 (unsigned long long)mddev->reshape_position);
3446 strcpy(page, "none\n");
3447 return 5;
3448}
3449
3450static ssize_t
3451reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3452{
3453 char *e;
3454 unsigned long long new = simple_strtoull(buf, &e, 10);
3455 if (mddev->pers)
3456 return -EBUSY;
3457 if (buf == e || (*e && *e != '\n'))
3458 return -EINVAL;
3459 mddev->reshape_position = new;
3460 mddev->delta_disks = 0;
3461 mddev->new_level = mddev->level;
3462 mddev->new_layout = mddev->layout;
3463 mddev->new_chunk = mddev->chunk_size;
3464 return len;
3465}
3466
3467static struct md_sysfs_entry md_reshape_position =
3468__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3469 reshape_position_store);
3470
NeilBrowne464eaf2006-03-27 01:18:14 -08003471
NeilBrowneae17012005-11-08 21:39:23 -08003472static struct attribute *md_default_attrs[] = {
3473 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003474 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003475 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003476 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003477 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003478 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003479 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003480 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003481 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003482 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003483 &md_reshape_position.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003484 NULL,
3485};
3486
3487static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003488 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003489 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003490 &md_sync_min.attr,
3491 &md_sync_max.attr,
3492 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003493 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003494 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003495 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003496 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003497 &md_suspend_lo.attr,
3498 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003499 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003500 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003501 NULL,
3502};
NeilBrown411036f2005-11-08 21:39:40 -08003503static struct attribute_group md_redundancy_group = {
3504 .name = NULL,
3505 .attrs = md_redundancy_attrs,
3506};
3507
NeilBrowneae17012005-11-08 21:39:23 -08003508
3509static ssize_t
3510md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3511{
3512 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3513 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003514 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003515
3516 if (!entry->show)
3517 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003518 rv = mddev_lock(mddev);
3519 if (!rv) {
3520 rv = entry->show(mddev, page);
3521 mddev_unlock(mddev);
3522 }
NeilBrown96de1e62005-11-08 21:39:39 -08003523 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003524}
3525
3526static ssize_t
3527md_attr_store(struct kobject *kobj, struct attribute *attr,
3528 const char *page, size_t length)
3529{
3530 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3531 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003532 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003533
3534 if (!entry->store)
3535 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003536 if (!capable(CAP_SYS_ADMIN))
3537 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003538 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003539 if (mddev->hold_active == UNTIL_IOCTL)
3540 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003541 if (!rv) {
3542 rv = entry->store(mddev, page, length);
3543 mddev_unlock(mddev);
3544 }
NeilBrown96de1e62005-11-08 21:39:39 -08003545 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003546}
3547
3548static void md_free(struct kobject *ko)
3549{
3550 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003551
3552 if (mddev->sysfs_state)
3553 sysfs_put(mddev->sysfs_state);
3554
3555 if (mddev->gendisk) {
3556 del_gendisk(mddev->gendisk);
3557 put_disk(mddev->gendisk);
3558 }
3559 if (mddev->queue)
3560 blk_cleanup_queue(mddev->queue);
3561
NeilBrowneae17012005-11-08 21:39:23 -08003562 kfree(mddev);
3563}
3564
3565static struct sysfs_ops md_sysfs_ops = {
3566 .show = md_attr_show,
3567 .store = md_attr_store,
3568};
3569static struct kobj_type md_ktype = {
3570 .release = md_free,
3571 .sysfs_ops = &md_sysfs_ops,
3572 .default_attrs = md_default_attrs,
3573};
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575int mdp_major = 0;
3576
NeilBrownefeb53c2009-01-09 08:31:10 +11003577static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003579 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 mddev_t *mddev = mddev_find(dev);
3581 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003582 int partitioned;
3583 int shift;
3584 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003585 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
3587 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003588 return -ENODEV;
3589
3590 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3591 shift = partitioned ? MdpMinorShift : 0;
3592 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
NeilBrownd3374822009-01-09 08:31:10 +11003594 /* wait for any previous instance if this device
3595 * to be completed removed (mddev_delayed_delete).
3596 */
3597 flush_scheduled_work();
3598
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003599 mutex_lock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 if (mddev->gendisk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003601 mutex_unlock(&disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003603 return -EEXIST;
3604 }
3605
3606 if (name) {
3607 /* Need to ensure that 'name' is not a duplicate.
3608 */
3609 mddev_t *mddev2;
3610 spin_lock(&all_mddevs_lock);
3611
3612 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3613 if (mddev2->gendisk &&
3614 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3615 spin_unlock(&all_mddevs_lock);
3616 return -EEXIST;
3617 }
3618 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
NeilBrown8b765392009-01-09 08:31:08 +11003620
3621 mddev->queue = blk_alloc_queue(GFP_KERNEL);
3622 if (!mddev->queue) {
3623 mutex_unlock(&disks_mutex);
3624 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003625 return -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003626 }
3627 /* Can be unlocked because the queue is new: no concurrency */
3628 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3629
3630 blk_queue_make_request(mddev->queue, md_fail_request);
3631
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 disk = alloc_disk(1 << shift);
3633 if (!disk) {
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003634 mutex_unlock(&disks_mutex);
NeilBrown8b765392009-01-09 08:31:08 +11003635 blk_cleanup_queue(mddev->queue);
3636 mddev->queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003638 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003640 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003642 if (name)
3643 strcpy(disk->disk_name, name);
3644 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003646 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 disk->fops = &md_fops;
3649 disk->private_data = mddev;
3650 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003651 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003652 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003653 * remove it now.
3654 */
3655 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 add_disk(disk);
3657 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003658 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3659 &disk_to_dev(disk)->kobj, "%s", "md");
Neil Brownf48ed532008-06-28 08:31:26 +10003660 mutex_unlock(&disks_mutex);
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003661 if (error)
NeilBrown5e55e2f2007-03-26 21:32:14 -08003662 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3663 disk->disk_name);
NeilBrownb62b7592008-10-21 13:25:21 +11003664 else {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003665 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003666 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3667 }
NeilBrownd3374822009-01-09 08:31:10 +11003668 mddev_put(mddev);
NeilBrownefeb53c2009-01-09 08:31:10 +11003669 return 0;
3670}
3671
3672static struct kobject *md_probe(dev_t dev, int *part, void *data)
3673{
3674 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return NULL;
3676}
3677
NeilBrownefeb53c2009-01-09 08:31:10 +11003678static int add_named_array(const char *val, struct kernel_param *kp)
3679{
3680 /* val must be "md_*" where * is not all digits.
3681 * We allocate an array with a large free minor number, and
3682 * set the name to val. val must not already be an active name.
3683 */
3684 int len = strlen(val);
3685 char buf[DISK_NAME_LEN];
3686
3687 while (len && val[len-1] == '\n')
3688 len--;
3689 if (len >= DISK_NAME_LEN)
3690 return -E2BIG;
3691 strlcpy(buf, val, len+1);
3692 if (strncmp(buf, "md_", 3) != 0)
3693 return -EINVAL;
3694 return md_alloc(0, buf);
3695}
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697static void md_safemode_timeout(unsigned long data)
3698{
3699 mddev_t *mddev = (mddev_t *) data;
3700
Neil Brown0fd62b82008-06-28 08:31:36 +10003701 if (!atomic_read(&mddev->writes_pending)) {
3702 mddev->safemode = 1;
3703 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11003704 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10003705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 md_wakeup_thread(mddev->thread);
3707}
3708
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003709static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711static int do_md_run(mddev_t * mddev)
3712{
NeilBrown2604b702006-01-06 00:20:36 -08003713 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 int chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 mdk_rdev_t *rdev;
3716 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08003717 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 char b[BDEVNAME_SIZE];
3719
NeilBrowna757e642005-04-16 15:26:42 -07003720 if (list_empty(&mddev->disks))
3721 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724 if (mddev->pers)
3725 return -EBUSY;
3726
3727 /*
3728 * Analyze all RAID superblock(s)
3729 */
NeilBrown1ec4a932008-02-06 01:39:53 -08003730 if (!mddev->raid_disks) {
3731 if (!mddev->persistent)
3732 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07003733 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08003734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
3736 chunk_size = mddev->chunk_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
NeilBrown2604b702006-01-06 00:20:36 -08003738 if (chunk_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 if (chunk_size > MAX_CHUNK_SIZE) {
3740 printk(KERN_ERR "too big chunk_size: %d > %d\n",
3741 chunk_size, MAX_CHUNK_SIZE);
3742 return -EINVAL;
3743 }
3744 /*
NeilBrown4bbf3772008-10-13 11:55:12 +11003745 * chunk-size has to be a power of 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 */
3747 if ( (1 << ffz(~chunk_size)) != chunk_size) {
NeilBrowna757e642005-04-16 15:26:42 -07003748 printk(KERN_ERR "chunk_size of %d not valid\n", chunk_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return -EINVAL;
3750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 /* devices must have minimum size of one chunk */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003753 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003754 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 continue;
3756 if (rdev->size < chunk_size / 1024) {
3757 printk(KERN_WARNING
3758 "md: Dev %s smaller than chunk_size:"
3759 " %lluk < %dk\n",
3760 bdevname(rdev->bdev,b),
3761 (unsigned long long)rdev->size,
3762 chunk_size / 1024);
3763 return -EINVAL;
3764 }
3765 }
3766 }
3767
NeilBrownd9d166c2006-01-06 00:20:51 -08003768 if (mddev->level != LEVEL_NONE)
3769 request_module("md-level-%d", mddev->level);
3770 else if (mddev->clevel[0])
3771 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
3773 /*
3774 * Drop all container device buffers, from now on
3775 * the only valid external interface is through the md
3776 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003778 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08003779 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 continue;
3781 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07003782 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07003783
3784 /* perform some consistency tests on the device.
3785 * We don't want the data to overlap the metadata,
3786 * Internal Bitmap issues has handled elsewhere.
3787 */
Andre Noll0f420352008-07-11 22:02:23 +10003788 if (rdev->data_offset < rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003789 if (mddev->size &&
3790 rdev->data_offset + mddev->size*2
Andre Noll0f420352008-07-11 22:02:23 +10003791 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07003792 printk("md: %s: data overlaps metadata\n",
3793 mdname(mddev));
3794 return -EINVAL;
3795 }
3796 } else {
Andre Noll0f420352008-07-11 22:02:23 +10003797 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07003798 > rdev->data_offset) {
3799 printk("md: %s: metadata overlaps data\n",
3800 mdname(mddev));
3801 return -EINVAL;
3802 }
3803 }
NeilBrown3c0ee632008-10-21 13:25:28 +11003804 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 }
3806
3807 md_probe(mddev->unit, NULL, NULL);
3808 disk = mddev->gendisk;
3809 if (!disk)
3810 return -ENOMEM;
3811
3812 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003813 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08003814 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003816 if (mddev->level != LEVEL_NONE)
3817 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
3818 mddev->level);
3819 else
3820 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
3821 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 return -EINVAL;
3823 }
NeilBrown2604b702006-01-06 00:20:36 -08003824 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08003826 mddev->level = pers->level;
3827 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11003829 if (pers->level >= 4 && pers->level <= 6)
3830 /* Cannot support integrity (yet) */
3831 blk_integrity_unregister(mddev->gendisk);
3832
NeilBrownf6705572006-03-27 01:18:11 -08003833 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08003834 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08003835 /* This personality cannot handle reshaping... */
3836 mddev->pers = NULL;
3837 module_put(pers->owner);
3838 return -EINVAL;
3839 }
3840
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003841 if (pers->sync_request) {
3842 /* Warn if this is a potentially silly
3843 * configuration.
3844 */
3845 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
3846 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003847 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003848
3849 list_for_each_entry(rdev, &mddev->disks, same_set)
3850 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003851 if (rdev < rdev2 &&
3852 rdev->bdev->bd_contains ==
3853 rdev2->bdev->bd_contains) {
3854 printk(KERN_WARNING
3855 "%s: WARNING: %s appears to be"
3856 " on the same physical disk as"
3857 " %s.\n",
3858 mdname(mddev),
3859 bdevname(rdev->bdev,b),
3860 bdevname(rdev2->bdev,b2));
3861 warned = 1;
3862 }
3863 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003864
NeilBrown7dd5e7c32007-02-28 20:11:35 -08003865 if (warned)
3866 printk(KERN_WARNING
3867 "True protection against single-disk"
3868 " failure might be compromised.\n");
3869 }
3870
NeilBrown657390d2005-08-26 18:34:16 -07003871 mddev->recovery = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 mddev->resync_max_sectors = mddev->size << 1; /* may be over-ridden by personality */
NeilBrowna9701a32005-11-08 21:39:34 -08003873 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08003874 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
NeilBrownf91de922005-11-08 21:39:36 -08003876 if (start_readonly)
3877 mddev->ro = 2; /* read-only, but switch on first write */
3878
NeilBrownb15c2e52006-01-06 00:20:16 -08003879 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01003880 if (err)
3881 printk(KERN_ERR "md: pers->run() failed ...\n");
3882 else if (mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08003883 err = bitmap_create(mddev);
3884 if (err) {
3885 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
3886 mdname(mddev), err);
3887 mddev->pers->stop(mddev);
3888 }
3889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 module_put(mddev->pers->owner);
3892 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07003893 bitmap_destroy(mddev);
3894 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08003896 if (mddev->pers->sync_request) {
3897 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3898 printk(KERN_WARNING
3899 "md: cannot register extra attributes for %s\n",
3900 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11003901 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08003902 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08003903 mddev->ro = 0;
3904
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 atomic_set(&mddev->writes_pending,0);
3906 mddev->safemode = 0;
3907 mddev->safemode_timer.function = md_safemode_timeout;
3908 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07003909 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08003911
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003912 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08003913 if (rdev->raid_disk >= 0) {
3914 char nm[20];
3915 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08003916 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
3917 printk("md: cannot register %s for %s\n",
3918 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08003919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
3921 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3922
NeilBrown850b2b42006-10-03 01:15:46 -07003923 if (mddev->flags)
3924 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Andre Nollf233ea52008-07-21 17:05:22 +10003926 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927
3928 /* If we call blk_queue_make_request here, it will
3929 * re-initialise max_sectors etc which may have been
3930 * refined inside -> run. So just set the bits we need to set.
3931 * Most initialisation happended when we called
3932 * blk_queue_make_request(..., md_fail_request)
3933 * earlier.
3934 */
3935 mddev->queue->queuedata = mddev;
3936 mddev->queue->make_request_fn = mddev->pers->make_request;
3937
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003938 /* If there is a partially-recovered drive we need to
3939 * start recovery here. If we leave it to md_check_recovery,
3940 * it will remove the drives and not do the right thing
3941 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07003942 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003943 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11003944 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003945 if (rdev->raid_disk >= 0 &&
3946 !test_bit(In_sync, &rdev->flags) &&
3947 !test_bit(Faulty, &rdev->flags))
3948 /* complete an interrupted recovery */
3949 spares++;
3950 if (spares && mddev->pers->sync_request) {
3951 mddev->recovery = 0;
3952 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3953 mddev->sync_thread = md_register_thread(md_do_sync,
3954 mddev,
3955 "%s_resync");
3956 if (!mddev->sync_thread) {
3957 printk(KERN_ERR "%s: could not start resync"
3958 " thread...\n",
3959 mdname(mddev));
3960 /* leave the spares where they are, it shouldn't hurt */
3961 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07003962 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003963 }
3964 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07003965 md_wakeup_thread(mddev->thread);
3966 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07003967
Linus Torvalds44ce62942007-05-09 18:51:36 -07003968 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08003969 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11003970 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11003971 if (mddev->sysfs_action)
3972 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10003973 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09003974 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 return 0;
3976}
3977
3978static int restart_array(mddev_t *mddev)
3979{
3980 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Andre Noll80fab1d2008-07-11 22:02:21 +10003982 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10003984 return -ENXIO;
3985 if (!mddev->pers)
3986 return -EINVAL;
3987 if (!mddev->ro)
3988 return -EBUSY;
3989 mddev->safemode = 0;
3990 mddev->ro = 0;
3991 set_disk_ro(disk, 0);
3992 printk(KERN_INFO "md: %s switched to read-write mode.\n",
3993 mdname(mddev));
3994 /* Kick recovery or resync if necessary */
3995 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3996 md_wakeup_thread(mddev->thread);
3997 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11003998 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10003999 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000}
4001
NeilBrownacc55e22006-06-26 00:27:47 -07004002/* similar to deny_write_access, but accounts for our holding a reference
4003 * to the file ourselves */
4004static int deny_bitmap_write_access(struct file * file)
4005{
4006 struct inode *inode = file->f_mapping->host;
4007
4008 spin_lock(&inode->i_lock);
4009 if (atomic_read(&inode->i_writecount) > 1) {
4010 spin_unlock(&inode->i_lock);
4011 return -ETXTBSY;
4012 }
4013 atomic_set(&inode->i_writecount, -1);
4014 spin_unlock(&inode->i_lock);
4015
4016 return 0;
4017}
4018
4019static void restore_bitmap_write_access(struct file *file)
4020{
4021 struct inode *inode = file->f_mapping->host;
4022
4023 spin_lock(&inode->i_lock);
4024 atomic_set(&inode->i_writecount, 1);
4025 spin_unlock(&inode->i_lock);
4026}
4027
NeilBrown9e653b62006-06-26 00:27:58 -07004028/* mode:
4029 * 0 - completely stop and dis-assemble array
4030 * 1 - switch to readonly
4031 * 2 - stop but do not disassemble array
4032 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004033static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
4035 int err = 0;
4036 struct gendisk *disk = mddev->gendisk;
4037
NeilBrownf2ea68c2008-07-21 17:05:25 +10004038 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004039 printk("md: %s still in use.\n",mdname(mddev));
4040 return -EBUSY;
4041 }
4042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004046 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4048 md_unregister_thread(mddev->sync_thread);
4049 mddev->sync_thread = NULL;
4050 }
4051
4052 del_timer_sync(&mddev->safemode_timer);
4053
NeilBrown9e653b62006-06-26 00:27:58 -07004054 switch(mode) {
4055 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004057 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 goto out;
4059 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004060 break;
4061 case 0: /* disassemble */
4062 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004063 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004064 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 if (mddev->ro)
4066 set_disk_ro(disk, 0);
4067 blk_queue_make_request(mddev->queue, md_fail_request);
4068 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004069 mddev->queue->merge_bvec_fn = NULL;
4070 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004071 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown0c3573f2009-01-09 08:31:05 +11004072 if (mddev->pers->sync_request) {
NeilBrown411036f2005-11-08 21:39:40 -08004073 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
NeilBrown0c3573f2009-01-09 08:31:05 +11004074 if (mddev->sysfs_action)
4075 sysfs_put(mddev->sysfs_action);
4076 mddev->sysfs_action = NULL;
4077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 module_put(mddev->pers->owner);
4079 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004080 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004081 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004082
4083 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004084 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004085
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 if (mddev->ro)
4087 mddev->ro = 0;
4088 }
NeilBrown850b2b42006-10-03 01:15:46 -07004089 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 /* mark array as shutdown cleanly */
4091 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004092 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 }
NeilBrown9e653b62006-06-26 00:27:58 -07004094 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004096 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 }
NeilBrown32a76272005-06-21 17:17:14 -07004098
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 /*
4100 * Free resources if final stop
4101 */
NeilBrown9e653b62006-06-26 00:27:58 -07004102 if (mode == 0) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08004103 mdk_rdev_t *rdev;
NeilBrown0d4ca602006-12-10 02:20:44 -08004104
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4106
NeilBrown978f9462006-02-02 14:28:05 -08004107 bitmap_destroy(mddev);
4108 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004109 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004110 fput(mddev->bitmap_file);
4111 mddev->bitmap_file = NULL;
4112 }
4113 mddev->bitmap_offset = 0;
4114
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004115 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004116 if (rdev->raid_disk >= 0) {
4117 char nm[20];
4118 sprintf(nm, "rd%d", rdev->raid_disk);
4119 sysfs_remove_link(&mddev->kobj, nm);
4120 }
4121
NeilBrown177a99b2008-02-06 01:39:56 -08004122 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004123 flush_scheduled_work();
4124
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 export_array(mddev);
4126
Andre Nollf233ea52008-07-21 17:05:22 +10004127 mddev->array_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004128 mddev->size = 0;
4129 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004130 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004131 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004132 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004133 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004134 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004135 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004136 mddev->level = LEVEL_NONE;
4137 mddev->clevel[0] = 0;
4138 mddev->flags = 0;
4139 mddev->ro = 0;
4140 mddev->metadata_type[0] = 0;
4141 mddev->chunk_size = 0;
4142 mddev->ctime = mddev->utime = 0;
4143 mddev->layout = 0;
4144 mddev->max_disks = 0;
4145 mddev->events = 0;
4146 mddev->delta_disks = 0;
4147 mddev->new_level = LEVEL_NONE;
4148 mddev->new_layout = 0;
4149 mddev->new_chunk = 0;
4150 mddev->curr_resync = 0;
4151 mddev->resync_mismatches = 0;
4152 mddev->suspend_lo = mddev->suspend_hi = 0;
4153 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4154 mddev->recovery = 0;
4155 mddev->in_sync = 0;
4156 mddev->changed = 0;
4157 mddev->degraded = 0;
4158 mddev->barriers_work = 0;
4159 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004160 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004161 if (mddev->hold_active == UNTIL_STOP)
4162 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004163
NeilBrowna8a55c32006-06-26 00:27:39 -07004164 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4166 mdname(mddev));
4167 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004168 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004169 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004170 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171out:
4172 return err;
4173}
4174
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004175#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176static void autorun_array(mddev_t *mddev)
4177{
4178 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 int err;
4180
NeilBrowna757e642005-04-16 15:26:42 -07004181 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
4184 printk(KERN_INFO "md: running: ");
4185
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004186 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 char b[BDEVNAME_SIZE];
4188 printk("<%s>", bdevname(rdev->bdev,b));
4189 }
4190 printk("\n");
4191
NeilBrownd710e132008-10-13 11:55:12 +11004192 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 if (err) {
4194 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004195 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 }
4197}
4198
4199/*
4200 * lets try to run arrays based on all disks that have arrived
4201 * until now. (those are in pending_raid_disks)
4202 *
4203 * the method: pick the first pending disk, collect all disks with
4204 * the same UUID, remove all from the pending list and put them into
4205 * the 'same_array' list. Then order this list based on superblock
4206 * update time (freshest comes first), kick out 'old' disks and
4207 * compare superblocks. If everything's fine then run it.
4208 *
4209 * If "unit" is allocated, then bump its reference count
4210 */
4211static void autorun_devices(int part)
4212{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004213 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 mddev_t *mddev;
4215 char b[BDEVNAME_SIZE];
4216
4217 printk(KERN_INFO "md: autorun ...\n");
4218 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004219 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004221 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 rdev0 = list_entry(pending_raid_disks.next,
4223 mdk_rdev_t, same_set);
4224
4225 printk(KERN_INFO "md: considering %s ...\n",
4226 bdevname(rdev0->bdev,b));
4227 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004228 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 if (super_90_load(rdev, rdev0, 0) >= 0) {
4230 printk(KERN_INFO "md: adding %s ...\n",
4231 bdevname(rdev->bdev,b));
4232 list_move(&rdev->same_set, &candidates);
4233 }
4234 /*
4235 * now we have a set of devices, with all of them having
4236 * mostly sane superblocks. It's time to allocate the
4237 * mddev.
4238 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004239 if (part) {
4240 dev = MKDEV(mdp_major,
4241 rdev0->preferred_minor << MdpMinorShift);
4242 unit = MINOR(dev) >> MdpMinorShift;
4243 } else {
4244 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4245 unit = MINOR(dev);
4246 }
4247 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4249 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4250 break;
4251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252
4253 md_probe(dev, NULL, NULL);
4254 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004255 if (!mddev || !mddev->gendisk) {
4256 if (mddev)
4257 mddev_put(mddev);
4258 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 "md: cannot allocate memory for md drive.\n");
4260 break;
4261 }
4262 if (mddev_lock(mddev))
4263 printk(KERN_WARNING "md: %s locked, cannot run\n",
4264 mdname(mddev));
4265 else if (mddev->raid_disks || mddev->major_version
4266 || !list_empty(&mddev->disks)) {
4267 printk(KERN_WARNING
4268 "md: %s already running, cannot run %s\n",
4269 mdname(mddev), bdevname(rdev0->bdev,b));
4270 mddev_unlock(mddev);
4271 } else {
4272 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004273 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004274 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 list_del_init(&rdev->same_set);
4276 if (bind_rdev_to_array(rdev, mddev))
4277 export_rdev(rdev);
4278 }
4279 autorun_array(mddev);
4280 mddev_unlock(mddev);
4281 }
4282 /* on success, candidates will be empty, on error
4283 * it won't...
4284 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004285 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004286 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 mddev_put(mddev);
4290 }
4291 printk(KERN_INFO "md: ... autorun DONE.\n");
4292}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004293#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295static int get_version(void __user * arg)
4296{
4297 mdu_version_t ver;
4298
4299 ver.major = MD_MAJOR_VERSION;
4300 ver.minor = MD_MINOR_VERSION;
4301 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4302
4303 if (copy_to_user(arg, &ver, sizeof(ver)))
4304 return -EFAULT;
4305
4306 return 0;
4307}
4308
4309static int get_array_info(mddev_t * mddev, void __user * arg)
4310{
4311 mdu_array_info_t info;
4312 int nr,working,active,failed,spare;
4313 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
4315 nr=working=active=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004316 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004318 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 failed++;
4320 else {
4321 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004322 if (test_bit(In_sync, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 active++;
4324 else
4325 spare++;
4326 }
4327 }
4328
4329 info.major_version = mddev->major_version;
4330 info.minor_version = mddev->minor_version;
4331 info.patch_version = MD_PATCHLEVEL_VERSION;
4332 info.ctime = mddev->ctime;
4333 info.level = mddev->level;
4334 info.size = mddev->size;
NeilBrown284ae7c2006-02-03 03:03:40 -08004335 if (info.size != mddev->size) /* overflow */
4336 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 info.nr_disks = nr;
4338 info.raid_disks = mddev->raid_disks;
4339 info.md_minor = mddev->md_minor;
4340 info.not_persistent= !mddev->persistent;
4341
4342 info.utime = mddev->utime;
4343 info.state = 0;
4344 if (mddev->in_sync)
4345 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004346 if (mddev->bitmap && mddev->bitmap_offset)
4347 info.state = (1<<MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 info.active_disks = active;
4349 info.working_disks = working;
4350 info.failed_disks = failed;
4351 info.spare_disks = spare;
4352
4353 info.layout = mddev->layout;
4354 info.chunk_size = mddev->chunk_size;
4355
4356 if (copy_to_user(arg, &info, sizeof(info)))
4357 return -EFAULT;
4358
4359 return 0;
4360}
4361
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004362static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004363{
4364 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4365 char *ptr, *buf = NULL;
4366 int err = -ENOMEM;
4367
Dan Williamsb5470dc2008-06-27 21:44:04 -07004368 if (md_allow_write(mddev))
4369 file = kmalloc(sizeof(*file), GFP_NOIO);
4370 else
4371 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004372
NeilBrown32a76272005-06-21 17:17:14 -07004373 if (!file)
4374 goto out;
4375
4376 /* bitmap disabled, zero the first byte and copy out */
4377 if (!mddev->bitmap || !mddev->bitmap->file) {
4378 file->pathname[0] = '\0';
4379 goto copy_out;
4380 }
4381
4382 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4383 if (!buf)
4384 goto out;
4385
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004386 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4387 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004388 goto out;
4389
4390 strcpy(file->pathname, ptr);
4391
4392copy_out:
4393 err = 0;
4394 if (copy_to_user(arg, file, sizeof(*file)))
4395 err = -EFAULT;
4396out:
4397 kfree(buf);
4398 kfree(file);
4399 return err;
4400}
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402static int get_disk_info(mddev_t * mddev, void __user * arg)
4403{
4404 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 mdk_rdev_t *rdev;
4406
4407 if (copy_from_user(&info, arg, sizeof(info)))
4408 return -EFAULT;
4409
Andre Noll26ef3792008-07-11 22:02:21 +10004410 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 if (rdev) {
4412 info.major = MAJOR(rdev->bdev->bd_dev);
4413 info.minor = MINOR(rdev->bdev->bd_dev);
4414 info.raid_disk = rdev->raid_disk;
4415 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004416 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004418 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 info.state |= (1<<MD_DISK_ACTIVE);
4420 info.state |= (1<<MD_DISK_SYNC);
4421 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004422 if (test_bit(WriteMostly, &rdev->flags))
4423 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 } else {
4425 info.major = info.minor = 0;
4426 info.raid_disk = -1;
4427 info.state = (1<<MD_DISK_REMOVED);
4428 }
4429
4430 if (copy_to_user(arg, &info, sizeof(info)))
4431 return -EFAULT;
4432
4433 return 0;
4434}
4435
4436static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4437{
4438 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4439 mdk_rdev_t *rdev;
4440 dev_t dev = MKDEV(info->major,info->minor);
4441
4442 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4443 return -EOVERFLOW;
4444
4445 if (!mddev->raid_disks) {
4446 int err;
4447 /* expecting a device which has a superblock */
4448 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4449 if (IS_ERR(rdev)) {
4450 printk(KERN_WARNING
4451 "md: md_import_device returned %ld\n",
4452 PTR_ERR(rdev));
4453 return PTR_ERR(rdev);
4454 }
4455 if (!list_empty(&mddev->disks)) {
4456 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4457 mdk_rdev_t, same_set);
4458 int err = super_types[mddev->major_version]
4459 .load_super(rdev, rdev0, mddev->minor_version);
4460 if (err < 0) {
4461 printk(KERN_WARNING
4462 "md: %s has different UUID to %s\n",
4463 bdevname(rdev->bdev,b),
4464 bdevname(rdev0->bdev,b2));
4465 export_rdev(rdev);
4466 return -EINVAL;
4467 }
4468 }
4469 err = bind_rdev_to_array(rdev, mddev);
4470 if (err)
4471 export_rdev(rdev);
4472 return err;
4473 }
4474
4475 /*
4476 * add_new_disk can be used once the array is assembled
4477 * to add "hot spares". They must already have a superblock
4478 * written
4479 */
4480 if (mddev->pers) {
4481 int err;
4482 if (!mddev->pers->hot_add_disk) {
4483 printk(KERN_WARNING
4484 "%s: personality does not support diskops!\n",
4485 mdname(mddev));
4486 return -EINVAL;
4487 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004488 if (mddev->persistent)
4489 rdev = md_import_device(dev, mddev->major_version,
4490 mddev->minor_version);
4491 else
4492 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 if (IS_ERR(rdev)) {
4494 printk(KERN_WARNING
4495 "md: md_import_device returned %ld\n",
4496 PTR_ERR(rdev));
4497 return PTR_ERR(rdev);
4498 }
NeilBrown41158c72005-06-21 17:17:25 -07004499 /* set save_raid_disk if appropriate */
4500 if (!mddev->persistent) {
4501 if (info->state & (1<<MD_DISK_SYNC) &&
4502 info->raid_disk < mddev->raid_disks)
4503 rdev->raid_disk = info->raid_disk;
4504 else
4505 rdev->raid_disk = -1;
4506 } else
4507 super_types[mddev->major_version].
4508 validate_super(mddev, rdev);
4509 rdev->saved_raid_disk = rdev->raid_disk;
4510
NeilBrownb2d444d2005-11-08 21:39:31 -08004511 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004512 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4513 set_bit(WriteMostly, &rdev->flags);
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 rdev->raid_disk = -1;
4516 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004517 if (!err && !mddev->pers->hot_remove_disk) {
4518 /* If there is hot_add_disk but no hot_remove_disk
4519 * then added disks for geometry changes,
4520 * and should be added immediately.
4521 */
4522 super_types[mddev->major_version].
4523 validate_super(mddev, rdev);
4524 err = mddev->pers->hot_add_disk(mddev, rdev);
4525 if (err)
4526 unbind_rdev_from_array(rdev);
4527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (err)
4529 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004530 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004531 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004532
NeilBrown17571282006-12-10 02:20:52 -08004533 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004534 if (mddev->degraded)
4535 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004536 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004537 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return err;
4539 }
4540
4541 /* otherwise, add_new_disk is only allowed
4542 * for major_version==0 superblocks
4543 */
4544 if (mddev->major_version != 0) {
4545 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4546 mdname(mddev));
4547 return -EINVAL;
4548 }
4549
4550 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4551 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004552 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 if (IS_ERR(rdev)) {
4554 printk(KERN_WARNING
4555 "md: error, md_import_device() returned %ld\n",
4556 PTR_ERR(rdev));
4557 return PTR_ERR(rdev);
4558 }
4559 rdev->desc_nr = info->number;
4560 if (info->raid_disk < mddev->raid_disks)
4561 rdev->raid_disk = info->raid_disk;
4562 else
4563 rdev->raid_disk = -1;
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004566 if (info->state & (1<<MD_DISK_SYNC))
4567 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004569 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4570 set_bit(WriteMostly, &rdev->flags);
4571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 if (!mddev->persistent) {
4573 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004574 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 } else
Andre Noll0f420352008-07-11 22:02:23 +10004576 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Nolle7debaa2008-07-11 22:02:23 +10004577 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
NeilBrown2bf071b2006-01-06 00:20:55 -08004579 err = bind_rdev_to_array(rdev, mddev);
4580 if (err) {
4581 export_rdev(rdev);
4582 return err;
4583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 }
4585
4586 return 0;
4587}
4588
4589static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4590{
4591 char b[BDEVNAME_SIZE];
4592 mdk_rdev_t *rdev;
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 rdev = find_rdev(mddev, dev);
4595 if (!rdev)
4596 return -ENXIO;
4597
4598 if (rdev->raid_disk >= 0)
4599 goto busy;
4600
4601 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004602 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004603 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605 return 0;
4606busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004607 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 bdevname(rdev->bdev,b), mdname(mddev));
4609 return -EBUSY;
4610}
4611
4612static int hot_add_disk(mddev_t * mddev, dev_t dev)
4613{
4614 char b[BDEVNAME_SIZE];
4615 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 mdk_rdev_t *rdev;
4617
4618 if (!mddev->pers)
4619 return -ENODEV;
4620
4621 if (mddev->major_version != 0) {
4622 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4623 " version-0 superblocks.\n",
4624 mdname(mddev));
4625 return -EINVAL;
4626 }
4627 if (!mddev->pers->hot_add_disk) {
4628 printk(KERN_WARNING
4629 "%s: personality does not support diskops!\n",
4630 mdname(mddev));
4631 return -EINVAL;
4632 }
4633
NeilBrownd710e132008-10-13 11:55:12 +11004634 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 if (IS_ERR(rdev)) {
4636 printk(KERN_WARNING
4637 "md: error, md_import_device() returned %ld\n",
4638 PTR_ERR(rdev));
4639 return -EINVAL;
4640 }
4641
4642 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004643 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 else
Andre Noll0f420352008-07-11 22:02:23 +10004645 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
Andre Nolle7debaa2008-07-11 22:02:23 +10004647 rdev->size = calc_num_sectors(rdev, mddev->chunk_size) / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
NeilBrownb2d444d2005-11-08 21:39:31 -08004649 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 printk(KERN_WARNING
4651 "md: can not hot-add faulty %s disk to %s!\n",
4652 bdevname(rdev->bdev,b), mdname(mddev));
4653 err = -EINVAL;
4654 goto abort_export;
4655 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004656 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004658 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004659 err = bind_rdev_to_array(rdev, mddev);
4660 if (err)
4661 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
4663 /*
4664 * The rest should better be atomic, we can have disk failures
4665 * noticed in interrupt contexts ...
4666 */
4667
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 rdev->raid_disk = -1;
4669
NeilBrown850b2b42006-10-03 01:15:46 -07004670 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
4672 /*
4673 * Kick recovery, maybe this spare has to be added to the
4674 * array immediately.
4675 */
4676 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4677 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004678 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 return 0;
4680
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681abort_export:
4682 export_rdev(rdev);
4683 return err;
4684}
4685
NeilBrown32a76272005-06-21 17:17:14 -07004686static int set_bitmap_file(mddev_t *mddev, int fd)
4687{
4688 int err;
4689
NeilBrown36fa3062005-09-09 16:23:45 -07004690 if (mddev->pers) {
4691 if (!mddev->pers->quiesce)
4692 return -EBUSY;
4693 if (mddev->recovery || mddev->sync_thread)
4694 return -EBUSY;
4695 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004696 }
4697
NeilBrown36fa3062005-09-09 16:23:45 -07004698
4699 if (fd >= 0) {
4700 if (mddev->bitmap)
4701 return -EEXIST; /* cannot add when bitmap is present */
4702 mddev->bitmap_file = fget(fd);
4703
4704 if (mddev->bitmap_file == NULL) {
4705 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4706 mdname(mddev));
4707 return -EBADF;
4708 }
4709
4710 err = deny_bitmap_write_access(mddev->bitmap_file);
4711 if (err) {
4712 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4713 mdname(mddev));
4714 fput(mddev->bitmap_file);
4715 mddev->bitmap_file = NULL;
4716 return err;
4717 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004718 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004719 } else if (mddev->bitmap == NULL)
4720 return -ENOENT; /* cannot remove what isn't there */
4721 err = 0;
4722 if (mddev->pers) {
4723 mddev->pers->quiesce(mddev, 1);
4724 if (fd >= 0)
4725 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004726 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07004727 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07004728 fd = -1; /* make sure to put the file */
4729 }
NeilBrown36fa3062005-09-09 16:23:45 -07004730 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07004731 }
4732 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07004733 if (mddev->bitmap_file) {
4734 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07004735 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07004736 }
NeilBrown36fa3062005-09-09 16:23:45 -07004737 mddev->bitmap_file = NULL;
4738 }
4739
NeilBrown32a76272005-06-21 17:17:14 -07004740 return err;
4741}
4742
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743/*
4744 * set_array_info is used two different ways
4745 * The original usage is when creating a new array.
4746 * In this usage, raid_disks is > 0 and it together with
4747 * level, size, not_persistent,layout,chunksize determine the
4748 * shape of the array.
4749 * This will always create an array with a type-0.90.0 superblock.
4750 * The newer usage is when assembling an array.
4751 * In this case raid_disks will be 0, and the major_version field is
4752 * use to determine which style super-blocks are to be found on the devices.
4753 * The minor and patch _version numbers are also kept incase the
4754 * super_block handler wishes to interpret them.
4755 */
4756static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
4757{
4758
4759 if (info->raid_disks == 0) {
4760 /* just setting version number for superblock loading */
4761 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07004762 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 super_types[info->major_version].name == NULL) {
4764 /* maybe try to auto-load a module? */
4765 printk(KERN_INFO
4766 "md: superblock version %d not known\n",
4767 info->major_version);
4768 return -EINVAL;
4769 }
4770 mddev->major_version = info->major_version;
4771 mddev->minor_version = info->minor_version;
4772 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08004773 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 return 0;
4775 }
4776 mddev->major_version = MD_MAJOR_VERSION;
4777 mddev->minor_version = MD_MINOR_VERSION;
4778 mddev->patch_version = MD_PATCHLEVEL_VERSION;
4779 mddev->ctime = get_seconds();
4780
4781 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08004782 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 mddev->size = info->size;
4784 mddev->raid_disks = info->raid_disks;
4785 /* don't set md_minor, it is determined by which /dev/md* was
4786 * openned
4787 */
4788 if (info->state & (1<<MD_SB_CLEAN))
4789 mddev->recovery_cp = MaxSector;
4790 else
4791 mddev->recovery_cp = 0;
4792 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08004793 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794
4795 mddev->layout = info->layout;
4796 mddev->chunk_size = info->chunk_size;
4797
4798 mddev->max_disks = MD_SB_DISKS;
4799
NeilBrowne6910632008-02-06 01:39:51 -08004800 if (mddev->persistent)
4801 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07004802 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
NeilBrownb2a27032005-11-28 13:44:12 -08004804 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
4805 mddev->bitmap_offset = 0;
4806
NeilBrownf6705572006-03-27 01:18:11 -08004807 mddev->reshape_position = MaxSector;
4808
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 /*
4810 * Generate a 128 bit UUID
4811 */
4812 get_random_bytes(mddev->uuid, 16);
4813
NeilBrownf6705572006-03-27 01:18:11 -08004814 mddev->new_level = mddev->level;
4815 mddev->new_chunk = mddev->chunk_size;
4816 mddev->new_layout = mddev->layout;
4817 mddev->delta_disks = 0;
4818
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 return 0;
4820}
4821
Andre Nolld71f9f82008-07-11 22:02:22 +10004822static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004823{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004824 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08004825 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10004826 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08004827
4828 if (mddev->pers->resize == NULL)
4829 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10004830 /* The "num_sectors" is the number of sectors of each device that
4831 * is used. This can only make sense for arrays with redundancy.
4832 * linear and raid0 always use whatever space is available. We can only
4833 * consider changing this number if no resync or reconstruction is
4834 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10004835 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10004836 * of each device. If num_sectors is zero, we find the largest size
4837 * that fits.
4838
NeilBrowna35b0d62006-01-06 00:20:49 -08004839 */
4840 if (mddev->sync_thread)
4841 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10004842 if (mddev->bitmap)
4843 /* Sorry, cannot grow a bitmap yet, just remove it,
4844 * grow, and re-add.
4845 */
4846 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004847 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowna35b0d62006-01-06 00:20:49 -08004848 sector_t avail;
NeilBrown01ab5662006-10-28 10:38:30 -07004849 avail = rdev->size * 2;
4850
Andre Nolld71f9f82008-07-11 22:02:22 +10004851 if (fit && (num_sectors == 0 || num_sectors > avail))
4852 num_sectors = avail;
4853 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08004854 return -ENOSPC;
4855 }
Andre Nolld71f9f82008-07-11 22:02:22 +10004856 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08004857 if (!rv) {
4858 struct block_device *bdev;
4859
4860 bdev = bdget_disk(mddev->gendisk, 0);
4861 if (bdev) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004862 mutex_lock(&bdev->bd_inode->i_mutex);
Andre Nollf233ea52008-07-21 17:05:22 +10004863 i_size_write(bdev->bd_inode,
4864 (loff_t)mddev->array_sectors << 9);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004865 mutex_unlock(&bdev->bd_inode->i_mutex);
NeilBrowna35b0d62006-01-06 00:20:49 -08004866 bdput(bdev);
4867 }
4868 }
4869 return rv;
4870}
4871
NeilBrownda943b992006-01-06 00:20:54 -08004872static int update_raid_disks(mddev_t *mddev, int raid_disks)
4873{
4874 int rv;
4875 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08004876 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08004877 return -EINVAL;
4878 if (raid_disks <= 0 ||
4879 raid_disks >= mddev->max_disks)
4880 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08004881 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08004882 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08004883 mddev->delta_disks = raid_disks - mddev->raid_disks;
4884
4885 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08004886 return rv;
4887}
4888
4889
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890/*
4891 * update_array_info is used to change the configuration of an
4892 * on-line array.
4893 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
4894 * fields in the info are checked against the array.
4895 * Any differences that cannot be handled will cause an error.
4896 * Normally, only one change can be managed at a time.
4897 */
4898static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
4899{
4900 int rv = 0;
4901 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07004902 int state = 0;
4903
4904 /* calculate expected state,ignoring low bits */
4905 if (mddev->bitmap && mddev->bitmap_offset)
4906 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907
4908 if (mddev->major_version != info->major_version ||
4909 mddev->minor_version != info->minor_version ||
4910/* mddev->patch_version != info->patch_version || */
4911 mddev->ctime != info->ctime ||
4912 mddev->level != info->level ||
4913/* mddev->layout != info->layout || */
4914 !mddev->persistent != info->not_persistent||
NeilBrown36fa3062005-09-09 16:23:45 -07004915 mddev->chunk_size != info->chunk_size ||
4916 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
4917 ((state^info->state) & 0xfffffe00)
4918 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 return -EINVAL;
4920 /* Check there is only one change */
NeilBrown284ae7c2006-02-03 03:03:40 -08004921 if (info->size >= 0 && mddev->size != info->size) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 if (mddev->raid_disks != info->raid_disks) cnt++;
4923 if (mddev->layout != info->layout) cnt++;
NeilBrown36fa3062005-09-09 16:23:45 -07004924 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 if (cnt == 0) return 0;
4926 if (cnt > 1) return -EINVAL;
4927
4928 if (mddev->layout != info->layout) {
4929 /* Change layout
4930 * we don't need to do anything at the md level, the
4931 * personality will take care of it all.
4932 */
4933 if (mddev->pers->reconfig == NULL)
4934 return -EINVAL;
4935 else
4936 return mddev->pers->reconfig(mddev, info->layout, -1);
4937 }
NeilBrown284ae7c2006-02-03 03:03:40 -08004938 if (info->size >= 0 && mddev->size != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10004939 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
NeilBrownda943b992006-01-06 00:20:54 -08004941 if (mddev->raid_disks != info->raid_disks)
4942 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
NeilBrown36fa3062005-09-09 16:23:45 -07004944 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
4945 if (mddev->pers->quiesce == NULL)
4946 return -EINVAL;
4947 if (mddev->recovery || mddev->sync_thread)
4948 return -EBUSY;
4949 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
4950 /* add the bitmap */
4951 if (mddev->bitmap)
4952 return -EEXIST;
4953 if (mddev->default_bitmap_offset == 0)
4954 return -EINVAL;
4955 mddev->bitmap_offset = mddev->default_bitmap_offset;
4956 mddev->pers->quiesce(mddev, 1);
4957 rv = bitmap_create(mddev);
4958 if (rv)
4959 bitmap_destroy(mddev);
4960 mddev->pers->quiesce(mddev, 0);
4961 } else {
4962 /* remove the bitmap */
4963 if (!mddev->bitmap)
4964 return -ENOENT;
4965 if (mddev->bitmap->file)
4966 return -EINVAL;
4967 mddev->pers->quiesce(mddev, 1);
4968 bitmap_destroy(mddev);
4969 mddev->pers->quiesce(mddev, 0);
4970 mddev->bitmap_offset = 0;
4971 }
4972 }
NeilBrown850b2b42006-10-03 01:15:46 -07004973 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 return rv;
4975}
4976
4977static int set_disk_faulty(mddev_t *mddev, dev_t dev)
4978{
4979 mdk_rdev_t *rdev;
4980
4981 if (mddev->pers == NULL)
4982 return -ENODEV;
4983
4984 rdev = find_rdev(mddev, dev);
4985 if (!rdev)
4986 return -ENODEV;
4987
4988 md_error(mddev, rdev);
4989 return 0;
4990}
4991
Andre Noll2f9618c2008-04-25 18:57:58 +02004992/*
4993 * We have a problem here : there is no easy way to give a CHS
4994 * virtual geometry. We currently pretend that we have a 2 heads
4995 * 4 sectors (with a BIG number of cylinders...). This drives
4996 * dosfs just mad... ;-)
4997 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08004998static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
4999{
5000 mddev_t *mddev = bdev->bd_disk->private_data;
5001
5002 geo->heads = 2;
5003 geo->sectors = 4;
5004 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5005 return 0;
5006}
5007
Al Viroa39907f2008-03-02 10:31:15 -05005008static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009 unsigned int cmd, unsigned long arg)
5010{
5011 int err = 0;
5012 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 mddev_t *mddev = NULL;
5014
5015 if (!capable(CAP_SYS_ADMIN))
5016 return -EACCES;
5017
5018 /*
5019 * Commands dealing with the RAID driver but not any
5020 * particular array:
5021 */
5022 switch (cmd)
5023 {
5024 case RAID_VERSION:
5025 err = get_version(argp);
5026 goto done;
5027
5028 case PRINT_RAID_DEBUG:
5029 err = 0;
5030 md_print_devices();
5031 goto done;
5032
5033#ifndef MODULE
5034 case RAID_AUTORUN:
5035 err = 0;
5036 autostart_arrays(arg);
5037 goto done;
5038#endif
5039 default:;
5040 }
5041
5042 /*
5043 * Commands creating/starting a new array:
5044 */
5045
Al Viroa39907f2008-03-02 10:31:15 -05005046 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
5048 if (!mddev) {
5049 BUG();
5050 goto abort;
5051 }
5052
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053 err = mddev_lock(mddev);
5054 if (err) {
5055 printk(KERN_INFO
5056 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5057 err, cmd);
5058 goto abort;
5059 }
5060
5061 switch (cmd)
5062 {
5063 case SET_ARRAY_INFO:
5064 {
5065 mdu_array_info_t info;
5066 if (!arg)
5067 memset(&info, 0, sizeof(info));
5068 else if (copy_from_user(&info, argp, sizeof(info))) {
5069 err = -EFAULT;
5070 goto abort_unlock;
5071 }
5072 if (mddev->pers) {
5073 err = update_array_info(mddev, &info);
5074 if (err) {
5075 printk(KERN_WARNING "md: couldn't update"
5076 " array info. %d\n", err);
5077 goto abort_unlock;
5078 }
5079 goto done_unlock;
5080 }
5081 if (!list_empty(&mddev->disks)) {
5082 printk(KERN_WARNING
5083 "md: array %s already has disks!\n",
5084 mdname(mddev));
5085 err = -EBUSY;
5086 goto abort_unlock;
5087 }
5088 if (mddev->raid_disks) {
5089 printk(KERN_WARNING
5090 "md: array %s already initialised!\n",
5091 mdname(mddev));
5092 err = -EBUSY;
5093 goto abort_unlock;
5094 }
5095 err = set_array_info(mddev, &info);
5096 if (err) {
5097 printk(KERN_WARNING "md: couldn't set"
5098 " array info. %d\n", err);
5099 goto abort_unlock;
5100 }
5101 }
5102 goto done_unlock;
5103
5104 default:;
5105 }
5106
5107 /*
5108 * Commands querying/configuring an existing array:
5109 */
NeilBrown32a76272005-06-21 17:17:14 -07005110 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005111 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005112 if ((!mddev->raid_disks && !mddev->external)
5113 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5114 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5115 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 err = -ENODEV;
5117 goto abort_unlock;
5118 }
5119
5120 /*
5121 * Commands even a read-only array can execute:
5122 */
5123 switch (cmd)
5124 {
5125 case GET_ARRAY_INFO:
5126 err = get_array_info(mddev, argp);
5127 goto done_unlock;
5128
NeilBrown32a76272005-06-21 17:17:14 -07005129 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005130 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005131 goto done_unlock;
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 case GET_DISK_INFO:
5134 err = get_disk_info(mddev, argp);
5135 goto done_unlock;
5136
5137 case RESTART_ARRAY_RW:
5138 err = restart_array(mddev);
5139 goto done_unlock;
5140
5141 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005142 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 goto done_unlock;
5144
5145 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005146 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 goto done_unlock;
5148
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 }
5150
5151 /*
5152 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005153 * superblock, so we do not allow them on read-only arrays.
5154 * However non-MD ioctls (e.g. get-size) will still come through
5155 * here and hit the 'default' below, so only disallow
5156 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005158 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005159 if (mddev->ro == 2) {
5160 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005161 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005162 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5163 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005164 } else {
5165 err = -EROFS;
5166 goto abort_unlock;
5167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 }
5169
5170 switch (cmd)
5171 {
5172 case ADD_NEW_DISK:
5173 {
5174 mdu_disk_info_t info;
5175 if (copy_from_user(&info, argp, sizeof(info)))
5176 err = -EFAULT;
5177 else
5178 err = add_new_disk(mddev, &info);
5179 goto done_unlock;
5180 }
5181
5182 case HOT_REMOVE_DISK:
5183 err = hot_remove_disk(mddev, new_decode_dev(arg));
5184 goto done_unlock;
5185
5186 case HOT_ADD_DISK:
5187 err = hot_add_disk(mddev, new_decode_dev(arg));
5188 goto done_unlock;
5189
5190 case SET_DISK_FAULTY:
5191 err = set_disk_faulty(mddev, new_decode_dev(arg));
5192 goto done_unlock;
5193
5194 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005195 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 goto done_unlock;
5197
NeilBrown32a76272005-06-21 17:17:14 -07005198 case SET_BITMAP_FILE:
5199 err = set_bitmap_file(mddev, (int)arg);
5200 goto done_unlock;
5201
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 err = -EINVAL;
5204 goto abort_unlock;
5205 }
5206
5207done_unlock:
5208abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005209 if (mddev->hold_active == UNTIL_IOCTL &&
5210 err != -EINVAL)
5211 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 mddev_unlock(mddev);
5213
5214 return err;
5215done:
5216 if (err)
5217 MD_BUG();
5218abort:
5219 return err;
5220}
5221
Al Viroa39907f2008-03-02 10:31:15 -05005222static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223{
5224 /*
5225 * Succeed if we can lock the mddev, which confirms that
5226 * it isn't being stopped right now.
5227 */
NeilBrownd3374822009-01-09 08:31:10 +11005228 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 int err;
5230
NeilBrownd3374822009-01-09 08:31:10 +11005231 if (mddev->gendisk != bdev->bd_disk) {
5232 /* we are racing with mddev_put which is discarding this
5233 * bd_disk.
5234 */
5235 mddev_put(mddev);
5236 /* Wait until bdev->bd_disk is definitely gone */
5237 flush_scheduled_work();
5238 /* Then retry the open from the top */
5239 return -ERESTARTSYS;
5240 }
5241 BUG_ON(mddev != bdev->bd_disk->private_data);
5242
NeilBrownd63a5a72006-12-08 02:36:17 -08005243 if ((err = mutex_lock_interruptible_nested(&mddev->reconfig_mutex, 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 goto out;
5245
5246 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005247 atomic_inc(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 mddev_unlock(mddev);
5249
Al Viroa39907f2008-03-02 10:31:15 -05005250 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251 out:
5252 return err;
5253}
5254
Al Viroa39907f2008-03-02 10:31:15 -05005255static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256{
Al Viroa39907f2008-03-02 10:31:15 -05005257 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005259 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005260 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 mddev_put(mddev);
5262
5263 return 0;
5264}
5265
Linus Torvalds44ce62942007-05-09 18:51:36 -07005266static int md_media_changed(struct gendisk *disk)
5267{
5268 mddev_t *mddev = disk->private_data;
5269
5270 return mddev->changed;
5271}
5272
5273static int md_revalidate(struct gendisk *disk)
5274{
5275 mddev_t *mddev = disk->private_data;
5276
5277 mddev->changed = 0;
5278 return 0;
5279}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280static struct block_device_operations md_fops =
5281{
5282 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005283 .open = md_open,
5284 .release = md_release,
5285 .locked_ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005286 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005287 .media_changed = md_media_changed,
5288 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289};
5290
Adrian Bunk75c96f82005-05-05 16:16:09 -07005291static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292{
5293 mdk_thread_t *thread = arg;
5294
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 /*
5296 * md_thread is a 'system-thread', it's priority should be very
5297 * high. We avoid resource deadlocks individually in each
5298 * raid personality. (RAID5 does preallocation) We also use RR and
5299 * the very same RT priority as kswapd, thus we will never get
5300 * into a priority inversion deadlock.
5301 *
5302 * we definitely have to have equal or higher priority than
5303 * bdflush, otherwise bdflush will deadlock if there are too
5304 * many dirty RAID5 blocks.
5305 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306
NeilBrown6985c432005-10-19 21:23:47 -07005307 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005308 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
NeilBrown93588e22005-11-15 00:09:12 -08005310 /* We need to wait INTERRUPTIBLE so that
5311 * we don't add to the load-average.
5312 * That means we need to be sure no signals are
5313 * pending
5314 */
5315 if (signal_pending(current))
5316 flush_signals(current);
5317
5318 wait_event_interruptible_timeout
5319 (thread->wqueue,
5320 test_bit(THREAD_WAKEUP, &thread->flags)
5321 || kthread_should_stop(),
5322 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323
5324 clear_bit(THREAD_WAKEUP, &thread->flags);
5325
NeilBrown787453c2005-11-08 21:39:43 -08005326 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005328
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 return 0;
5330}
5331
5332void md_wakeup_thread(mdk_thread_t *thread)
5333{
5334 if (thread) {
5335 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5336 set_bit(THREAD_WAKEUP, &thread->flags);
5337 wake_up(&thread->wqueue);
5338 }
5339}
5340
5341mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5342 const char *name)
5343{
5344 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
NeilBrown9ffae0c2006-01-06 00:20:32 -08005346 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 if (!thread)
5348 return NULL;
5349
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 init_waitqueue_head(&thread->wqueue);
5351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 thread->run = run;
5353 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005354 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown6985c432005-10-19 21:23:47 -07005355 thread->tsk = kthread_run(md_thread, thread, name, mdname(thread->mddev));
NeilBrowna6fb0932005-09-09 16:23:56 -07005356 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 kfree(thread);
5358 return NULL;
5359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 return thread;
5361}
5362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363void md_unregister_thread(mdk_thread_t *thread)
5364{
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005365 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005366
5367 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 kfree(thread);
5369}
5370
5371void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5372{
5373 if (!mddev) {
5374 MD_BUG();
5375 return;
5376 }
5377
NeilBrownb2d444d2005-11-08 21:39:31 -08005378 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005380
5381 if (mddev->external)
5382 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005383/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5385 mdname(mddev),
5386 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5387 __builtin_return_address(0),__builtin_return_address(1),
5388 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005389*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005390 if (!mddev->pers)
5391 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 if (!mddev->pers->error_handler)
5393 return;
5394 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005395 if (mddev->degraded)
5396 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005397 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5399 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5400 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005401 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
5404/* seq_file implementation /proc/mdstat */
5405
5406static void status_unused(struct seq_file *seq)
5407{
5408 int i = 0;
5409 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410
5411 seq_printf(seq, "unused devices: ");
5412
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005413 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 char b[BDEVNAME_SIZE];
5415 i++;
5416 seq_printf(seq, "%s ",
5417 bdevname(rdev->bdev,b));
5418 }
5419 if (!i)
5420 seq_printf(seq, "<none>");
5421
5422 seq_printf(seq, "\n");
5423}
5424
5425
5426static void status_resync(struct seq_file *seq, mddev_t * mddev)
5427{
NeilBrown4588b422006-03-27 01:18:04 -08005428 sector_t max_blocks, resync, res;
5429 unsigned long dt, db, rt;
5430 int scale;
5431 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432
5433 resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
5434
5435 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
5436 max_blocks = mddev->resync_max_sectors >> 1;
5437 else
5438 max_blocks = mddev->size;
5439
5440 /*
5441 * Should not happen.
5442 */
5443 if (!max_blocks) {
5444 MD_BUG();
5445 return;
5446 }
NeilBrown4588b422006-03-27 01:18:04 -08005447 /* Pick 'scale' such that (resync>>scale)*1000 will fit
5448 * in a sector_t, and (max_blocks>>scale) will fit in a
5449 * u32, as those are the requirements for sector_div.
5450 * Thus 'scale' must be at least 10
5451 */
5452 scale = 10;
5453 if (sizeof(sector_t) > sizeof(unsigned long)) {
5454 while ( max_blocks/2 > (1ULL<<(scale+32)))
5455 scale++;
5456 }
5457 res = (resync>>scale)*1000;
5458 sector_div(res, (u32)((max_blocks>>scale)+1));
5459
5460 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 {
NeilBrown4588b422006-03-27 01:18:04 -08005462 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 seq_printf(seq, "[");
5464 for (i = 0; i < x; i++)
5465 seq_printf(seq, "=");
5466 seq_printf(seq, ">");
5467 for (i = 0; i < y; i++)
5468 seq_printf(seq, ".");
5469 seq_printf(seq, "] ");
5470 }
NeilBrown4588b422006-03-27 01:18:04 -08005471 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005472 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5473 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005474 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5475 "check" :
5476 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5477 "resync" : "recovery"))),
5478 per_milli/10, per_milli % 10,
NeilBrown4588b422006-03-27 01:18:04 -08005479 (unsigned long long) resync,
5480 (unsigned long long) max_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481
5482 /*
5483 * We do not want to overflow, so the order of operands and
5484 * the * 100 / 100 trick are important. We do a +1 to be
5485 * safe against division by zero. We only estimate anyway.
5486 *
5487 * dt: time from mark until now
5488 * db: blocks written from mark until now
5489 * rt: remaining time
5490 */
5491 dt = ((jiffies - mddev->resync_mark) / HZ);
5492 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005493 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5494 - mddev->resync_mark_cnt;
5495 rt = (dt * ((unsigned long)(max_blocks-resync) / (db/2/100+1)))/100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496
5497 seq_printf(seq, " finish=%lu.%lumin", rt / 60, (rt % 60)/6);
5498
NeilBrownff4e8d92006-07-10 04:44:16 -07005499 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500}
5501
5502static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5503{
5504 struct list_head *tmp;
5505 loff_t l = *pos;
5506 mddev_t *mddev;
5507
5508 if (l >= 0x10000)
5509 return NULL;
5510 if (!l--)
5511 /* header */
5512 return (void*)1;
5513
5514 spin_lock(&all_mddevs_lock);
5515 list_for_each(tmp,&all_mddevs)
5516 if (!l--) {
5517 mddev = list_entry(tmp, mddev_t, all_mddevs);
5518 mddev_get(mddev);
5519 spin_unlock(&all_mddevs_lock);
5520 return mddev;
5521 }
5522 spin_unlock(&all_mddevs_lock);
5523 if (!l--)
5524 return (void*)2;/* tail */
5525 return NULL;
5526}
5527
5528static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5529{
5530 struct list_head *tmp;
5531 mddev_t *next_mddev, *mddev = v;
5532
5533 ++*pos;
5534 if (v == (void*)2)
5535 return NULL;
5536
5537 spin_lock(&all_mddevs_lock);
5538 if (v == (void*)1)
5539 tmp = all_mddevs.next;
5540 else
5541 tmp = mddev->all_mddevs.next;
5542 if (tmp != &all_mddevs)
5543 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5544 else {
5545 next_mddev = (void*)2;
5546 *pos = 0x10000;
5547 }
5548 spin_unlock(&all_mddevs_lock);
5549
5550 if (v != (void*)1)
5551 mddev_put(mddev);
5552 return next_mddev;
5553
5554}
5555
5556static void md_seq_stop(struct seq_file *seq, void *v)
5557{
5558 mddev_t *mddev = v;
5559
5560 if (mddev && v != (void*)1 && v != (void*)2)
5561 mddev_put(mddev);
5562}
5563
NeilBrownd7603b72006-01-06 00:20:30 -08005564struct mdstat_info {
5565 int event;
5566};
5567
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568static int md_seq_show(struct seq_file *seq, void *v)
5569{
5570 mddev_t *mddev = v;
5571 sector_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005573 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005574 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575
5576 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005577 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 seq_printf(seq, "Personalities : ");
5579 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005580 list_for_each_entry(pers, &pers_list, list)
5581 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
5583 spin_unlock(&pers_lock);
5584 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005585 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 return 0;
5587 }
5588 if (v == (void*)2) {
5589 status_unused(seq);
5590 return 0;
5591 }
5592
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005593 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005595
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5597 seq_printf(seq, "%s : %sactive", mdname(mddev),
5598 mddev->pers ? "" : "in");
5599 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005600 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005602 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005603 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 seq_printf(seq, " %s", mddev->pers->name);
5605 }
5606
5607 size = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005608 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 char b[BDEVNAME_SIZE];
5610 seq_printf(seq, " %s[%d]",
5611 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005612 if (test_bit(WriteMostly, &rdev->flags))
5613 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005614 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 seq_printf(seq, "(F)");
5616 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005617 } else if (rdev->raid_disk < 0)
5618 seq_printf(seq, "(S)"); /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 size += rdev->size;
5620 }
5621
5622 if (!list_empty(&mddev->disks)) {
5623 if (mddev->pers)
5624 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005625 (unsigned long long)
5626 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 else
5628 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005629 (unsigned long long)size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005631 if (mddev->persistent) {
5632 if (mddev->major_version != 0 ||
5633 mddev->minor_version != 90) {
5634 seq_printf(seq," super %d.%d",
5635 mddev->major_version,
5636 mddev->minor_version);
5637 }
NeilBrowne6910632008-02-06 01:39:51 -08005638 } else if (mddev->external)
5639 seq_printf(seq, " super external:%s",
5640 mddev->metadata_type);
5641 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005642 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643
5644 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005645 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005647 if (mddev->pers->sync_request) {
5648 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005649 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005650 seq_printf(seq, "\n ");
5651 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5652 seq_printf(seq, "\tresync=DELAYED\n ");
5653 else if (mddev->recovery_cp < MaxSector)
5654 seq_printf(seq, "\tresync=PENDING\n ");
5655 }
NeilBrown32a76272005-06-21 17:17:14 -07005656 } else
5657 seq_printf(seq, "\n ");
5658
5659 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005660 unsigned long chunk_kb;
5661 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005662 spin_lock_irqsave(&bitmap->lock, flags);
5663 chunk_kb = bitmap->chunksize >> 10;
5664 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5665 "%lu%s chunk",
5666 bitmap->pages - bitmap->missing_pages,
5667 bitmap->pages,
5668 (bitmap->pages - bitmap->missing_pages)
5669 << (PAGE_SHIFT - 10),
5670 chunk_kb ? chunk_kb : bitmap->chunksize,
5671 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005672 if (bitmap->file) {
5673 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005674 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005675 }
NeilBrown78d742d2005-06-21 17:17:15 -07005676
NeilBrown32a76272005-06-21 17:17:14 -07005677 seq_printf(seq, "\n");
5678 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 }
5680
5681 seq_printf(seq, "\n");
5682 }
5683 mddev_unlock(mddev);
5684
5685 return 0;
5686}
5687
5688static struct seq_operations md_seq_ops = {
5689 .start = md_seq_start,
5690 .next = md_seq_next,
5691 .stop = md_seq_stop,
5692 .show = md_seq_show,
5693};
5694
5695static int md_seq_open(struct inode *inode, struct file *file)
5696{
5697 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08005698 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
5699 if (mi == NULL)
5700 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701
5702 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08005703 if (error)
5704 kfree(mi);
5705 else {
5706 struct seq_file *p = file->private_data;
5707 p->private = mi;
5708 mi->event = atomic_read(&md_event_count);
5709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 return error;
5711}
5712
NeilBrownd7603b72006-01-06 00:20:30 -08005713static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
5714{
5715 struct seq_file *m = filp->private_data;
5716 struct mdstat_info *mi = m->private;
5717 int mask;
5718
5719 poll_wait(filp, &md_event_waiters, wait);
5720
5721 /* always allow read */
5722 mask = POLLIN | POLLRDNORM;
5723
5724 if (mi->event != atomic_read(&md_event_count))
5725 mask |= POLLERR | POLLPRI;
5726 return mask;
5727}
5728
Arjan van de Venfa027c22007-02-12 00:55:33 -08005729static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07005730 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 .open = md_seq_open,
5732 .read = seq_read,
5733 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07005734 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08005735 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736};
5737
NeilBrown2604b702006-01-06 00:20:36 -08005738int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005741 list_add_tail(&p->list, &pers_list);
5742 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 spin_unlock(&pers_lock);
5744 return 0;
5745}
5746
NeilBrown2604b702006-01-06 00:20:36 -08005747int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748{
NeilBrown2604b702006-01-06 00:20:36 -08005749 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005751 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 spin_unlock(&pers_lock);
5753 return 0;
5754}
5755
NeilBrowneea1bf32009-03-31 14:27:02 +11005756static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757{
5758 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11005760 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761
5762 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10005763 rcu_read_lock();
5764 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11005766 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
5767 (int)part_stat_read(&disk->part0, sectors[1]) -
5768 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07005769 /* sync IO will cause sync_io to increase before the disk_stats
5770 * as sync_io is counted when a request starts, and
5771 * disk_stats is counted when it completes.
5772 * So resync activity will cause curr_events to be smaller than
5773 * when there was no such activity.
5774 * non-sync IO will cause disk_stat to increase without
5775 * increasing sync_io so curr_events will (eventually)
5776 * be larger than it was before. Once it becomes
5777 * substantially larger, the test below will cause
5778 * the array to appear non-idle, and resync will slow
5779 * down.
5780 * If there is a lot of outstanding resync activity when
5781 * we set last_event to curr_events, then all that activity
5782 * completing might cause the array to appear non-idle
5783 * and resync will be slowed down even though there might
5784 * not have been non-resync activity. This will only
5785 * happen once though. 'last_events' will soon reflect
5786 * the state where there is little or no outstanding
5787 * resync requests, and further resync activity will
5788 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08005789 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 */
NeilBrowneea1bf32009-03-31 14:27:02 +11005791 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 rdev->last_events = curr_events;
5793 idle = 0;
5794 }
5795 }
NeilBrown4b809912008-07-21 17:05:25 +10005796 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 return idle;
5798}
5799
5800void md_done_sync(mddev_t *mddev, int blocks, int ok)
5801{
5802 /* another "blocks" (512byte) blocks have been synced */
5803 atomic_sub(blocks, &mddev->recovery_active);
5804 wake_up(&mddev->recovery_wait);
5805 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07005806 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 md_wakeup_thread(mddev->thread);
5808 // stop recovery, signal do_sync ....
5809 }
5810}
5811
5812
NeilBrown06d91a52005-06-21 17:17:12 -07005813/* md_write_start(mddev, bi)
5814 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07005815 * in superblock) before writing, schedule a superblock update
5816 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07005817 */
NeilBrown3d310eb2005-06-21 17:17:26 -07005818void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819{
Neil Brown0fd62b82008-06-28 08:31:36 +10005820 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005821 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07005822 return;
NeilBrown06d91a52005-06-21 17:17:12 -07005823
NeilBrownf91de922005-11-08 21:39:36 -08005824 BUG_ON(mddev->ro == 1);
5825 if (mddev->ro == 2) {
5826 /* need to switch to read/write */
5827 mddev->ro = 0;
5828 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5829 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08005830 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005831 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08005832 }
NeilBrown06d91a52005-06-21 17:17:12 -07005833 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07005834 if (mddev->safemode == 1)
5835 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07005836 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08005837 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07005838 if (mddev->in_sync) {
5839 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005840 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07005841 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10005842 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07005843 }
NeilBrowna9701a32005-11-08 21:39:34 -08005844 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07005845 }
Neil Brown0fd62b82008-06-28 08:31:36 +10005846 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11005847 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07005848 wait_event(mddev->sb_wait,
5849 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
5850 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851}
5852
5853void md_write_end(mddev_t *mddev)
5854{
5855 if (atomic_dec_and_test(&mddev->writes_pending)) {
5856 if (mddev->safemode == 2)
5857 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07005858 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
5860 }
5861}
5862
NeilBrown2a2275d2007-01-26 00:57:11 -08005863/* md_allow_write(mddev)
5864 * Calling this ensures that the array is marked 'active' so that writes
5865 * may proceed without blocking. It is important to call this before
5866 * attempting a GFP_KERNEL allocation while holding the mddev lock.
5867 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07005868 *
5869 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
5870 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08005871 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07005872int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08005873{
5874 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005875 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005876 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005877 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10005878 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07005879 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005880
5881 spin_lock_irq(&mddev->write_lock);
5882 if (mddev->in_sync) {
5883 mddev->in_sync = 0;
5884 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
5885 if (mddev->safemode_delay &&
5886 mddev->safemode == 0)
5887 mddev->safemode = 1;
5888 spin_unlock_irq(&mddev->write_lock);
5889 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11005890 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08005891 } else
5892 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07005893
5894 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
5895 return -EAGAIN;
5896 else
5897 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08005898}
5899EXPORT_SYMBOL_GPL(md_allow_write);
5900
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901#define SYNC_MARKS 10
5902#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08005903void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904{
5905 mddev_t *mddev2;
5906 unsigned int currspeed = 0,
5907 window;
NeilBrown57afd892005-06-21 17:17:13 -07005908 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 unsigned long mark[SYNC_MARKS];
5910 sector_t mark_cnt[SYNC_MARKS];
5911 int last_mark,m;
5912 struct list_head *tmp;
5913 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07005914 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005915 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07005916 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
5918 /* just incase thread restarts... */
5919 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
5920 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07005921 if (mddev->ro) /* never try to sync a read-only array */
5922 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923
NeilBrown61df9d92006-10-03 01:15:57 -07005924 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
5925 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
5926 desc = "data-check";
5927 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
5928 desc = "requested-resync";
5929 else
5930 desc = "resync";
5931 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
5932 desc = "reshape";
5933 else
5934 desc = "recovery";
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 /* we overload curr_resync somewhat here.
5937 * 0 == not engaged in resync at all
5938 * 2 == checking that there is no conflict with another sync
5939 * 1 == like 2, but have yielded to allow conflicting resync to
5940 * commense
5941 * other == active in resync - this many blocks
5942 *
5943 * Before starting a resync we must have set curr_resync to
5944 * 2, and then checked that every "conflicting" array has curr_resync
5945 * less than ours. When we find one that is the same or higher
5946 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
5947 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
5948 * This will mean we have to start checking from the beginning again.
5949 *
5950 */
5951
5952 do {
5953 mddev->curr_resync = 2;
5954
5955 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08005956 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07005957 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 goto skip;
5959 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08005960 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961 if (mddev2 == mddev)
5962 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07005963 if (!mddev->parallel_resync
5964 && mddev2->curr_resync
5965 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 DEFINE_WAIT(wq);
5967 if (mddev < mddev2 && mddev->curr_resync == 2) {
5968 /* arbitrarily yield */
5969 mddev->curr_resync = 1;
5970 wake_up(&resync_wait);
5971 }
5972 if (mddev > mddev2 && mddev->curr_resync == 1)
5973 /* no need to wait here, we can wait the next
5974 * time 'round when curr_resync == 2
5975 */
5976 continue;
NeilBrown97441972008-09-19 11:49:54 +10005977 /* We need to wait 'interruptible' so as not to
5978 * contribute to the load average, and not to
5979 * be caught by 'softlockup'
5980 */
5981 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08005982 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07005983 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07005984 printk(KERN_INFO "md: delaying %s of %s"
5985 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07005987 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10005989 if (signal_pending(current))
5990 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 schedule();
5992 finish_wait(&resync_wait, &wq);
5993 goto try_again;
5994 }
5995 finish_wait(&resync_wait, &wq);
5996 }
5997 }
5998 } while (mddev->curr_resync < 2);
5999
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006000 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006001 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006003 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 */
6005 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006006 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006007 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006008 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6009 j = mddev->resync_min;
6010 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006011 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006012
NeilBrownccfcc3c2006-03-27 01:18:09 -08006013 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6014 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006015 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 /* recovery follows the physical size of devices */
6017 max_sectors = mddev->size << 1;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006018 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006019 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006020 if (rdev->raid_disk >= 0 &&
6021 !test_bit(Faulty, &rdev->flags) &&
6022 !test_bit(In_sync, &rdev->flags) &&
6023 rdev->recovery_offset < j)
6024 j = rdev->recovery_offset;
6025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
NeilBrown61df9d92006-10-03 01:15:57 -07006027 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6028 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6029 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006030 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006031 "(but not more than %d KB/sec) for %s.\n",
6032 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
NeilBrowneea1bf32009-03-31 14:27:02 +11006034 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006035
NeilBrown57afd892005-06-21 17:17:13 -07006036 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 for (m = 0; m < SYNC_MARKS; m++) {
6038 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006039 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 }
6041 last_mark = 0;
6042 mddev->resync_mark = mark[last_mark];
6043 mddev->resync_mark_cnt = mark_cnt[last_mark];
6044
6045 /*
6046 * Tune reconstruction:
6047 */
6048 window = 32*(PAGE_SIZE/512);
6049 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6050 window/2,(unsigned long long) max_sectors/2);
6051
6052 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 last_check = 0;
6054
6055 if (j>2) {
6056 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006057 "md: resuming %s of %s from checkpoint.\n",
6058 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 mddev->curr_resync = j;
6060 }
6061
6062 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006063 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064
NeilBrown57afd892005-06-21 17:17:13 -07006065 skipped = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006066 if (j >= mddev->resync_max) {
6067 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
6068 wait_event(mddev->recovery_wait,
6069 mddev->resync_max > j
6070 || kthread_should_stop());
6071 }
6072 if (kthread_should_stop())
6073 goto interrupted;
NeilBrown57afd892005-06-21 17:17:13 -07006074 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006075 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006076 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006077 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 goto out;
6079 }
NeilBrown57afd892005-06-21 17:17:13 -07006080
6081 if (!skipped) { /* actual IO requested */
6082 io_sectors += sectors;
6083 atomic_add(sectors, &mddev->recovery_active);
6084 }
6085
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 j += sectors;
6087 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006088 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006089 if (last_check == 0)
6090 /* this is the earliers that rebuilt will be
6091 * visible in /proc/mdstat
6092 */
6093 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006094
6095 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 continue;
6097
NeilBrown57afd892005-06-21 17:17:13 -07006098 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099
NeilBrowndfc70642008-05-23 13:04:39 -07006100 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 break;
6102
6103 repeat:
6104 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6105 /* step marks */
6106 int next = (last_mark+1) % SYNC_MARKS;
6107
6108 mddev->resync_mark = mark[next];
6109 mddev->resync_mark_cnt = mark_cnt[next];
6110 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006111 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 last_mark = next;
6113 }
6114
6115
NeilBrownc6207272008-02-06 01:39:52 -08006116 if (kthread_should_stop())
6117 goto interrupted;
6118
Linus Torvalds1da177e2005-04-16 15:20:36 -07006119
6120 /*
6121 * this loop exits only if either when we are slower than
6122 * the 'hard' speed limit, or the system was IO-idle for
6123 * a jiffy.
6124 * the system might be non-idle CPU-wise, but we only care
6125 * about not overloading the IO subsystem. (things like an
6126 * e2fsck being done on the RAID array should execute fast)
6127 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006128 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006129 cond_resched();
6130
NeilBrown57afd892005-06-21 17:17:13 -07006131 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6132 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133
NeilBrown88202a02006-01-06 00:21:36 -08006134 if (currspeed > speed_min(mddev)) {
6135 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006136 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006137 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006138 goto repeat;
6139 }
6140 }
6141 }
NeilBrown61df9d92006-10-03 01:15:57 -07006142 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 /*
6144 * this also signals 'finished resyncing' to md_stop
6145 */
6146 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006147 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148
6149 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6150
6151 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006152 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006153
NeilBrowndfc70642008-05-23 13:04:39 -07006154 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006155 mddev->curr_resync > 2) {
6156 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6157 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6158 if (mddev->curr_resync >= mddev->recovery_cp) {
6159 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006160 "md: checkpointing %s of %s.\n",
6161 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006162 mddev->recovery_cp = mddev->curr_resync;
6163 }
6164 } else
6165 mddev->recovery_cp = MaxSector;
6166 } else {
6167 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6168 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006169 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006170 if (rdev->raid_disk >= 0 &&
6171 !test_bit(Faulty, &rdev->flags) &&
6172 !test_bit(In_sync, &rdev->flags) &&
6173 rdev->recovery_offset < mddev->curr_resync)
6174 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176 }
NeilBrown17571282006-12-10 02:20:52 -08006177 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179 skip:
6180 mddev->curr_resync = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10006181 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006182 mddev->resync_max = MaxSector;
6183 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184 wake_up(&resync_wait);
6185 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6186 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006187 return;
6188
6189 interrupted:
6190 /*
6191 * got a signal, exit.
6192 */
6193 printk(KERN_INFO
6194 "md: md_do_sync() got signal ... exiting\n");
6195 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6196 goto out;
6197
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198}
NeilBrown29269552006-03-27 01:18:10 -08006199EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200
6201
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006202static int remove_and_add_spares(mddev_t *mddev)
6203{
6204 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006205 int spares = 0;
6206
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006207 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006208 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006209 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006210 (test_bit(Faulty, &rdev->flags) ||
6211 ! test_bit(In_sync, &rdev->flags)) &&
6212 atomic_read(&rdev->nr_pending)==0) {
6213 if (mddev->pers->hot_remove_disk(
6214 mddev, rdev->raid_disk)==0) {
6215 char nm[20];
6216 sprintf(nm,"rd%d", rdev->raid_disk);
6217 sysfs_remove_link(&mddev->kobj, nm);
6218 rdev->raid_disk = -1;
6219 }
6220 }
6221
NeilBrown4044ba52009-01-09 08:31:11 +11006222 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006223 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006224 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006225 !test_bit(In_sync, &rdev->flags) &&
6226 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006227 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006228 if (rdev->raid_disk < 0
6229 && !test_bit(Faulty, &rdev->flags)) {
6230 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006231 if (mddev->pers->
6232 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006233 char nm[20];
6234 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006235 if (sysfs_create_link(&mddev->kobj,
6236 &rdev->kobj, nm))
6237 printk(KERN_WARNING
6238 "md: cannot register "
6239 "%s for %s\n",
6240 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006241 spares++;
6242 md_new_event(mddev);
6243 } else
6244 break;
6245 }
NeilBrowndfc70642008-05-23 13:04:39 -07006246 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006247 }
6248 return spares;
6249}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250/*
6251 * This routine is regularly called by all per-raid-array threads to
6252 * deal with generic issues like resync and super-block update.
6253 * Raid personalities that don't have a thread (linear/raid0) do not
6254 * need this as they never do any recovery or update the superblock.
6255 *
6256 * It does not do any resync itself, but rather "forks" off other threads
6257 * to do that as needed.
6258 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6259 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006260 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 * and wakeups up this thread which will reap the thread and finish up.
6262 * This thread also removes any faulty devices (with nr_pending == 0).
6263 *
6264 * The overall approach is:
6265 * 1/ if the superblock needs updating, update it.
6266 * 2/ If a recovery thread is running, don't do anything else.
6267 * 3/ If recovery has finished, clean up, possibly marking spares active.
6268 * 4/ If there are any faulty devices, remove them.
6269 * 5/ If array is degraded, try to add spares devices
6270 * 6/ If array has spares or is not in-sync, start a resync thread.
6271 */
6272void md_check_recovery(mddev_t *mddev)
6273{
6274 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275
6276
NeilBrown5f404022005-06-21 17:17:16 -07006277 if (mddev->bitmap)
6278 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279
6280 if (mddev->ro)
6281 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006282
6283 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006284 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006285 printk(KERN_INFO "md: %s in immediate safe mode\n",
6286 mdname(mddev));
6287 mddev->safemode = 2;
6288 }
6289 flush_signals(current);
6290 }
6291
NeilBrownc89a8ee2008-08-05 15:54:13 +10006292 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006294 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006295 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006297 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006298 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006299 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6300 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 ))
6302 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006303
NeilBrowndf5b89b2006-03-27 01:18:20 -08006304 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006305 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006306
NeilBrownc89a8ee2008-08-05 15:54:13 +10006307 if (mddev->ro) {
6308 /* Only thing we do on a ro array is remove
6309 * failed devices.
6310 */
6311 remove_and_add_spares(mddev);
6312 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6313 goto unlock;
6314 }
6315
NeilBrown31a59e32008-04-30 00:52:30 -07006316 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006317 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006318 spin_lock_irq(&mddev->write_lock);
6319 if (mddev->safemode &&
6320 !atomic_read(&mddev->writes_pending) &&
6321 !mddev->in_sync &&
6322 mddev->recovery_cp == MaxSector) {
6323 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006324 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006325 if (mddev->persistent)
6326 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6327 }
6328 if (mddev->safemode == 1)
6329 mddev->safemode = 0;
6330 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006331 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006332 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006333 }
NeilBrownfca4d842005-06-21 17:17:11 -07006334
NeilBrown850b2b42006-10-03 01:15:46 -07006335 if (mddev->flags)
6336 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006337
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006338 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006339 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006340 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006341
NeilBrown06d91a52005-06-21 17:17:12 -07006342
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6344 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6345 /* resync/recovery still happening */
6346 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6347 goto unlock;
6348 }
6349 if (mddev->sync_thread) {
6350 /* resync has finished, collect result */
6351 md_unregister_thread(mddev->sync_thread);
6352 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006353 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6354 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 /* success...*/
6356 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006357 if (mddev->pers->spare_active(mddev))
6358 sysfs_notify(&mddev->kobj, NULL,
6359 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 }
NeilBrown850b2b42006-10-03 01:15:46 -07006361 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006362
6363 /* if array is no-longer degraded, then any saved_raid_disk
6364 * information must be scrapped
6365 */
6366 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006367 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006368 rdev->saved_raid_disk = -1;
6369
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 mddev->recovery = 0;
6371 /* flag recovery needed just to double check */
6372 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006373 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006374 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 goto unlock;
6376 }
Neil Brown72a23c22008-06-28 08:31:41 +10006377 /* Set RUNNING before clearing NEEDED to avoid
6378 * any transients in the value of "sync_action".
6379 */
6380 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6381 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006382 /* Clear some bits that don't mean anything, but
6383 * might be left set
6384 */
NeilBrown24dd4692005-11-08 21:39:26 -08006385 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6386 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006388 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6389 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390 /* no recovery is running.
6391 * remove any failed drives, then
6392 * add spares if possible.
6393 * Spare are also removed and re-added, to allow
6394 * the personality to fail the re-add.
6395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006397 if (mddev->reshape_position != MaxSector) {
6398 if (mddev->pers->check_reshape(mddev) != 0)
6399 /* Cannot proceed */
6400 goto unlock;
6401 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006402 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006403 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006404 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6405 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006406 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006407 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006408 } else if (mddev->recovery_cp < MaxSector) {
6409 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006410 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006411 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6412 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006413 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006414
Linus Torvalds1da177e2005-04-16 15:20:36 -07006415 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006416 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6417 /* We are adding a device or devices to an array
6418 * which has the bitmap stored on all devices.
6419 * So make sure all bitmap pages get written
6420 */
6421 bitmap_write_all(mddev->bitmap);
6422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006423 mddev->sync_thread = md_register_thread(md_do_sync,
6424 mddev,
6425 "%s_resync");
6426 if (!mddev->sync_thread) {
6427 printk(KERN_ERR "%s: could not start resync"
6428 " thread...\n",
6429 mdname(mddev));
6430 /* leave the spares where they are, it shouldn't hurt */
6431 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006432 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006434 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006435 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 }
6437 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006438 if (!mddev->sync_thread) {
6439 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6440 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6441 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006442 if (mddev->sysfs_action)
6443 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006445 mddev_unlock(mddev);
6446 }
6447}
6448
Dan Williams6bfe0b42008-04-30 00:52:32 -07006449void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6450{
NeilBrown3c0ee632008-10-21 13:25:28 +11006451 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006452 wait_event_timeout(rdev->blocked_wait,
6453 !test_bit(Blocked, &rdev->flags),
6454 msecs_to_jiffies(5000));
6455 rdev_dec_pending(rdev, mddev);
6456}
6457EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6458
Adrian Bunk75c96f82005-05-05 16:16:09 -07006459static int md_notify_reboot(struct notifier_block *this,
6460 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461{
6462 struct list_head *tmp;
6463 mddev_t *mddev;
6464
6465 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6466
6467 printk(KERN_INFO "md: stopping all md devices.\n");
6468
NeilBrown29ac4aa2008-02-06 01:39:58 -08006469 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006470 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006471 /* Force a switch to readonly even array
6472 * appears to still be in use. Hence
6473 * the '100'.
6474 */
NeilBrownd710e132008-10-13 11:55:12 +11006475 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006476 mddev_unlock(mddev);
6477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478 /*
6479 * certain more exotic SCSI devices are known to be
6480 * volatile wrt too early system reboots. While the
6481 * right place to handle this issue is the given
6482 * driver, we do want to have a safe RAID driver ...
6483 */
6484 mdelay(1000*1);
6485 }
6486 return NOTIFY_DONE;
6487}
6488
Adrian Bunk75c96f82005-05-05 16:16:09 -07006489static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 .notifier_call = md_notify_reboot,
6491 .next = NULL,
6492 .priority = INT_MAX, /* before any real devices */
6493};
6494
6495static void md_geninit(void)
6496{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006497 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6498
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006499 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500}
6501
Adrian Bunk75c96f82005-05-05 16:16:09 -07006502static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006504 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006505 return -1;
6506 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006507 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006508 return -1;
6509 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006510 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006511 md_probe, NULL, NULL);
6512 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513 md_probe, NULL, NULL);
6514
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006516 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517
6518 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006519 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520}
6521
6522
6523#ifndef MODULE
6524
6525/*
6526 * Searches all registered partitions for autorun RAID arrays
6527 * at boot time.
6528 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006529
6530static LIST_HEAD(all_detected_devices);
6531struct detected_devices_node {
6532 struct list_head list;
6533 dev_t dev;
6534};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006535
6536void md_autodetect_dev(dev_t dev)
6537{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006538 struct detected_devices_node *node_detected_dev;
6539
6540 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6541 if (node_detected_dev) {
6542 node_detected_dev->dev = dev;
6543 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6544 } else {
6545 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6546 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006548}
6549
6550
6551static void autostart_arrays(int part)
6552{
6553 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006554 struct detected_devices_node *node_detected_dev;
6555 dev_t dev;
6556 int i_scanned, i_passed;
6557
6558 i_scanned = 0;
6559 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560
6561 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6562
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006563 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6564 i_scanned++;
6565 node_detected_dev = list_entry(all_detected_devices.next,
6566 struct detected_devices_node, list);
6567 list_del(&node_detected_dev->list);
6568 dev = node_detected_dev->dev;
6569 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006570 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 if (IS_ERR(rdev))
6572 continue;
6573
NeilBrownb2d444d2005-11-08 21:39:31 -08006574 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 MD_BUG();
6576 continue;
6577 }
NeilBrownd0fae182008-03-04 14:29:31 -08006578 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006580 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006581 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006582
6583 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6584 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585
6586 autorun_devices(part);
6587}
6588
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006589#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
6591static __exit void md_exit(void)
6592{
6593 mddev_t *mddev;
6594 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006595
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006596 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006597 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006599 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 unregister_blkdev(mdp_major, "mdp");
6601 unregister_reboot_notifier(&md_notifier);
6602 unregister_sysctl_table(raid_table_header);
6603 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006604 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006606 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 }
6608}
6609
Dan Williams685784a2007-07-09 11:56:42 -07006610subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611module_exit(md_exit)
6612
NeilBrownf91de922005-11-08 21:39:36 -08006613static int get_ro(char *buffer, struct kernel_param *kp)
6614{
6615 return sprintf(buffer, "%d", start_readonly);
6616}
6617static int set_ro(const char *val, struct kernel_param *kp)
6618{
6619 char *e;
6620 int num = simple_strtoul(val, &e, 10);
6621 if (*val && (*e == '\0' || *e == '\n')) {
6622 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006623 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006624 }
6625 return -EINVAL;
6626}
6627
NeilBrown80ca3a42006-07-10 04:44:18 -07006628module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6629module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006630
NeilBrownefeb53c2009-01-09 08:31:10 +11006631module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006632
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633EXPORT_SYMBOL(register_md_personality);
6634EXPORT_SYMBOL(unregister_md_personality);
6635EXPORT_SYMBOL(md_error);
6636EXPORT_SYMBOL(md_done_sync);
6637EXPORT_SYMBOL(md_write_start);
6638EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639EXPORT_SYMBOL(md_register_thread);
6640EXPORT_SYMBOL(md_unregister_thread);
6641EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642EXPORT_SYMBOL(md_check_recovery);
6643MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006644MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006645MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);