blob: 1be7a16a7a53898196b4b9f03894e755e73d1894 [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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 .procname = "speed_limit_min",
102 .data = &sysctl_speed_limit_min,
103 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700104 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800105 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 },
107 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .procname = "speed_limit_max",
109 .data = &sysctl_speed_limit_max,
110 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700111 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800112 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800114 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117static ctl_table raid_dir_table[] = {
118 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .procname = "raid",
120 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700121 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .child = raid_table,
123 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800124 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125};
126
127static ctl_table raid_root_table[] = {
128 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 .procname = "dev",
130 .maxlen = 0,
131 .mode = 0555,
132 .child = raid_dir_table,
133 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800134 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700137static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
NeilBrownf91de922005-11-08 21:39:36 -0800139static int start_readonly;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141/*
NeilBrownd7603b72006-01-06 00:20:30 -0800142 * We have a system wide 'event count' that is incremented
143 * on any 'interesting' event, and readers of /proc/mdstat
144 * can use 'poll' or 'select' to find out when the event
145 * count increases.
146 *
147 * Events are:
148 * start array, stop array, error, add device, remove device,
149 * start build, activate spare
150 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800151static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800152static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800153void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800154{
155 atomic_inc(&md_event_count);
156 wake_up(&md_event_waiters);
157}
NeilBrown29269552006-03-27 01:18:10 -0800158EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800159
NeilBrownc331eb02006-05-30 21:27:13 -0700160/* Alternate version that can be called from interrupts
161 * when calling sysfs_notify isn't needed.
162 */
Adrian Bunk05381952006-06-26 00:28:01 -0700163static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700164{
165 atomic_inc(&md_event_count);
166 wake_up(&md_event_waiters);
167}
168
NeilBrownd7603b72006-01-06 00:20:30 -0800169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * Enables to iterate over all existing md arrays
171 * all_mddevs_lock protects this list.
172 */
173static LIST_HEAD(all_mddevs);
174static DEFINE_SPINLOCK(all_mddevs_lock);
175
176
177/*
178 * iterates through all used mddevs in the system.
179 * We take care to grab the all_mddevs_lock whenever navigating
180 * the list, and to always hold a refcount when unlocked.
181 * Any code which breaks out of this loop while own
182 * a reference to the current mddev and must mddev_put it.
183 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800184#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 \
186 for (({ spin_lock(&all_mddevs_lock); \
187 tmp = all_mddevs.next; \
188 mddev = NULL;}); \
189 ({ if (tmp != &all_mddevs) \
190 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
191 spin_unlock(&all_mddevs_lock); \
192 if (mddev) mddev_put(mddev); \
193 mddev = list_entry(tmp, mddev_t, all_mddevs); \
194 tmp != &all_mddevs;}); \
195 ({ spin_lock(&all_mddevs_lock); \
196 tmp = tmp->next;}) \
197 )
198
199
NeilBrown409c57f2009-03-31 14:39:39 +1100200/* Rather than calling directly into the personality make_request function,
201 * IO requests come here first so that we can check if the device is
202 * being suspended pending a reconfiguration.
203 * We hold a refcount over the call to ->make_request. By the time that
204 * call has finished, the bio has been linked into some internal structure
205 * and so is visible to ->quiesce(), so we don't need the refcount any more.
206 */
207static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
NeilBrown409c57f2009-03-31 14:39:39 +1100209 mddev_t *mddev = q->queuedata;
210 int rv;
211 if (mddev == NULL || mddev->pers == NULL) {
212 bio_io_error(bio);
213 return 0;
214 }
215 rcu_read_lock();
216 if (mddev->suspended) {
217 DEFINE_WAIT(__wait);
218 for (;;) {
219 prepare_to_wait(&mddev->sb_wait, &__wait,
220 TASK_UNINTERRUPTIBLE);
221 if (!mddev->suspended)
222 break;
223 rcu_read_unlock();
224 schedule();
225 rcu_read_lock();
226 }
227 finish_wait(&mddev->sb_wait, &__wait);
228 }
229 atomic_inc(&mddev->active_io);
230 rcu_read_unlock();
231 rv = mddev->pers->make_request(q, bio);
232 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
233 wake_up(&mddev->sb_wait);
234
235 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
NeilBrown409c57f2009-03-31 14:39:39 +1100238static void mddev_suspend(mddev_t *mddev)
239{
240 BUG_ON(mddev->suspended);
241 mddev->suspended = 1;
242 synchronize_rcu();
243 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
244 mddev->pers->quiesce(mddev, 1);
245 md_unregister_thread(mddev->thread);
246 mddev->thread = NULL;
247 /* we now know that no code is executing in the personality module,
248 * except possibly the tail end of a ->bi_end_io function, but that
249 * is certain to complete before the module has a chance to get
250 * unloaded
251 */
252}
253
254static void mddev_resume(mddev_t *mddev)
255{
256 mddev->suspended = 0;
257 wake_up(&mddev->sb_wait);
258 mddev->pers->quiesce(mddev, 0);
259}
260
NeilBrown3fa841d2009-09-23 18:10:29 +1000261int mddev_congested(mddev_t *mddev, int bits)
262{
263 return mddev->suspended;
264}
265EXPORT_SYMBOL(mddev_congested);
266
NeilBrown409c57f2009-03-31 14:39:39 +1100267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static inline mddev_t *mddev_get(mddev_t *mddev)
269{
270 atomic_inc(&mddev->active);
271 return mddev;
272}
273
Dan Williams5fd3a172009-03-04 00:57:25 -0700274static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static void mddev_put(mddev_t *mddev)
277{
278 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
279 return;
NeilBrownd3374822009-01-09 08:31:10 +1100280 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
281 !mddev->hold_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100283 if (mddev->gendisk) {
284 /* we did a probe so need to clean up.
285 * Call schedule_work inside the spinlock
286 * so that flush_scheduled_work() after
287 * mddev_find will succeed in waiting for the
288 * work to be done.
289 */
290 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
291 schedule_work(&mddev->del_work);
292 } else
293 kfree(mddev);
294 }
295 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
298static mddev_t * mddev_find(dev_t unit)
299{
300 mddev_t *mddev, *new = NULL;
301
302 retry:
303 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
NeilBrownefeb53c2009-01-09 08:31:10 +1100305 if (unit) {
306 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
307 if (mddev->unit == unit) {
308 mddev_get(mddev);
309 spin_unlock(&all_mddevs_lock);
310 kfree(new);
311 return mddev;
312 }
313
314 if (new) {
315 list_add(&new->all_mddevs, &all_mddevs);
316 spin_unlock(&all_mddevs_lock);
317 new->hold_active = UNTIL_IOCTL;
318 return new;
319 }
320 } else if (new) {
321 /* find an unused unit number */
322 static int next_minor = 512;
323 int start = next_minor;
324 int is_free = 0;
325 int dev = 0;
326 while (!is_free) {
327 dev = MKDEV(MD_MAJOR, next_minor);
328 next_minor++;
329 if (next_minor > MINORMASK)
330 next_minor = 0;
331 if (next_minor == start) {
332 /* Oh dear, all in use. */
333 spin_unlock(&all_mddevs_lock);
334 kfree(new);
335 return NULL;
336 }
337
338 is_free = 1;
339 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
340 if (mddev->unit == dev) {
341 is_free = 0;
342 break;
343 }
344 }
345 new->unit = dev;
346 new->md_minor = MINOR(dev);
347 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 list_add(&new->all_mddevs, &all_mddevs);
349 spin_unlock(&all_mddevs_lock);
350 return new;
351 }
352 spin_unlock(&all_mddevs_lock);
353
NeilBrown9ffae0c2006-01-06 00:20:32 -0800354 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!new)
356 return NULL;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 new->unit = unit;
359 if (MAJOR(unit) == MD_MAJOR)
360 new->md_minor = MINOR(unit);
361 else
362 new->md_minor = MINOR(unit) >> MdpMinorShift;
363
NeilBrownc8c00a62009-08-10 12:50:52 +1000364 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800365 mutex_init(&new->reconfig_mutex);
NeilBrownaa5cbd12009-12-14 12:49:46 +1100366 mutex_init(&new->bitmap_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 INIT_LIST_HEAD(&new->disks);
368 INIT_LIST_HEAD(&new->all_mddevs);
369 init_timer(&new->safemode_timer);
370 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000371 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100372 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700373 spin_lock_init(&new->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -0700374 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700375 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700376 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000377 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800378 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700379 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 goto retry;
382}
383
384static inline int mddev_lock(mddev_t * mddev)
385{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800386 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Dan Williamsb522adc2009-03-31 15:00:31 +1100389static inline int mddev_is_locked(mddev_t *mddev)
390{
391 return mutex_is_locked(&mddev->reconfig_mutex);
392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static inline int mddev_trylock(mddev_t * mddev)
395{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800396 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399static inline void mddev_unlock(mddev_t * mddev)
400{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800401 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
NeilBrown005eca52005-08-22 13:11:08 -0700403 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
NeilBrown2989ddb2006-01-06 00:20:43 -0800406static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100408 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100410 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 if (rdev->desc_nr == nr)
412 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return NULL;
415}
416
417static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
418{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 mdk_rdev_t *rdev;
420
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100421 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (rdev->bdev->bd_dev == dev)
423 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return NULL;
426}
427
NeilBrownd9d166c2006-01-06 00:20:51 -0800428static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800429{
430 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800431 list_for_each_entry(pers, &pers_list, list) {
432 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800433 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800434 if (strcmp(pers->name, clevel)==0)
435 return pers;
436 }
NeilBrown2604b702006-01-06 00:20:36 -0800437 return NULL;
438}
439
Andre Nollb73df2d2008-07-11 22:02:23 +1000440/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700441static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Andre Nollb73df2d2008-07-11 22:02:23 +1000443 sector_t num_sectors = bdev->bd_inode->i_size / 512;
444 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static int alloc_disk_sb(mdk_rdev_t * rdev)
448{
449 if (rdev->sb_page)
450 MD_BUG();
451
452 rdev->sb_page = alloc_page(GFP_KERNEL);
453 if (!rdev->sb_page) {
454 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000455 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
458 return 0;
459}
460
461static void free_disk_sb(mdk_rdev_t * rdev)
462{
463 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800464 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 rdev->sb_loaded = 0;
466 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000467 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100468 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470}
471
472
NeilBrown6712ecf2007-09-27 12:47:43 +0200473static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700474{
475 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800476 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700477
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700478 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
479 printk("md: super_written gets error=%d, uptodate=%d\n",
480 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
481 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800482 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700483 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700484
NeilBrowna9701a32005-11-08 21:39:34 -0800485 if (atomic_dec_and_test(&mddev->pending_writes))
486 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700487 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700488}
489
NeilBrown6712ecf2007-09-27 12:47:43 +0200490static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800491{
492 struct bio *bio2 = bio->bi_private;
493 mdk_rdev_t *rdev = bio2->bi_private;
494 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800495
496 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
497 error == -EOPNOTSUPP) {
498 unsigned long flags;
499 /* barriers don't appear to be supported :-( */
500 set_bit(BarriersNotsupp, &rdev->flags);
501 mddev->barriers_work = 0;
502 spin_lock_irqsave(&mddev->write_lock, flags);
503 bio2->bi_next = mddev->biolist;
504 mddev->biolist = bio2;
505 spin_unlock_irqrestore(&mddev->write_lock, flags);
506 wake_up(&mddev->sb_wait);
507 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200508 } else {
509 bio_put(bio2);
510 bio->bi_private = rdev;
511 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800512 }
NeilBrowna9701a32005-11-08 21:39:34 -0800513}
514
NeilBrown7bfa19f2005-06-21 17:17:28 -0700515void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
516 sector_t sector, int size, struct page *page)
517{
518 /* write first size bytes of page to sector of rdev
519 * Increment mddev->pending_writes before returning
520 * and decrement it on completion, waking up sb_wait
521 * if zero is reached.
522 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800523 *
524 * As we might need to resubmit the request if BIO_RW_BARRIER
525 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700526 */
527 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100528 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700529
530 bio->bi_bdev = rdev->bdev;
531 bio->bi_sector = sector;
532 bio_add_page(bio, page, size, 0);
533 bio->bi_private = rdev;
534 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800535 bio->bi_rw = rw;
536
NeilBrown7bfa19f2005-06-21 17:17:28 -0700537 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800538 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
539 struct bio *rbio;
540 rw |= (1<<BIO_RW_BARRIER);
541 rbio = bio_clone(bio, GFP_NOIO);
542 rbio->bi_private = bio;
543 rbio->bi_end_io = super_written_barrier;
544 submit_bio(rw, rbio);
545 } else
546 submit_bio(rw, bio);
547}
548
549void md_super_wait(mddev_t *mddev)
550{
551 /* wait for all superblock writes that were scheduled to complete.
552 * if any had to be retried (due to BARRIER problems), retry them
553 */
554 DEFINE_WAIT(wq);
555 for(;;) {
556 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
557 if (atomic_read(&mddev->pending_writes)==0)
558 break;
559 while (mddev->biolist) {
560 struct bio *bio;
561 spin_lock_irq(&mddev->write_lock);
562 bio = mddev->biolist;
563 mddev->biolist = bio->bi_next ;
564 bio->bi_next = NULL;
565 spin_unlock_irq(&mddev->write_lock);
566 submit_bio(bio->bi_rw, bio);
567 }
568 schedule();
569 }
570 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700571}
572
NeilBrown6712ecf2007-09-27 12:47:43 +0200573static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
NeilBrowna654b9d82005-06-21 17:17:27 -0700578int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct page *page, int rw)
580{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700581 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct completion event;
583 int ret;
584
Jens Axboe93dbb392009-02-16 10:25:40 +0100585 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 bio->bi_bdev = bdev;
588 bio->bi_sector = sector;
589 bio_add_page(bio, page, size, 0);
590 init_completion(&event);
591 bio->bi_private = &event;
592 bio->bi_end_io = bi_complete;
593 submit_bio(rw, bio);
594 wait_for_completion(&event);
595
596 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
597 bio_put(bio);
598 return ret;
599}
NeilBrowna8745db2006-01-06 00:20:34 -0800600EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
NeilBrown0002b272005-09-09 16:23:53 -0700602static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 char b[BDEVNAME_SIZE];
605 if (!rdev->sb_page) {
606 MD_BUG();
607 return -EINVAL;
608 }
609 if (rdev->sb_loaded)
610 return 0;
611
612
Andre Noll0f420352008-07-11 22:02:23 +1000613 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto fail;
615 rdev->sb_loaded = 1;
616 return 0;
617
618fail:
619 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
620 bdevname(rdev->bdev,b));
621 return -EINVAL;
622}
623
624static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
625{
Andre Noll05710462008-07-11 22:02:20 +1000626 return sb1->set_uuid0 == sb2->set_uuid0 &&
627 sb1->set_uuid1 == sb2->set_uuid1 &&
628 sb1->set_uuid2 == sb2->set_uuid2 &&
629 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
633{
634 int ret;
635 mdp_super_t *tmp1, *tmp2;
636
637 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
638 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
639
640 if (!tmp1 || !tmp2) {
641 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100642 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 goto abort;
644 }
645
646 *tmp1 = *sb1;
647 *tmp2 = *sb2;
648
649 /*
650 * nr_disks is not constant
651 */
652 tmp1->nr_disks = 0;
653 tmp2->nr_disks = 0;
654
Andre Nollce0c8e02008-07-11 22:02:20 +1000655 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700657 kfree(tmp1);
658 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return ret;
660}
661
NeilBrown4d167f02007-05-09 02:35:37 -0700662
663static u32 md_csum_fold(u32 csum)
664{
665 csum = (csum & 0xffff) + (csum >> 16);
666 return (csum & 0xffff) + (csum >> 16);
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static unsigned int calc_sb_csum(mdp_super_t * sb)
670{
NeilBrown4d167f02007-05-09 02:35:37 -0700671 u64 newcsum = 0;
672 u32 *sb32 = (u32*)sb;
673 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned int disk_csum, csum;
675
676 disk_csum = sb->sb_csum;
677 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700678
679 for (i = 0; i < MD_SB_BYTES/4 ; i++)
680 newcsum += sb32[i];
681 csum = (newcsum & 0xffffffff) + (newcsum>>32);
682
683
684#ifdef CONFIG_ALPHA
685 /* This used to use csum_partial, which was wrong for several
686 * reasons including that different results are returned on
687 * different architectures. It isn't critical that we get exactly
688 * the same return value as before (we always csum_fold before
689 * testing, and that removes any differences). However as we
690 * know that csum_partial always returned a 16bit value on
691 * alphas, do a fold to maximise conformity to previous behaviour.
692 */
693 sb->sb_csum = md_csum_fold(disk_csum);
694#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700696#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return csum;
698}
699
700
701/*
702 * Handle superblock details.
703 * We want to be able to handle multiple superblock formats
704 * so we have a common interface to them all, and an array of
705 * different handlers.
706 * We rely on user-space to write the initial superblock, and support
707 * reading and updating of superblocks.
708 * Interface methods are:
709 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
710 * loads and validates a superblock on dev.
711 * if refdev != NULL, compare superblocks on both devices
712 * Return:
713 * 0 - dev has a superblock that is compatible with refdev
714 * 1 - dev has a superblock that is compatible and newer than refdev
715 * so dev should be used as the refdev in future
716 * -EINVAL superblock incompatible or invalid
717 * -othererror e.g. -EIO
718 *
719 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
720 * Verify that dev is acceptable into mddev.
721 * The first time, mddev->raid_disks will be 0, and data from
722 * dev should be merged in. Subsequent calls check that dev
723 * is new enough. Return 0 or -EINVAL
724 *
725 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
726 * Update the superblock for rdev with data in mddev
727 * This does not write to disc.
728 *
729 */
730
731struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000732 char *name;
733 struct module *owner;
734 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
735 int minor_version);
736 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
737 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
738 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000739 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740};
741
742/*
Andre Noll0894cc32009-06-18 08:49:23 +1000743 * Check that the given mddev has no bitmap.
744 *
745 * This function is called from the run method of all personalities that do not
746 * support bitmaps. It prints an error message and returns non-zero if mddev
747 * has a bitmap. Otherwise, it returns 0.
748 *
749 */
750int md_check_no_bitmap(mddev_t *mddev)
751{
752 if (!mddev->bitmap_file && !mddev->bitmap_offset)
753 return 0;
754 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
755 mdname(mddev), mddev->pers->name);
756 return 1;
757}
758EXPORT_SYMBOL(md_check_no_bitmap);
759
760/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * load_super for 0.90.0
762 */
763static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
764{
765 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
766 mdp_super_t *sb;
767 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /*
Andre Noll0f420352008-07-11 22:02:23 +1000770 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * it's at the end of the disk.
772 *
773 * It also happens to be a multiple of 4Kb.
774 */
Andre Noll0f420352008-07-11 22:02:23 +1000775 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
NeilBrown0002b272005-09-09 16:23:53 -0700777 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (ret) return ret;
779
780 ret = -EINVAL;
781
782 bdevname(rdev->bdev, b);
783 sb = (mdp_super_t*)page_address(rdev->sb_page);
784
785 if (sb->md_magic != MD_SB_MAGIC) {
786 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
787 b);
788 goto abort;
789 }
790
791 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800792 sb->minor_version < 90 ||
793 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
795 sb->major_version, sb->minor_version,
796 b);
797 goto abort;
798 }
799
800 if (sb->raid_disks <= 0)
801 goto abort;
802
NeilBrown4d167f02007-05-09 02:35:37 -0700803 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
805 b);
806 goto abort;
807 }
808
809 rdev->preferred_minor = sb->md_minor;
810 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700811 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (sb->level == LEVEL_MULTIPATH)
814 rdev->desc_nr = -1;
815 else
816 rdev->desc_nr = sb->this_disk.number;
817
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700818 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700820 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 __u64 ev1, ev2;
822 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
823 if (!uuid_equal(refsb, sb)) {
824 printk(KERN_WARNING "md: %s has different UUID to %s\n",
825 b, bdevname(refdev->bdev,b2));
826 goto abort;
827 }
828 if (!sb_equal(refsb, sb)) {
829 printk(KERN_WARNING "md: %s has same UUID"
830 " but different superblock to %s\n",
831 b, bdevname(refdev->bdev, b2));
832 goto abort;
833 }
834 ev1 = md_event(sb);
835 ev2 = md_event(refsb);
836 if (ev1 > ev2)
837 ret = 1;
838 else
839 ret = 0;
840 }
NeilBrown8190e752009-06-18 08:48:58 +1000841 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Andre Nolldd8ac332009-03-31 14:33:13 +1100843 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800844 /* "this cannot possibly happen" ... */
845 ret = -EINVAL;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 abort:
848 return ret;
849}
850
851/*
852 * validate_super for 0.90.0
853 */
854static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
855{
856 mdp_disk_t *desc;
857 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700858 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
NeilBrown41158c72005-06-21 17:17:25 -0700860 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800861 clear_bit(Faulty, &rdev->flags);
862 clear_bit(In_sync, &rdev->flags);
863 clear_bit(WriteMostly, &rdev->flags);
864 clear_bit(BarriersNotsupp, &rdev->flags);
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (mddev->raid_disks == 0) {
867 mddev->major_version = 0;
868 mddev->minor_version = sb->minor_version;
869 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -0800870 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +1000871 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 mddev->ctime = sb->ctime;
873 mddev->utime = sb->utime;
874 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -0800875 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 mddev->layout = sb->layout;
877 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +1100878 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -0700879 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -0700880 mddev->bitmap_offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -0700881 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
NeilBrownf6705572006-03-27 01:18:11 -0800883 if (mddev->minor_version >= 91) {
884 mddev->reshape_position = sb->reshape_position;
885 mddev->delta_disks = sb->delta_disks;
886 mddev->new_level = sb->new_level;
887 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000888 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -0800889 } else {
890 mddev->reshape_position = MaxSector;
891 mddev->delta_disks = 0;
892 mddev->new_level = mddev->level;
893 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +1000894 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -0800895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (sb->state & (1<<MD_SB_CLEAN))
898 mddev->recovery_cp = MaxSector;
899 else {
900 if (sb->events_hi == sb->cp_events_hi &&
901 sb->events_lo == sb->cp_events_lo) {
902 mddev->recovery_cp = sb->recovery_cp;
903 } else
904 mddev->recovery_cp = 0;
905 }
906
907 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
908 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
909 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
910 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
911
912 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -0700913
914 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrowne11e93f2007-05-09 02:35:36 -0700915 mddev->bitmap_file == NULL)
NeilBrown36fa3062005-09-09 16:23:45 -0700916 mddev->bitmap_offset = mddev->default_bitmap_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -0700917
NeilBrown41158c72005-06-21 17:17:25 -0700918 } else if (mddev->pers == NULL) {
919 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 ++ev1;
921 if (ev1 < mddev->events)
922 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -0700923 } else if (mddev->bitmap) {
924 /* if adding to array with a bitmap, then we can accept an
925 * older device ... but not too old.
926 */
NeilBrown41158c72005-06-21 17:17:25 -0700927 if (ev1 < mddev->bitmap->events_cleared)
928 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -0700929 } else {
930 if (ev1 < mddev->events)
931 /* just a hot-add of a new device, leave raid_disk at -1 */
932 return 0;
933 }
NeilBrown41158c72005-06-21 17:17:25 -0700934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 desc = sb->disks + rdev->desc_nr;
937
938 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -0800939 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -0700940 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
941 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -0800942 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +1100944 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
945 /* active but not in sync implies recovery up to
946 * reshape position. We don't know exactly where
947 * that is, so set to zero for now */
948 if (mddev->minor_version >= 91) {
949 rdev->recovery_offset = 0;
950 rdev->raid_disk = desc->raid_disk;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -0700953 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
954 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -0700955 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -0800956 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return 0;
958}
959
960/*
961 * sync_super for 0.90.0
962 */
963static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
964{
965 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 mdk_rdev_t *rdev2;
967 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -0800968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /* make rdev->sb match mddev data..
971 *
972 * 1/ zero out disks
973 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
974 * 3/ any empty disks < next_spare become removed
975 *
976 * disks[0] gets initialised to REMOVED because
977 * we cannot be sure from other fields if it has
978 * been initialised or not.
979 */
980 int i;
981 int active=0, working=0,failed=0,spare=0,nr_disks=0;
982
NeilBrown61181562005-09-09 16:24:02 -0700983 rdev->sb_size = MD_SB_BYTES;
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 sb = (mdp_super_t*)page_address(rdev->sb_page);
986
987 memset(sb, 0, sizeof(*sb));
988
989 sb->md_magic = MD_SB_MAGIC;
990 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 sb->patch_version = mddev->patch_version;
992 sb->gvalid_words = 0; /* ignored */
993 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
994 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
995 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
996 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
997
998 sb->ctime = mddev->ctime;
999 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001000 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 sb->raid_disks = mddev->raid_disks;
1002 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001003 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 sb->utime = mddev->utime;
1005 sb->state = 0;
1006 sb->events_hi = (mddev->events>>32);
1007 sb->events_lo = (u32)mddev->events;
1008
NeilBrownf6705572006-03-27 01:18:11 -08001009 if (mddev->reshape_position == MaxSector)
1010 sb->minor_version = 90;
1011 else {
1012 sb->minor_version = 91;
1013 sb->reshape_position = mddev->reshape_position;
1014 sb->new_level = mddev->new_level;
1015 sb->delta_disks = mddev->delta_disks;
1016 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001017 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001018 }
1019 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (mddev->in_sync)
1021 {
1022 sb->recovery_cp = mddev->recovery_cp;
1023 sb->cp_events_hi = (mddev->events>>32);
1024 sb->cp_events_lo = (u32)mddev->events;
1025 if (mddev->recovery_cp == MaxSector)
1026 sb->state = (1<< MD_SB_CLEAN);
1027 } else
1028 sb->recovery_cp = 0;
1029
1030 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001031 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
NeilBrowna654b9d82005-06-21 17:17:27 -07001033 if (mddev->bitmap && mddev->bitmap_file == NULL)
1034 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001037 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001039 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001040 int is_active = test_bit(In_sync, &rdev2->flags);
1041
1042 if (rdev2->raid_disk >= 0 &&
1043 sb->minor_version >= 91)
1044 /* we have nowhere to store the recovery_offset,
1045 * but if it is not below the reshape_position,
1046 * we can piggy-back on that.
1047 */
1048 is_active = 1;
1049 if (rdev2->raid_disk < 0 ||
1050 test_bit(Faulty, &rdev2->flags))
1051 is_active = 0;
1052 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001053 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001055 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001056 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 d = &sb->disks[rdev2->desc_nr];
1058 nr_disks++;
1059 d->number = rdev2->desc_nr;
1060 d->major = MAJOR(rdev2->bdev->bd_dev);
1061 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001062 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 d->raid_disk = rdev2->raid_disk;
1064 else
1065 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001066 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001068 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001070 if (test_bit(In_sync, &rdev2->flags))
1071 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 active++;
1073 working++;
1074 } else {
1075 d->state = 0;
1076 spare++;
1077 working++;
1078 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001079 if (test_bit(WriteMostly, &rdev2->flags))
1080 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /* now set the "removed" and "faulty" bits on any missing devices */
1083 for (i=0 ; i < mddev->raid_disks ; i++) {
1084 mdp_disk_t *d = &sb->disks[i];
1085 if (d->state == 0 && d->number == 0) {
1086 d->number = i;
1087 d->raid_disk = i;
1088 d->state = (1<<MD_DISK_REMOVED);
1089 d->state |= (1<<MD_DISK_FAULTY);
1090 failed++;
1091 }
1092 }
1093 sb->nr_disks = nr_disks;
1094 sb->active_disks = active;
1095 sb->working_disks = working;
1096 sb->failed_disks = failed;
1097 sb->spare_disks = spare;
1098
1099 sb->this_disk = sb->disks[rdev->desc_nr];
1100 sb->sb_csum = calc_sb_csum(sb);
1101}
1102
1103/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001104 * rdev_size_change for 0.90.0
1105 */
1106static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001107super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001108{
Andre Noll58c0fed2009-03-31 14:33:13 +11001109 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001110 return 0; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001111 if (rdev->mddev->bitmap_offset)
1112 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001113 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001114 if (!num_sectors || num_sectors > rdev->sb_start)
1115 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001116 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001117 rdev->sb_page);
1118 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001119 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001120}
1121
1122
1123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 * version 1 superblock
1125 */
1126
NeilBrown1c05b4b2006-10-21 10:24:08 -07001127static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001129 __le32 disk_csum;
1130 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 unsigned long long newcsum;
1132 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001133 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 int i;
1135
1136 disk_csum = sb->sb_csum;
1137 sb->sb_csum = 0;
1138 newcsum = 0;
1139 for (i=0; size>=4; size -= 4 )
1140 newcsum += le32_to_cpu(*isuper++);
1141
1142 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001143 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1146 sb->sb_csum = disk_csum;
1147 return cpu_to_le32(csum);
1148}
1149
1150static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1151{
1152 struct mdp_superblock_1 *sb;
1153 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001154 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001156 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /*
Andre Noll0f420352008-07-11 22:02:23 +10001159 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * It is always aligned to a 4K boundary and
1161 * depeding on minor_version, it can be:
1162 * 0: At least 8K, but less than 12K, from end of device
1163 * 1: At start of device
1164 * 2: 4K from start of device.
1165 */
1166 switch(minor_version) {
1167 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001168 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1169 sb_start -= 8*2;
1170 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
1172 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001173 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001176 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 break;
1178 default:
1179 return -EINVAL;
1180 }
Andre Noll0f420352008-07-11 22:02:23 +10001181 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
NeilBrown0002b272005-09-09 16:23:53 -07001183 /* superblock is rarely larger than 1K, but it can be larger,
1184 * and it is safe to read 4k, so we do that
1185 */
1186 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (ret) return ret;
1188
1189
1190 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1191
1192 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1193 sb->major_version != cpu_to_le32(1) ||
1194 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001195 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001196 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return -EINVAL;
1198
1199 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1200 printk("md: invalid superblock checksum on %s\n",
1201 bdevname(rdev->bdev,b));
1202 return -EINVAL;
1203 }
1204 if (le64_to_cpu(sb->data_size) < 10) {
1205 printk("md: data_size too small on %s\n",
1206 bdevname(rdev->bdev,b));
1207 return -EINVAL;
1208 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 rdev->preferred_minor = 0xffff;
1211 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001212 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
NeilBrown0002b272005-09-09 16:23:53 -07001214 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001215 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001216 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001217 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1218
1219 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001220 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001221 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001222
NeilBrown31b65a02006-07-10 04:44:14 -07001223 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1224 rdev->desc_nr = -1;
1225 else
1226 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1227
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001228 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001229 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001230 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 __u64 ev1, ev2;
1232 struct mdp_superblock_1 *refsb =
1233 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1234
1235 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1236 sb->level != refsb->level ||
1237 sb->layout != refsb->layout ||
1238 sb->chunksize != refsb->chunksize) {
1239 printk(KERN_WARNING "md: %s has strangely different"
1240 " superblock to %s\n",
1241 bdevname(rdev->bdev,b),
1242 bdevname(refdev->bdev,b2));
1243 return -EINVAL;
1244 }
1245 ev1 = le64_to_cpu(sb->events);
1246 ev2 = le64_to_cpu(refsb->events);
1247
1248 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001249 ret = 1;
1250 else
1251 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
NeilBrowna1801f82008-03-04 14:29:31 -08001253 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001254 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1255 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001257 rdev->sectors = rdev->sb_start;
1258 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001260 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001261 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001262 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001263 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264}
1265
1266static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1267{
1268 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001269 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
NeilBrown41158c72005-06-21 17:17:25 -07001271 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001272 clear_bit(Faulty, &rdev->flags);
1273 clear_bit(In_sync, &rdev->flags);
1274 clear_bit(WriteMostly, &rdev->flags);
1275 clear_bit(BarriersNotsupp, &rdev->flags);
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (mddev->raid_disks == 0) {
1278 mddev->major_version = 1;
1279 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001280 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001281 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1283 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1284 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001285 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 mddev->layout = le32_to_cpu(sb->layout);
1287 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001288 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001289 mddev->events = ev1;
NeilBrown92232142005-08-18 11:24:16 -07001290 mddev->bitmap_offset = 0;
NeilBrown29fc7e32006-02-03 03:03:41 -08001291 mddev->default_bitmap_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1294 memcpy(mddev->uuid, sb->set_uuid, 16);
1295
1296 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001297
NeilBrown71c08052005-09-09 16:23:51 -07001298 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrowne11e93f2007-05-09 02:35:36 -07001299 mddev->bitmap_file == NULL )
NeilBrowna654b9d82005-06-21 17:17:27 -07001300 mddev->bitmap_offset = (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001301
NeilBrownf6705572006-03-27 01:18:11 -08001302 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1303 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1304 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1305 mddev->new_level = le32_to_cpu(sb->new_level);
1306 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001307 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001308 } else {
1309 mddev->reshape_position = MaxSector;
1310 mddev->delta_disks = 0;
1311 mddev->new_level = mddev->level;
1312 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001313 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001314 }
1315
NeilBrown41158c72005-06-21 17:17:25 -07001316 } else if (mddev->pers == NULL) {
1317 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 ++ev1;
1319 if (ev1 < mddev->events)
1320 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001321 } else if (mddev->bitmap) {
1322 /* If adding to array with a bitmap, then we can accept an
1323 * older device, but not too old.
1324 */
NeilBrown41158c72005-06-21 17:17:25 -07001325 if (ev1 < mddev->bitmap->events_cleared)
1326 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001327 } else {
1328 if (ev1 < mddev->events)
1329 /* just a hot-add of a new device, leave raid_disk at -1 */
1330 return 0;
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (mddev->level != LEVEL_MULTIPATH) {
1333 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001334 if (rdev->desc_nr < 0 ||
1335 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1336 role = 0xffff;
1337 rdev->desc_nr = -1;
1338 } else
1339 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 switch(role) {
1341 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 break;
1343 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001344 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 break;
1346 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001347 if ((le32_to_cpu(sb->feature_map) &
1348 MD_FEATURE_RECOVERY_OFFSET))
1349 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1350 else
1351 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 rdev->raid_disk = role;
1353 break;
1354 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001355 if (sb->devflags & WriteMostly1)
1356 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001357 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001358 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 return 0;
1361}
1362
1363static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1364{
1365 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 mdk_rdev_t *rdev2;
1367 int max_dev, i;
1368 /* make rdev->sb match mddev and rdev data. */
1369
1370 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1371
1372 sb->feature_map = 0;
1373 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001374 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 memset(sb->pad1, 0, sizeof(sb->pad1));
1376 memset(sb->pad2, 0, sizeof(sb->pad2));
1377 memset(sb->pad3, 0, sizeof(sb->pad3));
1378
1379 sb->utime = cpu_to_le64((__u64)mddev->utime);
1380 sb->events = cpu_to_le64(mddev->events);
1381 if (mddev->in_sync)
1382 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1383 else
1384 sb->resync_offset = cpu_to_le64(0);
1385
NeilBrown1c05b4b2006-10-21 10:24:08 -07001386 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001387
NeilBrownf0ca3402006-02-02 14:28:04 -08001388 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001389 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001390 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001391 sb->level = cpu_to_le32(mddev->level);
1392 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001393
NeilBrowna654b9d82005-06-21 17:17:27 -07001394 if (mddev->bitmap && mddev->bitmap_file == NULL) {
1395 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_offset);
NeilBrown71c08052005-09-09 16:23:51 -07001396 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001397 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001398
1399 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001400 !test_bit(In_sync, &rdev->flags)) {
NeilBrown97e4f422009-03-31 14:33:13 +11001401 if (rdev->recovery_offset > 0) {
1402 sb->feature_map |=
1403 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1404 sb->recovery_offset =
1405 cpu_to_le64(rdev->recovery_offset);
1406 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001407 }
1408
NeilBrownf6705572006-03-27 01:18:11 -08001409 if (mddev->reshape_position != MaxSector) {
1410 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1411 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1412 sb->new_layout = cpu_to_le32(mddev->new_layout);
1413 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1414 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001415 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001416 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001419 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 if (rdev2->desc_nr+1 > max_dev)
1421 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001422
NeilBrown70471da2009-08-03 10:59:57 +10001423 if (max_dev > le32_to_cpu(sb->max_dev)) {
1424 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001425 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001426 rdev->sb_size = max_dev * 2 + 256;
1427 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1428 if (rdev->sb_size & bmask)
1429 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 for (i=0; i<max_dev;i++)
1432 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1433
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001434 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001436 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001438 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001440 else if (rdev2->raid_disk >= 0 && rdev2->recovery_offset > 0)
1441 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 else
1443 sb->dev_roles[i] = cpu_to_le16(0xffff);
1444 }
1445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 sb->sb_csum = calc_sb_1_csum(sb);
1447}
1448
Chris Webb0cd17fe2008-06-28 08:31:46 +10001449static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001450super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001451{
1452 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001453 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001454 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001455 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001456 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001457 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001458 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1459 max_sectors -= rdev->data_offset;
1460 if (!num_sectors || num_sectors > max_sectors)
1461 num_sectors = max_sectors;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001462 } else if (rdev->mddev->bitmap_offset) {
1463 /* minor version 0 with bitmap we can't move */
1464 return 0;
1465 } else {
1466 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001467 sector_t sb_start;
1468 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1469 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001470 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001471 if (!num_sectors || num_sectors > max_sectors)
1472 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001473 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001474 }
1475 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001476 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001477 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001478 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001479 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001480 rdev->sb_page);
1481 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001482 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001483}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Adrian Bunk75c96f82005-05-05 16:16:09 -07001485static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 [0] = {
1487 .name = "0.90.0",
1488 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001489 .load_super = super_90_load,
1490 .validate_super = super_90_validate,
1491 .sync_super = super_90_sync,
1492 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 },
1494 [1] = {
1495 .name = "md-1",
1496 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001497 .load_super = super_1_load,
1498 .validate_super = super_1_validate,
1499 .sync_super = super_1_sync,
1500 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 },
1502};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1505{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001506 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
NeilBrown4b809912008-07-21 17:05:25 +10001508 rcu_read_lock();
1509 rdev_for_each_rcu(rdev, mddev1)
1510 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001511 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001512 rdev2->bdev->bd_contains) {
1513 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001514 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001515 }
1516 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 return 0;
1518}
1519
1520static LIST_HEAD(pending_raid_disks);
1521
Andre Nollac5e7112009-08-03 10:59:47 +10001522/*
1523 * Try to register data integrity profile for an mddev
1524 *
1525 * This is called when an array is started and after a disk has been kicked
1526 * from the array. It only succeeds if all working and active component devices
1527 * are integrity capable with matching profiles.
1528 */
1529int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001530{
Andre Nollac5e7112009-08-03 10:59:47 +10001531 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001532
Andre Nollac5e7112009-08-03 10:59:47 +10001533 if (list_empty(&mddev->disks))
1534 return 0; /* nothing to do */
1535 if (blk_get_integrity(mddev->gendisk))
1536 return 0; /* already registered */
1537 list_for_each_entry(rdev, &mddev->disks, same_set) {
1538 /* skip spares and non-functional disks */
1539 if (test_bit(Faulty, &rdev->flags))
1540 continue;
1541 if (rdev->raid_disk < 0)
1542 continue;
1543 /*
1544 * If at least one rdev is not integrity capable, we can not
1545 * enable data integrity for the md device.
1546 */
1547 if (!bdev_get_integrity(rdev->bdev))
1548 return -EINVAL;
1549 if (!reference) {
1550 /* Use the first rdev as the reference */
1551 reference = rdev;
1552 continue;
1553 }
1554 /* does this rdev's profile match the reference profile? */
1555 if (blk_integrity_compare(reference->bdev->bd_disk,
1556 rdev->bdev->bd_disk) < 0)
1557 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001558 }
Andre Nollac5e7112009-08-03 10:59:47 +10001559 /*
1560 * All component devices are integrity capable and have matching
1561 * profiles, register the common profile for the md device.
1562 */
1563 if (blk_integrity_register(mddev->gendisk,
1564 bdev_get_integrity(reference->bdev)) != 0) {
1565 printk(KERN_ERR "md: failed to register integrity for %s\n",
1566 mdname(mddev));
1567 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001568 }
Andre Nollac5e7112009-08-03 10:59:47 +10001569 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1570 mdname(mddev));
1571 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001572}
Andre Nollac5e7112009-08-03 10:59:47 +10001573EXPORT_SYMBOL(md_integrity_register);
1574
1575/* Disable data integrity if non-capable/non-matching disk is being added */
1576void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1577{
1578 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1579 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1580
1581 if (!bi_mddev) /* nothing to do */
1582 return;
1583 if (rdev->raid_disk < 0) /* skip spares */
1584 return;
1585 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1586 rdev->bdev->bd_disk) >= 0)
1587 return;
1588 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1589 blk_integrity_unregister(mddev->gendisk);
1590}
1591EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1594{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001595 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001596 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001597 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001598 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 if (rdev->mddev) {
1601 MD_BUG();
1602 return -EINVAL;
1603 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001604
1605 /* prevent duplicates */
1606 if (find_rdev(mddev, rdev->bdev->bd_dev))
1607 return -EEXIST;
1608
Andre Nolldd8ac332009-03-31 14:33:13 +11001609 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1610 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1611 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001612 if (mddev->pers) {
1613 /* Cannot change size, so fail
1614 * If mddev->level <= 0, then we don't care
1615 * about aligning sizes (e.g. linear)
1616 */
1617 if (mddev->level > 0)
1618 return -ENOSPC;
1619 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001620 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 /* Verify rdev->desc_nr is unique.
1624 * If it is -1, assign a free number, else
1625 * check number is not in use
1626 */
1627 if (rdev->desc_nr < 0) {
1628 int choice = 0;
1629 if (mddev->pers) choice = mddev->raid_disks;
1630 while (find_rdev_nr(mddev, choice))
1631 choice++;
1632 rdev->desc_nr = choice;
1633 } else {
1634 if (find_rdev_nr(mddev, rdev->desc_nr))
1635 return -EBUSY;
1636 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001637 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1638 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1639 mdname(mddev), mddev->max_disks);
1640 return -EBUSY;
1641 }
NeilBrown19133a42005-11-08 21:39:35 -08001642 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001643 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001644 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001647 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001648
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001649 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001650 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001651
Tejun Heo0762b8b2008-08-25 19:56:12 +09001652 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001653 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1654 kobject_del(&rdev->kobj);
1655 goto fail;
1656 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001657 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1658
NeilBrown4b809912008-07-21 17:05:25 +10001659 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001660 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001661
1662 /* May as well allow recovery to be retried once */
1663 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001666
1667 fail:
1668 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1669 b, mdname(mddev));
1670 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
NeilBrown177a99b2008-02-06 01:39:56 -08001673static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001674{
1675 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1676 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001677 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001678}
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1681{
1682 char b[BDEVNAME_SIZE];
1683 if (!rdev->mddev) {
1684 MD_BUG();
1685 return;
1686 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001687 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001688 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1690 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001691 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001692 sysfs_put(rdev->sysfs_state);
1693 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001694 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001695 * writing to 'remove' to "dev/state". We also need
1696 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001697 */
NeilBrown4b809912008-07-21 17:05:25 +10001698 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001699 INIT_WORK(&rdev->del_work, md_delayed_delete);
1700 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001701 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
1704/*
1705 * prevent the device from being mounted, repartitioned or
1706 * otherwise reused by a RAID array (or any other kernel
1707 * subsystem), by bd_claiming the device.
1708 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001709static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 int err = 0;
1712 struct block_device *bdev;
1713 char b[BDEVNAME_SIZE];
1714
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001715 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (IS_ERR(bdev)) {
1717 printk(KERN_ERR "md: could not open %s.\n",
1718 __bdevname(dev, b));
1719 return PTR_ERR(bdev);
1720 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001721 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (err) {
1723 printk(KERN_ERR "md: could not bd_claim %s.\n",
1724 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001725 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 return err;
1727 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001728 if (!shared)
1729 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 rdev->bdev = bdev;
1731 return err;
1732}
1733
1734static void unlock_rdev(mdk_rdev_t *rdev)
1735{
1736 struct block_device *bdev = rdev->bdev;
1737 rdev->bdev = NULL;
1738 if (!bdev)
1739 MD_BUG();
1740 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001741 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743
1744void md_autodetect_dev(dev_t dev);
1745
1746static void export_rdev(mdk_rdev_t * rdev)
1747{
1748 char b[BDEVNAME_SIZE];
1749 printk(KERN_INFO "md: export_rdev(%s)\n",
1750 bdevname(rdev->bdev,b));
1751 if (rdev->mddev)
1752 MD_BUG();
1753 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001755 if (test_bit(AutoDetected, &rdev->flags))
1756 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757#endif
1758 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001759 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760}
1761
1762static void kick_rdev_from_array(mdk_rdev_t * rdev)
1763{
1764 unbind_rdev_from_array(rdev);
1765 export_rdev(rdev);
1766}
1767
1768static void export_array(mddev_t *mddev)
1769{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001770 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
NeilBrownd089c6a2008-02-06 01:39:59 -08001772 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (!rdev->mddev) {
1774 MD_BUG();
1775 continue;
1776 }
1777 kick_rdev_from_array(rdev);
1778 }
1779 if (!list_empty(&mddev->disks))
1780 MD_BUG();
1781 mddev->raid_disks = 0;
1782 mddev->major_version = 0;
1783}
1784
1785static void print_desc(mdp_disk_t *desc)
1786{
1787 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1788 desc->major,desc->minor,desc->raid_disk,desc->state);
1789}
1790
Cheng Renquancd2ac932009-01-09 08:31:08 +11001791static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 int i;
1794
1795 printk(KERN_INFO
1796 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1797 sb->major_version, sb->minor_version, sb->patch_version,
1798 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1799 sb->ctime);
1800 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1801 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1802 sb->md_minor, sb->layout, sb->chunk_size);
1803 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1804 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1805 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1806 sb->failed_disks, sb->spare_disks,
1807 sb->sb_csum, (unsigned long)sb->events_lo);
1808
1809 printk(KERN_INFO);
1810 for (i = 0; i < MD_SB_DISKS; i++) {
1811 mdp_disk_t *desc;
1812
1813 desc = sb->disks + i;
1814 if (desc->number || desc->major || desc->minor ||
1815 desc->raid_disk || (desc->state && (desc->state != 4))) {
1816 printk(" D %2d: ", i);
1817 print_desc(desc);
1818 }
1819 }
1820 printk(KERN_INFO "md: THIS: ");
1821 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Cheng Renquancd2ac932009-01-09 08:31:08 +11001824static void print_sb_1(struct mdp_superblock_1 *sb)
1825{
1826 __u8 *uuid;
1827
1828 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001829 printk(KERN_INFO
1830 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%02x%02x%02x%02x"
1831 ":%02x%02x:%02x%02x:%02x%02x:%02x%02x%02x%02x%02x%02x>\n"
1832 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001833 le32_to_cpu(sb->major_version),
1834 le32_to_cpu(sb->feature_map),
1835 uuid[0], uuid[1], uuid[2], uuid[3],
1836 uuid[4], uuid[5], uuid[6], uuid[7],
1837 uuid[8], uuid[9], uuid[10], uuid[11],
1838 uuid[12], uuid[13], uuid[14], uuid[15],
1839 sb->set_name,
1840 (unsigned long long)le64_to_cpu(sb->ctime)
1841 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1842
1843 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001844 printk(KERN_INFO
1845 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001846 " RO:%llu\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001847 "md: Dev:%08x UUID: %02x%02x%02x%02x:%02x%02x:%02x%02x:%02x%02x"
1848 ":%02x%02x%02x%02x%02x%02x\n"
1849 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1850 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001851 le32_to_cpu(sb->level),
1852 (unsigned long long)le64_to_cpu(sb->size),
1853 le32_to_cpu(sb->raid_disks),
1854 le32_to_cpu(sb->layout),
1855 le32_to_cpu(sb->chunksize),
1856 (unsigned long long)le64_to_cpu(sb->data_offset),
1857 (unsigned long long)le64_to_cpu(sb->data_size),
1858 (unsigned long long)le64_to_cpu(sb->super_offset),
1859 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1860 le32_to_cpu(sb->dev_number),
1861 uuid[0], uuid[1], uuid[2], uuid[3],
1862 uuid[4], uuid[5], uuid[6], uuid[7],
1863 uuid[8], uuid[9], uuid[10], uuid[11],
1864 uuid[12], uuid[13], uuid[14], uuid[15],
1865 sb->devflags,
1866 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1867 (unsigned long long)le64_to_cpu(sb->events),
1868 (unsigned long long)le64_to_cpu(sb->resync_offset),
1869 le32_to_cpu(sb->sb_csum),
1870 le32_to_cpu(sb->max_dev)
1871 );
1872}
1873
1874static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11001877 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
1878 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08001879 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
1880 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11001882 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
1883 switch (major_version) {
1884 case 0:
1885 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
1886 break;
1887 case 1:
1888 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
1889 break;
1890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 } else
1892 printk(KERN_INFO "md: no rdev superblock!\n");
1893}
1894
Adrian Bunk5e563412006-06-26 00:27:42 -07001895static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001897 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 mdk_rdev_t *rdev;
1899 mddev_t *mddev;
1900 char b[BDEVNAME_SIZE];
1901
1902 printk("\n");
1903 printk("md: **********************************\n");
1904 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
1905 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08001906 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
NeilBrown32a76272005-06-21 17:17:14 -07001908 if (mddev->bitmap)
1909 bitmap_print_sb(mddev->bitmap);
1910 else
1911 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001912 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 printk("<%s>", bdevname(rdev->bdev,b));
1914 printk("\n");
1915
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001916 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11001917 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919 printk("md: **********************************\n");
1920 printk("\n");
1921}
1922
1923
NeilBrown42543762006-06-26 00:27:57 -07001924static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
NeilBrown42543762006-06-26 00:27:57 -07001926 /* Update each superblock (in-memory image), but
1927 * if we are allowed to, skip spares which already
1928 * have the right event counter, or have one earlier
1929 * (which would mean they aren't being marked as dirty
1930 * with the rest of the array)
1931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
NeilBrown5e865102009-11-12 12:08:04 +11001934 /* First make sure individual recovery_offsets are correct */
1935 list_for_each_entry(rdev, &mddev->disks, same_set) {
1936 if (rdev->raid_disk >= 0 &&
1937 !test_bit(In_sync, &rdev->flags) &&
1938 mddev->curr_resync_completed > rdev->recovery_offset)
1939 rdev->recovery_offset = mddev->curr_resync_completed;
1940
1941 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001942 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07001943 if (rdev->sb_events == mddev->events ||
1944 (nospares &&
1945 rdev->raid_disk < 0 &&
1946 (rdev->sb_events&1)==0 &&
1947 rdev->sb_events+1 == mddev->events)) {
1948 /* Don't update this superblock */
1949 rdev->sb_loaded = 2;
1950 } else {
1951 super_types[mddev->major_version].
1952 sync_super(mddev, rdev);
1953 rdev->sb_loaded = 1;
1954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956}
1957
NeilBrown850b2b42006-10-03 01:15:46 -07001958static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07001961 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07001962 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
NeilBrown1b57f132009-06-18 08:48:19 +10001964 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07001965 if (mddev->external)
1966 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08001968 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07001969
NeilBrown850b2b42006-10-03 01:15:46 -07001970 set_bit(MD_CHANGE_PENDING, &mddev->flags);
1971 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
1972 force_change = 1;
1973 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
1974 /* just a clean<-> dirty transition, possibly leave spares alone,
1975 * though if events isn't the right even/odd, we will have to do
1976 * spares after all
1977 */
1978 nospares = 1;
1979 if (force_change)
1980 nospares = 0;
1981 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07001982 /* If the array is degraded, then skipping spares is both
1983 * dangerous and fairly pointless.
1984 * Dangerous because a device that was removed from the array
1985 * might have a event_count that still looks up-to-date,
1986 * so it can be re-added without a resync.
1987 * Pointless because if there are any spares to skip,
1988 * then a recovery will happen and soon that array won't
1989 * be degraded any more and the spare can go back to sleep then.
1990 */
NeilBrown850b2b42006-10-03 01:15:46 -07001991 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07001992
NeilBrown06d91a52005-06-21 17:17:12 -07001993 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07001994
1995 /* If this is just a dirty<->clean transition, and the array is clean
1996 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07001997 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07001998 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08001999 && (mddev->events & 1)
2000 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002001 mddev->events--;
2002 else {
2003 /* otherwise we have to go forward and ... */
2004 mddev->events ++;
2005 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002006 /* .. if the array isn't clean, an 'even' event must also go
2007 * to spares. */
2008 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002009 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002010 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002011 /* otherwise an 'odd' event must go to spares */
2012 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002013 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002014 }
2015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 if (!mddev->events) {
2018 /*
2019 * oops, this 64-bit counter should never wrap.
2020 * Either we are in around ~1 trillion A.C., assuming
2021 * 1 reboot per second, or we have a bug:
2022 */
2023 MD_BUG();
2024 mddev->events --;
2025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 /*
2028 * do not write anything to disk if using
2029 * nonpersistent superblocks
2030 */
NeilBrown06d91a52005-06-21 17:17:12 -07002031 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002032 if (!mddev->external)
2033 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2034
NeilBrowna9701a32005-11-08 21:39:34 -08002035 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002036 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002038 }
NeilBrowne6910632008-02-06 01:39:51 -08002039 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002040 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 dprintk(KERN_INFO
2043 "md: updating %s RAID superblock on device (in sync %d)\n",
2044 mdname(mddev),mddev->in_sync);
2045
NeilBrown4ad13662007-07-17 04:06:13 -07002046 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002047 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 char b[BDEVNAME_SIZE];
2049 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002050 if (rdev->sb_loaded != 1)
2051 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002052 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 dprintk("(skipping faulty ");
2054
2055 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002056 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002057 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002058 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002059 rdev->sb_page);
2060 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2061 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002062 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002063 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 } else
2066 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002067 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /* only need to write one superblock... */
2069 break;
2070 }
NeilBrowna9701a32005-11-08 21:39:34 -08002071 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002072 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002073
NeilBrowna9701a32005-11-08 21:39:34 -08002074 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002075 if (mddev->in_sync != sync_req ||
2076 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002077 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002078 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002079 goto repeat;
2080 }
NeilBrown850b2b42006-10-03 01:15:46 -07002081 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002082 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002083 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002084 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2085 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Andre Noll7f6ce762008-03-23 18:34:54 +01002089/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002090 * We want to accept with case. For this we use cmd_match.
2091 */
2092static int cmd_match(const char *cmd, const char *str)
2093{
2094 /* See if cmd, written into a sysfs file, matches
2095 * str. They must either be the same, or cmd can
2096 * have a trailing newline
2097 */
2098 while (*cmd && *str && *cmd == *str) {
2099 cmd++;
2100 str++;
2101 }
2102 if (*cmd == '\n')
2103 cmd++;
2104 if (*str || *cmd)
2105 return 0;
2106 return 1;
2107}
2108
NeilBrown86e6ffd2005-11-08 21:39:24 -08002109struct rdev_sysfs_entry {
2110 struct attribute attr;
2111 ssize_t (*show)(mdk_rdev_t *, char *);
2112 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2113};
2114
2115static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002116state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002117{
2118 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002119 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002120
NeilBrownb2d444d2005-11-08 21:39:31 -08002121 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002122 len+= sprintf(page+len, "%sfaulty",sep);
2123 sep = ",";
2124 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002125 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002126 len += sprintf(page+len, "%sin_sync",sep);
2127 sep = ",";
2128 }
NeilBrownf6556752006-06-26 00:28:01 -07002129 if (test_bit(WriteMostly, &rdev->flags)) {
2130 len += sprintf(page+len, "%swrite_mostly",sep);
2131 sep = ",";
2132 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002133 if (test_bit(Blocked, &rdev->flags)) {
2134 len += sprintf(page+len, "%sblocked", sep);
2135 sep = ",";
2136 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002137 if (!test_bit(Faulty, &rdev->flags) &&
2138 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002139 len += sprintf(page+len, "%sspare", sep);
2140 sep = ",";
2141 }
2142 return len+sprintf(page+len, "\n");
2143}
2144
NeilBrown45dc2de2006-06-26 00:27:58 -07002145static ssize_t
2146state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2147{
2148 /* can write
2149 * faulty - simulates and error
2150 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002151 * writemostly - sets write_mostly
2152 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002153 * blocked - sets the Blocked flag
2154 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002155 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002156 */
2157 int err = -EINVAL;
2158 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2159 md_error(rdev->mddev, rdev);
2160 err = 0;
2161 } else if (cmd_match(buf, "remove")) {
2162 if (rdev->raid_disk >= 0)
2163 err = -EBUSY;
2164 else {
2165 mddev_t *mddev = rdev->mddev;
2166 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002167 if (mddev->pers)
2168 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002169 md_new_event(mddev);
2170 err = 0;
2171 }
NeilBrownf6556752006-06-26 00:28:01 -07002172 } else if (cmd_match(buf, "writemostly")) {
2173 set_bit(WriteMostly, &rdev->flags);
2174 err = 0;
2175 } else if (cmd_match(buf, "-writemostly")) {
2176 clear_bit(WriteMostly, &rdev->flags);
2177 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002178 } else if (cmd_match(buf, "blocked")) {
2179 set_bit(Blocked, &rdev->flags);
2180 err = 0;
2181 } else if (cmd_match(buf, "-blocked")) {
2182 clear_bit(Blocked, &rdev->flags);
2183 wake_up(&rdev->blocked_wait);
2184 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2185 md_wakeup_thread(rdev->mddev->thread);
2186
2187 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002188 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2189 set_bit(In_sync, &rdev->flags);
2190 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002191 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002192 if (!err && rdev->sysfs_state)
2193 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002194 return err ? err : len;
2195}
NeilBrown80ca3a42006-07-10 04:44:18 -07002196static struct rdev_sysfs_entry rdev_state =
2197__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002198
2199static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002200errors_show(mdk_rdev_t *rdev, char *page)
2201{
2202 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2203}
2204
2205static ssize_t
2206errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2207{
2208 char *e;
2209 unsigned long n = simple_strtoul(buf, &e, 10);
2210 if (*buf && (*e == 0 || *e == '\n')) {
2211 atomic_set(&rdev->corrected_errors, n);
2212 return len;
2213 }
2214 return -EINVAL;
2215}
2216static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002217__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002218
NeilBrown014236d2006-01-06 00:20:55 -08002219static ssize_t
2220slot_show(mdk_rdev_t *rdev, char *page)
2221{
2222 if (rdev->raid_disk < 0)
2223 return sprintf(page, "none\n");
2224 else
2225 return sprintf(page, "%d\n", rdev->raid_disk);
2226}
2227
2228static ssize_t
2229slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2230{
2231 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002232 int err;
2233 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002234 int slot = simple_strtoul(buf, &e, 10);
2235 if (strncmp(buf, "none", 4)==0)
2236 slot = -1;
2237 else if (e==buf || (*e && *e!= '\n'))
2238 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002239 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002240 /* Setting 'slot' on an active array requires also
2241 * updating the 'rd%d' link, and communicating
2242 * with the personality with ->hot_*_disk.
2243 * For now we only support removing
2244 * failed/spare devices. This normally happens automatically,
2245 * but not when the metadata is externally managed.
2246 */
NeilBrownc303da62008-02-06 01:39:51 -08002247 if (rdev->raid_disk == -1)
2248 return -EEXIST;
2249 /* personality does all needed checks */
2250 if (rdev->mddev->pers->hot_add_disk == NULL)
2251 return -EINVAL;
2252 err = rdev->mddev->pers->
2253 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2254 if (err)
2255 return err;
2256 sprintf(nm, "rd%d", rdev->raid_disk);
2257 sysfs_remove_link(&rdev->mddev->kobj, nm);
2258 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2259 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002260 } else if (rdev->mddev->pers) {
2261 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002262 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002263 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002264 */
2265
2266 if (rdev->raid_disk != -1)
2267 return -EBUSY;
2268
2269 if (rdev->mddev->pers->hot_add_disk == NULL)
2270 return -EINVAL;
2271
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002272 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002273 if (rdev2->raid_disk == slot)
2274 return -EEXIST;
2275
2276 rdev->raid_disk = slot;
2277 if (test_bit(In_sync, &rdev->flags))
2278 rdev->saved_raid_disk = slot;
2279 else
2280 rdev->saved_raid_disk = -1;
2281 err = rdev->mddev->pers->
2282 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002283 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002284 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002285 return err;
Neil Brown52664732008-06-28 08:31:44 +10002286 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002287 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002288 sprintf(nm, "rd%d", rdev->raid_disk);
2289 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2290 printk(KERN_WARNING
2291 "md: cannot register "
2292 "%s for %s\n",
2293 nm, mdname(rdev->mddev));
2294
2295 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002296 } else {
2297 if (slot >= rdev->mddev->raid_disks)
2298 return -ENOSPC;
2299 rdev->raid_disk = slot;
2300 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002301 clear_bit(Faulty, &rdev->flags);
2302 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002303 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002304 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002305 }
NeilBrown014236d2006-01-06 00:20:55 -08002306 return len;
2307}
2308
2309
2310static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002311__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002312
NeilBrown93c8cad2006-01-06 00:20:56 -08002313static ssize_t
2314offset_show(mdk_rdev_t *rdev, char *page)
2315{
Andrew Morton6961ece2006-01-06 00:20:59 -08002316 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002317}
2318
2319static ssize_t
2320offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2321{
2322 char *e;
2323 unsigned long long offset = simple_strtoull(buf, &e, 10);
2324 if (e==buf || (*e && *e != '\n'))
2325 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002326 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002327 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002328 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002329 /* Must set offset before size, so overlap checks
2330 * can be sane */
2331 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002332 rdev->data_offset = offset;
2333 return len;
2334}
2335
2336static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002337__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002338
NeilBrown83303b62006-01-06 00:21:06 -08002339static ssize_t
2340rdev_size_show(mdk_rdev_t *rdev, char *page)
2341{
Andre Nolldd8ac332009-03-31 14:33:13 +11002342 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002343}
2344
NeilBrownc5d79ad2008-02-06 01:39:54 -08002345static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2346{
2347 /* check if two start/length pairs overlap */
2348 if (s1+l1 <= s2)
2349 return 0;
2350 if (s2+l2 <= s1)
2351 return 0;
2352 return 1;
2353}
2354
Dan Williamsb522adc2009-03-31 15:00:31 +11002355static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2356{
2357 unsigned long long blocks;
2358 sector_t new;
2359
2360 if (strict_strtoull(buf, 10, &blocks) < 0)
2361 return -EINVAL;
2362
2363 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2364 return -EINVAL; /* sector conversion overflow */
2365
2366 new = blocks * 2;
2367 if (new != blocks * 2)
2368 return -EINVAL; /* unsigned long long to sector_t overflow */
2369
2370 *sectors = new;
2371 return 0;
2372}
2373
NeilBrown83303b62006-01-06 00:21:06 -08002374static ssize_t
2375rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2376{
NeilBrown27c529b2008-03-04 14:29:33 -08002377 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002378 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002379 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002380
Dan Williamsb522adc2009-03-31 15:00:31 +11002381 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002382 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002383 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002384 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002385 sectors = super_types[my_mddev->major_version].
2386 rdev_size_change(rdev, sectors);
2387 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002388 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002389 } else if (!sectors)
2390 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2391 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002392 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002393 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002394 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002395
Andre Nolldd8ac332009-03-31 14:33:13 +11002396 rdev->sectors = sectors;
2397 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002398 /* need to check that all other rdevs with the same ->bdev
2399 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002400 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002401 * we have to change it back, we will have the lock again.
2402 */
2403 mddev_t *mddev;
2404 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002405 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002406
NeilBrown27c529b2008-03-04 14:29:33 -08002407 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002408 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002409 mdk_rdev_t *rdev2;
2410
2411 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002412 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002413 if (test_bit(AllReserved, &rdev2->flags) ||
2414 (rdev->bdev == rdev2->bdev &&
2415 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002416 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002417 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002418 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002419 overlap = 1;
2420 break;
2421 }
2422 mddev_unlock(mddev);
2423 if (overlap) {
2424 mddev_put(mddev);
2425 break;
2426 }
2427 }
NeilBrown27c529b2008-03-04 14:29:33 -08002428 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002429 if (overlap) {
2430 /* Someone else could have slipped in a size
2431 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002432 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002433 * safe, and trust userspace not to race with
2434 * itself
2435 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002436 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002437 return -EBUSY;
2438 }
2439 }
NeilBrown83303b62006-01-06 00:21:06 -08002440 return len;
2441}
2442
2443static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002444__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002445
NeilBrown86e6ffd2005-11-08 21:39:24 -08002446static struct attribute *rdev_default_attrs[] = {
2447 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002448 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002449 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002450 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002451 &rdev_size.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002452 NULL,
2453};
2454static ssize_t
2455rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2456{
2457 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2458 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002459 mddev_t *mddev = rdev->mddev;
2460 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002461
2462 if (!entry->show)
2463 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002464
2465 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2466 if (!rv) {
2467 if (rdev->mddev == NULL)
2468 rv = -EBUSY;
2469 else
2470 rv = entry->show(rdev, page);
2471 mddev_unlock(mddev);
2472 }
2473 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002474}
2475
2476static ssize_t
2477rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2478 const char *page, size_t length)
2479{
2480 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2481 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002482 ssize_t rv;
2483 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002484
2485 if (!entry->store)
2486 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002487 if (!capable(CAP_SYS_ADMIN))
2488 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002489 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002490 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002491 if (rdev->mddev == NULL)
2492 rv = -EBUSY;
2493 else
2494 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002495 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002496 }
2497 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002498}
2499
2500static void rdev_free(struct kobject *ko)
2501{
2502 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2503 kfree(rdev);
2504}
2505static struct sysfs_ops rdev_sysfs_ops = {
2506 .show = rdev_attr_show,
2507 .store = rdev_attr_store,
2508};
2509static struct kobj_type rdev_ktype = {
2510 .release = rdev_free,
2511 .sysfs_ops = &rdev_sysfs_ops,
2512 .default_attrs = rdev_default_attrs,
2513};
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515/*
2516 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2517 *
2518 * mark the device faulty if:
2519 *
2520 * - the device is nonexistent (zero size)
2521 * - the device has no valid superblock
2522 *
2523 * a faulty rdev _never_ has rdev->sb set.
2524 */
2525static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2526{
2527 char b[BDEVNAME_SIZE];
2528 int err;
2529 mdk_rdev_t *rdev;
2530 sector_t size;
2531
NeilBrown9ffae0c2006-01-06 00:20:32 -08002532 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (!rdev) {
2534 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2535 return ERR_PTR(-ENOMEM);
2536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 if ((err = alloc_disk_sb(rdev)))
2539 goto abort_free;
2540
NeilBrownc5d79ad2008-02-06 01:39:54 -08002541 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (err)
2543 goto abort_free;
2544
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002545 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002546
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002548 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002549 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002550 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002552 rdev->sb_events = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002554 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002555 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2558 if (!size) {
2559 printk(KERN_WARNING
2560 "md: %s has zero or unknown size, marking faulty!\n",
2561 bdevname(rdev->bdev,b));
2562 err = -EINVAL;
2563 goto abort_free;
2564 }
2565
2566 if (super_format >= 0) {
2567 err = super_types[super_format].
2568 load_super(rdev, NULL, super_minor);
2569 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002570 printk(KERN_WARNING
2571 "md: %s does not have a valid v%d.%d "
2572 "superblock, not importing!\n",
2573 bdevname(rdev->bdev,b),
2574 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 goto abort_free;
2576 }
2577 if (err < 0) {
2578 printk(KERN_WARNING
2579 "md: could not read %s's sb, not importing!\n",
2580 bdevname(rdev->bdev,b));
2581 goto abort_free;
2582 }
2583 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002586 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
2588 return rdev;
2589
2590abort_free:
2591 if (rdev->sb_page) {
2592 if (rdev->bdev)
2593 unlock_rdev(rdev);
2594 free_disk_sb(rdev);
2595 }
2596 kfree(rdev);
2597 return ERR_PTR(err);
2598}
2599
2600/*
2601 * Check a full RAID array for plausibility
2602 */
2603
2604
NeilBrowna757e642005-04-16 15:26:42 -07002605static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
2607 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002608 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 char b[BDEVNAME_SIZE];
2610
2611 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002612 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 switch (super_types[mddev->major_version].
2614 load_super(rdev, freshest, mddev->minor_version)) {
2615 case 1:
2616 freshest = rdev;
2617 break;
2618 case 0:
2619 break;
2620 default:
2621 printk( KERN_ERR \
2622 "md: fatal superblock inconsistency in %s"
2623 " -- removing from array\n",
2624 bdevname(rdev->bdev,b));
2625 kick_rdev_from_array(rdev);
2626 }
2627
2628
2629 super_types[mddev->major_version].
2630 validate_super(mddev, freshest);
2631
2632 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002633 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002634 if (rdev->desc_nr >= mddev->max_disks ||
2635 i > mddev->max_disks) {
2636 printk(KERN_WARNING
2637 "md: %s: %s: only %d devices permitted\n",
2638 mdname(mddev), bdevname(rdev->bdev, b),
2639 mddev->max_disks);
2640 kick_rdev_from_array(rdev);
2641 continue;
2642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (rdev != freshest)
2644 if (super_types[mddev->major_version].
2645 validate_super(mddev, rdev)) {
2646 printk(KERN_WARNING "md: kicking non-fresh %s"
2647 " from array!\n",
2648 bdevname(rdev->bdev,b));
2649 kick_rdev_from_array(rdev);
2650 continue;
2651 }
2652 if (mddev->level == LEVEL_MULTIPATH) {
2653 rdev->desc_nr = i++;
2654 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002655 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002656 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002657 rdev->raid_disk = -1;
2658 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
NeilBrown19052c02008-08-05 15:54:13 +10002663static void md_safemode_timeout(unsigned long data);
2664
NeilBrowneae17012005-11-08 21:39:23 -08002665static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002666safe_delay_show(mddev_t *mddev, char *page)
2667{
2668 int msec = (mddev->safemode_delay*1000)/HZ;
2669 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2670}
2671static ssize_t
2672safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2673{
2674 int scale=1;
2675 int dot=0;
2676 int i;
2677 unsigned long msec;
2678 char buf[30];
Dan Williams97ce0a72008-09-24 22:48:19 -07002679
NeilBrown16f17b32006-06-26 00:27:37 -07002680 /* remove a period, and count digits after it */
2681 if (len >= sizeof(buf))
2682 return -EINVAL;
Dan Williams97ce0a72008-09-24 22:48:19 -07002683 strlcpy(buf, cbuf, sizeof(buf));
NeilBrown16f17b32006-06-26 00:27:37 -07002684 for (i=0; i<len; i++) {
2685 if (dot) {
2686 if (isdigit(buf[i])) {
2687 buf[i-1] = buf[i];
2688 scale *= 10;
2689 }
2690 buf[i] = 0;
2691 } else if (buf[i] == '.') {
2692 dot=1;
2693 buf[i] = 0;
2694 }
2695 }
Dan Williams97ce0a72008-09-24 22:48:19 -07002696 if (strict_strtoul(buf, 10, &msec) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002697 return -EINVAL;
2698 msec = (msec * 1000) / scale;
2699 if (msec == 0)
2700 mddev->safemode_delay = 0;
2701 else {
NeilBrown19052c02008-08-05 15:54:13 +10002702 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002703 mddev->safemode_delay = (msec*HZ)/1000;
2704 if (mddev->safemode_delay == 0)
2705 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002706 if (mddev->safemode_delay < old_delay)
2707 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002708 }
2709 return len;
2710}
2711static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002712__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002713
2714static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002715level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002716{
NeilBrown2604b702006-01-06 00:20:36 -08002717 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002718 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002719 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002720 else if (mddev->clevel[0])
2721 return sprintf(page, "%s\n", mddev->clevel);
2722 else if (mddev->level != LEVEL_NONE)
2723 return sprintf(page, "%d\n", mddev->level);
2724 else
2725 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002726}
2727
NeilBrownd9d166c2006-01-06 00:20:51 -08002728static ssize_t
2729level_store(mddev_t *mddev, const char *buf, size_t len)
2730{
NeilBrown245f46c2009-03-31 14:39:39 +11002731 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002732 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002733 struct mdk_personality *pers;
2734 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002735 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002736
2737 if (mddev->pers == NULL) {
2738 if (len == 0)
2739 return 0;
2740 if (len >= sizeof(mddev->clevel))
2741 return -ENOSPC;
2742 strncpy(mddev->clevel, buf, len);
2743 if (mddev->clevel[len-1] == '\n')
2744 len--;
2745 mddev->clevel[len] = 0;
2746 mddev->level = LEVEL_NONE;
2747 return rv;
2748 }
2749
2750 /* request to change the personality. Need to ensure:
2751 * - array is not engaged in resync/recovery/reshape
2752 * - old personality can be suspended
2753 * - new personality will access other array.
2754 */
2755
2756 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002757 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002758
2759 if (!mddev->pers->quiesce) {
2760 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2761 mdname(mddev), mddev->pers->name);
2762 return -EINVAL;
2763 }
2764
2765 /* Now find the new personality */
2766 if (len == 0 || len >= sizeof(level))
2767 return -EINVAL;
2768 strncpy(level, buf, len);
2769 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002770 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002771 level[len] = 0;
2772
2773 request_module("md-%s", level);
2774 spin_lock(&pers_lock);
2775 pers = find_pers(LEVEL_NONE, level);
2776 if (!pers || !try_module_get(pers->owner)) {
2777 spin_unlock(&pers_lock);
2778 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2779 return -EINVAL;
2780 }
2781 spin_unlock(&pers_lock);
2782
2783 if (pers == mddev->pers) {
2784 /* Nothing to do! */
2785 module_put(pers->owner);
2786 return rv;
2787 }
2788 if (!pers->takeover) {
2789 module_put(pers->owner);
2790 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2791 mdname(mddev), level);
2792 return -EINVAL;
2793 }
2794
2795 /* ->takeover must set new_* and/or delta_disks
2796 * if it succeeds, and may set them when it fails.
2797 */
2798 priv = pers->takeover(mddev);
2799 if (IS_ERR(priv)) {
2800 mddev->new_level = mddev->level;
2801 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002802 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002803 mddev->raid_disks -= mddev->delta_disks;
2804 mddev->delta_disks = 0;
2805 module_put(pers->owner);
2806 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2807 mdname(mddev), level);
2808 return PTR_ERR(priv);
2809 }
2810
2811 /* Looks like we have a winner */
2812 mddev_suspend(mddev);
2813 mddev->pers->stop(mddev);
2814 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10002815 /* Invalidate devices that are now superfluous */
2816 list_for_each_entry(rdev, &mddev->disks, same_set)
2817 if (rdev->raid_disk >= mddev->raid_disks) {
2818 rdev->raid_disk = -1;
2819 clear_bit(In_sync, &rdev->flags);
2820 }
NeilBrown245f46c2009-03-31 14:39:39 +11002821 mddev->pers = pers;
2822 mddev->private = priv;
2823 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
2824 mddev->level = mddev->new_level;
2825 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002826 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002827 mddev->delta_disks = 0;
2828 pers->run(mddev);
2829 mddev_resume(mddev);
2830 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2831 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2832 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08002833 return rv;
2834}
2835
2836static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07002837__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08002838
NeilBrownd4dbd022006-06-26 00:27:59 -07002839
2840static ssize_t
2841layout_show(mddev_t *mddev, char *page)
2842{
2843 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07002844 if (mddev->reshape_position != MaxSector &&
2845 mddev->layout != mddev->new_layout)
2846 return sprintf(page, "%d (%d)\n",
2847 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07002848 return sprintf(page, "%d\n", mddev->layout);
2849}
2850
2851static ssize_t
2852layout_store(mddev_t *mddev, const char *buf, size_t len)
2853{
2854 char *e;
2855 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07002856
2857 if (!*buf || (*e && *e != '\n'))
2858 return -EINVAL;
2859
NeilBrownb3546032009-03-31 14:56:41 +11002860 if (mddev->pers) {
2861 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002862 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002863 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002864 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10002865 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002866 if (err) {
2867 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11002868 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002869 }
NeilBrownb3546032009-03-31 14:56:41 +11002870 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07002871 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11002872 if (mddev->reshape_position == MaxSector)
2873 mddev->layout = n;
2874 }
NeilBrownd4dbd022006-06-26 00:27:59 -07002875 return len;
2876}
2877static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07002878__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07002879
2880
NeilBrowneae17012005-11-08 21:39:23 -08002881static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002882raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002883{
NeilBrownbb636542005-11-08 21:39:45 -08002884 if (mddev->raid_disks == 0)
2885 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07002886 if (mddev->reshape_position != MaxSector &&
2887 mddev->delta_disks != 0)
2888 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
2889 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08002890 return sprintf(page, "%d\n", mddev->raid_disks);
2891}
2892
NeilBrownda943b992006-01-06 00:20:54 -08002893static int update_raid_disks(mddev_t *mddev, int raid_disks);
2894
2895static ssize_t
2896raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
2897{
NeilBrownda943b992006-01-06 00:20:54 -08002898 char *e;
2899 int rv = 0;
2900 unsigned long n = simple_strtoul(buf, &e, 10);
2901
2902 if (!*buf || (*e && *e != '\n'))
2903 return -EINVAL;
2904
2905 if (mddev->pers)
2906 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07002907 else if (mddev->reshape_position != MaxSector) {
2908 int olddisks = mddev->raid_disks - mddev->delta_disks;
2909 mddev->delta_disks = n - olddisks;
2910 mddev->raid_disks = n;
2911 } else
NeilBrownda943b992006-01-06 00:20:54 -08002912 mddev->raid_disks = n;
2913 return rv ? rv : len;
2914}
2915static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07002916__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08002917
NeilBrown24dd4692005-11-08 21:39:26 -08002918static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08002919chunk_size_show(mddev_t *mddev, char *page)
2920{
NeilBrown08a02ec2007-05-09 02:35:38 -07002921 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10002922 mddev->chunk_sectors != mddev->new_chunk_sectors)
2923 return sprintf(page, "%d (%d)\n",
2924 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10002925 mddev->chunk_sectors << 9);
2926 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08002927}
2928
2929static ssize_t
2930chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
2931{
NeilBrown3b343802006-01-06 00:20:47 -08002932 char *e;
2933 unsigned long n = simple_strtoul(buf, &e, 10);
2934
NeilBrown3b343802006-01-06 00:20:47 -08002935 if (!*buf || (*e && *e != '\n'))
2936 return -EINVAL;
2937
NeilBrownb3546032009-03-31 14:56:41 +11002938 if (mddev->pers) {
2939 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10002940 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11002941 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10002942 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10002943 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10002944 if (err) {
2945 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11002946 return err;
NeilBrown597a7112009-06-18 08:47:42 +10002947 }
NeilBrownb3546032009-03-31 14:56:41 +11002948 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10002949 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002950 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10002951 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11002952 }
NeilBrown3b343802006-01-06 00:20:47 -08002953 return len;
2954}
2955static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002956__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08002957
NeilBrowna94213b2006-06-26 00:28:00 -07002958static ssize_t
2959resync_start_show(mddev_t *mddev, char *page)
2960{
NeilBrownd1a7c502009-03-31 15:24:32 +11002961 if (mddev->recovery_cp == MaxSector)
2962 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07002963 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
2964}
2965
2966static ssize_t
2967resync_start_store(mddev_t *mddev, const char *buf, size_t len)
2968{
NeilBrowna94213b2006-06-26 00:28:00 -07002969 char *e;
2970 unsigned long long n = simple_strtoull(buf, &e, 10);
2971
2972 if (mddev->pers)
2973 return -EBUSY;
2974 if (!*buf || (*e && *e != '\n'))
2975 return -EINVAL;
2976
2977 mddev->recovery_cp = n;
2978 return len;
2979}
2980static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07002981__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07002982
NeilBrown9e653b62006-06-26 00:27:58 -07002983/*
2984 * The array state can be:
2985 *
2986 * clear
2987 * No devices, no size, no level
2988 * Equivalent to STOP_ARRAY ioctl
2989 * inactive
2990 * May have some settings, but array is not active
2991 * all IO results in error
2992 * When written, doesn't tear down array, but just stops it
2993 * suspended (not supported yet)
2994 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01002995 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07002996 * readonly
2997 * no resync can happen. no superblocks get written.
2998 * write requests fail
2999 * read-auto
3000 * like readonly, but behaves like 'clean' on a write request.
3001 *
3002 * clean - no pending writes, but otherwise active.
3003 * When written to inactive array, starts without resync
3004 * If a write request arrives then
3005 * if metadata is known, mark 'dirty' and switch to 'active'.
3006 * if not known, block and switch to write-pending
3007 * If written to an active array that has pending writes, then fails.
3008 * active
3009 * fully active: IO and resync can be happening.
3010 * When written to inactive array, starts with resync
3011 *
3012 * write-pending
3013 * clean, but writes are blocked waiting for 'active' to be written.
3014 *
3015 * active-idle
3016 * like active, but no writes have been seen for a while (100msec).
3017 *
3018 */
3019enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3020 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003021static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003022 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3023 "write-pending", "active-idle", NULL };
3024
3025static int match_word(const char *word, char **list)
3026{
3027 int n;
3028 for (n=0; list[n]; n++)
3029 if (cmd_match(word, list[n]))
3030 break;
3031 return n;
3032}
3033
3034static ssize_t
3035array_state_show(mddev_t *mddev, char *page)
3036{
3037 enum array_state st = inactive;
3038
3039 if (mddev->pers)
3040 switch(mddev->ro) {
3041 case 1:
3042 st = readonly;
3043 break;
3044 case 2:
3045 st = read_auto;
3046 break;
3047 case 0:
3048 if (mddev->in_sync)
3049 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003050 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3051 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003052 else if (mddev->safemode)
3053 st = active_idle;
3054 else
3055 st = active;
3056 }
3057 else {
3058 if (list_empty(&mddev->disks) &&
3059 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003060 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003061 st = clear;
3062 else
3063 st = inactive;
3064 }
3065 return sprintf(page, "%s\n", array_states[st]);
3066}
3067
Neil Browndf5b20c2008-07-11 22:02:22 +10003068static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003069static int do_md_run(mddev_t * mddev);
3070static int restart_array(mddev_t *mddev);
3071
3072static ssize_t
3073array_state_store(mddev_t *mddev, const char *buf, size_t len)
3074{
3075 int err = -EINVAL;
3076 enum array_state st = match_word(buf, array_states);
3077 switch(st) {
3078 case bad_word:
3079 break;
3080 case clear:
3081 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003082 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003083 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003084 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003085 break;
3086 case inactive:
3087 /* stopping an active array */
3088 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003089 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003090 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003091 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003092 } else
3093 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003094 break;
3095 case suspended:
3096 break; /* not supported yet */
3097 case readonly:
3098 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003099 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003100 else {
3101 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003102 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003103 err = do_md_run(mddev);
3104 }
3105 break;
3106 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003107 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003108 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003109 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003110 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003111 err = restart_array(mddev);
3112 if (err == 0) {
3113 mddev->ro = 2;
3114 set_disk_ro(mddev->gendisk, 0);
3115 }
NeilBrown9e653b62006-06-26 00:27:58 -07003116 } else {
3117 mddev->ro = 2;
3118 err = do_md_run(mddev);
3119 }
3120 break;
3121 case clean:
3122 if (mddev->pers) {
3123 restart_array(mddev);
3124 spin_lock_irq(&mddev->write_lock);
3125 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003126 if (mddev->in_sync == 0) {
3127 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003128 if (mddev->safemode == 1)
3129 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003130 if (mddev->persistent)
3131 set_bit(MD_CHANGE_CLEAN,
3132 &mddev->flags);
3133 }
3134 err = 0;
3135 } else
3136 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003137 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003138 } else
3139 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003140 break;
3141 case active:
3142 if (mddev->pers) {
3143 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003144 if (mddev->external)
3145 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003146 wake_up(&mddev->sb_wait);
3147 err = 0;
3148 } else {
3149 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003150 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003151 err = do_md_run(mddev);
3152 }
3153 break;
3154 case write_pending:
3155 case active_idle:
3156 /* these cannot be set */
3157 break;
3158 }
3159 if (err)
3160 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003161 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003162 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003163 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003164 }
NeilBrown9e653b62006-06-26 00:27:58 -07003165}
NeilBrown80ca3a42006-07-10 04:44:18 -07003166static struct md_sysfs_entry md_array_state =
3167__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003168
NeilBrown6d7ff7382006-01-06 00:21:16 -08003169static ssize_t
3170null_show(mddev_t *mddev, char *page)
3171{
3172 return -EINVAL;
3173}
3174
3175static ssize_t
3176new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3177{
3178 /* buf must be %d:%d\n? giving major and minor numbers */
3179 /* The new device is added to the array.
3180 * If the array has a persistent superblock, we read the
3181 * superblock to initialise info and check validity.
3182 * Otherwise, only checking done is that in bind_rdev_to_array,
3183 * which mainly checks size.
3184 */
3185 char *e;
3186 int major = simple_strtoul(buf, &e, 10);
3187 int minor;
3188 dev_t dev;
3189 mdk_rdev_t *rdev;
3190 int err;
3191
3192 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3193 return -EINVAL;
3194 minor = simple_strtoul(e+1, &e, 10);
3195 if (*e && *e != '\n')
3196 return -EINVAL;
3197 dev = MKDEV(major, minor);
3198 if (major != MAJOR(dev) ||
3199 minor != MINOR(dev))
3200 return -EOVERFLOW;
3201
3202
3203 if (mddev->persistent) {
3204 rdev = md_import_device(dev, mddev->major_version,
3205 mddev->minor_version);
3206 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3207 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3208 mdk_rdev_t, same_set);
3209 err = super_types[mddev->major_version]
3210 .load_super(rdev, rdev0, mddev->minor_version);
3211 if (err < 0)
3212 goto out;
3213 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003214 } else if (mddev->external)
3215 rdev = md_import_device(dev, -2, -1);
3216 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003217 rdev = md_import_device(dev, -1, -1);
3218
3219 if (IS_ERR(rdev))
3220 return PTR_ERR(rdev);
3221 err = bind_rdev_to_array(rdev, mddev);
3222 out:
3223 if (err)
3224 export_rdev(rdev);
3225 return err ? err : len;
3226}
3227
3228static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003229__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003230
3231static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003232bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3233{
3234 char *end;
3235 unsigned long chunk, end_chunk;
3236
3237 if (!mddev->bitmap)
3238 goto out;
3239 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3240 while (*buf) {
3241 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3242 if (buf == end) break;
3243 if (*end == '-') { /* range */
3244 buf = end + 1;
3245 end_chunk = simple_strtoul(buf, &end, 0);
3246 if (buf == end) break;
3247 }
3248 if (*end && !isspace(*end)) break;
3249 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
3250 buf = end;
3251 while (isspace(*buf)) buf++;
3252 }
3253 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3254out:
3255 return len;
3256}
3257
3258static struct md_sysfs_entry md_bitmap =
3259__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3260
3261static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003262size_show(mddev_t *mddev, char *page)
3263{
Andre Noll58c0fed2009-03-31 14:33:13 +11003264 return sprintf(page, "%llu\n",
3265 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003266}
3267
Andre Nolld71f9f82008-07-11 22:02:22 +10003268static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003269
3270static ssize_t
3271size_store(mddev_t *mddev, const char *buf, size_t len)
3272{
3273 /* If array is inactive, we can reduce the component size, but
3274 * not increase it (except from 0).
3275 * If array is active, we can try an on-line resize
3276 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003277 sector_t sectors;
3278 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003279
Andre Noll58c0fed2009-03-31 14:33:13 +11003280 if (err < 0)
3281 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003282 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003283 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003284 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003285 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003286 if (mddev->dev_sectors == 0 ||
3287 mddev->dev_sectors > sectors)
3288 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003289 else
3290 err = -ENOSPC;
3291 }
3292 return err ? err : len;
3293}
3294
3295static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003296__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003297
NeilBrown8bb93aa2006-01-06 00:20:50 -08003298
3299/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003300 * This is one of
3301 * 'none' for arrays with no metadata (good luck...)
3302 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003303 * or N.M for internally known formats
3304 */
3305static ssize_t
3306metadata_show(mddev_t *mddev, char *page)
3307{
3308 if (mddev->persistent)
3309 return sprintf(page, "%d.%d\n",
3310 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003311 else if (mddev->external)
3312 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003313 else
3314 return sprintf(page, "none\n");
3315}
3316
3317static ssize_t
3318metadata_store(mddev_t *mddev, const char *buf, size_t len)
3319{
3320 int major, minor;
3321 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003322 /* Changing the details of 'external' metadata is
3323 * always permitted. Otherwise there must be
3324 * no devices attached to the array.
3325 */
3326 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3327 ;
3328 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003329 return -EBUSY;
3330
3331 if (cmd_match(buf, "none")) {
3332 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003333 mddev->external = 0;
3334 mddev->major_version = 0;
3335 mddev->minor_version = 90;
3336 return len;
3337 }
3338 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003339 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003340 if (namelen >= sizeof(mddev->metadata_type))
3341 namelen = sizeof(mddev->metadata_type)-1;
3342 strncpy(mddev->metadata_type, buf+9, namelen);
3343 mddev->metadata_type[namelen] = 0;
3344 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3345 mddev->metadata_type[--namelen] = 0;
3346 mddev->persistent = 0;
3347 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003348 mddev->major_version = 0;
3349 mddev->minor_version = 90;
3350 return len;
3351 }
3352 major = simple_strtoul(buf, &e, 10);
3353 if (e==buf || *e != '.')
3354 return -EINVAL;
3355 buf = e+1;
3356 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003357 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003358 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003359 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003360 return -ENOENT;
3361 mddev->major_version = major;
3362 mddev->minor_version = minor;
3363 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003364 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003365 return len;
3366}
3367
3368static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003369__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003370
NeilBrowna35b0d62006-01-06 00:20:49 -08003371static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003372action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003373{
NeilBrown7eec3142005-11-08 21:39:44 -08003374 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003375 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3376 type = "frozen";
3377 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003378 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003379 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3380 type = "reshape";
3381 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003382 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3383 type = "resync";
3384 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3385 type = "check";
3386 else
3387 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003388 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003389 type = "recover";
3390 }
3391 return sprintf(page, "%s\n", type);
3392}
3393
3394static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003395action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003396{
NeilBrown7eec3142005-11-08 21:39:44 -08003397 if (!mddev->pers || !mddev->pers->sync_request)
3398 return -EINVAL;
3399
NeilBrownb6a9ce62009-05-26 09:41:17 +10003400 if (cmd_match(page, "frozen"))
3401 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3402 else
3403 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3404
3405 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003406 if (mddev->sync_thread) {
3407 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3408 md_unregister_thread(mddev->sync_thread);
3409 mddev->sync_thread = NULL;
3410 mddev->recovery = 0;
3411 }
NeilBrown03c902e2006-01-06 00:20:46 -08003412 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3413 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003414 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003415 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003416 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003417 else if (cmd_match(page, "recover")) {
3418 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3419 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3420 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003421 int err;
3422 if (mddev->pers->start_reshape == NULL)
3423 return -EINVAL;
3424 err = mddev->pers->start_reshape(mddev);
3425 if (err)
3426 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003427 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003428 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003429 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003430 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003431 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003432 return -EINVAL;
3433 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3434 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003435 }
NeilBrown03c902e2006-01-06 00:20:46 -08003436 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003437 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003438 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003439 return len;
3440}
3441
NeilBrown9d888832005-11-08 21:39:26 -08003442static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003443mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003444{
3445 return sprintf(page, "%llu\n",
3446 (unsigned long long) mddev->resync_mismatches);
3447}
3448
NeilBrown80ca3a42006-07-10 04:44:18 -07003449static struct md_sysfs_entry md_scan_mode =
3450__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003451
NeilBrown96de1e62005-11-08 21:39:39 -08003452
NeilBrown80ca3a42006-07-10 04:44:18 -07003453static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003454
NeilBrown88202a02006-01-06 00:21:36 -08003455static ssize_t
3456sync_min_show(mddev_t *mddev, char *page)
3457{
3458 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3459 mddev->sync_speed_min ? "local": "system");
3460}
3461
3462static ssize_t
3463sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3464{
3465 int min;
3466 char *e;
3467 if (strncmp(buf, "system", 6)==0) {
3468 mddev->sync_speed_min = 0;
3469 return len;
3470 }
3471 min = simple_strtoul(buf, &e, 10);
3472 if (buf == e || (*e && *e != '\n') || min <= 0)
3473 return -EINVAL;
3474 mddev->sync_speed_min = min;
3475 return len;
3476}
3477
3478static struct md_sysfs_entry md_sync_min =
3479__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3480
3481static ssize_t
3482sync_max_show(mddev_t *mddev, char *page)
3483{
3484 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3485 mddev->sync_speed_max ? "local": "system");
3486}
3487
3488static ssize_t
3489sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3490{
3491 int max;
3492 char *e;
3493 if (strncmp(buf, "system", 6)==0) {
3494 mddev->sync_speed_max = 0;
3495 return len;
3496 }
3497 max = simple_strtoul(buf, &e, 10);
3498 if (buf == e || (*e && *e != '\n') || max <= 0)
3499 return -EINVAL;
3500 mddev->sync_speed_max = max;
3501 return len;
3502}
3503
3504static struct md_sysfs_entry md_sync_max =
3505__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3506
Iustin Popd7f3d292007-10-16 23:30:54 -07003507static ssize_t
3508degraded_show(mddev_t *mddev, char *page)
3509{
3510 return sprintf(page, "%d\n", mddev->degraded);
3511}
3512static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003513
3514static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003515sync_force_parallel_show(mddev_t *mddev, char *page)
3516{
3517 return sprintf(page, "%d\n", mddev->parallel_resync);
3518}
3519
3520static ssize_t
3521sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3522{
3523 long n;
3524
3525 if (strict_strtol(buf, 10, &n))
3526 return -EINVAL;
3527
3528 if (n != 0 && n != 1)
3529 return -EINVAL;
3530
3531 mddev->parallel_resync = n;
3532
3533 if (mddev->sync_thread)
3534 wake_up(&resync_wait);
3535
3536 return len;
3537}
3538
3539/* force parallel resync, even with shared block devices */
3540static struct md_sysfs_entry md_sync_force_parallel =
3541__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3542 sync_force_parallel_show, sync_force_parallel_store);
3543
3544static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003545sync_speed_show(mddev_t *mddev, char *page)
3546{
3547 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003548 if (mddev->curr_resync == 0)
3549 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003550 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3551 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003552 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003553 db = resync - mddev->resync_mark_cnt;
3554 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003555}
3556
NeilBrown80ca3a42006-07-10 04:44:18 -07003557static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003558
3559static ssize_t
3560sync_completed_show(mddev_t *mddev, char *page)
3561{
Andre Noll58c0fed2009-03-31 14:33:13 +11003562 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003563
NeilBrownacb180b2009-04-14 16:28:34 +10003564 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3565 return sprintf(page, "none\n");
3566
NeilBrown88202a02006-01-06 00:21:36 -08003567 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003568 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003569 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003570 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003571
NeilBrownacb180b2009-04-14 16:28:34 +10003572 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003573 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003574}
3575
NeilBrown80ca3a42006-07-10 04:44:18 -07003576static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003577
NeilBrowne464eaf2006-03-27 01:18:14 -08003578static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003579min_sync_show(mddev_t *mddev, char *page)
3580{
3581 return sprintf(page, "%llu\n",
3582 (unsigned long long)mddev->resync_min);
3583}
3584static ssize_t
3585min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3586{
3587 unsigned long long min;
3588 if (strict_strtoull(buf, 10, &min))
3589 return -EINVAL;
3590 if (min > mddev->resync_max)
3591 return -EINVAL;
3592 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3593 return -EBUSY;
3594
3595 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003596 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003597 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003598 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003599 return -EINVAL;
3600 }
3601 mddev->resync_min = min;
3602
3603 return len;
3604}
3605
3606static struct md_sysfs_entry md_min_sync =
3607__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3608
3609static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003610max_sync_show(mddev_t *mddev, char *page)
3611{
3612 if (mddev->resync_max == MaxSector)
3613 return sprintf(page, "max\n");
3614 else
3615 return sprintf(page, "%llu\n",
3616 (unsigned long long)mddev->resync_max);
3617}
3618static ssize_t
3619max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3620{
3621 if (strncmp(buf, "max", 3) == 0)
3622 mddev->resync_max = MaxSector;
3623 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003624 unsigned long long max;
3625 if (strict_strtoull(buf, 10, &max))
3626 return -EINVAL;
3627 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003628 return -EINVAL;
3629 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003630 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003631 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3632 return -EBUSY;
3633
3634 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003635 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003636 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003637 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003638 return -EINVAL;
3639 }
3640 mddev->resync_max = max;
3641 }
3642 wake_up(&mddev->recovery_wait);
3643 return len;
3644}
3645
3646static struct md_sysfs_entry md_max_sync =
3647__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3648
3649static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003650suspend_lo_show(mddev_t *mddev, char *page)
3651{
3652 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3653}
3654
3655static ssize_t
3656suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3657{
3658 char *e;
3659 unsigned long long new = simple_strtoull(buf, &e, 10);
3660
NeilBrownb8d966e2009-07-01 11:14:04 +10003661 if (mddev->pers == NULL ||
3662 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003663 return -EINVAL;
3664 if (buf == e || (*e && *e != '\n'))
3665 return -EINVAL;
3666 if (new >= mddev->suspend_hi ||
3667 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3668 mddev->suspend_lo = new;
3669 mddev->pers->quiesce(mddev, 2);
3670 return len;
3671 } else
3672 return -EINVAL;
3673}
3674static struct md_sysfs_entry md_suspend_lo =
3675__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3676
3677
3678static ssize_t
3679suspend_hi_show(mddev_t *mddev, char *page)
3680{
3681 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3682}
3683
3684static ssize_t
3685suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3686{
3687 char *e;
3688 unsigned long long new = simple_strtoull(buf, &e, 10);
3689
NeilBrownb8d966e2009-07-01 11:14:04 +10003690 if (mddev->pers == NULL ||
3691 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003692 return -EINVAL;
3693 if (buf == e || (*e && *e != '\n'))
3694 return -EINVAL;
3695 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3696 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3697 mddev->suspend_hi = new;
3698 mddev->pers->quiesce(mddev, 1);
3699 mddev->pers->quiesce(mddev, 0);
3700 return len;
3701 } else
3702 return -EINVAL;
3703}
3704static struct md_sysfs_entry md_suspend_hi =
3705__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3706
NeilBrown08a02ec2007-05-09 02:35:38 -07003707static ssize_t
3708reshape_position_show(mddev_t *mddev, char *page)
3709{
3710 if (mddev->reshape_position != MaxSector)
3711 return sprintf(page, "%llu\n",
3712 (unsigned long long)mddev->reshape_position);
3713 strcpy(page, "none\n");
3714 return 5;
3715}
3716
3717static ssize_t
3718reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3719{
3720 char *e;
3721 unsigned long long new = simple_strtoull(buf, &e, 10);
3722 if (mddev->pers)
3723 return -EBUSY;
3724 if (buf == e || (*e && *e != '\n'))
3725 return -EINVAL;
3726 mddev->reshape_position = new;
3727 mddev->delta_disks = 0;
3728 mddev->new_level = mddev->level;
3729 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003730 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003731 return len;
3732}
3733
3734static struct md_sysfs_entry md_reshape_position =
3735__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3736 reshape_position_store);
3737
Dan Williamsb522adc2009-03-31 15:00:31 +11003738static ssize_t
3739array_size_show(mddev_t *mddev, char *page)
3740{
3741 if (mddev->external_size)
3742 return sprintf(page, "%llu\n",
3743 (unsigned long long)mddev->array_sectors/2);
3744 else
3745 return sprintf(page, "default\n");
3746}
3747
3748static ssize_t
3749array_size_store(mddev_t *mddev, const char *buf, size_t len)
3750{
3751 sector_t sectors;
3752
3753 if (strncmp(buf, "default", 7) == 0) {
3754 if (mddev->pers)
3755 sectors = mddev->pers->size(mddev, 0, 0);
3756 else
3757 sectors = mddev->array_sectors;
3758
3759 mddev->external_size = 0;
3760 } else {
3761 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3762 return -EINVAL;
3763 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003764 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003765
3766 mddev->external_size = 1;
3767 }
3768
3769 mddev->array_sectors = sectors;
3770 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10003771 if (mddev->pers)
3772 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11003773
3774 return len;
3775}
3776
3777static struct md_sysfs_entry md_array_size =
3778__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
3779 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08003780
NeilBrowneae17012005-11-08 21:39:23 -08003781static struct attribute *md_default_attrs[] = {
3782 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07003783 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003784 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08003785 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08003786 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07003787 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003788 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08003789 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07003790 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07003791 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07003792 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11003793 &md_array_size.attr,
NeilBrown411036f2005-11-08 21:39:40 -08003794 NULL,
3795};
3796
3797static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08003798 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08003799 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003800 &md_sync_min.attr,
3801 &md_sync_max.attr,
3802 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07003803 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08003804 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10003805 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08003806 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08003807 &md_suspend_lo.attr,
3808 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07003809 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07003810 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08003811 NULL,
3812};
NeilBrown411036f2005-11-08 21:39:40 -08003813static struct attribute_group md_redundancy_group = {
3814 .name = NULL,
3815 .attrs = md_redundancy_attrs,
3816};
3817
NeilBrowneae17012005-11-08 21:39:23 -08003818
3819static ssize_t
3820md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
3821{
3822 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3823 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003824 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003825
3826 if (!entry->show)
3827 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003828 rv = mddev_lock(mddev);
3829 if (!rv) {
3830 rv = entry->show(mddev, page);
3831 mddev_unlock(mddev);
3832 }
NeilBrown96de1e62005-11-08 21:39:39 -08003833 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003834}
3835
3836static ssize_t
3837md_attr_store(struct kobject *kobj, struct attribute *attr,
3838 const char *page, size_t length)
3839{
3840 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
3841 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08003842 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08003843
3844 if (!entry->store)
3845 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07003846 if (!capable(CAP_SYS_ADMIN))
3847 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003848 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11003849 if (mddev->hold_active == UNTIL_IOCTL)
3850 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07003851 if (!rv) {
3852 rv = entry->store(mddev, page, length);
3853 mddev_unlock(mddev);
3854 }
NeilBrown96de1e62005-11-08 21:39:39 -08003855 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08003856}
3857
3858static void md_free(struct kobject *ko)
3859{
3860 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11003861
3862 if (mddev->sysfs_state)
3863 sysfs_put(mddev->sysfs_state);
3864
3865 if (mddev->gendisk) {
3866 del_gendisk(mddev->gendisk);
3867 put_disk(mddev->gendisk);
3868 }
3869 if (mddev->queue)
3870 blk_cleanup_queue(mddev->queue);
3871
NeilBrowneae17012005-11-08 21:39:23 -08003872 kfree(mddev);
3873}
3874
3875static struct sysfs_ops md_sysfs_ops = {
3876 .show = md_attr_show,
3877 .store = md_attr_store,
3878};
3879static struct kobj_type md_ktype = {
3880 .release = md_free,
3881 .sysfs_ops = &md_sysfs_ops,
3882 .default_attrs = md_default_attrs,
3883};
3884
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885int mdp_major = 0;
3886
Dan Williams5fd3a172009-03-04 00:57:25 -07003887static void mddev_delayed_delete(struct work_struct *ws)
3888{
3889 mddev_t *mddev = container_of(ws, mddev_t, del_work);
3890
3891 if (mddev->private == &md_redundancy_group) {
3892 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
3893 if (mddev->sysfs_action)
3894 sysfs_put(mddev->sysfs_action);
3895 mddev->sysfs_action = NULL;
3896 mddev->private = NULL;
3897 }
3898 kobject_del(&mddev->kobj);
3899 kobject_put(&mddev->kobj);
3900}
3901
NeilBrownefeb53c2009-01-09 08:31:10 +11003902static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003904 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 mddev_t *mddev = mddev_find(dev);
3906 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11003907 int partitioned;
3908 int shift;
3909 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003910 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
3912 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11003913 return -ENODEV;
3914
3915 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
3916 shift = partitioned ? MdpMinorShift : 0;
3917 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
NeilBrownd3374822009-01-09 08:31:10 +11003919 /* wait for any previous instance if this device
3920 * to be completed removed (mddev_delayed_delete).
3921 */
3922 flush_scheduled_work();
3923
Arjan van de Ven48c9c272006-03-27 01:18:20 -08003924 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10003925 error = -EEXIST;
3926 if (mddev->gendisk)
3927 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003928
3929 if (name) {
3930 /* Need to ensure that 'name' is not a duplicate.
3931 */
3932 mddev_t *mddev2;
3933 spin_lock(&all_mddevs_lock);
3934
3935 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
3936 if (mddev2->gendisk &&
3937 strcmp(mddev2->gendisk->disk_name, name) == 0) {
3938 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10003939 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11003940 }
3941 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 }
NeilBrown8b765392009-01-09 08:31:08 +11003943
NeilBrown0909dc42009-07-01 12:27:21 +10003944 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11003945 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10003946 if (!mddev->queue)
3947 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11003948 mddev->queue->queuedata = mddev;
3949
NeilBrown8b765392009-01-09 08:31:08 +11003950 /* Can be unlocked because the queue is new: no concurrency */
3951 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
3952
NeilBrown409c57f2009-03-31 14:39:39 +11003953 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11003954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 disk = alloc_disk(1 << shift);
3956 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11003957 blk_cleanup_queue(mddev->queue);
3958 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10003959 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 }
NeilBrownefeb53c2009-01-09 08:31:10 +11003961 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11003963 if (name)
3964 strcpy(disk->disk_name, name);
3965 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07003967 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 disk->fops = &md_fops;
3970 disk->private_data = mddev;
3971 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11003972 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11003973 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11003974 * remove it now.
3975 */
3976 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 add_disk(disk);
3978 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09003979 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
3980 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10003981 if (error) {
3982 /* This isn't possible, but as kobject_init_and_add is marked
3983 * __must_check, we must do something with the result
3984 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08003985 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
3986 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10003987 error = 0;
3988 }
3989 abort:
3990 mutex_unlock(&disks_mutex);
3991 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04003992 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11003993 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
3994 }
NeilBrownd3374822009-01-09 08:31:10 +11003995 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10003996 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11003997}
3998
3999static struct kobject *md_probe(dev_t dev, int *part, void *data)
4000{
4001 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 return NULL;
4003}
4004
NeilBrownefeb53c2009-01-09 08:31:10 +11004005static int add_named_array(const char *val, struct kernel_param *kp)
4006{
4007 /* val must be "md_*" where * is not all digits.
4008 * We allocate an array with a large free minor number, and
4009 * set the name to val. val must not already be an active name.
4010 */
4011 int len = strlen(val);
4012 char buf[DISK_NAME_LEN];
4013
4014 while (len && val[len-1] == '\n')
4015 len--;
4016 if (len >= DISK_NAME_LEN)
4017 return -E2BIG;
4018 strlcpy(buf, val, len+1);
4019 if (strncmp(buf, "md_", 3) != 0)
4020 return -EINVAL;
4021 return md_alloc(0, buf);
4022}
4023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024static void md_safemode_timeout(unsigned long data)
4025{
4026 mddev_t *mddev = (mddev_t *) data;
4027
Neil Brown0fd62b82008-06-28 08:31:36 +10004028 if (!atomic_read(&mddev->writes_pending)) {
4029 mddev->safemode = 1;
4030 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004031 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 md_wakeup_thread(mddev->thread);
4034}
4035
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004036static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
4038static int do_md_run(mddev_t * mddev)
4039{
NeilBrown2604b702006-01-06 00:20:36 -08004040 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 mdk_rdev_t *rdev;
4042 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004043 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
NeilBrowna757e642005-04-16 15:26:42 -07004045 if (list_empty(&mddev->disks))
4046 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
4049 if (mddev->pers)
4050 return -EBUSY;
4051
4052 /*
4053 * Analyze all RAID superblock(s)
4054 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004055 if (!mddev->raid_disks) {
4056 if (!mddev->persistent)
4057 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004058 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
NeilBrownd9d166c2006-01-06 00:20:51 -08004061 if (mddev->level != LEVEL_NONE)
4062 request_module("md-level-%d", mddev->level);
4063 else if (mddev->clevel[0])
4064 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
4066 /*
4067 * Drop all container device buffers, from now on
4068 * the only valid external interface is through the md
4069 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004071 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004072 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 continue;
4074 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004075 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004076
4077 /* perform some consistency tests on the device.
4078 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004079 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004080 */
Andre Noll0f420352008-07-11 22:02:23 +10004081 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004082 if (mddev->dev_sectors &&
4083 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004084 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004085 printk("md: %s: data overlaps metadata\n",
4086 mdname(mddev));
4087 return -EINVAL;
4088 }
4089 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004090 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004091 > rdev->data_offset) {
4092 printk("md: %s: metadata overlaps data\n",
4093 mdname(mddev));
4094 return -EINVAL;
4095 }
4096 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004097 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 }
4099
4100 md_probe(mddev->unit, NULL, NULL);
4101 disk = mddev->gendisk;
4102 if (!disk)
4103 return -ENOMEM;
4104
4105 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004106 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004107 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004109 if (mddev->level != LEVEL_NONE)
4110 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4111 mddev->level);
4112 else
4113 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4114 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 return -EINVAL;
4116 }
NeilBrown2604b702006-01-06 00:20:36 -08004117 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004119 if (mddev->level != pers->level) {
4120 mddev->level = pers->level;
4121 mddev->new_level = pers->level;
4122 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004123 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
NeilBrownf6705572006-03-27 01:18:11 -08004125 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004126 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004127 /* This personality cannot handle reshaping... */
4128 mddev->pers = NULL;
4129 module_put(pers->owner);
4130 return -EINVAL;
4131 }
4132
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004133 if (pers->sync_request) {
4134 /* Warn if this is a potentially silly
4135 * configuration.
4136 */
4137 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4138 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004139 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004140
4141 list_for_each_entry(rdev, &mddev->disks, same_set)
4142 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004143 if (rdev < rdev2 &&
4144 rdev->bdev->bd_contains ==
4145 rdev2->bdev->bd_contains) {
4146 printk(KERN_WARNING
4147 "%s: WARNING: %s appears to be"
4148 " on the same physical disk as"
4149 " %s.\n",
4150 mdname(mddev),
4151 bdevname(rdev->bdev,b),
4152 bdevname(rdev2->bdev,b2));
4153 warned = 1;
4154 }
4155 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004156
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004157 if (warned)
4158 printk(KERN_WARNING
4159 "True protection against single-disk"
4160 " failure might be compromised.\n");
4161 }
4162
NeilBrown657390d2005-08-26 18:34:16 -07004163 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004164 /* may be over-ridden by personality */
4165 mddev->resync_max_sectors = mddev->dev_sectors;
4166
NeilBrowna9701a32005-11-08 21:39:34 -08004167 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004168 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
NeilBrownf91de922005-11-08 21:39:36 -08004170 if (start_readonly)
4171 mddev->ro = 2; /* read-only, but switch on first write */
4172
NeilBrownb15c2e52006-01-06 00:20:16 -08004173 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004174 if (err)
4175 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004176 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4177 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4178 " but 'external_size' not in effect?\n", __func__);
4179 printk(KERN_ERR
4180 "md: invalid array_size %llu > default size %llu\n",
4181 (unsigned long long)mddev->array_sectors / 2,
4182 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4183 err = -EINVAL;
4184 mddev->pers->stop(mddev);
4185 }
4186 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004187 err = bitmap_create(mddev);
4188 if (err) {
4189 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4190 mdname(mddev), err);
4191 mddev->pers->stop(mddev);
4192 }
4193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 module_put(mddev->pers->owner);
4196 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004197 bitmap_destroy(mddev);
4198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004200 if (mddev->pers->sync_request) {
4201 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4202 printk(KERN_WARNING
4203 "md: cannot register extra attributes for %s\n",
4204 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004205 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004206 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004207 mddev->ro = 0;
4208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 atomic_set(&mddev->writes_pending,0);
4210 mddev->safemode = 0;
4211 mddev->safemode_timer.function = md_safemode_timeout;
4212 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004213 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004215
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004216 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004217 if (rdev->raid_disk >= 0) {
4218 char nm[20];
4219 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004220 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4221 printk("md: cannot register %s for %s\n",
4222 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
4225 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4226
NeilBrown850b2b42006-10-03 01:15:46 -07004227 if (mddev->flags)
4228 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Andre Nollf233ea52008-07-21 17:05:22 +10004230 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004232 /* If there is a partially-recovered drive we need to
4233 * start recovery here. If we leave it to md_check_recovery,
4234 * it will remove the drives and not do the right thing
4235 */
NeilBrown0b8c9de2006-07-10 04:44:15 -07004236 if (mddev->degraded && !mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004237 int spares = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004238 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004239 if (rdev->raid_disk >= 0 &&
4240 !test_bit(In_sync, &rdev->flags) &&
4241 !test_bit(Faulty, &rdev->flags))
4242 /* complete an interrupted recovery */
4243 spares++;
4244 if (spares && mddev->pers->sync_request) {
4245 mddev->recovery = 0;
4246 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4247 mddev->sync_thread = md_register_thread(md_do_sync,
4248 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10004249 "resync");
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004250 if (!mddev->sync_thread) {
4251 printk(KERN_ERR "%s: could not start resync"
4252 " thread...\n",
4253 mdname(mddev));
4254 /* leave the spares where they are, it shouldn't hurt */
4255 mddev->recovery = 0;
NeilBrown0b8c9de2006-07-10 04:44:15 -07004256 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004257 }
4258 }
NeilBrown0b8c9de2006-07-10 04:44:15 -07004259 md_wakeup_thread(mddev->thread);
4260 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004261
NeilBrown449aad32009-08-03 10:59:58 +10004262 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004263 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004264 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004265 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004266 if (mddev->sysfs_action)
4267 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004268 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004269 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 return 0;
4271}
4272
4273static int restart_array(mddev_t *mddev)
4274{
4275 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Andre Noll80fab1d2008-07-11 22:02:21 +10004277 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004279 return -ENXIO;
4280 if (!mddev->pers)
4281 return -EINVAL;
4282 if (!mddev->ro)
4283 return -EBUSY;
4284 mddev->safemode = 0;
4285 mddev->ro = 0;
4286 set_disk_ro(disk, 0);
4287 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4288 mdname(mddev));
4289 /* Kick recovery or resync if necessary */
4290 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4291 md_wakeup_thread(mddev->thread);
4292 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004293 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295}
4296
NeilBrownacc55e22006-06-26 00:27:47 -07004297/* similar to deny_write_access, but accounts for our holding a reference
4298 * to the file ourselves */
4299static int deny_bitmap_write_access(struct file * file)
4300{
4301 struct inode *inode = file->f_mapping->host;
4302
4303 spin_lock(&inode->i_lock);
4304 if (atomic_read(&inode->i_writecount) > 1) {
4305 spin_unlock(&inode->i_lock);
4306 return -ETXTBSY;
4307 }
4308 atomic_set(&inode->i_writecount, -1);
4309 spin_unlock(&inode->i_lock);
4310
4311 return 0;
4312}
4313
4314static void restore_bitmap_write_access(struct file *file)
4315{
4316 struct inode *inode = file->f_mapping->host;
4317
4318 spin_lock(&inode->i_lock);
4319 atomic_set(&inode->i_writecount, 1);
4320 spin_unlock(&inode->i_lock);
4321}
4322
NeilBrown9e653b62006-06-26 00:27:58 -07004323/* mode:
4324 * 0 - completely stop and dis-assemble array
4325 * 1 - switch to readonly
4326 * 2 - stop but do not disassemble array
4327 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004328static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
4330 int err = 0;
4331 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004332 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
NeilBrownc8c00a62009-08-10 12:50:52 +10004334 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004335 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004336 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004337 err = -EBUSY;
4338 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004341 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4343 md_unregister_thread(mddev->sync_thread);
4344 mddev->sync_thread = NULL;
4345 }
4346
4347 del_timer_sync(&mddev->safemode_timer);
4348
NeilBrown9e653b62006-06-26 00:27:58 -07004349 switch(mode) {
4350 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004352 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 goto out;
4354 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004355 break;
4356 case 0: /* disassemble */
4357 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004358 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004359 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 if (mddev->ro)
4361 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004364 mddev->queue->merge_bvec_fn = NULL;
4365 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004366 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 module_put(mddev->pers->owner);
Dan Williams5fd3a172009-03-04 00:57:25 -07004368 if (mddev->pers->sync_request)
4369 mddev->private = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004371 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004372 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004373
NeilBrownc4647292009-05-07 12:51:06 +10004374 list_for_each_entry(rdev, &mddev->disks, same_set)
4375 if (rdev->raid_disk >= 0) {
4376 char nm[20];
4377 sprintf(nm, "rd%d", rdev->raid_disk);
4378 sysfs_remove_link(&mddev->kobj, nm);
4379 }
4380
NeilBrown0d4ca602006-12-10 02:20:44 -08004381 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004382 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004383
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 if (mddev->ro)
4385 mddev->ro = 0;
4386 }
NeilBrown850b2b42006-10-03 01:15:46 -07004387 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388 /* mark array as shutdown cleanly */
4389 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004390 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 }
NeilBrown9e653b62006-06-26 00:27:58 -07004392 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004394 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004395 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004397out:
4398 mutex_unlock(&mddev->open_mutex);
4399 if (err)
4400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 /*
4402 * Free resources if final stop
4403 */
NeilBrown9e653b62006-06-26 00:27:58 -07004404 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004405
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4407
NeilBrown978f9462006-02-02 14:28:05 -08004408 bitmap_destroy(mddev);
4409 if (mddev->bitmap_file) {
NeilBrownacc55e22006-06-26 00:27:47 -07004410 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown978f9462006-02-02 14:28:05 -08004411 fput(mddev->bitmap_file);
4412 mddev->bitmap_file = NULL;
4413 }
4414 mddev->bitmap_offset = 0;
4415
NeilBrown177a99b2008-02-06 01:39:56 -08004416 /* make sure all md_delayed_delete calls have finished */
NeilBrown5792a282007-04-04 19:08:18 -07004417 flush_scheduled_work();
4418
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 export_array(mddev);
4420
Andre Nollf233ea52008-07-21 17:05:22 +10004421 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004422 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004423 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004424 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004425 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004426 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004427 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004428 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004429 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004430 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004431 mddev->level = LEVEL_NONE;
4432 mddev->clevel[0] = 0;
4433 mddev->flags = 0;
4434 mddev->ro = 0;
4435 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004436 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004437 mddev->ctime = mddev->utime = 0;
4438 mddev->layout = 0;
4439 mddev->max_disks = 0;
4440 mddev->events = 0;
4441 mddev->delta_disks = 0;
4442 mddev->new_level = LEVEL_NONE;
4443 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004444 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004445 mddev->curr_resync = 0;
4446 mddev->resync_mismatches = 0;
4447 mddev->suspend_lo = mddev->suspend_hi = 0;
4448 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4449 mddev->recovery = 0;
4450 mddev->in_sync = 0;
4451 mddev->changed = 0;
4452 mddev->degraded = 0;
4453 mddev->barriers_work = 0;
4454 mddev->safemode = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004455 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004456 if (mddev->hold_active == UNTIL_STOP)
4457 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004458
NeilBrowna8a55c32006-06-26 00:27:39 -07004459 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4461 mdname(mddev));
4462 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004463 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004464 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004465 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return err;
4467}
4468
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004469#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470static void autorun_array(mddev_t *mddev)
4471{
4472 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 int err;
4474
NeilBrowna757e642005-04-16 15:26:42 -07004475 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
4478 printk(KERN_INFO "md: running: ");
4479
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004480 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 char b[BDEVNAME_SIZE];
4482 printk("<%s>", bdevname(rdev->bdev,b));
4483 }
4484 printk("\n");
4485
NeilBrownd710e132008-10-13 11:55:12 +11004486 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 if (err) {
4488 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004489 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 }
4491}
4492
4493/*
4494 * lets try to run arrays based on all disks that have arrived
4495 * until now. (those are in pending_raid_disks)
4496 *
4497 * the method: pick the first pending disk, collect all disks with
4498 * the same UUID, remove all from the pending list and put them into
4499 * the 'same_array' list. Then order this list based on superblock
4500 * update time (freshest comes first), kick out 'old' disks and
4501 * compare superblocks. If everything's fine then run it.
4502 *
4503 * If "unit" is allocated, then bump its reference count
4504 */
4505static void autorun_devices(int part)
4506{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004507 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 mddev_t *mddev;
4509 char b[BDEVNAME_SIZE];
4510
4511 printk(KERN_INFO "md: autorun ...\n");
4512 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004513 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004515 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 rdev0 = list_entry(pending_raid_disks.next,
4517 mdk_rdev_t, same_set);
4518
4519 printk(KERN_INFO "md: considering %s ...\n",
4520 bdevname(rdev0->bdev,b));
4521 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004522 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 if (super_90_load(rdev, rdev0, 0) >= 0) {
4524 printk(KERN_INFO "md: adding %s ...\n",
4525 bdevname(rdev->bdev,b));
4526 list_move(&rdev->same_set, &candidates);
4527 }
4528 /*
4529 * now we have a set of devices, with all of them having
4530 * mostly sane superblocks. It's time to allocate the
4531 * mddev.
4532 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004533 if (part) {
4534 dev = MKDEV(mdp_major,
4535 rdev0->preferred_minor << MdpMinorShift);
4536 unit = MINOR(dev) >> MdpMinorShift;
4537 } else {
4538 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4539 unit = MINOR(dev);
4540 }
4541 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4543 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4544 break;
4545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
4547 md_probe(dev, NULL, NULL);
4548 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004549 if (!mddev || !mddev->gendisk) {
4550 if (mddev)
4551 mddev_put(mddev);
4552 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 "md: cannot allocate memory for md drive.\n");
4554 break;
4555 }
4556 if (mddev_lock(mddev))
4557 printk(KERN_WARNING "md: %s locked, cannot run\n",
4558 mdname(mddev));
4559 else if (mddev->raid_disks || mddev->major_version
4560 || !list_empty(&mddev->disks)) {
4561 printk(KERN_WARNING
4562 "md: %s already running, cannot run %s\n",
4563 mdname(mddev), bdevname(rdev0->bdev,b));
4564 mddev_unlock(mddev);
4565 } else {
4566 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004567 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004568 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 list_del_init(&rdev->same_set);
4570 if (bind_rdev_to_array(rdev, mddev))
4571 export_rdev(rdev);
4572 }
4573 autorun_array(mddev);
4574 mddev_unlock(mddev);
4575 }
4576 /* on success, candidates will be empty, on error
4577 * it won't...
4578 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004579 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004580 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 mddev_put(mddev);
4584 }
4585 printk(KERN_INFO "md: ... autorun DONE.\n");
4586}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004587#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589static int get_version(void __user * arg)
4590{
4591 mdu_version_t ver;
4592
4593 ver.major = MD_MAJOR_VERSION;
4594 ver.minor = MD_MINOR_VERSION;
4595 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4596
4597 if (copy_to_user(arg, &ver, sizeof(ver)))
4598 return -EFAULT;
4599
4600 return 0;
4601}
4602
4603static int get_array_info(mddev_t * mddev, void __user * arg)
4604{
4605 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004606 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
NeilBrowna9f326e2009-09-23 18:06:41 +10004609 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004610 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004612 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 failed++;
4614 else {
4615 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004616 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004617 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 else
4619 spare++;
4620 }
4621 }
4622
4623 info.major_version = mddev->major_version;
4624 info.minor_version = mddev->minor_version;
4625 info.patch_version = MD_PATCHLEVEL_VERSION;
4626 info.ctime = mddev->ctime;
4627 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004628 info.size = mddev->dev_sectors / 2;
4629 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004630 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 info.nr_disks = nr;
4632 info.raid_disks = mddev->raid_disks;
4633 info.md_minor = mddev->md_minor;
4634 info.not_persistent= !mddev->persistent;
4635
4636 info.utime = mddev->utime;
4637 info.state = 0;
4638 if (mddev->in_sync)
4639 info.state = (1<<MD_SB_CLEAN);
NeilBrown36fa3062005-09-09 16:23:45 -07004640 if (mddev->bitmap && mddev->bitmap_offset)
4641 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004642 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 info.working_disks = working;
4644 info.failed_disks = failed;
4645 info.spare_disks = spare;
4646
4647 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004648 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
4650 if (copy_to_user(arg, &info, sizeof(info)))
4651 return -EFAULT;
4652
4653 return 0;
4654}
4655
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004656static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004657{
4658 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4659 char *ptr, *buf = NULL;
4660 int err = -ENOMEM;
4661
Dan Williamsb5470dc2008-06-27 21:44:04 -07004662 if (md_allow_write(mddev))
4663 file = kmalloc(sizeof(*file), GFP_NOIO);
4664 else
4665 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004666
NeilBrown32a76272005-06-21 17:17:14 -07004667 if (!file)
4668 goto out;
4669
4670 /* bitmap disabled, zero the first byte and copy out */
4671 if (!mddev->bitmap || !mddev->bitmap->file) {
4672 file->pathname[0] = '\0';
4673 goto copy_out;
4674 }
4675
4676 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4677 if (!buf)
4678 goto out;
4679
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004680 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4681 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004682 goto out;
4683
4684 strcpy(file->pathname, ptr);
4685
4686copy_out:
4687 err = 0;
4688 if (copy_to_user(arg, file, sizeof(*file)))
4689 err = -EFAULT;
4690out:
4691 kfree(buf);
4692 kfree(file);
4693 return err;
4694}
4695
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696static int get_disk_info(mddev_t * mddev, void __user * arg)
4697{
4698 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 mdk_rdev_t *rdev;
4700
4701 if (copy_from_user(&info, arg, sizeof(info)))
4702 return -EFAULT;
4703
Andre Noll26ef3792008-07-11 22:02:21 +10004704 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 if (rdev) {
4706 info.major = MAJOR(rdev->bdev->bd_dev);
4707 info.minor = MINOR(rdev->bdev->bd_dev);
4708 info.raid_disk = rdev->raid_disk;
4709 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004710 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004712 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 info.state |= (1<<MD_DISK_ACTIVE);
4714 info.state |= (1<<MD_DISK_SYNC);
4715 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004716 if (test_bit(WriteMostly, &rdev->flags))
4717 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 } else {
4719 info.major = info.minor = 0;
4720 info.raid_disk = -1;
4721 info.state = (1<<MD_DISK_REMOVED);
4722 }
4723
4724 if (copy_to_user(arg, &info, sizeof(info)))
4725 return -EFAULT;
4726
4727 return 0;
4728}
4729
4730static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4731{
4732 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4733 mdk_rdev_t *rdev;
4734 dev_t dev = MKDEV(info->major,info->minor);
4735
4736 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4737 return -EOVERFLOW;
4738
4739 if (!mddev->raid_disks) {
4740 int err;
4741 /* expecting a device which has a superblock */
4742 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4743 if (IS_ERR(rdev)) {
4744 printk(KERN_WARNING
4745 "md: md_import_device returned %ld\n",
4746 PTR_ERR(rdev));
4747 return PTR_ERR(rdev);
4748 }
4749 if (!list_empty(&mddev->disks)) {
4750 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4751 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004752 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 .load_super(rdev, rdev0, mddev->minor_version);
4754 if (err < 0) {
4755 printk(KERN_WARNING
4756 "md: %s has different UUID to %s\n",
4757 bdevname(rdev->bdev,b),
4758 bdevname(rdev0->bdev,b2));
4759 export_rdev(rdev);
4760 return -EINVAL;
4761 }
4762 }
4763 err = bind_rdev_to_array(rdev, mddev);
4764 if (err)
4765 export_rdev(rdev);
4766 return err;
4767 }
4768
4769 /*
4770 * add_new_disk can be used once the array is assembled
4771 * to add "hot spares". They must already have a superblock
4772 * written
4773 */
4774 if (mddev->pers) {
4775 int err;
4776 if (!mddev->pers->hot_add_disk) {
4777 printk(KERN_WARNING
4778 "%s: personality does not support diskops!\n",
4779 mdname(mddev));
4780 return -EINVAL;
4781 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004782 if (mddev->persistent)
4783 rdev = md_import_device(dev, mddev->major_version,
4784 mddev->minor_version);
4785 else
4786 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 if (IS_ERR(rdev)) {
4788 printk(KERN_WARNING
4789 "md: md_import_device returned %ld\n",
4790 PTR_ERR(rdev));
4791 return PTR_ERR(rdev);
4792 }
NeilBrown41158c72005-06-21 17:17:25 -07004793 /* set save_raid_disk if appropriate */
4794 if (!mddev->persistent) {
4795 if (info->state & (1<<MD_DISK_SYNC) &&
4796 info->raid_disk < mddev->raid_disks)
4797 rdev->raid_disk = info->raid_disk;
4798 else
4799 rdev->raid_disk = -1;
4800 } else
4801 super_types[mddev->major_version].
4802 validate_super(mddev, rdev);
4803 rdev->saved_raid_disk = rdev->raid_disk;
4804
NeilBrownb2d444d2005-11-08 21:39:31 -08004805 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004806 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4807 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11004808 else
4809 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004810
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 rdev->raid_disk = -1;
4812 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07004813 if (!err && !mddev->pers->hot_remove_disk) {
4814 /* If there is hot_add_disk but no hot_remove_disk
4815 * then added disks for geometry changes,
4816 * and should be added immediately.
4817 */
4818 super_types[mddev->major_version].
4819 validate_super(mddev, rdev);
4820 err = mddev->pers->hot_add_disk(mddev, rdev);
4821 if (err)
4822 unbind_rdev_from_array(rdev);
4823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 if (err)
4825 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10004826 else
NeilBrown3c0ee632008-10-21 13:25:28 +11004827 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07004828
NeilBrown17571282006-12-10 02:20:52 -08004829 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10004830 if (mddev->degraded)
4831 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07004832 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07004833 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 return err;
4835 }
4836
4837 /* otherwise, add_new_disk is only allowed
4838 * for major_version==0 superblocks
4839 */
4840 if (mddev->major_version != 0) {
4841 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
4842 mdname(mddev));
4843 return -EINVAL;
4844 }
4845
4846 if (!(info->state & (1<<MD_DISK_FAULTY))) {
4847 int err;
NeilBrownd710e132008-10-13 11:55:12 +11004848 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 if (IS_ERR(rdev)) {
4850 printk(KERN_WARNING
4851 "md: error, md_import_device() returned %ld\n",
4852 PTR_ERR(rdev));
4853 return PTR_ERR(rdev);
4854 }
4855 rdev->desc_nr = info->number;
4856 if (info->raid_disk < mddev->raid_disks)
4857 rdev->raid_disk = info->raid_disk;
4858 else
4859 rdev->raid_disk = -1;
4860
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08004862 if (info->state & (1<<MD_DISK_SYNC))
4863 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004865 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
4866 set_bit(WriteMostly, &rdev->flags);
4867
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 if (!mddev->persistent) {
4869 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10004870 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 } else
Andre Noll0f420352008-07-11 22:02:23 +10004872 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10004873 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
NeilBrown2bf071b2006-01-06 00:20:55 -08004875 err = bind_rdev_to_array(rdev, mddev);
4876 if (err) {
4877 export_rdev(rdev);
4878 return err;
4879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 }
4881
4882 return 0;
4883}
4884
4885static int hot_remove_disk(mddev_t * mddev, dev_t dev)
4886{
4887 char b[BDEVNAME_SIZE];
4888 mdk_rdev_t *rdev;
4889
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 rdev = find_rdev(mddev, dev);
4891 if (!rdev)
4892 return -ENXIO;
4893
4894 if (rdev->raid_disk >= 0)
4895 goto busy;
4896
4897 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07004898 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08004899 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900
4901 return 0;
4902busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00004903 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 bdevname(rdev->bdev,b), mdname(mddev));
4905 return -EBUSY;
4906}
4907
4908static int hot_add_disk(mddev_t * mddev, dev_t dev)
4909{
4910 char b[BDEVNAME_SIZE];
4911 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 mdk_rdev_t *rdev;
4913
4914 if (!mddev->pers)
4915 return -ENODEV;
4916
4917 if (mddev->major_version != 0) {
4918 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
4919 " version-0 superblocks.\n",
4920 mdname(mddev));
4921 return -EINVAL;
4922 }
4923 if (!mddev->pers->hot_add_disk) {
4924 printk(KERN_WARNING
4925 "%s: personality does not support diskops!\n",
4926 mdname(mddev));
4927 return -EINVAL;
4928 }
4929
NeilBrownd710e132008-10-13 11:55:12 +11004930 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 if (IS_ERR(rdev)) {
4932 printk(KERN_WARNING
4933 "md: error, md_import_device() returned %ld\n",
4934 PTR_ERR(rdev));
4935 return -EINVAL;
4936 }
4937
4938 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10004939 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 else
Andre Noll0f420352008-07-11 22:02:23 +10004941 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
NeilBrown8190e752009-06-18 08:48:58 +10004943 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944
NeilBrownb2d444d2005-11-08 21:39:31 -08004945 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 printk(KERN_WARNING
4947 "md: can not hot-add faulty %s disk to %s!\n",
4948 bdevname(rdev->bdev,b), mdname(mddev));
4949 err = -EINVAL;
4950 goto abort_export;
4951 }
NeilBrownb2d444d2005-11-08 21:39:31 -08004952 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07004954 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08004955 err = bind_rdev_to_array(rdev, mddev);
4956 if (err)
4957 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958
4959 /*
4960 * The rest should better be atomic, we can have disk failures
4961 * noticed in interrupt contexts ...
4962 */
4963
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 rdev->raid_disk = -1;
4965
NeilBrown850b2b42006-10-03 01:15:46 -07004966 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967
4968 /*
4969 * Kick recovery, maybe this spare has to be added to the
4970 * array immediately.
4971 */
4972 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4973 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08004974 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975 return 0;
4976
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977abort_export:
4978 export_rdev(rdev);
4979 return err;
4980}
4981
NeilBrown32a76272005-06-21 17:17:14 -07004982static int set_bitmap_file(mddev_t *mddev, int fd)
4983{
4984 int err;
4985
NeilBrown36fa3062005-09-09 16:23:45 -07004986 if (mddev->pers) {
4987 if (!mddev->pers->quiesce)
4988 return -EBUSY;
4989 if (mddev->recovery || mddev->sync_thread)
4990 return -EBUSY;
4991 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07004992 }
4993
NeilBrown36fa3062005-09-09 16:23:45 -07004994
4995 if (fd >= 0) {
4996 if (mddev->bitmap)
4997 return -EEXIST; /* cannot add when bitmap is present */
4998 mddev->bitmap_file = fget(fd);
4999
5000 if (mddev->bitmap_file == NULL) {
5001 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5002 mdname(mddev));
5003 return -EBADF;
5004 }
5005
5006 err = deny_bitmap_write_access(mddev->bitmap_file);
5007 if (err) {
5008 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5009 mdname(mddev));
5010 fput(mddev->bitmap_file);
5011 mddev->bitmap_file = NULL;
5012 return err;
5013 }
NeilBrowna654b9d82005-06-21 17:17:27 -07005014 mddev->bitmap_offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005015 } else if (mddev->bitmap == NULL)
5016 return -ENOENT; /* cannot remove what isn't there */
5017 err = 0;
5018 if (mddev->pers) {
5019 mddev->pers->quiesce(mddev, 1);
5020 if (fd >= 0)
5021 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005022 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005023 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005024 fd = -1; /* make sure to put the file */
5025 }
NeilBrown36fa3062005-09-09 16:23:45 -07005026 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005027 }
5028 if (fd < 0) {
NeilBrownacc55e22006-06-26 00:27:47 -07005029 if (mddev->bitmap_file) {
5030 restore_bitmap_write_access(mddev->bitmap_file);
NeilBrown36fa3062005-09-09 16:23:45 -07005031 fput(mddev->bitmap_file);
NeilBrownacc55e22006-06-26 00:27:47 -07005032 }
NeilBrown36fa3062005-09-09 16:23:45 -07005033 mddev->bitmap_file = NULL;
5034 }
5035
NeilBrown32a76272005-06-21 17:17:14 -07005036 return err;
5037}
5038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039/*
5040 * set_array_info is used two different ways
5041 * The original usage is when creating a new array.
5042 * In this usage, raid_disks is > 0 and it together with
5043 * level, size, not_persistent,layout,chunksize determine the
5044 * shape of the array.
5045 * This will always create an array with a type-0.90.0 superblock.
5046 * The newer usage is when assembling an array.
5047 * In this case raid_disks will be 0, and the major_version field is
5048 * use to determine which style super-blocks are to be found on the devices.
5049 * The minor and patch _version numbers are also kept incase the
5050 * super_block handler wishes to interpret them.
5051 */
5052static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5053{
5054
5055 if (info->raid_disks == 0) {
5056 /* just setting version number for superblock loading */
5057 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005058 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 super_types[info->major_version].name == NULL) {
5060 /* maybe try to auto-load a module? */
5061 printk(KERN_INFO
5062 "md: superblock version %d not known\n",
5063 info->major_version);
5064 return -EINVAL;
5065 }
5066 mddev->major_version = info->major_version;
5067 mddev->minor_version = info->minor_version;
5068 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005069 mddev->persistent = !info->not_persistent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 return 0;
5071 }
5072 mddev->major_version = MD_MAJOR_VERSION;
5073 mddev->minor_version = MD_MINOR_VERSION;
5074 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5075 mddev->ctime = get_seconds();
5076
5077 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005078 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005079 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 mddev->raid_disks = info->raid_disks;
5081 /* don't set md_minor, it is determined by which /dev/md* was
5082 * openned
5083 */
5084 if (info->state & (1<<MD_SB_CLEAN))
5085 mddev->recovery_cp = MaxSector;
5086 else
5087 mddev->recovery_cp = 0;
5088 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005089 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090
5091 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005092 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093
5094 mddev->max_disks = MD_SB_DISKS;
5095
NeilBrowne6910632008-02-06 01:39:51 -08005096 if (mddev->persistent)
5097 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005098 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099
NeilBrownb2a27032005-11-28 13:44:12 -08005100 mddev->default_bitmap_offset = MD_SB_BYTES >> 9;
5101 mddev->bitmap_offset = 0;
5102
NeilBrownf6705572006-03-27 01:18:11 -08005103 mddev->reshape_position = MaxSector;
5104
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 /*
5106 * Generate a 128 bit UUID
5107 */
5108 get_random_bytes(mddev->uuid, 16);
5109
NeilBrownf6705572006-03-27 01:18:11 -08005110 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005111 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005112 mddev->new_layout = mddev->layout;
5113 mddev->delta_disks = 0;
5114
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 return 0;
5116}
5117
Dan Williams1f403622009-03-31 14:59:03 +11005118void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5119{
Dan Williamsb522adc2009-03-31 15:00:31 +11005120 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5121
5122 if (mddev->external_size)
5123 return;
5124
Dan Williams1f403622009-03-31 14:59:03 +11005125 mddev->array_sectors = array_sectors;
5126}
5127EXPORT_SYMBOL(md_set_array_sectors);
5128
Andre Nolld71f9f82008-07-11 22:02:22 +10005129static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005130{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005131 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005132 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005133 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005134
5135 if (mddev->pers->resize == NULL)
5136 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005137 /* The "num_sectors" is the number of sectors of each device that
5138 * is used. This can only make sense for arrays with redundancy.
5139 * linear and raid0 always use whatever space is available. We can only
5140 * consider changing this number if no resync or reconstruction is
5141 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005142 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005143 * of each device. If num_sectors is zero, we find the largest size
5144 * that fits.
5145
NeilBrowna35b0d62006-01-06 00:20:49 -08005146 */
5147 if (mddev->sync_thread)
5148 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005149 if (mddev->bitmap)
5150 /* Sorry, cannot grow a bitmap yet, just remove it,
5151 * grow, and re-add.
5152 */
5153 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005154 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005155 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005156
Andre Nolld71f9f82008-07-11 22:02:22 +10005157 if (fit && (num_sectors == 0 || num_sectors > avail))
5158 num_sectors = avail;
5159 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005160 return -ENOSPC;
5161 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005162 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005163 if (!rv)
5164 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005165 return rv;
5166}
5167
NeilBrownda943b992006-01-06 00:20:54 -08005168static int update_raid_disks(mddev_t *mddev, int raid_disks)
5169{
5170 int rv;
5171 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005172 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005173 return -EINVAL;
5174 if (raid_disks <= 0 ||
5175 raid_disks >= mddev->max_disks)
5176 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005177 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005178 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005179 mddev->delta_disks = raid_disks - mddev->raid_disks;
5180
5181 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005182 return rv;
5183}
5184
5185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186/*
5187 * update_array_info is used to change the configuration of an
5188 * on-line array.
5189 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5190 * fields in the info are checked against the array.
5191 * Any differences that cannot be handled will cause an error.
5192 * Normally, only one change can be managed at a time.
5193 */
5194static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5195{
5196 int rv = 0;
5197 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005198 int state = 0;
5199
5200 /* calculate expected state,ignoring low bits */
5201 if (mddev->bitmap && mddev->bitmap_offset)
5202 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203
5204 if (mddev->major_version != info->major_version ||
5205 mddev->minor_version != info->minor_version ||
5206/* mddev->patch_version != info->patch_version || */
5207 mddev->ctime != info->ctime ||
5208 mddev->level != info->level ||
5209/* mddev->layout != info->layout || */
5210 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005211 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005212 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5213 ((state^info->state) & 0xfffffe00)
5214 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 return -EINVAL;
5216 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005217 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5218 cnt++;
5219 if (mddev->raid_disks != info->raid_disks)
5220 cnt++;
5221 if (mddev->layout != info->layout)
5222 cnt++;
5223 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5224 cnt++;
5225 if (cnt == 0)
5226 return 0;
5227 if (cnt > 1)
5228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229
5230 if (mddev->layout != info->layout) {
5231 /* Change layout
5232 * we don't need to do anything at the md level, the
5233 * personality will take care of it all.
5234 */
NeilBrown50ac1682009-06-18 08:47:55 +10005235 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005237 else {
5238 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005239 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005240 if (rv)
5241 mddev->new_layout = mddev->layout;
5242 return rv;
5243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005245 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005246 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
NeilBrownda943b992006-01-06 00:20:54 -08005248 if (mddev->raid_disks != info->raid_disks)
5249 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250
NeilBrown36fa3062005-09-09 16:23:45 -07005251 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5252 if (mddev->pers->quiesce == NULL)
5253 return -EINVAL;
5254 if (mddev->recovery || mddev->sync_thread)
5255 return -EBUSY;
5256 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5257 /* add the bitmap */
5258 if (mddev->bitmap)
5259 return -EEXIST;
5260 if (mddev->default_bitmap_offset == 0)
5261 return -EINVAL;
5262 mddev->bitmap_offset = mddev->default_bitmap_offset;
5263 mddev->pers->quiesce(mddev, 1);
5264 rv = bitmap_create(mddev);
5265 if (rv)
5266 bitmap_destroy(mddev);
5267 mddev->pers->quiesce(mddev, 0);
5268 } else {
5269 /* remove the bitmap */
5270 if (!mddev->bitmap)
5271 return -ENOENT;
5272 if (mddev->bitmap->file)
5273 return -EINVAL;
5274 mddev->pers->quiesce(mddev, 1);
5275 bitmap_destroy(mddev);
5276 mddev->pers->quiesce(mddev, 0);
5277 mddev->bitmap_offset = 0;
5278 }
5279 }
NeilBrown850b2b42006-10-03 01:15:46 -07005280 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 return rv;
5282}
5283
5284static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5285{
5286 mdk_rdev_t *rdev;
5287
5288 if (mddev->pers == NULL)
5289 return -ENODEV;
5290
5291 rdev = find_rdev(mddev, dev);
5292 if (!rdev)
5293 return -ENODEV;
5294
5295 md_error(mddev, rdev);
5296 return 0;
5297}
5298
Andre Noll2f9618c2008-04-25 18:57:58 +02005299/*
5300 * We have a problem here : there is no easy way to give a CHS
5301 * virtual geometry. We currently pretend that we have a 2 heads
5302 * 4 sectors (with a BIG number of cylinders...). This drives
5303 * dosfs just mad... ;-)
5304 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005305static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5306{
5307 mddev_t *mddev = bdev->bd_disk->private_data;
5308
5309 geo->heads = 2;
5310 geo->sectors = 4;
5311 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5312 return 0;
5313}
5314
Al Viroa39907f2008-03-02 10:31:15 -05005315static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 unsigned int cmd, unsigned long arg)
5317{
5318 int err = 0;
5319 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 mddev_t *mddev = NULL;
5321
5322 if (!capable(CAP_SYS_ADMIN))
5323 return -EACCES;
5324
5325 /*
5326 * Commands dealing with the RAID driver but not any
5327 * particular array:
5328 */
5329 switch (cmd)
5330 {
5331 case RAID_VERSION:
5332 err = get_version(argp);
5333 goto done;
5334
5335 case PRINT_RAID_DEBUG:
5336 err = 0;
5337 md_print_devices();
5338 goto done;
5339
5340#ifndef MODULE
5341 case RAID_AUTORUN:
5342 err = 0;
5343 autostart_arrays(arg);
5344 goto done;
5345#endif
5346 default:;
5347 }
5348
5349 /*
5350 * Commands creating/starting a new array:
5351 */
5352
Al Viroa39907f2008-03-02 10:31:15 -05005353 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354
5355 if (!mddev) {
5356 BUG();
5357 goto abort;
5358 }
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 err = mddev_lock(mddev);
5361 if (err) {
5362 printk(KERN_INFO
5363 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5364 err, cmd);
5365 goto abort;
5366 }
5367
5368 switch (cmd)
5369 {
5370 case SET_ARRAY_INFO:
5371 {
5372 mdu_array_info_t info;
5373 if (!arg)
5374 memset(&info, 0, sizeof(info));
5375 else if (copy_from_user(&info, argp, sizeof(info))) {
5376 err = -EFAULT;
5377 goto abort_unlock;
5378 }
5379 if (mddev->pers) {
5380 err = update_array_info(mddev, &info);
5381 if (err) {
5382 printk(KERN_WARNING "md: couldn't update"
5383 " array info. %d\n", err);
5384 goto abort_unlock;
5385 }
5386 goto done_unlock;
5387 }
5388 if (!list_empty(&mddev->disks)) {
5389 printk(KERN_WARNING
5390 "md: array %s already has disks!\n",
5391 mdname(mddev));
5392 err = -EBUSY;
5393 goto abort_unlock;
5394 }
5395 if (mddev->raid_disks) {
5396 printk(KERN_WARNING
5397 "md: array %s already initialised!\n",
5398 mdname(mddev));
5399 err = -EBUSY;
5400 goto abort_unlock;
5401 }
5402 err = set_array_info(mddev, &info);
5403 if (err) {
5404 printk(KERN_WARNING "md: couldn't set"
5405 " array info. %d\n", err);
5406 goto abort_unlock;
5407 }
5408 }
5409 goto done_unlock;
5410
5411 default:;
5412 }
5413
5414 /*
5415 * Commands querying/configuring an existing array:
5416 */
NeilBrown32a76272005-06-21 17:17:14 -07005417 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005418 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005419 if ((!mddev->raid_disks && !mddev->external)
5420 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5421 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5422 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 err = -ENODEV;
5424 goto abort_unlock;
5425 }
5426
5427 /*
5428 * Commands even a read-only array can execute:
5429 */
5430 switch (cmd)
5431 {
5432 case GET_ARRAY_INFO:
5433 err = get_array_info(mddev, argp);
5434 goto done_unlock;
5435
NeilBrown32a76272005-06-21 17:17:14 -07005436 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005437 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005438 goto done_unlock;
5439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 case GET_DISK_INFO:
5441 err = get_disk_info(mddev, argp);
5442 goto done_unlock;
5443
5444 case RESTART_ARRAY_RW:
5445 err = restart_array(mddev);
5446 goto done_unlock;
5447
5448 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005449 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 goto done_unlock;
5451
5452 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005453 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 goto done_unlock;
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 }
5457
5458 /*
5459 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005460 * superblock, so we do not allow them on read-only arrays.
5461 * However non-MD ioctls (e.g. get-size) will still come through
5462 * here and hit the 'default' below, so only disallow
5463 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005465 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005466 if (mddev->ro == 2) {
5467 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005468 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005469 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5470 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005471 } else {
5472 err = -EROFS;
5473 goto abort_unlock;
5474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 }
5476
5477 switch (cmd)
5478 {
5479 case ADD_NEW_DISK:
5480 {
5481 mdu_disk_info_t info;
5482 if (copy_from_user(&info, argp, sizeof(info)))
5483 err = -EFAULT;
5484 else
5485 err = add_new_disk(mddev, &info);
5486 goto done_unlock;
5487 }
5488
5489 case HOT_REMOVE_DISK:
5490 err = hot_remove_disk(mddev, new_decode_dev(arg));
5491 goto done_unlock;
5492
5493 case HOT_ADD_DISK:
5494 err = hot_add_disk(mddev, new_decode_dev(arg));
5495 goto done_unlock;
5496
5497 case SET_DISK_FAULTY:
5498 err = set_disk_faulty(mddev, new_decode_dev(arg));
5499 goto done_unlock;
5500
5501 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005502 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 goto done_unlock;
5504
NeilBrown32a76272005-06-21 17:17:14 -07005505 case SET_BITMAP_FILE:
5506 err = set_bitmap_file(mddev, (int)arg);
5507 goto done_unlock;
5508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510 err = -EINVAL;
5511 goto abort_unlock;
5512 }
5513
5514done_unlock:
5515abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005516 if (mddev->hold_active == UNTIL_IOCTL &&
5517 err != -EINVAL)
5518 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 mddev_unlock(mddev);
5520
5521 return err;
5522done:
5523 if (err)
5524 MD_BUG();
5525abort:
5526 return err;
5527}
5528
Al Viroa39907f2008-03-02 10:31:15 -05005529static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530{
5531 /*
5532 * Succeed if we can lock the mddev, which confirms that
5533 * it isn't being stopped right now.
5534 */
NeilBrownd3374822009-01-09 08:31:10 +11005535 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 int err;
5537
NeilBrownd3374822009-01-09 08:31:10 +11005538 if (mddev->gendisk != bdev->bd_disk) {
5539 /* we are racing with mddev_put which is discarding this
5540 * bd_disk.
5541 */
5542 mddev_put(mddev);
5543 /* Wait until bdev->bd_disk is definitely gone */
5544 flush_scheduled_work();
5545 /* Then retry the open from the top */
5546 return -ERESTARTSYS;
5547 }
5548 BUG_ON(mddev != bdev->bd_disk->private_data);
5549
NeilBrownc8c00a62009-08-10 12:50:52 +10005550 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 goto out;
5552
5553 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005554 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005555 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556
Al Viroa39907f2008-03-02 10:31:15 -05005557 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 out:
5559 return err;
5560}
5561
Al Viroa39907f2008-03-02 10:31:15 -05005562static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563{
Al Viroa39907f2008-03-02 10:31:15 -05005564 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005566 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005567 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 mddev_put(mddev);
5569
5570 return 0;
5571}
5572
Linus Torvalds44ce62942007-05-09 18:51:36 -07005573static int md_media_changed(struct gendisk *disk)
5574{
5575 mddev_t *mddev = disk->private_data;
5576
5577 return mddev->changed;
5578}
5579
5580static int md_revalidate(struct gendisk *disk)
5581{
5582 mddev_t *mddev = disk->private_data;
5583
5584 mddev->changed = 0;
5585 return 0;
5586}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005587static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588{
5589 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005590 .open = md_open,
5591 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005592 .ioctl = md_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005593 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005594 .media_changed = md_media_changed,
5595 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596};
5597
Adrian Bunk75c96f82005-05-05 16:16:09 -07005598static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599{
5600 mdk_thread_t *thread = arg;
5601
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 /*
5603 * md_thread is a 'system-thread', it's priority should be very
5604 * high. We avoid resource deadlocks individually in each
5605 * raid personality. (RAID5 does preallocation) We also use RR and
5606 * the very same RT priority as kswapd, thus we will never get
5607 * into a priority inversion deadlock.
5608 *
5609 * we definitely have to have equal or higher priority than
5610 * bdflush, otherwise bdflush will deadlock if there are too
5611 * many dirty RAID5 blocks.
5612 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613
NeilBrown6985c432005-10-19 21:23:47 -07005614 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005615 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616
NeilBrown93588e22005-11-15 00:09:12 -08005617 /* We need to wait INTERRUPTIBLE so that
5618 * we don't add to the load-average.
5619 * That means we need to be sure no signals are
5620 * pending
5621 */
5622 if (signal_pending(current))
5623 flush_signals(current);
5624
5625 wait_event_interruptible_timeout
5626 (thread->wqueue,
5627 test_bit(THREAD_WAKEUP, &thread->flags)
5628 || kthread_should_stop(),
5629 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630
5631 clear_bit(THREAD_WAKEUP, &thread->flags);
5632
NeilBrown787453c2005-11-08 21:39:43 -08005633 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005635
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 return 0;
5637}
5638
5639void md_wakeup_thread(mdk_thread_t *thread)
5640{
5641 if (thread) {
5642 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5643 set_bit(THREAD_WAKEUP, &thread->flags);
5644 wake_up(&thread->wqueue);
5645 }
5646}
5647
5648mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5649 const char *name)
5650{
5651 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652
NeilBrown9ffae0c2006-01-06 00:20:32 -08005653 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 if (!thread)
5655 return NULL;
5656
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 init_waitqueue_head(&thread->wqueue);
5658
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 thread->run = run;
5660 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005661 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005662 thread->tsk = kthread_run(md_thread, thread,
5663 "%s_%s",
5664 mdname(thread->mddev),
5665 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005666 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 kfree(thread);
5668 return NULL;
5669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 return thread;
5671}
5672
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673void md_unregister_thread(mdk_thread_t *thread)
5674{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005675 if (!thread)
5676 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005677 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005678
5679 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 kfree(thread);
5681}
5682
5683void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5684{
5685 if (!mddev) {
5686 MD_BUG();
5687 return;
5688 }
5689
NeilBrownb2d444d2005-11-08 21:39:31 -08005690 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005692
5693 if (mddev->external)
5694 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005695/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5697 mdname(mddev),
5698 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5699 __builtin_return_address(0),__builtin_return_address(1),
5700 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005701*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005702 if (!mddev->pers)
5703 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 if (!mddev->pers->error_handler)
5705 return;
5706 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005707 if (mddev->degraded)
5708 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
Neil Brown52664732008-06-28 08:31:44 +10005709 set_bit(StateChanged, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5711 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5712 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005713 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714}
5715
5716/* seq_file implementation /proc/mdstat */
5717
5718static void status_unused(struct seq_file *seq)
5719{
5720 int i = 0;
5721 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 seq_printf(seq, "unused devices: ");
5724
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005725 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 char b[BDEVNAME_SIZE];
5727 i++;
5728 seq_printf(seq, "%s ",
5729 bdevname(rdev->bdev,b));
5730 }
5731 if (!i)
5732 seq_printf(seq, "<none>");
5733
5734 seq_printf(seq, "\n");
5735}
5736
5737
5738static void status_resync(struct seq_file *seq, mddev_t * mddev)
5739{
NeilBrowndd71cf62009-05-07 12:49:35 +10005740 sector_t max_sectors, resync, res;
5741 unsigned long dt, db;
5742 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08005743 int scale;
5744 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745
NeilBrowndd71cf62009-05-07 12:49:35 +10005746 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747
5748 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10005749 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 else
NeilBrowndd71cf62009-05-07 12:49:35 +10005751 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
5753 /*
5754 * Should not happen.
5755 */
NeilBrowndd71cf62009-05-07 12:49:35 +10005756 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 MD_BUG();
5758 return;
5759 }
NeilBrown4588b422006-03-27 01:18:04 -08005760 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10005761 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08005762 * u32, as those are the requirements for sector_div.
5763 * Thus 'scale' must be at least 10
5764 */
5765 scale = 10;
5766 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10005767 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08005768 scale++;
5769 }
5770 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10005771 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08005772
5773 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 {
NeilBrown4588b422006-03-27 01:18:04 -08005775 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 seq_printf(seq, "[");
5777 for (i = 0; i < x; i++)
5778 seq_printf(seq, "=");
5779 seq_printf(seq, ">");
5780 for (i = 0; i < y; i++)
5781 seq_printf(seq, ".");
5782 seq_printf(seq, "] ");
5783 }
NeilBrown4588b422006-03-27 01:18:04 -08005784 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08005785 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
5786 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07005787 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
5788 "check" :
5789 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
5790 "resync" : "recovery"))),
5791 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10005792 (unsigned long long) resync/2,
5793 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794
5795 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 * dt: time from mark until now
5797 * db: blocks written from mark until now
5798 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10005799 *
5800 * rt is a sector_t, so could be 32bit or 64bit.
5801 * So we divide before multiply in case it is 32bit and close
5802 * to the limit.
5803 * We scale the divisor (db) by 32 to avoid loosing precision
5804 * near the end of resync when the number of remaining sectors
5805 * is close to 'db'.
5806 * We then divide rt by 32 after multiplying by db to compensate.
5807 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 */
5809 dt = ((jiffies - mddev->resync_mark) / HZ);
5810 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07005811 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
5812 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
NeilBrowndd71cf62009-05-07 12:49:35 +10005814 rt = max_sectors - resync; /* number of remaining sectors */
5815 sector_div(rt, db/32+1);
5816 rt *= dt;
5817 rt >>= 5;
5818
5819 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
5820 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821
NeilBrownff4e8d92006-07-10 04:44:16 -07005822 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823}
5824
5825static void *md_seq_start(struct seq_file *seq, loff_t *pos)
5826{
5827 struct list_head *tmp;
5828 loff_t l = *pos;
5829 mddev_t *mddev;
5830
5831 if (l >= 0x10000)
5832 return NULL;
5833 if (!l--)
5834 /* header */
5835 return (void*)1;
5836
5837 spin_lock(&all_mddevs_lock);
5838 list_for_each(tmp,&all_mddevs)
5839 if (!l--) {
5840 mddev = list_entry(tmp, mddev_t, all_mddevs);
5841 mddev_get(mddev);
5842 spin_unlock(&all_mddevs_lock);
5843 return mddev;
5844 }
5845 spin_unlock(&all_mddevs_lock);
5846 if (!l--)
5847 return (void*)2;/* tail */
5848 return NULL;
5849}
5850
5851static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
5852{
5853 struct list_head *tmp;
5854 mddev_t *next_mddev, *mddev = v;
5855
5856 ++*pos;
5857 if (v == (void*)2)
5858 return NULL;
5859
5860 spin_lock(&all_mddevs_lock);
5861 if (v == (void*)1)
5862 tmp = all_mddevs.next;
5863 else
5864 tmp = mddev->all_mddevs.next;
5865 if (tmp != &all_mddevs)
5866 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
5867 else {
5868 next_mddev = (void*)2;
5869 *pos = 0x10000;
5870 }
5871 spin_unlock(&all_mddevs_lock);
5872
5873 if (v != (void*)1)
5874 mddev_put(mddev);
5875 return next_mddev;
5876
5877}
5878
5879static void md_seq_stop(struct seq_file *seq, void *v)
5880{
5881 mddev_t *mddev = v;
5882
5883 if (mddev && v != (void*)1 && v != (void*)2)
5884 mddev_put(mddev);
5885}
5886
NeilBrownd7603b72006-01-06 00:20:30 -08005887struct mdstat_info {
5888 int event;
5889};
5890
Linus Torvalds1da177e2005-04-16 15:20:36 -07005891static int md_seq_show(struct seq_file *seq, void *v)
5892{
5893 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11005894 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08005896 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07005897 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898
5899 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08005900 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 seq_printf(seq, "Personalities : ");
5902 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08005903 list_for_each_entry(pers, &pers_list, list)
5904 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905
5906 spin_unlock(&pers_lock);
5907 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08005908 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 return 0;
5910 }
5911 if (v == (void*)2) {
5912 status_unused(seq);
5913 return 0;
5914 }
5915
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005916 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07005918
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
5920 seq_printf(seq, "%s : %sactive", mdname(mddev),
5921 mddev->pers ? "" : "in");
5922 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005923 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08005925 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07005926 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005927 seq_printf(seq, " %s", mddev->pers->name);
5928 }
5929
Andre Nolldd8ac332009-03-31 14:33:13 +11005930 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005931 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 char b[BDEVNAME_SIZE];
5933 seq_printf(seq, " %s[%d]",
5934 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005935 if (test_bit(WriteMostly, &rdev->flags))
5936 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08005937 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 seq_printf(seq, "(F)");
5939 continue;
NeilBrownb325a322005-09-09 16:24:00 -07005940 } else if (rdev->raid_disk < 0)
5941 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11005942 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 }
5944
5945 if (!list_empty(&mddev->disks)) {
5946 if (mddev->pers)
5947 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10005948 (unsigned long long)
5949 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 else
5951 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11005952 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07005954 if (mddev->persistent) {
5955 if (mddev->major_version != 0 ||
5956 mddev->minor_version != 90) {
5957 seq_printf(seq," super %d.%d",
5958 mddev->major_version,
5959 mddev->minor_version);
5960 }
NeilBrowne6910632008-02-06 01:39:51 -08005961 } else if (mddev->external)
5962 seq_printf(seq, " super external:%s",
5963 mddev->metadata_type);
5964 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07005965 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966
5967 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11005968 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08005970 if (mddev->pers->sync_request) {
5971 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11005972 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08005973 seq_printf(seq, "\n ");
5974 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
5975 seq_printf(seq, "\tresync=DELAYED\n ");
5976 else if (mddev->recovery_cp < MaxSector)
5977 seq_printf(seq, "\tresync=PENDING\n ");
5978 }
NeilBrown32a76272005-06-21 17:17:14 -07005979 } else
5980 seq_printf(seq, "\n ");
5981
5982 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07005983 unsigned long chunk_kb;
5984 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07005985 spin_lock_irqsave(&bitmap->lock, flags);
5986 chunk_kb = bitmap->chunksize >> 10;
5987 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
5988 "%lu%s chunk",
5989 bitmap->pages - bitmap->missing_pages,
5990 bitmap->pages,
5991 (bitmap->pages - bitmap->missing_pages)
5992 << (PAGE_SHIFT - 10),
5993 chunk_kb ? chunk_kb : bitmap->chunksize,
5994 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07005995 if (bitmap->file) {
5996 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08005997 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07005998 }
NeilBrown78d742d2005-06-21 17:17:15 -07005999
NeilBrown32a76272005-06-21 17:17:14 -07006000 seq_printf(seq, "\n");
6001 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 }
6003
6004 seq_printf(seq, "\n");
6005 }
6006 mddev_unlock(mddev);
6007
6008 return 0;
6009}
6010
Jan Engelhardt110518b2009-05-07 12:49:37 +10006011static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 .start = md_seq_start,
6013 .next = md_seq_next,
6014 .stop = md_seq_stop,
6015 .show = md_seq_show,
6016};
6017
6018static int md_seq_open(struct inode *inode, struct file *file)
6019{
6020 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006021 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6022 if (mi == NULL)
6023 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
6025 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006026 if (error)
6027 kfree(mi);
6028 else {
6029 struct seq_file *p = file->private_data;
6030 p->private = mi;
6031 mi->event = atomic_read(&md_event_count);
6032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 return error;
6034}
6035
NeilBrownd7603b72006-01-06 00:20:30 -08006036static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6037{
6038 struct seq_file *m = filp->private_data;
6039 struct mdstat_info *mi = m->private;
6040 int mask;
6041
6042 poll_wait(filp, &md_event_waiters, wait);
6043
6044 /* always allow read */
6045 mask = POLLIN | POLLRDNORM;
6046
6047 if (mi->event != atomic_read(&md_event_count))
6048 mask |= POLLERR | POLLPRI;
6049 return mask;
6050}
6051
Arjan van de Venfa027c22007-02-12 00:55:33 -08006052static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006053 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 .open = md_seq_open,
6055 .read = seq_read,
6056 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006057 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006058 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059};
6060
NeilBrown2604b702006-01-06 00:20:36 -08006061int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006064 list_add_tail(&p->list, &pers_list);
6065 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006066 spin_unlock(&pers_lock);
6067 return 0;
6068}
6069
NeilBrown2604b702006-01-06 00:20:36 -08006070int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071{
NeilBrown2604b702006-01-06 00:20:36 -08006072 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006074 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 spin_unlock(&pers_lock);
6076 return 0;
6077}
6078
NeilBrowneea1bf32009-03-31 14:27:02 +11006079static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006080{
6081 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006083 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084
6085 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006086 rcu_read_lock();
6087 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006088 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006089 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6090 (int)part_stat_read(&disk->part0, sectors[1]) -
6091 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006092 /* sync IO will cause sync_io to increase before the disk_stats
6093 * as sync_io is counted when a request starts, and
6094 * disk_stats is counted when it completes.
6095 * So resync activity will cause curr_events to be smaller than
6096 * when there was no such activity.
6097 * non-sync IO will cause disk_stat to increase without
6098 * increasing sync_io so curr_events will (eventually)
6099 * be larger than it was before. Once it becomes
6100 * substantially larger, the test below will cause
6101 * the array to appear non-idle, and resync will slow
6102 * down.
6103 * If there is a lot of outstanding resync activity when
6104 * we set last_event to curr_events, then all that activity
6105 * completing might cause the array to appear non-idle
6106 * and resync will be slowed down even though there might
6107 * not have been non-resync activity. This will only
6108 * happen once though. 'last_events' will soon reflect
6109 * the state where there is little or no outstanding
6110 * resync requests, and further resync activity will
6111 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006112 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006113 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006114 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006115 rdev->last_events = curr_events;
6116 idle = 0;
6117 }
6118 }
NeilBrown4b809912008-07-21 17:05:25 +10006119 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120 return idle;
6121}
6122
6123void md_done_sync(mddev_t *mddev, int blocks, int ok)
6124{
6125 /* another "blocks" (512byte) blocks have been synced */
6126 atomic_sub(blocks, &mddev->recovery_active);
6127 wake_up(&mddev->recovery_wait);
6128 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006129 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 md_wakeup_thread(mddev->thread);
6131 // stop recovery, signal do_sync ....
6132 }
6133}
6134
6135
NeilBrown06d91a52005-06-21 17:17:12 -07006136/* md_write_start(mddev, bi)
6137 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006138 * in superblock) before writing, schedule a superblock update
6139 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006140 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006141void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006142{
Neil Brown0fd62b82008-06-28 08:31:36 +10006143 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006144 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006145 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006146
NeilBrownf91de922005-11-08 21:39:36 -08006147 BUG_ON(mddev->ro == 1);
6148 if (mddev->ro == 2) {
6149 /* need to switch to read/write */
6150 mddev->ro = 0;
6151 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6152 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006153 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006154 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006155 }
NeilBrown06d91a52005-06-21 17:17:12 -07006156 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006157 if (mddev->safemode == 1)
6158 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006159 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006160 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006161 if (mddev->in_sync) {
6162 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006163 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006164 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006165 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006166 }
NeilBrowna9701a32005-11-08 21:39:34 -08006167 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006168 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006169 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006170 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006171 wait_event(mddev->sb_wait,
6172 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6173 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174}
6175
6176void md_write_end(mddev_t *mddev)
6177{
6178 if (atomic_dec_and_test(&mddev->writes_pending)) {
6179 if (mddev->safemode == 2)
6180 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006181 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6183 }
6184}
6185
NeilBrown2a2275d2007-01-26 00:57:11 -08006186/* md_allow_write(mddev)
6187 * Calling this ensures that the array is marked 'active' so that writes
6188 * may proceed without blocking. It is important to call this before
6189 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6190 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006191 *
6192 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6193 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006194 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006195int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006196{
6197 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006198 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006199 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006200 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006201 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006202 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006203
6204 spin_lock_irq(&mddev->write_lock);
6205 if (mddev->in_sync) {
6206 mddev->in_sync = 0;
6207 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6208 if (mddev->safemode_delay &&
6209 mddev->safemode == 0)
6210 mddev->safemode = 1;
6211 spin_unlock_irq(&mddev->write_lock);
6212 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006213 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006214 } else
6215 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006216
6217 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6218 return -EAGAIN;
6219 else
6220 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006221}
6222EXPORT_SYMBOL_GPL(md_allow_write);
6223
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224#define SYNC_MARKS 10
6225#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006226void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227{
6228 mddev_t *mddev2;
6229 unsigned int currspeed = 0,
6230 window;
NeilBrown57afd892005-06-21 17:17:13 -07006231 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 unsigned long mark[SYNC_MARKS];
6233 sector_t mark_cnt[SYNC_MARKS];
6234 int last_mark,m;
6235 struct list_head *tmp;
6236 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006237 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006238 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006239 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
6241 /* just incase thread restarts... */
6242 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6243 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006244 if (mddev->ro) /* never try to sync a read-only array */
6245 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246
NeilBrown61df9d92006-10-03 01:15:57 -07006247 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6248 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6249 desc = "data-check";
6250 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6251 desc = "requested-resync";
6252 else
6253 desc = "resync";
6254 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6255 desc = "reshape";
6256 else
6257 desc = "recovery";
6258
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259 /* we overload curr_resync somewhat here.
6260 * 0 == not engaged in resync at all
6261 * 2 == checking that there is no conflict with another sync
6262 * 1 == like 2, but have yielded to allow conflicting resync to
6263 * commense
6264 * other == active in resync - this many blocks
6265 *
6266 * Before starting a resync we must have set curr_resync to
6267 * 2, and then checked that every "conflicting" array has curr_resync
6268 * less than ours. When we find one that is the same or higher
6269 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6270 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6271 * This will mean we have to start checking from the beginning again.
6272 *
6273 */
6274
6275 do {
6276 mddev->curr_resync = 2;
6277
6278 try_again:
NeilBrown787453c2005-11-08 21:39:43 -08006279 if (kthread_should_stop()) {
NeilBrown6985c432005-10-19 21:23:47 -07006280 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 goto skip;
6282 }
NeilBrown29ac4aa2008-02-06 01:39:58 -08006283 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284 if (mddev2 == mddev)
6285 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006286 if (!mddev->parallel_resync
6287 && mddev2->curr_resync
6288 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 DEFINE_WAIT(wq);
6290 if (mddev < mddev2 && mddev->curr_resync == 2) {
6291 /* arbitrarily yield */
6292 mddev->curr_resync = 1;
6293 wake_up(&resync_wait);
6294 }
6295 if (mddev > mddev2 && mddev->curr_resync == 1)
6296 /* no need to wait here, we can wait the next
6297 * time 'round when curr_resync == 2
6298 */
6299 continue;
NeilBrown97441972008-09-19 11:49:54 +10006300 /* We need to wait 'interruptible' so as not to
6301 * contribute to the load average, and not to
6302 * be caught by 'softlockup'
6303 */
6304 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006305 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006306 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006307 printk(KERN_INFO "md: delaying %s of %s"
6308 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006310 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006312 if (signal_pending(current))
6313 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 schedule();
6315 finish_wait(&resync_wait, &wq);
6316 goto try_again;
6317 }
6318 finish_wait(&resync_wait, &wq);
6319 }
6320 }
6321 } while (mddev->curr_resync < 2);
6322
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006323 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006324 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006325 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006326 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 */
6328 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006329 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006330 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006331 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6332 j = mddev->resync_min;
6333 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006334 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006335
NeilBrownccfcc3c2006-03-27 01:18:09 -08006336 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006337 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006338 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006340 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006341 j = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006342 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006343 if (rdev->raid_disk >= 0 &&
6344 !test_bit(Faulty, &rdev->flags) &&
6345 !test_bit(In_sync, &rdev->flags) &&
6346 rdev->recovery_offset < j)
6347 j = rdev->recovery_offset;
6348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349
NeilBrown61df9d92006-10-03 01:15:57 -07006350 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6351 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6352 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006353 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006354 "(but not more than %d KB/sec) for %s.\n",
6355 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
NeilBrowneea1bf32009-03-31 14:27:02 +11006357 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006358
NeilBrown57afd892005-06-21 17:17:13 -07006359 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 for (m = 0; m < SYNC_MARKS; m++) {
6361 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006362 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 }
6364 last_mark = 0;
6365 mddev->resync_mark = mark[last_mark];
6366 mddev->resync_mark_cnt = mark_cnt[last_mark];
6367
6368 /*
6369 * Tune reconstruction:
6370 */
6371 window = 32*(PAGE_SIZE/512);
6372 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6373 window/2,(unsigned long long) max_sectors/2);
6374
6375 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 last_check = 0;
6377
6378 if (j>2) {
6379 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006380 "md: resuming %s of %s from checkpoint.\n",
6381 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006382 mddev->curr_resync = j;
6383 }
6384
6385 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006386 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387
NeilBrown57afd892005-06-21 17:17:13 -07006388 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006389
NeilBrown7a91ee12009-05-26 12:57:21 +10006390 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6391 ((mddev->curr_resync > mddev->curr_resync_completed &&
6392 (mddev->curr_resync - mddev->curr_resync_completed)
6393 > (max_sectors >> 4)) ||
6394 (j - mddev->curr_resync_completed)*2
6395 >= mddev->resync_max - mddev->curr_resync_completed
6396 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006397 /* time to update curr_resync_completed */
6398 blk_unplug(mddev->queue);
6399 wait_event(mddev->recovery_wait,
6400 atomic_read(&mddev->recovery_active) == 0);
6401 mddev->curr_resync_completed =
6402 mddev->curr_resync;
6403 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006404 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006405 }
NeilBrownacb180b2009-04-14 16:28:34 +10006406
NeilBrowne62e58a2009-07-01 13:15:35 +10006407 while (j >= mddev->resync_max && !kthread_should_stop()) {
6408 /* As this condition is controlled by user-space,
6409 * we can block indefinitely, so use '_interruptible'
6410 * to avoid triggering warnings.
6411 */
6412 flush_signals(current); /* just in case */
6413 wait_event_interruptible(mddev->recovery_wait,
6414 mddev->resync_max > j
6415 || kthread_should_stop());
6416 }
NeilBrownacb180b2009-04-14 16:28:34 +10006417
6418 if (kthread_should_stop())
6419 goto interrupted;
6420
NeilBrown57afd892005-06-21 17:17:13 -07006421 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006422 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006423 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006424 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006425 goto out;
6426 }
NeilBrown57afd892005-06-21 17:17:13 -07006427
6428 if (!skipped) { /* actual IO requested */
6429 io_sectors += sectors;
6430 atomic_add(sectors, &mddev->recovery_active);
6431 }
6432
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 j += sectors;
6434 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006435 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006436 if (last_check == 0)
6437 /* this is the earliers that rebuilt will be
6438 * visible in /proc/mdstat
6439 */
6440 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006441
6442 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443 continue;
6444
NeilBrown57afd892005-06-21 17:17:13 -07006445 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446
NeilBrowndfc70642008-05-23 13:04:39 -07006447 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 break;
6449
6450 repeat:
6451 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6452 /* step marks */
6453 int next = (last_mark+1) % SYNC_MARKS;
6454
6455 mddev->resync_mark = mark[next];
6456 mddev->resync_mark_cnt = mark_cnt[next];
6457 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006458 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459 last_mark = next;
6460 }
6461
6462
NeilBrownc6207272008-02-06 01:39:52 -08006463 if (kthread_should_stop())
6464 goto interrupted;
6465
Linus Torvalds1da177e2005-04-16 15:20:36 -07006466
6467 /*
6468 * this loop exits only if either when we are slower than
6469 * the 'hard' speed limit, or the system was IO-idle for
6470 * a jiffy.
6471 * the system might be non-idle CPU-wise, but we only care
6472 * about not overloading the IO subsystem. (things like an
6473 * e2fsck being done on the RAID array should execute fast)
6474 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006475 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006476 cond_resched();
6477
NeilBrown57afd892005-06-21 17:17:13 -07006478 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6479 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480
NeilBrown88202a02006-01-06 00:21:36 -08006481 if (currspeed > speed_min(mddev)) {
6482 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006483 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006484 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006485 goto repeat;
6486 }
6487 }
6488 }
NeilBrown61df9d92006-10-03 01:15:57 -07006489 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006490 /*
6491 * this also signals 'finished resyncing' to md_stop
6492 */
6493 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006494 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006495
6496 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6497
6498 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006499 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500
NeilBrowndfc70642008-05-23 13:04:39 -07006501 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006502 mddev->curr_resync > 2) {
6503 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6504 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6505 if (mddev->curr_resync >= mddev->recovery_cp) {
6506 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006507 "md: checkpointing %s of %s.\n",
6508 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006509 mddev->recovery_cp = mddev->curr_resync;
6510 }
6511 } else
6512 mddev->recovery_cp = MaxSector;
6513 } else {
6514 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6515 mddev->curr_resync = MaxSector;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006516 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006517 if (rdev->raid_disk >= 0 &&
6518 !test_bit(Faulty, &rdev->flags) &&
6519 !test_bit(In_sync, &rdev->flags) &&
6520 rdev->recovery_offset < mddev->curr_resync)
6521 rdev->recovery_offset = mddev->curr_resync;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006522 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 }
NeilBrown17571282006-12-10 02:20:52 -08006524 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526 skip:
6527 mddev->curr_resync = 0;
NeilBrownacb180b2009-04-14 16:28:34 +10006528 mddev->curr_resync_completed = 0;
NeilBrown24395a82009-11-06 14:59:27 +11006529 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6530 /* We completed so max setting can be forgotten. */
6531 mddev->resync_max = MaxSector;
NeilBrownc6207272008-02-06 01:39:52 -08006532 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533 wake_up(&resync_wait);
6534 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6535 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006536 return;
6537
6538 interrupted:
6539 /*
6540 * got a signal, exit.
6541 */
6542 printk(KERN_INFO
6543 "md: md_do_sync() got signal ... exiting\n");
6544 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6545 goto out;
6546
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547}
NeilBrown29269552006-03-27 01:18:10 -08006548EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
6550
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006551static int remove_and_add_spares(mddev_t *mddev)
6552{
6553 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006554 int spares = 0;
6555
NeilBrown97e4f422009-03-31 14:33:13 +11006556 mddev->curr_resync_completed = 0;
6557
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006558 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006559 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006560 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006561 (test_bit(Faulty, &rdev->flags) ||
6562 ! test_bit(In_sync, &rdev->flags)) &&
6563 atomic_read(&rdev->nr_pending)==0) {
6564 if (mddev->pers->hot_remove_disk(
6565 mddev, rdev->raid_disk)==0) {
6566 char nm[20];
6567 sprintf(nm,"rd%d", rdev->raid_disk);
6568 sysfs_remove_link(&mddev->kobj, nm);
6569 rdev->raid_disk = -1;
6570 }
6571 }
6572
NeilBrown4044ba52009-01-09 08:31:11 +11006573 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006574 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006575 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006576 !test_bit(In_sync, &rdev->flags) &&
6577 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006578 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006579 if (rdev->raid_disk < 0
6580 && !test_bit(Faulty, &rdev->flags)) {
6581 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006582 if (mddev->pers->
6583 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006584 char nm[20];
6585 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006586 if (sysfs_create_link(&mddev->kobj,
6587 &rdev->kobj, nm))
6588 printk(KERN_WARNING
6589 "md: cannot register "
6590 "%s for %s\n",
6591 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006592 spares++;
6593 md_new_event(mddev);
6594 } else
6595 break;
6596 }
NeilBrowndfc70642008-05-23 13:04:39 -07006597 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006598 }
6599 return spares;
6600}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601/*
6602 * This routine is regularly called by all per-raid-array threads to
6603 * deal with generic issues like resync and super-block update.
6604 * Raid personalities that don't have a thread (linear/raid0) do not
6605 * need this as they never do any recovery or update the superblock.
6606 *
6607 * It does not do any resync itself, but rather "forks" off other threads
6608 * to do that as needed.
6609 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6610 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006611 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006612 * and wakeups up this thread which will reap the thread and finish up.
6613 * This thread also removes any faulty devices (with nr_pending == 0).
6614 *
6615 * The overall approach is:
6616 * 1/ if the superblock needs updating, update it.
6617 * 2/ If a recovery thread is running, don't do anything else.
6618 * 3/ If recovery has finished, clean up, possibly marking spares active.
6619 * 4/ If there are any faulty devices, remove them.
6620 * 5/ If array is degraded, try to add spares devices
6621 * 6/ If array has spares or is not in-sync, start a resync thread.
6622 */
6623void md_check_recovery(mddev_t *mddev)
6624{
6625 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626
6627
NeilBrown5f404022005-06-21 17:17:16 -07006628 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006629 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630
6631 if (mddev->ro)
6632 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006633
6634 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006635 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006636 printk(KERN_INFO "md: %s in immediate safe mode\n",
6637 mdname(mddev));
6638 mddev->safemode = 2;
6639 }
6640 flush_signals(current);
6641 }
6642
NeilBrownc89a8ee2008-08-05 15:54:13 +10006643 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6644 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006646 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006648 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006649 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006650 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6651 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 ))
6653 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006654
NeilBrowndf5b89b2006-03-27 01:18:20 -08006655 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006656 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006657
NeilBrownc89a8ee2008-08-05 15:54:13 +10006658 if (mddev->ro) {
6659 /* Only thing we do on a ro array is remove
6660 * failed devices.
6661 */
6662 remove_and_add_spares(mddev);
6663 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6664 goto unlock;
6665 }
6666
NeilBrown31a59e32008-04-30 00:52:30 -07006667 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006668 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006669 spin_lock_irq(&mddev->write_lock);
6670 if (mddev->safemode &&
6671 !atomic_read(&mddev->writes_pending) &&
6672 !mddev->in_sync &&
6673 mddev->recovery_cp == MaxSector) {
6674 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006675 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006676 if (mddev->persistent)
6677 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6678 }
6679 if (mddev->safemode == 1)
6680 mddev->safemode = 0;
6681 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006682 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006683 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006684 }
NeilBrownfca4d842005-06-21 17:17:11 -07006685
NeilBrown850b2b42006-10-03 01:15:46 -07006686 if (mddev->flags)
6687 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006688
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006689 list_for_each_entry(rdev, &mddev->disks, same_set)
Neil Brown52664732008-06-28 08:31:44 +10006690 if (test_and_clear_bit(StateChanged, &rdev->flags))
NeilBrown3c0ee632008-10-21 13:25:28 +11006691 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown52664732008-06-28 08:31:44 +10006692
NeilBrown06d91a52005-06-21 17:17:12 -07006693
Linus Torvalds1da177e2005-04-16 15:20:36 -07006694 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6695 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6696 /* resync/recovery still happening */
6697 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6698 goto unlock;
6699 }
6700 if (mddev->sync_thread) {
6701 /* resync has finished, collect result */
6702 md_unregister_thread(mddev->sync_thread);
6703 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006704 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6705 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706 /* success...*/
6707 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006708 if (mddev->pers->spare_active(mddev))
6709 sysfs_notify(&mddev->kobj, NULL,
6710 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 }
NeilBrowncea9c222009-03-31 15:15:05 +11006712 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6713 mddev->pers->finish_reshape)
6714 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006715 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006716
6717 /* if array is no-longer degraded, then any saved_raid_disk
6718 * information must be scrapped
6719 */
6720 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006721 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006722 rdev->saved_raid_disk = -1;
6723
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 mddev->recovery = 0;
6725 /* flag recovery needed just to double check */
6726 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006727 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006728 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729 goto unlock;
6730 }
Neil Brown72a23c22008-06-28 08:31:41 +10006731 /* Set RUNNING before clearing NEEDED to avoid
6732 * any transients in the value of "sync_action".
6733 */
6734 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6735 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006736 /* Clear some bits that don't mean anything, but
6737 * might be left set
6738 */
NeilBrown24dd4692005-11-08 21:39:26 -08006739 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
6740 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006742 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
6743 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 /* no recovery is running.
6745 * remove any failed drives, then
6746 * add spares if possible.
6747 * Spare are also removed and re-added, to allow
6748 * the personality to fail the re-add.
6749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006751 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10006752 if (mddev->pers->check_reshape == NULL ||
6753 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006754 /* Cannot proceed */
6755 goto unlock;
6756 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006757 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006758 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08006759 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
6760 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07006761 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006762 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006763 } else if (mddev->recovery_cp < MaxSector) {
6764 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10006765 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08006766 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
6767 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08006769
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07006771 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
6772 /* We are adding a device or devices to an array
6773 * which has the bitmap stored on all devices.
6774 * So make sure all bitmap pages get written
6775 */
6776 bitmap_write_all(mddev->bitmap);
6777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 mddev->sync_thread = md_register_thread(md_do_sync,
6779 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10006780 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 if (!mddev->sync_thread) {
6782 printk(KERN_ERR "%s: could not start resync"
6783 " thread...\n",
6784 mdname(mddev));
6785 /* leave the spares where they are, it shouldn't hurt */
6786 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08006787 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11006789 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006790 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791 }
6792 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10006793 if (!mddev->sync_thread) {
6794 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
6795 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
6796 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11006797 if (mddev->sysfs_action)
6798 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10006799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 mddev_unlock(mddev);
6801 }
6802}
6803
Dan Williams6bfe0b42008-04-30 00:52:32 -07006804void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
6805{
NeilBrown3c0ee632008-10-21 13:25:28 +11006806 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07006807 wait_event_timeout(rdev->blocked_wait,
6808 !test_bit(Blocked, &rdev->flags),
6809 msecs_to_jiffies(5000));
6810 rdev_dec_pending(rdev, mddev);
6811}
6812EXPORT_SYMBOL(md_wait_for_blocked_rdev);
6813
Adrian Bunk75c96f82005-05-05 16:16:09 -07006814static int md_notify_reboot(struct notifier_block *this,
6815 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816{
6817 struct list_head *tmp;
6818 mddev_t *mddev;
6819
6820 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
6821
6822 printk(KERN_INFO "md: stopping all md devices.\n");
6823
NeilBrown29ac4aa2008-02-06 01:39:58 -08006824 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10006825 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10006826 /* Force a switch to readonly even array
6827 * appears to still be in use. Hence
6828 * the '100'.
6829 */
NeilBrownd710e132008-10-13 11:55:12 +11006830 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10006831 mddev_unlock(mddev);
6832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 /*
6834 * certain more exotic SCSI devices are known to be
6835 * volatile wrt too early system reboots. While the
6836 * right place to handle this issue is the given
6837 * driver, we do want to have a safe RAID driver ...
6838 */
6839 mdelay(1000*1);
6840 }
6841 return NOTIFY_DONE;
6842}
6843
Adrian Bunk75c96f82005-05-05 16:16:09 -07006844static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845 .notifier_call = md_notify_reboot,
6846 .next = NULL,
6847 .priority = INT_MAX, /* before any real devices */
6848};
6849
6850static void md_geninit(void)
6851{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006852 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
6853
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07006854 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855}
6856
Adrian Bunk75c96f82005-05-05 16:16:09 -07006857static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006859 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006860 return -1;
6861 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006862 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 return -1;
6864 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006865 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07006866 md_probe, NULL, NULL);
6867 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868 md_probe, NULL, NULL);
6869
Linus Torvalds1da177e2005-04-16 15:20:36 -07006870 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08006871 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006872
6873 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11006874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875}
6876
6877
6878#ifndef MODULE
6879
6880/*
6881 * Searches all registered partitions for autorun RAID arrays
6882 * at boot time.
6883 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006884
6885static LIST_HEAD(all_detected_devices);
6886struct detected_devices_node {
6887 struct list_head list;
6888 dev_t dev;
6889};
Linus Torvalds1da177e2005-04-16 15:20:36 -07006890
6891void md_autodetect_dev(dev_t dev)
6892{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006893 struct detected_devices_node *node_detected_dev;
6894
6895 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
6896 if (node_detected_dev) {
6897 node_detected_dev->dev = dev;
6898 list_add_tail(&node_detected_dev->list, &all_detected_devices);
6899 } else {
6900 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
6901 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
6902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903}
6904
6905
6906static void autostart_arrays(int part)
6907{
6908 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006909 struct detected_devices_node *node_detected_dev;
6910 dev_t dev;
6911 int i_scanned, i_passed;
6912
6913 i_scanned = 0;
6914 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006915
6916 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
6917
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006918 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
6919 i_scanned++;
6920 node_detected_dev = list_entry(all_detected_devices.next,
6921 struct detected_devices_node, list);
6922 list_del(&node_detected_dev->list);
6923 dev = node_detected_dev->dev;
6924 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07006925 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926 if (IS_ERR(rdev))
6927 continue;
6928
NeilBrownb2d444d2005-11-08 21:39:31 -08006929 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930 MD_BUG();
6931 continue;
6932 }
NeilBrownd0fae182008-03-04 14:29:31 -08006933 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006934 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006935 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07006937
6938 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
6939 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006940
6941 autorun_devices(part);
6942}
6943
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08006944#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945
6946static __exit void md_exit(void)
6947{
6948 mddev_t *mddev;
6949 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07006950
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006951 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07006952 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006953
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11006954 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955 unregister_blkdev(mdp_major, "mdp");
6956 unregister_reboot_notifier(&md_notifier);
6957 unregister_sysctl_table(raid_table_header);
6958 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08006959 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11006961 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006962 }
6963}
6964
Dan Williams685784a2007-07-09 11:56:42 -07006965subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006966module_exit(md_exit)
6967
NeilBrownf91de922005-11-08 21:39:36 -08006968static int get_ro(char *buffer, struct kernel_param *kp)
6969{
6970 return sprintf(buffer, "%d", start_readonly);
6971}
6972static int set_ro(const char *val, struct kernel_param *kp)
6973{
6974 char *e;
6975 int num = simple_strtoul(val, &e, 10);
6976 if (*val && (*e == '\0' || *e == '\n')) {
6977 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08006978 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08006979 }
6980 return -EINVAL;
6981}
6982
NeilBrown80ca3a42006-07-10 04:44:18 -07006983module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
6984module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08006985
NeilBrownefeb53c2009-01-09 08:31:10 +11006986module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08006987
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988EXPORT_SYMBOL(register_md_personality);
6989EXPORT_SYMBOL(unregister_md_personality);
6990EXPORT_SYMBOL(md_error);
6991EXPORT_SYMBOL(md_done_sync);
6992EXPORT_SYMBOL(md_write_start);
6993EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994EXPORT_SYMBOL(md_register_thread);
6995EXPORT_SYMBOL(md_unregister_thread);
6996EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997EXPORT_SYMBOL(md_check_recovery);
6998MODULE_LICENSE("GPL");
NeilBrownaa1595e2005-08-04 12:53:32 -07006999MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007000MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);