blob: b182f86a19dda9c71d12553e9d6891f2e24d0cfe [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>
NeilBrownbff61972009-03-31 14:33:13 +110036#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sysctl.h>
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/buffer_head.h> /* for invalidate_bdev */
NeilBrownd7603b72006-01-06 00:20:30 -080040#include <linux/poll.h>
NeilBrown16f17b32006-06-26 00:27:37 -070041#include <linux/ctype.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110042#include <linux/hdreg.h>
43#include <linux/proc_fs.h>
44#include <linux/random.h>
45#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070046#include <linux/file.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110047#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110048#include <linux/raid/md_p.h>
49#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110050#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110051#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#define DEBUG 0
54#define dprintk(x...) ((void)(DEBUG && printk(x)))
55
56
57#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110058static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#endif
60
NeilBrown2604b702006-01-06 00:20:36 -080061static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(pers_lock);
63
Adrian Bunk5e563412006-06-26 00:27:42 -070064static void md_print_devices(void);
65
Bernd Schubert90b08712008-05-23 13:04:38 -070066static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
67
Adrian Bunk5e563412006-06-26 00:27:42 -070068#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
71 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
72 * is 1000 KB/sec, so the extra system load does not show up that much.
73 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070074 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * subsystem is idle. There is also an 'absolute maximum' reconstruction
76 * speed limit - in case reconstruction slows down your system despite
77 * idle IO detection.
78 *
79 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080080 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static int sysctl_speed_limit_min = 1000;
84static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080085static inline int speed_min(mddev_t *mddev)
86{
87 return mddev->sync_speed_min ?
88 mddev->sync_speed_min : sysctl_speed_limit_min;
89}
90
91static inline int speed_max(mddev_t *mddev)
92{
93 return mddev->sync_speed_max ?
94 mddev->sync_speed_max : sysctl_speed_limit_max;
95}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97static struct ctl_table_header *raid_table_header;
98
99static ctl_table raid_table[] = {
100 {
101 .ctl_name = DEV_RAID_SPEED_LIMIT_MIN,
102 .procname = "speed_limit_min",
103 .data = &sysctl_speed_limit_min,
104 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700105 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 .proc_handler = &proc_dointvec,
107 },
108 {
109 .ctl_name = DEV_RAID_SPEED_LIMIT_MAX,
110 .procname = "speed_limit_max",
111 .data = &sysctl_speed_limit_max,
112 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700113 .mode = S_IRUGO|S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 .proc_handler = &proc_dointvec,
115 },
116 { .ctl_name = 0 }
117};
118
119static ctl_table raid_dir_table[] = {
120 {
121 .ctl_name = DEV_RAID,
122 .procname = "raid",
123 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700124 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 .child = raid_table,
126 },
127 { .ctl_name = 0 }
128};
129
130static ctl_table raid_root_table[] = {
131 {
132 .ctl_name = CTL_DEV,
133 .procname = "dev",
134 .maxlen = 0,
135 .mode = 0555,
136 .child = raid_dir_table,
137 },
138 { .ctl_name = 0 }
139};
140
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700141static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
NeilBrownf91de922005-11-08 21:39:36 -0800143static int start_readonly;
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
NeilBrownd7603b72006-01-06 00:20:30 -0800146 * We have a system wide 'event count' that is incremented
147 * on any 'interesting' event, and readers of /proc/mdstat
148 * can use 'poll' or 'select' to find out when the event
149 * count increases.
150 *
151 * Events are:
152 * start array, stop array, error, add device, remove device,
153 * start build, activate spare
154 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800155static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800156static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800157void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800158{
159 atomic_inc(&md_event_count);
160 wake_up(&md_event_waiters);
161}
NeilBrown29269552006-03-27 01:18:10 -0800162EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800163
NeilBrownc331eb02006-05-30 21:27:13 -0700164/* Alternate version that can be called from interrupts
165 * when calling sysfs_notify isn't needed.
166 */
Adrian Bunk05381952006-06-26 00:28:01 -0700167static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700168{
169 atomic_inc(&md_event_count);
170 wake_up(&md_event_waiters);
171}
172
NeilBrownd7603b72006-01-06 00:20:30 -0800173/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Enables to iterate over all existing md arrays
175 * all_mddevs_lock protects this list.
176 */
177static LIST_HEAD(all_mddevs);
178static DEFINE_SPINLOCK(all_mddevs_lock);
179
180
181/*
182 * iterates through all used mddevs in the system.
183 * We take care to grab the all_mddevs_lock whenever navigating
184 * the list, and to always hold a refcount when unlocked.
185 * Any code which breaks out of this loop while own
186 * a reference to the current mddev and must mddev_put it.
187 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800188#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 \
190 for (({ spin_lock(&all_mddevs_lock); \
191 tmp = all_mddevs.next; \
192 mddev = NULL;}); \
193 ({ if (tmp != &all_mddevs) \
194 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
195 spin_unlock(&all_mddevs_lock); \
196 if (mddev) mddev_put(mddev); \
197 mddev = list_entry(tmp, mddev_t, all_mddevs); \
198 tmp != &all_mddevs;}); \
199 ({ spin_lock(&all_mddevs_lock); \
200 tmp = tmp->next;}) \
201 )
202
203
NeilBrown409c57f2009-03-31 14:39:39 +1100204/* Rather than calling directly into the personality make_request function,
205 * IO requests come here first so that we can check if the device is
206 * being suspended pending a reconfiguration.
207 * We hold a refcount over the call to ->make_request. By the time that
208 * call has finished, the bio has been linked into some internal structure
209 * and so is visible to ->quiesce(), so we don't need the refcount any more.
210 */
211static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
NeilBrown409c57f2009-03-31 14:39:39 +1100213 mddev_t *mddev = q->queuedata;
214 int rv;
215 if (mddev == NULL || mddev->pers == NULL) {
216 bio_io_error(bio);
217 return 0;
218 }
219 rcu_read_lock();
220 if (mddev->suspended) {
221 DEFINE_WAIT(__wait);
222 for (;;) {
223 prepare_to_wait(&mddev->sb_wait, &__wait,
224 TASK_UNINTERRUPTIBLE);
225 if (!mddev->suspended)
226 break;
227 rcu_read_unlock();
228 schedule();
229 rcu_read_lock();
230 }
231 finish_wait(&mddev->sb_wait, &__wait);
232 }
233 atomic_inc(&mddev->active_io);
234 rcu_read_unlock();
235 rv = mddev->pers->make_request(q, bio);
236 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
237 wake_up(&mddev->sb_wait);
238
239 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
NeilBrown409c57f2009-03-31 14:39:39 +1100242static void mddev_suspend(mddev_t *mddev)
243{
244 BUG_ON(mddev->suspended);
245 mddev->suspended = 1;
246 synchronize_rcu();
247 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
248 mddev->pers->quiesce(mddev, 1);
249 md_unregister_thread(mddev->thread);
250 mddev->thread = NULL;
251 /* we now know that no code is executing in the personality module,
252 * except possibly the tail end of a ->bi_end_io function, but that
253 * is certain to complete before the module has a chance to get
254 * unloaded
255 */
256}
257
258static void mddev_resume(mddev_t *mddev)
259{
260 mddev->suspended = 0;
261 wake_up(&mddev->sb_wait);
262 mddev->pers->quiesce(mddev, 0);
263}
264
NeilBrown3fa841d2009-09-23 18:10:29 +1000265int mddev_congested(mddev_t *mddev, int bits)
266{
267 return mddev->suspended;
268}
269EXPORT_SYMBOL(mddev_congested);
270
NeilBrown409c57f2009-03-31 14:39:39 +1100271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272static inline mddev_t *mddev_get(mddev_t *mddev)
273{
274 atomic_inc(&mddev->active);
275 return mddev;
276}
277
Dan Williams5fd3a172009-03-04 00:57:25 -0700278static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static void mddev_put(mddev_t *mddev)
281{
282 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
283 return;
NeilBrownd3374822009-01-09 08:31:10 +1100284 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
285 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100287 if (mddev->gendisk) {
288 /* we did a probe so need to clean up.
289 * Call schedule_work inside the spinlock
290 * so that flush_scheduled_work() after
291 * mddev_find will succeed in waiting for the
292 * work to be done.
293 */
294 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
295 schedule_work(&mddev->del_work);
296 } else
297 kfree(mddev);
298 }
299 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302static mddev_t * mddev_find(dev_t unit)
303{
304 mddev_t *mddev, *new = NULL;
305
306 retry:
307 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
NeilBrownefeb53c2009-01-09 08:31:10 +1100309 if (unit) {
310 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
311 if (mddev->unit == unit) {
312 mddev_get(mddev);
313 spin_unlock(&all_mddevs_lock);
314 kfree(new);
315 return mddev;
316 }
317
318 if (new) {
319 list_add(&new->all_mddevs, &all_mddevs);
320 spin_unlock(&all_mddevs_lock);
321 new->hold_active = UNTIL_IOCTL;
322 return new;
323 }
324 } else if (new) {
325 /* find an unused unit number */
326 static int next_minor = 512;
327 int start = next_minor;
328 int is_free = 0;
329 int dev = 0;
330 while (!is_free) {
331 dev = MKDEV(MD_MAJOR, next_minor);
332 next_minor++;
333 if (next_minor > MINORMASK)
334 next_minor = 0;
335 if (next_minor == start) {
336 /* Oh dear, all in use. */
337 spin_unlock(&all_mddevs_lock);
338 kfree(new);
339 return NULL;
340 }
341
342 is_free = 1;
343 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
344 if (mddev->unit == dev) {
345 is_free = 0;
346 break;
347 }
348 }
349 new->unit = dev;
350 new->md_minor = MINOR(dev);
351 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 list_add(&new->all_mddevs, &all_mddevs);
353 spin_unlock(&all_mddevs_lock);
354 return new;
355 }
356 spin_unlock(&all_mddevs_lock);
357
NeilBrown9ffae0c2006-01-06 00:20:32 -0800358 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (!new)
360 return NULL;
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 new->unit = unit;
363 if (MAJOR(unit) == MD_MAJOR)
364 new->md_minor = MINOR(unit);
365 else
366 new->md_minor = MINOR(unit) >> MdpMinorShift;
367
NeilBrownc8c00a62009-08-10 12:50:52 +1000368 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800369 mutex_init(&new->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 INIT_LIST_HEAD(&new->disks);
371 INIT_LIST_HEAD(&new->all_mddevs);
372 init_timer(&new->safemode_timer);
373 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000374 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100375 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700376 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700377 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700378 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700379 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000380 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800381 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700382 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 goto retry;
385}
386
387static inline int mddev_lock(mddev_t * mddev)
388{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800389 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Dan Williamsb522adc2009-03-31 15:00:31 +1100392static inline int mddev_is_locked(mddev_t *mddev)
393{
394 return mutex_is_locked(&mddev->reconfig_mutex);
395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397static inline int mddev_trylock(mddev_t * mddev)
398{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800399 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
402static inline void mddev_unlock(mddev_t * mddev)
403{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800404 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
NeilBrown005eca52005-08-22 13:11:08 -0700406 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
NeilBrown2989ddb2006-01-06 00:20:43 -0800409static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100411 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100413 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (rdev->desc_nr == nr)
415 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return NULL;
418}
419
420static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
421{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 mdk_rdev_t *rdev;
423
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100424 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (rdev->bdev->bd_dev == dev)
426 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return NULL;
429}
430
NeilBrownd9d166c2006-01-06 00:20:51 -0800431static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800432{
433 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800434 list_for_each_entry(pers, &pers_list, list) {
435 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800436 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800437 if (strcmp(pers->name, clevel)==0)
438 return pers;
439 }
NeilBrown2604b702006-01-06 00:20:36 -0800440 return NULL;
441}
442
Andre Nollb73df2d2008-07-11 22:02:23 +1000443/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700444static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Andre Nollb73df2d2008-07-11 22:02:23 +1000446 sector_t num_sectors = bdev->bd_inode->i_size / 512;
447 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static int alloc_disk_sb(mdk_rdev_t * rdev)
451{
452 if (rdev->sb_page)
453 MD_BUG();
454
455 rdev->sb_page = alloc_page(GFP_KERNEL);
456 if (!rdev->sb_page) {
457 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000458 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 return 0;
462}
463
464static void free_disk_sb(mdk_rdev_t * rdev)
465{
466 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800467 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 rdev->sb_loaded = 0;
469 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000470 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100471 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473}
474
475
NeilBrown6712ecf2007-09-27 12:47:43 +0200476static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700477{
478 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800479 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700480
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700481 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
482 printk("md: super_written gets error=%d, uptodate=%d\n",
483 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
484 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800485 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700486 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700487
NeilBrowna9701a32005-11-08 21:39:34 -0800488 if (atomic_dec_and_test(&mddev->pending_writes))
489 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700490 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700491}
492
NeilBrown6712ecf2007-09-27 12:47:43 +0200493static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800494{
495 struct bio *bio2 = bio->bi_private;
496 mdk_rdev_t *rdev = bio2->bi_private;
497 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800498
499 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
500 error == -EOPNOTSUPP) {
501 unsigned long flags;
502 /* barriers don't appear to be supported :-( */
503 set_bit(BarriersNotsupp, &rdev->flags);
504 mddev->barriers_work = 0;
505 spin_lock_irqsave(&mddev->write_lock, flags);
506 bio2->bi_next = mddev->biolist;
507 mddev->biolist = bio2;
508 spin_unlock_irqrestore(&mddev->write_lock, flags);
509 wake_up(&mddev->sb_wait);
510 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200511 } else {
512 bio_put(bio2);
513 bio->bi_private = rdev;
514 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800515 }
NeilBrowna9701a32005-11-08 21:39:34 -0800516}
517
NeilBrown7bfa19f2005-06-21 17:17:28 -0700518void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
519 sector_t sector, int size, struct page *page)
520{
521 /* write first size bytes of page to sector of rdev
522 * Increment mddev->pending_writes before returning
523 * and decrement it on completion, waking up sb_wait
524 * if zero is reached.
525 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800526 *
527 * As we might need to resubmit the request if BIO_RW_BARRIER
528 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700529 */
530 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100531 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700532
533 bio->bi_bdev = rdev->bdev;
534 bio->bi_sector = sector;
535 bio_add_page(bio, page, size, 0);
536 bio->bi_private = rdev;
537 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800538 bio->bi_rw = rw;
539
NeilBrown7bfa19f2005-06-21 17:17:28 -0700540 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800541 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
542 struct bio *rbio;
543 rw |= (1<<BIO_RW_BARRIER);
544 rbio = bio_clone(bio, GFP_NOIO);
545 rbio->bi_private = bio;
546 rbio->bi_end_io = super_written_barrier;
547 submit_bio(rw, rbio);
548 } else
549 submit_bio(rw, bio);
550}
551
552void md_super_wait(mddev_t *mddev)
553{
554 /* wait for all superblock writes that were scheduled to complete.
555 * if any had to be retried (due to BARRIER problems), retry them
556 */
557 DEFINE_WAIT(wq);
558 for(;;) {
559 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
560 if (atomic_read(&mddev->pending_writes)==0)
561 break;
562 while (mddev->biolist) {
563 struct bio *bio;
564 spin_lock_irq(&mddev->write_lock);
565 bio = mddev->biolist;
566 mddev->biolist = bio->bi_next ;
567 bio->bi_next = NULL;
568 spin_unlock_irq(&mddev->write_lock);
569 submit_bio(bio->bi_rw, bio);
570 }
571 schedule();
572 }
573 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700574}
575
NeilBrown6712ecf2007-09-27 12:47:43 +0200576static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
NeilBrowna654b9d82005-06-21 17:17:27 -0700581int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct page *page, int rw)
583{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700584 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct completion event;
586 int ret;
587
Jens Axboe93dbb392009-02-16 10:25:40 +0100588 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 bio->bi_bdev = bdev;
591 bio->bi_sector = sector;
592 bio_add_page(bio, page, size, 0);
593 init_completion(&event);
594 bio->bi_private = &event;
595 bio->bi_end_io = bi_complete;
596 submit_bio(rw, bio);
597 wait_for_completion(&event);
598
599 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
600 bio_put(bio);
601 return ret;
602}
NeilBrowna8745db2006-01-06 00:20:34 -0800603EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
NeilBrown0002b272005-09-09 16:23:53 -0700605static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
607 char b[BDEVNAME_SIZE];
608 if (!rdev->sb_page) {
609 MD_BUG();
610 return -EINVAL;
611 }
612 if (rdev->sb_loaded)
613 return 0;
614
615
Andre Noll0f420352008-07-11 22:02:23 +1000616 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 goto fail;
618 rdev->sb_loaded = 1;
619 return 0;
620
621fail:
622 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
623 bdevname(rdev->bdev,b));
624 return -EINVAL;
625}
626
627static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
628{
Andre Noll05710462008-07-11 22:02:20 +1000629 return sb1->set_uuid0 == sb2->set_uuid0 &&
630 sb1->set_uuid1 == sb2->set_uuid1 &&
631 sb1->set_uuid2 == sb2->set_uuid2 &&
632 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
636{
637 int ret;
638 mdp_super_t *tmp1, *tmp2;
639
640 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
641 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
642
643 if (!tmp1 || !tmp2) {
644 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100645 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto abort;
647 }
648
649 *tmp1 = *sb1;
650 *tmp2 = *sb2;
651
652 /*
653 * nr_disks is not constant
654 */
655 tmp1->nr_disks = 0;
656 tmp2->nr_disks = 0;
657
Andre Nollce0c8e02008-07-11 22:02:20 +1000658 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700660 kfree(tmp1);
661 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return ret;
663}
664
NeilBrown4d167f02007-05-09 02:35:37 -0700665
666static u32 md_csum_fold(u32 csum)
667{
668 csum = (csum & 0xffff) + (csum >> 16);
669 return (csum & 0xffff) + (csum >> 16);
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672static unsigned int calc_sb_csum(mdp_super_t * sb)
673{
NeilBrown4d167f02007-05-09 02:35:37 -0700674 u64 newcsum = 0;
675 u32 *sb32 = (u32*)sb;
676 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 unsigned int disk_csum, csum;
678
679 disk_csum = sb->sb_csum;
680 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700681
682 for (i = 0; i < MD_SB_BYTES/4 ; i++)
683 newcsum += sb32[i];
684 csum = (newcsum & 0xffffffff) + (newcsum>>32);
685
686
687#ifdef CONFIG_ALPHA
688 /* This used to use csum_partial, which was wrong for several
689 * reasons including that different results are returned on
690 * different architectures. It isn't critical that we get exactly
691 * the same return value as before (we always csum_fold before
692 * testing, and that removes any differences). However as we
693 * know that csum_partial always returned a 16bit value on
694 * alphas, do a fold to maximise conformity to previous behaviour.
695 */
696 sb->sb_csum = md_csum_fold(disk_csum);
697#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700699#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return csum;
701}
702
703
704/*
705 * Handle superblock details.
706 * We want to be able to handle multiple superblock formats
707 * so we have a common interface to them all, and an array of
708 * different handlers.
709 * We rely on user-space to write the initial superblock, and support
710 * reading and updating of superblocks.
711 * Interface methods are:
712 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
713 * loads and validates a superblock on dev.
714 * if refdev != NULL, compare superblocks on both devices
715 * Return:
716 * 0 - dev has a superblock that is compatible with refdev
717 * 1 - dev has a superblock that is compatible and newer than refdev
718 * so dev should be used as the refdev in future
719 * -EINVAL superblock incompatible or invalid
720 * -othererror e.g. -EIO
721 *
722 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
723 * Verify that dev is acceptable into mddev.
724 * The first time, mddev->raid_disks will be 0, and data from
725 * dev should be merged in. Subsequent calls check that dev
726 * is new enough. Return 0 or -EINVAL
727 *
728 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
729 * Update the superblock for rdev with data in mddev
730 * This does not write to disc.
731 *
732 */
733
734struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000735 char *name;
736 struct module *owner;
737 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
738 int minor_version);
739 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
740 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
741 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000742 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743};
744
745/*
Andre Noll0894cc32009-06-18 08:49:23 +1000746 * Check that the given mddev has no bitmap.
747 *
748 * This function is called from the run method of all personalities that do not
749 * support bitmaps. It prints an error message and returns non-zero if mddev
750 * has a bitmap. Otherwise, it returns 0.
751 *
752 */
753int md_check_no_bitmap(mddev_t *mddev)
754{
755 if (!mddev->bitmap_file && !mddev->bitmap_offset)
756 return 0;
757 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
758 mdname(mddev), mddev->pers->name);
759 return 1;
760}
761EXPORT_SYMBOL(md_check_no_bitmap);
762
763/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 * load_super for 0.90.0
765 */
766static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
767{
768 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
769 mdp_super_t *sb;
770 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /*
Andre Noll0f420352008-07-11 22:02:23 +1000773 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * it's at the end of the disk.
775 *
776 * It also happens to be a multiple of 4Kb.
777 */
Andre Noll0f420352008-07-11 22:02:23 +1000778 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
NeilBrown0002b272005-09-09 16:23:53 -0700780 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (ret) return ret;
782
783 ret = -EINVAL;
784
785 bdevname(rdev->bdev, b);
786 sb = (mdp_super_t*)page_address(rdev->sb_page);
787
788 if (sb->md_magic != MD_SB_MAGIC) {
789 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
790 b);
791 goto abort;
792 }
793
794 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800795 sb->minor_version < 90 ||
796 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
798 sb->major_version, sb->minor_version,
799 b);
800 goto abort;
801 }
802
803 if (sb->raid_disks <= 0)
804 goto abort;
805
NeilBrown4d167f02007-05-09 02:35:37 -0700806 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
808 b);
809 goto abort;
810 }
811
812 rdev->preferred_minor = sb->md_minor;
813 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700814 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (sb->level == LEVEL_MULTIPATH)
817 rdev->desc_nr = -1;
818 else
819 rdev->desc_nr = sb->this_disk.number;
820
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700821 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700823 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 __u64 ev1, ev2;
825 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
826 if (!uuid_equal(refsb, sb)) {
827 printk(KERN_WARNING "md: %s has different UUID to %s\n",
828 b, bdevname(refdev->bdev,b2));
829 goto abort;
830 }
831 if (!sb_equal(refsb, sb)) {
832 printk(KERN_WARNING "md: %s has same UUID"
833 " but different superblock to %s\n",
834 b, bdevname(refdev->bdev, b2));
835 goto abort;
836 }
837 ev1 = md_event(sb);
838 ev2 = md_event(refsb);
839 if (ev1 > ev2)
840 ret = 1;
841 else
842 ret = 0;
843 }
NeilBrown8190e752009-06-18 08:48:58 +1000844 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Andre Nolldd8ac332009-03-31 14:33:13 +1100846 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800847 /* "this cannot possibly happen" ... */
848 ret = -EINVAL;
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 abort:
851 return ret;
852}
853
854/*
855 * validate_super for 0.90.0
856 */
857static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
858{
859 mdp_disk_t *desc;
860 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700861 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
NeilBrown41158c72005-06-21 17:17:25 -0700863 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800864 clear_bit(Faulty, &rdev->flags);
865 clear_bit(In_sync, &rdev->flags);
866 clear_bit(WriteMostly, &rdev->flags);
867 clear_bit(BarriersNotsupp, &rdev->flags);
868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (mddev->raid_disks == 0) {
870 mddev->major_version = 0;
871 mddev->minor_version = sb->minor_version;
872 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800873 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000874 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 mddev->ctime = sb->ctime;
876 mddev->utime = sb->utime;
877 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800878 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 mddev->layout = sb->layout;
880 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100881 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700882 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700883 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700884 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
NeilBrownf6705572006-03-27 01:18:11 -0800886 if (mddev->minor_version >= 91) {
887 mddev->reshape_position = sb->reshape_position;
888 mddev->delta_disks = sb->delta_disks;
889 mddev->new_level = sb->new_level;
890 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000891 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800892 } else {
893 mddev->reshape_position = MaxSector;
894 mddev->delta_disks = 0;
895 mddev->new_level = mddev->level;
896 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000897 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800898 }
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (sb->state & (1<<MD_SB_CLEAN))
901 mddev->recovery_cp = MaxSector;
902 else {
903 if (sb->events_hi == sb->cp_events_hi &&
904 sb->events_lo == sb->cp_events_lo) {
905 mddev->recovery_cp = sb->recovery_cp;
906 } else
907 mddev->recovery_cp = 0;
908 }
909
910 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
911 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
912 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
913 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
914
915 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700916
917 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700918 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700919 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700920
NeilBrown41158c72005-06-21 17:17:25 -0700921 } else if (mddev->pers == NULL) {
922 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 ++ev1;
924 if (ev1 < mddev->events)
925 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700926 } else if (mddev->bitmap) {
927 /* if adding to array with a bitmap, then we can accept an
928 * older device ... but not too old.
929 */
NeilBrown41158c72005-06-21 17:17:25 -0700930 if (ev1 < mddev->bitmap->events_cleared)
931 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700932 } else {
933 if (ev1 < mddev->events)
934 /* just a hot-add of a new device, leave raid_disk at -1 */
935 return 0;
936 }
NeilBrown41158c72005-06-21 17:17:25 -0700937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 desc = sb->disks + rdev->desc_nr;
940
941 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800942 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700943 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
944 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800945 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd92009-11-13 17:40:48 +1100947 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
948 /* active but not in sync implies recovery up to
949 * reshape position. We don't know exactly where
950 * that is, so set to zero for now */
951 if (mddev->minor_version >= 91) {
952 rdev->recovery_offset = 0;
953 rdev->raid_disk = desc->raid_disk;
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700956 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
957 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700958 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800959 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return 0;
961}
962
963/*
964 * sync_super for 0.90.0
965 */
966static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
967{
968 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 mdk_rdev_t *rdev2;
970 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /* make rdev->sb match mddev data..
974 *
975 * 1/ zero out disks
976 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
977 * 3/ any empty disks < next_spare become removed
978 *
979 * disks[0] gets initialised to REMOVED because
980 * we cannot be sure from other fields if it has
981 * been initialised or not.
982 */
983 int i;
984 int active=0, working=0,failed=0,spare=0,nr_disks=0;
985
NeilBrown61181562005-09-09 16:24:02 -0700986 rdev->sb_size = MD_SB_BYTES;
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 sb = (mdp_super_t*)page_address(rdev->sb_page);
989
990 memset(sb, 0, sizeof(*sb));
991
992 sb->md_magic = MD_SB_MAGIC;
993 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 sb->patch_version = mddev->patch_version;
995 sb->gvalid_words = 0; /* ignored */
996 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
997 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
998 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
999 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1000
1001 sb->ctime = mddev->ctime;
1002 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001003 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 sb->raid_disks = mddev->raid_disks;
1005 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001006 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 sb->utime = mddev->utime;
1008 sb->state = 0;
1009 sb->events_hi = (mddev->events>>32);
1010 sb->events_lo = (u32)mddev->events;
1011
NeilBrownf6705572006-03-27 01:18:11 -08001012 if (mddev->reshape_position == MaxSector)
1013 sb->minor_version = 90;
1014 else {
1015 sb->minor_version = 91;
1016 sb->reshape_position = mddev->reshape_position;
1017 sb->new_level = mddev->new_level;
1018 sb->delta_disks = mddev->delta_disks;
1019 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001020 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001021 }
1022 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (mddev->in_sync)
1024 {
1025 sb->recovery_cp = mddev->recovery_cp;
1026 sb->cp_events_hi = (mddev->events>>32);
1027 sb->cp_events_lo = (u32)mddev->events;
1028 if (mddev->recovery_cp == MaxSector)
1029 sb->state = (1<< MD_SB_CLEAN);
1030 } else
1031 sb->recovery_cp = 0;
1032
1033 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001034 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
NeilBrowna654b9d82005-06-21 17:17:27 -07001036 if (mddev->bitmap && mddev->bitmap_file == NULL)
1037 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001040 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001042 int desc_nr;
NeilBrown0261cd92009-11-13 17:40:48 +11001043 int is_active = test_bit(In_sync, &rdev2->flags);
1044
1045 if (rdev2->raid_disk >= 0 &&
1046 sb->minor_version >= 91)
1047 /* we have nowhere to store the recovery_offset,
1048 * but if it is not below the reshape_position,
1049 * we can piggy-back on that.
1050 */
1051 is_active = 1;
1052 if (rdev2->raid_disk < 0 ||
1053 test_bit(Faulty, &rdev2->flags))
1054 is_active = 0;
1055 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001056 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001058 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001059 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 d = &sb->disks[rdev2->desc_nr];
1061 nr_disks++;
1062 d->number = rdev2->desc_nr;
1063 d->major = MAJOR(rdev2->bdev->bd_dev);
1064 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd92009-11-13 17:40:48 +11001065 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 d->raid_disk = rdev2->raid_disk;
1067 else
1068 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001069 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd92009-11-13 17:40:48 +11001071 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd92009-11-13 17:40:48 +11001073 if (test_bit(In_sync, &rdev2->flags))
1074 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 active++;
1076 working++;
1077 } else {
1078 d->state = 0;
1079 spare++;
1080 working++;
1081 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001082 if (test_bit(WriteMostly, &rdev2->flags))
1083 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 /* now set the "removed" and "faulty" bits on any missing devices */
1086 for (i=0 ; i < mddev->raid_disks ; i++) {
1087 mdp_disk_t *d = &sb->disks[i];
1088 if (d->state == 0 && d->number == 0) {
1089 d->number = i;
1090 d->raid_disk = i;
1091 d->state = (1<<MD_DISK_REMOVED);
1092 d->state |= (1<<MD_DISK_FAULTY);
1093 failed++;
1094 }
1095 }
1096 sb->nr_disks = nr_disks;
1097 sb->active_disks = active;
1098 sb->working_disks = working;
1099 sb->failed_disks = failed;
1100 sb->spare_disks = spare;
1101
1102 sb->this_disk = sb->disks[rdev->desc_nr];
1103 sb->sb_csum = calc_sb_csum(sb);
1104}
1105
1106/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001107 * rdev_size_change for 0.90.0
1108 */
1109static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001110super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001111{
Andre Noll58c0fed2009-03-31 14:33:13 +11001112 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001113 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001114 if (rdev->mddev->bitmap_offset)
1115 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001116 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001117 if (!num_sectors || num_sectors > rdev->sb_start)
1118 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001119 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001120 rdev->sb_page);
1121 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001122 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001123}
1124
1125
1126/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 * version 1 superblock
1128 */
1129
NeilBrown1c05b4b2006-10-21 10:24:08 -07001130static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001132 __le32 disk_csum;
1133 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 unsigned long long newcsum;
1135 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001136 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int i;
1138
1139 disk_csum = sb->sb_csum;
1140 sb->sb_csum = 0;
1141 newcsum = 0;
1142 for (i=0; size>=4; size -= 4 )
1143 newcsum += le32_to_cpu(*isuper++);
1144
1145 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001146 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1149 sb->sb_csum = disk_csum;
1150 return cpu_to_le32(csum);
1151}
1152
1153static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1154{
1155 struct mdp_superblock_1 *sb;
1156 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001157 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001159 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
1161 /*
Andre Noll0f420352008-07-11 22:02:23 +10001162 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * It is always aligned to a 4K boundary and
1164 * depeding on minor_version, it can be:
1165 * 0: At least 8K, but less than 12K, from end of device
1166 * 1: At start of device
1167 * 2: 4K from start of device.
1168 */
1169 switch(minor_version) {
1170 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001171 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1172 sb_start -= 8*2;
1173 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001176 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 break;
1178 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001179 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
1181 default:
1182 return -EINVAL;
1183 }
Andre Noll0f420352008-07-11 22:02:23 +10001184 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
NeilBrown0002b272005-09-09 16:23:53 -07001186 /* superblock is rarely larger than 1K, but it can be larger,
1187 * and it is safe to read 4k, so we do that
1188 */
1189 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (ret) return ret;
1191
1192
1193 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1194
1195 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1196 sb->major_version != cpu_to_le32(1) ||
1197 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001198 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001199 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return -EINVAL;
1201
1202 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1203 printk("md: invalid superblock checksum on %s\n",
1204 bdevname(rdev->bdev,b));
1205 return -EINVAL;
1206 }
1207 if (le64_to_cpu(sb->data_size) < 10) {
1208 printk("md: data_size too small on %s\n",
1209 bdevname(rdev->bdev,b));
1210 return -EINVAL;
1211 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 rdev->preferred_minor = 0xffff;
1214 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001215 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
NeilBrown0002b272005-09-09 16:23:53 -07001217 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001218 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001219 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001220 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1221
1222 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001223 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001224 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001225
NeilBrown31b65a02006-07-10 04:44:14 -07001226 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1227 rdev->desc_nr = -1;
1228 else
1229 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1230
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001231 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001232 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001233 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 __u64 ev1, ev2;
1235 struct mdp_superblock_1 *refsb =
1236 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1237
1238 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1239 sb->level != refsb->level ||
1240 sb->layout != refsb->layout ||
1241 sb->chunksize != refsb->chunksize) {
1242 printk(KERN_WARNING "md: %s has strangely different"
1243 " superblock to %s\n",
1244 bdevname(rdev->bdev,b),
1245 bdevname(refdev->bdev,b2));
1246 return -EINVAL;
1247 }
1248 ev1 = le64_to_cpu(sb->events);
1249 ev2 = le64_to_cpu(refsb->events);
1250
1251 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001252 ret = 1;
1253 else
1254 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
NeilBrowna1801f82008-03-04 14:29:31 -08001256 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001257 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1258 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001260 rdev->sectors = rdev->sb_start;
1261 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001263 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001264 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001265 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001266 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1270{
1271 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001272 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
NeilBrown41158c72005-06-21 17:17:25 -07001274 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001275 clear_bit(Faulty, &rdev->flags);
1276 clear_bit(In_sync, &rdev->flags);
1277 clear_bit(WriteMostly, &rdev->flags);
1278 clear_bit(BarriersNotsupp, &rdev->flags);
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (mddev->raid_disks == 0) {
1281 mddev->major_version = 1;
1282 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001283 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001284 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1286 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1287 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001288 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 mddev->layout = le32_to_cpu(sb->layout);
1290 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001291 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001292 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001293 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001294 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1297 memcpy(mddev->uuid, sb->set_uuid, 16);
1298
1299 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001300
NeilBrown71c08052005-09-09 16:23:51 -07001301 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001302 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001303 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001304
NeilBrownf6705572006-03-27 01:18:11 -08001305 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1306 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1307 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1308 mddev->new_level = le32_to_cpu(sb->new_level);
1309 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001310 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001311 } else {
1312 mddev->reshape_position = MaxSector;
1313 mddev->delta_disks = 0;
1314 mddev->new_level = mddev->level;
1315 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001316 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001317 }
1318
NeilBrown41158c72005-06-21 17:17:25 -07001319 } else if (mddev->pers == NULL) {
1320 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 ++ev1;
1322 if (ev1 < mddev->events)
1323 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001324 } else if (mddev->bitmap) {
1325 /* If adding to array with a bitmap, then we can accept an
1326 * older device, but not too old.
1327 */
NeilBrown41158c72005-06-21 17:17:25 -07001328 if (ev1 < mddev->bitmap->events_cleared)
1329 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001330 } else {
1331 if (ev1 < mddev->events)
1332 /* just a hot-add of a new device, leave raid_disk at -1 */
1333 return 0;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (mddev->level != LEVEL_MULTIPATH) {
1336 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001337 if (rdev->desc_nr < 0 ||
1338 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1339 role = 0xffff;
1340 rdev->desc_nr = -1;
1341 } else
1342 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 switch(role) {
1344 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 break;
1346 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001347 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 break;
1349 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001350 if ((le32_to_cpu(sb->feature_map) &
1351 MD_FEATURE_RECOVERY_OFFSET))
1352 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1353 else
1354 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 rdev->raid_disk = role;
1356 break;
1357 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001358 if (sb->devflags & WriteMostly1)
1359 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001360 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001361 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return 0;
1364}
1365
1366static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1367{
1368 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 mdk_rdev_t *rdev2;
1370 int max_dev, i;
1371 /* make rdev->sb match mddev and rdev data. */
1372
1373 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1374
1375 sb->feature_map = 0;
1376 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001377 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 memset(sb->pad1, 0, sizeof(sb->pad1));
1379 memset(sb->pad2, 0, sizeof(sb->pad2));
1380 memset(sb->pad3, 0, sizeof(sb->pad3));
1381
1382 sb->utime = cpu_to_le64((__u64)mddev->utime);
1383 sb->events = cpu_to_le64(mddev->events);
1384 if (mddev->in_sync)
1385 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1386 else
1387 sb->resync_offset = cpu_to_le64(0);
1388
NeilBrown1c05b4b2006-10-21 10:24:08 -07001389 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001390
NeilBrownf0ca3402006-02-02 14:28:04 -08001391 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001392 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001393 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001394 sb->level = cpu_to_le32(mddev->level);
1395 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001396
NeilBrowna654b9d82005-06-21 17:17:27 -07001397 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1398 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001399 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001400 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001401
1402 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001403 !test_bit(In_sync, &rdev->flags)) {
NeilBrown97e4f422009-03-31 14:33:13 +11001404 if (rdev->recovery_offset > 0) {
1405 sb->feature_map |=
1406 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1407 sb->recovery_offset =
1408 cpu_to_le64(rdev->recovery_offset);
1409 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001410 }
1411
NeilBrownf6705572006-03-27 01:18:11 -08001412 if (mddev->reshape_position != MaxSector) {
1413 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1414 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1415 sb->new_layout = cpu_to_le32(mddev->new_layout);
1416 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1417 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001418 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001419 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001422 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (rdev2->desc_nr+1 > max_dev)
1424 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001425
NeilBrown70471da2009-08-03 10:59:57 +10001426 if (max_dev > le32_to_cpu(sb->max_dev)) {
1427 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001428 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001429 rdev->sb_size = max_dev * 2 + 256;
1430 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1431 if (rdev->sb_size & bmask)
1432 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 for (i=0; i<max_dev;i++)
1435 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1436
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001437 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001439 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001441 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001443 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1444 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 else
1446 sb->dev_roles[i] = cpu_to_le16(0xffff);
1447 }
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 sb->sb_csum = calc_sb_1_csum(sb);
1450}
1451
Chris Webb0cd17fe2008-06-28 08:31:46 +10001452static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001453super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001454{
1455 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001456 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001457 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001458 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001459 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001460 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001461 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1462 max_sectors -= rdev->data_offset;
1463 if (!num_sectors || num_sectors > max_sectors)
1464 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001465 } else if (rdev->mddev->bitmap_offset) {
1466 /* minor version 0 with bitmap we can't move */
1467 return 0;
1468 } else {
1469 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001470 sector_t sb_start;
1471 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1472 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001473 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001474 if (!num_sectors || num_sectors > max_sectors)
1475 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001476 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001477 }
1478 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001479 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001480 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001481 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001482 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001483 rdev->sb_page);
1484 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001485 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Adrian Bunk75c96f82005-05-05 16:16:09 -07001488static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 [0] = {
1490 .name = "0.90.0",
1491 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001492 .load_super = super_90_load,
1493 .validate_super = super_90_validate,
1494 .sync_super = super_90_sync,
1495 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 },
1497 [1] = {
1498 .name = "md-1",
1499 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001500 .load_super = super_1_load,
1501 .validate_super = super_1_validate,
1502 .sync_super = super_1_sync,
1503 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 },
1505};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1508{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001509 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
NeilBrown4b809912008-07-21 17:05:25 +10001511 rcu_read_lock();
1512 rdev_for_each_rcu(rdev, mddev1)
1513 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001514 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001515 rdev2->bdev->bd_contains) {
1516 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001517 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001518 }
1519 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return 0;
1521}
1522
1523static LIST_HEAD(pending_raid_disks);
1524
Andre Nollac5e7112009-08-03 10:59:47 +10001525/*
1526 * Try to register data integrity profile for an mddev
1527 *
1528 * This is called when an array is started and after a disk has been kicked
1529 * from the array. It only succeeds if all working and active component devices
1530 * are integrity capable with matching profiles.
1531 */
1532int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001533{
Andre Nollac5e7112009-08-03 10:59:47 +10001534 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001535
Andre Nollac5e7112009-08-03 10:59:47 +10001536 if (list_empty(&mddev->disks))
1537 return 0; /* nothing to do */
1538 if (blk_get_integrity(mddev->gendisk))
1539 return 0; /* already registered */
1540 list_for_each_entry(rdev, &mddev->disks, same_set) {
1541 /* skip spares and non-functional disks */
1542 if (test_bit(Faulty, &rdev->flags))
1543 continue;
1544 if (rdev->raid_disk < 0)
1545 continue;
1546 /*
1547 * If at least one rdev is not integrity capable, we can not
1548 * enable data integrity for the md device.
1549 */
1550 if (!bdev_get_integrity(rdev->bdev))
1551 return -EINVAL;
1552 if (!reference) {
1553 /* Use the first rdev as the reference */
1554 reference = rdev;
1555 continue;
1556 }
1557 /* does this rdev's profile match the reference profile? */
1558 if (blk_integrity_compare(reference->bdev->bd_disk,
1559 rdev->bdev->bd_disk) < 0)
1560 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001561 }
Andre Nollac5e7112009-08-03 10:59:47 +10001562 /*
1563 * All component devices are integrity capable and have matching
1564 * profiles, register the common profile for the md device.
1565 */
1566 if (blk_integrity_register(mddev->gendisk,
1567 bdev_get_integrity(reference->bdev)) != 0) {
1568 printk(KERN_ERR "md: failed to register integrity for %s\n",
1569 mdname(mddev));
1570 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001571 }
Andre Nollac5e7112009-08-03 10:59:47 +10001572 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1573 mdname(mddev));
1574 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001575}
Andre Nollac5e7112009-08-03 10:59:47 +10001576EXPORT_SYMBOL(md_integrity_register);
1577
1578/* Disable data integrity if non-capable/non-matching disk is being added */
1579void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1580{
1581 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1582 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1583
1584 if (!bi_mddev) /* nothing to do */
1585 return;
1586 if (rdev->raid_disk < 0) /* skip spares */
1587 return;
1588 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1589 rdev->bdev->bd_disk) >= 0)
1590 return;
1591 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1592 blk_integrity_unregister(mddev->gendisk);
1593}
1594EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1597{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001598 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001599 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001600 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001601 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 if (rdev->mddev) {
1604 MD_BUG();
1605 return -EINVAL;
1606 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001607
1608 /* prevent duplicates */
1609 if (find_rdev(mddev, rdev->bdev->bd_dev))
1610 return -EEXIST;
1611
Andre Nolldd8ac332009-03-31 14:33:13 +11001612 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1613 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1614 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001615 if (mddev->pers) {
1616 /* Cannot change size, so fail
1617 * If mddev->level <= 0, then we don't care
1618 * about aligning sizes (e.g. linear)
1619 */
1620 if (mddev->level > 0)
1621 return -ENOSPC;
1622 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001623 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /* Verify rdev->desc_nr is unique.
1627 * If it is -1, assign a free number, else
1628 * check number is not in use
1629 */
1630 if (rdev->desc_nr < 0) {
1631 int choice = 0;
1632 if (mddev->pers) choice = mddev->raid_disks;
1633 while (find_rdev_nr(mddev, choice))
1634 choice++;
1635 rdev->desc_nr = choice;
1636 } else {
1637 if (find_rdev_nr(mddev, rdev->desc_nr))
1638 return -EBUSY;
1639 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001640 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1641 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1642 mdname(mddev), mddev->max_disks);
1643 return -EBUSY;
1644 }
NeilBrown19133a42005-11-08 21:39:35 -08001645 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001646 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001647 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001650 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001651
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001652 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001653 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001654
Tejun Heo0762b8b2008-08-25 19:56:12 +09001655 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001656 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1657 kobject_del(&rdev->kobj);
1658 goto fail;
1659 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001660 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1661
NeilBrown4b809912008-07-21 17:05:25 +10001662 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001663 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001664
1665 /* May as well allow recovery to be retried once */
1666 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001669
1670 fail:
1671 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1672 b, mdname(mddev));
1673 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674}
1675
NeilBrown177a99b2008-02-06 01:39:56 -08001676static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001677{
1678 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1679 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001680 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1684{
1685 char b[BDEVNAME_SIZE];
1686 if (!rdev->mddev) {
1687 MD_BUG();
1688 return;
1689 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001690 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001691 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1693 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001694 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001695 sysfs_put(rdev->sysfs_state);
1696 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001697 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001698 * writing to 'remove' to "dev/state". We also need
1699 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001700 */
NeilBrown4b809912008-07-21 17:05:25 +10001701 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001702 INIT_WORK(&rdev->del_work, md_delayed_delete);
1703 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001704 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707/*
1708 * prevent the device from being mounted, repartitioned or
1709 * otherwise reused by a RAID array (or any other kernel
1710 * subsystem), by bd_claiming the device.
1711 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001712static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int err = 0;
1715 struct block_device *bdev;
1716 char b[BDEVNAME_SIZE];
1717
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001718 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 if (IS_ERR(bdev)) {
1720 printk(KERN_ERR "md: could not open %s.\n",
1721 __bdevname(dev, b));
1722 return PTR_ERR(bdev);
1723 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001724 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (err) {
1726 printk(KERN_ERR "md: could not bd_claim %s.\n",
1727 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001728 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 return err;
1730 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001731 if (!shared)
1732 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 rdev->bdev = bdev;
1734 return err;
1735}
1736
1737static void unlock_rdev(mdk_rdev_t *rdev)
1738{
1739 struct block_device *bdev = rdev->bdev;
1740 rdev->bdev = NULL;
1741 if (!bdev)
1742 MD_BUG();
1743 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001744 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747void md_autodetect_dev(dev_t dev);
1748
1749static void export_rdev(mdk_rdev_t * rdev)
1750{
1751 char b[BDEVNAME_SIZE];
1752 printk(KERN_INFO "md: export_rdev(%s)\n",
1753 bdevname(rdev->bdev,b));
1754 if (rdev->mddev)
1755 MD_BUG();
1756 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001758 if (test_bit(AutoDetected, &rdev->flags))
1759 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760#endif
1761 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001762 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765static void kick_rdev_from_array(mdk_rdev_t * rdev)
1766{
1767 unbind_rdev_from_array(rdev);
1768 export_rdev(rdev);
1769}
1770
1771static void export_array(mddev_t *mddev)
1772{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001773 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
NeilBrownd089c6a2008-02-06 01:39:59 -08001775 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (!rdev->mddev) {
1777 MD_BUG();
1778 continue;
1779 }
1780 kick_rdev_from_array(rdev);
1781 }
1782 if (!list_empty(&mddev->disks))
1783 MD_BUG();
1784 mddev->raid_disks = 0;
1785 mddev->major_version = 0;
1786}
1787
1788static void print_desc(mdp_disk_t *desc)
1789{
1790 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1791 desc->major,desc->minor,desc->raid_disk,desc->state);
1792}
1793
Cheng Renquancd2ac932009-01-09 08:31:08 +11001794static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
1796 int i;
1797
1798 printk(KERN_INFO
1799 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1800 sb->major_version, sb->minor_version, sb->patch_version,
1801 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1802 sb->ctime);
1803 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1804 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1805 sb->md_minor, sb->layout, sb->chunk_size);
1806 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1807 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1808 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1809 sb->failed_disks, sb->spare_disks,
1810 sb->sb_csum, (unsigned long)sb->events_lo);
1811
1812 printk(KERN_INFO);
1813 for (i = 0; i < MD_SB_DISKS; i++) {
1814 mdp_disk_t *desc;
1815
1816 desc = sb->disks + i;
1817 if (desc->number || desc->major || desc->minor ||
1818 desc->raid_disk || (desc->state && (desc->state != 4))) {
1819 printk(" D %2d: ", i);
1820 print_desc(desc);
1821 }
1822 }
1823 printk(KERN_INFO "md: THIS: ");
1824 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Cheng Renquancd2ac932009-01-09 08:31:08 +11001827static void print_sb_1(struct mdp_superblock_1 *sb)
1828{
1829 __u8 *uuid;
1830
1831 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001832 printk(KERN_INFO
1833 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1834 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1835 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001836 le32_to_cpu(sb->major_version),
1837 le32_to_cpu(sb->feature_map),
1838 uuid[0], uuid[1], uuid[2], uuid[3],
1839 uuid[4], uuid[5], uuid[6], uuid[7],
1840 uuid[8], uuid[9], uuid[10], uuid[11],
1841 uuid[12], uuid[13], uuid[14], uuid[15],
1842 sb->set_name,
1843 (unsigned long long)le64_to_cpu(sb->ctime)
1844 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1845
1846 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001847 printk(KERN_INFO
1848 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001849 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001850 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1851 ":%02x%02x%02x%02x%02x%02x\n"
1852 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1853 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001854 le32_to_cpu(sb->level),
1855 (unsigned long long)le64_to_cpu(sb->size),
1856 le32_to_cpu(sb->raid_disks),
1857 le32_to_cpu(sb->layout),
1858 le32_to_cpu(sb->chunksize),
1859 (unsigned long long)le64_to_cpu(sb->data_offset),
1860 (unsigned long long)le64_to_cpu(sb->data_size),
1861 (unsigned long long)le64_to_cpu(sb->super_offset),
1862 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1863 le32_to_cpu(sb->dev_number),
1864 uuid[0], uuid[1], uuid[2], uuid[3],
1865 uuid[4], uuid[5], uuid[6], uuid[7],
1866 uuid[8], uuid[9], uuid[10], uuid[11],
1867 uuid[12], uuid[13], uuid[14], uuid[15],
1868 sb->devflags,
1869 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1870 (unsigned long long)le64_to_cpu(sb->events),
1871 (unsigned long long)le64_to_cpu(sb->resync_offset),
1872 le32_to_cpu(sb->sb_csum),
1873 le32_to_cpu(sb->max_dev)
1874 );
1875}
1876
1877static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001880 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1881 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001882 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1883 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001885 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1886 switch (major_version) {
1887 case 0:
1888 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1889 break;
1890 case 1:
1891 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1892 break;
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 } else
1895 printk(KERN_INFO "md: no rdev superblock!\n");
1896}
1897
Adrian Bunk5e563412006-06-26 00:27:42 -07001898static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001900 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 mdk_rdev_t *rdev;
1902 mddev_t *mddev;
1903 char b[BDEVNAME_SIZE];
1904
1905 printk("\n");
1906 printk("md: **********************************\n");
1907 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1908 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001909 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
NeilBrown32a76272005-06-21 17:17:14 -07001911 if (mddev->bitmap)
1912 bitmap_print_sb(mddev->bitmap);
1913 else
1914 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001915 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 printk("<%s>", bdevname(rdev->bdev,b));
1917 printk("\n");
1918
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001919 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001920 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922 printk("md: **********************************\n");
1923 printk("\n");
1924}
1925
1926
NeilBrown42543762006-06-26 00:27:57 -07001927static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
NeilBrown42543762006-06-26 00:27:57 -07001929 /* Update each superblock (in-memory image), but
1930 * if we are allowed to, skip spares which already
1931 * have the right event counter, or have one earlier
1932 * (which would mean they aren't being marked as dirty
1933 * with the rest of the array)
1934 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
NeilBrown5e865102009-11-12 12:08:04 +11001937 /* First make sure individual recovery_offsets are correct */
1938 list_for_each_entry(rdev, &mddev->disks, same_set) {
1939 if (rdev->raid_disk >= 0 &&
1940 !test_bit(In_sync, &rdev->flags) &&
1941 mddev->curr_resync_completed > rdev->recovery_offset)
1942 rdev->recovery_offset = mddev->curr_resync_completed;
1943
1944 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001945 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001946 if (rdev->sb_events == mddev->events ||
1947 (nospares &&
1948 rdev->raid_disk < 0 &&
1949 (rdev->sb_events&1)==0 &&
1950 rdev->sb_events+1 == mddev->events)) {
1951 /* Don't update this superblock */
1952 rdev->sb_loaded = 2;
1953 } else {
1954 super_types[mddev->major_version].
1955 sync_super(mddev, rdev);
1956 rdev->sb_loaded = 1;
1957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959}
1960
NeilBrown850b2b42006-10-03 01:15:46 -07001961static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001964 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001965 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
NeilBrown1b57f132009-06-18 08:48:19 +10001967 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07001968 if (mddev->external)
1969 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001971 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001972
NeilBrown850b2b42006-10-03 01:15:46 -07001973 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1974 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1975 force_change = 1;
1976 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1977 /* just a clean<-> dirty transition, possibly leave spares alone,
1978 * though if events isn't the right even/odd, we will have to do
1979 * spares after all
1980 */
1981 nospares = 1;
1982 if (force_change)
1983 nospares = 0;
1984 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001985 /* If the array is degraded, then skipping spares is both
1986 * dangerous and fairly pointless.
1987 * Dangerous because a device that was removed from the array
1988 * might have a event_count that still looks up-to-date,
1989 * so it can be re-added without a resync.
1990 * Pointless because if there are any spares to skip,
1991 * then a recovery will happen and soon that array won't
1992 * be degraded any more and the spare can go back to sleep then.
1993 */
NeilBrown850b2b42006-10-03 01:15:46 -07001994 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001995
NeilBrown06d91a52005-06-21 17:17:12 -07001996 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07001997
1998 /* If this is just a dirty<->clean transition, and the array is clean
1999 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002000 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002001 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002002 && (mddev->events & 1)
2003 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002004 mddev->events--;
2005 else {
2006 /* otherwise we have to go forward and ... */
2007 mddev->events ++;
2008 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002009 /* .. if the array isn't clean, an 'even' event must also go
2010 * to spares. */
2011 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002012 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002013 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002014 /* otherwise an 'odd' event must go to spares */
2015 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002016 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002017 }
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 if (!mddev->events) {
2021 /*
2022 * oops, this 64-bit counter should never wrap.
2023 * Either we are in around ~1 trillion A.C., assuming
2024 * 1 reboot per second, or we have a bug:
2025 */
2026 MD_BUG();
2027 mddev->events --;
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 /*
2031 * do not write anything to disk if using
2032 * nonpersistent superblocks
2033 */
NeilBrown06d91a52005-06-21 17:17:12 -07002034 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002035 if (!mddev->external)
2036 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2037
NeilBrowna9701a32005-11-08 21:39:34 -08002038 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002039 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002041 }
NeilBrowne6910632008-02-06 01:39:51 -08002042 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002043 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 dprintk(KERN_INFO
2046 "md: updating %s RAID superblock on device (in sync %d)\n",
2047 mdname(mddev),mddev->in_sync);
2048
NeilBrown4ad13662007-07-17 04:06:13 -07002049 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002050 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 char b[BDEVNAME_SIZE];
2052 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002053 if (rdev->sb_loaded != 1)
2054 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002055 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 dprintk("(skipping faulty ");
2057
2058 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002059 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002060 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002061 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002062 rdev->sb_page);
2063 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2064 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002065 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002066 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 } else
2069 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002070 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /* only need to write one superblock... */
2072 break;
2073 }
NeilBrowna9701a32005-11-08 21:39:34 -08002074 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002075 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002076
NeilBrowna9701a32005-11-08 21:39:34 -08002077 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002078 if (mddev->in_sync != sync_req ||
2079 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002080 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002081 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002082 goto repeat;
2083 }
NeilBrown850b2b42006-10-03 01:15:46 -07002084 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002085 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002086 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002087 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2088 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Andre Noll7f6ce762008-03-23 18:34:54 +01002092/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002093 * We want to accept with case. For this we use cmd_match.
2094 */
2095static int cmd_match(const char *cmd, const char *str)
2096{
2097 /* See if cmd, written into a sysfs file, matches
2098 * str. They must either be the same, or cmd can
2099 * have a trailing newline
2100 */
2101 while (*cmd && *str && *cmd == *str) {
2102 cmd++;
2103 str++;
2104 }
2105 if (*cmd == '\n')
2106 cmd++;
2107 if (*str || *cmd)
2108 return 0;
2109 return 1;
2110}
2111
NeilBrown86e6ffd2005-11-08 21:39:24 -08002112struct rdev_sysfs_entry {
2113 struct attribute attr;
2114 ssize_t (*show)(mdk_rdev_t *, char *);
2115 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2116};
2117
2118static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002119state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002120{
2121 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002122 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002123
NeilBrownb2d444d2005-11-08 21:39:31 -08002124 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002125 len+= sprintf(page+len, "%sfaulty",sep);
2126 sep = ",";
2127 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002128 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002129 len += sprintf(page+len, "%sin_sync",sep);
2130 sep = ",";
2131 }
NeilBrownf6556752006-06-26 00:28:01 -07002132 if (test_bit(WriteMostly, &rdev->flags)) {
2133 len += sprintf(page+len, "%swrite_mostly",sep);
2134 sep = ",";
2135 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002136 if (test_bit(Blocked, &rdev->flags)) {
2137 len += sprintf(page+len, "%sblocked", sep);
2138 sep = ",";
2139 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002140 if (!test_bit(Faulty, &rdev->flags) &&
2141 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002142 len += sprintf(page+len, "%sspare", sep);
2143 sep = ",";
2144 }
2145 return len+sprintf(page+len, "\n");
2146}
2147
NeilBrown45dc2de2006-06-26 00:27:58 -07002148static ssize_t
2149state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2150{
2151 /* can write
2152 * faulty - simulates and error
2153 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002154 * writemostly - sets write_mostly
2155 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002156 * blocked - sets the Blocked flag
2157 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002158 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002159 */
2160 int err = -EINVAL;
2161 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2162 md_error(rdev->mddev, rdev);
2163 err = 0;
2164 } else if (cmd_match(buf, "remove")) {
2165 if (rdev->raid_disk >= 0)
2166 err = -EBUSY;
2167 else {
2168 mddev_t *mddev = rdev->mddev;
2169 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002170 if (mddev->pers)
2171 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002172 md_new_event(mddev);
2173 err = 0;
2174 }
NeilBrownf6556752006-06-26 00:28:01 -07002175 } else if (cmd_match(buf, "writemostly")) {
2176 set_bit(WriteMostly, &rdev->flags);
2177 err = 0;
2178 } else if (cmd_match(buf, "-writemostly")) {
2179 clear_bit(WriteMostly, &rdev->flags);
2180 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002181 } else if (cmd_match(buf, "blocked")) {
2182 set_bit(Blocked, &rdev->flags);
2183 err = 0;
2184 } else if (cmd_match(buf, "-blocked")) {
2185 clear_bit(Blocked, &rdev->flags);
2186 wake_up(&rdev->blocked_wait);
2187 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2188 md_wakeup_thread(rdev->mddev->thread);
2189
2190 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002191 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2192 set_bit(In_sync, &rdev->flags);
2193 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002194 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002195 if (!err && rdev->sysfs_state)
2196 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002197 return err ? err : len;
2198}
NeilBrown80ca3a42006-07-10 04:44:18 -07002199static struct rdev_sysfs_entry rdev_state =
2200__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002201
2202static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002203errors_show(mdk_rdev_t *rdev, char *page)
2204{
2205 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2206}
2207
2208static ssize_t
2209errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2210{
2211 char *e;
2212 unsigned long n = simple_strtoul(buf, &e, 10);
2213 if (*buf && (*e == 0 || *e == '\n')) {
2214 atomic_set(&rdev->corrected_errors, n);
2215 return len;
2216 }
2217 return -EINVAL;
2218}
2219static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002220__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002221
NeilBrown014236d2006-01-06 00:20:55 -08002222static ssize_t
2223slot_show(mdk_rdev_t *rdev, char *page)
2224{
2225 if (rdev->raid_disk < 0)
2226 return sprintf(page, "none\n");
2227 else
2228 return sprintf(page, "%d\n", rdev->raid_disk);
2229}
2230
2231static ssize_t
2232slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2233{
2234 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002235 int err;
2236 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002237 int slot = simple_strtoul(buf, &e, 10);
2238 if (strncmp(buf, "none", 4)==0)
2239 slot = -1;
2240 else if (e==buf || (*e && *e!= '\n'))
2241 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002242 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002243 /* Setting 'slot' on an active array requires also
2244 * updating the 'rd%d' link, and communicating
2245 * with the personality with ->hot_*_disk.
2246 * For now we only support removing
2247 * failed/spare devices. This normally happens automatically,
2248 * but not when the metadata is externally managed.
2249 */
NeilBrownc303da62008-02-06 01:39:51 -08002250 if (rdev->raid_disk == -1)
2251 return -EEXIST;
2252 /* personality does all needed checks */
2253 if (rdev->mddev->pers->hot_add_disk == NULL)
2254 return -EINVAL;
2255 err = rdev->mddev->pers->
2256 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2257 if (err)
2258 return err;
2259 sprintf(nm, "rd%d", rdev->raid_disk);
2260 sysfs_remove_link(&rdev->mddev->kobj, nm);
2261 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2262 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002263 } else if (rdev->mddev->pers) {
2264 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002265 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002266 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002267 */
2268
2269 if (rdev->raid_disk != -1)
2270 return -EBUSY;
2271
2272 if (rdev->mddev->pers->hot_add_disk == NULL)
2273 return -EINVAL;
2274
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002275 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002276 if (rdev2->raid_disk == slot)
2277 return -EEXIST;
2278
2279 rdev->raid_disk = slot;
2280 if (test_bit(In_sync, &rdev->flags))
2281 rdev->saved_raid_disk = slot;
2282 else
2283 rdev->saved_raid_disk = -1;
2284 err = rdev->mddev->pers->
2285 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002286 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002287 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002288 return err;
Neil Brown52664732008-06-28 08:31:44 +10002289 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002290 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002291 sprintf(nm, "rd%d", rdev->raid_disk);
2292 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2293 printk(KERN_WARNING
2294 "md: cannot register "
2295 "%s for %s\n",
2296 nm, mdname(rdev->mddev));
2297
2298 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002299 } else {
2300 if (slot >= rdev->mddev->raid_disks)
2301 return -ENOSPC;
2302 rdev->raid_disk = slot;
2303 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002304 clear_bit(Faulty, &rdev->flags);
2305 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002306 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002307 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002308 }
NeilBrown014236d2006-01-06 00:20:55 -08002309 return len;
2310}
2311
2312
2313static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002314__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002315
NeilBrown93c8cad2006-01-06 00:20:56 -08002316static ssize_t
2317offset_show(mdk_rdev_t *rdev, char *page)
2318{
Andrew Morton6961ece2006-01-06 00:20:59 -08002319 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002320}
2321
2322static ssize_t
2323offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2324{
2325 char *e;
2326 unsigned long long offset = simple_strtoull(buf, &e, 10);
2327 if (e==buf || (*e && *e != '\n'))
2328 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002329 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002330 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002331 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002332 /* Must set offset before size, so overlap checks
2333 * can be sane */
2334 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002335 rdev->data_offset = offset;
2336 return len;
2337}
2338
2339static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002340__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002341
NeilBrown83303b62006-01-06 00:21:06 -08002342static ssize_t
2343rdev_size_show(mdk_rdev_t *rdev, char *page)
2344{
Andre Nolldd8ac332009-03-31 14:33:13 +11002345 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002346}
2347
NeilBrownc5d79ad2008-02-06 01:39:54 -08002348static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2349{
2350 /* check if two start/length pairs overlap */
2351 if (s1+l1 <= s2)
2352 return 0;
2353 if (s2+l2 <= s1)
2354 return 0;
2355 return 1;
2356}
2357
Dan Williamsb522adc2009-03-31 15:00:31 +11002358static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2359{
2360 unsigned long long blocks;
2361 sector_t new;
2362
2363 if (strict_strtoull(buf, 10, &blocks) < 0)
2364 return -EINVAL;
2365
2366 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2367 return -EINVAL; /* sector conversion overflow */
2368
2369 new = blocks * 2;
2370 if (new != blocks * 2)
2371 return -EINVAL; /* unsigned long long to sector_t overflow */
2372
2373 *sectors = new;
2374 return 0;
2375}
2376
NeilBrown83303b62006-01-06 00:21:06 -08002377static ssize_t
2378rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2379{
NeilBrown27c529b2008-03-04 14:29:33 -08002380 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002381 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002382 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002383
Dan Williamsb522adc2009-03-31 15:00:31 +11002384 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002385 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002386 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002387 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002388 sectors = super_types[my_mddev->major_version].
2389 rdev_size_change(rdev, sectors);
2390 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002391 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002392 } else if (!sectors)
2393 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2394 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002395 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002396 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002397 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002398
Andre Nolldd8ac332009-03-31 14:33:13 +11002399 rdev->sectors = sectors;
2400 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002401 /* need to check that all other rdevs with the same ->bdev
2402 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002403 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002404 * we have to change it back, we will have the lock again.
2405 */
2406 mddev_t *mddev;
2407 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002408 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002409
NeilBrown27c529b2008-03-04 14:29:33 -08002410 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002411 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002412 mdk_rdev_t *rdev2;
2413
2414 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002415 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002416 if (test_bit(AllReserved, &rdev2->flags) ||
2417 (rdev->bdev == rdev2->bdev &&
2418 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002419 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002420 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002421 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002422 overlap = 1;
2423 break;
2424 }
2425 mddev_unlock(mddev);
2426 if (overlap) {
2427 mddev_put(mddev);
2428 break;
2429 }
2430 }
NeilBrown27c529b2008-03-04 14:29:33 -08002431 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002432 if (overlap) {
2433 /* Someone else could have slipped in a size
2434 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002435 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002436 * safe, and trust userspace not to race with
2437 * itself
2438 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002439 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002440 return -EBUSY;
2441 }
2442 }
NeilBrown83303b62006-01-06 00:21:06 -08002443 return len;
2444}
2445
2446static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002447__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002448
NeilBrown86e6ffd2005-11-08 21:39:24 -08002449static struct attribute *rdev_default_attrs[] = {
2450 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002451 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002452 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002453 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002454 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002455 NULL,
2456};
2457static ssize_t
2458rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2459{
2460 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2461 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002462 mddev_t *mddev = rdev->mddev;
2463 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002464
2465 if (!entry->show)
2466 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002467
2468 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2469 if (!rv) {
2470 if (rdev->mddev == NULL)
2471 rv = -EBUSY;
2472 else
2473 rv = entry->show(rdev, page);
2474 mddev_unlock(mddev);
2475 }
2476 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002477}
2478
2479static ssize_t
2480rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2481 const char *page, size_t length)
2482{
2483 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2484 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002485 ssize_t rv;
2486 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002487
2488 if (!entry->store)
2489 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002490 if (!capable(CAP_SYS_ADMIN))
2491 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002492 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002493 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002494 if (rdev->mddev == NULL)
2495 rv = -EBUSY;
2496 else
2497 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002498 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002499 }
2500 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002501}
2502
2503static void rdev_free(struct kobject *ko)
2504{
2505 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2506 kfree(rdev);
2507}
2508static struct sysfs_ops rdev_sysfs_ops = {
2509 .show = rdev_attr_show,
2510 .store = rdev_attr_store,
2511};
2512static struct kobj_type rdev_ktype = {
2513 .release = rdev_free,
2514 .sysfs_ops = &rdev_sysfs_ops,
2515 .default_attrs = rdev_default_attrs,
2516};
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518/*
2519 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2520 *
2521 * mark the device faulty if:
2522 *
2523 * - the device is nonexistent (zero size)
2524 * - the device has no valid superblock
2525 *
2526 * a faulty rdev _never_ has rdev->sb set.
2527 */
2528static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2529{
2530 char b[BDEVNAME_SIZE];
2531 int err;
2532 mdk_rdev_t *rdev;
2533 sector_t size;
2534
NeilBrown9ffae0c2006-01-06 00:20:32 -08002535 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (!rdev) {
2537 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2538 return ERR_PTR(-ENOMEM);
2539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 if ((err = alloc_disk_sb(rdev)))
2542 goto abort_free;
2543
NeilBrownc5d79ad2008-02-06 01:39:54 -08002544 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 if (err)
2546 goto abort_free;
2547
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002548 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002551 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002552 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002553 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002555 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002557 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002558 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2561 if (!size) {
2562 printk(KERN_WARNING
2563 "md: %s has zero or unknown size, marking faulty!\n",
2564 bdevname(rdev->bdev,b));
2565 err = -EINVAL;
2566 goto abort_free;
2567 }
2568
2569 if (super_format >= 0) {
2570 err = super_types[super_format].
2571 load_super(rdev, NULL, super_minor);
2572 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002573 printk(KERN_WARNING
2574 "md: %s does not have a valid v%d.%d "
2575 "superblock, not importing!\n",
2576 bdevname(rdev->bdev,b),
2577 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 goto abort_free;
2579 }
2580 if (err < 0) {
2581 printk(KERN_WARNING
2582 "md: could not read %s's sb, not importing!\n",
2583 bdevname(rdev->bdev,b));
2584 goto abort_free;
2585 }
2586 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002589 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
2591 return rdev;
2592
2593abort_free:
2594 if (rdev->sb_page) {
2595 if (rdev->bdev)
2596 unlock_rdev(rdev);
2597 free_disk_sb(rdev);
2598 }
2599 kfree(rdev);
2600 return ERR_PTR(err);
2601}
2602
2603/*
2604 * Check a full RAID array for plausibility
2605 */
2606
2607
NeilBrowna757e642005-04-16 15:26:42 -07002608static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
2610 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002611 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 char b[BDEVNAME_SIZE];
2613
2614 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002615 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 switch (super_types[mddev->major_version].
2617 load_super(rdev, freshest, mddev->minor_version)) {
2618 case 1:
2619 freshest = rdev;
2620 break;
2621 case 0:
2622 break;
2623 default:
2624 printk( KERN_ERR \
2625 "md: fatal superblock inconsistency in %s"
2626 " -- removing from array\n",
2627 bdevname(rdev->bdev,b));
2628 kick_rdev_from_array(rdev);
2629 }
2630
2631
2632 super_types[mddev->major_version].
2633 validate_super(mddev, freshest);
2634
2635 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002636 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002637 if (rdev->desc_nr >= mddev->max_disks ||
2638 i > mddev->max_disks) {
2639 printk(KERN_WARNING
2640 "md: %s: %s: only %d devices permitted\n",
2641 mdname(mddev), bdevname(rdev->bdev, b),
2642 mddev->max_disks);
2643 kick_rdev_from_array(rdev);
2644 continue;
2645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (rdev != freshest)
2647 if (super_types[mddev->major_version].
2648 validate_super(mddev, rdev)) {
2649 printk(KERN_WARNING "md: kicking non-fresh %s"
2650 " from array!\n",
2651 bdevname(rdev->bdev,b));
2652 kick_rdev_from_array(rdev);
2653 continue;
2654 }
2655 if (mddev->level == LEVEL_MULTIPATH) {
2656 rdev->desc_nr = i++;
2657 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002658 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002659 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002660 rdev->raid_disk = -1;
2661 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
NeilBrown19052c02008-08-05 15:54:13 +10002666static void md_safemode_timeout(unsigned long data);
2667
NeilBrowneae17012005-11-08 21:39:23 -08002668static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002669safe_delay_show(mddev_t *mddev, char *page)
2670{
2671 int msec = (mddev->safemode_delay*1000)/HZ;
2672 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2673}
2674static ssize_t
2675safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2676{
2677 int scale=1;
2678 int dot=0;
2679 int i;
2680 unsigned long msec;
2681 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002682
NeilBrown16f17b32006-06-26 00:27:37 -07002683 /* remove a period, and count digits after it */
2684 if (len >= sizeof(buf))
2685 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002686 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002687 for (i=0; i<len; i++) {
2688 if (dot) {
2689 if (isdigit(buf[i])) {
2690 buf[i-1] = buf[i];
2691 scale *= 10;
2692 }
2693 buf[i] = 0;
2694 } else if (buf[i] == '.') {
2695 dot=1;
2696 buf[i] = 0;
2697 }
2698 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002699 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002700 return -EINVAL;
2701 msec = (msec * 1000) / scale;
2702 if (msec == 0)
2703 mddev->safemode_delay = 0;
2704 else {
NeilBrown19052c02008-08-05 15:54:13 +10002705 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002706 mddev->safemode_delay = (msec*HZ)/1000;
2707 if (mddev->safemode_delay == 0)
2708 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002709 if (mddev->safemode_delay < old_delay)
2710 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002711 }
2712 return len;
2713}
2714static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002715__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002716
2717static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002718level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002719{
NeilBrown2604b702006-01-06 00:20:36 -08002720 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002721 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002722 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002723 else if (mddev->clevel[0])
2724 return sprintf(page, "%s\n", mddev->clevel);
2725 else if (mddev->level != LEVEL_NONE)
2726 return sprintf(page, "%d\n", mddev->level);
2727 else
2728 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002729}
2730
NeilBrownd9d166c2006-01-06 00:20:51 -08002731static ssize_t
2732level_store(mddev_t *mddev, const char *buf, size_t len)
2733{
NeilBrown245f46c2009-03-31 14:39:39 +11002734 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002735 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002736 struct mdk_personality *pers;
2737 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002738 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002739
2740 if (mddev->pers == NULL) {
2741 if (len == 0)
2742 return 0;
2743 if (len >= sizeof(mddev->clevel))
2744 return -ENOSPC;
2745 strncpy(mddev->clevel, buf, len);
2746 if (mddev->clevel[len-1] == '\n')
2747 len--;
2748 mddev->clevel[len] = 0;
2749 mddev->level = LEVEL_NONE;
2750 return rv;
2751 }
2752
2753 /* request to change the personality. Need to ensure:
2754 * - array is not engaged in resync/recovery/reshape
2755 * - old personality can be suspended
2756 * - new personality will access other array.
2757 */
2758
2759 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002760 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002761
2762 if (!mddev->pers->quiesce) {
2763 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2764 mdname(mddev), mddev->pers->name);
2765 return -EINVAL;
2766 }
2767
2768 /* Now find the new personality */
2769 if (len == 0 || len >= sizeof(level))
2770 return -EINVAL;
2771 strncpy(level, buf, len);
2772 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002773 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002774 level[len] = 0;
2775
2776 request_module("md-%s", level);
2777 spin_lock(&pers_lock);
2778 pers = find_pers(LEVEL_NONE, level);
2779 if (!pers || !try_module_get(pers->owner)) {
2780 spin_unlock(&pers_lock);
2781 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2782 return -EINVAL;
2783 }
2784 spin_unlock(&pers_lock);
2785
2786 if (pers == mddev->pers) {
2787 /* Nothing to do! */
2788 module_put(pers->owner);
2789 return rv;
2790 }
2791 if (!pers->takeover) {
2792 module_put(pers->owner);
2793 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2794 mdname(mddev), level);
2795 return -EINVAL;
2796 }
2797
2798 /* ->takeover must set new_* and/or delta_disks
2799 * if it succeeds, and may set them when it fails.
2800 */
2801 priv = pers->takeover(mddev);
2802 if (IS_ERR(priv)) {
2803 mddev->new_level = mddev->level;
2804 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002805 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002806 mddev->raid_disks -= mddev->delta_disks;
2807 mddev->delta_disks = 0;
2808 module_put(pers->owner);
2809 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2810 mdname(mddev), level);
2811 return PTR_ERR(priv);
2812 }
2813
2814 /* Looks like we have a winner */
2815 mddev_suspend(mddev);
2816 mddev->pers->stop(mddev);
2817 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002818 /* Invalidate devices that are now superfluous */
2819 list_for_each_entry(rdev, &mddev->disks, same_set)
2820 if (rdev->raid_disk >= mddev->raid_disks) {
2821 rdev->raid_disk = -1;
2822 clear_bit(In_sync, &rdev->flags);
2823 }
NeilBrown245f46c2009-03-31 14:39:39 +11002824 mddev->pers = pers;
2825 mddev->private = priv;
2826 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2827 mddev->level = mddev->new_level;
2828 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002829 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002830 mddev->delta_disks = 0;
2831 pers->run(mddev);
2832 mddev_resume(mddev);
2833 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2834 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2835 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002836 return rv;
2837}
2838
2839static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002840__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002841
NeilBrownd4dbd022006-06-26 00:27:59 -07002842
2843static ssize_t
2844layout_show(mddev_t *mddev, char *page)
2845{
2846 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002847 if (mddev->reshape_position != MaxSector &&
2848 mddev->layout != mddev->new_layout)
2849 return sprintf(page, "%d (%d)\n",
2850 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002851 return sprintf(page, "%d\n", mddev->layout);
2852}
2853
2854static ssize_t
2855layout_store(mddev_t *mddev, const char *buf, size_t len)
2856{
2857 char *e;
2858 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002859
2860 if (!*buf || (*e && *e != '\n'))
2861 return -EINVAL;
2862
NeilBrownb3546032009-03-31 14:56:41 +11002863 if (mddev->pers) {
2864 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002865 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002866 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002867 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002868 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002869 if (err) {
2870 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002871 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002872 }
NeilBrownb3546032009-03-31 14:56:41 +11002873 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002874 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002875 if (mddev->reshape_position == MaxSector)
2876 mddev->layout = n;
2877 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002878 return len;
2879}
2880static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002881__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002882
2883
NeilBrowneae17012005-11-08 21:39:23 -08002884static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002885raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002886{
NeilBrownbb636542005-11-08 21:39:45 -08002887 if (mddev->raid_disks == 0)
2888 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002889 if (mddev->reshape_position != MaxSector &&
2890 mddev->delta_disks != 0)
2891 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2892 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002893 return sprintf(page, "%d\n", mddev->raid_disks);
2894}
2895
NeilBrownda943b92006-01-06 00:20:54 -08002896static int update_raid_disks(mddev_t *mddev, int raid_disks);
2897
2898static ssize_t
2899raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2900{
NeilBrownda943b92006-01-06 00:20:54 -08002901 char *e;
2902 int rv = 0;
2903 unsigned long n = simple_strtoul(buf, &e, 10);
2904
2905 if (!*buf || (*e && *e != '\n'))
2906 return -EINVAL;
2907
2908 if (mddev->pers)
2909 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002910 else if (mddev->reshape_position != MaxSector) {
2911 int olddisks = mddev->raid_disks - mddev->delta_disks;
2912 mddev->delta_disks = n - olddisks;
2913 mddev->raid_disks = n;
2914 } else
NeilBrownda943b92006-01-06 00:20:54 -08002915 mddev->raid_disks = n;
2916 return rv ? rv : len;
2917}
2918static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002919__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002920
NeilBrown24dd4692005-11-08 21:39:26 -08002921static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002922chunk_size_show(mddev_t *mddev, char *page)
2923{
NeilBrown08a02ec2007-05-09 02:35:38 -07002924 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10002925 mddev->chunk_sectors != mddev->new_chunk_sectors)
2926 return sprintf(page, "%d (%d)\n",
2927 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10002928 mddev->chunk_sectors << 9);
2929 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08002930}
2931
2932static ssize_t
2933chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2934{
NeilBrown3b343802006-01-06 00:20:47 -08002935 char *e;
2936 unsigned long n = simple_strtoul(buf, &e, 10);
2937
NeilBrown3b343802006-01-06 00:20:47 -08002938 if (!*buf || (*e && *e != '\n'))
2939 return -EINVAL;
2940
NeilBrownb3546032009-03-31 14:56:41 +11002941 if (mddev->pers) {
2942 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002943 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002944 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002945 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10002946 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002947 if (err) {
2948 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11002949 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002950 }
NeilBrownb3546032009-03-31 14:56:41 +11002951 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10002952 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002953 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10002954 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002955 }
NeilBrown3b343802006-01-06 00:20:47 -08002956 return len;
2957}
2958static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002959__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002960
NeilBrowna94213b2006-06-26 00:28:00 -07002961static ssize_t
2962resync_start_show(mddev_t *mddev, char *page)
2963{
NeilBrownd1a7c502009-03-31 15:24:32 +11002964 if (mddev->recovery_cp == MaxSector)
2965 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002966 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2967}
2968
2969static ssize_t
2970resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2971{
NeilBrowna94213b2006-06-26 00:28:00 -07002972 char *e;
2973 unsigned long long n = simple_strtoull(buf, &e, 10);
2974
2975 if (mddev->pers)
2976 return -EBUSY;
2977 if (!*buf || (*e && *e != '\n'))
2978 return -EINVAL;
2979
2980 mddev->recovery_cp = n;
2981 return len;
2982}
2983static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002984__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002985
NeilBrown9e653b62006-06-26 00:27:58 -07002986/*
2987 * The array state can be:
2988 *
2989 * clear
2990 * No devices, no size, no level
2991 * Equivalent to STOP_ARRAY ioctl
2992 * inactive
2993 * May have some settings, but array is not active
2994 * all IO results in error
2995 * When written, doesn't tear down array, but just stops it
2996 * suspended (not supported yet)
2997 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002998 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002999 * readonly
3000 * no resync can happen. no superblocks get written.
3001 * write requests fail
3002 * read-auto
3003 * like readonly, but behaves like 'clean' on a write request.
3004 *
3005 * clean - no pending writes, but otherwise active.
3006 * When written to inactive array, starts without resync
3007 * If a write request arrives then
3008 * if metadata is known, mark 'dirty' and switch to 'active'.
3009 * if not known, block and switch to write-pending
3010 * If written to an active array that has pending writes, then fails.
3011 * active
3012 * fully active: IO and resync can be happening.
3013 * When written to inactive array, starts with resync
3014 *
3015 * write-pending
3016 * clean, but writes are blocked waiting for 'active' to be written.
3017 *
3018 * active-idle
3019 * like active, but no writes have been seen for a while (100msec).
3020 *
3021 */
3022enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3023 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003024static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003025 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3026 "write-pending", "active-idle", NULL };
3027
3028static int match_word(const char *word, char **list)
3029{
3030 int n;
3031 for (n=0; list[n]; n++)
3032 if (cmd_match(word, list[n]))
3033 break;
3034 return n;
3035}
3036
3037static ssize_t
3038array_state_show(mddev_t *mddev, char *page)
3039{
3040 enum array_state st = inactive;
3041
3042 if (mddev->pers)
3043 switch(mddev->ro) {
3044 case 1:
3045 st = readonly;
3046 break;
3047 case 2:
3048 st = read_auto;
3049 break;
3050 case 0:
3051 if (mddev->in_sync)
3052 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003053 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3054 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003055 else if (mddev->safemode)
3056 st = active_idle;
3057 else
3058 st = active;
3059 }
3060 else {
3061 if (list_empty(&mddev->disks) &&
3062 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003063 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003064 st = clear;
3065 else
3066 st = inactive;
3067 }
3068 return sprintf(page, "%s\n", array_states[st]);
3069}
3070
Neil Browndf5b20c2008-07-11 22:02:22 +10003071static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003072static int do_md_run(mddev_t * mddev);
3073static int restart_array(mddev_t *mddev);
3074
3075static ssize_t
3076array_state_store(mddev_t *mddev, const char *buf, size_t len)
3077{
3078 int err = -EINVAL;
3079 enum array_state st = match_word(buf, array_states);
3080 switch(st) {
3081 case bad_word:
3082 break;
3083 case clear:
3084 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003085 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003086 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003087 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003088 break;
3089 case inactive:
3090 /* stopping an active array */
3091 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003092 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003093 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003094 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003095 } else
3096 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003097 break;
3098 case suspended:
3099 break; /* not supported yet */
3100 case readonly:
3101 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003102 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003103 else {
3104 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003105 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003106 err = do_md_run(mddev);
3107 }
3108 break;
3109 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003110 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003111 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003112 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003113 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003114 err = restart_array(mddev);
3115 if (err == 0) {
3116 mddev->ro = 2;
3117 set_disk_ro(mddev->gendisk, 0);
3118 }
NeilBrown9e653b62006-06-26 00:27:58 -07003119 } else {
3120 mddev->ro = 2;
3121 err = do_md_run(mddev);
3122 }
3123 break;
3124 case clean:
3125 if (mddev->pers) {
3126 restart_array(mddev);
3127 spin_lock_irq(&mddev->write_lock);
3128 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003129 if (mddev->in_sync == 0) {
3130 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003131 if (mddev->safemode == 1)
3132 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003133 if (mddev->persistent)
3134 set_bit(MD_CHANGE_CLEAN,
3135 &mddev->flags);
3136 }
3137 err = 0;
3138 } else
3139 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003140 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003141 } else
3142 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003143 break;
3144 case active:
3145 if (mddev->pers) {
3146 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003147 if (mddev->external)
3148 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003149 wake_up(&mddev->sb_wait);
3150 err = 0;
3151 } else {
3152 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003153 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003154 err = do_md_run(mddev);
3155 }
3156 break;
3157 case write_pending:
3158 case active_idle:
3159 /* these cannot be set */
3160 break;
3161 }
3162 if (err)
3163 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003164 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003165 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003166 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003167 }
NeilBrown9e653b62006-06-26 00:27:58 -07003168}
NeilBrown80ca3a42006-07-10 04:44:18 -07003169static struct md_sysfs_entry md_array_state =
3170__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003171
NeilBrown6d7ff732006-01-06 00:21:16 -08003172static ssize_t
3173null_show(mddev_t *mddev, char *page)
3174{
3175 return -EINVAL;
3176}
3177
3178static ssize_t
3179new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3180{
3181 /* buf must be %d:%d\n? giving major and minor numbers */
3182 /* The new device is added to the array.
3183 * If the array has a persistent superblock, we read the
3184 * superblock to initialise info and check validity.
3185 * Otherwise, only checking done is that in bind_rdev_to_array,
3186 * which mainly checks size.
3187 */
3188 char *e;
3189 int major = simple_strtoul(buf, &e, 10);
3190 int minor;
3191 dev_t dev;
3192 mdk_rdev_t *rdev;
3193 int err;
3194
3195 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3196 return -EINVAL;
3197 minor = simple_strtoul(e+1, &e, 10);
3198 if (*e && *e != '\n')
3199 return -EINVAL;
3200 dev = MKDEV(major, minor);
3201 if (major != MAJOR(dev) ||
3202 minor != MINOR(dev))
3203 return -EOVERFLOW;
3204
3205
3206 if (mddev->persistent) {
3207 rdev = md_import_device(dev, mddev->major_version,
3208 mddev->minor_version);
3209 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3210 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3211 mdk_rdev_t, same_set);
3212 err = super_types[mddev->major_version]
3213 .load_super(rdev, rdev0, mddev->minor_version);
3214 if (err < 0)
3215 goto out;
3216 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003217 } else if (mddev->external)
3218 rdev = md_import_device(dev, -2, -1);
3219 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003220 rdev = md_import_device(dev, -1, -1);
3221
3222 if (IS_ERR(rdev))
3223 return PTR_ERR(rdev);
3224 err = bind_rdev_to_array(rdev, mddev);
3225 out:
3226 if (err)
3227 export_rdev(rdev);
3228 return err ? err : len;
3229}
3230
3231static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003232__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003233
3234static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003235bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3236{
3237 char *end;
3238 unsigned long chunk, end_chunk;
3239
3240 if (!mddev->bitmap)
3241 goto out;
3242 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3243 while (*buf) {
3244 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3245 if (buf == end) break;
3246 if (*end == '-') { /* range */
3247 buf = end + 1;
3248 end_chunk = simple_strtoul(buf, &end, 0);
3249 if (buf == end) break;
3250 }
3251 if (*end && !isspace(*end)) break;
3252 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3253 buf = end;
3254 while (isspace(*buf)) buf++;
3255 }
3256 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3257out:
3258 return len;
3259}
3260
3261static struct md_sysfs_entry md_bitmap =
3262__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3263
3264static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003265size_show(mddev_t *mddev, char *page)
3266{
Andre Noll58c0fed2009-03-31 14:33:13 +11003267 return sprintf(page, "%llu\n",
3268 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003269}
3270
Andre Nolld71f9f82008-07-11 22:02:22 +10003271static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003272
3273static ssize_t
3274size_store(mddev_t *mddev, const char *buf, size_t len)
3275{
3276 /* If array is inactive, we can reduce the component size, but
3277 * not increase it (except from 0).
3278 * If array is active, we can try an on-line resize
3279 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003280 sector_t sectors;
3281 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003282
Andre Noll58c0fed2009-03-31 14:33:13 +11003283 if (err < 0)
3284 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003285 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003286 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003287 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003288 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003289 if (mddev->dev_sectors == 0 ||
3290 mddev->dev_sectors > sectors)
3291 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003292 else
3293 err = -ENOSPC;
3294 }
3295 return err ? err : len;
3296}
3297
3298static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003299__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003300
NeilBrown8bb93aa2006-01-06 00:20:50 -08003301
3302/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003303 * This is one of
3304 * 'none' for arrays with no metadata (good luck...)
3305 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003306 * or N.M for internally known formats
3307 */
3308static ssize_t
3309metadata_show(mddev_t *mddev, char *page)
3310{
3311 if (mddev->persistent)
3312 return sprintf(page, "%d.%d\n",
3313 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003314 else if (mddev->external)
3315 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003316 else
3317 return sprintf(page, "none\n");
3318}
3319
3320static ssize_t
3321metadata_store(mddev_t *mddev, const char *buf, size_t len)
3322{
3323 int major, minor;
3324 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003325 /* Changing the details of 'external' metadata is
3326 * always permitted. Otherwise there must be
3327 * no devices attached to the array.
3328 */
3329 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3330 ;
3331 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003332 return -EBUSY;
3333
3334 if (cmd_match(buf, "none")) {
3335 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003336 mddev->external = 0;
3337 mddev->major_version = 0;
3338 mddev->minor_version = 90;
3339 return len;
3340 }
3341 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003342 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003343 if (namelen >= sizeof(mddev->metadata_type))
3344 namelen = sizeof(mddev->metadata_type)-1;
3345 strncpy(mddev->metadata_type, buf+9, namelen);
3346 mddev->metadata_type[namelen] = 0;
3347 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3348 mddev->metadata_type[--namelen] = 0;
3349 mddev->persistent = 0;
3350 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003351 mddev->major_version = 0;
3352 mddev->minor_version = 90;
3353 return len;
3354 }
3355 major = simple_strtoul(buf, &e, 10);
3356 if (e==buf || *e != '.')
3357 return -EINVAL;
3358 buf = e+1;
3359 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003360 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003361 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003362 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003363 return -ENOENT;
3364 mddev->major_version = major;
3365 mddev->minor_version = minor;
3366 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003367 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003368 return len;
3369}
3370
3371static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003372__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003373
NeilBrowna35b0d62006-01-06 00:20:49 -08003374static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003375action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003376{
NeilBrown7eec3142005-11-08 21:39:44 -08003377 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003378 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3379 type = "frozen";
3380 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003381 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003382 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3383 type = "reshape";
3384 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003385 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3386 type = "resync";
3387 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3388 type = "check";
3389 else
3390 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003391 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003392 type = "recover";
3393 }
3394 return sprintf(page, "%s\n", type);
3395}
3396
3397static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003398action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003399{
NeilBrown7eec3142005-11-08 21:39:44 -08003400 if (!mddev->pers || !mddev->pers->sync_request)
3401 return -EINVAL;
3402
NeilBrownb6a9ce62009-05-26 09:41:17 +10003403 if (cmd_match(page, "frozen"))
3404 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3405 else
3406 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3407
3408 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003409 if (mddev->sync_thread) {
3410 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3411 md_unregister_thread(mddev->sync_thread);
3412 mddev->sync_thread = NULL;
3413 mddev->recovery = 0;
3414 }
NeilBrown03c902e2006-01-06 00:20:46 -08003415 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3416 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003417 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003418 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003419 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003420 else if (cmd_match(page, "recover")) {
3421 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3422 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3423 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003424 int err;
3425 if (mddev->pers->start_reshape == NULL)
3426 return -EINVAL;
3427 err = mddev->pers->start_reshape(mddev);
3428 if (err)
3429 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003430 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003431 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003432 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003433 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003434 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003435 return -EINVAL;
3436 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3437 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003438 }
NeilBrown03c902e2006-01-06 00:20:46 -08003439 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003440 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003441 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003442 return len;
3443}
3444
NeilBrown9d888832005-11-08 21:39:26 -08003445static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003446mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003447{
3448 return sprintf(page, "%llu\n",
3449 (unsigned long long) mddev->resync_mismatches);
3450}
3451
NeilBrown80ca3a42006-07-10 04:44:18 -07003452static struct md_sysfs_entry md_scan_mode =
3453__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003454
NeilBrown96de1e62005-11-08 21:39:39 -08003455
NeilBrown80ca3a42006-07-10 04:44:18 -07003456static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003457
NeilBrown88202a02006-01-06 00:21:36 -08003458static ssize_t
3459sync_min_show(mddev_t *mddev, char *page)
3460{
3461 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3462 mddev->sync_speed_min ? "local": "system");
3463}
3464
3465static ssize_t
3466sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3467{
3468 int min;
3469 char *e;
3470 if (strncmp(buf, "system", 6)==0) {
3471 mddev->sync_speed_min = 0;
3472 return len;
3473 }
3474 min = simple_strtoul(buf, &e, 10);
3475 if (buf == e || (*e && *e != '\n') || min <= 0)
3476 return -EINVAL;
3477 mddev->sync_speed_min = min;
3478 return len;
3479}
3480
3481static struct md_sysfs_entry md_sync_min =
3482__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3483
3484static ssize_t
3485sync_max_show(mddev_t *mddev, char *page)
3486{
3487 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3488 mddev->sync_speed_max ? "local": "system");
3489}
3490
3491static ssize_t
3492sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3493{
3494 int max;
3495 char *e;
3496 if (strncmp(buf, "system", 6)==0) {
3497 mddev->sync_speed_max = 0;
3498 return len;
3499 }
3500 max = simple_strtoul(buf, &e, 10);
3501 if (buf == e || (*e && *e != '\n') || max <= 0)
3502 return -EINVAL;
3503 mddev->sync_speed_max = max;
3504 return len;
3505}
3506
3507static struct md_sysfs_entry md_sync_max =
3508__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3509
Iustin Popd7f3d292007-10-16 23:30:54 -07003510static ssize_t
3511degraded_show(mddev_t *mddev, char *page)
3512{
3513 return sprintf(page, "%d\n", mddev->degraded);
3514}
3515static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003516
3517static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003518sync_force_parallel_show(mddev_t *mddev, char *page)
3519{
3520 return sprintf(page, "%d\n", mddev->parallel_resync);
3521}
3522
3523static ssize_t
3524sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3525{
3526 long n;
3527
3528 if (strict_strtol(buf, 10, &n))
3529 return -EINVAL;
3530
3531 if (n != 0 && n != 1)
3532 return -EINVAL;
3533
3534 mddev->parallel_resync = n;
3535
3536 if (mddev->sync_thread)
3537 wake_up(&resync_wait);
3538
3539 return len;
3540}
3541
3542/* force parallel resync, even with shared block devices */
3543static struct md_sysfs_entry md_sync_force_parallel =
3544__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3545 sync_force_parallel_show, sync_force_parallel_store);
3546
3547static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003548sync_speed_show(mddev_t *mddev, char *page)
3549{
3550 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003551 if (mddev->curr_resync == 0)
3552 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003553 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3554 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003555 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003556 db = resync - mddev->resync_mark_cnt;
3557 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003558}
3559
NeilBrown80ca3a42006-07-10 04:44:18 -07003560static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003561
3562static ssize_t
3563sync_completed_show(mddev_t *mddev, char *page)
3564{
Andre Noll58c0fed2009-03-31 14:33:13 +11003565 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003566
NeilBrownacb180b2009-04-14 16:28:34 +10003567 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3568 return sprintf(page, "none\n");
3569
NeilBrown88202a02006-01-06 00:21:36 -08003570 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003571 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003572 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003573 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003574
NeilBrownacb180b2009-04-14 16:28:34 +10003575 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003576 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003577}
3578
NeilBrown80ca3a42006-07-10 04:44:18 -07003579static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003580
NeilBrowne464eaf2006-03-27 01:18:14 -08003581static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003582min_sync_show(mddev_t *mddev, char *page)
3583{
3584 return sprintf(page, "%llu\n",
3585 (unsigned long long)mddev->resync_min);
3586}
3587static ssize_t
3588min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3589{
3590 unsigned long long min;
3591 if (strict_strtoull(buf, 10, &min))
3592 return -EINVAL;
3593 if (min > mddev->resync_max)
3594 return -EINVAL;
3595 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3596 return -EBUSY;
3597
3598 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003599 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003600 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003601 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003602 return -EINVAL;
3603 }
3604 mddev->resync_min = min;
3605
3606 return len;
3607}
3608
3609static struct md_sysfs_entry md_min_sync =
3610__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3611
3612static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003613max_sync_show(mddev_t *mddev, char *page)
3614{
3615 if (mddev->resync_max == MaxSector)
3616 return sprintf(page, "max\n");
3617 else
3618 return sprintf(page, "%llu\n",
3619 (unsigned long long)mddev->resync_max);
3620}
3621static ssize_t
3622max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3623{
3624 if (strncmp(buf, "max", 3) == 0)
3625 mddev->resync_max = MaxSector;
3626 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003627 unsigned long long max;
3628 if (strict_strtoull(buf, 10, &max))
3629 return -EINVAL;
3630 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003631 return -EINVAL;
3632 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003633 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003634 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3635 return -EBUSY;
3636
3637 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003638 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003639 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003640 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003641 return -EINVAL;
3642 }
3643 mddev->resync_max = max;
3644 }
3645 wake_up(&mddev->recovery_wait);
3646 return len;
3647}
3648
3649static struct md_sysfs_entry md_max_sync =
3650__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3651
3652static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003653suspend_lo_show(mddev_t *mddev, char *page)
3654{
3655 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3656}
3657
3658static ssize_t
3659suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3660{
3661 char *e;
3662 unsigned long long new = simple_strtoull(buf, &e, 10);
3663
NeilBrownb8d966e2009-07-01 11:14:04 +10003664 if (mddev->pers == NULL ||
3665 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003666 return -EINVAL;
3667 if (buf == e || (*e && *e != '\n'))
3668 return -EINVAL;
3669 if (new >= mddev->suspend_hi ||
3670 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3671 mddev->suspend_lo = new;
3672 mddev->pers->quiesce(mddev, 2);
3673 return len;
3674 } else
3675 return -EINVAL;
3676}
3677static struct md_sysfs_entry md_suspend_lo =
3678__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3679
3680
3681static ssize_t
3682suspend_hi_show(mddev_t *mddev, char *page)
3683{
3684 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3685}
3686
3687static ssize_t
3688suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3689{
3690 char *e;
3691 unsigned long long new = simple_strtoull(buf, &e, 10);
3692
NeilBrownb8d966e2009-07-01 11:14:04 +10003693 if (mddev->pers == NULL ||
3694 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003695 return -EINVAL;
3696 if (buf == e || (*e && *e != '\n'))
3697 return -EINVAL;
3698 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3699 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3700 mddev->suspend_hi = new;
3701 mddev->pers->quiesce(mddev, 1);
3702 mddev->pers->quiesce(mddev, 0);
3703 return len;
3704 } else
3705 return -EINVAL;
3706}
3707static struct md_sysfs_entry md_suspend_hi =
3708__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3709
NeilBrown08a02ec2007-05-09 02:35:38 -07003710static ssize_t
3711reshape_position_show(mddev_t *mddev, char *page)
3712{
3713 if (mddev->reshape_position != MaxSector)
3714 return sprintf(page, "%llu\n",
3715 (unsigned long long)mddev->reshape_position);
3716 strcpy(page, "none\n");
3717 return 5;
3718}
3719
3720static ssize_t
3721reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3722{
3723 char *e;
3724 unsigned long long new = simple_strtoull(buf, &e, 10);
3725 if (mddev->pers)
3726 return -EBUSY;
3727 if (buf == e || (*e && *e != '\n'))
3728 return -EINVAL;
3729 mddev->reshape_position = new;
3730 mddev->delta_disks = 0;
3731 mddev->new_level = mddev->level;
3732 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003733 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003734 return len;
3735}
3736
3737static struct md_sysfs_entry md_reshape_position =
3738__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3739 reshape_position_store);
3740
Dan Williamsb522adc2009-03-31 15:00:31 +11003741static ssize_t
3742array_size_show(mddev_t *mddev, char *page)
3743{
3744 if (mddev->external_size)
3745 return sprintf(page, "%llu\n",
3746 (unsigned long long)mddev->array_sectors/2);
3747 else
3748 return sprintf(page, "default\n");
3749}
3750
3751static ssize_t
3752array_size_store(mddev_t *mddev, const char *buf, size_t len)
3753{
3754 sector_t sectors;
3755
3756 if (strncmp(buf, "default", 7) == 0) {
3757 if (mddev->pers)
3758 sectors = mddev->pers->size(mddev, 0, 0);
3759 else
3760 sectors = mddev->array_sectors;
3761
3762 mddev->external_size = 0;
3763 } else {
3764 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3765 return -EINVAL;
3766 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003767 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003768
3769 mddev->external_size = 1;
3770 }
3771
3772 mddev->array_sectors = sectors;
3773 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003774 if (mddev->pers)
3775 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003776
3777 return len;
3778}
3779
3780static struct md_sysfs_entry md_array_size =
3781__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3782 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003783
NeilBrowneae17012005-11-08 21:39:23 -08003784static struct attribute *md_default_attrs[] = {
3785 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003786 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003787 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003788 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003789 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003790 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003791 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003792 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003793 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003794 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003795 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003796 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003797 NULL,
3798};
3799
3800static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003801 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003802 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003803 &md_sync_min.attr,
3804 &md_sync_max.attr,
3805 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003806 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003807 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003808 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003809 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003810 &md_suspend_lo.attr,
3811 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003812 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003813 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003814 NULL,
3815};
NeilBrown411036f2005-11-08 21:39:40 -08003816static struct attribute_group md_redundancy_group = {
3817 .name = NULL,
3818 .attrs = md_redundancy_attrs,
3819};
3820
NeilBrowneae17012005-11-08 21:39:23 -08003821
3822static ssize_t
3823md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3824{
3825 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3826 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003827 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003828
3829 if (!entry->show)
3830 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003831 rv = mddev_lock(mddev);
3832 if (!rv) {
3833 rv = entry->show(mddev, page);
3834 mddev_unlock(mddev);
3835 }
NeilBrown96de1e62005-11-08 21:39:39 -08003836 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003837}
3838
3839static ssize_t
3840md_attr_store(struct kobject *kobj, struct attribute *attr,
3841 const char *page, size_t length)
3842{
3843 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3844 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003845 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003846
3847 if (!entry->store)
3848 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003849 if (!capable(CAP_SYS_ADMIN))
3850 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003851 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003852 if (mddev->hold_active == UNTIL_IOCTL)
3853 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003854 if (!rv) {
3855 rv = entry->store(mddev, page, length);
3856 mddev_unlock(mddev);
3857 }
NeilBrown96de1e62005-11-08 21:39:39 -08003858 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003859}
3860
3861static void md_free(struct kobject *ko)
3862{
3863 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003864
3865 if (mddev->sysfs_state)
3866 sysfs_put(mddev->sysfs_state);
3867
3868 if (mddev->gendisk) {
3869 del_gendisk(mddev->gendisk);
3870 put_disk(mddev->gendisk);
3871 }
3872 if (mddev->queue)
3873 blk_cleanup_queue(mddev->queue);
3874
NeilBrowneae17012005-11-08 21:39:23 -08003875 kfree(mddev);
3876}
3877
3878static struct sysfs_ops md_sysfs_ops = {
3879 .show = md_attr_show,
3880 .store = md_attr_store,
3881};
3882static struct kobj_type md_ktype = {
3883 .release = md_free,
3884 .sysfs_ops = &md_sysfs_ops,
3885 .default_attrs = md_default_attrs,
3886};
3887
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888int mdp_major = 0;
3889
Dan Williams5fd3a172009-03-04 00:57:25 -07003890static void mddev_delayed_delete(struct work_struct *ws)
3891{
3892 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3893
3894 if (mddev->private == &md_redundancy_group) {
3895 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3896 if (mddev->sysfs_action)
3897 sysfs_put(mddev->sysfs_action);
3898 mddev->sysfs_action = NULL;
3899 mddev->private = NULL;
3900 }
3901 kobject_del(&mddev->kobj);
3902 kobject_put(&mddev->kobj);
3903}
3904
NeilBrownefeb53c2009-01-09 08:31:10 +11003905static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003907 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 mddev_t *mddev = mddev_find(dev);
3909 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003910 int partitioned;
3911 int shift;
3912 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003913 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
3915 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003916 return -ENODEV;
3917
3918 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3919 shift = partitioned ? MdpMinorShift : 0;
3920 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
NeilBrownd3374822009-01-09 08:31:10 +11003922 /* wait for any previous instance if this device
3923 * to be completed removed (mddev_delayed_delete).
3924 */
3925 flush_scheduled_work();
3926
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003927 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10003928 error = -EEXIST;
3929 if (mddev->gendisk)
3930 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003931
3932 if (name) {
3933 /* Need to ensure that 'name' is not a duplicate.
3934 */
3935 mddev_t *mddev2;
3936 spin_lock(&all_mddevs_lock);
3937
3938 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3939 if (mddev2->gendisk &&
3940 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3941 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10003942 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003943 }
3944 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 }
NeilBrown8b765392009-01-09 08:31:08 +11003946
NeilBrown0909dc42009-07-01 12:27:21 +10003947 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003948 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10003949 if (!mddev->queue)
3950 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11003951 mddev->queue->queuedata = mddev;
3952
NeilBrown8b765392009-01-09 08:31:08 +11003953 /* Can be unlocked because the queue is new: no concurrency */
3954 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3955
NeilBrown409c57f2009-03-31 14:39:39 +11003956 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 disk = alloc_disk(1 << shift);
3959 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11003960 blk_cleanup_queue(mddev->queue);
3961 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10003962 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003964 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003966 if (name)
3967 strcpy(disk->disk_name, name);
3968 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003970 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 disk->fops = &md_fops;
3973 disk->private_data = mddev;
3974 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003975 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003976 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003977 * remove it now.
3978 */
3979 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 add_disk(disk);
3981 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003982 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3983 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10003984 if (error) {
3985 /* This isn't possible, but as kobject_init_and_add is marked
3986 * __must_check, we must do something with the result
3987 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08003988 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3989 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10003990 error = 0;
3991 }
3992 abort:
3993 mutex_unlock(&disks_mutex);
3994 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003995 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003996 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3997 }
NeilBrownd3374822009-01-09 08:31:10 +11003998 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10003999 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004000}
4001
4002static struct kobject *md_probe(dev_t dev, int *part, void *data)
4003{
4004 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 return NULL;
4006}
4007
NeilBrownefeb53c2009-01-09 08:31:10 +11004008static int add_named_array(const char *val, struct kernel_param *kp)
4009{
4010 /* val must be "md_*" where * is not all digits.
4011 * We allocate an array with a large free minor number, and
4012 * set the name to val. val must not already be an active name.
4013 */
4014 int len = strlen(val);
4015 char buf[DISK_NAME_LEN];
4016
4017 while (len && val[len-1] == '\n')
4018 len--;
4019 if (len >= DISK_NAME_LEN)
4020 return -E2BIG;
4021 strlcpy(buf, val, len+1);
4022 if (strncmp(buf, "md_", 3) != 0)
4023 return -EINVAL;
4024 return md_alloc(0, buf);
4025}
4026
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027static void md_safemode_timeout(unsigned long data)
4028{
4029 mddev_t *mddev = (mddev_t *) data;
4030
Neil Brown0fd62b82008-06-28 08:31:36 +10004031 if (!atomic_read(&mddev->writes_pending)) {
4032 mddev->safemode = 1;
4033 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004034 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 md_wakeup_thread(mddev->thread);
4037}
4038
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004039static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
4041static int do_md_run(mddev_t * mddev)
4042{
NeilBrown2604b702006-01-06 00:20:36 -08004043 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 mdk_rdev_t *rdev;
4045 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004046 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
NeilBrowna757e642005-04-16 15:26:42 -07004048 if (list_empty(&mddev->disks))
4049 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 if (mddev->pers)
4053 return -EBUSY;
4054
4055 /*
4056 * Analyze all RAID superblock(s)
4057 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004058 if (!mddev->raid_disks) {
4059 if (!mddev->persistent)
4060 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004061 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
NeilBrownd9d166c2006-01-06 00:20:51 -08004064 if (mddev->level != LEVEL_NONE)
4065 request_module("md-level-%d", mddev->level);
4066 else if (mddev->clevel[0])
4067 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
4069 /*
4070 * Drop all container device buffers, from now on
4071 * the only valid external interface is through the md
4072 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004074 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004075 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 continue;
4077 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004078 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004079
4080 /* perform some consistency tests on the device.
4081 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004082 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004083 */
Andre Noll0f420352008-07-11 22:02:23 +10004084 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004085 if (mddev->dev_sectors &&
4086 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004087 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004088 printk("md: %s: data overlaps metadata\n",
4089 mdname(mddev));
4090 return -EINVAL;
4091 }
4092 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004093 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004094 > rdev->data_offset) {
4095 printk("md: %s: metadata overlaps data\n",
4096 mdname(mddev));
4097 return -EINVAL;
4098 }
4099 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004100 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 }
4102
4103 md_probe(mddev->unit, NULL, NULL);
4104 disk = mddev->gendisk;
4105 if (!disk)
4106 return -ENOMEM;
4107
4108 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004109 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004110 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004112 if (mddev->level != LEVEL_NONE)
4113 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4114 mddev->level);
4115 else
4116 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4117 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return -EINVAL;
4119 }
NeilBrown2604b702006-01-06 00:20:36 -08004120 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004122 if (mddev->level != pers->level) {
4123 mddev->level = pers->level;
4124 mddev->new_level = pers->level;
4125 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004126 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
NeilBrownf6705572006-03-27 01:18:11 -08004128 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004129 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004130 /* This personality cannot handle reshaping... */
4131 mddev->pers = NULL;
4132 module_put(pers->owner);
4133 return -EINVAL;
4134 }
4135
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004136 if (pers->sync_request) {
4137 /* Warn if this is a potentially silly
4138 * configuration.
4139 */
4140 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4141 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004142 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004143
4144 list_for_each_entry(rdev, &mddev->disks, same_set)
4145 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004146 if (rdev < rdev2 &&
4147 rdev->bdev->bd_contains ==
4148 rdev2->bdev->bd_contains) {
4149 printk(KERN_WARNING
4150 "%s: WARNING: %s appears to be"
4151 " on the same physical disk as"
4152 " %s.\n",
4153 mdname(mddev),
4154 bdevname(rdev->bdev,b),
4155 bdevname(rdev2->bdev,b2));
4156 warned = 1;
4157 }
4158 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004159
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004160 if (warned)
4161 printk(KERN_WARNING
4162 "True protection against single-disk"
4163 " failure might be compromised.\n");
4164 }
4165
NeilBrown657390d2005-08-26 18:34:16 -07004166 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004167 /* may be over-ridden by personality */
4168 mddev->resync_max_sectors = mddev->dev_sectors;
4169
NeilBrowna9701a32005-11-08 21:39:34 -08004170 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004171 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
NeilBrownf91de922005-11-08 21:39:36 -08004173 if (start_readonly)
4174 mddev->ro = 2; /* read-only, but switch on first write */
4175
NeilBrownb15c2e52006-01-06 00:20:16 -08004176 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004177 if (err)
4178 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004179 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4180 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4181 " but 'external_size' not in effect?\n", __func__);
4182 printk(KERN_ERR
4183 "md: invalid array_size %llu > default size %llu\n",
4184 (unsigned long long)mddev->array_sectors / 2,
4185 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4186 err = -EINVAL;
4187 mddev->pers->stop(mddev);
4188 }
4189 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004190 err = bitmap_create(mddev);
4191 if (err) {
4192 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4193 mdname(mddev), err);
4194 mddev->pers->stop(mddev);
4195 }
4196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 module_put(mddev->pers->owner);
4199 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004200 bitmap_destroy(mddev);
4201 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004203 if (mddev->pers->sync_request) {
4204 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4205 printk(KERN_WARNING
4206 "md: cannot register extra attributes for %s\n",
4207 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004208 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004209 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004210 mddev->ro = 0;
4211
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 atomic_set(&mddev->writes_pending,0);
4213 mddev->safemode = 0;
4214 mddev->safemode_timer.function = md_safemode_timeout;
4215 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004216 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004218
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004219 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004220 if (rdev->raid_disk >= 0) {
4221 char nm[20];
4222 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004223 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4224 printk("md: cannot register %s for %s\n",
4225 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
4228 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4229
NeilBrown850b2b42006-10-03 01:15:46 -07004230 if (mddev->flags)
4231 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Andre Nollf233ea52008-07-21 17:05:22 +10004233 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004235 /* If there is a partially-recovered drive we need to
4236 * start recovery here. If we leave it to md_check_recovery,
4237 * it will remove the drives and not do the right thing
4238 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004239 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004240 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004241 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004242 if (rdev->raid_disk >= 0 &&
4243 !test_bit(In_sync, &rdev->flags) &&
4244 !test_bit(Faulty, &rdev->flags))
4245 /* complete an interrupted recovery */
4246 spares++;
4247 if (spares && mddev->pers->sync_request) {
4248 mddev->recovery = 0;
4249 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4250 mddev->sync_thread = md_register_thread(md_do_sync,
4251 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004252 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004253 if (!mddev->sync_thread) {
4254 printk(KERN_ERR "%s: could not start resync"
4255 " thread...\n",
4256 mdname(mddev));
4257 /* leave the spares where they are, it shouldn't hurt */
4258 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004259 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004260 }
4261 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004262 md_wakeup_thread(mddev->thread);
4263 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004264
NeilBrown449aad32009-08-03 10:59:58 +10004265 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004266 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004267 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004268 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004269 if (mddev->sysfs_action)
4270 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004271 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004272 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 return 0;
4274}
4275
4276static int restart_array(mddev_t *mddev)
4277{
4278 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
Andre Noll80fab1d2008-07-11 22:02:21 +10004280 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004282 return -ENXIO;
4283 if (!mddev->pers)
4284 return -EINVAL;
4285 if (!mddev->ro)
4286 return -EBUSY;
4287 mddev->safemode = 0;
4288 mddev->ro = 0;
4289 set_disk_ro(disk, 0);
4290 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4291 mdname(mddev));
4292 /* Kick recovery or resync if necessary */
4293 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4294 md_wakeup_thread(mddev->thread);
4295 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004296 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
NeilBrownacc55e22006-06-26 00:27:47 -07004300/* similar to deny_write_access, but accounts for our holding a reference
4301 * to the file ourselves */
4302static int deny_bitmap_write_access(struct file * file)
4303{
4304 struct inode *inode = file->f_mapping->host;
4305
4306 spin_lock(&inode->i_lock);
4307 if (atomic_read(&inode->i_writecount) > 1) {
4308 spin_unlock(&inode->i_lock);
4309 return -ETXTBSY;
4310 }
4311 atomic_set(&inode->i_writecount, -1);
4312 spin_unlock(&inode->i_lock);
4313
4314 return 0;
4315}
4316
4317static void restore_bitmap_write_access(struct file *file)
4318{
4319 struct inode *inode = file->f_mapping->host;
4320
4321 spin_lock(&inode->i_lock);
4322 atomic_set(&inode->i_writecount, 1);
4323 spin_unlock(&inode->i_lock);
4324}
4325
NeilBrown9e653b62006-06-26 00:27:58 -07004326/* mode:
4327 * 0 - completely stop and dis-assemble array
4328 * 1 - switch to readonly
4329 * 2 - stop but do not disassemble array
4330 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004331static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332{
4333 int err = 0;
4334 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004335 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336
NeilBrownc8c00a62009-08-10 12:50:52 +10004337 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004338 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004339 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004340 err = -EBUSY;
4341 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
4343 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004344 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4346 md_unregister_thread(mddev->sync_thread);
4347 mddev->sync_thread = NULL;
4348 }
4349
4350 del_timer_sync(&mddev->safemode_timer);
4351
NeilBrown9e653b62006-06-26 00:27:58 -07004352 switch(mode) {
4353 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004355 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 goto out;
4357 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004358 break;
4359 case 0: /* disassemble */
4360 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004361 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004362 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 if (mddev->ro)
4364 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004367 mddev->queue->merge_bvec_fn = NULL;
4368 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004369 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004371 if (mddev->pers->sync_request)
4372 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004374 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004375 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004376
NeilBrownc4647292009-05-07 12:51:06 +10004377 list_for_each_entry(rdev, &mddev->disks, same_set)
4378 if (rdev->raid_disk >= 0) {
4379 char nm[20];
4380 sprintf(nm, "rd%d", rdev->raid_disk);
4381 sysfs_remove_link(&mddev->kobj, nm);
4382 }
4383
NeilBrown0d4ca602006-12-10 02:20:44 -08004384 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004385 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004386
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 if (mddev->ro)
4388 mddev->ro = 0;
4389 }
NeilBrown850b2b42006-10-03 01:15:46 -07004390 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 /* mark array as shutdown cleanly */
4392 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004393 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 }
NeilBrown9e653b62006-06-26 00:27:58 -07004395 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004397 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004398 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004400out:
4401 mutex_unlock(&mddev->open_mutex);
4402 if (err)
4403 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 /*
4405 * Free resources if final stop
4406 */
NeilBrown9e653b62006-06-26 00:27:58 -07004407 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004408
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4410
NeilBrown978f9462006-02-02 14:28:05 -08004411 bitmap_destroy(mddev);
4412 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004413 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004414 fput(mddev->bitmap_file);
4415 mddev->bitmap_file = NULL;
4416 }
4417 mddev->bitmap_offset = 0;
4418
NeilBrown177a99b2008-02-06 01:39:56 -08004419 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004420 flush_scheduled_work();
4421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 export_array(mddev);
4423
Andre Nollf233ea52008-07-21 17:05:22 +10004424 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004425 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004426 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004427 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004428 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004429 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004430 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004431 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004432 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004433 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004434 mddev->level = LEVEL_NONE;
4435 mddev->clevel[0] = 0;
4436 mddev->flags = 0;
4437 mddev->ro = 0;
4438 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004439 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004440 mddev->ctime = mddev->utime = 0;
4441 mddev->layout = 0;
4442 mddev->max_disks = 0;
4443 mddev->events = 0;
4444 mddev->delta_disks = 0;
4445 mddev->new_level = LEVEL_NONE;
4446 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004447 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004448 mddev->curr_resync = 0;
4449 mddev->resync_mismatches = 0;
4450 mddev->suspend_lo = mddev->suspend_hi = 0;
4451 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4452 mddev->recovery = 0;
4453 mddev->in_sync = 0;
4454 mddev->changed = 0;
4455 mddev->degraded = 0;
4456 mddev->barriers_work = 0;
4457 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004458 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004459 if (mddev->hold_active == UNTIL_STOP)
4460 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004461
NeilBrowna8a55c32006-06-26 00:27:39 -07004462 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4464 mdname(mddev));
4465 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004466 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004467 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004468 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return err;
4470}
4471
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004472#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473static void autorun_array(mddev_t *mddev)
4474{
4475 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 int err;
4477
NeilBrowna757e642005-04-16 15:26:42 -07004478 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
4481 printk(KERN_INFO "md: running: ");
4482
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004483 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 char b[BDEVNAME_SIZE];
4485 printk("<%s>", bdevname(rdev->bdev,b));
4486 }
4487 printk("\n");
4488
NeilBrownd710e132008-10-13 11:55:12 +11004489 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 if (err) {
4491 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004492 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 }
4494}
4495
4496/*
4497 * lets try to run arrays based on all disks that have arrived
4498 * until now. (those are in pending_raid_disks)
4499 *
4500 * the method: pick the first pending disk, collect all disks with
4501 * the same UUID, remove all from the pending list and put them into
4502 * the 'same_array' list. Then order this list based on superblock
4503 * update time (freshest comes first), kick out 'old' disks and
4504 * compare superblocks. If everything's fine then run it.
4505 *
4506 * If "unit" is allocated, then bump its reference count
4507 */
4508static void autorun_devices(int part)
4509{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004510 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 mddev_t *mddev;
4512 char b[BDEVNAME_SIZE];
4513
4514 printk(KERN_INFO "md: autorun ...\n");
4515 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004516 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004518 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 rdev0 = list_entry(pending_raid_disks.next,
4520 mdk_rdev_t, same_set);
4521
4522 printk(KERN_INFO "md: considering %s ...\n",
4523 bdevname(rdev0->bdev,b));
4524 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004525 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 if (super_90_load(rdev, rdev0, 0) >= 0) {
4527 printk(KERN_INFO "md: adding %s ...\n",
4528 bdevname(rdev->bdev,b));
4529 list_move(&rdev->same_set, &candidates);
4530 }
4531 /*
4532 * now we have a set of devices, with all of them having
4533 * mostly sane superblocks. It's time to allocate the
4534 * mddev.
4535 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004536 if (part) {
4537 dev = MKDEV(mdp_major,
4538 rdev0->preferred_minor << MdpMinorShift);
4539 unit = MINOR(dev) >> MdpMinorShift;
4540 } else {
4541 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4542 unit = MINOR(dev);
4543 }
4544 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4546 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4547 break;
4548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
4550 md_probe(dev, NULL, NULL);
4551 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004552 if (!mddev || !mddev->gendisk) {
4553 if (mddev)
4554 mddev_put(mddev);
4555 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 "md: cannot allocate memory for md drive.\n");
4557 break;
4558 }
4559 if (mddev_lock(mddev))
4560 printk(KERN_WARNING "md: %s locked, cannot run\n",
4561 mdname(mddev));
4562 else if (mddev->raid_disks || mddev->major_version
4563 || !list_empty(&mddev->disks)) {
4564 printk(KERN_WARNING
4565 "md: %s already running, cannot run %s\n",
4566 mdname(mddev), bdevname(rdev0->bdev,b));
4567 mddev_unlock(mddev);
4568 } else {
4569 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004570 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004571 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 list_del_init(&rdev->same_set);
4573 if (bind_rdev_to_array(rdev, mddev))
4574 export_rdev(rdev);
4575 }
4576 autorun_array(mddev);
4577 mddev_unlock(mddev);
4578 }
4579 /* on success, candidates will be empty, on error
4580 * it won't...
4581 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004582 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004583 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 mddev_put(mddev);
4587 }
4588 printk(KERN_INFO "md: ... autorun DONE.\n");
4589}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004590#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592static int get_version(void __user * arg)
4593{
4594 mdu_version_t ver;
4595
4596 ver.major = MD_MAJOR_VERSION;
4597 ver.minor = MD_MINOR_VERSION;
4598 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4599
4600 if (copy_to_user(arg, &ver, sizeof(ver)))
4601 return -EFAULT;
4602
4603 return 0;
4604}
4605
4606static int get_array_info(mddev_t * mddev, void __user * arg)
4607{
4608 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004609 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
NeilBrowna9f326e2009-09-23 18:06:41 +10004612 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004613 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004615 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 failed++;
4617 else {
4618 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004619 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004620 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 else
4622 spare++;
4623 }
4624 }
4625
4626 info.major_version = mddev->major_version;
4627 info.minor_version = mddev->minor_version;
4628 info.patch_version = MD_PATCHLEVEL_VERSION;
4629 info.ctime = mddev->ctime;
4630 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004631 info.size = mddev->dev_sectors / 2;
4632 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004633 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 info.nr_disks = nr;
4635 info.raid_disks = mddev->raid_disks;
4636 info.md_minor = mddev->md_minor;
4637 info.not_persistent= !mddev->persistent;
4638
4639 info.utime = mddev->utime;
4640 info.state = 0;
4641 if (mddev->in_sync)
4642 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004643 if (mddev->bitmap && mddev->bitmap_offset)
4644 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004645 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 info.working_disks = working;
4647 info.failed_disks = failed;
4648 info.spare_disks = spare;
4649
4650 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004651 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652
4653 if (copy_to_user(arg, &info, sizeof(info)))
4654 return -EFAULT;
4655
4656 return 0;
4657}
4658
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004659static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004660{
4661 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4662 char *ptr, *buf = NULL;
4663 int err = -ENOMEM;
4664
Dan Williamsb5470dc2008-06-27 21:44:04 -07004665 if (md_allow_write(mddev))
4666 file = kmalloc(sizeof(*file), GFP_NOIO);
4667 else
4668 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004669
NeilBrown32a76272005-06-21 17:17:14 -07004670 if (!file)
4671 goto out;
4672
4673 /* bitmap disabled, zero the first byte and copy out */
4674 if (!mddev->bitmap || !mddev->bitmap->file) {
4675 file->pathname[0] = '\0';
4676 goto copy_out;
4677 }
4678
4679 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4680 if (!buf)
4681 goto out;
4682
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004683 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4684 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004685 goto out;
4686
4687 strcpy(file->pathname, ptr);
4688
4689copy_out:
4690 err = 0;
4691 if (copy_to_user(arg, file, sizeof(*file)))
4692 err = -EFAULT;
4693out:
4694 kfree(buf);
4695 kfree(file);
4696 return err;
4697}
4698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699static int get_disk_info(mddev_t * mddev, void __user * arg)
4700{
4701 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 mdk_rdev_t *rdev;
4703
4704 if (copy_from_user(&info, arg, sizeof(info)))
4705 return -EFAULT;
4706
Andre Noll26ef3792008-07-11 22:02:21 +10004707 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 if (rdev) {
4709 info.major = MAJOR(rdev->bdev->bd_dev);
4710 info.minor = MINOR(rdev->bdev->bd_dev);
4711 info.raid_disk = rdev->raid_disk;
4712 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004713 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004715 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 info.state |= (1<<MD_DISK_ACTIVE);
4717 info.state |= (1<<MD_DISK_SYNC);
4718 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004719 if (test_bit(WriteMostly, &rdev->flags))
4720 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 } else {
4722 info.major = info.minor = 0;
4723 info.raid_disk = -1;
4724 info.state = (1<<MD_DISK_REMOVED);
4725 }
4726
4727 if (copy_to_user(arg, &info, sizeof(info)))
4728 return -EFAULT;
4729
4730 return 0;
4731}
4732
4733static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4734{
4735 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4736 mdk_rdev_t *rdev;
4737 dev_t dev = MKDEV(info->major,info->minor);
4738
4739 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4740 return -EOVERFLOW;
4741
4742 if (!mddev->raid_disks) {
4743 int err;
4744 /* expecting a device which has a superblock */
4745 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4746 if (IS_ERR(rdev)) {
4747 printk(KERN_WARNING
4748 "md: md_import_device returned %ld\n",
4749 PTR_ERR(rdev));
4750 return PTR_ERR(rdev);
4751 }
4752 if (!list_empty(&mddev->disks)) {
4753 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4754 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004755 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 .load_super(rdev, rdev0, mddev->minor_version);
4757 if (err < 0) {
4758 printk(KERN_WARNING
4759 "md: %s has different UUID to %s\n",
4760 bdevname(rdev->bdev,b),
4761 bdevname(rdev0->bdev,b2));
4762 export_rdev(rdev);
4763 return -EINVAL;
4764 }
4765 }
4766 err = bind_rdev_to_array(rdev, mddev);
4767 if (err)
4768 export_rdev(rdev);
4769 return err;
4770 }
4771
4772 /*
4773 * add_new_disk can be used once the array is assembled
4774 * to add "hot spares". They must already have a superblock
4775 * written
4776 */
4777 if (mddev->pers) {
4778 int err;
4779 if (!mddev->pers->hot_add_disk) {
4780 printk(KERN_WARNING
4781 "%s: personality does not support diskops!\n",
4782 mdname(mddev));
4783 return -EINVAL;
4784 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004785 if (mddev->persistent)
4786 rdev = md_import_device(dev, mddev->major_version,
4787 mddev->minor_version);
4788 else
4789 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790 if (IS_ERR(rdev)) {
4791 printk(KERN_WARNING
4792 "md: md_import_device returned %ld\n",
4793 PTR_ERR(rdev));
4794 return PTR_ERR(rdev);
4795 }
NeilBrown41158c72005-06-21 17:17:25 -07004796 /* set save_raid_disk if appropriate */
4797 if (!mddev->persistent) {
4798 if (info->state & (1<<MD_DISK_SYNC) &&
4799 info->raid_disk < mddev->raid_disks)
4800 rdev->raid_disk = info->raid_disk;
4801 else
4802 rdev->raid_disk = -1;
4803 } else
4804 super_types[mddev->major_version].
4805 validate_super(mddev, rdev);
4806 rdev->saved_raid_disk = rdev->raid_disk;
4807
NeilBrownb2d444d2005-11-08 21:39:31 -08004808 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004809 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4810 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004811 else
4812 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 rdev->raid_disk = -1;
4815 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004816 if (!err && !mddev->pers->hot_remove_disk) {
4817 /* If there is hot_add_disk but no hot_remove_disk
4818 * then added disks for geometry changes,
4819 * and should be added immediately.
4820 */
4821 super_types[mddev->major_version].
4822 validate_super(mddev, rdev);
4823 err = mddev->pers->hot_add_disk(mddev, rdev);
4824 if (err)
4825 unbind_rdev_from_array(rdev);
4826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 if (err)
4828 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004829 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004830 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004831
NeilBrown17571282006-12-10 02:20:52 -08004832 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004833 if (mddev->degraded)
4834 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004835 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004836 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 return err;
4838 }
4839
4840 /* otherwise, add_new_disk is only allowed
4841 * for major_version==0 superblocks
4842 */
4843 if (mddev->major_version != 0) {
4844 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4845 mdname(mddev));
4846 return -EINVAL;
4847 }
4848
4849 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4850 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004851 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 if (IS_ERR(rdev)) {
4853 printk(KERN_WARNING
4854 "md: error, md_import_device() returned %ld\n",
4855 PTR_ERR(rdev));
4856 return PTR_ERR(rdev);
4857 }
4858 rdev->desc_nr = info->number;
4859 if (info->raid_disk < mddev->raid_disks)
4860 rdev->raid_disk = info->raid_disk;
4861 else
4862 rdev->raid_disk = -1;
4863
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004865 if (info->state & (1<<MD_DISK_SYNC))
4866 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004868 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4869 set_bit(WriteMostly, &rdev->flags);
4870
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 if (!mddev->persistent) {
4872 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004873 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 } else
Andre Noll0f420352008-07-11 22:02:23 +10004875 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004876 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877
NeilBrown2bf071b2006-01-06 00:20:55 -08004878 err = bind_rdev_to_array(rdev, mddev);
4879 if (err) {
4880 export_rdev(rdev);
4881 return err;
4882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 }
4884
4885 return 0;
4886}
4887
4888static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4889{
4890 char b[BDEVNAME_SIZE];
4891 mdk_rdev_t *rdev;
4892
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 rdev = find_rdev(mddev, dev);
4894 if (!rdev)
4895 return -ENXIO;
4896
4897 if (rdev->raid_disk >= 0)
4898 goto busy;
4899
4900 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004901 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004902 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
4904 return 0;
4905busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004906 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 bdevname(rdev->bdev,b), mdname(mddev));
4908 return -EBUSY;
4909}
4910
4911static int hot_add_disk(mddev_t * mddev, dev_t dev)
4912{
4913 char b[BDEVNAME_SIZE];
4914 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 mdk_rdev_t *rdev;
4916
4917 if (!mddev->pers)
4918 return -ENODEV;
4919
4920 if (mddev->major_version != 0) {
4921 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4922 " version-0 superblocks.\n",
4923 mdname(mddev));
4924 return -EINVAL;
4925 }
4926 if (!mddev->pers->hot_add_disk) {
4927 printk(KERN_WARNING
4928 "%s: personality does not support diskops!\n",
4929 mdname(mddev));
4930 return -EINVAL;
4931 }
4932
NeilBrownd710e132008-10-13 11:55:12 +11004933 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 if (IS_ERR(rdev)) {
4935 printk(KERN_WARNING
4936 "md: error, md_import_device() returned %ld\n",
4937 PTR_ERR(rdev));
4938 return -EINVAL;
4939 }
4940
4941 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004942 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 else
Andre Noll0f420352008-07-11 22:02:23 +10004944 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945
NeilBrown8190e752009-06-18 08:48:58 +10004946 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
NeilBrownb2d444d2005-11-08 21:39:31 -08004948 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 printk(KERN_WARNING
4950 "md: can not hot-add faulty %s disk to %s!\n",
4951 bdevname(rdev->bdev,b), mdname(mddev));
4952 err = -EINVAL;
4953 goto abort_export;
4954 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004955 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004957 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004958 err = bind_rdev_to_array(rdev, mddev);
4959 if (err)
4960 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
4962 /*
4963 * The rest should better be atomic, we can have disk failures
4964 * noticed in interrupt contexts ...
4965 */
4966
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 rdev->raid_disk = -1;
4968
NeilBrown850b2b42006-10-03 01:15:46 -07004969 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970
4971 /*
4972 * Kick recovery, maybe this spare has to be added to the
4973 * array immediately.
4974 */
4975 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4976 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004977 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 return 0;
4979
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980abort_export:
4981 export_rdev(rdev);
4982 return err;
4983}
4984
NeilBrown32a76272005-06-21 17:17:14 -07004985static int set_bitmap_file(mddev_t *mddev, int fd)
4986{
4987 int err;
4988
NeilBrown36fa3062005-09-09 16:23:45 -07004989 if (mddev->pers) {
4990 if (!mddev->pers->quiesce)
4991 return -EBUSY;
4992 if (mddev->recovery || mddev->sync_thread)
4993 return -EBUSY;
4994 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004995 }
4996
NeilBrown36fa3062005-09-09 16:23:45 -07004997
4998 if (fd >= 0) {
4999 if (mddev->bitmap)
5000 return -EEXIST; /* cannot add when bitmap is present */
5001 mddev->bitmap_file = fget(fd);
5002
5003 if (mddev->bitmap_file == NULL) {
5004 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5005 mdname(mddev));
5006 return -EBADF;
5007 }
5008
5009 err = deny_bitmap_write_access(mddev->bitmap_file);
5010 if (err) {
5011 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5012 mdname(mddev));
5013 fput(mddev->bitmap_file);
5014 mddev->bitmap_file = NULL;
5015 return err;
5016 }
NeilBrowna654b9d82005-06-21 17:17:27 -07005017 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005018 } else if (mddev->bitmap == NULL)
5019 return -ENOENT; /* cannot remove what isn't there */
5020 err = 0;
5021 if (mddev->pers) {
5022 mddev->pers->quiesce(mddev, 1);
5023 if (fd >= 0)
5024 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005025 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005026 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005027 fd = -1; /* make sure to put the file */
5028 }
NeilBrown36fa3062005-09-09 16:23:45 -07005029 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005030 }
5031 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07005032 if (mddev->bitmap_file) {
5033 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07005034 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07005035 }
NeilBrown36fa3062005-09-09 16:23:45 -07005036 mddev->bitmap_file = NULL;
5037 }
5038
NeilBrown32a76272005-06-21 17:17:14 -07005039 return err;
5040}
5041
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042/*
5043 * set_array_info is used two different ways
5044 * The original usage is when creating a new array.
5045 * In this usage, raid_disks is > 0 and it together with
5046 * level, size, not_persistent,layout,chunksize determine the
5047 * shape of the array.
5048 * This will always create an array with a type-0.90.0 superblock.
5049 * The newer usage is when assembling an array.
5050 * In this case raid_disks will be 0, and the major_version field is
5051 * use to determine which style super-blocks are to be found on the devices.
5052 * The minor and patch _version numbers are also kept incase the
5053 * super_block handler wishes to interpret them.
5054 */
5055static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5056{
5057
5058 if (info->raid_disks == 0) {
5059 /* just setting version number for superblock loading */
5060 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005061 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 super_types[info->major_version].name == NULL) {
5063 /* maybe try to auto-load a module? */
5064 printk(KERN_INFO
5065 "md: superblock version %d not known\n",
5066 info->major_version);
5067 return -EINVAL;
5068 }
5069 mddev->major_version = info->major_version;
5070 mddev->minor_version = info->minor_version;
5071 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005072 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 return 0;
5074 }
5075 mddev->major_version = MD_MAJOR_VERSION;
5076 mddev->minor_version = MD_MINOR_VERSION;
5077 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5078 mddev->ctime = get_seconds();
5079
5080 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005081 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005082 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 mddev->raid_disks = info->raid_disks;
5084 /* don't set md_minor, it is determined by which /dev/md* was
5085 * openned
5086 */
5087 if (info->state & (1<<MD_SB_CLEAN))
5088 mddev->recovery_cp = MaxSector;
5089 else
5090 mddev->recovery_cp = 0;
5091 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005092 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
5094 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005095 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
5097 mddev->max_disks = MD_SB_DISKS;
5098
NeilBrowne6910632008-02-06 01:39:51 -08005099 if (mddev->persistent)
5100 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005101 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
NeilBrownb2a27032005-11-28 13:44:12 -08005103 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5104 mddev->bitmap_offset = 0;
5105
NeilBrownf6705572006-03-27 01:18:11 -08005106 mddev->reshape_position = MaxSector;
5107
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 /*
5109 * Generate a 128 bit UUID
5110 */
5111 get_random_bytes(mddev->uuid, 16);
5112
NeilBrownf6705572006-03-27 01:18:11 -08005113 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005114 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005115 mddev->new_layout = mddev->layout;
5116 mddev->delta_disks = 0;
5117
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 return 0;
5119}
5120
Dan Williams1f403622009-03-31 14:59:03 +11005121void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5122{
Dan Williamsb522adc2009-03-31 15:00:31 +11005123 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5124
5125 if (mddev->external_size)
5126 return;
5127
Dan Williams1f403622009-03-31 14:59:03 +11005128 mddev->array_sectors = array_sectors;
5129}
5130EXPORT_SYMBOL(md_set_array_sectors);
5131
Andre Nolld71f9f82008-07-11 22:02:22 +10005132static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005133{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005134 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005135 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005136 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005137
5138 if (mddev->pers->resize == NULL)
5139 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005140 /* The "num_sectors" is the number of sectors of each device that
5141 * is used. This can only make sense for arrays with redundancy.
5142 * linear and raid0 always use whatever space is available. We can only
5143 * consider changing this number if no resync or reconstruction is
5144 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005145 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005146 * of each device. If num_sectors is zero, we find the largest size
5147 * that fits.
5148
NeilBrowna35b0d62006-01-06 00:20:49 -08005149 */
5150 if (mddev->sync_thread)
5151 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005152 if (mddev->bitmap)
5153 /* Sorry, cannot grow a bitmap yet, just remove it,
5154 * grow, and re-add.
5155 */
5156 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005157 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005158 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005159
Andre Nolld71f9f82008-07-11 22:02:22 +10005160 if (fit && (num_sectors == 0 || num_sectors > avail))
5161 num_sectors = avail;
5162 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005163 return -ENOSPC;
5164 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005165 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005166 if (!rv)
5167 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005168 return rv;
5169}
5170
NeilBrownda943b92006-01-06 00:20:54 -08005171static int update_raid_disks(mddev_t *mddev, int raid_disks)
5172{
5173 int rv;
5174 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005175 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005176 return -EINVAL;
5177 if (raid_disks <= 0 ||
5178 raid_disks >= mddev->max_disks)
5179 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005180 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005181 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005182 mddev->delta_disks = raid_disks - mddev->raid_disks;
5183
5184 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08005185 return rv;
5186}
5187
5188
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189/*
5190 * update_array_info is used to change the configuration of an
5191 * on-line array.
5192 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5193 * fields in the info are checked against the array.
5194 * Any differences that cannot be handled will cause an error.
5195 * Normally, only one change can be managed at a time.
5196 */
5197static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5198{
5199 int rv = 0;
5200 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005201 int state = 0;
5202
5203 /* calculate expected state,ignoring low bits */
5204 if (mddev->bitmap && mddev->bitmap_offset)
5205 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206
5207 if (mddev->major_version != info->major_version ||
5208 mddev->minor_version != info->minor_version ||
5209/* mddev->patch_version != info->patch_version || */
5210 mddev->ctime != info->ctime ||
5211 mddev->level != info->level ||
5212/* mddev->layout != info->layout || */
5213 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005214 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005215 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5216 ((state^info->state) & 0xfffffe00)
5217 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 return -EINVAL;
5219 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005220 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5221 cnt++;
5222 if (mddev->raid_disks != info->raid_disks)
5223 cnt++;
5224 if (mddev->layout != info->layout)
5225 cnt++;
5226 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5227 cnt++;
5228 if (cnt == 0)
5229 return 0;
5230 if (cnt > 1)
5231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232
5233 if (mddev->layout != info->layout) {
5234 /* Change layout
5235 * we don't need to do anything at the md level, the
5236 * personality will take care of it all.
5237 */
NeilBrown50ac1682009-06-18 08:47:55 +10005238 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005240 else {
5241 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005242 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005243 if (rv)
5244 mddev->new_layout = mddev->layout;
5245 return rv;
5246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005248 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005249 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
NeilBrownda943b92006-01-06 00:20:54 -08005251 if (mddev->raid_disks != info->raid_disks)
5252 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253
NeilBrown36fa3062005-09-09 16:23:45 -07005254 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5255 if (mddev->pers->quiesce == NULL)
5256 return -EINVAL;
5257 if (mddev->recovery || mddev->sync_thread)
5258 return -EBUSY;
5259 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5260 /* add the bitmap */
5261 if (mddev->bitmap)
5262 return -EEXIST;
5263 if (mddev->default_bitmap_offset == 0)
5264 return -EINVAL;
5265 mddev->bitmap_offset = mddev->default_bitmap_offset;
5266 mddev->pers->quiesce(mddev, 1);
5267 rv = bitmap_create(mddev);
5268 if (rv)
5269 bitmap_destroy(mddev);
5270 mddev->pers->quiesce(mddev, 0);
5271 } else {
5272 /* remove the bitmap */
5273 if (!mddev->bitmap)
5274 return -ENOENT;
5275 if (mddev->bitmap->file)
5276 return -EINVAL;
5277 mddev->pers->quiesce(mddev, 1);
5278 bitmap_destroy(mddev);
5279 mddev->pers->quiesce(mddev, 0);
5280 mddev->bitmap_offset = 0;
5281 }
5282 }
NeilBrown850b2b42006-10-03 01:15:46 -07005283 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284 return rv;
5285}
5286
5287static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5288{
5289 mdk_rdev_t *rdev;
5290
5291 if (mddev->pers == NULL)
5292 return -ENODEV;
5293
5294 rdev = find_rdev(mddev, dev);
5295 if (!rdev)
5296 return -ENODEV;
5297
5298 md_error(mddev, rdev);
5299 return 0;
5300}
5301
Andre Noll2f9618c2008-04-25 18:57:58 +02005302/*
5303 * We have a problem here : there is no easy way to give a CHS
5304 * virtual geometry. We currently pretend that we have a 2 heads
5305 * 4 sectors (with a BIG number of cylinders...). This drives
5306 * dosfs just mad... ;-)
5307 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005308static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5309{
5310 mddev_t *mddev = bdev->bd_disk->private_data;
5311
5312 geo->heads = 2;
5313 geo->sectors = 4;
5314 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5315 return 0;
5316}
5317
Al Viroa39907f2008-03-02 10:31:15 -05005318static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 unsigned int cmd, unsigned long arg)
5320{
5321 int err = 0;
5322 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 mddev_t *mddev = NULL;
5324
5325 if (!capable(CAP_SYS_ADMIN))
5326 return -EACCES;
5327
5328 /*
5329 * Commands dealing with the RAID driver but not any
5330 * particular array:
5331 */
5332 switch (cmd)
5333 {
5334 case RAID_VERSION:
5335 err = get_version(argp);
5336 goto done;
5337
5338 case PRINT_RAID_DEBUG:
5339 err = 0;
5340 md_print_devices();
5341 goto done;
5342
5343#ifndef MODULE
5344 case RAID_AUTORUN:
5345 err = 0;
5346 autostart_arrays(arg);
5347 goto done;
5348#endif
5349 default:;
5350 }
5351
5352 /*
5353 * Commands creating/starting a new array:
5354 */
5355
Al Viroa39907f2008-03-02 10:31:15 -05005356 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357
5358 if (!mddev) {
5359 BUG();
5360 goto abort;
5361 }
5362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 err = mddev_lock(mddev);
5364 if (err) {
5365 printk(KERN_INFO
5366 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5367 err, cmd);
5368 goto abort;
5369 }
5370
5371 switch (cmd)
5372 {
5373 case SET_ARRAY_INFO:
5374 {
5375 mdu_array_info_t info;
5376 if (!arg)
5377 memset(&info, 0, sizeof(info));
5378 else if (copy_from_user(&info, argp, sizeof(info))) {
5379 err = -EFAULT;
5380 goto abort_unlock;
5381 }
5382 if (mddev->pers) {
5383 err = update_array_info(mddev, &info);
5384 if (err) {
5385 printk(KERN_WARNING "md: couldn't update"
5386 " array info. %d\n", err);
5387 goto abort_unlock;
5388 }
5389 goto done_unlock;
5390 }
5391 if (!list_empty(&mddev->disks)) {
5392 printk(KERN_WARNING
5393 "md: array %s already has disks!\n",
5394 mdname(mddev));
5395 err = -EBUSY;
5396 goto abort_unlock;
5397 }
5398 if (mddev->raid_disks) {
5399 printk(KERN_WARNING
5400 "md: array %s already initialised!\n",
5401 mdname(mddev));
5402 err = -EBUSY;
5403 goto abort_unlock;
5404 }
5405 err = set_array_info(mddev, &info);
5406 if (err) {
5407 printk(KERN_WARNING "md: couldn't set"
5408 " array info. %d\n", err);
5409 goto abort_unlock;
5410 }
5411 }
5412 goto done_unlock;
5413
5414 default:;
5415 }
5416
5417 /*
5418 * Commands querying/configuring an existing array:
5419 */
NeilBrown32a76272005-06-21 17:17:14 -07005420 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005421 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005422 if ((!mddev->raid_disks && !mddev->external)
5423 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5424 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5425 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 err = -ENODEV;
5427 goto abort_unlock;
5428 }
5429
5430 /*
5431 * Commands even a read-only array can execute:
5432 */
5433 switch (cmd)
5434 {
5435 case GET_ARRAY_INFO:
5436 err = get_array_info(mddev, argp);
5437 goto done_unlock;
5438
NeilBrown32a76272005-06-21 17:17:14 -07005439 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005440 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005441 goto done_unlock;
5442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 case GET_DISK_INFO:
5444 err = get_disk_info(mddev, argp);
5445 goto done_unlock;
5446
5447 case RESTART_ARRAY_RW:
5448 err = restart_array(mddev);
5449 goto done_unlock;
5450
5451 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005452 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 goto done_unlock;
5454
5455 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005456 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 goto done_unlock;
5458
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 }
5460
5461 /*
5462 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005463 * superblock, so we do not allow them on read-only arrays.
5464 * However non-MD ioctls (e.g. get-size) will still come through
5465 * here and hit the 'default' below, so only disallow
5466 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005468 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005469 if (mddev->ro == 2) {
5470 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005471 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005472 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5473 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005474 } else {
5475 err = -EROFS;
5476 goto abort_unlock;
5477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 }
5479
5480 switch (cmd)
5481 {
5482 case ADD_NEW_DISK:
5483 {
5484 mdu_disk_info_t info;
5485 if (copy_from_user(&info, argp, sizeof(info)))
5486 err = -EFAULT;
5487 else
5488 err = add_new_disk(mddev, &info);
5489 goto done_unlock;
5490 }
5491
5492 case HOT_REMOVE_DISK:
5493 err = hot_remove_disk(mddev, new_decode_dev(arg));
5494 goto done_unlock;
5495
5496 case HOT_ADD_DISK:
5497 err = hot_add_disk(mddev, new_decode_dev(arg));
5498 goto done_unlock;
5499
5500 case SET_DISK_FAULTY:
5501 err = set_disk_faulty(mddev, new_decode_dev(arg));
5502 goto done_unlock;
5503
5504 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005505 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 goto done_unlock;
5507
NeilBrown32a76272005-06-21 17:17:14 -07005508 case SET_BITMAP_FILE:
5509 err = set_bitmap_file(mddev, (int)arg);
5510 goto done_unlock;
5511
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 err = -EINVAL;
5514 goto abort_unlock;
5515 }
5516
5517done_unlock:
5518abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005519 if (mddev->hold_active == UNTIL_IOCTL &&
5520 err != -EINVAL)
5521 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 mddev_unlock(mddev);
5523
5524 return err;
5525done:
5526 if (err)
5527 MD_BUG();
5528abort:
5529 return err;
5530}
5531
Al Viroa39907f2008-03-02 10:31:15 -05005532static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533{
5534 /*
5535 * Succeed if we can lock the mddev, which confirms that
5536 * it isn't being stopped right now.
5537 */
NeilBrownd3374822009-01-09 08:31:10 +11005538 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 int err;
5540
NeilBrownd3374822009-01-09 08:31:10 +11005541 if (mddev->gendisk != bdev->bd_disk) {
5542 /* we are racing with mddev_put which is discarding this
5543 * bd_disk.
5544 */
5545 mddev_put(mddev);
5546 /* Wait until bdev->bd_disk is definitely gone */
5547 flush_scheduled_work();
5548 /* Then retry the open from the top */
5549 return -ERESTARTSYS;
5550 }
5551 BUG_ON(mddev != bdev->bd_disk->private_data);
5552
NeilBrownc8c00a62009-08-10 12:50:52 +10005553 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 goto out;
5555
5556 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005557 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005558 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
Al Viroa39907f2008-03-02 10:31:15 -05005560 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 out:
5562 return err;
5563}
5564
Al Viroa39907f2008-03-02 10:31:15 -05005565static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566{
Al Viroa39907f2008-03-02 10:31:15 -05005567 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005569 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005570 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 mddev_put(mddev);
5572
5573 return 0;
5574}
5575
Linus Torvalds44ce6292007-05-09 18:51:36 -07005576static int md_media_changed(struct gendisk *disk)
5577{
5578 mddev_t *mddev = disk->private_data;
5579
5580 return mddev->changed;
5581}
5582
5583static int md_revalidate(struct gendisk *disk)
5584{
5585 mddev_t *mddev = disk->private_data;
5586
5587 mddev->changed = 0;
5588 return 0;
5589}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005590static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591{
5592 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005593 .open = md_open,
5594 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005595 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005596 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005597 .media_changed = md_media_changed,
5598 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599};
5600
Adrian Bunk75c96f82005-05-05 16:16:09 -07005601static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602{
5603 mdk_thread_t *thread = arg;
5604
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 /*
5606 * md_thread is a 'system-thread', it's priority should be very
5607 * high. We avoid resource deadlocks individually in each
5608 * raid personality. (RAID5 does preallocation) We also use RR and
5609 * the very same RT priority as kswapd, thus we will never get
5610 * into a priority inversion deadlock.
5611 *
5612 * we definitely have to have equal or higher priority than
5613 * bdflush, otherwise bdflush will deadlock if there are too
5614 * many dirty RAID5 blocks.
5615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
NeilBrown6985c432005-10-19 21:23:47 -07005617 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005618 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619
NeilBrown93588e22005-11-15 00:09:12 -08005620 /* We need to wait INTERRUPTIBLE so that
5621 * we don't add to the load-average.
5622 * That means we need to be sure no signals are
5623 * pending
5624 */
5625 if (signal_pending(current))
5626 flush_signals(current);
5627
5628 wait_event_interruptible_timeout
5629 (thread->wqueue,
5630 test_bit(THREAD_WAKEUP, &thread->flags)
5631 || kthread_should_stop(),
5632 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633
5634 clear_bit(THREAD_WAKEUP, &thread->flags);
5635
NeilBrown787453c2005-11-08 21:39:43 -08005636 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005638
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 return 0;
5640}
5641
5642void md_wakeup_thread(mdk_thread_t *thread)
5643{
5644 if (thread) {
5645 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5646 set_bit(THREAD_WAKEUP, &thread->flags);
5647 wake_up(&thread->wqueue);
5648 }
5649}
5650
5651mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5652 const char *name)
5653{
5654 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655
NeilBrown9ffae0c2006-01-06 00:20:32 -08005656 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 if (!thread)
5658 return NULL;
5659
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 init_waitqueue_head(&thread->wqueue);
5661
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 thread->run = run;
5663 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005664 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005665 thread->tsk = kthread_run(md_thread, thread,
5666 "%s_%s",
5667 mdname(thread->mddev),
5668 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005669 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 kfree(thread);
5671 return NULL;
5672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 return thread;
5674}
5675
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676void md_unregister_thread(mdk_thread_t *thread)
5677{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005678 if (!thread)
5679 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005680 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005681
5682 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 kfree(thread);
5684}
5685
5686void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5687{
5688 if (!mddev) {
5689 MD_BUG();
5690 return;
5691 }
5692
NeilBrownb2d444d2005-11-08 21:39:31 -08005693 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005695
5696 if (mddev->external)
5697 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005698/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5700 mdname(mddev),
5701 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5702 __builtin_return_address(0),__builtin_return_address(1),
5703 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005704*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005705 if (!mddev->pers)
5706 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005707 if (!mddev->pers->error_handler)
5708 return;
5709 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005710 if (mddev->degraded)
5711 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005712 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5714 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5715 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005716 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717}
5718
5719/* seq_file implementation /proc/mdstat */
5720
5721static void status_unused(struct seq_file *seq)
5722{
5723 int i = 0;
5724 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
5726 seq_printf(seq, "unused devices: ");
5727
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005728 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 char b[BDEVNAME_SIZE];
5730 i++;
5731 seq_printf(seq, "%s ",
5732 bdevname(rdev->bdev,b));
5733 }
5734 if (!i)
5735 seq_printf(seq, "<none>");
5736
5737 seq_printf(seq, "\n");
5738}
5739
5740
5741static void status_resync(struct seq_file *seq, mddev_t * mddev)
5742{
NeilBrowndd71cf62009-05-07 12:49:35 +10005743 sector_t max_sectors, resync, res;
5744 unsigned long dt, db;
5745 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005746 int scale;
5747 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
NeilBrowndd71cf62009-05-07 12:49:35 +10005749 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750
5751 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005752 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005754 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755
5756 /*
5757 * Should not happen.
5758 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005759 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 MD_BUG();
5761 return;
5762 }
NeilBrown4588b422006-03-27 01:18:04 -08005763 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005764 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005765 * u32, as those are the requirements for sector_div.
5766 * Thus 'scale' must be at least 10
5767 */
5768 scale = 10;
5769 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005770 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005771 scale++;
5772 }
5773 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005774 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005775
5776 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 {
NeilBrown4588b422006-03-27 01:18:04 -08005778 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 seq_printf(seq, "[");
5780 for (i = 0; i < x; i++)
5781 seq_printf(seq, "=");
5782 seq_printf(seq, ">");
5783 for (i = 0; i < y; i++)
5784 seq_printf(seq, ".");
5785 seq_printf(seq, "] ");
5786 }
NeilBrown4588b422006-03-27 01:18:04 -08005787 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005788 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5789 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005790 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5791 "check" :
5792 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5793 "resync" : "recovery"))),
5794 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005795 (unsigned long long) resync/2,
5796 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
5798 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 * dt: time from mark until now
5800 * db: blocks written from mark until now
5801 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005802 *
5803 * rt is a sector_t, so could be 32bit or 64bit.
5804 * So we divide before multiply in case it is 32bit and close
5805 * to the limit.
5806 * We scale the divisor (db) by 32 to avoid loosing precision
5807 * near the end of resync when the number of remaining sectors
5808 * is close to 'db'.
5809 * We then divide rt by 32 after multiplying by db to compensate.
5810 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 */
5812 dt = ((jiffies - mddev->resync_mark) / HZ);
5813 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005814 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5815 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005816
NeilBrowndd71cf62009-05-07 12:49:35 +10005817 rt = max_sectors - resync; /* number of remaining sectors */
5818 sector_div(rt, db/32+1);
5819 rt *= dt;
5820 rt >>= 5;
5821
5822 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5823 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
NeilBrownff4e8d92006-07-10 04:44:16 -07005825 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826}
5827
5828static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5829{
5830 struct list_head *tmp;
5831 loff_t l = *pos;
5832 mddev_t *mddev;
5833
5834 if (l >= 0x10000)
5835 return NULL;
5836 if (!l--)
5837 /* header */
5838 return (void*)1;
5839
5840 spin_lock(&all_mddevs_lock);
5841 list_for_each(tmp,&all_mddevs)
5842 if (!l--) {
5843 mddev = list_entry(tmp, mddev_t, all_mddevs);
5844 mddev_get(mddev);
5845 spin_unlock(&all_mddevs_lock);
5846 return mddev;
5847 }
5848 spin_unlock(&all_mddevs_lock);
5849 if (!l--)
5850 return (void*)2;/* tail */
5851 return NULL;
5852}
5853
5854static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5855{
5856 struct list_head *tmp;
5857 mddev_t *next_mddev, *mddev = v;
5858
5859 ++*pos;
5860 if (v == (void*)2)
5861 return NULL;
5862
5863 spin_lock(&all_mddevs_lock);
5864 if (v == (void*)1)
5865 tmp = all_mddevs.next;
5866 else
5867 tmp = mddev->all_mddevs.next;
5868 if (tmp != &all_mddevs)
5869 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5870 else {
5871 next_mddev = (void*)2;
5872 *pos = 0x10000;
5873 }
5874 spin_unlock(&all_mddevs_lock);
5875
5876 if (v != (void*)1)
5877 mddev_put(mddev);
5878 return next_mddev;
5879
5880}
5881
5882static void md_seq_stop(struct seq_file *seq, void *v)
5883{
5884 mddev_t *mddev = v;
5885
5886 if (mddev && v != (void*)1 && v != (void*)2)
5887 mddev_put(mddev);
5888}
5889
NeilBrownd7603b72006-01-06 00:20:30 -08005890struct mdstat_info {
5891 int event;
5892};
5893
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894static int md_seq_show(struct seq_file *seq, void *v)
5895{
5896 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005897 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005899 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005900 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
5902 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005903 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 seq_printf(seq, "Personalities : ");
5905 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005906 list_for_each_entry(pers, &pers_list, list)
5907 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
5909 spin_unlock(&pers_lock);
5910 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005911 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 return 0;
5913 }
5914 if (v == (void*)2) {
5915 status_unused(seq);
5916 return 0;
5917 }
5918
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005919 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005921
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5923 seq_printf(seq, "%s : %sactive", mdname(mddev),
5924 mddev->pers ? "" : "in");
5925 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005926 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005928 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005929 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 seq_printf(seq, " %s", mddev->pers->name);
5931 }
5932
Andre Nolldd8ac332009-03-31 14:33:13 +11005933 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005934 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 char b[BDEVNAME_SIZE];
5936 seq_printf(seq, " %s[%d]",
5937 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005938 if (test_bit(WriteMostly, &rdev->flags))
5939 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005940 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 seq_printf(seq, "(F)");
5942 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005943 } else if (rdev->raid_disk < 0)
5944 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005945 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 }
5947
5948 if (!list_empty(&mddev->disks)) {
5949 if (mddev->pers)
5950 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005951 (unsigned long long)
5952 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 else
5954 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005955 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005957 if (mddev->persistent) {
5958 if (mddev->major_version != 0 ||
5959 mddev->minor_version != 90) {
5960 seq_printf(seq," super %d.%d",
5961 mddev->major_version,
5962 mddev->minor_version);
5963 }
NeilBrowne6910632008-02-06 01:39:51 -08005964 } else if (mddev->external)
5965 seq_printf(seq, " super external:%s",
5966 mddev->metadata_type);
5967 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005968 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969
5970 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005971 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005973 if (mddev->pers->sync_request) {
5974 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005975 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005976 seq_printf(seq, "\n ");
5977 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5978 seq_printf(seq, "\tresync=DELAYED\n ");
5979 else if (mddev->recovery_cp < MaxSector)
5980 seq_printf(seq, "\tresync=PENDING\n ");
5981 }
NeilBrown32a76272005-06-21 17:17:14 -07005982 } else
5983 seq_printf(seq, "\n ");
5984
5985 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005986 unsigned long chunk_kb;
5987 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005988 spin_lock_irqsave(&bitmap->lock, flags);
5989 chunk_kb = bitmap->chunksize >> 10;
5990 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5991 "%lu%s chunk",
5992 bitmap->pages - bitmap->missing_pages,
5993 bitmap->pages,
5994 (bitmap->pages - bitmap->missing_pages)
5995 << (PAGE_SHIFT - 10),
5996 chunk_kb ? chunk_kb : bitmap->chunksize,
5997 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005998 if (bitmap->file) {
5999 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006000 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006001 }
NeilBrown78d742d2005-06-21 17:17:15 -07006002
NeilBrown32a76272005-06-21 17:17:14 -07006003 seq_printf(seq, "\n");
6004 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 }
6006
6007 seq_printf(seq, "\n");
6008 }
6009 mddev_unlock(mddev);
6010
6011 return 0;
6012}
6013
Jan Engelhardt110518b2009-05-07 12:49:37 +10006014static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 .start = md_seq_start,
6016 .next = md_seq_next,
6017 .stop = md_seq_stop,
6018 .show = md_seq_show,
6019};
6020
6021static int md_seq_open(struct inode *inode, struct file *file)
6022{
6023 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006024 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6025 if (mi == NULL)
6026 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027
6028 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006029 if (error)
6030 kfree(mi);
6031 else {
6032 struct seq_file *p = file->private_data;
6033 p->private = mi;
6034 mi->event = atomic_read(&md_event_count);
6035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 return error;
6037}
6038
NeilBrownd7603b72006-01-06 00:20:30 -08006039static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6040{
6041 struct seq_file *m = filp->private_data;
6042 struct mdstat_info *mi = m->private;
6043 int mask;
6044
6045 poll_wait(filp, &md_event_waiters, wait);
6046
6047 /* always allow read */
6048 mask = POLLIN | POLLRDNORM;
6049
6050 if (mi->event != atomic_read(&md_event_count))
6051 mask |= POLLERR | POLLPRI;
6052 return mask;
6053}
6054
Arjan van de Venfa027c22007-02-12 00:55:33 -08006055static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006056 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 .open = md_seq_open,
6058 .read = seq_read,
6059 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006060 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006061 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062};
6063
NeilBrown2604b702006-01-06 00:20:36 -08006064int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006067 list_add_tail(&p->list, &pers_list);
6068 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006069 spin_unlock(&pers_lock);
6070 return 0;
6071}
6072
NeilBrown2604b702006-01-06 00:20:36 -08006073int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074{
NeilBrown2604b702006-01-06 00:20:36 -08006075 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006076 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006077 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 spin_unlock(&pers_lock);
6079 return 0;
6080}
6081
NeilBrowneea1bf32009-03-31 14:27:02 +11006082static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083{
6084 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006086 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087
6088 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006089 rcu_read_lock();
6090 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006092 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6093 (int)part_stat_read(&disk->part0, sectors[1]) -
6094 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006095 /* sync IO will cause sync_io to increase before the disk_stats
6096 * as sync_io is counted when a request starts, and
6097 * disk_stats is counted when it completes.
6098 * So resync activity will cause curr_events to be smaller than
6099 * when there was no such activity.
6100 * non-sync IO will cause disk_stat to increase without
6101 * increasing sync_io so curr_events will (eventually)
6102 * be larger than it was before. Once it becomes
6103 * substantially larger, the test below will cause
6104 * the array to appear non-idle, and resync will slow
6105 * down.
6106 * If there is a lot of outstanding resync activity when
6107 * we set last_event to curr_events, then all that activity
6108 * completing might cause the array to appear non-idle
6109 * and resync will be slowed down even though there might
6110 * not have been non-resync activity. This will only
6111 * happen once though. 'last_events' will soon reflect
6112 * the state where there is little or no outstanding
6113 * resync requests, and further resync activity will
6114 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006115 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006117 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006118 rdev->last_events = curr_events;
6119 idle = 0;
6120 }
6121 }
NeilBrown4b809912008-07-21 17:05:25 +10006122 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006123 return idle;
6124}
6125
6126void md_done_sync(mddev_t *mddev, int blocks, int ok)
6127{
6128 /* another "blocks" (512byte) blocks have been synced */
6129 atomic_sub(blocks, &mddev->recovery_active);
6130 wake_up(&mddev->recovery_wait);
6131 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006132 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 md_wakeup_thread(mddev->thread);
6134 // stop recovery, signal do_sync ....
6135 }
6136}
6137
6138
NeilBrown06d91a52005-06-21 17:17:12 -07006139/* md_write_start(mddev, bi)
6140 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006141 * in superblock) before writing, schedule a superblock update
6142 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006143 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006144void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006145{
Neil Brown0fd62b82008-06-28 08:31:36 +10006146 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006147 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006148 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006149
NeilBrownf91de922005-11-08 21:39:36 -08006150 BUG_ON(mddev->ro == 1);
6151 if (mddev->ro == 2) {
6152 /* need to switch to read/write */
6153 mddev->ro = 0;
6154 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6155 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006156 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006157 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006158 }
NeilBrown06d91a52005-06-21 17:17:12 -07006159 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006160 if (mddev->safemode == 1)
6161 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006162 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006163 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006164 if (mddev->in_sync) {
6165 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006166 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006167 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006168 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006169 }
NeilBrowna9701a32005-11-08 21:39:34 -08006170 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006171 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006172 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006173 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006174 wait_event(mddev->sb_wait,
6175 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6176 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177}
6178
6179void md_write_end(mddev_t *mddev)
6180{
6181 if (atomic_dec_and_test(&mddev->writes_pending)) {
6182 if (mddev->safemode == 2)
6183 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006184 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6186 }
6187}
6188
NeilBrown2a2275d2007-01-26 00:57:11 -08006189/* md_allow_write(mddev)
6190 * Calling this ensures that the array is marked 'active' so that writes
6191 * may proceed without blocking. It is important to call this before
6192 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6193 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006194 *
6195 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6196 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006197 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006198int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006199{
6200 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006201 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006202 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006203 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006204 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006205 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006206
6207 spin_lock_irq(&mddev->write_lock);
6208 if (mddev->in_sync) {
6209 mddev->in_sync = 0;
6210 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6211 if (mddev->safemode_delay &&
6212 mddev->safemode == 0)
6213 mddev->safemode = 1;
6214 spin_unlock_irq(&mddev->write_lock);
6215 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006216 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006217 } else
6218 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006219
6220 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6221 return -EAGAIN;
6222 else
6223 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006224}
6225EXPORT_SYMBOL_GPL(md_allow_write);
6226
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227#define SYNC_MARKS 10
6228#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006229void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230{
6231 mddev_t *mddev2;
6232 unsigned int currspeed = 0,
6233 window;
NeilBrown57afd892005-06-21 17:17:13 -07006234 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 unsigned long mark[SYNC_MARKS];
6236 sector_t mark_cnt[SYNC_MARKS];
6237 int last_mark,m;
6238 struct list_head *tmp;
6239 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006240 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006241 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006242 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243
6244 /* just incase thread restarts... */
6245 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6246 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006247 if (mddev->ro) /* never try to sync a read-only array */
6248 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249
NeilBrown61df9d92006-10-03 01:15:57 -07006250 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6251 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6252 desc = "data-check";
6253 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6254 desc = "requested-resync";
6255 else
6256 desc = "resync";
6257 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6258 desc = "reshape";
6259 else
6260 desc = "recovery";
6261
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 /* we overload curr_resync somewhat here.
6263 * 0 == not engaged in resync at all
6264 * 2 == checking that there is no conflict with another sync
6265 * 1 == like 2, but have yielded to allow conflicting resync to
6266 * commense
6267 * other == active in resync - this many blocks
6268 *
6269 * Before starting a resync we must have set curr_resync to
6270 * 2, and then checked that every "conflicting" array has curr_resync
6271 * less than ours. When we find one that is the same or higher
6272 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6273 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6274 * This will mean we have to start checking from the beginning again.
6275 *
6276 */
6277
6278 do {
6279 mddev->curr_resync = 2;
6280
6281 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006282 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006283 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 goto skip;
6285 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006286 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 if (mddev2 == mddev)
6288 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006289 if (!mddev->parallel_resync
6290 && mddev2->curr_resync
6291 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006292 DEFINE_WAIT(wq);
6293 if (mddev < mddev2 && mddev->curr_resync == 2) {
6294 /* arbitrarily yield */
6295 mddev->curr_resync = 1;
6296 wake_up(&resync_wait);
6297 }
6298 if (mddev > mddev2 && mddev->curr_resync == 1)
6299 /* no need to wait here, we can wait the next
6300 * time 'round when curr_resync == 2
6301 */
6302 continue;
NeilBrown97441972008-09-19 11:49:54 +10006303 /* We need to wait 'interruptible' so as not to
6304 * contribute to the load average, and not to
6305 * be caught by 'softlockup'
6306 */
6307 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006308 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006309 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006310 printk(KERN_INFO "md: delaying %s of %s"
6311 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006313 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006315 if (signal_pending(current))
6316 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 schedule();
6318 finish_wait(&resync_wait, &wq);
6319 goto try_again;
6320 }
6321 finish_wait(&resync_wait, &wq);
6322 }
6323 }
6324 } while (mddev->curr_resync < 2);
6325
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006326 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006327 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006328 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006329 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 */
6331 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006332 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006333 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006334 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6335 j = mddev->resync_min;
6336 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006337 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006338
NeilBrownccfcc3c2006-03-27 01:18:09 -08006339 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006340 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006341 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006343 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006344 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006345 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006346 if (rdev->raid_disk >= 0 &&
6347 !test_bit(Faulty, &rdev->flags) &&
6348 !test_bit(In_sync, &rdev->flags) &&
6349 rdev->recovery_offset < j)
6350 j = rdev->recovery_offset;
6351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352
NeilBrown61df9d92006-10-03 01:15:57 -07006353 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6354 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6355 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006356 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006357 "(but not more than %d KB/sec) for %s.\n",
6358 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359
NeilBrowneea1bf32009-03-31 14:27:02 +11006360 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006361
NeilBrown57afd892005-06-21 17:17:13 -07006362 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 for (m = 0; m < SYNC_MARKS; m++) {
6364 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006365 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 }
6367 last_mark = 0;
6368 mddev->resync_mark = mark[last_mark];
6369 mddev->resync_mark_cnt = mark_cnt[last_mark];
6370
6371 /*
6372 * Tune reconstruction:
6373 */
6374 window = 32*(PAGE_SIZE/512);
6375 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6376 window/2,(unsigned long long) max_sectors/2);
6377
6378 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 last_check = 0;
6380
6381 if (j>2) {
6382 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006383 "md: resuming %s of %s from checkpoint.\n",
6384 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 mddev->curr_resync = j;
6386 }
6387
6388 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006389 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006390
NeilBrown57afd892005-06-21 17:17:13 -07006391 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006392
NeilBrown7a91ee12009-05-26 12:57:21 +10006393 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6394 ((mddev->curr_resync > mddev->curr_resync_completed &&
6395 (mddev->curr_resync - mddev->curr_resync_completed)
6396 > (max_sectors >> 4)) ||
6397 (j - mddev->curr_resync_completed)*2
6398 >= mddev->resync_max - mddev->curr_resync_completed
6399 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006400 /* time to update curr_resync_completed */
6401 blk_unplug(mddev->queue);
6402 wait_event(mddev->recovery_wait,
6403 atomic_read(&mddev->recovery_active) == 0);
6404 mddev->curr_resync_completed =
6405 mddev->curr_resync;
6406 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006407 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006408 }
NeilBrownacb180b2009-04-14 16:28:34 +10006409
NeilBrowne62e58a2009-07-01 13:15:35 +10006410 while (j >= mddev->resync_max && !kthread_should_stop()) {
6411 /* As this condition is controlled by user-space,
6412 * we can block indefinitely, so use '_interruptible'
6413 * to avoid triggering warnings.
6414 */
6415 flush_signals(current); /* just in case */
6416 wait_event_interruptible(mddev->recovery_wait,
6417 mddev->resync_max > j
6418 || kthread_should_stop());
6419 }
NeilBrownacb180b2009-04-14 16:28:34 +10006420
6421 if (kthread_should_stop())
6422 goto interrupted;
6423
NeilBrown57afd892005-06-21 17:17:13 -07006424 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006425 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006426 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006427 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006428 goto out;
6429 }
NeilBrown57afd892005-06-21 17:17:13 -07006430
6431 if (!skipped) { /* actual IO requested */
6432 io_sectors += sectors;
6433 atomic_add(sectors, &mddev->recovery_active);
6434 }
6435
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436 j += sectors;
6437 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006438 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006439 if (last_check == 0)
6440 /* this is the earliers that rebuilt will be
6441 * visible in /proc/mdstat
6442 */
6443 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006444
6445 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 continue;
6447
NeilBrown57afd892005-06-21 17:17:13 -07006448 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006449
NeilBrowndfc70642008-05-23 13:04:39 -07006450 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 break;
6452
6453 repeat:
6454 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6455 /* step marks */
6456 int next = (last_mark+1) % SYNC_MARKS;
6457
6458 mddev->resync_mark = mark[next];
6459 mddev->resync_mark_cnt = mark_cnt[next];
6460 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006461 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462 last_mark = next;
6463 }
6464
6465
NeilBrownc6207272008-02-06 01:39:52 -08006466 if (kthread_should_stop())
6467 goto interrupted;
6468
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469
6470 /*
6471 * this loop exits only if either when we are slower than
6472 * the 'hard' speed limit, or the system was IO-idle for
6473 * a jiffy.
6474 * the system might be non-idle CPU-wise, but we only care
6475 * about not overloading the IO subsystem. (things like an
6476 * e2fsck being done on the RAID array should execute fast)
6477 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006478 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479 cond_resched();
6480
NeilBrown57afd892005-06-21 17:17:13 -07006481 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6482 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483
NeilBrown88202a02006-01-06 00:21:36 -08006484 if (currspeed > speed_min(mddev)) {
6485 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006486 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006487 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488 goto repeat;
6489 }
6490 }
6491 }
NeilBrown61df9d92006-10-03 01:15:57 -07006492 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006493 /*
6494 * this also signals 'finished resyncing' to md_stop
6495 */
6496 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006497 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006498
6499 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6500
6501 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006502 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503
NeilBrowndfc70642008-05-23 13:04:39 -07006504 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006505 mddev->curr_resync > 2) {
6506 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6507 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6508 if (mddev->curr_resync >= mddev->recovery_cp) {
6509 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006510 "md: checkpointing %s of %s.\n",
6511 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006512 mddev->recovery_cp = mddev->curr_resync;
6513 }
6514 } else
6515 mddev->recovery_cp = MaxSector;
6516 } else {
6517 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6518 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006519 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006520 if (rdev->raid_disk >= 0 &&
6521 !test_bit(Faulty, &rdev->flags) &&
6522 !test_bit(In_sync, &rdev->flags) &&
6523 rdev->recovery_offset < mddev->curr_resync)
6524 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 }
NeilBrown17571282006-12-10 02:20:52 -08006527 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 skip:
6530 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006531 mddev->curr_resync_completed = 0;
NeilBrown24395a82009-11-06 14:59:27 +11006532 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6533 /* We completed so max setting can be forgotten. */
6534 mddev->resync_max = MaxSector;
NeilBrownc6207272008-02-06 01:39:52 -08006535 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536 wake_up(&resync_wait);
6537 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6538 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006539 return;
6540
6541 interrupted:
6542 /*
6543 * got a signal, exit.
6544 */
6545 printk(KERN_INFO
6546 "md: md_do_sync() got signal ... exiting\n");
6547 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6548 goto out;
6549
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550}
NeilBrown29269552006-03-27 01:18:10 -08006551EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006552
6553
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006554static int remove_and_add_spares(mddev_t *mddev)
6555{
6556 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006557 int spares = 0;
6558
NeilBrown97e4f422009-03-31 14:33:13 +11006559 mddev->curr_resync_completed = 0;
6560
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006561 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006562 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006563 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006564 (test_bit(Faulty, &rdev->flags) ||
6565 ! test_bit(In_sync, &rdev->flags)) &&
6566 atomic_read(&rdev->nr_pending)==0) {
6567 if (mddev->pers->hot_remove_disk(
6568 mddev, rdev->raid_disk)==0) {
6569 char nm[20];
6570 sprintf(nm,"rd%d", rdev->raid_disk);
6571 sysfs_remove_link(&mddev->kobj, nm);
6572 rdev->raid_disk = -1;
6573 }
6574 }
6575
NeilBrown4044ba52009-01-09 08:31:11 +11006576 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006577 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006578 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006579 !test_bit(In_sync, &rdev->flags) &&
6580 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006581 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006582 if (rdev->raid_disk < 0
6583 && !test_bit(Faulty, &rdev->flags)) {
6584 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006585 if (mddev->pers->
6586 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006587 char nm[20];
6588 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006589 if (sysfs_create_link(&mddev->kobj,
6590 &rdev->kobj, nm))
6591 printk(KERN_WARNING
6592 "md: cannot register "
6593 "%s for %s\n",
6594 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006595 spares++;
6596 md_new_event(mddev);
6597 } else
6598 break;
6599 }
NeilBrowndfc70642008-05-23 13:04:39 -07006600 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006601 }
6602 return spares;
6603}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604/*
6605 * This routine is regularly called by all per-raid-array threads to
6606 * deal with generic issues like resync and super-block update.
6607 * Raid personalities that don't have a thread (linear/raid0) do not
6608 * need this as they never do any recovery or update the superblock.
6609 *
6610 * It does not do any resync itself, but rather "forks" off other threads
6611 * to do that as needed.
6612 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6613 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006614 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 * and wakeups up this thread which will reap the thread and finish up.
6616 * This thread also removes any faulty devices (with nr_pending == 0).
6617 *
6618 * The overall approach is:
6619 * 1/ if the superblock needs updating, update it.
6620 * 2/ If a recovery thread is running, don't do anything else.
6621 * 3/ If recovery has finished, clean up, possibly marking spares active.
6622 * 4/ If there are any faulty devices, remove them.
6623 * 5/ If array is degraded, try to add spares devices
6624 * 6/ If array has spares or is not in-sync, start a resync thread.
6625 */
6626void md_check_recovery(mddev_t *mddev)
6627{
6628 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
6630
NeilBrown5f404022005-06-21 17:17:16 -07006631 if (mddev->bitmap)
6632 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633
6634 if (mddev->ro)
6635 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006636
6637 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006638 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006639 printk(KERN_INFO "md: %s in immediate safe mode\n",
6640 mdname(mddev));
6641 mddev->safemode = 2;
6642 }
6643 flush_signals(current);
6644 }
6645
NeilBrownc89a8ee2008-08-05 15:54:13 +10006646 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6647 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006648 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006649 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006651 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006652 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006653 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6654 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655 ))
6656 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006657
NeilBrowndf5b89b2006-03-27 01:18:20 -08006658 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006659 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006660
NeilBrownc89a8ee2008-08-05 15:54:13 +10006661 if (mddev->ro) {
6662 /* Only thing we do on a ro array is remove
6663 * failed devices.
6664 */
6665 remove_and_add_spares(mddev);
6666 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6667 goto unlock;
6668 }
6669
NeilBrown31a59e32008-04-30 00:52:30 -07006670 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006671 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006672 spin_lock_irq(&mddev->write_lock);
6673 if (mddev->safemode &&
6674 !atomic_read(&mddev->writes_pending) &&
6675 !mddev->in_sync &&
6676 mddev->recovery_cp == MaxSector) {
6677 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006678 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006679 if (mddev->persistent)
6680 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6681 }
6682 if (mddev->safemode == 1)
6683 mddev->safemode = 0;
6684 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006685 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006686 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006687 }
NeilBrownfca4d842005-06-21 17:17:11 -07006688
NeilBrown850b2b42006-10-03 01:15:46 -07006689 if (mddev->flags)
6690 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006691
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006692 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006693 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006694 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006695
NeilBrown06d91a52005-06-21 17:17:12 -07006696
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6698 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6699 /* resync/recovery still happening */
6700 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6701 goto unlock;
6702 }
6703 if (mddev->sync_thread) {
6704 /* resync has finished, collect result */
6705 md_unregister_thread(mddev->sync_thread);
6706 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006707 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6708 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709 /* success...*/
6710 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006711 if (mddev->pers->spare_active(mddev))
6712 sysfs_notify(&mddev->kobj, NULL,
6713 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714 }
NeilBrowncea9c222009-03-31 15:15:05 +11006715 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6716 mddev->pers->finish_reshape)
6717 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006718 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006719
6720 /* if array is no-longer degraded, then any saved_raid_disk
6721 * information must be scrapped
6722 */
6723 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006724 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006725 rdev->saved_raid_disk = -1;
6726
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 mddev->recovery = 0;
6728 /* flag recovery needed just to double check */
6729 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006730 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006731 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732 goto unlock;
6733 }
Neil Brown72a23c22008-06-28 08:31:41 +10006734 /* Set RUNNING before clearing NEEDED to avoid
6735 * any transients in the value of "sync_action".
6736 */
6737 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6738 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006739 /* Clear some bits that don't mean anything, but
6740 * might be left set
6741 */
NeilBrown24dd4692005-11-08 21:39:26 -08006742 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6743 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006745 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6746 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 /* no recovery is running.
6748 * remove any failed drives, then
6749 * add spares if possible.
6750 * Spare are also removed and re-added, to allow
6751 * the personality to fail the re-add.
6752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006754 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006755 if (mddev->pers->check_reshape == NULL ||
6756 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006757 /* Cannot proceed */
6758 goto unlock;
6759 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006760 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006761 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006762 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6763 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006764 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006765 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006766 } else if (mddev->recovery_cp < MaxSector) {
6767 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006768 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006769 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6770 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006774 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6775 /* We are adding a device or devices to an array
6776 * which has the bitmap stored on all devices.
6777 * So make sure all bitmap pages get written
6778 */
6779 bitmap_write_all(mddev->bitmap);
6780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 mddev->sync_thread = md_register_thread(md_do_sync,
6782 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006783 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 if (!mddev->sync_thread) {
6785 printk(KERN_ERR "%s: could not start resync"
6786 " thread...\n",
6787 mdname(mddev));
6788 /* leave the spares where they are, it shouldn't hurt */
6789 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006790 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006792 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006793 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794 }
6795 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006796 if (!mddev->sync_thread) {
6797 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6798 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6799 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006800 if (mddev->sysfs_action)
6801 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803 mddev_unlock(mddev);
6804 }
6805}
6806
Dan Williams6bfe0b42008-04-30 00:52:32 -07006807void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6808{
NeilBrown3c0ee632008-10-21 13:25:28 +11006809 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006810 wait_event_timeout(rdev->blocked_wait,
6811 !test_bit(Blocked, &rdev->flags),
6812 msecs_to_jiffies(5000));
6813 rdev_dec_pending(rdev, mddev);
6814}
6815EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6816
Adrian Bunk75c96f82005-05-05 16:16:09 -07006817static int md_notify_reboot(struct notifier_block *this,
6818 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819{
6820 struct list_head *tmp;
6821 mddev_t *mddev;
6822
6823 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6824
6825 printk(KERN_INFO "md: stopping all md devices.\n");
6826
NeilBrown29ac4aa2008-02-06 01:39:58 -08006827 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006828 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006829 /* Force a switch to readonly even array
6830 * appears to still be in use. Hence
6831 * the '100'.
6832 */
NeilBrownd710e132008-10-13 11:55:12 +11006833 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006834 mddev_unlock(mddev);
6835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 /*
6837 * certain more exotic SCSI devices are known to be
6838 * volatile wrt too early system reboots. While the
6839 * right place to handle this issue is the given
6840 * driver, we do want to have a safe RAID driver ...
6841 */
6842 mdelay(1000*1);
6843 }
6844 return NOTIFY_DONE;
6845}
6846
Adrian Bunk75c96f82005-05-05 16:16:09 -07006847static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848 .notifier_call = md_notify_reboot,
6849 .next = NULL,
6850 .priority = INT_MAX, /* before any real devices */
6851};
6852
6853static void md_geninit(void)
6854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6856
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006857 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858}
6859
Adrian Bunk75c96f82005-05-05 16:16:09 -07006860static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006861{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006862 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 return -1;
6864 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006865 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866 return -1;
6867 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006868 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006869 md_probe, NULL, NULL);
6870 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871 md_probe, NULL, NULL);
6872
Linus Torvalds1da177e2005-04-16 15:20:36 -07006873 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006874 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875
6876 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006877 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878}
6879
6880
6881#ifndef MODULE
6882
6883/*
6884 * Searches all registered partitions for autorun RAID arrays
6885 * at boot time.
6886 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006887
6888static LIST_HEAD(all_detected_devices);
6889struct detected_devices_node {
6890 struct list_head list;
6891 dev_t dev;
6892};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006893
6894void md_autodetect_dev(dev_t dev)
6895{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006896 struct detected_devices_node *node_detected_dev;
6897
6898 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6899 if (node_detected_dev) {
6900 node_detected_dev->dev = dev;
6901 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6902 } else {
6903 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6904 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906}
6907
6908
6909static void autostart_arrays(int part)
6910{
6911 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006912 struct detected_devices_node *node_detected_dev;
6913 dev_t dev;
6914 int i_scanned, i_passed;
6915
6916 i_scanned = 0;
6917 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918
6919 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6920
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006921 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6922 i_scanned++;
6923 node_detected_dev = list_entry(all_detected_devices.next,
6924 struct detected_devices_node, list);
6925 list_del(&node_detected_dev->list);
6926 dev = node_detected_dev->dev;
6927 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006928 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929 if (IS_ERR(rdev))
6930 continue;
6931
NeilBrownb2d444d2005-11-08 21:39:31 -08006932 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006933 MD_BUG();
6934 continue;
6935 }
NeilBrownd0fae182008-03-04 14:29:31 -08006936 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006938 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006940
6941 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6942 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943
6944 autorun_devices(part);
6945}
6946
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006947#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948
6949static __exit void md_exit(void)
6950{
6951 mddev_t *mddev;
6952 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006953
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006954 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006955 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006957 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 unregister_blkdev(mdp_major, "mdp");
6959 unregister_reboot_notifier(&md_notifier);
6960 unregister_sysctl_table(raid_table_header);
6961 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006962 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006964 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 }
6966}
6967
Dan Williams685784a2007-07-09 11:56:42 -07006968subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969module_exit(md_exit)
6970
NeilBrownf91de922005-11-08 21:39:36 -08006971static int get_ro(char *buffer, struct kernel_param *kp)
6972{
6973 return sprintf(buffer, "%d", start_readonly);
6974}
6975static int set_ro(const char *val, struct kernel_param *kp)
6976{
6977 char *e;
6978 int num = simple_strtoul(val, &e, 10);
6979 if (*val && (*e == '\0' || *e == '\n')) {
6980 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006981 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006982 }
6983 return -EINVAL;
6984}
6985
NeilBrown80ca3a42006-07-10 04:44:18 -07006986module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6987module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08006988
NeilBrownefeb53c2009-01-09 08:31:10 +11006989module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006990
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991EXPORT_SYMBOL(register_md_personality);
6992EXPORT_SYMBOL(unregister_md_personality);
6993EXPORT_SYMBOL(md_error);
6994EXPORT_SYMBOL(md_done_sync);
6995EXPORT_SYMBOL(md_write_start);
6996EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997EXPORT_SYMBOL(md_register_thread);
6998EXPORT_SYMBOL(md_unregister_thread);
6999EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000EXPORT_SYMBOL(md_check_recovery);
7001MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07007002MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007003MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);