blob: 01b9a0fd16e07d3ced5466921078d271c8d3c04f [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;
947 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700948 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
949 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700950 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800951 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 0;
953}
954
955/*
956 * sync_super for 0.90.0
957 */
958static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
959{
960 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 mdk_rdev_t *rdev2;
962 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /* make rdev->sb match mddev data..
966 *
967 * 1/ zero out disks
968 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
969 * 3/ any empty disks < next_spare become removed
970 *
971 * disks[0] gets initialised to REMOVED because
972 * we cannot be sure from other fields if it has
973 * been initialised or not.
974 */
975 int i;
976 int active=0, working=0,failed=0,spare=0,nr_disks=0;
977
NeilBrown61181562005-09-09 16:24:02 -0700978 rdev->sb_size = MD_SB_BYTES;
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 sb = (mdp_super_t*)page_address(rdev->sb_page);
981
982 memset(sb, 0, sizeof(*sb));
983
984 sb->md_magic = MD_SB_MAGIC;
985 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 sb->patch_version = mddev->patch_version;
987 sb->gvalid_words = 0; /* ignored */
988 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
989 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
990 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
991 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
992
993 sb->ctime = mddev->ctime;
994 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +1100995 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 sb->raid_disks = mddev->raid_disks;
997 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -0800998 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 sb->utime = mddev->utime;
1000 sb->state = 0;
1001 sb->events_hi = (mddev->events>>32);
1002 sb->events_lo = (u32)mddev->events;
1003
NeilBrownf6705572006-03-27 01:18:11 -08001004 if (mddev->reshape_position == MaxSector)
1005 sb->minor_version = 90;
1006 else {
1007 sb->minor_version = 91;
1008 sb->reshape_position = mddev->reshape_position;
1009 sb->new_level = mddev->new_level;
1010 sb->delta_disks = mddev->delta_disks;
1011 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001012 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001013 }
1014 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (mddev->in_sync)
1016 {
1017 sb->recovery_cp = mddev->recovery_cp;
1018 sb->cp_events_hi = (mddev->events>>32);
1019 sb->cp_events_lo = (u32)mddev->events;
1020 if (mddev->recovery_cp == MaxSector)
1021 sb->state = (1<< MD_SB_CLEAN);
1022 } else
1023 sb->recovery_cp = 0;
1024
1025 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001026 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
NeilBrowna654b9d82005-06-21 17:17:27 -07001028 if (mddev->bitmap && mddev->bitmap_file == NULL)
1029 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001032 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001034 int desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001035 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1036 && !test_bit(Faulty, &rdev2->flags))
NeilBrown86e6ffd2005-11-08 21:39:24 -08001037 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001039 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001040 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 d = &sb->disks[rdev2->desc_nr];
1042 nr_disks++;
1043 d->number = rdev2->desc_nr;
1044 d->major = MAJOR(rdev2->bdev->bd_dev);
1045 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrownb2d444d2005-11-08 21:39:31 -08001046 if (rdev2->raid_disk >= 0 && test_bit(In_sync, &rdev2->flags)
1047 && !test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 d->raid_disk = rdev2->raid_disk;
1049 else
1050 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001051 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 d->state = (1<<MD_DISK_FAULTY);
NeilBrown1be78922006-03-27 01:18:03 -08001053 else if (test_bit(In_sync, &rdev2->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 d->state = (1<<MD_DISK_ACTIVE);
1055 d->state |= (1<<MD_DISK_SYNC);
1056 active++;
1057 working++;
1058 } else {
1059 d->state = 0;
1060 spare++;
1061 working++;
1062 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001063 if (test_bit(WriteMostly, &rdev2->flags))
1064 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* now set the "removed" and "faulty" bits on any missing devices */
1067 for (i=0 ; i < mddev->raid_disks ; i++) {
1068 mdp_disk_t *d = &sb->disks[i];
1069 if (d->state == 0 && d->number == 0) {
1070 d->number = i;
1071 d->raid_disk = i;
1072 d->state = (1<<MD_DISK_REMOVED);
1073 d->state |= (1<<MD_DISK_FAULTY);
1074 failed++;
1075 }
1076 }
1077 sb->nr_disks = nr_disks;
1078 sb->active_disks = active;
1079 sb->working_disks = working;
1080 sb->failed_disks = failed;
1081 sb->spare_disks = spare;
1082
1083 sb->this_disk = sb->disks[rdev->desc_nr];
1084 sb->sb_csum = calc_sb_csum(sb);
1085}
1086
1087/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001088 * rdev_size_change for 0.90.0
1089 */
1090static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001091super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001092{
Andre Noll58c0fed2009-03-31 14:33:13 +11001093 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001094 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001095 if (rdev->mddev->bitmap_offset)
1096 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001097 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001098 if (!num_sectors || num_sectors > rdev->sb_start)
1099 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001100 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001101 rdev->sb_page);
1102 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001103 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001104}
1105
1106
1107/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 * version 1 superblock
1109 */
1110
NeilBrown1c05b4b2006-10-21 10:24:08 -07001111static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001113 __le32 disk_csum;
1114 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 unsigned long long newcsum;
1116 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001117 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int i;
1119
1120 disk_csum = sb->sb_csum;
1121 sb->sb_csum = 0;
1122 newcsum = 0;
1123 for (i=0; size>=4; size -= 4 )
1124 newcsum += le32_to_cpu(*isuper++);
1125
1126 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001127 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1130 sb->sb_csum = disk_csum;
1131 return cpu_to_le32(csum);
1132}
1133
1134static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1135{
1136 struct mdp_superblock_1 *sb;
1137 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001138 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001140 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 /*
Andre Noll0f420352008-07-11 22:02:23 +10001143 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 * It is always aligned to a 4K boundary and
1145 * depeding on minor_version, it can be:
1146 * 0: At least 8K, but less than 12K, from end of device
1147 * 1: At start of device
1148 * 2: 4K from start of device.
1149 */
1150 switch(minor_version) {
1151 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001152 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1153 sb_start -= 8*2;
1154 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001157 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001160 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 break;
1162 default:
1163 return -EINVAL;
1164 }
Andre Noll0f420352008-07-11 22:02:23 +10001165 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
NeilBrown0002b272005-09-09 16:23:53 -07001167 /* superblock is rarely larger than 1K, but it can be larger,
1168 * and it is safe to read 4k, so we do that
1169 */
1170 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (ret) return ret;
1172
1173
1174 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1175
1176 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1177 sb->major_version != cpu_to_le32(1) ||
1178 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001179 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001180 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return -EINVAL;
1182
1183 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1184 printk("md: invalid superblock checksum on %s\n",
1185 bdevname(rdev->bdev,b));
1186 return -EINVAL;
1187 }
1188 if (le64_to_cpu(sb->data_size) < 10) {
1189 printk("md: data_size too small on %s\n",
1190 bdevname(rdev->bdev,b));
1191 return -EINVAL;
1192 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 rdev->preferred_minor = 0xffff;
1195 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001196 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
NeilBrown0002b272005-09-09 16:23:53 -07001198 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001199 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001200 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001201 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1202
1203 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001204 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001205 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001206
NeilBrown31b65a02006-07-10 04:44:14 -07001207 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1208 rdev->desc_nr = -1;
1209 else
1210 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1211
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001212 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001213 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001214 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 __u64 ev1, ev2;
1216 struct mdp_superblock_1 *refsb =
1217 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1218
1219 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1220 sb->level != refsb->level ||
1221 sb->layout != refsb->layout ||
1222 sb->chunksize != refsb->chunksize) {
1223 printk(KERN_WARNING "md: %s has strangely different"
1224 " superblock to %s\n",
1225 bdevname(rdev->bdev,b),
1226 bdevname(refdev->bdev,b2));
1227 return -EINVAL;
1228 }
1229 ev1 = le64_to_cpu(sb->events);
1230 ev2 = le64_to_cpu(refsb->events);
1231
1232 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001233 ret = 1;
1234 else
1235 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
NeilBrowna1801f82008-03-04 14:29:31 -08001237 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001238 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1239 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001241 rdev->sectors = rdev->sb_start;
1242 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001244 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001245 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001246 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001247 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1251{
1252 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001253 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
NeilBrown41158c72005-06-21 17:17:25 -07001255 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001256 clear_bit(Faulty, &rdev->flags);
1257 clear_bit(In_sync, &rdev->flags);
1258 clear_bit(WriteMostly, &rdev->flags);
1259 clear_bit(BarriersNotsupp, &rdev->flags);
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (mddev->raid_disks == 0) {
1262 mddev->major_version = 1;
1263 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001264 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001265 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1267 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1268 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001269 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 mddev->layout = le32_to_cpu(sb->layout);
1271 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001272 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001273 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001274 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001275 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1278 memcpy(mddev->uuid, sb->set_uuid, 16);
1279
1280 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001281
NeilBrown71c08052005-09-09 16:23:51 -07001282 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001283 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001284 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001285
NeilBrownf6705572006-03-27 01:18:11 -08001286 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1287 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1288 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1289 mddev->new_level = le32_to_cpu(sb->new_level);
1290 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001291 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001292 } else {
1293 mddev->reshape_position = MaxSector;
1294 mddev->delta_disks = 0;
1295 mddev->new_level = mddev->level;
1296 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001297 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001298 }
1299
NeilBrown41158c72005-06-21 17:17:25 -07001300 } else if (mddev->pers == NULL) {
1301 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ++ev1;
1303 if (ev1 < mddev->events)
1304 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001305 } else if (mddev->bitmap) {
1306 /* If adding to array with a bitmap, then we can accept an
1307 * older device, but not too old.
1308 */
NeilBrown41158c72005-06-21 17:17:25 -07001309 if (ev1 < mddev->bitmap->events_cleared)
1310 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001311 } else {
1312 if (ev1 < mddev->events)
1313 /* just a hot-add of a new device, leave raid_disk at -1 */
1314 return 0;
1315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (mddev->level != LEVEL_MULTIPATH) {
1317 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001318 if (rdev->desc_nr < 0 ||
1319 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1320 role = 0xffff;
1321 rdev->desc_nr = -1;
1322 } else
1323 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 switch(role) {
1325 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 break;
1327 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001328 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
1330 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001331 if ((le32_to_cpu(sb->feature_map) &
1332 MD_FEATURE_RECOVERY_OFFSET))
1333 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1334 else
1335 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 rdev->raid_disk = role;
1337 break;
1338 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001339 if (sb->devflags & WriteMostly1)
1340 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001341 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001342 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return 0;
1345}
1346
1347static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1348{
1349 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 mdk_rdev_t *rdev2;
1351 int max_dev, i;
1352 /* make rdev->sb match mddev and rdev data. */
1353
1354 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1355
1356 sb->feature_map = 0;
1357 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001358 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 memset(sb->pad1, 0, sizeof(sb->pad1));
1360 memset(sb->pad2, 0, sizeof(sb->pad2));
1361 memset(sb->pad3, 0, sizeof(sb->pad3));
1362
1363 sb->utime = cpu_to_le64((__u64)mddev->utime);
1364 sb->events = cpu_to_le64(mddev->events);
1365 if (mddev->in_sync)
1366 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1367 else
1368 sb->resync_offset = cpu_to_le64(0);
1369
NeilBrown1c05b4b2006-10-21 10:24:08 -07001370 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001371
NeilBrownf0ca3402006-02-02 14:28:04 -08001372 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001373 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001374 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001375 sb->level = cpu_to_le32(mddev->level);
1376 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001377
NeilBrowna654b9d82005-06-21 17:17:27 -07001378 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1379 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001380 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001381 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001382
1383 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001384 !test_bit(In_sync, &rdev->flags)) {
NeilBrown97e4f422009-03-31 14:33:13 +11001385 if (rdev->recovery_offset > 0) {
1386 sb->feature_map |=
1387 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1388 sb->recovery_offset =
1389 cpu_to_le64(rdev->recovery_offset);
1390 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001391 }
1392
NeilBrownf6705572006-03-27 01:18:11 -08001393 if (mddev->reshape_position != MaxSector) {
1394 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1395 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1396 sb->new_layout = cpu_to_le32(mddev->new_layout);
1397 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1398 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001399 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001400 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001403 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 if (rdev2->desc_nr+1 > max_dev)
1405 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001406
NeilBrown70471da2009-08-03 10:59:57 +10001407 if (max_dev > le32_to_cpu(sb->max_dev)) {
1408 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001409 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001410 rdev->sb_size = max_dev * 2 + 256;
1411 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1412 if (rdev->sb_size & bmask)
1413 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 for (i=0; i<max_dev;i++)
1416 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1417
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001418 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001420 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001422 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001424 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1425 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 else
1427 sb->dev_roles[i] = cpu_to_le16(0xffff);
1428 }
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 sb->sb_csum = calc_sb_1_csum(sb);
1431}
1432
Chris Webb0cd17fe2008-06-28 08:31:46 +10001433static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001434super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001435{
1436 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001437 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001438 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001439 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001440 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001441 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001442 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1443 max_sectors -= rdev->data_offset;
1444 if (!num_sectors || num_sectors > max_sectors)
1445 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001446 } else if (rdev->mddev->bitmap_offset) {
1447 /* minor version 0 with bitmap we can't move */
1448 return 0;
1449 } else {
1450 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001451 sector_t sb_start;
1452 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1453 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001454 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001455 if (!num_sectors || num_sectors > max_sectors)
1456 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001457 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001458 }
1459 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001460 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001461 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001462 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001463 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001464 rdev->sb_page);
1465 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001466 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001467}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Adrian Bunk75c96f82005-05-05 16:16:09 -07001469static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 [0] = {
1471 .name = "0.90.0",
1472 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001473 .load_super = super_90_load,
1474 .validate_super = super_90_validate,
1475 .sync_super = super_90_sync,
1476 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 },
1478 [1] = {
1479 .name = "md-1",
1480 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001481 .load_super = super_1_load,
1482 .validate_super = super_1_validate,
1483 .sync_super = super_1_sync,
1484 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 },
1486};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1489{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001490 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
NeilBrown4b809912008-07-21 17:05:25 +10001492 rcu_read_lock();
1493 rdev_for_each_rcu(rdev, mddev1)
1494 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001495 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001496 rdev2->bdev->bd_contains) {
1497 rcu_read_unlock();
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001498 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001499 }
1500 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 return 0;
1502}
1503
1504static LIST_HEAD(pending_raid_disks);
1505
Andre Nollac5e7112009-08-03 10:59:47 +10001506/*
1507 * Try to register data integrity profile for an mddev
1508 *
1509 * This is called when an array is started and after a disk has been kicked
1510 * from the array. It only succeeds if all working and active component devices
1511 * are integrity capable with matching profiles.
1512 */
1513int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001514{
Andre Nollac5e7112009-08-03 10:59:47 +10001515 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001516
Andre Nollac5e7112009-08-03 10:59:47 +10001517 if (list_empty(&mddev->disks))
1518 return 0; /* nothing to do */
1519 if (blk_get_integrity(mddev->gendisk))
1520 return 0; /* already registered */
1521 list_for_each_entry(rdev, &mddev->disks, same_set) {
1522 /* skip spares and non-functional disks */
1523 if (test_bit(Faulty, &rdev->flags))
1524 continue;
1525 if (rdev->raid_disk < 0)
1526 continue;
1527 /*
1528 * If at least one rdev is not integrity capable, we can not
1529 * enable data integrity for the md device.
1530 */
1531 if (!bdev_get_integrity(rdev->bdev))
1532 return -EINVAL;
1533 if (!reference) {
1534 /* Use the first rdev as the reference */
1535 reference = rdev;
1536 continue;
1537 }
1538 /* does this rdev's profile match the reference profile? */
1539 if (blk_integrity_compare(reference->bdev->bd_disk,
1540 rdev->bdev->bd_disk) < 0)
1541 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001542 }
Andre Nollac5e7112009-08-03 10:59:47 +10001543 /*
1544 * All component devices are integrity capable and have matching
1545 * profiles, register the common profile for the md device.
1546 */
1547 if (blk_integrity_register(mddev->gendisk,
1548 bdev_get_integrity(reference->bdev)) != 0) {
1549 printk(KERN_ERR "md: failed to register integrity for %s\n",
1550 mdname(mddev));
1551 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001552 }
Andre Nollac5e7112009-08-03 10:59:47 +10001553 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1554 mdname(mddev));
1555 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001556}
Andre Nollac5e7112009-08-03 10:59:47 +10001557EXPORT_SYMBOL(md_integrity_register);
1558
1559/* Disable data integrity if non-capable/non-matching disk is being added */
1560void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1561{
1562 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1563 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1564
1565 if (!bi_mddev) /* nothing to do */
1566 return;
1567 if (rdev->raid_disk < 0) /* skip spares */
1568 return;
1569 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1570 rdev->bdev->bd_disk) >= 0)
1571 return;
1572 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1573 blk_integrity_unregister(mddev->gendisk);
1574}
1575EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1578{
NeilBrown7dd5e7c2007-02-28 20:11:35 -08001579 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001580 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001581 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001582 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 if (rdev->mddev) {
1585 MD_BUG();
1586 return -EINVAL;
1587 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001588
1589 /* prevent duplicates */
1590 if (find_rdev(mddev, rdev->bdev->bd_dev))
1591 return -EEXIST;
1592
Andre Nolldd8ac332009-03-31 14:33:13 +11001593 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1594 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1595 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001596 if (mddev->pers) {
1597 /* Cannot change size, so fail
1598 * If mddev->level <= 0, then we don't care
1599 * about aligning sizes (e.g. linear)
1600 */
1601 if (mddev->level > 0)
1602 return -ENOSPC;
1603 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001604 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /* Verify rdev->desc_nr is unique.
1608 * If it is -1, assign a free number, else
1609 * check number is not in use
1610 */
1611 if (rdev->desc_nr < 0) {
1612 int choice = 0;
1613 if (mddev->pers) choice = mddev->raid_disks;
1614 while (find_rdev_nr(mddev, choice))
1615 choice++;
1616 rdev->desc_nr = choice;
1617 } else {
1618 if (find_rdev_nr(mddev, rdev->desc_nr))
1619 return -EBUSY;
1620 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001621 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1622 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1623 mdname(mddev), mddev->max_disks);
1624 return -EBUSY;
1625 }
NeilBrown19133a42005-11-08 21:39:35 -08001626 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001627 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001628 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001631 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001632
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001633 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001634 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001635
Tejun Heo0762b8b2008-08-25 19:56:12 +09001636 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001637 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1638 kobject_del(&rdev->kobj);
1639 goto fail;
1640 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001641 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1642
NeilBrown4b809912008-07-21 17:05:25 +10001643 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001644 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001645
1646 /* May as well allow recovery to be retried once */
1647 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001650
1651 fail:
1652 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1653 b, mdname(mddev));
1654 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
NeilBrown177a99b2008-02-06 01:39:56 -08001657static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001658{
1659 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1660 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001661 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001662}
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1665{
1666 char b[BDEVNAME_SIZE];
1667 if (!rdev->mddev) {
1668 MD_BUG();
1669 return;
1670 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001671 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001672 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1674 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001675 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001676 sysfs_put(rdev->sysfs_state);
1677 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001678 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001679 * writing to 'remove' to "dev/state". We also need
1680 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001681 */
NeilBrown4b809912008-07-21 17:05:25 +10001682 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001683 INIT_WORK(&rdev->del_work, md_delayed_delete);
1684 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001685 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688/*
1689 * prevent the device from being mounted, repartitioned or
1690 * otherwise reused by a RAID array (or any other kernel
1691 * subsystem), by bd_claiming the device.
1692 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001693static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 int err = 0;
1696 struct block_device *bdev;
1697 char b[BDEVNAME_SIZE];
1698
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001699 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (IS_ERR(bdev)) {
1701 printk(KERN_ERR "md: could not open %s.\n",
1702 __bdevname(dev, b));
1703 return PTR_ERR(bdev);
1704 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001705 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (err) {
1707 printk(KERN_ERR "md: could not bd_claim %s.\n",
1708 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001709 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return err;
1711 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001712 if (!shared)
1713 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 rdev->bdev = bdev;
1715 return err;
1716}
1717
1718static void unlock_rdev(mdk_rdev_t *rdev)
1719{
1720 struct block_device *bdev = rdev->bdev;
1721 rdev->bdev = NULL;
1722 if (!bdev)
1723 MD_BUG();
1724 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001725 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727
1728void md_autodetect_dev(dev_t dev);
1729
1730static void export_rdev(mdk_rdev_t * rdev)
1731{
1732 char b[BDEVNAME_SIZE];
1733 printk(KERN_INFO "md: export_rdev(%s)\n",
1734 bdevname(rdev->bdev,b));
1735 if (rdev->mddev)
1736 MD_BUG();
1737 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001739 if (test_bit(AutoDetected, &rdev->flags))
1740 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741#endif
1742 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001743 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
1746static void kick_rdev_from_array(mdk_rdev_t * rdev)
1747{
1748 unbind_rdev_from_array(rdev);
1749 export_rdev(rdev);
1750}
1751
1752static void export_array(mddev_t *mddev)
1753{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001754 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
NeilBrownd089c6a2008-02-06 01:39:59 -08001756 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (!rdev->mddev) {
1758 MD_BUG();
1759 continue;
1760 }
1761 kick_rdev_from_array(rdev);
1762 }
1763 if (!list_empty(&mddev->disks))
1764 MD_BUG();
1765 mddev->raid_disks = 0;
1766 mddev->major_version = 0;
1767}
1768
1769static void print_desc(mdp_disk_t *desc)
1770{
1771 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1772 desc->major,desc->minor,desc->raid_disk,desc->state);
1773}
1774
Cheng Renquancd2ac932009-01-09 08:31:08 +11001775static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 int i;
1778
1779 printk(KERN_INFO
1780 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1781 sb->major_version, sb->minor_version, sb->patch_version,
1782 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1783 sb->ctime);
1784 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1785 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1786 sb->md_minor, sb->layout, sb->chunk_size);
1787 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1788 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1789 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1790 sb->failed_disks, sb->spare_disks,
1791 sb->sb_csum, (unsigned long)sb->events_lo);
1792
1793 printk(KERN_INFO);
1794 for (i = 0; i < MD_SB_DISKS; i++) {
1795 mdp_disk_t *desc;
1796
1797 desc = sb->disks + i;
1798 if (desc->number || desc->major || desc->minor ||
1799 desc->raid_disk || (desc->state && (desc->state != 4))) {
1800 printk(" D %2d: ", i);
1801 print_desc(desc);
1802 }
1803 }
1804 printk(KERN_INFO "md: THIS: ");
1805 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
1807
Cheng Renquancd2ac932009-01-09 08:31:08 +11001808static void print_sb_1(struct mdp_superblock_1 *sb)
1809{
1810 __u8 *uuid;
1811
1812 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001813 printk(KERN_INFO
1814 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1815 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1816 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001817 le32_to_cpu(sb->major_version),
1818 le32_to_cpu(sb->feature_map),
1819 uuid[0], uuid[1], uuid[2], uuid[3],
1820 uuid[4], uuid[5], uuid[6], uuid[7],
1821 uuid[8], uuid[9], uuid[10], uuid[11],
1822 uuid[12], uuid[13], uuid[14], uuid[15],
1823 sb->set_name,
1824 (unsigned long long)le64_to_cpu(sb->ctime)
1825 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1826
1827 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001828 printk(KERN_INFO
1829 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001830 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001831 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1832 ":%02x%02x%02x%02x%02x%02x\n"
1833 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1834 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001835 le32_to_cpu(sb->level),
1836 (unsigned long long)le64_to_cpu(sb->size),
1837 le32_to_cpu(sb->raid_disks),
1838 le32_to_cpu(sb->layout),
1839 le32_to_cpu(sb->chunksize),
1840 (unsigned long long)le64_to_cpu(sb->data_offset),
1841 (unsigned long long)le64_to_cpu(sb->data_size),
1842 (unsigned long long)le64_to_cpu(sb->super_offset),
1843 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1844 le32_to_cpu(sb->dev_number),
1845 uuid[0], uuid[1], uuid[2], uuid[3],
1846 uuid[4], uuid[5], uuid[6], uuid[7],
1847 uuid[8], uuid[9], uuid[10], uuid[11],
1848 uuid[12], uuid[13], uuid[14], uuid[15],
1849 sb->devflags,
1850 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1851 (unsigned long long)le64_to_cpu(sb->events),
1852 (unsigned long long)le64_to_cpu(sb->resync_offset),
1853 le32_to_cpu(sb->sb_csum),
1854 le32_to_cpu(sb->max_dev)
1855 );
1856}
1857
1858static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001861 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1862 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001863 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1864 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001866 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1867 switch (major_version) {
1868 case 0:
1869 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1870 break;
1871 case 1:
1872 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1873 break;
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 } else
1876 printk(KERN_INFO "md: no rdev superblock!\n");
1877}
1878
Adrian Bunk5e563412006-06-26 00:27:42 -07001879static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001881 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 mdk_rdev_t *rdev;
1883 mddev_t *mddev;
1884 char b[BDEVNAME_SIZE];
1885
1886 printk("\n");
1887 printk("md: **********************************\n");
1888 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1889 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001890 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
NeilBrown32a76272005-06-21 17:17:14 -07001892 if (mddev->bitmap)
1893 bitmap_print_sb(mddev->bitmap);
1894 else
1895 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001896 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 printk("<%s>", bdevname(rdev->bdev,b));
1898 printk("\n");
1899
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001900 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001901 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 printk("md: **********************************\n");
1904 printk("\n");
1905}
1906
1907
NeilBrown42543762006-06-26 00:27:57 -07001908static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
NeilBrown42543762006-06-26 00:27:57 -07001910 /* Update each superblock (in-memory image), but
1911 * if we are allowed to, skip spares which already
1912 * have the right event counter, or have one earlier
1913 * (which would mean they aren't being marked as dirty
1914 * with the rest of the array)
1915 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
NeilBrown5e865102009-11-12 12:08:04 +11001918 /* First make sure individual recovery_offsets are correct */
1919 list_for_each_entry(rdev, &mddev->disks, same_set) {
1920 if (rdev->raid_disk >= 0 &&
1921 !test_bit(In_sync, &rdev->flags) &&
1922 mddev->curr_resync_completed > rdev->recovery_offset)
1923 rdev->recovery_offset = mddev->curr_resync_completed;
1924
1925 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001926 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001927 if (rdev->sb_events == mddev->events ||
1928 (nospares &&
1929 rdev->raid_disk < 0 &&
1930 (rdev->sb_events&1)==0 &&
1931 rdev->sb_events+1 == mddev->events)) {
1932 /* Don't update this superblock */
1933 rdev->sb_loaded = 2;
1934 } else {
1935 super_types[mddev->major_version].
1936 sync_super(mddev, rdev);
1937 rdev->sb_loaded = 1;
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
1940}
1941
NeilBrown850b2b42006-10-03 01:15:46 -07001942static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001945 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001946 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
NeilBrown1b57f132009-06-18 08:48:19 +10001948 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07001949 if (mddev->external)
1950 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001952 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001953
NeilBrown850b2b42006-10-03 01:15:46 -07001954 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1955 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1956 force_change = 1;
1957 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1958 /* just a clean<-> dirty transition, possibly leave spares alone,
1959 * though if events isn't the right even/odd, we will have to do
1960 * spares after all
1961 */
1962 nospares = 1;
1963 if (force_change)
1964 nospares = 0;
1965 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001966 /* If the array is degraded, then skipping spares is both
1967 * dangerous and fairly pointless.
1968 * Dangerous because a device that was removed from the array
1969 * might have a event_count that still looks up-to-date,
1970 * so it can be re-added without a resync.
1971 * Pointless because if there are any spares to skip,
1972 * then a recovery will happen and soon that array won't
1973 * be degraded any more and the spare can go back to sleep then.
1974 */
NeilBrown850b2b42006-10-03 01:15:46 -07001975 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001976
NeilBrown06d91a52005-06-21 17:17:12 -07001977 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07001978
1979 /* If this is just a dirty<->clean transition, and the array is clean
1980 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001981 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001982 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001983 && (mddev->events & 1)
1984 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07001985 mddev->events--;
1986 else {
1987 /* otherwise we have to go forward and ... */
1988 mddev->events ++;
1989 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10001990 /* .. if the array isn't clean, an 'even' event must also go
1991 * to spares. */
1992 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07001993 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07001994 } else {
NeilBrown51d56682009-08-13 09:54:02 +10001995 /* otherwise an 'odd' event must go to spares */
1996 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07001997 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07001998 }
1999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 if (!mddev->events) {
2002 /*
2003 * oops, this 64-bit counter should never wrap.
2004 * Either we are in around ~1 trillion A.C., assuming
2005 * 1 reboot per second, or we have a bug:
2006 */
2007 MD_BUG();
2008 mddev->events --;
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 /*
2012 * do not write anything to disk if using
2013 * nonpersistent superblocks
2014 */
NeilBrown06d91a52005-06-21 17:17:12 -07002015 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002016 if (!mddev->external)
2017 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2018
NeilBrowna9701a32005-11-08 21:39:34 -08002019 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002020 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002022 }
NeilBrowne6910632008-02-06 01:39:51 -08002023 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002024 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026 dprintk(KERN_INFO
2027 "md: updating %s RAID superblock on device (in sync %d)\n",
2028 mdname(mddev),mddev->in_sync);
2029
NeilBrown4ad13662007-07-17 04:06:13 -07002030 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002031 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 char b[BDEVNAME_SIZE];
2033 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002034 if (rdev->sb_loaded != 1)
2035 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002036 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 dprintk("(skipping faulty ");
2038
2039 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002040 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002041 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002042 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002043 rdev->sb_page);
2044 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2045 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002046 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002047 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 } else
2050 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002051 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* only need to write one superblock... */
2053 break;
2054 }
NeilBrowna9701a32005-11-08 21:39:34 -08002055 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002056 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002057
NeilBrowna9701a32005-11-08 21:39:34 -08002058 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002059 if (mddev->in_sync != sync_req ||
2060 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002061 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002062 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002063 goto repeat;
2064 }
NeilBrown850b2b42006-10-03 01:15:46 -07002065 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002066 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002067 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002068 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2069 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Andre Noll7f6ce762008-03-23 18:34:54 +01002073/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002074 * We want to accept with case. For this we use cmd_match.
2075 */
2076static int cmd_match(const char *cmd, const char *str)
2077{
2078 /* See if cmd, written into a sysfs file, matches
2079 * str. They must either be the same, or cmd can
2080 * have a trailing newline
2081 */
2082 while (*cmd && *str && *cmd == *str) {
2083 cmd++;
2084 str++;
2085 }
2086 if (*cmd == '\n')
2087 cmd++;
2088 if (*str || *cmd)
2089 return 0;
2090 return 1;
2091}
2092
NeilBrown86e6ffd2005-11-08 21:39:24 -08002093struct rdev_sysfs_entry {
2094 struct attribute attr;
2095 ssize_t (*show)(mdk_rdev_t *, char *);
2096 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2097};
2098
2099static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002100state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002101{
2102 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002103 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002104
NeilBrownb2d444d2005-11-08 21:39:31 -08002105 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002106 len+= sprintf(page+len, "%sfaulty",sep);
2107 sep = ",";
2108 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002109 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002110 len += sprintf(page+len, "%sin_sync",sep);
2111 sep = ",";
2112 }
NeilBrownf6556752006-06-26 00:28:01 -07002113 if (test_bit(WriteMostly, &rdev->flags)) {
2114 len += sprintf(page+len, "%swrite_mostly",sep);
2115 sep = ",";
2116 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002117 if (test_bit(Blocked, &rdev->flags)) {
2118 len += sprintf(page+len, "%sblocked", sep);
2119 sep = ",";
2120 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002121 if (!test_bit(Faulty, &rdev->flags) &&
2122 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002123 len += sprintf(page+len, "%sspare", sep);
2124 sep = ",";
2125 }
2126 return len+sprintf(page+len, "\n");
2127}
2128
NeilBrown45dc2de2006-06-26 00:27:58 -07002129static ssize_t
2130state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2131{
2132 /* can write
2133 * faulty - simulates and error
2134 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002135 * writemostly - sets write_mostly
2136 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002137 * blocked - sets the Blocked flag
2138 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002139 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002140 */
2141 int err = -EINVAL;
2142 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2143 md_error(rdev->mddev, rdev);
2144 err = 0;
2145 } else if (cmd_match(buf, "remove")) {
2146 if (rdev->raid_disk >= 0)
2147 err = -EBUSY;
2148 else {
2149 mddev_t *mddev = rdev->mddev;
2150 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002151 if (mddev->pers)
2152 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002153 md_new_event(mddev);
2154 err = 0;
2155 }
NeilBrownf6556752006-06-26 00:28:01 -07002156 } else if (cmd_match(buf, "writemostly")) {
2157 set_bit(WriteMostly, &rdev->flags);
2158 err = 0;
2159 } else if (cmd_match(buf, "-writemostly")) {
2160 clear_bit(WriteMostly, &rdev->flags);
2161 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002162 } else if (cmd_match(buf, "blocked")) {
2163 set_bit(Blocked, &rdev->flags);
2164 err = 0;
2165 } else if (cmd_match(buf, "-blocked")) {
2166 clear_bit(Blocked, &rdev->flags);
2167 wake_up(&rdev->blocked_wait);
2168 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2169 md_wakeup_thread(rdev->mddev->thread);
2170
2171 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002172 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2173 set_bit(In_sync, &rdev->flags);
2174 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002175 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002176 if (!err && rdev->sysfs_state)
2177 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002178 return err ? err : len;
2179}
NeilBrown80ca3a42006-07-10 04:44:18 -07002180static struct rdev_sysfs_entry rdev_state =
2181__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002182
2183static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002184errors_show(mdk_rdev_t *rdev, char *page)
2185{
2186 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2187}
2188
2189static ssize_t
2190errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2191{
2192 char *e;
2193 unsigned long n = simple_strtoul(buf, &e, 10);
2194 if (*buf && (*e == 0 || *e == '\n')) {
2195 atomic_set(&rdev->corrected_errors, n);
2196 return len;
2197 }
2198 return -EINVAL;
2199}
2200static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002201__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002202
NeilBrown014236d2006-01-06 00:20:55 -08002203static ssize_t
2204slot_show(mdk_rdev_t *rdev, char *page)
2205{
2206 if (rdev->raid_disk < 0)
2207 return sprintf(page, "none\n");
2208 else
2209 return sprintf(page, "%d\n", rdev->raid_disk);
2210}
2211
2212static ssize_t
2213slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2214{
2215 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002216 int err;
2217 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002218 int slot = simple_strtoul(buf, &e, 10);
2219 if (strncmp(buf, "none", 4)==0)
2220 slot = -1;
2221 else if (e==buf || (*e && *e!= '\n'))
2222 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002223 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002224 /* Setting 'slot' on an active array requires also
2225 * updating the 'rd%d' link, and communicating
2226 * with the personality with ->hot_*_disk.
2227 * For now we only support removing
2228 * failed/spare devices. This normally happens automatically,
2229 * but not when the metadata is externally managed.
2230 */
NeilBrownc303da62008-02-06 01:39:51 -08002231 if (rdev->raid_disk == -1)
2232 return -EEXIST;
2233 /* personality does all needed checks */
2234 if (rdev->mddev->pers->hot_add_disk == NULL)
2235 return -EINVAL;
2236 err = rdev->mddev->pers->
2237 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2238 if (err)
2239 return err;
2240 sprintf(nm, "rd%d", rdev->raid_disk);
2241 sysfs_remove_link(&rdev->mddev->kobj, nm);
2242 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2243 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002244 } else if (rdev->mddev->pers) {
2245 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002246 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002247 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002248 */
2249
2250 if (rdev->raid_disk != -1)
2251 return -EBUSY;
2252
2253 if (rdev->mddev->pers->hot_add_disk == NULL)
2254 return -EINVAL;
2255
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002256 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002257 if (rdev2->raid_disk == slot)
2258 return -EEXIST;
2259
2260 rdev->raid_disk = slot;
2261 if (test_bit(In_sync, &rdev->flags))
2262 rdev->saved_raid_disk = slot;
2263 else
2264 rdev->saved_raid_disk = -1;
2265 err = rdev->mddev->pers->
2266 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002267 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002268 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002269 return err;
Neil Brown52664732008-06-28 08:31:44 +10002270 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002271 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002272 sprintf(nm, "rd%d", rdev->raid_disk);
2273 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2274 printk(KERN_WARNING
2275 "md: cannot register "
2276 "%s for %s\n",
2277 nm, mdname(rdev->mddev));
2278
2279 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002280 } else {
2281 if (slot >= rdev->mddev->raid_disks)
2282 return -ENOSPC;
2283 rdev->raid_disk = slot;
2284 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002285 clear_bit(Faulty, &rdev->flags);
2286 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002287 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002288 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002289 }
NeilBrown014236d2006-01-06 00:20:55 -08002290 return len;
2291}
2292
2293
2294static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002295__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002296
NeilBrown93c8cad2006-01-06 00:20:56 -08002297static ssize_t
2298offset_show(mdk_rdev_t *rdev, char *page)
2299{
Andrew Morton6961ece2006-01-06 00:20:59 -08002300 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002301}
2302
2303static ssize_t
2304offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2305{
2306 char *e;
2307 unsigned long long offset = simple_strtoull(buf, &e, 10);
2308 if (e==buf || (*e && *e != '\n'))
2309 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002310 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002311 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002312 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002313 /* Must set offset before size, so overlap checks
2314 * can be sane */
2315 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002316 rdev->data_offset = offset;
2317 return len;
2318}
2319
2320static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002321__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002322
NeilBrown83303b62006-01-06 00:21:06 -08002323static ssize_t
2324rdev_size_show(mdk_rdev_t *rdev, char *page)
2325{
Andre Nolldd8ac332009-03-31 14:33:13 +11002326 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002327}
2328
NeilBrownc5d79ad2008-02-06 01:39:54 -08002329static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2330{
2331 /* check if two start/length pairs overlap */
2332 if (s1+l1 <= s2)
2333 return 0;
2334 if (s2+l2 <= s1)
2335 return 0;
2336 return 1;
2337}
2338
Dan Williamsb522adc2009-03-31 15:00:31 +11002339static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2340{
2341 unsigned long long blocks;
2342 sector_t new;
2343
2344 if (strict_strtoull(buf, 10, &blocks) < 0)
2345 return -EINVAL;
2346
2347 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2348 return -EINVAL; /* sector conversion overflow */
2349
2350 new = blocks * 2;
2351 if (new != blocks * 2)
2352 return -EINVAL; /* unsigned long long to sector_t overflow */
2353
2354 *sectors = new;
2355 return 0;
2356}
2357
NeilBrown83303b62006-01-06 00:21:06 -08002358static ssize_t
2359rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2360{
NeilBrown27c529b2008-03-04 14:29:33 -08002361 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002362 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002363 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002364
Dan Williamsb522adc2009-03-31 15:00:31 +11002365 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002366 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002367 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002368 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002369 sectors = super_types[my_mddev->major_version].
2370 rdev_size_change(rdev, sectors);
2371 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002372 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002373 } else if (!sectors)
2374 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2375 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002376 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002377 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002378 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002379
Andre Nolldd8ac332009-03-31 14:33:13 +11002380 rdev->sectors = sectors;
2381 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002382 /* need to check that all other rdevs with the same ->bdev
2383 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002384 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002385 * we have to change it back, we will have the lock again.
2386 */
2387 mddev_t *mddev;
2388 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002389 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002390
NeilBrown27c529b2008-03-04 14:29:33 -08002391 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002392 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002393 mdk_rdev_t *rdev2;
2394
2395 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002396 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002397 if (test_bit(AllReserved, &rdev2->flags) ||
2398 (rdev->bdev == rdev2->bdev &&
2399 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002400 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002401 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002402 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002403 overlap = 1;
2404 break;
2405 }
2406 mddev_unlock(mddev);
2407 if (overlap) {
2408 mddev_put(mddev);
2409 break;
2410 }
2411 }
NeilBrown27c529b2008-03-04 14:29:33 -08002412 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002413 if (overlap) {
2414 /* Someone else could have slipped in a size
2415 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002416 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002417 * safe, and trust userspace not to race with
2418 * itself
2419 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002420 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002421 return -EBUSY;
2422 }
2423 }
NeilBrown83303b62006-01-06 00:21:06 -08002424 return len;
2425}
2426
2427static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002428__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002429
NeilBrown86e6ffd2005-11-08 21:39:24 -08002430static struct attribute *rdev_default_attrs[] = {
2431 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002432 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002433 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002434 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002435 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002436 NULL,
2437};
2438static ssize_t
2439rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2440{
2441 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2442 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002443 mddev_t *mddev = rdev->mddev;
2444 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002445
2446 if (!entry->show)
2447 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002448
2449 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2450 if (!rv) {
2451 if (rdev->mddev == NULL)
2452 rv = -EBUSY;
2453 else
2454 rv = entry->show(rdev, page);
2455 mddev_unlock(mddev);
2456 }
2457 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002458}
2459
2460static ssize_t
2461rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2462 const char *page, size_t length)
2463{
2464 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2465 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002466 ssize_t rv;
2467 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002468
2469 if (!entry->store)
2470 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002471 if (!capable(CAP_SYS_ADMIN))
2472 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002473 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002474 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002475 if (rdev->mddev == NULL)
2476 rv = -EBUSY;
2477 else
2478 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002479 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002480 }
2481 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002482}
2483
2484static void rdev_free(struct kobject *ko)
2485{
2486 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2487 kfree(rdev);
2488}
2489static struct sysfs_ops rdev_sysfs_ops = {
2490 .show = rdev_attr_show,
2491 .store = rdev_attr_store,
2492};
2493static struct kobj_type rdev_ktype = {
2494 .release = rdev_free,
2495 .sysfs_ops = &rdev_sysfs_ops,
2496 .default_attrs = rdev_default_attrs,
2497};
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499/*
2500 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2501 *
2502 * mark the device faulty if:
2503 *
2504 * - the device is nonexistent (zero size)
2505 * - the device has no valid superblock
2506 *
2507 * a faulty rdev _never_ has rdev->sb set.
2508 */
2509static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2510{
2511 char b[BDEVNAME_SIZE];
2512 int err;
2513 mdk_rdev_t *rdev;
2514 sector_t size;
2515
NeilBrown9ffae0c2006-01-06 00:20:32 -08002516 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (!rdev) {
2518 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2519 return ERR_PTR(-ENOMEM);
2520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 if ((err = alloc_disk_sb(rdev)))
2523 goto abort_free;
2524
NeilBrownc5d79ad2008-02-06 01:39:54 -08002525 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (err)
2527 goto abort_free;
2528
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002529 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002532 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002533 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002534 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002536 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002538 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002539 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
2541 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2542 if (!size) {
2543 printk(KERN_WARNING
2544 "md: %s has zero or unknown size, marking faulty!\n",
2545 bdevname(rdev->bdev,b));
2546 err = -EINVAL;
2547 goto abort_free;
2548 }
2549
2550 if (super_format >= 0) {
2551 err = super_types[super_format].
2552 load_super(rdev, NULL, super_minor);
2553 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002554 printk(KERN_WARNING
2555 "md: %s does not have a valid v%d.%d "
2556 "superblock, not importing!\n",
2557 bdevname(rdev->bdev,b),
2558 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 goto abort_free;
2560 }
2561 if (err < 0) {
2562 printk(KERN_WARNING
2563 "md: could not read %s's sb, not importing!\n",
2564 bdevname(rdev->bdev,b));
2565 goto abort_free;
2566 }
2567 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002570 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572 return rdev;
2573
2574abort_free:
2575 if (rdev->sb_page) {
2576 if (rdev->bdev)
2577 unlock_rdev(rdev);
2578 free_disk_sb(rdev);
2579 }
2580 kfree(rdev);
2581 return ERR_PTR(err);
2582}
2583
2584/*
2585 * Check a full RAID array for plausibility
2586 */
2587
2588
NeilBrowna757e642005-04-16 15:26:42 -07002589static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002592 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 char b[BDEVNAME_SIZE];
2594
2595 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002596 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 switch (super_types[mddev->major_version].
2598 load_super(rdev, freshest, mddev->minor_version)) {
2599 case 1:
2600 freshest = rdev;
2601 break;
2602 case 0:
2603 break;
2604 default:
2605 printk( KERN_ERR \
2606 "md: fatal superblock inconsistency in %s"
2607 " -- removing from array\n",
2608 bdevname(rdev->bdev,b));
2609 kick_rdev_from_array(rdev);
2610 }
2611
2612
2613 super_types[mddev->major_version].
2614 validate_super(mddev, freshest);
2615
2616 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002617 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002618 if (rdev->desc_nr >= mddev->max_disks ||
2619 i > mddev->max_disks) {
2620 printk(KERN_WARNING
2621 "md: %s: %s: only %d devices permitted\n",
2622 mdname(mddev), bdevname(rdev->bdev, b),
2623 mddev->max_disks);
2624 kick_rdev_from_array(rdev);
2625 continue;
2626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (rdev != freshest)
2628 if (super_types[mddev->major_version].
2629 validate_super(mddev, rdev)) {
2630 printk(KERN_WARNING "md: kicking non-fresh %s"
2631 " from array!\n",
2632 bdevname(rdev->bdev,b));
2633 kick_rdev_from_array(rdev);
2634 continue;
2635 }
2636 if (mddev->level == LEVEL_MULTIPATH) {
2637 rdev->desc_nr = i++;
2638 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002639 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002640 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002641 rdev->raid_disk = -1;
2642 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
2644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
NeilBrown19052c02008-08-05 15:54:13 +10002647static void md_safemode_timeout(unsigned long data);
2648
NeilBrowneae17012005-11-08 21:39:23 -08002649static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002650safe_delay_show(mddev_t *mddev, char *page)
2651{
2652 int msec = (mddev->safemode_delay*1000)/HZ;
2653 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2654}
2655static ssize_t
2656safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2657{
2658 int scale=1;
2659 int dot=0;
2660 int i;
2661 unsigned long msec;
2662 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002663
NeilBrown16f17b32006-06-26 00:27:37 -07002664 /* remove a period, and count digits after it */
2665 if (len >= sizeof(buf))
2666 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002667 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002668 for (i=0; i<len; i++) {
2669 if (dot) {
2670 if (isdigit(buf[i])) {
2671 buf[i-1] = buf[i];
2672 scale *= 10;
2673 }
2674 buf[i] = 0;
2675 } else if (buf[i] == '.') {
2676 dot=1;
2677 buf[i] = 0;
2678 }
2679 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002680 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002681 return -EINVAL;
2682 msec = (msec * 1000) / scale;
2683 if (msec == 0)
2684 mddev->safemode_delay = 0;
2685 else {
NeilBrown19052c02008-08-05 15:54:13 +10002686 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002687 mddev->safemode_delay = (msec*HZ)/1000;
2688 if (mddev->safemode_delay == 0)
2689 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002690 if (mddev->safemode_delay < old_delay)
2691 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002692 }
2693 return len;
2694}
2695static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002696__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002697
2698static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002699level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002700{
NeilBrown2604b702006-01-06 00:20:36 -08002701 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002702 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002703 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002704 else if (mddev->clevel[0])
2705 return sprintf(page, "%s\n", mddev->clevel);
2706 else if (mddev->level != LEVEL_NONE)
2707 return sprintf(page, "%d\n", mddev->level);
2708 else
2709 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002710}
2711
NeilBrownd9d166c2006-01-06 00:20:51 -08002712static ssize_t
2713level_store(mddev_t *mddev, const char *buf, size_t len)
2714{
NeilBrown245f46c2009-03-31 14:39:39 +11002715 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002716 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002717 struct mdk_personality *pers;
2718 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002719 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002720
2721 if (mddev->pers == NULL) {
2722 if (len == 0)
2723 return 0;
2724 if (len >= sizeof(mddev->clevel))
2725 return -ENOSPC;
2726 strncpy(mddev->clevel, buf, len);
2727 if (mddev->clevel[len-1] == '\n')
2728 len--;
2729 mddev->clevel[len] = 0;
2730 mddev->level = LEVEL_NONE;
2731 return rv;
2732 }
2733
2734 /* request to change the personality. Need to ensure:
2735 * - array is not engaged in resync/recovery/reshape
2736 * - old personality can be suspended
2737 * - new personality will access other array.
2738 */
2739
2740 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002741 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002742
2743 if (!mddev->pers->quiesce) {
2744 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2745 mdname(mddev), mddev->pers->name);
2746 return -EINVAL;
2747 }
2748
2749 /* Now find the new personality */
2750 if (len == 0 || len >= sizeof(level))
2751 return -EINVAL;
2752 strncpy(level, buf, len);
2753 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002754 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002755 level[len] = 0;
2756
2757 request_module("md-%s", level);
2758 spin_lock(&pers_lock);
2759 pers = find_pers(LEVEL_NONE, level);
2760 if (!pers || !try_module_get(pers->owner)) {
2761 spin_unlock(&pers_lock);
2762 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2763 return -EINVAL;
2764 }
2765 spin_unlock(&pers_lock);
2766
2767 if (pers == mddev->pers) {
2768 /* Nothing to do! */
2769 module_put(pers->owner);
2770 return rv;
2771 }
2772 if (!pers->takeover) {
2773 module_put(pers->owner);
2774 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2775 mdname(mddev), level);
2776 return -EINVAL;
2777 }
2778
2779 /* ->takeover must set new_* and/or delta_disks
2780 * if it succeeds, and may set them when it fails.
2781 */
2782 priv = pers->takeover(mddev);
2783 if (IS_ERR(priv)) {
2784 mddev->new_level = mddev->level;
2785 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002786 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002787 mddev->raid_disks -= mddev->delta_disks;
2788 mddev->delta_disks = 0;
2789 module_put(pers->owner);
2790 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2791 mdname(mddev), level);
2792 return PTR_ERR(priv);
2793 }
2794
2795 /* Looks like we have a winner */
2796 mddev_suspend(mddev);
2797 mddev->pers->stop(mddev);
2798 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002799 /* Invalidate devices that are now superfluous */
2800 list_for_each_entry(rdev, &mddev->disks, same_set)
2801 if (rdev->raid_disk >= mddev->raid_disks) {
2802 rdev->raid_disk = -1;
2803 clear_bit(In_sync, &rdev->flags);
2804 }
NeilBrown245f46c2009-03-31 14:39:39 +11002805 mddev->pers = pers;
2806 mddev->private = priv;
2807 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2808 mddev->level = mddev->new_level;
2809 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002810 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002811 mddev->delta_disks = 0;
2812 pers->run(mddev);
2813 mddev_resume(mddev);
2814 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2815 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2816 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002817 return rv;
2818}
2819
2820static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002821__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002822
NeilBrownd4dbd022006-06-26 00:27:59 -07002823
2824static ssize_t
2825layout_show(mddev_t *mddev, char *page)
2826{
2827 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002828 if (mddev->reshape_position != MaxSector &&
2829 mddev->layout != mddev->new_layout)
2830 return sprintf(page, "%d (%d)\n",
2831 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002832 return sprintf(page, "%d\n", mddev->layout);
2833}
2834
2835static ssize_t
2836layout_store(mddev_t *mddev, const char *buf, size_t len)
2837{
2838 char *e;
2839 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002840
2841 if (!*buf || (*e && *e != '\n'))
2842 return -EINVAL;
2843
NeilBrownb3546032009-03-31 14:56:41 +11002844 if (mddev->pers) {
2845 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002846 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002847 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002848 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002849 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002850 if (err) {
2851 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002852 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002853 }
NeilBrownb3546032009-03-31 14:56:41 +11002854 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002855 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002856 if (mddev->reshape_position == MaxSector)
2857 mddev->layout = n;
2858 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002859 return len;
2860}
2861static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002862__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002863
2864
NeilBrowneae17012005-11-08 21:39:23 -08002865static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002866raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002867{
NeilBrownbb636542005-11-08 21:39:45 -08002868 if (mddev->raid_disks == 0)
2869 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002870 if (mddev->reshape_position != MaxSector &&
2871 mddev->delta_disks != 0)
2872 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2873 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002874 return sprintf(page, "%d\n", mddev->raid_disks);
2875}
2876
NeilBrownda943b92006-01-06 00:20:54 -08002877static int update_raid_disks(mddev_t *mddev, int raid_disks);
2878
2879static ssize_t
2880raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2881{
NeilBrownda943b92006-01-06 00:20:54 -08002882 char *e;
2883 int rv = 0;
2884 unsigned long n = simple_strtoul(buf, &e, 10);
2885
2886 if (!*buf || (*e && *e != '\n'))
2887 return -EINVAL;
2888
2889 if (mddev->pers)
2890 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002891 else if (mddev->reshape_position != MaxSector) {
2892 int olddisks = mddev->raid_disks - mddev->delta_disks;
2893 mddev->delta_disks = n - olddisks;
2894 mddev->raid_disks = n;
2895 } else
NeilBrownda943b92006-01-06 00:20:54 -08002896 mddev->raid_disks = n;
2897 return rv ? rv : len;
2898}
2899static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002900__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002901
NeilBrown24dd4692005-11-08 21:39:26 -08002902static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002903chunk_size_show(mddev_t *mddev, char *page)
2904{
NeilBrown08a02ec2007-05-09 02:35:38 -07002905 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10002906 mddev->chunk_sectors != mddev->new_chunk_sectors)
2907 return sprintf(page, "%d (%d)\n",
2908 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10002909 mddev->chunk_sectors << 9);
2910 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08002911}
2912
2913static ssize_t
2914chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2915{
NeilBrown3b343802006-01-06 00:20:47 -08002916 char *e;
2917 unsigned long n = simple_strtoul(buf, &e, 10);
2918
NeilBrown3b343802006-01-06 00:20:47 -08002919 if (!*buf || (*e && *e != '\n'))
2920 return -EINVAL;
2921
NeilBrownb3546032009-03-31 14:56:41 +11002922 if (mddev->pers) {
2923 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002924 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002925 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002926 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10002927 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002928 if (err) {
2929 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11002930 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002931 }
NeilBrownb3546032009-03-31 14:56:41 +11002932 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10002933 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002934 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10002935 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002936 }
NeilBrown3b343802006-01-06 00:20:47 -08002937 return len;
2938}
2939static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002940__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002941
NeilBrowna94213b2006-06-26 00:28:00 -07002942static ssize_t
2943resync_start_show(mddev_t *mddev, char *page)
2944{
NeilBrownd1a7c502009-03-31 15:24:32 +11002945 if (mddev->recovery_cp == MaxSector)
2946 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002947 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2948}
2949
2950static ssize_t
2951resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2952{
NeilBrowna94213b2006-06-26 00:28:00 -07002953 char *e;
2954 unsigned long long n = simple_strtoull(buf, &e, 10);
2955
2956 if (mddev->pers)
2957 return -EBUSY;
2958 if (!*buf || (*e && *e != '\n'))
2959 return -EINVAL;
2960
2961 mddev->recovery_cp = n;
2962 return len;
2963}
2964static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002965__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002966
NeilBrown9e653b62006-06-26 00:27:58 -07002967/*
2968 * The array state can be:
2969 *
2970 * clear
2971 * No devices, no size, no level
2972 * Equivalent to STOP_ARRAY ioctl
2973 * inactive
2974 * May have some settings, but array is not active
2975 * all IO results in error
2976 * When written, doesn't tear down array, but just stops it
2977 * suspended (not supported yet)
2978 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002979 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002980 * readonly
2981 * no resync can happen. no superblocks get written.
2982 * write requests fail
2983 * read-auto
2984 * like readonly, but behaves like 'clean' on a write request.
2985 *
2986 * clean - no pending writes, but otherwise active.
2987 * When written to inactive array, starts without resync
2988 * If a write request arrives then
2989 * if metadata is known, mark 'dirty' and switch to 'active'.
2990 * if not known, block and switch to write-pending
2991 * If written to an active array that has pending writes, then fails.
2992 * active
2993 * fully active: IO and resync can be happening.
2994 * When written to inactive array, starts with resync
2995 *
2996 * write-pending
2997 * clean, but writes are blocked waiting for 'active' to be written.
2998 *
2999 * active-idle
3000 * like active, but no writes have been seen for a while (100msec).
3001 *
3002 */
3003enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3004 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003005static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003006 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3007 "write-pending", "active-idle", NULL };
3008
3009static int match_word(const char *word, char **list)
3010{
3011 int n;
3012 for (n=0; list[n]; n++)
3013 if (cmd_match(word, list[n]))
3014 break;
3015 return n;
3016}
3017
3018static ssize_t
3019array_state_show(mddev_t *mddev, char *page)
3020{
3021 enum array_state st = inactive;
3022
3023 if (mddev->pers)
3024 switch(mddev->ro) {
3025 case 1:
3026 st = readonly;
3027 break;
3028 case 2:
3029 st = read_auto;
3030 break;
3031 case 0:
3032 if (mddev->in_sync)
3033 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003034 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3035 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003036 else if (mddev->safemode)
3037 st = active_idle;
3038 else
3039 st = active;
3040 }
3041 else {
3042 if (list_empty(&mddev->disks) &&
3043 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003044 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003045 st = clear;
3046 else
3047 st = inactive;
3048 }
3049 return sprintf(page, "%s\n", array_states[st]);
3050}
3051
Neil Browndf5b20c2008-07-11 22:02:22 +10003052static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003053static int do_md_run(mddev_t * mddev);
3054static int restart_array(mddev_t *mddev);
3055
3056static ssize_t
3057array_state_store(mddev_t *mddev, const char *buf, size_t len)
3058{
3059 int err = -EINVAL;
3060 enum array_state st = match_word(buf, array_states);
3061 switch(st) {
3062 case bad_word:
3063 break;
3064 case clear:
3065 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003066 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003067 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003068 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003069 break;
3070 case inactive:
3071 /* stopping an active array */
3072 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003073 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003074 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003075 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003076 } else
3077 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003078 break;
3079 case suspended:
3080 break; /* not supported yet */
3081 case readonly:
3082 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003083 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003084 else {
3085 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003086 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003087 err = do_md_run(mddev);
3088 }
3089 break;
3090 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003091 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003092 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003093 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003094 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003095 err = restart_array(mddev);
3096 if (err == 0) {
3097 mddev->ro = 2;
3098 set_disk_ro(mddev->gendisk, 0);
3099 }
NeilBrown9e653b62006-06-26 00:27:58 -07003100 } else {
3101 mddev->ro = 2;
3102 err = do_md_run(mddev);
3103 }
3104 break;
3105 case clean:
3106 if (mddev->pers) {
3107 restart_array(mddev);
3108 spin_lock_irq(&mddev->write_lock);
3109 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003110 if (mddev->in_sync == 0) {
3111 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003112 if (mddev->safemode == 1)
3113 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003114 if (mddev->persistent)
3115 set_bit(MD_CHANGE_CLEAN,
3116 &mddev->flags);
3117 }
3118 err = 0;
3119 } else
3120 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003121 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003122 } else
3123 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003124 break;
3125 case active:
3126 if (mddev->pers) {
3127 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003128 if (mddev->external)
3129 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003130 wake_up(&mddev->sb_wait);
3131 err = 0;
3132 } else {
3133 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003134 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003135 err = do_md_run(mddev);
3136 }
3137 break;
3138 case write_pending:
3139 case active_idle:
3140 /* these cannot be set */
3141 break;
3142 }
3143 if (err)
3144 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003145 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003146 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003147 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003148 }
NeilBrown9e653b62006-06-26 00:27:58 -07003149}
NeilBrown80ca3a42006-07-10 04:44:18 -07003150static struct md_sysfs_entry md_array_state =
3151__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003152
NeilBrown6d7ff732006-01-06 00:21:16 -08003153static ssize_t
3154null_show(mddev_t *mddev, char *page)
3155{
3156 return -EINVAL;
3157}
3158
3159static ssize_t
3160new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3161{
3162 /* buf must be %d:%d\n? giving major and minor numbers */
3163 /* The new device is added to the array.
3164 * If the array has a persistent superblock, we read the
3165 * superblock to initialise info and check validity.
3166 * Otherwise, only checking done is that in bind_rdev_to_array,
3167 * which mainly checks size.
3168 */
3169 char *e;
3170 int major = simple_strtoul(buf, &e, 10);
3171 int minor;
3172 dev_t dev;
3173 mdk_rdev_t *rdev;
3174 int err;
3175
3176 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3177 return -EINVAL;
3178 minor = simple_strtoul(e+1, &e, 10);
3179 if (*e && *e != '\n')
3180 return -EINVAL;
3181 dev = MKDEV(major, minor);
3182 if (major != MAJOR(dev) ||
3183 minor != MINOR(dev))
3184 return -EOVERFLOW;
3185
3186
3187 if (mddev->persistent) {
3188 rdev = md_import_device(dev, mddev->major_version,
3189 mddev->minor_version);
3190 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3191 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3192 mdk_rdev_t, same_set);
3193 err = super_types[mddev->major_version]
3194 .load_super(rdev, rdev0, mddev->minor_version);
3195 if (err < 0)
3196 goto out;
3197 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003198 } else if (mddev->external)
3199 rdev = md_import_device(dev, -2, -1);
3200 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003201 rdev = md_import_device(dev, -1, -1);
3202
3203 if (IS_ERR(rdev))
3204 return PTR_ERR(rdev);
3205 err = bind_rdev_to_array(rdev, mddev);
3206 out:
3207 if (err)
3208 export_rdev(rdev);
3209 return err ? err : len;
3210}
3211
3212static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003213__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003214
3215static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003216bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3217{
3218 char *end;
3219 unsigned long chunk, end_chunk;
3220
3221 if (!mddev->bitmap)
3222 goto out;
3223 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3224 while (*buf) {
3225 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3226 if (buf == end) break;
3227 if (*end == '-') { /* range */
3228 buf = end + 1;
3229 end_chunk = simple_strtoul(buf, &end, 0);
3230 if (buf == end) break;
3231 }
3232 if (*end && !isspace(*end)) break;
3233 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3234 buf = end;
3235 while (isspace(*buf)) buf++;
3236 }
3237 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3238out:
3239 return len;
3240}
3241
3242static struct md_sysfs_entry md_bitmap =
3243__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3244
3245static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003246size_show(mddev_t *mddev, char *page)
3247{
Andre Noll58c0fed2009-03-31 14:33:13 +11003248 return sprintf(page, "%llu\n",
3249 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003250}
3251
Andre Nolld71f9f82008-07-11 22:02:22 +10003252static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003253
3254static ssize_t
3255size_store(mddev_t *mddev, const char *buf, size_t len)
3256{
3257 /* If array is inactive, we can reduce the component size, but
3258 * not increase it (except from 0).
3259 * If array is active, we can try an on-line resize
3260 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003261 sector_t sectors;
3262 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003263
Andre Noll58c0fed2009-03-31 14:33:13 +11003264 if (err < 0)
3265 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003266 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003267 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003268 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003269 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003270 if (mddev->dev_sectors == 0 ||
3271 mddev->dev_sectors > sectors)
3272 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003273 else
3274 err = -ENOSPC;
3275 }
3276 return err ? err : len;
3277}
3278
3279static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003280__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003281
NeilBrown8bb93aa2006-01-06 00:20:50 -08003282
3283/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003284 * This is one of
3285 * 'none' for arrays with no metadata (good luck...)
3286 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003287 * or N.M for internally known formats
3288 */
3289static ssize_t
3290metadata_show(mddev_t *mddev, char *page)
3291{
3292 if (mddev->persistent)
3293 return sprintf(page, "%d.%d\n",
3294 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003295 else if (mddev->external)
3296 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003297 else
3298 return sprintf(page, "none\n");
3299}
3300
3301static ssize_t
3302metadata_store(mddev_t *mddev, const char *buf, size_t len)
3303{
3304 int major, minor;
3305 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003306 /* Changing the details of 'external' metadata is
3307 * always permitted. Otherwise there must be
3308 * no devices attached to the array.
3309 */
3310 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3311 ;
3312 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003313 return -EBUSY;
3314
3315 if (cmd_match(buf, "none")) {
3316 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003317 mddev->external = 0;
3318 mddev->major_version = 0;
3319 mddev->minor_version = 90;
3320 return len;
3321 }
3322 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003323 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003324 if (namelen >= sizeof(mddev->metadata_type))
3325 namelen = sizeof(mddev->metadata_type)-1;
3326 strncpy(mddev->metadata_type, buf+9, namelen);
3327 mddev->metadata_type[namelen] = 0;
3328 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3329 mddev->metadata_type[--namelen] = 0;
3330 mddev->persistent = 0;
3331 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003332 mddev->major_version = 0;
3333 mddev->minor_version = 90;
3334 return len;
3335 }
3336 major = simple_strtoul(buf, &e, 10);
3337 if (e==buf || *e != '.')
3338 return -EINVAL;
3339 buf = e+1;
3340 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003341 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003342 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003343 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003344 return -ENOENT;
3345 mddev->major_version = major;
3346 mddev->minor_version = minor;
3347 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003348 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003349 return len;
3350}
3351
3352static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003353__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003354
NeilBrowna35b0d62006-01-06 00:20:49 -08003355static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003356action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003357{
NeilBrown7eec3142005-11-08 21:39:44 -08003358 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003359 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3360 type = "frozen";
3361 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003362 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003363 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3364 type = "reshape";
3365 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003366 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3367 type = "resync";
3368 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3369 type = "check";
3370 else
3371 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003372 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003373 type = "recover";
3374 }
3375 return sprintf(page, "%s\n", type);
3376}
3377
3378static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003379action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003380{
NeilBrown7eec3142005-11-08 21:39:44 -08003381 if (!mddev->pers || !mddev->pers->sync_request)
3382 return -EINVAL;
3383
NeilBrownb6a9ce62009-05-26 09:41:17 +10003384 if (cmd_match(page, "frozen"))
3385 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3386 else
3387 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3388
3389 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003390 if (mddev->sync_thread) {
3391 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3392 md_unregister_thread(mddev->sync_thread);
3393 mddev->sync_thread = NULL;
3394 mddev->recovery = 0;
3395 }
NeilBrown03c902e2006-01-06 00:20:46 -08003396 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3397 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003398 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003399 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003400 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003401 else if (cmd_match(page, "recover")) {
3402 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3403 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3404 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003405 int err;
3406 if (mddev->pers->start_reshape == NULL)
3407 return -EINVAL;
3408 err = mddev->pers->start_reshape(mddev);
3409 if (err)
3410 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003411 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003412 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003413 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003414 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003415 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003416 return -EINVAL;
3417 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3418 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003419 }
NeilBrown03c902e2006-01-06 00:20:46 -08003420 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003421 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003422 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003423 return len;
3424}
3425
NeilBrown9d888832005-11-08 21:39:26 -08003426static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003427mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003428{
3429 return sprintf(page, "%llu\n",
3430 (unsigned long long) mddev->resync_mismatches);
3431}
3432
NeilBrown80ca3a42006-07-10 04:44:18 -07003433static struct md_sysfs_entry md_scan_mode =
3434__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003435
NeilBrown96de1e62005-11-08 21:39:39 -08003436
NeilBrown80ca3a42006-07-10 04:44:18 -07003437static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003438
NeilBrown88202a02006-01-06 00:21:36 -08003439static ssize_t
3440sync_min_show(mddev_t *mddev, char *page)
3441{
3442 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3443 mddev->sync_speed_min ? "local": "system");
3444}
3445
3446static ssize_t
3447sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3448{
3449 int min;
3450 char *e;
3451 if (strncmp(buf, "system", 6)==0) {
3452 mddev->sync_speed_min = 0;
3453 return len;
3454 }
3455 min = simple_strtoul(buf, &e, 10);
3456 if (buf == e || (*e && *e != '\n') || min <= 0)
3457 return -EINVAL;
3458 mddev->sync_speed_min = min;
3459 return len;
3460}
3461
3462static struct md_sysfs_entry md_sync_min =
3463__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3464
3465static ssize_t
3466sync_max_show(mddev_t *mddev, char *page)
3467{
3468 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3469 mddev->sync_speed_max ? "local": "system");
3470}
3471
3472static ssize_t
3473sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3474{
3475 int max;
3476 char *e;
3477 if (strncmp(buf, "system", 6)==0) {
3478 mddev->sync_speed_max = 0;
3479 return len;
3480 }
3481 max = simple_strtoul(buf, &e, 10);
3482 if (buf == e || (*e && *e != '\n') || max <= 0)
3483 return -EINVAL;
3484 mddev->sync_speed_max = max;
3485 return len;
3486}
3487
3488static struct md_sysfs_entry md_sync_max =
3489__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3490
Iustin Popd7f3d292007-10-16 23:30:54 -07003491static ssize_t
3492degraded_show(mddev_t *mddev, char *page)
3493{
3494 return sprintf(page, "%d\n", mddev->degraded);
3495}
3496static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003497
3498static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003499sync_force_parallel_show(mddev_t *mddev, char *page)
3500{
3501 return sprintf(page, "%d\n", mddev->parallel_resync);
3502}
3503
3504static ssize_t
3505sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3506{
3507 long n;
3508
3509 if (strict_strtol(buf, 10, &n))
3510 return -EINVAL;
3511
3512 if (n != 0 && n != 1)
3513 return -EINVAL;
3514
3515 mddev->parallel_resync = n;
3516
3517 if (mddev->sync_thread)
3518 wake_up(&resync_wait);
3519
3520 return len;
3521}
3522
3523/* force parallel resync, even with shared block devices */
3524static struct md_sysfs_entry md_sync_force_parallel =
3525__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3526 sync_force_parallel_show, sync_force_parallel_store);
3527
3528static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003529sync_speed_show(mddev_t *mddev, char *page)
3530{
3531 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003532 if (mddev->curr_resync == 0)
3533 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003534 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3535 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003536 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003537 db = resync - mddev->resync_mark_cnt;
3538 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003539}
3540
NeilBrown80ca3a42006-07-10 04:44:18 -07003541static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003542
3543static ssize_t
3544sync_completed_show(mddev_t *mddev, char *page)
3545{
Andre Noll58c0fed2009-03-31 14:33:13 +11003546 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003547
NeilBrownacb180b2009-04-14 16:28:34 +10003548 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3549 return sprintf(page, "none\n");
3550
NeilBrown88202a02006-01-06 00:21:36 -08003551 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003552 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003553 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003554 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003555
NeilBrownacb180b2009-04-14 16:28:34 +10003556 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003557 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003558}
3559
NeilBrown80ca3a42006-07-10 04:44:18 -07003560static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003561
NeilBrowne464eaf2006-03-27 01:18:14 -08003562static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003563min_sync_show(mddev_t *mddev, char *page)
3564{
3565 return sprintf(page, "%llu\n",
3566 (unsigned long long)mddev->resync_min);
3567}
3568static ssize_t
3569min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3570{
3571 unsigned long long min;
3572 if (strict_strtoull(buf, 10, &min))
3573 return -EINVAL;
3574 if (min > mddev->resync_max)
3575 return -EINVAL;
3576 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3577 return -EBUSY;
3578
3579 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003580 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003581 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003582 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003583 return -EINVAL;
3584 }
3585 mddev->resync_min = min;
3586
3587 return len;
3588}
3589
3590static struct md_sysfs_entry md_min_sync =
3591__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3592
3593static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003594max_sync_show(mddev_t *mddev, char *page)
3595{
3596 if (mddev->resync_max == MaxSector)
3597 return sprintf(page, "max\n");
3598 else
3599 return sprintf(page, "%llu\n",
3600 (unsigned long long)mddev->resync_max);
3601}
3602static ssize_t
3603max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3604{
3605 if (strncmp(buf, "max", 3) == 0)
3606 mddev->resync_max = MaxSector;
3607 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003608 unsigned long long max;
3609 if (strict_strtoull(buf, 10, &max))
3610 return -EINVAL;
3611 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003612 return -EINVAL;
3613 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003614 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003615 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3616 return -EBUSY;
3617
3618 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003619 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003620 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003621 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003622 return -EINVAL;
3623 }
3624 mddev->resync_max = max;
3625 }
3626 wake_up(&mddev->recovery_wait);
3627 return len;
3628}
3629
3630static struct md_sysfs_entry md_max_sync =
3631__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3632
3633static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003634suspend_lo_show(mddev_t *mddev, char *page)
3635{
3636 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3637}
3638
3639static ssize_t
3640suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3641{
3642 char *e;
3643 unsigned long long new = simple_strtoull(buf, &e, 10);
3644
NeilBrownb8d966e2009-07-01 11:14:04 +10003645 if (mddev->pers == NULL ||
3646 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003647 return -EINVAL;
3648 if (buf == e || (*e && *e != '\n'))
3649 return -EINVAL;
3650 if (new >= mddev->suspend_hi ||
3651 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3652 mddev->suspend_lo = new;
3653 mddev->pers->quiesce(mddev, 2);
3654 return len;
3655 } else
3656 return -EINVAL;
3657}
3658static struct md_sysfs_entry md_suspend_lo =
3659__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3660
3661
3662static ssize_t
3663suspend_hi_show(mddev_t *mddev, char *page)
3664{
3665 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3666}
3667
3668static ssize_t
3669suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3670{
3671 char *e;
3672 unsigned long long new = simple_strtoull(buf, &e, 10);
3673
NeilBrownb8d966e2009-07-01 11:14:04 +10003674 if (mddev->pers == NULL ||
3675 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003676 return -EINVAL;
3677 if (buf == e || (*e && *e != '\n'))
3678 return -EINVAL;
3679 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3680 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3681 mddev->suspend_hi = new;
3682 mddev->pers->quiesce(mddev, 1);
3683 mddev->pers->quiesce(mddev, 0);
3684 return len;
3685 } else
3686 return -EINVAL;
3687}
3688static struct md_sysfs_entry md_suspend_hi =
3689__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3690
NeilBrown08a02ec2007-05-09 02:35:38 -07003691static ssize_t
3692reshape_position_show(mddev_t *mddev, char *page)
3693{
3694 if (mddev->reshape_position != MaxSector)
3695 return sprintf(page, "%llu\n",
3696 (unsigned long long)mddev->reshape_position);
3697 strcpy(page, "none\n");
3698 return 5;
3699}
3700
3701static ssize_t
3702reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3703{
3704 char *e;
3705 unsigned long long new = simple_strtoull(buf, &e, 10);
3706 if (mddev->pers)
3707 return -EBUSY;
3708 if (buf == e || (*e && *e != '\n'))
3709 return -EINVAL;
3710 mddev->reshape_position = new;
3711 mddev->delta_disks = 0;
3712 mddev->new_level = mddev->level;
3713 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003714 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003715 return len;
3716}
3717
3718static struct md_sysfs_entry md_reshape_position =
3719__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3720 reshape_position_store);
3721
Dan Williamsb522adc2009-03-31 15:00:31 +11003722static ssize_t
3723array_size_show(mddev_t *mddev, char *page)
3724{
3725 if (mddev->external_size)
3726 return sprintf(page, "%llu\n",
3727 (unsigned long long)mddev->array_sectors/2);
3728 else
3729 return sprintf(page, "default\n");
3730}
3731
3732static ssize_t
3733array_size_store(mddev_t *mddev, const char *buf, size_t len)
3734{
3735 sector_t sectors;
3736
3737 if (strncmp(buf, "default", 7) == 0) {
3738 if (mddev->pers)
3739 sectors = mddev->pers->size(mddev, 0, 0);
3740 else
3741 sectors = mddev->array_sectors;
3742
3743 mddev->external_size = 0;
3744 } else {
3745 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3746 return -EINVAL;
3747 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003748 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003749
3750 mddev->external_size = 1;
3751 }
3752
3753 mddev->array_sectors = sectors;
3754 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003755 if (mddev->pers)
3756 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003757
3758 return len;
3759}
3760
3761static struct md_sysfs_entry md_array_size =
3762__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3763 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003764
NeilBrowneae17012005-11-08 21:39:23 -08003765static struct attribute *md_default_attrs[] = {
3766 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003767 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003768 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003769 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003770 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003771 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003772 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08003773 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003774 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003775 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003776 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003777 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003778 NULL,
3779};
3780
3781static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003782 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003783 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003784 &md_sync_min.attr,
3785 &md_sync_max.attr,
3786 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003787 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003788 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003789 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003790 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003791 &md_suspend_lo.attr,
3792 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003793 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003794 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003795 NULL,
3796};
NeilBrown411036f2005-11-08 21:39:40 -08003797static struct attribute_group md_redundancy_group = {
3798 .name = NULL,
3799 .attrs = md_redundancy_attrs,
3800};
3801
NeilBrowneae17012005-11-08 21:39:23 -08003802
3803static ssize_t
3804md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3805{
3806 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3807 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003808 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003809
3810 if (!entry->show)
3811 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003812 rv = mddev_lock(mddev);
3813 if (!rv) {
3814 rv = entry->show(mddev, page);
3815 mddev_unlock(mddev);
3816 }
NeilBrown96de1e62005-11-08 21:39:39 -08003817 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003818}
3819
3820static ssize_t
3821md_attr_store(struct kobject *kobj, struct attribute *attr,
3822 const char *page, size_t length)
3823{
3824 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3825 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003826 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003827
3828 if (!entry->store)
3829 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003830 if (!capable(CAP_SYS_ADMIN))
3831 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003832 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003833 if (mddev->hold_active == UNTIL_IOCTL)
3834 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003835 if (!rv) {
3836 rv = entry->store(mddev, page, length);
3837 mddev_unlock(mddev);
3838 }
NeilBrown96de1e62005-11-08 21:39:39 -08003839 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003840}
3841
3842static void md_free(struct kobject *ko)
3843{
3844 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003845
3846 if (mddev->sysfs_state)
3847 sysfs_put(mddev->sysfs_state);
3848
3849 if (mddev->gendisk) {
3850 del_gendisk(mddev->gendisk);
3851 put_disk(mddev->gendisk);
3852 }
3853 if (mddev->queue)
3854 blk_cleanup_queue(mddev->queue);
3855
NeilBrowneae17012005-11-08 21:39:23 -08003856 kfree(mddev);
3857}
3858
3859static struct sysfs_ops md_sysfs_ops = {
3860 .show = md_attr_show,
3861 .store = md_attr_store,
3862};
3863static struct kobj_type md_ktype = {
3864 .release = md_free,
3865 .sysfs_ops = &md_sysfs_ops,
3866 .default_attrs = md_default_attrs,
3867};
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869int mdp_major = 0;
3870
Dan Williams5fd3a172009-03-04 00:57:25 -07003871static void mddev_delayed_delete(struct work_struct *ws)
3872{
3873 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3874
3875 if (mddev->private == &md_redundancy_group) {
3876 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3877 if (mddev->sysfs_action)
3878 sysfs_put(mddev->sysfs_action);
3879 mddev->sysfs_action = NULL;
3880 mddev->private = NULL;
3881 }
3882 kobject_del(&mddev->kobj);
3883 kobject_put(&mddev->kobj);
3884}
3885
NeilBrownefeb53c2009-01-09 08:31:10 +11003886static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003888 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 mddev_t *mddev = mddev_find(dev);
3890 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003891 int partitioned;
3892 int shift;
3893 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003894 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
3896 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003897 return -ENODEV;
3898
3899 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3900 shift = partitioned ? MdpMinorShift : 0;
3901 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
NeilBrownd3374822009-01-09 08:31:10 +11003903 /* wait for any previous instance if this device
3904 * to be completed removed (mddev_delayed_delete).
3905 */
3906 flush_scheduled_work();
3907
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003908 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10003909 error = -EEXIST;
3910 if (mddev->gendisk)
3911 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003912
3913 if (name) {
3914 /* Need to ensure that 'name' is not a duplicate.
3915 */
3916 mddev_t *mddev2;
3917 spin_lock(&all_mddevs_lock);
3918
3919 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3920 if (mddev2->gendisk &&
3921 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3922 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10003923 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003924 }
3925 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
NeilBrown8b765392009-01-09 08:31:08 +11003927
NeilBrown0909dc42009-07-01 12:27:21 +10003928 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003929 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10003930 if (!mddev->queue)
3931 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11003932 mddev->queue->queuedata = mddev;
3933
NeilBrown8b765392009-01-09 08:31:08 +11003934 /* Can be unlocked because the queue is new: no concurrency */
3935 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3936
NeilBrown409c57f2009-03-31 14:39:39 +11003937 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 disk = alloc_disk(1 << shift);
3940 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11003941 blk_cleanup_queue(mddev->queue);
3942 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10003943 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003945 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003947 if (name)
3948 strcpy(disk->disk_name, name);
3949 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003951 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 disk->fops = &md_fops;
3954 disk->private_data = mddev;
3955 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003956 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003957 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003958 * remove it now.
3959 */
3960 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 add_disk(disk);
3962 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003963 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3964 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10003965 if (error) {
3966 /* This isn't possible, but as kobject_init_and_add is marked
3967 * __must_check, we must do something with the result
3968 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08003969 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3970 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10003971 error = 0;
3972 }
3973 abort:
3974 mutex_unlock(&disks_mutex);
3975 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003976 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003977 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3978 }
NeilBrownd3374822009-01-09 08:31:10 +11003979 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10003980 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11003981}
3982
3983static struct kobject *md_probe(dev_t dev, int *part, void *data)
3984{
3985 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 return NULL;
3987}
3988
NeilBrownefeb53c2009-01-09 08:31:10 +11003989static int add_named_array(const char *val, struct kernel_param *kp)
3990{
3991 /* val must be "md_*" where * is not all digits.
3992 * We allocate an array with a large free minor number, and
3993 * set the name to val. val must not already be an active name.
3994 */
3995 int len = strlen(val);
3996 char buf[DISK_NAME_LEN];
3997
3998 while (len && val[len-1] == '\n')
3999 len--;
4000 if (len >= DISK_NAME_LEN)
4001 return -E2BIG;
4002 strlcpy(buf, val, len+1);
4003 if (strncmp(buf, "md_", 3) != 0)
4004 return -EINVAL;
4005 return md_alloc(0, buf);
4006}
4007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008static void md_safemode_timeout(unsigned long data)
4009{
4010 mddev_t *mddev = (mddev_t *) data;
4011
Neil Brown0fd62b82008-06-28 08:31:36 +10004012 if (!atomic_read(&mddev->writes_pending)) {
4013 mddev->safemode = 1;
4014 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004015 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 md_wakeup_thread(mddev->thread);
4018}
4019
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004020static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022static int do_md_run(mddev_t * mddev)
4023{
NeilBrown2604b702006-01-06 00:20:36 -08004024 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 mdk_rdev_t *rdev;
4026 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004027 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028
NeilBrowna757e642005-04-16 15:26:42 -07004029 if (list_empty(&mddev->disks))
4030 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
4033 if (mddev->pers)
4034 return -EBUSY;
4035
4036 /*
4037 * Analyze all RAID superblock(s)
4038 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004039 if (!mddev->raid_disks) {
4040 if (!mddev->persistent)
4041 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004042 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
NeilBrownd9d166c2006-01-06 00:20:51 -08004045 if (mddev->level != LEVEL_NONE)
4046 request_module("md-level-%d", mddev->level);
4047 else if (mddev->clevel[0])
4048 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
4050 /*
4051 * Drop all container device buffers, from now on
4052 * the only valid external interface is through the md
4053 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004055 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004056 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 continue;
4058 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004059 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004060
4061 /* perform some consistency tests on the device.
4062 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004063 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004064 */
Andre Noll0f420352008-07-11 22:02:23 +10004065 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004066 if (mddev->dev_sectors &&
4067 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004068 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004069 printk("md: %s: data overlaps metadata\n",
4070 mdname(mddev));
4071 return -EINVAL;
4072 }
4073 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004074 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004075 > rdev->data_offset) {
4076 printk("md: %s: metadata overlaps data\n",
4077 mdname(mddev));
4078 return -EINVAL;
4079 }
4080 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004081 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 }
4083
4084 md_probe(mddev->unit, NULL, NULL);
4085 disk = mddev->gendisk;
4086 if (!disk)
4087 return -ENOMEM;
4088
4089 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004090 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004091 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004093 if (mddev->level != LEVEL_NONE)
4094 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4095 mddev->level);
4096 else
4097 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4098 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 return -EINVAL;
4100 }
NeilBrown2604b702006-01-06 00:20:36 -08004101 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004103 if (mddev->level != pers->level) {
4104 mddev->level = pers->level;
4105 mddev->new_level = pers->level;
4106 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004107 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
NeilBrownf6705572006-03-27 01:18:11 -08004109 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004110 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004111 /* This personality cannot handle reshaping... */
4112 mddev->pers = NULL;
4113 module_put(pers->owner);
4114 return -EINVAL;
4115 }
4116
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004117 if (pers->sync_request) {
4118 /* Warn if this is a potentially silly
4119 * configuration.
4120 */
4121 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4122 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004123 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004124
4125 list_for_each_entry(rdev, &mddev->disks, same_set)
4126 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004127 if (rdev < rdev2 &&
4128 rdev->bdev->bd_contains ==
4129 rdev2->bdev->bd_contains) {
4130 printk(KERN_WARNING
4131 "%s: WARNING: %s appears to be"
4132 " on the same physical disk as"
4133 " %s.\n",
4134 mdname(mddev),
4135 bdevname(rdev->bdev,b),
4136 bdevname(rdev2->bdev,b2));
4137 warned = 1;
4138 }
4139 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004140
NeilBrown7dd5e7c2007-02-28 20:11:35 -08004141 if (warned)
4142 printk(KERN_WARNING
4143 "True protection against single-disk"
4144 " failure might be compromised.\n");
4145 }
4146
NeilBrown657390d2005-08-26 18:34:16 -07004147 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004148 /* may be over-ridden by personality */
4149 mddev->resync_max_sectors = mddev->dev_sectors;
4150
NeilBrowna9701a32005-11-08 21:39:34 -08004151 mddev->barriers_work = 1;
NeilBrown6ff8d8e2006-01-06 00:20:15 -08004152 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153
NeilBrownf91de922005-11-08 21:39:36 -08004154 if (start_readonly)
4155 mddev->ro = 2; /* read-only, but switch on first write */
4156
NeilBrownb15c2e52006-01-06 00:20:16 -08004157 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004158 if (err)
4159 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004160 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4161 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4162 " but 'external_size' not in effect?\n", __func__);
4163 printk(KERN_ERR
4164 "md: invalid array_size %llu > default size %llu\n",
4165 (unsigned long long)mddev->array_sectors / 2,
4166 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4167 err = -EINVAL;
4168 mddev->pers->stop(mddev);
4169 }
4170 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004171 err = bitmap_create(mddev);
4172 if (err) {
4173 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4174 mdname(mddev), err);
4175 mddev->pers->stop(mddev);
4176 }
4177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 module_put(mddev->pers->owner);
4180 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004181 bitmap_destroy(mddev);
4182 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004184 if (mddev->pers->sync_request) {
4185 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4186 printk(KERN_WARNING
4187 "md: cannot register extra attributes for %s\n",
4188 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004189 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004190 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004191 mddev->ro = 0;
4192
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 atomic_set(&mddev->writes_pending,0);
4194 mddev->safemode = 0;
4195 mddev->safemode_timer.function = md_safemode_timeout;
4196 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004197 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004199
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004200 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004201 if (rdev->raid_disk >= 0) {
4202 char nm[20];
4203 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004204 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4205 printk("md: cannot register %s for %s\n",
4206 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208
4209 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4210
NeilBrown850b2b42006-10-03 01:15:46 -07004211 if (mddev->flags)
4212 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Andre Nollf233ea52008-07-21 17:05:22 +10004214 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004216 /* If there is a partially-recovered drive we need to
4217 * start recovery here. If we leave it to md_check_recovery,
4218 * it will remove the drives and not do the right thing
4219 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004220 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004221 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004222 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004223 if (rdev->raid_disk >= 0 &&
4224 !test_bit(In_sync, &rdev->flags) &&
4225 !test_bit(Faulty, &rdev->flags))
4226 /* complete an interrupted recovery */
4227 spares++;
4228 if (spares && mddev->pers->sync_request) {
4229 mddev->recovery = 0;
4230 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4231 mddev->sync_thread = md_register_thread(md_do_sync,
4232 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004233 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004234 if (!mddev->sync_thread) {
4235 printk(KERN_ERR "%s: could not start resync"
4236 " thread...\n",
4237 mdname(mddev));
4238 /* leave the spares where they are, it shouldn't hurt */
4239 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004240 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004241 }
4242 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004243 md_wakeup_thread(mddev->thread);
4244 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004245
NeilBrown449aad32009-08-03 10:59:58 +10004246 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004247 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004248 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004249 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004250 if (mddev->sysfs_action)
4251 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004252 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004253 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return 0;
4255}
4256
4257static int restart_array(mddev_t *mddev)
4258{
4259 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Andre Noll80fab1d2008-07-11 22:02:21 +10004261 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004263 return -ENXIO;
4264 if (!mddev->pers)
4265 return -EINVAL;
4266 if (!mddev->ro)
4267 return -EBUSY;
4268 mddev->safemode = 0;
4269 mddev->ro = 0;
4270 set_disk_ro(disk, 0);
4271 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4272 mdname(mddev));
4273 /* Kick recovery or resync if necessary */
4274 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4275 md_wakeup_thread(mddev->thread);
4276 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004277 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279}
4280
NeilBrownacc55e22006-06-26 00:27:47 -07004281/* similar to deny_write_access, but accounts for our holding a reference
4282 * to the file ourselves */
4283static int deny_bitmap_write_access(struct file * file)
4284{
4285 struct inode *inode = file->f_mapping->host;
4286
4287 spin_lock(&inode->i_lock);
4288 if (atomic_read(&inode->i_writecount) > 1) {
4289 spin_unlock(&inode->i_lock);
4290 return -ETXTBSY;
4291 }
4292 atomic_set(&inode->i_writecount, -1);
4293 spin_unlock(&inode->i_lock);
4294
4295 return 0;
4296}
4297
4298static void restore_bitmap_write_access(struct file *file)
4299{
4300 struct inode *inode = file->f_mapping->host;
4301
4302 spin_lock(&inode->i_lock);
4303 atomic_set(&inode->i_writecount, 1);
4304 spin_unlock(&inode->i_lock);
4305}
4306
NeilBrown9e653b62006-06-26 00:27:58 -07004307/* mode:
4308 * 0 - completely stop and dis-assemble array
4309 * 1 - switch to readonly
4310 * 2 - stop but do not disassemble array
4311 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004312static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313{
4314 int err = 0;
4315 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004316 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317
NeilBrownc8c00a62009-08-10 12:50:52 +10004318 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004319 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004320 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004321 err = -EBUSY;
4322 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
4324 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004325 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4327 md_unregister_thread(mddev->sync_thread);
4328 mddev->sync_thread = NULL;
4329 }
4330
4331 del_timer_sync(&mddev->safemode_timer);
4332
NeilBrown9e653b62006-06-26 00:27:58 -07004333 switch(mode) {
4334 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004336 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 goto out;
4338 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004339 break;
4340 case 0: /* disassemble */
4341 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004342 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004343 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 if (mddev->ro)
4345 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004346
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004348 mddev->queue->merge_bvec_fn = NULL;
4349 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004350 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004352 if (mddev->pers->sync_request)
4353 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004355 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004356 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004357
NeilBrownc4647292009-05-07 12:51:06 +10004358 list_for_each_entry(rdev, &mddev->disks, same_set)
4359 if (rdev->raid_disk >= 0) {
4360 char nm[20];
4361 sprintf(nm, "rd%d", rdev->raid_disk);
4362 sysfs_remove_link(&mddev->kobj, nm);
4363 }
4364
NeilBrown0d4ca602006-12-10 02:20:44 -08004365 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004366 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 if (mddev->ro)
4369 mddev->ro = 0;
4370 }
NeilBrown850b2b42006-10-03 01:15:46 -07004371 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 /* mark array as shutdown cleanly */
4373 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004374 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 }
NeilBrown9e653b62006-06-26 00:27:58 -07004376 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004378 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004379 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004381out:
4382 mutex_unlock(&mddev->open_mutex);
4383 if (err)
4384 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 /*
4386 * Free resources if final stop
4387 */
NeilBrown9e653b62006-06-26 00:27:58 -07004388 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004389
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4391
NeilBrown978f9462006-02-02 14:28:05 -08004392 bitmap_destroy(mddev);
4393 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004394 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004395 fput(mddev->bitmap_file);
4396 mddev->bitmap_file = NULL;
4397 }
4398 mddev->bitmap_offset = 0;
4399
NeilBrown177a99b2008-02-06 01:39:56 -08004400 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004401 flush_scheduled_work();
4402
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 export_array(mddev);
4404
Andre Nollf233ea52008-07-21 17:05:22 +10004405 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004406 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004407 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004408 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004409 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004410 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004411 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004412 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004413 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004414 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004415 mddev->level = LEVEL_NONE;
4416 mddev->clevel[0] = 0;
4417 mddev->flags = 0;
4418 mddev->ro = 0;
4419 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004420 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004421 mddev->ctime = mddev->utime = 0;
4422 mddev->layout = 0;
4423 mddev->max_disks = 0;
4424 mddev->events = 0;
4425 mddev->delta_disks = 0;
4426 mddev->new_level = LEVEL_NONE;
4427 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004428 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004429 mddev->curr_resync = 0;
4430 mddev->resync_mismatches = 0;
4431 mddev->suspend_lo = mddev->suspend_hi = 0;
4432 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4433 mddev->recovery = 0;
4434 mddev->in_sync = 0;
4435 mddev->changed = 0;
4436 mddev->degraded = 0;
4437 mddev->barriers_work = 0;
4438 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004439 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004440 if (mddev->hold_active == UNTIL_STOP)
4441 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004442
NeilBrowna8a55c32006-06-26 00:27:39 -07004443 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4445 mdname(mddev));
4446 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004447 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004448 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004449 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 return err;
4451}
4452
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004453#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454static void autorun_array(mddev_t *mddev)
4455{
4456 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 int err;
4458
NeilBrowna757e642005-04-16 15:26:42 -07004459 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
4462 printk(KERN_INFO "md: running: ");
4463
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004464 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 char b[BDEVNAME_SIZE];
4466 printk("<%s>", bdevname(rdev->bdev,b));
4467 }
4468 printk("\n");
4469
NeilBrownd710e132008-10-13 11:55:12 +11004470 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 if (err) {
4472 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004473 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 }
4475}
4476
4477/*
4478 * lets try to run arrays based on all disks that have arrived
4479 * until now. (those are in pending_raid_disks)
4480 *
4481 * the method: pick the first pending disk, collect all disks with
4482 * the same UUID, remove all from the pending list and put them into
4483 * the 'same_array' list. Then order this list based on superblock
4484 * update time (freshest comes first), kick out 'old' disks and
4485 * compare superblocks. If everything's fine then run it.
4486 *
4487 * If "unit" is allocated, then bump its reference count
4488 */
4489static void autorun_devices(int part)
4490{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004491 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 mddev_t *mddev;
4493 char b[BDEVNAME_SIZE];
4494
4495 printk(KERN_INFO "md: autorun ...\n");
4496 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004497 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004499 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 rdev0 = list_entry(pending_raid_disks.next,
4501 mdk_rdev_t, same_set);
4502
4503 printk(KERN_INFO "md: considering %s ...\n",
4504 bdevname(rdev0->bdev,b));
4505 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004506 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 if (super_90_load(rdev, rdev0, 0) >= 0) {
4508 printk(KERN_INFO "md: adding %s ...\n",
4509 bdevname(rdev->bdev,b));
4510 list_move(&rdev->same_set, &candidates);
4511 }
4512 /*
4513 * now we have a set of devices, with all of them having
4514 * mostly sane superblocks. It's time to allocate the
4515 * mddev.
4516 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004517 if (part) {
4518 dev = MKDEV(mdp_major,
4519 rdev0->preferred_minor << MdpMinorShift);
4520 unit = MINOR(dev) >> MdpMinorShift;
4521 } else {
4522 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4523 unit = MINOR(dev);
4524 }
4525 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4527 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4528 break;
4529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
4531 md_probe(dev, NULL, NULL);
4532 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004533 if (!mddev || !mddev->gendisk) {
4534 if (mddev)
4535 mddev_put(mddev);
4536 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 "md: cannot allocate memory for md drive.\n");
4538 break;
4539 }
4540 if (mddev_lock(mddev))
4541 printk(KERN_WARNING "md: %s locked, cannot run\n",
4542 mdname(mddev));
4543 else if (mddev->raid_disks || mddev->major_version
4544 || !list_empty(&mddev->disks)) {
4545 printk(KERN_WARNING
4546 "md: %s already running, cannot run %s\n",
4547 mdname(mddev), bdevname(rdev0->bdev,b));
4548 mddev_unlock(mddev);
4549 } else {
4550 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004551 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004552 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 list_del_init(&rdev->same_set);
4554 if (bind_rdev_to_array(rdev, mddev))
4555 export_rdev(rdev);
4556 }
4557 autorun_array(mddev);
4558 mddev_unlock(mddev);
4559 }
4560 /* on success, candidates will be empty, on error
4561 * it won't...
4562 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004563 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004564 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 mddev_put(mddev);
4568 }
4569 printk(KERN_INFO "md: ... autorun DONE.\n");
4570}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004571#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573static int get_version(void __user * arg)
4574{
4575 mdu_version_t ver;
4576
4577 ver.major = MD_MAJOR_VERSION;
4578 ver.minor = MD_MINOR_VERSION;
4579 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4580
4581 if (copy_to_user(arg, &ver, sizeof(ver)))
4582 return -EFAULT;
4583
4584 return 0;
4585}
4586
4587static int get_array_info(mddev_t * mddev, void __user * arg)
4588{
4589 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004590 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
NeilBrowna9f326e2009-09-23 18:06:41 +10004593 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004594 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004596 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 failed++;
4598 else {
4599 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004600 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004601 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 else
4603 spare++;
4604 }
4605 }
4606
4607 info.major_version = mddev->major_version;
4608 info.minor_version = mddev->minor_version;
4609 info.patch_version = MD_PATCHLEVEL_VERSION;
4610 info.ctime = mddev->ctime;
4611 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004612 info.size = mddev->dev_sectors / 2;
4613 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004614 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 info.nr_disks = nr;
4616 info.raid_disks = mddev->raid_disks;
4617 info.md_minor = mddev->md_minor;
4618 info.not_persistent= !mddev->persistent;
4619
4620 info.utime = mddev->utime;
4621 info.state = 0;
4622 if (mddev->in_sync)
4623 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004624 if (mddev->bitmap && mddev->bitmap_offset)
4625 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004626 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 info.working_disks = working;
4628 info.failed_disks = failed;
4629 info.spare_disks = spare;
4630
4631 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004632 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
4634 if (copy_to_user(arg, &info, sizeof(info)))
4635 return -EFAULT;
4636
4637 return 0;
4638}
4639
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004640static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004641{
4642 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4643 char *ptr, *buf = NULL;
4644 int err = -ENOMEM;
4645
Dan Williamsb5470dc2008-06-27 21:44:04 -07004646 if (md_allow_write(mddev))
4647 file = kmalloc(sizeof(*file), GFP_NOIO);
4648 else
4649 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004650
NeilBrown32a76272005-06-21 17:17:14 -07004651 if (!file)
4652 goto out;
4653
4654 /* bitmap disabled, zero the first byte and copy out */
4655 if (!mddev->bitmap || !mddev->bitmap->file) {
4656 file->pathname[0] = '\0';
4657 goto copy_out;
4658 }
4659
4660 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4661 if (!buf)
4662 goto out;
4663
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004664 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4665 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004666 goto out;
4667
4668 strcpy(file->pathname, ptr);
4669
4670copy_out:
4671 err = 0;
4672 if (copy_to_user(arg, file, sizeof(*file)))
4673 err = -EFAULT;
4674out:
4675 kfree(buf);
4676 kfree(file);
4677 return err;
4678}
4679
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680static int get_disk_info(mddev_t * mddev, void __user * arg)
4681{
4682 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 mdk_rdev_t *rdev;
4684
4685 if (copy_from_user(&info, arg, sizeof(info)))
4686 return -EFAULT;
4687
Andre Noll26ef3792008-07-11 22:02:21 +10004688 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 if (rdev) {
4690 info.major = MAJOR(rdev->bdev->bd_dev);
4691 info.minor = MINOR(rdev->bdev->bd_dev);
4692 info.raid_disk = rdev->raid_disk;
4693 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004694 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004696 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 info.state |= (1<<MD_DISK_ACTIVE);
4698 info.state |= (1<<MD_DISK_SYNC);
4699 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004700 if (test_bit(WriteMostly, &rdev->flags))
4701 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 } else {
4703 info.major = info.minor = 0;
4704 info.raid_disk = -1;
4705 info.state = (1<<MD_DISK_REMOVED);
4706 }
4707
4708 if (copy_to_user(arg, &info, sizeof(info)))
4709 return -EFAULT;
4710
4711 return 0;
4712}
4713
4714static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4715{
4716 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4717 mdk_rdev_t *rdev;
4718 dev_t dev = MKDEV(info->major,info->minor);
4719
4720 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4721 return -EOVERFLOW;
4722
4723 if (!mddev->raid_disks) {
4724 int err;
4725 /* expecting a device which has a superblock */
4726 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4727 if (IS_ERR(rdev)) {
4728 printk(KERN_WARNING
4729 "md: md_import_device returned %ld\n",
4730 PTR_ERR(rdev));
4731 return PTR_ERR(rdev);
4732 }
4733 if (!list_empty(&mddev->disks)) {
4734 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4735 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004736 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 .load_super(rdev, rdev0, mddev->minor_version);
4738 if (err < 0) {
4739 printk(KERN_WARNING
4740 "md: %s has different UUID to %s\n",
4741 bdevname(rdev->bdev,b),
4742 bdevname(rdev0->bdev,b2));
4743 export_rdev(rdev);
4744 return -EINVAL;
4745 }
4746 }
4747 err = bind_rdev_to_array(rdev, mddev);
4748 if (err)
4749 export_rdev(rdev);
4750 return err;
4751 }
4752
4753 /*
4754 * add_new_disk can be used once the array is assembled
4755 * to add "hot spares". They must already have a superblock
4756 * written
4757 */
4758 if (mddev->pers) {
4759 int err;
4760 if (!mddev->pers->hot_add_disk) {
4761 printk(KERN_WARNING
4762 "%s: personality does not support diskops!\n",
4763 mdname(mddev));
4764 return -EINVAL;
4765 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004766 if (mddev->persistent)
4767 rdev = md_import_device(dev, mddev->major_version,
4768 mddev->minor_version);
4769 else
4770 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 if (IS_ERR(rdev)) {
4772 printk(KERN_WARNING
4773 "md: md_import_device returned %ld\n",
4774 PTR_ERR(rdev));
4775 return PTR_ERR(rdev);
4776 }
NeilBrown41158c72005-06-21 17:17:25 -07004777 /* set save_raid_disk if appropriate */
4778 if (!mddev->persistent) {
4779 if (info->state & (1<<MD_DISK_SYNC) &&
4780 info->raid_disk < mddev->raid_disks)
4781 rdev->raid_disk = info->raid_disk;
4782 else
4783 rdev->raid_disk = -1;
4784 } else
4785 super_types[mddev->major_version].
4786 validate_super(mddev, rdev);
4787 rdev->saved_raid_disk = rdev->raid_disk;
4788
NeilBrownb2d444d2005-11-08 21:39:31 -08004789 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004790 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4791 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004792 else
4793 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004794
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 rdev->raid_disk = -1;
4796 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004797 if (!err && !mddev->pers->hot_remove_disk) {
4798 /* If there is hot_add_disk but no hot_remove_disk
4799 * then added disks for geometry changes,
4800 * and should be added immediately.
4801 */
4802 super_types[mddev->major_version].
4803 validate_super(mddev, rdev);
4804 err = mddev->pers->hot_add_disk(mddev, rdev);
4805 if (err)
4806 unbind_rdev_from_array(rdev);
4807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808 if (err)
4809 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004810 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004811 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004812
NeilBrown17571282006-12-10 02:20:52 -08004813 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004814 if (mddev->degraded)
4815 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004816 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004817 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 return err;
4819 }
4820
4821 /* otherwise, add_new_disk is only allowed
4822 * for major_version==0 superblocks
4823 */
4824 if (mddev->major_version != 0) {
4825 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4826 mdname(mddev));
4827 return -EINVAL;
4828 }
4829
4830 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4831 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004832 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 if (IS_ERR(rdev)) {
4834 printk(KERN_WARNING
4835 "md: error, md_import_device() returned %ld\n",
4836 PTR_ERR(rdev));
4837 return PTR_ERR(rdev);
4838 }
4839 rdev->desc_nr = info->number;
4840 if (info->raid_disk < mddev->raid_disks)
4841 rdev->raid_disk = info->raid_disk;
4842 else
4843 rdev->raid_disk = -1;
4844
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004846 if (info->state & (1<<MD_DISK_SYNC))
4847 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004849 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4850 set_bit(WriteMostly, &rdev->flags);
4851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 if (!mddev->persistent) {
4853 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004854 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 } else
Andre Noll0f420352008-07-11 22:02:23 +10004856 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004857 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858
NeilBrown2bf071b2006-01-06 00:20:55 -08004859 err = bind_rdev_to_array(rdev, mddev);
4860 if (err) {
4861 export_rdev(rdev);
4862 return err;
4863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 }
4865
4866 return 0;
4867}
4868
4869static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4870{
4871 char b[BDEVNAME_SIZE];
4872 mdk_rdev_t *rdev;
4873
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 rdev = find_rdev(mddev, dev);
4875 if (!rdev)
4876 return -ENXIO;
4877
4878 if (rdev->raid_disk >= 0)
4879 goto busy;
4880
4881 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004882 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004883 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885 return 0;
4886busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004887 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888 bdevname(rdev->bdev,b), mdname(mddev));
4889 return -EBUSY;
4890}
4891
4892static int hot_add_disk(mddev_t * mddev, dev_t dev)
4893{
4894 char b[BDEVNAME_SIZE];
4895 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 mdk_rdev_t *rdev;
4897
4898 if (!mddev->pers)
4899 return -ENODEV;
4900
4901 if (mddev->major_version != 0) {
4902 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4903 " version-0 superblocks.\n",
4904 mdname(mddev));
4905 return -EINVAL;
4906 }
4907 if (!mddev->pers->hot_add_disk) {
4908 printk(KERN_WARNING
4909 "%s: personality does not support diskops!\n",
4910 mdname(mddev));
4911 return -EINVAL;
4912 }
4913
NeilBrownd710e132008-10-13 11:55:12 +11004914 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 if (IS_ERR(rdev)) {
4916 printk(KERN_WARNING
4917 "md: error, md_import_device() returned %ld\n",
4918 PTR_ERR(rdev));
4919 return -EINVAL;
4920 }
4921
4922 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004923 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 else
Andre Noll0f420352008-07-11 22:02:23 +10004925 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926
NeilBrown8190e752009-06-18 08:48:58 +10004927 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928
NeilBrownb2d444d2005-11-08 21:39:31 -08004929 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 printk(KERN_WARNING
4931 "md: can not hot-add faulty %s disk to %s!\n",
4932 bdevname(rdev->bdev,b), mdname(mddev));
4933 err = -EINVAL;
4934 goto abort_export;
4935 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004936 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004938 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004939 err = bind_rdev_to_array(rdev, mddev);
4940 if (err)
4941 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
4943 /*
4944 * The rest should better be atomic, we can have disk failures
4945 * noticed in interrupt contexts ...
4946 */
4947
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 rdev->raid_disk = -1;
4949
NeilBrown850b2b42006-10-03 01:15:46 -07004950 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951
4952 /*
4953 * Kick recovery, maybe this spare has to be added to the
4954 * array immediately.
4955 */
4956 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4957 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004958 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 return 0;
4960
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961abort_export:
4962 export_rdev(rdev);
4963 return err;
4964}
4965
NeilBrown32a76272005-06-21 17:17:14 -07004966static int set_bitmap_file(mddev_t *mddev, int fd)
4967{
4968 int err;
4969
NeilBrown36fa3062005-09-09 16:23:45 -07004970 if (mddev->pers) {
4971 if (!mddev->pers->quiesce)
4972 return -EBUSY;
4973 if (mddev->recovery || mddev->sync_thread)
4974 return -EBUSY;
4975 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004976 }
4977
NeilBrown36fa3062005-09-09 16:23:45 -07004978
4979 if (fd >= 0) {
4980 if (mddev->bitmap)
4981 return -EEXIST; /* cannot add when bitmap is present */
4982 mddev->bitmap_file = fget(fd);
4983
4984 if (mddev->bitmap_file == NULL) {
4985 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
4986 mdname(mddev));
4987 return -EBADF;
4988 }
4989
4990 err = deny_bitmap_write_access(mddev->bitmap_file);
4991 if (err) {
4992 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
4993 mdname(mddev));
4994 fput(mddev->bitmap_file);
4995 mddev->bitmap_file = NULL;
4996 return err;
4997 }
NeilBrowna654b9d82005-06-21 17:17:27 -07004998 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07004999 } else if (mddev->bitmap == NULL)
5000 return -ENOENT; /* cannot remove what isn't there */
5001 err = 0;
5002 if (mddev->pers) {
5003 mddev->pers->quiesce(mddev, 1);
5004 if (fd >= 0)
5005 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005006 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005007 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005008 fd = -1; /* make sure to put the file */
5009 }
NeilBrown36fa3062005-09-09 16:23:45 -07005010 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005011 }
5012 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07005013 if (mddev->bitmap_file) {
5014 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07005015 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07005016 }
NeilBrown36fa3062005-09-09 16:23:45 -07005017 mddev->bitmap_file = NULL;
5018 }
5019
NeilBrown32a76272005-06-21 17:17:14 -07005020 return err;
5021}
5022
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023/*
5024 * set_array_info is used two different ways
5025 * The original usage is when creating a new array.
5026 * In this usage, raid_disks is > 0 and it together with
5027 * level, size, not_persistent,layout,chunksize determine the
5028 * shape of the array.
5029 * This will always create an array with a type-0.90.0 superblock.
5030 * The newer usage is when assembling an array.
5031 * In this case raid_disks will be 0, and the major_version field is
5032 * use to determine which style super-blocks are to be found on the devices.
5033 * The minor and patch _version numbers are also kept incase the
5034 * super_block handler wishes to interpret them.
5035 */
5036static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5037{
5038
5039 if (info->raid_disks == 0) {
5040 /* just setting version number for superblock loading */
5041 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005042 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 super_types[info->major_version].name == NULL) {
5044 /* maybe try to auto-load a module? */
5045 printk(KERN_INFO
5046 "md: superblock version %d not known\n",
5047 info->major_version);
5048 return -EINVAL;
5049 }
5050 mddev->major_version = info->major_version;
5051 mddev->minor_version = info->minor_version;
5052 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005053 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 return 0;
5055 }
5056 mddev->major_version = MD_MAJOR_VERSION;
5057 mddev->minor_version = MD_MINOR_VERSION;
5058 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5059 mddev->ctime = get_seconds();
5060
5061 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005062 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005063 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 mddev->raid_disks = info->raid_disks;
5065 /* don't set md_minor, it is determined by which /dev/md* was
5066 * openned
5067 */
5068 if (info->state & (1<<MD_SB_CLEAN))
5069 mddev->recovery_cp = MaxSector;
5070 else
5071 mddev->recovery_cp = 0;
5072 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005073 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
5075 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005076 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077
5078 mddev->max_disks = MD_SB_DISKS;
5079
NeilBrowne6910632008-02-06 01:39:51 -08005080 if (mddev->persistent)
5081 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005082 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083
NeilBrownb2a27032005-11-28 13:44:12 -08005084 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5085 mddev->bitmap_offset = 0;
5086
NeilBrownf6705572006-03-27 01:18:11 -08005087 mddev->reshape_position = MaxSector;
5088
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 /*
5090 * Generate a 128 bit UUID
5091 */
5092 get_random_bytes(mddev->uuid, 16);
5093
NeilBrownf6705572006-03-27 01:18:11 -08005094 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005095 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005096 mddev->new_layout = mddev->layout;
5097 mddev->delta_disks = 0;
5098
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 return 0;
5100}
5101
Dan Williams1f403622009-03-31 14:59:03 +11005102void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5103{
Dan Williamsb522adc2009-03-31 15:00:31 +11005104 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5105
5106 if (mddev->external_size)
5107 return;
5108
Dan Williams1f403622009-03-31 14:59:03 +11005109 mddev->array_sectors = array_sectors;
5110}
5111EXPORT_SYMBOL(md_set_array_sectors);
5112
Andre Nolld71f9f82008-07-11 22:02:22 +10005113static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005114{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005115 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005116 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005117 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005118
5119 if (mddev->pers->resize == NULL)
5120 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005121 /* The "num_sectors" is the number of sectors of each device that
5122 * is used. This can only make sense for arrays with redundancy.
5123 * linear and raid0 always use whatever space is available. We can only
5124 * consider changing this number if no resync or reconstruction is
5125 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005126 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005127 * of each device. If num_sectors is zero, we find the largest size
5128 * that fits.
5129
NeilBrowna35b0d62006-01-06 00:20:49 -08005130 */
5131 if (mddev->sync_thread)
5132 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005133 if (mddev->bitmap)
5134 /* Sorry, cannot grow a bitmap yet, just remove it,
5135 * grow, and re-add.
5136 */
5137 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005138 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005139 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005140
Andre Nolld71f9f82008-07-11 22:02:22 +10005141 if (fit && (num_sectors == 0 || num_sectors > avail))
5142 num_sectors = avail;
5143 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005144 return -ENOSPC;
5145 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005146 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005147 if (!rv)
5148 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005149 return rv;
5150}
5151
NeilBrownda943b92006-01-06 00:20:54 -08005152static int update_raid_disks(mddev_t *mddev, int raid_disks)
5153{
5154 int rv;
5155 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005156 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b92006-01-06 00:20:54 -08005157 return -EINVAL;
5158 if (raid_disks <= 0 ||
5159 raid_disks >= mddev->max_disks)
5160 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005161 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b92006-01-06 00:20:54 -08005162 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005163 mddev->delta_disks = raid_disks - mddev->raid_disks;
5164
5165 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b92006-01-06 00:20:54 -08005166 return rv;
5167}
5168
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170/*
5171 * update_array_info is used to change the configuration of an
5172 * on-line array.
5173 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5174 * fields in the info are checked against the array.
5175 * Any differences that cannot be handled will cause an error.
5176 * Normally, only one change can be managed at a time.
5177 */
5178static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5179{
5180 int rv = 0;
5181 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005182 int state = 0;
5183
5184 /* calculate expected state,ignoring low bits */
5185 if (mddev->bitmap && mddev->bitmap_offset)
5186 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
5188 if (mddev->major_version != info->major_version ||
5189 mddev->minor_version != info->minor_version ||
5190/* mddev->patch_version != info->patch_version || */
5191 mddev->ctime != info->ctime ||
5192 mddev->level != info->level ||
5193/* mddev->layout != info->layout || */
5194 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005195 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005196 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5197 ((state^info->state) & 0xfffffe00)
5198 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 return -EINVAL;
5200 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005201 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5202 cnt++;
5203 if (mddev->raid_disks != info->raid_disks)
5204 cnt++;
5205 if (mddev->layout != info->layout)
5206 cnt++;
5207 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5208 cnt++;
5209 if (cnt == 0)
5210 return 0;
5211 if (cnt > 1)
5212 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213
5214 if (mddev->layout != info->layout) {
5215 /* Change layout
5216 * we don't need to do anything at the md level, the
5217 * personality will take care of it all.
5218 */
NeilBrown50ac1682009-06-18 08:47:55 +10005219 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005221 else {
5222 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005223 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005224 if (rv)
5225 mddev->new_layout = mddev->layout;
5226 return rv;
5227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005229 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005230 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231
NeilBrownda943b92006-01-06 00:20:54 -08005232 if (mddev->raid_disks != info->raid_disks)
5233 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234
NeilBrown36fa3062005-09-09 16:23:45 -07005235 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5236 if (mddev->pers->quiesce == NULL)
5237 return -EINVAL;
5238 if (mddev->recovery || mddev->sync_thread)
5239 return -EBUSY;
5240 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5241 /* add the bitmap */
5242 if (mddev->bitmap)
5243 return -EEXIST;
5244 if (mddev->default_bitmap_offset == 0)
5245 return -EINVAL;
5246 mddev->bitmap_offset = mddev->default_bitmap_offset;
5247 mddev->pers->quiesce(mddev, 1);
5248 rv = bitmap_create(mddev);
5249 if (rv)
5250 bitmap_destroy(mddev);
5251 mddev->pers->quiesce(mddev, 0);
5252 } else {
5253 /* remove the bitmap */
5254 if (!mddev->bitmap)
5255 return -ENOENT;
5256 if (mddev->bitmap->file)
5257 return -EINVAL;
5258 mddev->pers->quiesce(mddev, 1);
5259 bitmap_destroy(mddev);
5260 mddev->pers->quiesce(mddev, 0);
5261 mddev->bitmap_offset = 0;
5262 }
5263 }
NeilBrown850b2b42006-10-03 01:15:46 -07005264 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 return rv;
5266}
5267
5268static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5269{
5270 mdk_rdev_t *rdev;
5271
5272 if (mddev->pers == NULL)
5273 return -ENODEV;
5274
5275 rdev = find_rdev(mddev, dev);
5276 if (!rdev)
5277 return -ENODEV;
5278
5279 md_error(mddev, rdev);
5280 return 0;
5281}
5282
Andre Noll2f9618c2008-04-25 18:57:58 +02005283/*
5284 * We have a problem here : there is no easy way to give a CHS
5285 * virtual geometry. We currently pretend that we have a 2 heads
5286 * 4 sectors (with a BIG number of cylinders...). This drives
5287 * dosfs just mad... ;-)
5288 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005289static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5290{
5291 mddev_t *mddev = bdev->bd_disk->private_data;
5292
5293 geo->heads = 2;
5294 geo->sectors = 4;
5295 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5296 return 0;
5297}
5298
Al Viroa39907f2008-03-02 10:31:15 -05005299static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 unsigned int cmd, unsigned long arg)
5301{
5302 int err = 0;
5303 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 mddev_t *mddev = NULL;
5305
5306 if (!capable(CAP_SYS_ADMIN))
5307 return -EACCES;
5308
5309 /*
5310 * Commands dealing with the RAID driver but not any
5311 * particular array:
5312 */
5313 switch (cmd)
5314 {
5315 case RAID_VERSION:
5316 err = get_version(argp);
5317 goto done;
5318
5319 case PRINT_RAID_DEBUG:
5320 err = 0;
5321 md_print_devices();
5322 goto done;
5323
5324#ifndef MODULE
5325 case RAID_AUTORUN:
5326 err = 0;
5327 autostart_arrays(arg);
5328 goto done;
5329#endif
5330 default:;
5331 }
5332
5333 /*
5334 * Commands creating/starting a new array:
5335 */
5336
Al Viroa39907f2008-03-02 10:31:15 -05005337 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 if (!mddev) {
5340 BUG();
5341 goto abort;
5342 }
5343
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 err = mddev_lock(mddev);
5345 if (err) {
5346 printk(KERN_INFO
5347 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5348 err, cmd);
5349 goto abort;
5350 }
5351
5352 switch (cmd)
5353 {
5354 case SET_ARRAY_INFO:
5355 {
5356 mdu_array_info_t info;
5357 if (!arg)
5358 memset(&info, 0, sizeof(info));
5359 else if (copy_from_user(&info, argp, sizeof(info))) {
5360 err = -EFAULT;
5361 goto abort_unlock;
5362 }
5363 if (mddev->pers) {
5364 err = update_array_info(mddev, &info);
5365 if (err) {
5366 printk(KERN_WARNING "md: couldn't update"
5367 " array info. %d\n", err);
5368 goto abort_unlock;
5369 }
5370 goto done_unlock;
5371 }
5372 if (!list_empty(&mddev->disks)) {
5373 printk(KERN_WARNING
5374 "md: array %s already has disks!\n",
5375 mdname(mddev));
5376 err = -EBUSY;
5377 goto abort_unlock;
5378 }
5379 if (mddev->raid_disks) {
5380 printk(KERN_WARNING
5381 "md: array %s already initialised!\n",
5382 mdname(mddev));
5383 err = -EBUSY;
5384 goto abort_unlock;
5385 }
5386 err = set_array_info(mddev, &info);
5387 if (err) {
5388 printk(KERN_WARNING "md: couldn't set"
5389 " array info. %d\n", err);
5390 goto abort_unlock;
5391 }
5392 }
5393 goto done_unlock;
5394
5395 default:;
5396 }
5397
5398 /*
5399 * Commands querying/configuring an existing array:
5400 */
NeilBrown32a76272005-06-21 17:17:14 -07005401 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005402 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005403 if ((!mddev->raid_disks && !mddev->external)
5404 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5405 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5406 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 err = -ENODEV;
5408 goto abort_unlock;
5409 }
5410
5411 /*
5412 * Commands even a read-only array can execute:
5413 */
5414 switch (cmd)
5415 {
5416 case GET_ARRAY_INFO:
5417 err = get_array_info(mddev, argp);
5418 goto done_unlock;
5419
NeilBrown32a76272005-06-21 17:17:14 -07005420 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005421 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005422 goto done_unlock;
5423
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 case GET_DISK_INFO:
5425 err = get_disk_info(mddev, argp);
5426 goto done_unlock;
5427
5428 case RESTART_ARRAY_RW:
5429 err = restart_array(mddev);
5430 goto done_unlock;
5431
5432 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005433 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 goto done_unlock;
5435
5436 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005437 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 goto done_unlock;
5439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 }
5441
5442 /*
5443 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005444 * superblock, so we do not allow them on read-only arrays.
5445 * However non-MD ioctls (e.g. get-size) will still come through
5446 * here and hit the 'default' below, so only disallow
5447 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005449 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005450 if (mddev->ro == 2) {
5451 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005452 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005453 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5454 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005455 } else {
5456 err = -EROFS;
5457 goto abort_unlock;
5458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 }
5460
5461 switch (cmd)
5462 {
5463 case ADD_NEW_DISK:
5464 {
5465 mdu_disk_info_t info;
5466 if (copy_from_user(&info, argp, sizeof(info)))
5467 err = -EFAULT;
5468 else
5469 err = add_new_disk(mddev, &info);
5470 goto done_unlock;
5471 }
5472
5473 case HOT_REMOVE_DISK:
5474 err = hot_remove_disk(mddev, new_decode_dev(arg));
5475 goto done_unlock;
5476
5477 case HOT_ADD_DISK:
5478 err = hot_add_disk(mddev, new_decode_dev(arg));
5479 goto done_unlock;
5480
5481 case SET_DISK_FAULTY:
5482 err = set_disk_faulty(mddev, new_decode_dev(arg));
5483 goto done_unlock;
5484
5485 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005486 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 goto done_unlock;
5488
NeilBrown32a76272005-06-21 17:17:14 -07005489 case SET_BITMAP_FILE:
5490 err = set_bitmap_file(mddev, (int)arg);
5491 goto done_unlock;
5492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 err = -EINVAL;
5495 goto abort_unlock;
5496 }
5497
5498done_unlock:
5499abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005500 if (mddev->hold_active == UNTIL_IOCTL &&
5501 err != -EINVAL)
5502 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 mddev_unlock(mddev);
5504
5505 return err;
5506done:
5507 if (err)
5508 MD_BUG();
5509abort:
5510 return err;
5511}
5512
Al Viroa39907f2008-03-02 10:31:15 -05005513static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514{
5515 /*
5516 * Succeed if we can lock the mddev, which confirms that
5517 * it isn't being stopped right now.
5518 */
NeilBrownd3374822009-01-09 08:31:10 +11005519 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 int err;
5521
NeilBrownd3374822009-01-09 08:31:10 +11005522 if (mddev->gendisk != bdev->bd_disk) {
5523 /* we are racing with mddev_put which is discarding this
5524 * bd_disk.
5525 */
5526 mddev_put(mddev);
5527 /* Wait until bdev->bd_disk is definitely gone */
5528 flush_scheduled_work();
5529 /* Then retry the open from the top */
5530 return -ERESTARTSYS;
5531 }
5532 BUG_ON(mddev != bdev->bd_disk->private_data);
5533
NeilBrownc8c00a62009-08-10 12:50:52 +10005534 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 goto out;
5536
5537 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005538 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005539 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540
Al Viroa39907f2008-03-02 10:31:15 -05005541 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 out:
5543 return err;
5544}
5545
Al Viroa39907f2008-03-02 10:31:15 -05005546static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547{
Al Viroa39907f2008-03-02 10:31:15 -05005548 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005550 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005551 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 mddev_put(mddev);
5553
5554 return 0;
5555}
5556
Linus Torvalds44ce6292007-05-09 18:51:36 -07005557static int md_media_changed(struct gendisk *disk)
5558{
5559 mddev_t *mddev = disk->private_data;
5560
5561 return mddev->changed;
5562}
5563
5564static int md_revalidate(struct gendisk *disk)
5565{
5566 mddev_t *mddev = disk->private_data;
5567
5568 mddev->changed = 0;
5569 return 0;
5570}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005571static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572{
5573 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005574 .open = md_open,
5575 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005576 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005577 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005578 .media_changed = md_media_changed,
5579 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580};
5581
Adrian Bunk75c96f82005-05-05 16:16:09 -07005582static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583{
5584 mdk_thread_t *thread = arg;
5585
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 /*
5587 * md_thread is a 'system-thread', it's priority should be very
5588 * high. We avoid resource deadlocks individually in each
5589 * raid personality. (RAID5 does preallocation) We also use RR and
5590 * the very same RT priority as kswapd, thus we will never get
5591 * into a priority inversion deadlock.
5592 *
5593 * we definitely have to have equal or higher priority than
5594 * bdflush, otherwise bdflush will deadlock if there are too
5595 * many dirty RAID5 blocks.
5596 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
NeilBrown6985c432005-10-19 21:23:47 -07005598 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005599 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600
NeilBrown93588e22005-11-15 00:09:12 -08005601 /* We need to wait INTERRUPTIBLE so that
5602 * we don't add to the load-average.
5603 * That means we need to be sure no signals are
5604 * pending
5605 */
5606 if (signal_pending(current))
5607 flush_signals(current);
5608
5609 wait_event_interruptible_timeout
5610 (thread->wqueue,
5611 test_bit(THREAD_WAKEUP, &thread->flags)
5612 || kthread_should_stop(),
5613 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614
5615 clear_bit(THREAD_WAKEUP, &thread->flags);
5616
NeilBrown787453c2005-11-08 21:39:43 -08005617 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005619
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 return 0;
5621}
5622
5623void md_wakeup_thread(mdk_thread_t *thread)
5624{
5625 if (thread) {
5626 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5627 set_bit(THREAD_WAKEUP, &thread->flags);
5628 wake_up(&thread->wqueue);
5629 }
5630}
5631
5632mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5633 const char *name)
5634{
5635 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636
NeilBrown9ffae0c2006-01-06 00:20:32 -08005637 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 if (!thread)
5639 return NULL;
5640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 init_waitqueue_head(&thread->wqueue);
5642
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 thread->run = run;
5644 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005645 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005646 thread->tsk = kthread_run(md_thread, thread,
5647 "%s_%s",
5648 mdname(thread->mddev),
5649 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005650 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 kfree(thread);
5652 return NULL;
5653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 return thread;
5655}
5656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657void md_unregister_thread(mdk_thread_t *thread)
5658{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005659 if (!thread)
5660 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005661 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005662
5663 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 kfree(thread);
5665}
5666
5667void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5668{
5669 if (!mddev) {
5670 MD_BUG();
5671 return;
5672 }
5673
NeilBrownb2d444d2005-11-08 21:39:31 -08005674 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005676
5677 if (mddev->external)
5678 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005679/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5681 mdname(mddev),
5682 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5683 __builtin_return_address(0),__builtin_return_address(1),
5684 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005685*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005686 if (!mddev->pers)
5687 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 if (!mddev->pers->error_handler)
5689 return;
5690 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005691 if (mddev->degraded)
5692 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005693 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5695 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5696 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005697 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698}
5699
5700/* seq_file implementation /proc/mdstat */
5701
5702static void status_unused(struct seq_file *seq)
5703{
5704 int i = 0;
5705 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706
5707 seq_printf(seq, "unused devices: ");
5708
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005709 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 char b[BDEVNAME_SIZE];
5711 i++;
5712 seq_printf(seq, "%s ",
5713 bdevname(rdev->bdev,b));
5714 }
5715 if (!i)
5716 seq_printf(seq, "<none>");
5717
5718 seq_printf(seq, "\n");
5719}
5720
5721
5722static void status_resync(struct seq_file *seq, mddev_t * mddev)
5723{
NeilBrowndd71cf62009-05-07 12:49:35 +10005724 sector_t max_sectors, resync, res;
5725 unsigned long dt, db;
5726 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005727 int scale;
5728 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729
NeilBrowndd71cf62009-05-07 12:49:35 +10005730 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731
5732 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005733 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005735 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736
5737 /*
5738 * Should not happen.
5739 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005740 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 MD_BUG();
5742 return;
5743 }
NeilBrown4588b422006-03-27 01:18:04 -08005744 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005745 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005746 * u32, as those are the requirements for sector_div.
5747 * Thus 'scale' must be at least 10
5748 */
5749 scale = 10;
5750 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005751 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005752 scale++;
5753 }
5754 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005755 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005756
5757 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 {
NeilBrown4588b422006-03-27 01:18:04 -08005759 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 seq_printf(seq, "[");
5761 for (i = 0; i < x; i++)
5762 seq_printf(seq, "=");
5763 seq_printf(seq, ">");
5764 for (i = 0; i < y; i++)
5765 seq_printf(seq, ".");
5766 seq_printf(seq, "] ");
5767 }
NeilBrown4588b422006-03-27 01:18:04 -08005768 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005769 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5770 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005771 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5772 "check" :
5773 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5774 "resync" : "recovery"))),
5775 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005776 (unsigned long long) resync/2,
5777 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778
5779 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 * dt: time from mark until now
5781 * db: blocks written from mark until now
5782 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005783 *
5784 * rt is a sector_t, so could be 32bit or 64bit.
5785 * So we divide before multiply in case it is 32bit and close
5786 * to the limit.
5787 * We scale the divisor (db) by 32 to avoid loosing precision
5788 * near the end of resync when the number of remaining sectors
5789 * is close to 'db'.
5790 * We then divide rt by 32 after multiplying by db to compensate.
5791 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 */
5793 dt = ((jiffies - mddev->resync_mark) / HZ);
5794 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005795 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5796 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797
NeilBrowndd71cf62009-05-07 12:49:35 +10005798 rt = max_sectors - resync; /* number of remaining sectors */
5799 sector_div(rt, db/32+1);
5800 rt *= dt;
5801 rt >>= 5;
5802
5803 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5804 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805
NeilBrownff4e8d92006-07-10 04:44:16 -07005806 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807}
5808
5809static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5810{
5811 struct list_head *tmp;
5812 loff_t l = *pos;
5813 mddev_t *mddev;
5814
5815 if (l >= 0x10000)
5816 return NULL;
5817 if (!l--)
5818 /* header */
5819 return (void*)1;
5820
5821 spin_lock(&all_mddevs_lock);
5822 list_for_each(tmp,&all_mddevs)
5823 if (!l--) {
5824 mddev = list_entry(tmp, mddev_t, all_mddevs);
5825 mddev_get(mddev);
5826 spin_unlock(&all_mddevs_lock);
5827 return mddev;
5828 }
5829 spin_unlock(&all_mddevs_lock);
5830 if (!l--)
5831 return (void*)2;/* tail */
5832 return NULL;
5833}
5834
5835static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5836{
5837 struct list_head *tmp;
5838 mddev_t *next_mddev, *mddev = v;
5839
5840 ++*pos;
5841 if (v == (void*)2)
5842 return NULL;
5843
5844 spin_lock(&all_mddevs_lock);
5845 if (v == (void*)1)
5846 tmp = all_mddevs.next;
5847 else
5848 tmp = mddev->all_mddevs.next;
5849 if (tmp != &all_mddevs)
5850 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5851 else {
5852 next_mddev = (void*)2;
5853 *pos = 0x10000;
5854 }
5855 spin_unlock(&all_mddevs_lock);
5856
5857 if (v != (void*)1)
5858 mddev_put(mddev);
5859 return next_mddev;
5860
5861}
5862
5863static void md_seq_stop(struct seq_file *seq, void *v)
5864{
5865 mddev_t *mddev = v;
5866
5867 if (mddev && v != (void*)1 && v != (void*)2)
5868 mddev_put(mddev);
5869}
5870
NeilBrownd7603b72006-01-06 00:20:30 -08005871struct mdstat_info {
5872 int event;
5873};
5874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875static int md_seq_show(struct seq_file *seq, void *v)
5876{
5877 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005878 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005880 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005881 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005882
5883 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005884 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005885 seq_printf(seq, "Personalities : ");
5886 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005887 list_for_each_entry(pers, &pers_list, list)
5888 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889
5890 spin_unlock(&pers_lock);
5891 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005892 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 return 0;
5894 }
5895 if (v == (void*)2) {
5896 status_unused(seq);
5897 return 0;
5898 }
5899
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005900 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005902
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5904 seq_printf(seq, "%s : %sactive", mdname(mddev),
5905 mddev->pers ? "" : "in");
5906 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005907 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005909 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005910 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 seq_printf(seq, " %s", mddev->pers->name);
5912 }
5913
Andre Nolldd8ac332009-03-31 14:33:13 +11005914 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005915 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 char b[BDEVNAME_SIZE];
5917 seq_printf(seq, " %s[%d]",
5918 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005919 if (test_bit(WriteMostly, &rdev->flags))
5920 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005921 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 seq_printf(seq, "(F)");
5923 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005924 } else if (rdev->raid_disk < 0)
5925 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005926 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 }
5928
5929 if (!list_empty(&mddev->disks)) {
5930 if (mddev->pers)
5931 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005932 (unsigned long long)
5933 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 else
5935 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005936 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005938 if (mddev->persistent) {
5939 if (mddev->major_version != 0 ||
5940 mddev->minor_version != 90) {
5941 seq_printf(seq," super %d.%d",
5942 mddev->major_version,
5943 mddev->minor_version);
5944 }
NeilBrowne6910632008-02-06 01:39:51 -08005945 } else if (mddev->external)
5946 seq_printf(seq, " super external:%s",
5947 mddev->metadata_type);
5948 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005949 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950
5951 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005952 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005954 if (mddev->pers->sync_request) {
5955 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005956 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005957 seq_printf(seq, "\n ");
5958 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5959 seq_printf(seq, "\tresync=DELAYED\n ");
5960 else if (mddev->recovery_cp < MaxSector)
5961 seq_printf(seq, "\tresync=PENDING\n ");
5962 }
NeilBrown32a76272005-06-21 17:17:14 -07005963 } else
5964 seq_printf(seq, "\n ");
5965
5966 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005967 unsigned long chunk_kb;
5968 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005969 spin_lock_irqsave(&bitmap->lock, flags);
5970 chunk_kb = bitmap->chunksize >> 10;
5971 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5972 "%lu%s chunk",
5973 bitmap->pages - bitmap->missing_pages,
5974 bitmap->pages,
5975 (bitmap->pages - bitmap->missing_pages)
5976 << (PAGE_SHIFT - 10),
5977 chunk_kb ? chunk_kb : bitmap->chunksize,
5978 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005979 if (bitmap->file) {
5980 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005981 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005982 }
NeilBrown78d742d2005-06-21 17:17:15 -07005983
NeilBrown32a76272005-06-21 17:17:14 -07005984 seq_printf(seq, "\n");
5985 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 }
5987
5988 seq_printf(seq, "\n");
5989 }
5990 mddev_unlock(mddev);
5991
5992 return 0;
5993}
5994
Jan Engelhardt110518b2009-05-07 12:49:37 +10005995static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996 .start = md_seq_start,
5997 .next = md_seq_next,
5998 .stop = md_seq_stop,
5999 .show = md_seq_show,
6000};
6001
6002static int md_seq_open(struct inode *inode, struct file *file)
6003{
6004 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006005 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6006 if (mi == NULL)
6007 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008
6009 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006010 if (error)
6011 kfree(mi);
6012 else {
6013 struct seq_file *p = file->private_data;
6014 p->private = mi;
6015 mi->event = atomic_read(&md_event_count);
6016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 return error;
6018}
6019
NeilBrownd7603b72006-01-06 00:20:30 -08006020static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6021{
6022 struct seq_file *m = filp->private_data;
6023 struct mdstat_info *mi = m->private;
6024 int mask;
6025
6026 poll_wait(filp, &md_event_waiters, wait);
6027
6028 /* always allow read */
6029 mask = POLLIN | POLLRDNORM;
6030
6031 if (mi->event != atomic_read(&md_event_count))
6032 mask |= POLLERR | POLLPRI;
6033 return mask;
6034}
6035
Arjan van de Venfa027c22007-02-12 00:55:33 -08006036static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006037 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 .open = md_seq_open,
6039 .read = seq_read,
6040 .llseek = seq_lseek,
Martin Peschkec3f94b42007-05-09 02:35:35 -07006041 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006042 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043};
6044
NeilBrown2604b702006-01-06 00:20:36 -08006045int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006048 list_add_tail(&p->list, &pers_list);
6049 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050 spin_unlock(&pers_lock);
6051 return 0;
6052}
6053
NeilBrown2604b702006-01-06 00:20:36 -08006054int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055{
NeilBrown2604b702006-01-06 00:20:36 -08006056 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006058 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 spin_unlock(&pers_lock);
6060 return 0;
6061}
6062
NeilBrowneea1bf32009-03-31 14:27:02 +11006063static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064{
6065 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006067 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068
6069 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006070 rcu_read_lock();
6071 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006073 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6074 (int)part_stat_read(&disk->part0, sectors[1]) -
6075 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006076 /* sync IO will cause sync_io to increase before the disk_stats
6077 * as sync_io is counted when a request starts, and
6078 * disk_stats is counted when it completes.
6079 * So resync activity will cause curr_events to be smaller than
6080 * when there was no such activity.
6081 * non-sync IO will cause disk_stat to increase without
6082 * increasing sync_io so curr_events will (eventually)
6083 * be larger than it was before. Once it becomes
6084 * substantially larger, the test below will cause
6085 * the array to appear non-idle, and resync will slow
6086 * down.
6087 * If there is a lot of outstanding resync activity when
6088 * we set last_event to curr_events, then all that activity
6089 * completing might cause the array to appear non-idle
6090 * and resync will be slowed down even though there might
6091 * not have been non-resync activity. This will only
6092 * happen once though. 'last_events' will soon reflect
6093 * the state where there is little or no outstanding
6094 * resync requests, and further resync activity will
6095 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006096 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006098 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006099 rdev->last_events = curr_events;
6100 idle = 0;
6101 }
6102 }
NeilBrown4b809912008-07-21 17:05:25 +10006103 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return idle;
6105}
6106
6107void md_done_sync(mddev_t *mddev, int blocks, int ok)
6108{
6109 /* another "blocks" (512byte) blocks have been synced */
6110 atomic_sub(blocks, &mddev->recovery_active);
6111 wake_up(&mddev->recovery_wait);
6112 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006113 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 md_wakeup_thread(mddev->thread);
6115 // stop recovery, signal do_sync ....
6116 }
6117}
6118
6119
NeilBrown06d91a52005-06-21 17:17:12 -07006120/* md_write_start(mddev, bi)
6121 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006122 * in superblock) before writing, schedule a superblock update
6123 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006124 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006125void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126{
Neil Brown0fd62b82008-06-28 08:31:36 +10006127 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006128 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006129 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006130
NeilBrownf91de922005-11-08 21:39:36 -08006131 BUG_ON(mddev->ro == 1);
6132 if (mddev->ro == 2) {
6133 /* need to switch to read/write */
6134 mddev->ro = 0;
6135 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6136 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006137 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006138 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006139 }
NeilBrown06d91a52005-06-21 17:17:12 -07006140 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006141 if (mddev->safemode == 1)
6142 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006143 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006144 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006145 if (mddev->in_sync) {
6146 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006147 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006148 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006149 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006150 }
NeilBrowna9701a32005-11-08 21:39:34 -08006151 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006152 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006153 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006154 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006155 wait_event(mddev->sb_wait,
6156 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6157 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158}
6159
6160void md_write_end(mddev_t *mddev)
6161{
6162 if (atomic_dec_and_test(&mddev->writes_pending)) {
6163 if (mddev->safemode == 2)
6164 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006165 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6167 }
6168}
6169
NeilBrown2a2275d2007-01-26 00:57:11 -08006170/* md_allow_write(mddev)
6171 * Calling this ensures that the array is marked 'active' so that writes
6172 * may proceed without blocking. It is important to call this before
6173 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6174 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006175 *
6176 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6177 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006178 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006179int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006180{
6181 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006182 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006183 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006184 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006185 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006186 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006187
6188 spin_lock_irq(&mddev->write_lock);
6189 if (mddev->in_sync) {
6190 mddev->in_sync = 0;
6191 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6192 if (mddev->safemode_delay &&
6193 mddev->safemode == 0)
6194 mddev->safemode = 1;
6195 spin_unlock_irq(&mddev->write_lock);
6196 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006197 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006198 } else
6199 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006200
6201 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6202 return -EAGAIN;
6203 else
6204 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006205}
6206EXPORT_SYMBOL_GPL(md_allow_write);
6207
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208#define SYNC_MARKS 10
6209#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006210void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211{
6212 mddev_t *mddev2;
6213 unsigned int currspeed = 0,
6214 window;
NeilBrown57afd892005-06-21 17:17:13 -07006215 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 unsigned long mark[SYNC_MARKS];
6217 sector_t mark_cnt[SYNC_MARKS];
6218 int last_mark,m;
6219 struct list_head *tmp;
6220 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006221 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006222 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006223 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224
6225 /* just incase thread restarts... */
6226 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6227 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006228 if (mddev->ro) /* never try to sync a read-only array */
6229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
NeilBrown61df9d92006-10-03 01:15:57 -07006231 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6232 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6233 desc = "data-check";
6234 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6235 desc = "requested-resync";
6236 else
6237 desc = "resync";
6238 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6239 desc = "reshape";
6240 else
6241 desc = "recovery";
6242
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 /* we overload curr_resync somewhat here.
6244 * 0 == not engaged in resync at all
6245 * 2 == checking that there is no conflict with another sync
6246 * 1 == like 2, but have yielded to allow conflicting resync to
6247 * commense
6248 * other == active in resync - this many blocks
6249 *
6250 * Before starting a resync we must have set curr_resync to
6251 * 2, and then checked that every "conflicting" array has curr_resync
6252 * less than ours. When we find one that is the same or higher
6253 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6254 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6255 * This will mean we have to start checking from the beginning again.
6256 *
6257 */
6258
6259 do {
6260 mddev->curr_resync = 2;
6261
6262 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006263 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006264 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 goto skip;
6266 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006267 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 if (mddev2 == mddev)
6269 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006270 if (!mddev->parallel_resync
6271 && mddev2->curr_resync
6272 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 DEFINE_WAIT(wq);
6274 if (mddev < mddev2 && mddev->curr_resync == 2) {
6275 /* arbitrarily yield */
6276 mddev->curr_resync = 1;
6277 wake_up(&resync_wait);
6278 }
6279 if (mddev > mddev2 && mddev->curr_resync == 1)
6280 /* no need to wait here, we can wait the next
6281 * time 'round when curr_resync == 2
6282 */
6283 continue;
NeilBrown97441972008-09-19 11:49:54 +10006284 /* We need to wait 'interruptible' so as not to
6285 * contribute to the load average, and not to
6286 * be caught by 'softlockup'
6287 */
6288 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006289 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006290 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006291 printk(KERN_INFO "md: delaying %s of %s"
6292 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006294 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006296 if (signal_pending(current))
6297 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298 schedule();
6299 finish_wait(&resync_wait, &wq);
6300 goto try_again;
6301 }
6302 finish_wait(&resync_wait, &wq);
6303 }
6304 }
6305 } while (mddev->curr_resync < 2);
6306
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006307 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006308 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006310 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 */
6312 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006313 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006314 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006315 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6316 j = mddev->resync_min;
6317 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006318 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006319
NeilBrownccfcc3c2006-03-27 01:18:09 -08006320 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006321 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006322 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006324 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006325 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006326 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006327 if (rdev->raid_disk >= 0 &&
6328 !test_bit(Faulty, &rdev->flags) &&
6329 !test_bit(In_sync, &rdev->flags) &&
6330 rdev->recovery_offset < j)
6331 j = rdev->recovery_offset;
6332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
NeilBrown61df9d92006-10-03 01:15:57 -07006334 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6335 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6336 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006337 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006338 "(but not more than %d KB/sec) for %s.\n",
6339 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340
NeilBrowneea1bf32009-03-31 14:27:02 +11006341 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006342
NeilBrown57afd892005-06-21 17:17:13 -07006343 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344 for (m = 0; m < SYNC_MARKS; m++) {
6345 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006346 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 }
6348 last_mark = 0;
6349 mddev->resync_mark = mark[last_mark];
6350 mddev->resync_mark_cnt = mark_cnt[last_mark];
6351
6352 /*
6353 * Tune reconstruction:
6354 */
6355 window = 32*(PAGE_SIZE/512);
6356 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6357 window/2,(unsigned long long) max_sectors/2);
6358
6359 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 last_check = 0;
6361
6362 if (j>2) {
6363 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006364 "md: resuming %s of %s from checkpoint.\n",
6365 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 mddev->curr_resync = j;
6367 }
6368
6369 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006370 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
NeilBrown57afd892005-06-21 17:17:13 -07006372 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006373
NeilBrown7a91ee12009-05-26 12:57:21 +10006374 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6375 ((mddev->curr_resync > mddev->curr_resync_completed &&
6376 (mddev->curr_resync - mddev->curr_resync_completed)
6377 > (max_sectors >> 4)) ||
6378 (j - mddev->curr_resync_completed)*2
6379 >= mddev->resync_max - mddev->curr_resync_completed
6380 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006381 /* time to update curr_resync_completed */
6382 blk_unplug(mddev->queue);
6383 wait_event(mddev->recovery_wait,
6384 atomic_read(&mddev->recovery_active) == 0);
6385 mddev->curr_resync_completed =
6386 mddev->curr_resync;
6387 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006388 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006389 }
NeilBrownacb180b2009-04-14 16:28:34 +10006390
NeilBrowne62e58a2009-07-01 13:15:35 +10006391 while (j >= mddev->resync_max && !kthread_should_stop()) {
6392 /* As this condition is controlled by user-space,
6393 * we can block indefinitely, so use '_interruptible'
6394 * to avoid triggering warnings.
6395 */
6396 flush_signals(current); /* just in case */
6397 wait_event_interruptible(mddev->recovery_wait,
6398 mddev->resync_max > j
6399 || kthread_should_stop());
6400 }
NeilBrownacb180b2009-04-14 16:28:34 +10006401
6402 if (kthread_should_stop())
6403 goto interrupted;
6404
NeilBrown57afd892005-06-21 17:17:13 -07006405 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006406 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006407 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006408 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 goto out;
6410 }
NeilBrown57afd892005-06-21 17:17:13 -07006411
6412 if (!skipped) { /* actual IO requested */
6413 io_sectors += sectors;
6414 atomic_add(sectors, &mddev->recovery_active);
6415 }
6416
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 j += sectors;
6418 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006419 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006420 if (last_check == 0)
6421 /* this is the earliers that rebuilt will be
6422 * visible in /proc/mdstat
6423 */
6424 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006425
6426 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006427 continue;
6428
NeilBrown57afd892005-06-21 17:17:13 -07006429 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430
NeilBrowndfc70642008-05-23 13:04:39 -07006431 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006432 break;
6433
6434 repeat:
6435 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6436 /* step marks */
6437 int next = (last_mark+1) % SYNC_MARKS;
6438
6439 mddev->resync_mark = mark[next];
6440 mddev->resync_mark_cnt = mark_cnt[next];
6441 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006442 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 last_mark = next;
6444 }
6445
6446
NeilBrownc6207272008-02-06 01:39:52 -08006447 if (kthread_should_stop())
6448 goto interrupted;
6449
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450
6451 /*
6452 * this loop exits only if either when we are slower than
6453 * the 'hard' speed limit, or the system was IO-idle for
6454 * a jiffy.
6455 * the system might be non-idle CPU-wise, but we only care
6456 * about not overloading the IO subsystem. (things like an
6457 * e2fsck being done on the RAID array should execute fast)
6458 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006459 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460 cond_resched();
6461
NeilBrown57afd892005-06-21 17:17:13 -07006462 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6463 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464
NeilBrown88202a02006-01-06 00:21:36 -08006465 if (currspeed > speed_min(mddev)) {
6466 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006467 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006468 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 goto repeat;
6470 }
6471 }
6472 }
NeilBrown61df9d92006-10-03 01:15:57 -07006473 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006474 /*
6475 * this also signals 'finished resyncing' to md_stop
6476 */
6477 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006478 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006479
6480 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6481
6482 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006483 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484
NeilBrowndfc70642008-05-23 13:04:39 -07006485 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006486 mddev->curr_resync > 2) {
6487 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6488 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6489 if (mddev->curr_resync >= mddev->recovery_cp) {
6490 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006491 "md: checkpointing %s of %s.\n",
6492 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006493 mddev->recovery_cp = mddev->curr_resync;
6494 }
6495 } else
6496 mddev->recovery_cp = MaxSector;
6497 } else {
6498 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6499 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006500 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006501 if (rdev->raid_disk >= 0 &&
6502 !test_bit(Faulty, &rdev->flags) &&
6503 !test_bit(In_sync, &rdev->flags) &&
6504 rdev->recovery_offset < mddev->curr_resync)
6505 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006507 }
NeilBrown17571282006-12-10 02:20:52 -08006508 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006509
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510 skip:
6511 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006512 mddev->curr_resync_completed = 0;
NeilBrown24395a82009-11-06 14:59:27 +11006513 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6514 /* We completed so max setting can be forgotten. */
6515 mddev->resync_max = MaxSector;
NeilBrownc6207272008-02-06 01:39:52 -08006516 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 wake_up(&resync_wait);
6518 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6519 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006520 return;
6521
6522 interrupted:
6523 /*
6524 * got a signal, exit.
6525 */
6526 printk(KERN_INFO
6527 "md: md_do_sync() got signal ... exiting\n");
6528 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6529 goto out;
6530
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531}
NeilBrown29269552006-03-27 01:18:10 -08006532EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533
6534
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006535static int remove_and_add_spares(mddev_t *mddev)
6536{
6537 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006538 int spares = 0;
6539
NeilBrown97e4f422009-03-31 14:33:13 +11006540 mddev->curr_resync_completed = 0;
6541
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006542 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006543 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006544 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006545 (test_bit(Faulty, &rdev->flags) ||
6546 ! test_bit(In_sync, &rdev->flags)) &&
6547 atomic_read(&rdev->nr_pending)==0) {
6548 if (mddev->pers->hot_remove_disk(
6549 mddev, rdev->raid_disk)==0) {
6550 char nm[20];
6551 sprintf(nm,"rd%d", rdev->raid_disk);
6552 sysfs_remove_link(&mddev->kobj, nm);
6553 rdev->raid_disk = -1;
6554 }
6555 }
6556
NeilBrown4044ba52009-01-09 08:31:11 +11006557 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006558 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006559 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006560 !test_bit(In_sync, &rdev->flags) &&
6561 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006562 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006563 if (rdev->raid_disk < 0
6564 && !test_bit(Faulty, &rdev->flags)) {
6565 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006566 if (mddev->pers->
6567 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006568 char nm[20];
6569 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006570 if (sysfs_create_link(&mddev->kobj,
6571 &rdev->kobj, nm))
6572 printk(KERN_WARNING
6573 "md: cannot register "
6574 "%s for %s\n",
6575 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006576 spares++;
6577 md_new_event(mddev);
6578 } else
6579 break;
6580 }
NeilBrowndfc70642008-05-23 13:04:39 -07006581 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006582 }
6583 return spares;
6584}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585/*
6586 * This routine is regularly called by all per-raid-array threads to
6587 * deal with generic issues like resync and super-block update.
6588 * Raid personalities that don't have a thread (linear/raid0) do not
6589 * need this as they never do any recovery or update the superblock.
6590 *
6591 * It does not do any resync itself, but rather "forks" off other threads
6592 * to do that as needed.
6593 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6594 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006595 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006596 * and wakeups up this thread which will reap the thread and finish up.
6597 * This thread also removes any faulty devices (with nr_pending == 0).
6598 *
6599 * The overall approach is:
6600 * 1/ if the superblock needs updating, update it.
6601 * 2/ If a recovery thread is running, don't do anything else.
6602 * 3/ If recovery has finished, clean up, possibly marking spares active.
6603 * 4/ If there are any faulty devices, remove them.
6604 * 5/ If array is degraded, try to add spares devices
6605 * 6/ If array has spares or is not in-sync, start a resync thread.
6606 */
6607void md_check_recovery(mddev_t *mddev)
6608{
6609 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610
6611
NeilBrown5f404022005-06-21 17:17:16 -07006612 if (mddev->bitmap)
6613 bitmap_daemon_work(mddev->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614
6615 if (mddev->ro)
6616 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006617
6618 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006619 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006620 printk(KERN_INFO "md: %s in immediate safe mode\n",
6621 mdname(mddev));
6622 mddev->safemode = 2;
6623 }
6624 flush_signals(current);
6625 }
6626
NeilBrownc89a8ee2008-08-05 15:54:13 +10006627 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6628 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006630 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006632 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006633 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006634 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6635 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 ))
6637 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006638
NeilBrowndf5b89b2006-03-27 01:18:20 -08006639 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006640 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006641
NeilBrownc89a8ee2008-08-05 15:54:13 +10006642 if (mddev->ro) {
6643 /* Only thing we do on a ro array is remove
6644 * failed devices.
6645 */
6646 remove_and_add_spares(mddev);
6647 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6648 goto unlock;
6649 }
6650
NeilBrown31a59e32008-04-30 00:52:30 -07006651 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006652 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006653 spin_lock_irq(&mddev->write_lock);
6654 if (mddev->safemode &&
6655 !atomic_read(&mddev->writes_pending) &&
6656 !mddev->in_sync &&
6657 mddev->recovery_cp == MaxSector) {
6658 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006659 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006660 if (mddev->persistent)
6661 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6662 }
6663 if (mddev->safemode == 1)
6664 mddev->safemode = 0;
6665 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006666 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006667 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006668 }
NeilBrownfca4d842005-06-21 17:17:11 -07006669
NeilBrown850b2b42006-10-03 01:15:46 -07006670 if (mddev->flags)
6671 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006672
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006673 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006674 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006675 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006676
NeilBrown06d91a52005-06-21 17:17:12 -07006677
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6679 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6680 /* resync/recovery still happening */
6681 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6682 goto unlock;
6683 }
6684 if (mddev->sync_thread) {
6685 /* resync has finished, collect result */
6686 md_unregister_thread(mddev->sync_thread);
6687 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006688 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6689 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 /* success...*/
6691 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006692 if (mddev->pers->spare_active(mddev))
6693 sysfs_notify(&mddev->kobj, NULL,
6694 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006695 }
NeilBrowncea9c222009-03-31 15:15:05 +11006696 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6697 mddev->pers->finish_reshape)
6698 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006699 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006700
6701 /* if array is no-longer degraded, then any saved_raid_disk
6702 * information must be scrapped
6703 */
6704 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006705 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006706 rdev->saved_raid_disk = -1;
6707
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 mddev->recovery = 0;
6709 /* flag recovery needed just to double check */
6710 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006711 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006712 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006713 goto unlock;
6714 }
Neil Brown72a23c22008-06-28 08:31:41 +10006715 /* Set RUNNING before clearing NEEDED to avoid
6716 * any transients in the value of "sync_action".
6717 */
6718 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6719 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006720 /* Clear some bits that don't mean anything, but
6721 * might be left set
6722 */
NeilBrown24dd4692005-11-08 21:39:26 -08006723 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6724 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006726 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6727 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 /* no recovery is running.
6729 * remove any failed drives, then
6730 * add spares if possible.
6731 * Spare are also removed and re-added, to allow
6732 * the personality to fail the re-add.
6733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006735 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006736 if (mddev->pers->check_reshape == NULL ||
6737 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006738 /* Cannot proceed */
6739 goto unlock;
6740 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006741 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006742 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006743 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6744 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006745 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006746 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006747 } else if (mddev->recovery_cp < MaxSector) {
6748 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006749 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006750 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6751 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006752 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006753
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006755 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6756 /* We are adding a device or devices to an array
6757 * which has the bitmap stored on all devices.
6758 * So make sure all bitmap pages get written
6759 */
6760 bitmap_write_all(mddev->bitmap);
6761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 mddev->sync_thread = md_register_thread(md_do_sync,
6763 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006764 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 if (!mddev->sync_thread) {
6766 printk(KERN_ERR "%s: could not start resync"
6767 " thread...\n",
6768 mdname(mddev));
6769 /* leave the spares where they are, it shouldn't hurt */
6770 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006771 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006773 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006774 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006775 }
6776 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006777 if (!mddev->sync_thread) {
6778 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6779 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6780 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006781 if (mddev->sysfs_action)
6782 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006784 mddev_unlock(mddev);
6785 }
6786}
6787
Dan Williams6bfe0b42008-04-30 00:52:32 -07006788void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6789{
NeilBrown3c0ee632008-10-21 13:25:28 +11006790 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006791 wait_event_timeout(rdev->blocked_wait,
6792 !test_bit(Blocked, &rdev->flags),
6793 msecs_to_jiffies(5000));
6794 rdev_dec_pending(rdev, mddev);
6795}
6796EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6797
Adrian Bunk75c96f82005-05-05 16:16:09 -07006798static int md_notify_reboot(struct notifier_block *this,
6799 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800{
6801 struct list_head *tmp;
6802 mddev_t *mddev;
6803
6804 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6805
6806 printk(KERN_INFO "md: stopping all md devices.\n");
6807
NeilBrown29ac4aa2008-02-06 01:39:58 -08006808 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006809 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006810 /* Force a switch to readonly even array
6811 * appears to still be in use. Hence
6812 * the '100'.
6813 */
NeilBrownd710e132008-10-13 11:55:12 +11006814 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006815 mddev_unlock(mddev);
6816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 /*
6818 * certain more exotic SCSI devices are known to be
6819 * volatile wrt too early system reboots. While the
6820 * right place to handle this issue is the given
6821 * driver, we do want to have a safe RAID driver ...
6822 */
6823 mdelay(1000*1);
6824 }
6825 return NOTIFY_DONE;
6826}
6827
Adrian Bunk75c96f82005-05-05 16:16:09 -07006828static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006829 .notifier_call = md_notify_reboot,
6830 .next = NULL,
6831 .priority = INT_MAX, /* before any real devices */
6832};
6833
6834static void md_geninit(void)
6835{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006836 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6837
Denis V. Lunevc7705f32008-04-29 01:02:35 -07006838 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839}
6840
Adrian Bunk75c96f82005-05-05 16:16:09 -07006841static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006843 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844 return -1;
6845 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006846 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 return -1;
6848 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006849 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006850 md_probe, NULL, NULL);
6851 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 md_probe, NULL, NULL);
6853
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006855 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006856
6857 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859}
6860
6861
6862#ifndef MODULE
6863
6864/*
6865 * Searches all registered partitions for autorun RAID arrays
6866 * at boot time.
6867 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006868
6869static LIST_HEAD(all_detected_devices);
6870struct detected_devices_node {
6871 struct list_head list;
6872 dev_t dev;
6873};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874
6875void md_autodetect_dev(dev_t dev)
6876{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006877 struct detected_devices_node *node_detected_dev;
6878
6879 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6880 if (node_detected_dev) {
6881 node_detected_dev->dev = dev;
6882 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6883 } else {
6884 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6885 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006887}
6888
6889
6890static void autostart_arrays(int part)
6891{
6892 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006893 struct detected_devices_node *node_detected_dev;
6894 dev_t dev;
6895 int i_scanned, i_passed;
6896
6897 i_scanned = 0;
6898 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
6900 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6901
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006902 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6903 i_scanned++;
6904 node_detected_dev = list_entry(all_detected_devices.next,
6905 struct detected_devices_node, list);
6906 list_del(&node_detected_dev->list);
6907 dev = node_detected_dev->dev;
6908 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006909 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910 if (IS_ERR(rdev))
6911 continue;
6912
NeilBrownb2d444d2005-11-08 21:39:31 -08006913 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006914 MD_BUG();
6915 continue;
6916 }
NeilBrownd0fae182008-03-04 14:29:31 -08006917 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006918 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006919 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006920 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006921
6922 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6923 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006924
6925 autorun_devices(part);
6926}
6927
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006928#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
6930static __exit void md_exit(void)
6931{
6932 mddev_t *mddev;
6933 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006934
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006935 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006936 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006938 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006939 unregister_blkdev(mdp_major, "mdp");
6940 unregister_reboot_notifier(&md_notifier);
6941 unregister_sysctl_table(raid_table_header);
6942 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006943 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006945 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 }
6947}
6948
Dan Williams685784a2007-07-09 11:56:42 -07006949subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950module_exit(md_exit)
6951
NeilBrownf91de922005-11-08 21:39:36 -08006952static int get_ro(char *buffer, struct kernel_param *kp)
6953{
6954 return sprintf(buffer, "%d", start_readonly);
6955}
6956static int set_ro(const char *val, struct kernel_param *kp)
6957{
6958 char *e;
6959 int num = simple_strtoul(val, &e, 10);
6960 if (*val && (*e == '\0' || *e == '\n')) {
6961 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006962 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006963 }
6964 return -EINVAL;
6965}
6966
NeilBrown80ca3a42006-07-10 04:44:18 -07006967module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6968module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8e2006-01-06 00:20:15 -08006969
NeilBrownefeb53c2009-01-09 08:31:10 +11006970module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006971
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972EXPORT_SYMBOL(register_md_personality);
6973EXPORT_SYMBOL(unregister_md_personality);
6974EXPORT_SYMBOL(md_error);
6975EXPORT_SYMBOL(md_done_sync);
6976EXPORT_SYMBOL(md_write_start);
6977EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006978EXPORT_SYMBOL(md_register_thread);
6979EXPORT_SYMBOL(md_unregister_thread);
6980EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006981EXPORT_SYMBOL(md_check_recovery);
6982MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006983MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07006984MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);