blob: 2a64cba9ea72f534f202fee7c059ba59d5f9dea5 [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>
André Goddard Rosae7d28602009-12-14 18:01:06 -080042#include <linux/string.h>
NeilBrownfb4d8c72008-10-13 11:55:12 +110043#include <linux/hdreg.h>
44#include <linux/proc_fs.h>
45#include <linux/random.h>
46#include <linux/reboot.h>
NeilBrown32a76272005-06-21 17:17:14 -070047#include <linux/file.h>
Arnd Bergmannaa98aa32009-12-14 12:50:05 +110048#include <linux/compat.h>
Stephen Rothwell25570722008-10-15 09:09:21 +110049#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110050#include <linux/raid/md_p.h>
51#include <linux/raid/md_u.h>
NeilBrown43b2e5d2009-03-31 14:33:13 +110052#include "md.h"
Christoph Hellwigef740c32009-03-31 14:27:03 +110053#include "bitmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#define DEBUG 0
56#define dprintk(x...) ((void)(DEBUG && printk(x)))
57
58
59#ifndef MODULE
NeilBrownd710e132008-10-13 11:55:12 +110060static void autostart_arrays(int part);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
NeilBrown2604b702006-01-06 00:20:36 -080063static LIST_HEAD(pers_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static DEFINE_SPINLOCK(pers_lock);
65
Adrian Bunk5e563412006-06-26 00:27:42 -070066static void md_print_devices(void);
67
Bernd Schubert90b08712008-05-23 13:04:38 -070068static DECLARE_WAIT_QUEUE_HEAD(resync_wait);
69
Adrian Bunk5e563412006-06-26 00:27:42 -070070#define MD_BUG(x...) { printk("md: bug in file %s, line %d\n", __FILE__, __LINE__); md_print_devices(); }
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/*
Robert Becker1e509152009-12-14 12:49:58 +110073 * Default number of read corrections we'll attempt on an rdev
74 * before ejecting it from the array. We divide the read error
75 * count by 2 for every hour elapsed between read errors.
76 */
77#define MD_DEFAULT_MAX_CORRECTED_READ_ERRORS 20
78/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * Current RAID-1,4,5 parallel reconstruction 'guaranteed speed limit'
80 * is 1000 KB/sec, so the extra system load does not show up that much.
81 * Increase it if you want to have more _guaranteed_ speed. Note that
Adrian Bunk338cec32005-09-10 00:26:54 -070082 * the RAID driver will use the maximum available bandwidth if the IO
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * subsystem is idle. There is also an 'absolute maximum' reconstruction
84 * speed limit - in case reconstruction slows down your system despite
85 * idle IO detection.
86 *
87 * you can change it via /proc/sys/dev/raid/speed_limit_min and _max.
NeilBrown88202a02006-01-06 00:21:36 -080088 * or /sys/block/mdX/md/sync_speed_{min,max}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
90
91static int sysctl_speed_limit_min = 1000;
92static int sysctl_speed_limit_max = 200000;
NeilBrown88202a02006-01-06 00:21:36 -080093static inline int speed_min(mddev_t *mddev)
94{
95 return mddev->sync_speed_min ?
96 mddev->sync_speed_min : sysctl_speed_limit_min;
97}
98
99static inline int speed_max(mddev_t *mddev)
100{
101 return mddev->sync_speed_max ?
102 mddev->sync_speed_max : sysctl_speed_limit_max;
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105static struct ctl_table_header *raid_table_header;
106
107static ctl_table raid_table[] = {
108 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .procname = "speed_limit_min",
110 .data = &sysctl_speed_limit_min,
111 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700112 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800113 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 },
115 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 .procname = "speed_limit_max",
117 .data = &sysctl_speed_limit_max,
118 .maxlen = sizeof(int),
NeilBrown80ca3a42006-07-10 04:44:18 -0700119 .mode = S_IRUGO|S_IWUSR,
Eric W. Biederman6d456112009-11-16 03:11:48 -0800120 .proc_handler = proc_dointvec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800122 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123};
124
125static ctl_table raid_dir_table[] = {
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 .procname = "raid",
128 .maxlen = 0,
NeilBrown80ca3a42006-07-10 04:44:18 -0700129 .mode = S_IRUGO|S_IXUGO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 .child = raid_table,
131 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800132 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133};
134
135static ctl_table raid_root_table[] = {
136 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .procname = "dev",
138 .maxlen = 0,
139 .mode = 0555,
140 .child = raid_dir_table,
141 },
Eric W. Biederman894d2492009-11-05 14:34:02 -0800142 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700145static const struct block_device_operations md_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
NeilBrownf91de922005-11-08 21:39:36 -0800147static int start_readonly;
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149/*
NeilBrownd7603b72006-01-06 00:20:30 -0800150 * We have a system wide 'event count' that is incremented
151 * on any 'interesting' event, and readers of /proc/mdstat
152 * can use 'poll' or 'select' to find out when the event
153 * count increases.
154 *
155 * Events are:
156 * start array, stop array, error, add device, remove device,
157 * start build, activate spare
158 */
NeilBrown2989ddb2006-01-06 00:20:43 -0800159static DECLARE_WAIT_QUEUE_HEAD(md_event_waiters);
NeilBrownd7603b72006-01-06 00:20:30 -0800160static atomic_t md_event_count;
NeilBrown29269552006-03-27 01:18:10 -0800161void md_new_event(mddev_t *mddev)
NeilBrownd7603b72006-01-06 00:20:30 -0800162{
163 atomic_inc(&md_event_count);
164 wake_up(&md_event_waiters);
165}
NeilBrown29269552006-03-27 01:18:10 -0800166EXPORT_SYMBOL_GPL(md_new_event);
NeilBrownd7603b72006-01-06 00:20:30 -0800167
NeilBrownc331eb02006-05-30 21:27:13 -0700168/* Alternate version that can be called from interrupts
169 * when calling sysfs_notify isn't needed.
170 */
Adrian Bunk05381952006-06-26 00:28:01 -0700171static void md_new_event_inintr(mddev_t *mddev)
NeilBrownc331eb02006-05-30 21:27:13 -0700172{
173 atomic_inc(&md_event_count);
174 wake_up(&md_event_waiters);
175}
176
NeilBrownd7603b72006-01-06 00:20:30 -0800177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * Enables to iterate over all existing md arrays
179 * all_mddevs_lock protects this list.
180 */
181static LIST_HEAD(all_mddevs);
182static DEFINE_SPINLOCK(all_mddevs_lock);
183
184
185/*
186 * iterates through all used mddevs in the system.
187 * We take care to grab the all_mddevs_lock whenever navigating
188 * the list, and to always hold a refcount when unlocked.
189 * Any code which breaks out of this loop while own
190 * a reference to the current mddev and must mddev_put it.
191 */
NeilBrown29ac4aa2008-02-06 01:39:58 -0800192#define for_each_mddev(mddev,tmp) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 \
194 for (({ spin_lock(&all_mddevs_lock); \
195 tmp = all_mddevs.next; \
196 mddev = NULL;}); \
197 ({ if (tmp != &all_mddevs) \
198 mddev_get(list_entry(tmp, mddev_t, all_mddevs));\
199 spin_unlock(&all_mddevs_lock); \
200 if (mddev) mddev_put(mddev); \
201 mddev = list_entry(tmp, mddev_t, all_mddevs); \
202 tmp != &all_mddevs;}); \
203 ({ spin_lock(&all_mddevs_lock); \
204 tmp = tmp->next;}) \
205 )
206
207
NeilBrown409c57f2009-03-31 14:39:39 +1100208/* Rather than calling directly into the personality make_request function,
209 * IO requests come here first so that we can check if the device is
210 * being suspended pending a reconfiguration.
211 * We hold a refcount over the call to ->make_request. By the time that
212 * call has finished, the bio has been linked into some internal structure
213 * and so is visible to ->quiesce(), so we don't need the refcount any more.
214 */
215static int md_make_request(struct request_queue *q, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
NeilBrown409c57f2009-03-31 14:39:39 +1100217 mddev_t *mddev = q->queuedata;
218 int rv;
219 if (mddev == NULL || mddev->pers == NULL) {
220 bio_io_error(bio);
221 return 0;
222 }
223 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100224 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100225 DEFINE_WAIT(__wait);
226 for (;;) {
227 prepare_to_wait(&mddev->sb_wait, &__wait,
228 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100229 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100230 break;
231 rcu_read_unlock();
232 schedule();
233 rcu_read_lock();
234 }
235 finish_wait(&mddev->sb_wait, &__wait);
236 }
237 atomic_inc(&mddev->active_io);
238 rcu_read_unlock();
239 rv = mddev->pers->make_request(q, bio);
240 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
241 wake_up(&mddev->sb_wait);
242
243 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
NeilBrown409c57f2009-03-31 14:39:39 +1100246static void mddev_suspend(mddev_t *mddev)
247{
248 BUG_ON(mddev->suspended);
249 mddev->suspended = 1;
250 synchronize_rcu();
251 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
252 mddev->pers->quiesce(mddev, 1);
253 md_unregister_thread(mddev->thread);
254 mddev->thread = NULL;
255 /* we now know that no code is executing in the personality module,
256 * except possibly the tail end of a ->bi_end_io function, but that
257 * is certain to complete before the module has a chance to get
258 * unloaded
259 */
260}
261
262static void mddev_resume(mddev_t *mddev)
263{
264 mddev->suspended = 0;
265 wake_up(&mddev->sb_wait);
266 mddev->pers->quiesce(mddev, 0);
267}
268
NeilBrown3fa841d2009-09-23 18:10:29 +1000269int mddev_congested(mddev_t *mddev, int bits)
270{
NeilBrowna2826aa2009-12-14 12:49:49 +1100271 if (mddev->barrier)
272 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000273 return mddev->suspended;
274}
275EXPORT_SYMBOL(mddev_congested);
276
NeilBrowna2826aa2009-12-14 12:49:49 +1100277/*
278 * Generic barrier handling for md
279 */
280
281#define POST_REQUEST_BARRIER ((void*)1)
282
283static void md_end_barrier(struct bio *bio, int err)
284{
285 mdk_rdev_t *rdev = bio->bi_private;
286 mddev_t *mddev = rdev->mddev;
287 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
288 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
289
290 rdev_dec_pending(rdev, mddev);
291
292 if (atomic_dec_and_test(&mddev->flush_pending)) {
293 if (mddev->barrier == POST_REQUEST_BARRIER) {
294 /* This was a post-request barrier */
295 mddev->barrier = NULL;
296 wake_up(&mddev->sb_wait);
297 } else
298 /* The pre-request barrier has finished */
299 schedule_work(&mddev->barrier_work);
300 }
301 bio_put(bio);
302}
303
304static void submit_barriers(mddev_t *mddev)
305{
306 mdk_rdev_t *rdev;
307
308 rcu_read_lock();
309 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
310 if (rdev->raid_disk >= 0 &&
311 !test_bit(Faulty, &rdev->flags)) {
312 /* Take two references, one is dropped
313 * when request finishes, one after
314 * we reclaim rcu_read_lock
315 */
316 struct bio *bi;
317 atomic_inc(&rdev->nr_pending);
318 atomic_inc(&rdev->nr_pending);
319 rcu_read_unlock();
320 bi = bio_alloc(GFP_KERNEL, 0);
321 bi->bi_end_io = md_end_barrier;
322 bi->bi_private = rdev;
323 bi->bi_bdev = rdev->bdev;
324 atomic_inc(&mddev->flush_pending);
325 submit_bio(WRITE_BARRIER, bi);
326 rcu_read_lock();
327 rdev_dec_pending(rdev, mddev);
328 }
329 rcu_read_unlock();
330}
331
332static void md_submit_barrier(struct work_struct *ws)
333{
334 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
335 struct bio *bio = mddev->barrier;
336
337 atomic_set(&mddev->flush_pending, 1);
338
339 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
340 bio_endio(bio, -EOPNOTSUPP);
341 else if (bio->bi_size == 0)
342 /* an empty barrier - all done */
343 bio_endio(bio, 0);
344 else {
345 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
346 if (mddev->pers->make_request(mddev->queue, bio))
347 generic_make_request(bio);
348 mddev->barrier = POST_REQUEST_BARRIER;
349 submit_barriers(mddev);
350 }
351 if (atomic_dec_and_test(&mddev->flush_pending)) {
352 mddev->barrier = NULL;
353 wake_up(&mddev->sb_wait);
354 }
355}
356
357void md_barrier_request(mddev_t *mddev, struct bio *bio)
358{
359 spin_lock_irq(&mddev->write_lock);
360 wait_event_lock_irq(mddev->sb_wait,
361 !mddev->barrier,
362 mddev->write_lock, /*nothing*/);
363 mddev->barrier = bio;
364 spin_unlock_irq(&mddev->write_lock);
365
366 atomic_set(&mddev->flush_pending, 1);
367 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
368
369 submit_barriers(mddev);
370
371 if (atomic_dec_and_test(&mddev->flush_pending))
372 schedule_work(&mddev->barrier_work);
373}
374EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376static inline mddev_t *mddev_get(mddev_t *mddev)
377{
378 atomic_inc(&mddev->active);
379 return mddev;
380}
381
Dan Williams5fd3a172009-03-04 00:57:25 -0700382static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384static void mddev_put(mddev_t *mddev)
385{
386 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
387 return;
NeilBrownd3374822009-01-09 08:31:10 +1100388 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100389 mddev->ctime == 0 && !mddev->hold_active) {
390 /* Array is not configured at all, and not held active,
391 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100393 if (mddev->gendisk) {
394 /* we did a probe so need to clean up.
395 * Call schedule_work inside the spinlock
396 * so that flush_scheduled_work() after
397 * mddev_find will succeed in waiting for the
398 * work to be done.
399 */
400 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
401 schedule_work(&mddev->del_work);
402 } else
403 kfree(mddev);
404 }
405 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408static mddev_t * mddev_find(dev_t unit)
409{
410 mddev_t *mddev, *new = NULL;
411
412 retry:
413 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
NeilBrownefeb53c2009-01-09 08:31:10 +1100415 if (unit) {
416 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
417 if (mddev->unit == unit) {
418 mddev_get(mddev);
419 spin_unlock(&all_mddevs_lock);
420 kfree(new);
421 return mddev;
422 }
423
424 if (new) {
425 list_add(&new->all_mddevs, &all_mddevs);
426 spin_unlock(&all_mddevs_lock);
427 new->hold_active = UNTIL_IOCTL;
428 return new;
429 }
430 } else if (new) {
431 /* find an unused unit number */
432 static int next_minor = 512;
433 int start = next_minor;
434 int is_free = 0;
435 int dev = 0;
436 while (!is_free) {
437 dev = MKDEV(MD_MAJOR, next_minor);
438 next_minor++;
439 if (next_minor > MINORMASK)
440 next_minor = 0;
441 if (next_minor == start) {
442 /* Oh dear, all in use. */
443 spin_unlock(&all_mddevs_lock);
444 kfree(new);
445 return NULL;
446 }
447
448 is_free = 1;
449 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
450 if (mddev->unit == dev) {
451 is_free = 0;
452 break;
453 }
454 }
455 new->unit = dev;
456 new->md_minor = MINOR(dev);
457 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 list_add(&new->all_mddevs, &all_mddevs);
459 spin_unlock(&all_mddevs_lock);
460 return new;
461 }
462 spin_unlock(&all_mddevs_lock);
463
NeilBrown9ffae0c2006-01-06 00:20:32 -0800464 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!new)
466 return NULL;
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 new->unit = unit;
469 if (MAJOR(unit) == MD_MAJOR)
470 new->md_minor = MINOR(unit);
471 else
472 new->md_minor = MINOR(unit) >> MdpMinorShift;
473
NeilBrownc8c00a62009-08-10 12:50:52 +1000474 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800475 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100476 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 INIT_LIST_HEAD(&new->disks);
478 INIT_LIST_HEAD(&new->all_mddevs);
479 init_timer(&new->safemode_timer);
480 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000481 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100482 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700483 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100484 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700485 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700486 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700487 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000488 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800489 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700490 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto retry;
493}
494
495static inline int mddev_lock(mddev_t * mddev)
496{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800497 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Dan Williamsb522adc2009-03-31 15:00:31 +1100500static inline int mddev_is_locked(mddev_t *mddev)
501{
502 return mutex_is_locked(&mddev->reconfig_mutex);
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static inline int mddev_trylock(mddev_t * mddev)
506{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800507 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
NeilBrownb6eb1272010-04-15 10:13:47 +1000510static struct attribute_group md_redundancy_group;
511
NeilBrowna64c8762010-04-14 17:15:37 +1000512static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
NeilBrowna64c8762010-04-14 17:15:37 +1000514 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000515 /* These cannot be removed under reconfig_mutex as
516 * an access to the files will try to take reconfig_mutex
517 * while holding the file unremovable, which leads to
518 * a deadlock.
519 * So hold open_mutex instead - we are allowed to take
520 * it while holding reconfig_mutex, and md_run can
521 * use it to wait for the remove to complete.
522 */
NeilBrowna64c8762010-04-14 17:15:37 +1000523 struct attribute_group *to_remove = mddev->to_remove;
524 mddev->to_remove = NULL;
NeilBrownb6eb1272010-04-15 10:13:47 +1000525 mutex_lock(&mddev->open_mutex);
526 mutex_unlock(&mddev->reconfig_mutex);
527
NeilBrowna64c8762010-04-14 17:15:37 +1000528 if (to_remove != &md_redundancy_group)
529 sysfs_remove_group(&mddev->kobj, to_remove);
530 if (mddev->pers == NULL ||
531 mddev->pers->sync_request == NULL) {
532 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
533 if (mddev->sysfs_action)
534 sysfs_put(mddev->sysfs_action);
535 mddev->sysfs_action = NULL;
536 }
NeilBrownb6eb1272010-04-15 10:13:47 +1000537 mutex_unlock(&mddev->open_mutex);
538 } else
539 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
NeilBrown005eca52005-08-22 13:11:08 -0700541 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
NeilBrown2989ddb2006-01-06 00:20:43 -0800544static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100546 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100548 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (rdev->desc_nr == nr)
550 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return NULL;
553}
554
555static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
556{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 mdk_rdev_t *rdev;
558
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100559 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (rdev->bdev->bd_dev == dev)
561 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return NULL;
564}
565
NeilBrownd9d166c2006-01-06 00:20:51 -0800566static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800567{
568 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800569 list_for_each_entry(pers, &pers_list, list) {
570 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800571 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800572 if (strcmp(pers->name, clevel)==0)
573 return pers;
574 }
NeilBrown2604b702006-01-06 00:20:36 -0800575 return NULL;
576}
577
Andre Nollb73df2d2008-07-11 22:02:23 +1000578/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700579static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Andre Nollb73df2d2008-07-11 22:02:23 +1000581 sector_t num_sectors = bdev->bd_inode->i_size / 512;
582 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static int alloc_disk_sb(mdk_rdev_t * rdev)
586{
587 if (rdev->sb_page)
588 MD_BUG();
589
590 rdev->sb_page = alloc_page(GFP_KERNEL);
591 if (!rdev->sb_page) {
592 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000593 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 return 0;
597}
598
599static void free_disk_sb(mdk_rdev_t * rdev)
600{
601 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800602 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 rdev->sb_loaded = 0;
604 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000605 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100606 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608}
609
610
NeilBrown6712ecf2007-09-27 12:47:43 +0200611static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700612{
613 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800614 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700615
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700616 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
617 printk("md: super_written gets error=%d, uptodate=%d\n",
618 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
619 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800620 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700621 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700622
NeilBrowna9701a32005-11-08 21:39:34 -0800623 if (atomic_dec_and_test(&mddev->pending_writes))
624 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700625 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700626}
627
NeilBrown6712ecf2007-09-27 12:47:43 +0200628static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800629{
630 struct bio *bio2 = bio->bi_private;
631 mdk_rdev_t *rdev = bio2->bi_private;
632 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800633
634 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
635 error == -EOPNOTSUPP) {
636 unsigned long flags;
637 /* barriers don't appear to be supported :-( */
638 set_bit(BarriersNotsupp, &rdev->flags);
639 mddev->barriers_work = 0;
640 spin_lock_irqsave(&mddev->write_lock, flags);
641 bio2->bi_next = mddev->biolist;
642 mddev->biolist = bio2;
643 spin_unlock_irqrestore(&mddev->write_lock, flags);
644 wake_up(&mddev->sb_wait);
645 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200646 } else {
647 bio_put(bio2);
648 bio->bi_private = rdev;
649 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800650 }
NeilBrowna9701a32005-11-08 21:39:34 -0800651}
652
NeilBrown7bfa19f2005-06-21 17:17:28 -0700653void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
654 sector_t sector, int size, struct page *page)
655{
656 /* write first size bytes of page to sector of rdev
657 * Increment mddev->pending_writes before returning
658 * and decrement it on completion, waking up sb_wait
659 * if zero is reached.
660 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800661 *
662 * As we might need to resubmit the request if BIO_RW_BARRIER
663 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700664 */
665 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100666 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700667
668 bio->bi_bdev = rdev->bdev;
669 bio->bi_sector = sector;
670 bio_add_page(bio, page, size, 0);
671 bio->bi_private = rdev;
672 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800673 bio->bi_rw = rw;
674
NeilBrown7bfa19f2005-06-21 17:17:28 -0700675 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800676 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
677 struct bio *rbio;
678 rw |= (1<<BIO_RW_BARRIER);
679 rbio = bio_clone(bio, GFP_NOIO);
680 rbio->bi_private = bio;
681 rbio->bi_end_io = super_written_barrier;
682 submit_bio(rw, rbio);
683 } else
684 submit_bio(rw, bio);
685}
686
687void md_super_wait(mddev_t *mddev)
688{
689 /* wait for all superblock writes that were scheduled to complete.
690 * if any had to be retried (due to BARRIER problems), retry them
691 */
692 DEFINE_WAIT(wq);
693 for(;;) {
694 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
695 if (atomic_read(&mddev->pending_writes)==0)
696 break;
697 while (mddev->biolist) {
698 struct bio *bio;
699 spin_lock_irq(&mddev->write_lock);
700 bio = mddev->biolist;
701 mddev->biolist = bio->bi_next ;
702 bio->bi_next = NULL;
703 spin_unlock_irq(&mddev->write_lock);
704 submit_bio(bio->bi_rw, bio);
705 }
706 schedule();
707 }
708 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700709}
710
NeilBrown6712ecf2007-09-27 12:47:43 +0200711static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
NeilBrowna654b9d82005-06-21 17:17:27 -0700716int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct page *page, int rw)
718{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700719 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct completion event;
721 int ret;
722
Jens Axboe93dbb392009-02-16 10:25:40 +0100723 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 bio->bi_bdev = bdev;
726 bio->bi_sector = sector;
727 bio_add_page(bio, page, size, 0);
728 init_completion(&event);
729 bio->bi_private = &event;
730 bio->bi_end_io = bi_complete;
731 submit_bio(rw, bio);
732 wait_for_completion(&event);
733
734 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
735 bio_put(bio);
736 return ret;
737}
NeilBrowna8745db2006-01-06 00:20:34 -0800738EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
NeilBrown0002b272005-09-09 16:23:53 -0700740static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 char b[BDEVNAME_SIZE];
743 if (!rdev->sb_page) {
744 MD_BUG();
745 return -EINVAL;
746 }
747 if (rdev->sb_loaded)
748 return 0;
749
750
Andre Noll0f420352008-07-11 22:02:23 +1000751 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 goto fail;
753 rdev->sb_loaded = 1;
754 return 0;
755
756fail:
757 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
758 bdevname(rdev->bdev,b));
759 return -EINVAL;
760}
761
762static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
763{
Andre Noll05710462008-07-11 22:02:20 +1000764 return sb1->set_uuid0 == sb2->set_uuid0 &&
765 sb1->set_uuid1 == sb2->set_uuid1 &&
766 sb1->set_uuid2 == sb2->set_uuid2 &&
767 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
771{
772 int ret;
773 mdp_super_t *tmp1, *tmp2;
774
775 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
776 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
777
778 if (!tmp1 || !tmp2) {
779 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100780 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 goto abort;
782 }
783
784 *tmp1 = *sb1;
785 *tmp2 = *sb2;
786
787 /*
788 * nr_disks is not constant
789 */
790 tmp1->nr_disks = 0;
791 tmp2->nr_disks = 0;
792
Andre Nollce0c8e02008-07-11 22:02:20 +1000793 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700795 kfree(tmp1);
796 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return ret;
798}
799
NeilBrown4d167f02007-05-09 02:35:37 -0700800
801static u32 md_csum_fold(u32 csum)
802{
803 csum = (csum & 0xffff) + (csum >> 16);
804 return (csum & 0xffff) + (csum >> 16);
805}
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807static unsigned int calc_sb_csum(mdp_super_t * sb)
808{
NeilBrown4d167f02007-05-09 02:35:37 -0700809 u64 newcsum = 0;
810 u32 *sb32 = (u32*)sb;
811 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned int disk_csum, csum;
813
814 disk_csum = sb->sb_csum;
815 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700816
817 for (i = 0; i < MD_SB_BYTES/4 ; i++)
818 newcsum += sb32[i];
819 csum = (newcsum & 0xffffffff) + (newcsum>>32);
820
821
822#ifdef CONFIG_ALPHA
823 /* This used to use csum_partial, which was wrong for several
824 * reasons including that different results are returned on
825 * different architectures. It isn't critical that we get exactly
826 * the same return value as before (we always csum_fold before
827 * testing, and that removes any differences). However as we
828 * know that csum_partial always returned a 16bit value on
829 * alphas, do a fold to maximise conformity to previous behaviour.
830 */
831 sb->sb_csum = md_csum_fold(disk_csum);
832#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700834#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return csum;
836}
837
838
839/*
840 * Handle superblock details.
841 * We want to be able to handle multiple superblock formats
842 * so we have a common interface to them all, and an array of
843 * different handlers.
844 * We rely on user-space to write the initial superblock, and support
845 * reading and updating of superblocks.
846 * Interface methods are:
847 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
848 * loads and validates a superblock on dev.
849 * if refdev != NULL, compare superblocks on both devices
850 * Return:
851 * 0 - dev has a superblock that is compatible with refdev
852 * 1 - dev has a superblock that is compatible and newer than refdev
853 * so dev should be used as the refdev in future
854 * -EINVAL superblock incompatible or invalid
855 * -othererror e.g. -EIO
856 *
857 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
858 * Verify that dev is acceptable into mddev.
859 * The first time, mddev->raid_disks will be 0, and data from
860 * dev should be merged in. Subsequent calls check that dev
861 * is new enough. Return 0 or -EINVAL
862 *
863 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
864 * Update the superblock for rdev with data in mddev
865 * This does not write to disc.
866 *
867 */
868
869struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000870 char *name;
871 struct module *owner;
872 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
873 int minor_version);
874 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
875 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
876 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000877 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878};
879
880/*
Andre Noll0894cc32009-06-18 08:49:23 +1000881 * Check that the given mddev has no bitmap.
882 *
883 * This function is called from the run method of all personalities that do not
884 * support bitmaps. It prints an error message and returns non-zero if mddev
885 * has a bitmap. Otherwise, it returns 0.
886 *
887 */
888int md_check_no_bitmap(mddev_t *mddev)
889{
NeilBrownc3d97142009-12-14 12:49:52 +1100890 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000891 return 0;
892 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
893 mdname(mddev), mddev->pers->name);
894 return 1;
895}
896EXPORT_SYMBOL(md_check_no_bitmap);
897
898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 * load_super for 0.90.0
900 */
901static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
902{
903 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
904 mdp_super_t *sb;
905 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /*
Andre Noll0f420352008-07-11 22:02:23 +1000908 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * it's at the end of the disk.
910 *
911 * It also happens to be a multiple of 4Kb.
912 */
Andre Noll0f420352008-07-11 22:02:23 +1000913 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
NeilBrown0002b272005-09-09 16:23:53 -0700915 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (ret) return ret;
917
918 ret = -EINVAL;
919
920 bdevname(rdev->bdev, b);
921 sb = (mdp_super_t*)page_address(rdev->sb_page);
922
923 if (sb->md_magic != MD_SB_MAGIC) {
924 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
925 b);
926 goto abort;
927 }
928
929 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800930 sb->minor_version < 90 ||
931 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
933 sb->major_version, sb->minor_version,
934 b);
935 goto abort;
936 }
937
938 if (sb->raid_disks <= 0)
939 goto abort;
940
NeilBrown4d167f02007-05-09 02:35:37 -0700941 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
943 b);
944 goto abort;
945 }
946
947 rdev->preferred_minor = sb->md_minor;
948 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700949 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (sb->level == LEVEL_MULTIPATH)
952 rdev->desc_nr = -1;
953 else
954 rdev->desc_nr = sb->this_disk.number;
955
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700956 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700958 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 __u64 ev1, ev2;
960 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
961 if (!uuid_equal(refsb, sb)) {
962 printk(KERN_WARNING "md: %s has different UUID to %s\n",
963 b, bdevname(refdev->bdev,b2));
964 goto abort;
965 }
966 if (!sb_equal(refsb, sb)) {
967 printk(KERN_WARNING "md: %s has same UUID"
968 " but different superblock to %s\n",
969 b, bdevname(refdev->bdev, b2));
970 goto abort;
971 }
972 ev1 = md_event(sb);
973 ev2 = md_event(refsb);
974 if (ev1 > ev2)
975 ret = 1;
976 else
977 ret = 0;
978 }
NeilBrown8190e752009-06-18 08:48:58 +1000979 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Andre Nolldd8ac332009-03-31 14:33:13 +1100981 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800982 /* "this cannot possibly happen" ... */
983 ret = -EINVAL;
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 abort:
986 return ret;
987}
988
989/*
990 * validate_super for 0.90.0
991 */
992static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
993{
994 mdp_disk_t *desc;
995 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -0700996 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
NeilBrown41158c72005-06-21 17:17:25 -0700998 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -0800999 clear_bit(Faulty, &rdev->flags);
1000 clear_bit(In_sync, &rdev->flags);
1001 clear_bit(WriteMostly, &rdev->flags);
1002 clear_bit(BarriersNotsupp, &rdev->flags);
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 if (mddev->raid_disks == 0) {
1005 mddev->major_version = 0;
1006 mddev->minor_version = sb->minor_version;
1007 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001008 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001009 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 mddev->ctime = sb->ctime;
1011 mddev->utime = sb->utime;
1012 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001013 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 mddev->layout = sb->layout;
1015 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001016 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001017 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001018 mddev->bitmap_info.offset = 0;
1019 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
NeilBrownf6705572006-03-27 01:18:11 -08001021 if (mddev->minor_version >= 91) {
1022 mddev->reshape_position = sb->reshape_position;
1023 mddev->delta_disks = sb->delta_disks;
1024 mddev->new_level = sb->new_level;
1025 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001026 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001027 } else {
1028 mddev->reshape_position = MaxSector;
1029 mddev->delta_disks = 0;
1030 mddev->new_level = mddev->level;
1031 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001032 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001033 }
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (sb->state & (1<<MD_SB_CLEAN))
1036 mddev->recovery_cp = MaxSector;
1037 else {
1038 if (sb->events_hi == sb->cp_events_hi &&
1039 sb->events_lo == sb->cp_events_lo) {
1040 mddev->recovery_cp = sb->recovery_cp;
1041 } else
1042 mddev->recovery_cp = 0;
1043 }
1044
1045 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1046 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1047 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1048 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1049
1050 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001051
1052 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001053 mddev->bitmap_info.file == NULL)
1054 mddev->bitmap_info.offset =
1055 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001056
NeilBrown41158c72005-06-21 17:17:25 -07001057 } else if (mddev->pers == NULL) {
1058 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 ++ev1;
1060 if (ev1 < mddev->events)
1061 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001062 } else if (mddev->bitmap) {
1063 /* if adding to array with a bitmap, then we can accept an
1064 * older device ... but not too old.
1065 */
NeilBrown41158c72005-06-21 17:17:25 -07001066 if (ev1 < mddev->bitmap->events_cleared)
1067 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001068 } else {
1069 if (ev1 < mddev->events)
1070 /* just a hot-add of a new device, leave raid_disk at -1 */
1071 return 0;
1072 }
NeilBrown41158c72005-06-21 17:17:25 -07001073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 desc = sb->disks + rdev->desc_nr;
1076
1077 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001078 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001079 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1080 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001081 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001083 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1084 /* active but not in sync implies recovery up to
1085 * reshape position. We don't know exactly where
1086 * that is, so set to zero for now */
1087 if (mddev->minor_version >= 91) {
1088 rdev->recovery_offset = 0;
1089 rdev->raid_disk = desc->raid_disk;
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001092 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1093 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001094 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001095 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097}
1098
1099/*
1100 * sync_super for 0.90.0
1101 */
1102static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1103{
1104 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 mdk_rdev_t *rdev2;
1106 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 /* make rdev->sb match mddev data..
1110 *
1111 * 1/ zero out disks
1112 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1113 * 3/ any empty disks < next_spare become removed
1114 *
1115 * disks[0] gets initialised to REMOVED because
1116 * we cannot be sure from other fields if it has
1117 * been initialised or not.
1118 */
1119 int i;
1120 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1121
NeilBrown61181562005-09-09 16:24:02 -07001122 rdev->sb_size = MD_SB_BYTES;
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 sb = (mdp_super_t*)page_address(rdev->sb_page);
1125
1126 memset(sb, 0, sizeof(*sb));
1127
1128 sb->md_magic = MD_SB_MAGIC;
1129 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sb->patch_version = mddev->patch_version;
1131 sb->gvalid_words = 0; /* ignored */
1132 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1133 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1134 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1135 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1136
1137 sb->ctime = mddev->ctime;
1138 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001139 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 sb->raid_disks = mddev->raid_disks;
1141 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001142 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 sb->utime = mddev->utime;
1144 sb->state = 0;
1145 sb->events_hi = (mddev->events>>32);
1146 sb->events_lo = (u32)mddev->events;
1147
NeilBrownf6705572006-03-27 01:18:11 -08001148 if (mddev->reshape_position == MaxSector)
1149 sb->minor_version = 90;
1150 else {
1151 sb->minor_version = 91;
1152 sb->reshape_position = mddev->reshape_position;
1153 sb->new_level = mddev->new_level;
1154 sb->delta_disks = mddev->delta_disks;
1155 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001156 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001157 }
1158 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (mddev->in_sync)
1160 {
1161 sb->recovery_cp = mddev->recovery_cp;
1162 sb->cp_events_hi = (mddev->events>>32);
1163 sb->cp_events_lo = (u32)mddev->events;
1164 if (mddev->recovery_cp == MaxSector)
1165 sb->state = (1<< MD_SB_CLEAN);
1166 } else
1167 sb->recovery_cp = 0;
1168
1169 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001170 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
NeilBrownc3d97142009-12-14 12:49:52 +11001172 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001173 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001176 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001178 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001179 int is_active = test_bit(In_sync, &rdev2->flags);
1180
1181 if (rdev2->raid_disk >= 0 &&
1182 sb->minor_version >= 91)
1183 /* we have nowhere to store the recovery_offset,
1184 * but if it is not below the reshape_position,
1185 * we can piggy-back on that.
1186 */
1187 is_active = 1;
1188 if (rdev2->raid_disk < 0 ||
1189 test_bit(Faulty, &rdev2->flags))
1190 is_active = 0;
1191 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001192 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001194 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001195 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 d = &sb->disks[rdev2->desc_nr];
1197 nr_disks++;
1198 d->number = rdev2->desc_nr;
1199 d->major = MAJOR(rdev2->bdev->bd_dev);
1200 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001201 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 d->raid_disk = rdev2->raid_disk;
1203 else
1204 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001205 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001207 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001209 if (test_bit(In_sync, &rdev2->flags))
1210 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 active++;
1212 working++;
1213 } else {
1214 d->state = 0;
1215 spare++;
1216 working++;
1217 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001218 if (test_bit(WriteMostly, &rdev2->flags))
1219 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 /* now set the "removed" and "faulty" bits on any missing devices */
1222 for (i=0 ; i < mddev->raid_disks ; i++) {
1223 mdp_disk_t *d = &sb->disks[i];
1224 if (d->state == 0 && d->number == 0) {
1225 d->number = i;
1226 d->raid_disk = i;
1227 d->state = (1<<MD_DISK_REMOVED);
1228 d->state |= (1<<MD_DISK_FAULTY);
1229 failed++;
1230 }
1231 }
1232 sb->nr_disks = nr_disks;
1233 sb->active_disks = active;
1234 sb->working_disks = working;
1235 sb->failed_disks = failed;
1236 sb->spare_disks = spare;
1237
1238 sb->this_disk = sb->disks[rdev->desc_nr];
1239 sb->sb_csum = calc_sb_csum(sb);
1240}
1241
1242/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001243 * rdev_size_change for 0.90.0
1244 */
1245static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001246super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001247{
Andre Noll58c0fed2009-03-31 14:33:13 +11001248 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001249 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001250 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001251 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001252 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001253 if (!num_sectors || num_sectors > rdev->sb_start)
1254 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001255 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001256 rdev->sb_page);
1257 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001258 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001259}
1260
1261
1262/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * version 1 superblock
1264 */
1265
NeilBrown1c05b4b2006-10-21 10:24:08 -07001266static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001268 __le32 disk_csum;
1269 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 unsigned long long newcsum;
1271 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001272 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 int i;
1274
1275 disk_csum = sb->sb_csum;
1276 sb->sb_csum = 0;
1277 newcsum = 0;
1278 for (i=0; size>=4; size -= 4 )
1279 newcsum += le32_to_cpu(*isuper++);
1280
1281 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001282 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1285 sb->sb_csum = disk_csum;
1286 return cpu_to_le32(csum);
1287}
1288
1289static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1290{
1291 struct mdp_superblock_1 *sb;
1292 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001293 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001295 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /*
Andre Noll0f420352008-07-11 22:02:23 +10001298 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 * It is always aligned to a 4K boundary and
1300 * depeding on minor_version, it can be:
1301 * 0: At least 8K, but less than 12K, from end of device
1302 * 1: At start of device
1303 * 2: 4K from start of device.
1304 */
1305 switch(minor_version) {
1306 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001307 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1308 sb_start -= 8*2;
1309 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 break;
1311 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001312 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001315 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 break;
1317 default:
1318 return -EINVAL;
1319 }
Andre Noll0f420352008-07-11 22:02:23 +10001320 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
NeilBrown0002b272005-09-09 16:23:53 -07001322 /* superblock is rarely larger than 1K, but it can be larger,
1323 * and it is safe to read 4k, so we do that
1324 */
1325 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 if (ret) return ret;
1327
1328
1329 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1330
1331 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1332 sb->major_version != cpu_to_le32(1) ||
1333 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001334 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001335 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return -EINVAL;
1337
1338 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1339 printk("md: invalid superblock checksum on %s\n",
1340 bdevname(rdev->bdev,b));
1341 return -EINVAL;
1342 }
1343 if (le64_to_cpu(sb->data_size) < 10) {
1344 printk("md: data_size too small on %s\n",
1345 bdevname(rdev->bdev,b));
1346 return -EINVAL;
1347 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 rdev->preferred_minor = 0xffff;
1350 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001351 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
NeilBrown0002b272005-09-09 16:23:53 -07001353 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001354 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001355 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001356 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1357
1358 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001359 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001360 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001361
NeilBrown31b65a02006-07-10 04:44:14 -07001362 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1363 rdev->desc_nr = -1;
1364 else
1365 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1366
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001367 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001368 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001369 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 __u64 ev1, ev2;
1371 struct mdp_superblock_1 *refsb =
1372 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1373
1374 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1375 sb->level != refsb->level ||
1376 sb->layout != refsb->layout ||
1377 sb->chunksize != refsb->chunksize) {
1378 printk(KERN_WARNING "md: %s has strangely different"
1379 " superblock to %s\n",
1380 bdevname(rdev->bdev,b),
1381 bdevname(refdev->bdev,b2));
1382 return -EINVAL;
1383 }
1384 ev1 = le64_to_cpu(sb->events);
1385 ev2 = le64_to_cpu(refsb->events);
1386
1387 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001388 ret = 1;
1389 else
1390 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
NeilBrowna1801f82008-03-04 14:29:31 -08001392 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001393 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1394 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001396 rdev->sectors = rdev->sb_start;
1397 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001399 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001400 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001401 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001402 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404
1405static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1406{
1407 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001408 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
NeilBrown41158c72005-06-21 17:17:25 -07001410 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001411 clear_bit(Faulty, &rdev->flags);
1412 clear_bit(In_sync, &rdev->flags);
1413 clear_bit(WriteMostly, &rdev->flags);
1414 clear_bit(BarriersNotsupp, &rdev->flags);
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (mddev->raid_disks == 0) {
1417 mddev->major_version = 1;
1418 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001419 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001420 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1422 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1423 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001424 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 mddev->layout = le32_to_cpu(sb->layout);
1426 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001427 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001428 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001429 mddev->bitmap_info.offset = 0;
1430 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1433 memcpy(mddev->uuid, sb->set_uuid, 16);
1434
1435 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001436
NeilBrown71c08052005-09-09 16:23:51 -07001437 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001438 mddev->bitmap_info.file == NULL )
1439 mddev->bitmap_info.offset =
1440 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001441
NeilBrownf6705572006-03-27 01:18:11 -08001442 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1443 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1444 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1445 mddev->new_level = le32_to_cpu(sb->new_level);
1446 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001447 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001448 } else {
1449 mddev->reshape_position = MaxSector;
1450 mddev->delta_disks = 0;
1451 mddev->new_level = mddev->level;
1452 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001453 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001454 }
1455
NeilBrown41158c72005-06-21 17:17:25 -07001456 } else if (mddev->pers == NULL) {
1457 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 ++ev1;
1459 if (ev1 < mddev->events)
1460 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001461 } else if (mddev->bitmap) {
1462 /* If adding to array with a bitmap, then we can accept an
1463 * older device, but not too old.
1464 */
NeilBrown41158c72005-06-21 17:17:25 -07001465 if (ev1 < mddev->bitmap->events_cleared)
1466 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001467 } else {
1468 if (ev1 < mddev->events)
1469 /* just a hot-add of a new device, leave raid_disk at -1 */
1470 return 0;
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 if (mddev->level != LEVEL_MULTIPATH) {
1473 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001474 if (rdev->desc_nr < 0 ||
1475 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1476 role = 0xffff;
1477 rdev->desc_nr = -1;
1478 } else
1479 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 switch(role) {
1481 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 break;
1483 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001484 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 break;
1486 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001487 if ((le32_to_cpu(sb->feature_map) &
1488 MD_FEATURE_RECOVERY_OFFSET))
1489 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1490 else
1491 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 rdev->raid_disk = role;
1493 break;
1494 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001495 if (sb->devflags & WriteMostly1)
1496 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001497 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001498 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return 0;
1501}
1502
1503static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1504{
1505 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 mdk_rdev_t *rdev2;
1507 int max_dev, i;
1508 /* make rdev->sb match mddev and rdev data. */
1509
1510 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1511
1512 sb->feature_map = 0;
1513 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001514 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 memset(sb->pad1, 0, sizeof(sb->pad1));
1516 memset(sb->pad2, 0, sizeof(sb->pad2));
1517 memset(sb->pad3, 0, sizeof(sb->pad3));
1518
1519 sb->utime = cpu_to_le64((__u64)mddev->utime);
1520 sb->events = cpu_to_le64(mddev->events);
1521 if (mddev->in_sync)
1522 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1523 else
1524 sb->resync_offset = cpu_to_le64(0);
1525
NeilBrown1c05b4b2006-10-21 10:24:08 -07001526 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001527
NeilBrownf0ca3402006-02-02 14:28:04 -08001528 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001529 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001530 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001531 sb->level = cpu_to_le32(mddev->level);
1532 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001533
NeilBrownc3d97142009-12-14 12:49:52 +11001534 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1535 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001536 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001537 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001538
1539 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001540 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001541 sb->feature_map |=
1542 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1543 sb->recovery_offset =
1544 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001545 }
1546
NeilBrownf6705572006-03-27 01:18:11 -08001547 if (mddev->reshape_position != MaxSector) {
1548 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1549 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1550 sb->new_layout = cpu_to_le32(mddev->new_layout);
1551 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1552 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001553 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001554 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001557 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (rdev2->desc_nr+1 > max_dev)
1559 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001560
NeilBrown70471da2009-08-03 10:59:57 +10001561 if (max_dev > le32_to_cpu(sb->max_dev)) {
1562 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001563 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001564 rdev->sb_size = max_dev * 2 + 256;
1565 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1566 if (rdev->sb_size & bmask)
1567 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 for (i=0; i<max_dev;i++)
1570 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1571
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001572 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001574 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001576 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001578 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001579 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 else
1581 sb->dev_roles[i] = cpu_to_le16(0xffff);
1582 }
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 sb->sb_csum = calc_sb_1_csum(sb);
1585}
1586
Chris Webb0cd17fe2008-06-28 08:31:46 +10001587static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001588super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001589{
1590 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001591 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001592 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001593 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001594 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001595 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001596 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1597 max_sectors -= rdev->data_offset;
1598 if (!num_sectors || num_sectors > max_sectors)
1599 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001600 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001601 /* minor version 0 with bitmap we can't move */
1602 return 0;
1603 } else {
1604 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001605 sector_t sb_start;
1606 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1607 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001608 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001609 if (!num_sectors || num_sectors > max_sectors)
1610 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001611 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001612 }
1613 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001614 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001615 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001616 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001617 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001618 rdev->sb_page);
1619 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001620 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Adrian Bunk75c96f82005-05-05 16:16:09 -07001623static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 [0] = {
1625 .name = "0.90.0",
1626 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001627 .load_super = super_90_load,
1628 .validate_super = super_90_validate,
1629 .sync_super = super_90_sync,
1630 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 },
1632 [1] = {
1633 .name = "md-1",
1634 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001635 .load_super = super_1_load,
1636 .validate_super = super_1_validate,
1637 .sync_super = super_1_sync,
1638 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 },
1640};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1643{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001644 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
NeilBrown4b809912008-07-21 17:05:25 +10001646 rcu_read_lock();
1647 rdev_for_each_rcu(rdev, mddev1)
1648 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001649 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001650 rdev2->bdev->bd_contains) {
1651 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001652 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001653 }
1654 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return 0;
1656}
1657
1658static LIST_HEAD(pending_raid_disks);
1659
Andre Nollac5e7112009-08-03 10:59:47 +10001660/*
1661 * Try to register data integrity profile for an mddev
1662 *
1663 * This is called when an array is started and after a disk has been kicked
1664 * from the array. It only succeeds if all working and active component devices
1665 * are integrity capable with matching profiles.
1666 */
1667int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001668{
Andre Nollac5e7112009-08-03 10:59:47 +10001669 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001670
Andre Nollac5e7112009-08-03 10:59:47 +10001671 if (list_empty(&mddev->disks))
1672 return 0; /* nothing to do */
1673 if (blk_get_integrity(mddev->gendisk))
1674 return 0; /* already registered */
1675 list_for_each_entry(rdev, &mddev->disks, same_set) {
1676 /* skip spares and non-functional disks */
1677 if (test_bit(Faulty, &rdev->flags))
1678 continue;
1679 if (rdev->raid_disk < 0)
1680 continue;
1681 /*
1682 * If at least one rdev is not integrity capable, we can not
1683 * enable data integrity for the md device.
1684 */
1685 if (!bdev_get_integrity(rdev->bdev))
1686 return -EINVAL;
1687 if (!reference) {
1688 /* Use the first rdev as the reference */
1689 reference = rdev;
1690 continue;
1691 }
1692 /* does this rdev's profile match the reference profile? */
1693 if (blk_integrity_compare(reference->bdev->bd_disk,
1694 rdev->bdev->bd_disk) < 0)
1695 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001696 }
Andre Nollac5e7112009-08-03 10:59:47 +10001697 /*
1698 * All component devices are integrity capable and have matching
1699 * profiles, register the common profile for the md device.
1700 */
1701 if (blk_integrity_register(mddev->gendisk,
1702 bdev_get_integrity(reference->bdev)) != 0) {
1703 printk(KERN_ERR "md: failed to register integrity for %s\n",
1704 mdname(mddev));
1705 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001706 }
Andre Nollac5e7112009-08-03 10:59:47 +10001707 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1708 mdname(mddev));
1709 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001710}
Andre Nollac5e7112009-08-03 10:59:47 +10001711EXPORT_SYMBOL(md_integrity_register);
1712
1713/* Disable data integrity if non-capable/non-matching disk is being added */
1714void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1715{
1716 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1717 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1718
1719 if (!bi_mddev) /* nothing to do */
1720 return;
1721 if (rdev->raid_disk < 0) /* skip spares */
1722 return;
1723 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1724 rdev->bdev->bd_disk) >= 0)
1725 return;
1726 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1727 blk_integrity_unregister(mddev->gendisk);
1728}
1729EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1732{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001733 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001734 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001735 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001736 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 if (rdev->mddev) {
1739 MD_BUG();
1740 return -EINVAL;
1741 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001742
1743 /* prevent duplicates */
1744 if (find_rdev(mddev, rdev->bdev->bd_dev))
1745 return -EEXIST;
1746
Andre Nolldd8ac332009-03-31 14:33:13 +11001747 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1748 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1749 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001750 if (mddev->pers) {
1751 /* Cannot change size, so fail
1752 * If mddev->level <= 0, then we don't care
1753 * about aligning sizes (e.g. linear)
1754 */
1755 if (mddev->level > 0)
1756 return -ENOSPC;
1757 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001758 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /* Verify rdev->desc_nr is unique.
1762 * If it is -1, assign a free number, else
1763 * check number is not in use
1764 */
1765 if (rdev->desc_nr < 0) {
1766 int choice = 0;
1767 if (mddev->pers) choice = mddev->raid_disks;
1768 while (find_rdev_nr(mddev, choice))
1769 choice++;
1770 rdev->desc_nr = choice;
1771 } else {
1772 if (find_rdev_nr(mddev, rdev->desc_nr))
1773 return -EBUSY;
1774 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001775 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1776 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1777 mdname(mddev), mddev->max_disks);
1778 return -EBUSY;
1779 }
NeilBrown19133a42005-11-08 21:39:35 -08001780 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001781 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001782 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001785 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001786
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001787 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001788 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001789
Tejun Heo0762b8b2008-08-25 19:56:12 +09001790 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001791 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1792 kobject_del(&rdev->kobj);
1793 goto fail;
1794 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001795 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1796
NeilBrown4b809912008-07-21 17:05:25 +10001797 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001798 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001799
1800 /* May as well allow recovery to be retried once */
1801 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001804
1805 fail:
1806 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1807 b, mdname(mddev));
1808 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
NeilBrown177a99b2008-02-06 01:39:56 -08001811static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001812{
1813 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1814 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001815 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1819{
1820 char b[BDEVNAME_SIZE];
1821 if (!rdev->mddev) {
1822 MD_BUG();
1823 return;
1824 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001825 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001826 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1828 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001829 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001830 sysfs_put(rdev->sysfs_state);
1831 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001832 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001833 * writing to 'remove' to "dev/state". We also need
1834 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001835 */
NeilBrown4b809912008-07-21 17:05:25 +10001836 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001837 INIT_WORK(&rdev->del_work, md_delayed_delete);
1838 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001839 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
1842/*
1843 * prevent the device from being mounted, repartitioned or
1844 * otherwise reused by a RAID array (or any other kernel
1845 * subsystem), by bd_claiming the device.
1846 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001847static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 int err = 0;
1850 struct block_device *bdev;
1851 char b[BDEVNAME_SIZE];
1852
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001853 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (IS_ERR(bdev)) {
1855 printk(KERN_ERR "md: could not open %s.\n",
1856 __bdevname(dev, b));
1857 return PTR_ERR(bdev);
1858 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001859 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 if (err) {
1861 printk(KERN_ERR "md: could not bd_claim %s.\n",
1862 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001863 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return err;
1865 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001866 if (!shared)
1867 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 rdev->bdev = bdev;
1869 return err;
1870}
1871
1872static void unlock_rdev(mdk_rdev_t *rdev)
1873{
1874 struct block_device *bdev = rdev->bdev;
1875 rdev->bdev = NULL;
1876 if (!bdev)
1877 MD_BUG();
1878 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001879 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
1882void md_autodetect_dev(dev_t dev);
1883
1884static void export_rdev(mdk_rdev_t * rdev)
1885{
1886 char b[BDEVNAME_SIZE];
1887 printk(KERN_INFO "md: export_rdev(%s)\n",
1888 bdevname(rdev->bdev,b));
1889 if (rdev->mddev)
1890 MD_BUG();
1891 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001893 if (test_bit(AutoDetected, &rdev->flags))
1894 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895#endif
1896 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001897 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
1900static void kick_rdev_from_array(mdk_rdev_t * rdev)
1901{
1902 unbind_rdev_from_array(rdev);
1903 export_rdev(rdev);
1904}
1905
1906static void export_array(mddev_t *mddev)
1907{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001908 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
NeilBrownd089c6a2008-02-06 01:39:59 -08001910 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (!rdev->mddev) {
1912 MD_BUG();
1913 continue;
1914 }
1915 kick_rdev_from_array(rdev);
1916 }
1917 if (!list_empty(&mddev->disks))
1918 MD_BUG();
1919 mddev->raid_disks = 0;
1920 mddev->major_version = 0;
1921}
1922
1923static void print_desc(mdp_disk_t *desc)
1924{
1925 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1926 desc->major,desc->minor,desc->raid_disk,desc->state);
1927}
1928
Cheng Renquancd2ac932009-01-09 08:31:08 +11001929static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
1931 int i;
1932
1933 printk(KERN_INFO
1934 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1935 sb->major_version, sb->minor_version, sb->patch_version,
1936 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1937 sb->ctime);
1938 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1939 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1940 sb->md_minor, sb->layout, sb->chunk_size);
1941 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1942 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1943 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1944 sb->failed_disks, sb->spare_disks,
1945 sb->sb_csum, (unsigned long)sb->events_lo);
1946
1947 printk(KERN_INFO);
1948 for (i = 0; i < MD_SB_DISKS; i++) {
1949 mdp_disk_t *desc;
1950
1951 desc = sb->disks + i;
1952 if (desc->number || desc->major || desc->minor ||
1953 desc->raid_disk || (desc->state && (desc->state != 4))) {
1954 printk(" D %2d: ", i);
1955 print_desc(desc);
1956 }
1957 }
1958 printk(KERN_INFO "md: THIS: ");
1959 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
Cheng Renquancd2ac932009-01-09 08:31:08 +11001962static void print_sb_1(struct mdp_superblock_1 *sb)
1963{
1964 __u8 *uuid;
1965
1966 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001967 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001968 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001969 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001970 le32_to_cpu(sb->major_version),
1971 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001972 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001973 sb->set_name,
1974 (unsigned long long)le64_to_cpu(sb->ctime)
1975 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1976
1977 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001978 printk(KERN_INFO
1979 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001980 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001981 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001982 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1983 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001984 le32_to_cpu(sb->level),
1985 (unsigned long long)le64_to_cpu(sb->size),
1986 le32_to_cpu(sb->raid_disks),
1987 le32_to_cpu(sb->layout),
1988 le32_to_cpu(sb->chunksize),
1989 (unsigned long long)le64_to_cpu(sb->data_offset),
1990 (unsigned long long)le64_to_cpu(sb->data_size),
1991 (unsigned long long)le64_to_cpu(sb->super_offset),
1992 (unsigned long long)le64_to_cpu(sb->recovery_offset),
1993 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001994 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001995 sb->devflags,
1996 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
1997 (unsigned long long)le64_to_cpu(sb->events),
1998 (unsigned long long)le64_to_cpu(sb->resync_offset),
1999 le32_to_cpu(sb->sb_csum),
2000 le32_to_cpu(sb->max_dev)
2001 );
2002}
2003
2004static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
2006 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002007 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2008 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002009 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2010 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002012 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2013 switch (major_version) {
2014 case 0:
2015 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2016 break;
2017 case 1:
2018 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2019 break;
2020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 } else
2022 printk(KERN_INFO "md: no rdev superblock!\n");
2023}
2024
Adrian Bunk5e563412006-06-26 00:27:42 -07002025static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002027 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 mdk_rdev_t *rdev;
2029 mddev_t *mddev;
2030 char b[BDEVNAME_SIZE];
2031
2032 printk("\n");
2033 printk("md: **********************************\n");
2034 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2035 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002036 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
NeilBrown32a76272005-06-21 17:17:14 -07002038 if (mddev->bitmap)
2039 bitmap_print_sb(mddev->bitmap);
2040 else
2041 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002042 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 printk("<%s>", bdevname(rdev->bdev,b));
2044 printk("\n");
2045
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002046 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002047 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049 printk("md: **********************************\n");
2050 printk("\n");
2051}
2052
2053
NeilBrown42543762006-06-26 00:27:57 -07002054static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055{
NeilBrown42543762006-06-26 00:27:57 -07002056 /* Update each superblock (in-memory image), but
2057 * if we are allowed to, skip spares which already
2058 * have the right event counter, or have one earlier
2059 * (which would mean they aren't being marked as dirty
2060 * with the rest of the array)
2061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
NeilBrown5e865102009-11-12 12:08:04 +11002064 /* First make sure individual recovery_offsets are correct */
2065 list_for_each_entry(rdev, &mddev->disks, same_set) {
2066 if (rdev->raid_disk >= 0 &&
2067 !test_bit(In_sync, &rdev->flags) &&
2068 mddev->curr_resync_completed > rdev->recovery_offset)
2069 rdev->recovery_offset = mddev->curr_resync_completed;
2070
2071 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002072 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002073 if (rdev->sb_events == mddev->events ||
2074 (nospares &&
2075 rdev->raid_disk < 0 &&
2076 (rdev->sb_events&1)==0 &&
2077 rdev->sb_events+1 == mddev->events)) {
2078 /* Don't update this superblock */
2079 rdev->sb_loaded = 2;
2080 } else {
2081 super_types[mddev->major_version].
2082 sync_super(mddev, rdev);
2083 rdev->sb_loaded = 1;
2084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
2086}
2087
NeilBrown850b2b42006-10-03 01:15:46 -07002088static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002091 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002092 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
NeilBrown1b57f132009-06-18 08:48:19 +10002094 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002095 if (mddev->external)
2096 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002098 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002099
NeilBrown850b2b42006-10-03 01:15:46 -07002100 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2101 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2102 force_change = 1;
2103 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2104 /* just a clean<-> dirty transition, possibly leave spares alone,
2105 * though if events isn't the right even/odd, we will have to do
2106 * spares after all
2107 */
2108 nospares = 1;
2109 if (force_change)
2110 nospares = 0;
2111 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002112 /* If the array is degraded, then skipping spares is both
2113 * dangerous and fairly pointless.
2114 * Dangerous because a device that was removed from the array
2115 * might have a event_count that still looks up-to-date,
2116 * so it can be re-added without a resync.
2117 * Pointless because if there are any spares to skip,
2118 * then a recovery will happen and soon that array won't
2119 * be degraded any more and the spare can go back to sleep then.
2120 */
NeilBrown850b2b42006-10-03 01:15:46 -07002121 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002122
NeilBrown06d91a52005-06-21 17:17:12 -07002123 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002124
2125 /* If this is just a dirty<->clean transition, and the array is clean
2126 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b42006-10-03 01:15:46 -07002127 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002128 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002129 && (mddev->events & 1)
2130 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002131 mddev->events--;
2132 else {
2133 /* otherwise we have to go forward and ... */
2134 mddev->events ++;
2135 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002136 /* .. if the array isn't clean, an 'even' event must also go
2137 * to spares. */
2138 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002139 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002140 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002141 /* otherwise an 'odd' event must go to spares */
2142 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002143 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002144 }
2145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 if (!mddev->events) {
2148 /*
2149 * oops, this 64-bit counter should never wrap.
2150 * Either we are in around ~1 trillion A.C., assuming
2151 * 1 reboot per second, or we have a bug:
2152 */
2153 MD_BUG();
2154 mddev->events --;
2155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 /*
2158 * do not write anything to disk if using
2159 * nonpersistent superblocks
2160 */
NeilBrown06d91a52005-06-21 17:17:12 -07002161 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002162 if (!mddev->external)
2163 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2164
NeilBrowna9701a32005-11-08 21:39:34 -08002165 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002166 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002168 }
NeilBrowne6910632008-02-06 01:39:51 -08002169 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002170 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 dprintk(KERN_INFO
2173 "md: updating %s RAID superblock on device (in sync %d)\n",
2174 mdname(mddev),mddev->in_sync);
2175
NeilBrown4ad13662007-07-17 04:06:13 -07002176 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002177 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 char b[BDEVNAME_SIZE];
2179 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002180 if (rdev->sb_loaded != 1)
2181 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002182 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 dprintk("(skipping faulty ");
2184
2185 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002186 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002187 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002188 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002189 rdev->sb_page);
2190 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2191 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002192 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002193 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 } else
2196 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002197 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /* only need to write one superblock... */
2199 break;
2200 }
NeilBrowna9701a32005-11-08 21:39:34 -08002201 md_super_wait(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07002202 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002203
NeilBrowna9701a32005-11-08 21:39:34 -08002204 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b42006-10-03 01:15:46 -07002205 if (mddev->in_sync != sync_req ||
2206 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002207 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002208 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002209 goto repeat;
2210 }
NeilBrown850b2b42006-10-03 01:15:46 -07002211 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002212 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002213 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002214 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2215 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Andre Noll7f6ce762008-03-23 18:34:54 +01002219/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002220 * We want to accept with case. For this we use cmd_match.
2221 */
2222static int cmd_match(const char *cmd, const char *str)
2223{
2224 /* See if cmd, written into a sysfs file, matches
2225 * str. They must either be the same, or cmd can
2226 * have a trailing newline
2227 */
2228 while (*cmd && *str && *cmd == *str) {
2229 cmd++;
2230 str++;
2231 }
2232 if (*cmd == '\n')
2233 cmd++;
2234 if (*str || *cmd)
2235 return 0;
2236 return 1;
2237}
2238
NeilBrown86e6ffd2005-11-08 21:39:24 -08002239struct rdev_sysfs_entry {
2240 struct attribute attr;
2241 ssize_t (*show)(mdk_rdev_t *, char *);
2242 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2243};
2244
2245static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002246state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002247{
2248 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002249 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002250
NeilBrownb2d444d2005-11-08 21:39:31 -08002251 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002252 len+= sprintf(page+len, "%sfaulty",sep);
2253 sep = ",";
2254 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002255 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002256 len += sprintf(page+len, "%sin_sync",sep);
2257 sep = ",";
2258 }
NeilBrownf6556752006-06-26 00:28:01 -07002259 if (test_bit(WriteMostly, &rdev->flags)) {
2260 len += sprintf(page+len, "%swrite_mostly",sep);
2261 sep = ",";
2262 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002263 if (test_bit(Blocked, &rdev->flags)) {
2264 len += sprintf(page+len, "%sblocked", sep);
2265 sep = ",";
2266 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002267 if (!test_bit(Faulty, &rdev->flags) &&
2268 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002269 len += sprintf(page+len, "%sspare", sep);
2270 sep = ",";
2271 }
2272 return len+sprintf(page+len, "\n");
2273}
2274
NeilBrown45dc2de2006-06-26 00:27:58 -07002275static ssize_t
2276state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2277{
2278 /* can write
2279 * faulty - simulates and error
2280 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002281 * writemostly - sets write_mostly
2282 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002283 * blocked - sets the Blocked flag
2284 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002285 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002286 */
2287 int err = -EINVAL;
2288 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2289 md_error(rdev->mddev, rdev);
2290 err = 0;
2291 } else if (cmd_match(buf, "remove")) {
2292 if (rdev->raid_disk >= 0)
2293 err = -EBUSY;
2294 else {
2295 mddev_t *mddev = rdev->mddev;
2296 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002297 if (mddev->pers)
2298 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002299 md_new_event(mddev);
2300 err = 0;
2301 }
NeilBrownf6556752006-06-26 00:28:01 -07002302 } else if (cmd_match(buf, "writemostly")) {
2303 set_bit(WriteMostly, &rdev->flags);
2304 err = 0;
2305 } else if (cmd_match(buf, "-writemostly")) {
2306 clear_bit(WriteMostly, &rdev->flags);
2307 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002308 } else if (cmd_match(buf, "blocked")) {
2309 set_bit(Blocked, &rdev->flags);
2310 err = 0;
2311 } else if (cmd_match(buf, "-blocked")) {
2312 clear_bit(Blocked, &rdev->flags);
2313 wake_up(&rdev->blocked_wait);
2314 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2315 md_wakeup_thread(rdev->mddev->thread);
2316
2317 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002318 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2319 set_bit(In_sync, &rdev->flags);
2320 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002321 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002322 if (!err && rdev->sysfs_state)
2323 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002324 return err ? err : len;
2325}
NeilBrown80ca3a42006-07-10 04:44:18 -07002326static struct rdev_sysfs_entry rdev_state =
2327__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002328
2329static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002330errors_show(mdk_rdev_t *rdev, char *page)
2331{
2332 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2333}
2334
2335static ssize_t
2336errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2337{
2338 char *e;
2339 unsigned long n = simple_strtoul(buf, &e, 10);
2340 if (*buf && (*e == 0 || *e == '\n')) {
2341 atomic_set(&rdev->corrected_errors, n);
2342 return len;
2343 }
2344 return -EINVAL;
2345}
2346static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002347__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002348
NeilBrown014236d2006-01-06 00:20:55 -08002349static ssize_t
2350slot_show(mdk_rdev_t *rdev, char *page)
2351{
2352 if (rdev->raid_disk < 0)
2353 return sprintf(page, "none\n");
2354 else
2355 return sprintf(page, "%d\n", rdev->raid_disk);
2356}
2357
2358static ssize_t
2359slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2360{
2361 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002362 int err;
2363 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002364 int slot = simple_strtoul(buf, &e, 10);
2365 if (strncmp(buf, "none", 4)==0)
2366 slot = -1;
2367 else if (e==buf || (*e && *e!= '\n'))
2368 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002369 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002370 /* Setting 'slot' on an active array requires also
2371 * updating the 'rd%d' link, and communicating
2372 * with the personality with ->hot_*_disk.
2373 * For now we only support removing
2374 * failed/spare devices. This normally happens automatically,
2375 * but not when the metadata is externally managed.
2376 */
NeilBrownc303da62008-02-06 01:39:51 -08002377 if (rdev->raid_disk == -1)
2378 return -EEXIST;
2379 /* personality does all needed checks */
2380 if (rdev->mddev->pers->hot_add_disk == NULL)
2381 return -EINVAL;
2382 err = rdev->mddev->pers->
2383 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2384 if (err)
2385 return err;
2386 sprintf(nm, "rd%d", rdev->raid_disk);
2387 sysfs_remove_link(&rdev->mddev->kobj, nm);
2388 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2389 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002390 } else if (rdev->mddev->pers) {
2391 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002392 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002393 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002394 */
2395
2396 if (rdev->raid_disk != -1)
2397 return -EBUSY;
2398
2399 if (rdev->mddev->pers->hot_add_disk == NULL)
2400 return -EINVAL;
2401
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002402 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002403 if (rdev2->raid_disk == slot)
2404 return -EEXIST;
2405
2406 rdev->raid_disk = slot;
2407 if (test_bit(In_sync, &rdev->flags))
2408 rdev->saved_raid_disk = slot;
2409 else
2410 rdev->saved_raid_disk = -1;
2411 err = rdev->mddev->pers->
2412 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002413 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002414 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002415 return err;
Neil Brown52664732008-06-28 08:31:44 +10002416 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002417 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002418 sprintf(nm, "rd%d", rdev->raid_disk);
2419 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2420 printk(KERN_WARNING
2421 "md: cannot register "
2422 "%s for %s\n",
2423 nm, mdname(rdev->mddev));
2424
2425 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002426 } else {
2427 if (slot >= rdev->mddev->raid_disks)
2428 return -ENOSPC;
2429 rdev->raid_disk = slot;
2430 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002431 clear_bit(Faulty, &rdev->flags);
2432 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002433 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002434 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002435 }
NeilBrown014236d2006-01-06 00:20:55 -08002436 return len;
2437}
2438
2439
2440static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002441__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002442
NeilBrown93c8cad2006-01-06 00:20:56 -08002443static ssize_t
2444offset_show(mdk_rdev_t *rdev, char *page)
2445{
Andrew Morton6961ece2006-01-06 00:20:59 -08002446 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002447}
2448
2449static ssize_t
2450offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2451{
2452 char *e;
2453 unsigned long long offset = simple_strtoull(buf, &e, 10);
2454 if (e==buf || (*e && *e != '\n'))
2455 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002456 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002457 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002458 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002459 /* Must set offset before size, so overlap checks
2460 * can be sane */
2461 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002462 rdev->data_offset = offset;
2463 return len;
2464}
2465
2466static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002467__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002468
NeilBrown83303b62006-01-06 00:21:06 -08002469static ssize_t
2470rdev_size_show(mdk_rdev_t *rdev, char *page)
2471{
Andre Nolldd8ac332009-03-31 14:33:13 +11002472 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002473}
2474
NeilBrownc5d79ad2008-02-06 01:39:54 -08002475static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2476{
2477 /* check if two start/length pairs overlap */
2478 if (s1+l1 <= s2)
2479 return 0;
2480 if (s2+l2 <= s1)
2481 return 0;
2482 return 1;
2483}
2484
Dan Williamsb522adc2009-03-31 15:00:31 +11002485static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2486{
2487 unsigned long long blocks;
2488 sector_t new;
2489
2490 if (strict_strtoull(buf, 10, &blocks) < 0)
2491 return -EINVAL;
2492
2493 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2494 return -EINVAL; /* sector conversion overflow */
2495
2496 new = blocks * 2;
2497 if (new != blocks * 2)
2498 return -EINVAL; /* unsigned long long to sector_t overflow */
2499
2500 *sectors = new;
2501 return 0;
2502}
2503
NeilBrown83303b62006-01-06 00:21:06 -08002504static ssize_t
2505rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2506{
NeilBrown27c529b2008-03-04 14:29:33 -08002507 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002508 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002509 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002510
Dan Williamsb522adc2009-03-31 15:00:31 +11002511 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002512 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002513 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002514 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002515 sectors = super_types[my_mddev->major_version].
2516 rdev_size_change(rdev, sectors);
2517 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002518 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002519 } else if (!sectors)
2520 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2521 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002522 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002523 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002524 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002525
Andre Nolldd8ac332009-03-31 14:33:13 +11002526 rdev->sectors = sectors;
2527 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002528 /* need to check that all other rdevs with the same ->bdev
2529 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002530 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002531 * we have to change it back, we will have the lock again.
2532 */
2533 mddev_t *mddev;
2534 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002535 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002536
NeilBrown27c529b2008-03-04 14:29:33 -08002537 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002538 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002539 mdk_rdev_t *rdev2;
2540
2541 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002542 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002543 if (test_bit(AllReserved, &rdev2->flags) ||
2544 (rdev->bdev == rdev2->bdev &&
2545 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002546 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002547 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002548 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002549 overlap = 1;
2550 break;
2551 }
2552 mddev_unlock(mddev);
2553 if (overlap) {
2554 mddev_put(mddev);
2555 break;
2556 }
2557 }
NeilBrown27c529b2008-03-04 14:29:33 -08002558 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002559 if (overlap) {
2560 /* Someone else could have slipped in a size
2561 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002562 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002563 * safe, and trust userspace not to race with
2564 * itself
2565 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002566 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002567 return -EBUSY;
2568 }
2569 }
NeilBrown83303b62006-01-06 00:21:06 -08002570 return len;
2571}
2572
2573static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002574__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002575
Dan Williams06e3c812009-12-12 21:17:12 -07002576
2577static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2578{
2579 unsigned long long recovery_start = rdev->recovery_offset;
2580
2581 if (test_bit(In_sync, &rdev->flags) ||
2582 recovery_start == MaxSector)
2583 return sprintf(page, "none\n");
2584
2585 return sprintf(page, "%llu\n", recovery_start);
2586}
2587
2588static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2589{
2590 unsigned long long recovery_start;
2591
2592 if (cmd_match(buf, "none"))
2593 recovery_start = MaxSector;
2594 else if (strict_strtoull(buf, 10, &recovery_start))
2595 return -EINVAL;
2596
2597 if (rdev->mddev->pers &&
2598 rdev->raid_disk >= 0)
2599 return -EBUSY;
2600
2601 rdev->recovery_offset = recovery_start;
2602 if (recovery_start == MaxSector)
2603 set_bit(In_sync, &rdev->flags);
2604 else
2605 clear_bit(In_sync, &rdev->flags);
2606 return len;
2607}
2608
2609static struct rdev_sysfs_entry rdev_recovery_start =
2610__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2611
NeilBrown86e6ffd2005-11-08 21:39:24 -08002612static struct attribute *rdev_default_attrs[] = {
2613 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002614 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002615 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002616 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002617 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002618 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002619 NULL,
2620};
2621static ssize_t
2622rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2623{
2624 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2625 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002626 mddev_t *mddev = rdev->mddev;
2627 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002628
2629 if (!entry->show)
2630 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002631
2632 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2633 if (!rv) {
2634 if (rdev->mddev == NULL)
2635 rv = -EBUSY;
2636 else
2637 rv = entry->show(rdev, page);
2638 mddev_unlock(mddev);
2639 }
2640 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002641}
2642
2643static ssize_t
2644rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2645 const char *page, size_t length)
2646{
2647 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2648 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002649 ssize_t rv;
2650 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002651
2652 if (!entry->store)
2653 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002654 if (!capable(CAP_SYS_ADMIN))
2655 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002656 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002657 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002658 if (rdev->mddev == NULL)
2659 rv = -EBUSY;
2660 else
2661 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002662 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002663 }
2664 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002665}
2666
2667static void rdev_free(struct kobject *ko)
2668{
2669 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2670 kfree(rdev);
2671}
2672static struct sysfs_ops rdev_sysfs_ops = {
2673 .show = rdev_attr_show,
2674 .store = rdev_attr_store,
2675};
2676static struct kobj_type rdev_ktype = {
2677 .release = rdev_free,
2678 .sysfs_ops = &rdev_sysfs_ops,
2679 .default_attrs = rdev_default_attrs,
2680};
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682/*
2683 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2684 *
2685 * mark the device faulty if:
2686 *
2687 * - the device is nonexistent (zero size)
2688 * - the device has no valid superblock
2689 *
2690 * a faulty rdev _never_ has rdev->sb set.
2691 */
2692static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2693{
2694 char b[BDEVNAME_SIZE];
2695 int err;
2696 mdk_rdev_t *rdev;
2697 sector_t size;
2698
NeilBrown9ffae0c2006-01-06 00:20:32 -08002699 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 if (!rdev) {
2701 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2702 return ERR_PTR(-ENOMEM);
2703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 if ((err = alloc_disk_sb(rdev)))
2706 goto abort_free;
2707
NeilBrownc5d79ad2008-02-06 01:39:54 -08002708 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (err)
2710 goto abort_free;
2711
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002712 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002715 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002716 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002717 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002719 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002720 rdev->last_read_error.tv_sec = 0;
2721 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002723 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002724 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2727 if (!size) {
2728 printk(KERN_WARNING
2729 "md: %s has zero or unknown size, marking faulty!\n",
2730 bdevname(rdev->bdev,b));
2731 err = -EINVAL;
2732 goto abort_free;
2733 }
2734
2735 if (super_format >= 0) {
2736 err = super_types[super_format].
2737 load_super(rdev, NULL, super_minor);
2738 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002739 printk(KERN_WARNING
2740 "md: %s does not have a valid v%d.%d "
2741 "superblock, not importing!\n",
2742 bdevname(rdev->bdev,b),
2743 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 goto abort_free;
2745 }
2746 if (err < 0) {
2747 printk(KERN_WARNING
2748 "md: could not read %s's sb, not importing!\n",
2749 bdevname(rdev->bdev,b));
2750 goto abort_free;
2751 }
2752 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002755 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
2757 return rdev;
2758
2759abort_free:
2760 if (rdev->sb_page) {
2761 if (rdev->bdev)
2762 unlock_rdev(rdev);
2763 free_disk_sb(rdev);
2764 }
2765 kfree(rdev);
2766 return ERR_PTR(err);
2767}
2768
2769/*
2770 * Check a full RAID array for plausibility
2771 */
2772
2773
NeilBrowna757e642005-04-16 15:26:42 -07002774static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
2776 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002777 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 char b[BDEVNAME_SIZE];
2779
2780 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002781 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 switch (super_types[mddev->major_version].
2783 load_super(rdev, freshest, mddev->minor_version)) {
2784 case 1:
2785 freshest = rdev;
2786 break;
2787 case 0:
2788 break;
2789 default:
2790 printk( KERN_ERR \
2791 "md: fatal superblock inconsistency in %s"
2792 " -- removing from array\n",
2793 bdevname(rdev->bdev,b));
2794 kick_rdev_from_array(rdev);
2795 }
2796
2797
2798 super_types[mddev->major_version].
2799 validate_super(mddev, freshest);
2800
2801 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002802 rdev_for_each(rdev, tmp, mddev) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002803 if (rdev->desc_nr >= mddev->max_disks ||
2804 i > mddev->max_disks) {
2805 printk(KERN_WARNING
2806 "md: %s: %s: only %d devices permitted\n",
2807 mdname(mddev), bdevname(rdev->bdev, b),
2808 mddev->max_disks);
2809 kick_rdev_from_array(rdev);
2810 continue;
2811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (rdev != freshest)
2813 if (super_types[mddev->major_version].
2814 validate_super(mddev, rdev)) {
2815 printk(KERN_WARNING "md: kicking non-fresh %s"
2816 " from array!\n",
2817 bdevname(rdev->bdev,b));
2818 kick_rdev_from_array(rdev);
2819 continue;
2820 }
2821 if (mddev->level == LEVEL_MULTIPATH) {
2822 rdev->desc_nr = i++;
2823 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002824 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002825 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002826 rdev->raid_disk = -1;
2827 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
2829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830}
2831
NeilBrown72e02072009-12-14 12:49:55 +11002832/* Read a fixed-point number.
2833 * Numbers in sysfs attributes should be in "standard" units where
2834 * possible, so time should be in seconds.
2835 * However we internally use a a much smaller unit such as
2836 * milliseconds or jiffies.
2837 * This function takes a decimal number with a possible fractional
2838 * component, and produces an integer which is the result of
2839 * multiplying that number by 10^'scale'.
2840 * all without any floating-point arithmetic.
2841 */
2842int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2843{
2844 unsigned long result = 0;
2845 long decimals = -1;
2846 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2847 if (*cp == '.')
2848 decimals = 0;
2849 else if (decimals < scale) {
2850 unsigned int value;
2851 value = *cp - '0';
2852 result = result * 10 + value;
2853 if (decimals >= 0)
2854 decimals++;
2855 }
2856 cp++;
2857 }
2858 if (*cp == '\n')
2859 cp++;
2860 if (*cp)
2861 return -EINVAL;
2862 if (decimals < 0)
2863 decimals = 0;
2864 while (decimals < scale) {
2865 result *= 10;
2866 decimals ++;
2867 }
2868 *res = result;
2869 return 0;
2870}
2871
2872
NeilBrown19052c02008-08-05 15:54:13 +10002873static void md_safemode_timeout(unsigned long data);
2874
NeilBrowneae17012005-11-08 21:39:23 -08002875static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002876safe_delay_show(mddev_t *mddev, char *page)
2877{
2878 int msec = (mddev->safemode_delay*1000)/HZ;
2879 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2880}
2881static ssize_t
2882safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2883{
NeilBrown16f17b32006-06-26 00:27:37 -07002884 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002885
NeilBrown72e02072009-12-14 12:49:55 +11002886 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002887 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002888 if (msec == 0)
2889 mddev->safemode_delay = 0;
2890 else {
NeilBrown19052c02008-08-05 15:54:13 +10002891 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002892 mddev->safemode_delay = (msec*HZ)/1000;
2893 if (mddev->safemode_delay == 0)
2894 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002895 if (mddev->safemode_delay < old_delay)
2896 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002897 }
2898 return len;
2899}
2900static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002901__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002902
2903static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002904level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002905{
NeilBrown2604b702006-01-06 00:20:36 -08002906 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002907 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002908 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002909 else if (mddev->clevel[0])
2910 return sprintf(page, "%s\n", mddev->clevel);
2911 else if (mddev->level != LEVEL_NONE)
2912 return sprintf(page, "%d\n", mddev->level);
2913 else
2914 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002915}
2916
NeilBrownd9d166c2006-01-06 00:20:51 -08002917static ssize_t
2918level_store(mddev_t *mddev, const char *buf, size_t len)
2919{
NeilBrown245f46c2009-03-31 14:39:39 +11002920 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002921 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002922 struct mdk_personality *pers;
2923 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002924 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002925
2926 if (mddev->pers == NULL) {
2927 if (len == 0)
2928 return 0;
2929 if (len >= sizeof(mddev->clevel))
2930 return -ENOSPC;
2931 strncpy(mddev->clevel, buf, len);
2932 if (mddev->clevel[len-1] == '\n')
2933 len--;
2934 mddev->clevel[len] = 0;
2935 mddev->level = LEVEL_NONE;
2936 return rv;
2937 }
2938
2939 /* request to change the personality. Need to ensure:
2940 * - array is not engaged in resync/recovery/reshape
2941 * - old personality can be suspended
2942 * - new personality will access other array.
2943 */
2944
2945 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002946 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002947
2948 if (!mddev->pers->quiesce) {
2949 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2950 mdname(mddev), mddev->pers->name);
2951 return -EINVAL;
2952 }
2953
2954 /* Now find the new personality */
2955 if (len == 0 || len >= sizeof(level))
2956 return -EINVAL;
2957 strncpy(level, buf, len);
2958 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002959 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002960 level[len] = 0;
2961
2962 request_module("md-%s", level);
2963 spin_lock(&pers_lock);
2964 pers = find_pers(LEVEL_NONE, level);
2965 if (!pers || !try_module_get(pers->owner)) {
2966 spin_unlock(&pers_lock);
2967 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2968 return -EINVAL;
2969 }
2970 spin_unlock(&pers_lock);
2971
2972 if (pers == mddev->pers) {
2973 /* Nothing to do! */
2974 module_put(pers->owner);
2975 return rv;
2976 }
2977 if (!pers->takeover) {
2978 module_put(pers->owner);
2979 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2980 mdname(mddev), level);
2981 return -EINVAL;
2982 }
2983
2984 /* ->takeover must set new_* and/or delta_disks
2985 * if it succeeds, and may set them when it fails.
2986 */
2987 priv = pers->takeover(mddev);
2988 if (IS_ERR(priv)) {
2989 mddev->new_level = mddev->level;
2990 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002991 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002992 mddev->raid_disks -= mddev->delta_disks;
2993 mddev->delta_disks = 0;
2994 module_put(pers->owner);
2995 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2996 mdname(mddev), level);
2997 return PTR_ERR(priv);
2998 }
2999
3000 /* Looks like we have a winner */
3001 mddev_suspend(mddev);
3002 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003003
3004 if (mddev->pers->sync_request == NULL &&
3005 pers->sync_request != NULL) {
3006 /* need to add the md_redundancy_group */
3007 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3008 printk(KERN_WARNING
3009 "md: cannot register extra attributes for %s\n",
3010 mdname(mddev));
3011 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3012 }
3013 if (mddev->pers->sync_request != NULL &&
3014 pers->sync_request == NULL) {
3015 /* need to remove the md_redundancy_group */
3016 if (mddev->to_remove == NULL)
3017 mddev->to_remove = &md_redundancy_group;
3018 }
3019
NeilBrown245f46c2009-03-31 14:39:39 +11003020 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10003021 /* Invalidate devices that are now superfluous */
3022 list_for_each_entry(rdev, &mddev->disks, same_set)
3023 if (rdev->raid_disk >= mddev->raid_disks) {
3024 rdev->raid_disk = -1;
3025 clear_bit(In_sync, &rdev->flags);
3026 }
NeilBrown245f46c2009-03-31 14:39:39 +11003027 mddev->pers = pers;
3028 mddev->private = priv;
3029 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3030 mddev->level = mddev->new_level;
3031 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003032 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003033 mddev->delta_disks = 0;
3034 pers->run(mddev);
3035 mddev_resume(mddev);
3036 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3037 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3038 md_wakeup_thread(mddev->thread);
NeilBrownd9d166c2006-01-06 00:20:51 -08003039 return rv;
3040}
3041
3042static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003043__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003044
NeilBrownd4dbd022006-06-26 00:27:59 -07003045
3046static ssize_t
3047layout_show(mddev_t *mddev, char *page)
3048{
3049 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003050 if (mddev->reshape_position != MaxSector &&
3051 mddev->layout != mddev->new_layout)
3052 return sprintf(page, "%d (%d)\n",
3053 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003054 return sprintf(page, "%d\n", mddev->layout);
3055}
3056
3057static ssize_t
3058layout_store(mddev_t *mddev, const char *buf, size_t len)
3059{
3060 char *e;
3061 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003062
3063 if (!*buf || (*e && *e != '\n'))
3064 return -EINVAL;
3065
NeilBrownb3546032009-03-31 14:56:41 +11003066 if (mddev->pers) {
3067 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003068 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003069 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003070 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003071 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003072 if (err) {
3073 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003074 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003075 }
NeilBrownb3546032009-03-31 14:56:41 +11003076 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003077 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003078 if (mddev->reshape_position == MaxSector)
3079 mddev->layout = n;
3080 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003081 return len;
3082}
3083static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003084__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003085
3086
NeilBrowneae17012005-11-08 21:39:23 -08003087static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003088raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003089{
NeilBrownbb636542005-11-08 21:39:45 -08003090 if (mddev->raid_disks == 0)
3091 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003092 if (mddev->reshape_position != MaxSector &&
3093 mddev->delta_disks != 0)
3094 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3095 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003096 return sprintf(page, "%d\n", mddev->raid_disks);
3097}
3098
NeilBrownda943b992006-01-06 00:20:54 -08003099static int update_raid_disks(mddev_t *mddev, int raid_disks);
3100
3101static ssize_t
3102raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3103{
NeilBrownda943b992006-01-06 00:20:54 -08003104 char *e;
3105 int rv = 0;
3106 unsigned long n = simple_strtoul(buf, &e, 10);
3107
3108 if (!*buf || (*e && *e != '\n'))
3109 return -EINVAL;
3110
3111 if (mddev->pers)
3112 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003113 else if (mddev->reshape_position != MaxSector) {
3114 int olddisks = mddev->raid_disks - mddev->delta_disks;
3115 mddev->delta_disks = n - olddisks;
3116 mddev->raid_disks = n;
3117 } else
NeilBrownda943b992006-01-06 00:20:54 -08003118 mddev->raid_disks = n;
3119 return rv ? rv : len;
3120}
3121static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003122__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003123
NeilBrown24dd4692005-11-08 21:39:26 -08003124static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003125chunk_size_show(mddev_t *mddev, char *page)
3126{
NeilBrown08a02ec2007-05-09 02:35:38 -07003127 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003128 mddev->chunk_sectors != mddev->new_chunk_sectors)
3129 return sprintf(page, "%d (%d)\n",
3130 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003131 mddev->chunk_sectors << 9);
3132 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003133}
3134
3135static ssize_t
3136chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3137{
NeilBrown3b343802006-01-06 00:20:47 -08003138 char *e;
3139 unsigned long n = simple_strtoul(buf, &e, 10);
3140
NeilBrown3b343802006-01-06 00:20:47 -08003141 if (!*buf || (*e && *e != '\n'))
3142 return -EINVAL;
3143
NeilBrownb3546032009-03-31 14:56:41 +11003144 if (mddev->pers) {
3145 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003146 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003147 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003148 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003149 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003150 if (err) {
3151 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003152 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003153 }
NeilBrownb3546032009-03-31 14:56:41 +11003154 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003155 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003156 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003157 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003158 }
NeilBrown3b343802006-01-06 00:20:47 -08003159 return len;
3160}
3161static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003162__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003163
NeilBrowna94213b2006-06-26 00:28:00 -07003164static ssize_t
3165resync_start_show(mddev_t *mddev, char *page)
3166{
NeilBrownd1a7c502009-03-31 15:24:32 +11003167 if (mddev->recovery_cp == MaxSector)
3168 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003169 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3170}
3171
3172static ssize_t
3173resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3174{
NeilBrowna94213b2006-06-26 00:28:00 -07003175 char *e;
3176 unsigned long long n = simple_strtoull(buf, &e, 10);
3177
3178 if (mddev->pers)
3179 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003180 if (cmd_match(buf, "none"))
3181 n = MaxSector;
3182 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003183 return -EINVAL;
3184
3185 mddev->recovery_cp = n;
3186 return len;
3187}
3188static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003189__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003190
NeilBrown9e653b62006-06-26 00:27:58 -07003191/*
3192 * The array state can be:
3193 *
3194 * clear
3195 * No devices, no size, no level
3196 * Equivalent to STOP_ARRAY ioctl
3197 * inactive
3198 * May have some settings, but array is not active
3199 * all IO results in error
3200 * When written, doesn't tear down array, but just stops it
3201 * suspended (not supported yet)
3202 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003203 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003204 * readonly
3205 * no resync can happen. no superblocks get written.
3206 * write requests fail
3207 * read-auto
3208 * like readonly, but behaves like 'clean' on a write request.
3209 *
3210 * clean - no pending writes, but otherwise active.
3211 * When written to inactive array, starts without resync
3212 * If a write request arrives then
3213 * if metadata is known, mark 'dirty' and switch to 'active'.
3214 * if not known, block and switch to write-pending
3215 * If written to an active array that has pending writes, then fails.
3216 * active
3217 * fully active: IO and resync can be happening.
3218 * When written to inactive array, starts with resync
3219 *
3220 * write-pending
3221 * clean, but writes are blocked waiting for 'active' to be written.
3222 *
3223 * active-idle
3224 * like active, but no writes have been seen for a while (100msec).
3225 *
3226 */
3227enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3228 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003229static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003230 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3231 "write-pending", "active-idle", NULL };
3232
3233static int match_word(const char *word, char **list)
3234{
3235 int n;
3236 for (n=0; list[n]; n++)
3237 if (cmd_match(word, list[n]))
3238 break;
3239 return n;
3240}
3241
3242static ssize_t
3243array_state_show(mddev_t *mddev, char *page)
3244{
3245 enum array_state st = inactive;
3246
3247 if (mddev->pers)
3248 switch(mddev->ro) {
3249 case 1:
3250 st = readonly;
3251 break;
3252 case 2:
3253 st = read_auto;
3254 break;
3255 case 0:
3256 if (mddev->in_sync)
3257 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003258 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3259 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003260 else if (mddev->safemode)
3261 st = active_idle;
3262 else
3263 st = active;
3264 }
3265 else {
3266 if (list_empty(&mddev->disks) &&
3267 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003268 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003269 st = clear;
3270 else
3271 st = inactive;
3272 }
3273 return sprintf(page, "%s\n", array_states[st]);
3274}
3275
Neil Browndf5b20c2008-07-11 22:02:22 +10003276static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003277static int do_md_run(mddev_t * mddev);
3278static int restart_array(mddev_t *mddev);
3279
3280static ssize_t
3281array_state_store(mddev_t *mddev, const char *buf, size_t len)
3282{
3283 int err = -EINVAL;
3284 enum array_state st = match_word(buf, array_states);
3285 switch(st) {
3286 case bad_word:
3287 break;
3288 case clear:
3289 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003290 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003291 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003292 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003293 break;
3294 case inactive:
3295 /* stopping an active array */
3296 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003297 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003298 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003299 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003300 } else
3301 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003302 break;
3303 case suspended:
3304 break; /* not supported yet */
3305 case readonly:
3306 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003307 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003308 else {
3309 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003310 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003311 err = do_md_run(mddev);
3312 }
3313 break;
3314 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003315 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003316 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003317 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003318 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003319 err = restart_array(mddev);
3320 if (err == 0) {
3321 mddev->ro = 2;
3322 set_disk_ro(mddev->gendisk, 0);
3323 }
NeilBrown9e653b62006-06-26 00:27:58 -07003324 } else {
3325 mddev->ro = 2;
3326 err = do_md_run(mddev);
3327 }
3328 break;
3329 case clean:
3330 if (mddev->pers) {
3331 restart_array(mddev);
3332 spin_lock_irq(&mddev->write_lock);
3333 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003334 if (mddev->in_sync == 0) {
3335 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003336 if (mddev->safemode == 1)
3337 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003338 if (mddev->persistent)
3339 set_bit(MD_CHANGE_CLEAN,
3340 &mddev->flags);
3341 }
3342 err = 0;
3343 } else
3344 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003345 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003346 } else
3347 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003348 break;
3349 case active:
3350 if (mddev->pers) {
3351 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003352 if (mddev->external)
3353 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003354 wake_up(&mddev->sb_wait);
3355 err = 0;
3356 } else {
3357 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003358 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003359 err = do_md_run(mddev);
3360 }
3361 break;
3362 case write_pending:
3363 case active_idle:
3364 /* these cannot be set */
3365 break;
3366 }
3367 if (err)
3368 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003369 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003370 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003371 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003372 }
NeilBrown9e653b62006-06-26 00:27:58 -07003373}
NeilBrown80ca3a42006-07-10 04:44:18 -07003374static struct md_sysfs_entry md_array_state =
3375__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003376
NeilBrown6d7ff7382006-01-06 00:21:16 -08003377static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003378max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3379 return sprintf(page, "%d\n",
3380 atomic_read(&mddev->max_corr_read_errors));
3381}
3382
3383static ssize_t
3384max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3385{
3386 char *e;
3387 unsigned long n = simple_strtoul(buf, &e, 10);
3388
3389 if (*buf && (*e == 0 || *e == '\n')) {
3390 atomic_set(&mddev->max_corr_read_errors, n);
3391 return len;
3392 }
3393 return -EINVAL;
3394}
3395
3396static struct md_sysfs_entry max_corr_read_errors =
3397__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3398 max_corrected_read_errors_store);
3399
3400static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003401null_show(mddev_t *mddev, char *page)
3402{
3403 return -EINVAL;
3404}
3405
3406static ssize_t
3407new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3408{
3409 /* buf must be %d:%d\n? giving major and minor numbers */
3410 /* The new device is added to the array.
3411 * If the array has a persistent superblock, we read the
3412 * superblock to initialise info and check validity.
3413 * Otherwise, only checking done is that in bind_rdev_to_array,
3414 * which mainly checks size.
3415 */
3416 char *e;
3417 int major = simple_strtoul(buf, &e, 10);
3418 int minor;
3419 dev_t dev;
3420 mdk_rdev_t *rdev;
3421 int err;
3422
3423 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3424 return -EINVAL;
3425 minor = simple_strtoul(e+1, &e, 10);
3426 if (*e && *e != '\n')
3427 return -EINVAL;
3428 dev = MKDEV(major, minor);
3429 if (major != MAJOR(dev) ||
3430 minor != MINOR(dev))
3431 return -EOVERFLOW;
3432
3433
3434 if (mddev->persistent) {
3435 rdev = md_import_device(dev, mddev->major_version,
3436 mddev->minor_version);
3437 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3438 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3439 mdk_rdev_t, same_set);
3440 err = super_types[mddev->major_version]
3441 .load_super(rdev, rdev0, mddev->minor_version);
3442 if (err < 0)
3443 goto out;
3444 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003445 } else if (mddev->external)
3446 rdev = md_import_device(dev, -2, -1);
3447 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003448 rdev = md_import_device(dev, -1, -1);
3449
3450 if (IS_ERR(rdev))
3451 return PTR_ERR(rdev);
3452 err = bind_rdev_to_array(rdev, mddev);
3453 out:
3454 if (err)
3455 export_rdev(rdev);
3456 return err ? err : len;
3457}
3458
3459static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003460__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003461
3462static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003463bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3464{
3465 char *end;
3466 unsigned long chunk, end_chunk;
3467
3468 if (!mddev->bitmap)
3469 goto out;
3470 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3471 while (*buf) {
3472 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3473 if (buf == end) break;
3474 if (*end == '-') { /* range */
3475 buf = end + 1;
3476 end_chunk = simple_strtoul(buf, &end, 0);
3477 if (buf == end) break;
3478 }
3479 if (*end && !isspace(*end)) break;
3480 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003481 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003482 }
3483 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3484out:
3485 return len;
3486}
3487
3488static struct md_sysfs_entry md_bitmap =
3489__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3490
3491static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003492size_show(mddev_t *mddev, char *page)
3493{
Andre Noll58c0fed2009-03-31 14:33:13 +11003494 return sprintf(page, "%llu\n",
3495 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003496}
3497
Andre Nolld71f9f82008-07-11 22:02:22 +10003498static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003499
3500static ssize_t
3501size_store(mddev_t *mddev, const char *buf, size_t len)
3502{
3503 /* If array is inactive, we can reduce the component size, but
3504 * not increase it (except from 0).
3505 * If array is active, we can try an on-line resize
3506 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003507 sector_t sectors;
3508 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003509
Andre Noll58c0fed2009-03-31 14:33:13 +11003510 if (err < 0)
3511 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003512 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003513 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003514 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003515 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003516 if (mddev->dev_sectors == 0 ||
3517 mddev->dev_sectors > sectors)
3518 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003519 else
3520 err = -ENOSPC;
3521 }
3522 return err ? err : len;
3523}
3524
3525static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003526__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003527
NeilBrown8bb93aa2006-01-06 00:20:50 -08003528
3529/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003530 * This is one of
3531 * 'none' for arrays with no metadata (good luck...)
3532 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003533 * or N.M for internally known formats
3534 */
3535static ssize_t
3536metadata_show(mddev_t *mddev, char *page)
3537{
3538 if (mddev->persistent)
3539 return sprintf(page, "%d.%d\n",
3540 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003541 else if (mddev->external)
3542 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003543 else
3544 return sprintf(page, "none\n");
3545}
3546
3547static ssize_t
3548metadata_store(mddev_t *mddev, const char *buf, size_t len)
3549{
3550 int major, minor;
3551 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003552 /* Changing the details of 'external' metadata is
3553 * always permitted. Otherwise there must be
3554 * no devices attached to the array.
3555 */
3556 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3557 ;
3558 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003559 return -EBUSY;
3560
3561 if (cmd_match(buf, "none")) {
3562 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003563 mddev->external = 0;
3564 mddev->major_version = 0;
3565 mddev->minor_version = 90;
3566 return len;
3567 }
3568 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003569 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003570 if (namelen >= sizeof(mddev->metadata_type))
3571 namelen = sizeof(mddev->metadata_type)-1;
3572 strncpy(mddev->metadata_type, buf+9, namelen);
3573 mddev->metadata_type[namelen] = 0;
3574 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3575 mddev->metadata_type[--namelen] = 0;
3576 mddev->persistent = 0;
3577 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003578 mddev->major_version = 0;
3579 mddev->minor_version = 90;
3580 return len;
3581 }
3582 major = simple_strtoul(buf, &e, 10);
3583 if (e==buf || *e != '.')
3584 return -EINVAL;
3585 buf = e+1;
3586 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003587 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003588 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003589 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003590 return -ENOENT;
3591 mddev->major_version = major;
3592 mddev->minor_version = minor;
3593 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003594 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003595 return len;
3596}
3597
3598static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003599__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003600
NeilBrowna35b0d62006-01-06 00:20:49 -08003601static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003602action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003603{
NeilBrown7eec3142005-11-08 21:39:44 -08003604 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003605 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3606 type = "frozen";
3607 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003608 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003609 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3610 type = "reshape";
3611 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003612 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3613 type = "resync";
3614 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3615 type = "check";
3616 else
3617 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003618 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003619 type = "recover";
3620 }
3621 return sprintf(page, "%s\n", type);
3622}
3623
3624static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003625action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003626{
NeilBrown7eec3142005-11-08 21:39:44 -08003627 if (!mddev->pers || !mddev->pers->sync_request)
3628 return -EINVAL;
3629
NeilBrownb6a9ce62009-05-26 09:41:17 +10003630 if (cmd_match(page, "frozen"))
3631 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3632 else
3633 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3634
3635 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003636 if (mddev->sync_thread) {
3637 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3638 md_unregister_thread(mddev->sync_thread);
3639 mddev->sync_thread = NULL;
3640 mddev->recovery = 0;
3641 }
NeilBrown03c902e2006-01-06 00:20:46 -08003642 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3643 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003644 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003645 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003646 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003647 else if (cmd_match(page, "recover")) {
3648 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3649 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3650 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003651 int err;
3652 if (mddev->pers->start_reshape == NULL)
3653 return -EINVAL;
3654 err = mddev->pers->start_reshape(mddev);
3655 if (err)
3656 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003657 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003658 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003659 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003660 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003661 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003662 return -EINVAL;
3663 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3664 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003665 }
NeilBrown03c902e2006-01-06 00:20:46 -08003666 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003667 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003668 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003669 return len;
3670}
3671
NeilBrown9d888832005-11-08 21:39:26 -08003672static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003673mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003674{
3675 return sprintf(page, "%llu\n",
3676 (unsigned long long) mddev->resync_mismatches);
3677}
3678
NeilBrown80ca3a42006-07-10 04:44:18 -07003679static struct md_sysfs_entry md_scan_mode =
3680__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003681
NeilBrown96de1e62005-11-08 21:39:39 -08003682
NeilBrown80ca3a42006-07-10 04:44:18 -07003683static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003684
NeilBrown88202a02006-01-06 00:21:36 -08003685static ssize_t
3686sync_min_show(mddev_t *mddev, char *page)
3687{
3688 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3689 mddev->sync_speed_min ? "local": "system");
3690}
3691
3692static ssize_t
3693sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3694{
3695 int min;
3696 char *e;
3697 if (strncmp(buf, "system", 6)==0) {
3698 mddev->sync_speed_min = 0;
3699 return len;
3700 }
3701 min = simple_strtoul(buf, &e, 10);
3702 if (buf == e || (*e && *e != '\n') || min <= 0)
3703 return -EINVAL;
3704 mddev->sync_speed_min = min;
3705 return len;
3706}
3707
3708static struct md_sysfs_entry md_sync_min =
3709__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3710
3711static ssize_t
3712sync_max_show(mddev_t *mddev, char *page)
3713{
3714 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3715 mddev->sync_speed_max ? "local": "system");
3716}
3717
3718static ssize_t
3719sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3720{
3721 int max;
3722 char *e;
3723 if (strncmp(buf, "system", 6)==0) {
3724 mddev->sync_speed_max = 0;
3725 return len;
3726 }
3727 max = simple_strtoul(buf, &e, 10);
3728 if (buf == e || (*e && *e != '\n') || max <= 0)
3729 return -EINVAL;
3730 mddev->sync_speed_max = max;
3731 return len;
3732}
3733
3734static struct md_sysfs_entry md_sync_max =
3735__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3736
Iustin Popd7f3d292007-10-16 23:30:54 -07003737static ssize_t
3738degraded_show(mddev_t *mddev, char *page)
3739{
3740 return sprintf(page, "%d\n", mddev->degraded);
3741}
3742static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003743
3744static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003745sync_force_parallel_show(mddev_t *mddev, char *page)
3746{
3747 return sprintf(page, "%d\n", mddev->parallel_resync);
3748}
3749
3750static ssize_t
3751sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3752{
3753 long n;
3754
3755 if (strict_strtol(buf, 10, &n))
3756 return -EINVAL;
3757
3758 if (n != 0 && n != 1)
3759 return -EINVAL;
3760
3761 mddev->parallel_resync = n;
3762
3763 if (mddev->sync_thread)
3764 wake_up(&resync_wait);
3765
3766 return len;
3767}
3768
3769/* force parallel resync, even with shared block devices */
3770static struct md_sysfs_entry md_sync_force_parallel =
3771__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3772 sync_force_parallel_show, sync_force_parallel_store);
3773
3774static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003775sync_speed_show(mddev_t *mddev, char *page)
3776{
3777 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003778 if (mddev->curr_resync == 0)
3779 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003780 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3781 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003782 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003783 db = resync - mddev->resync_mark_cnt;
3784 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003785}
3786
NeilBrown80ca3a42006-07-10 04:44:18 -07003787static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003788
3789static ssize_t
3790sync_completed_show(mddev_t *mddev, char *page)
3791{
Andre Noll58c0fed2009-03-31 14:33:13 +11003792 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003793
NeilBrownacb180b2009-04-14 16:28:34 +10003794 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3795 return sprintf(page, "none\n");
3796
NeilBrown88202a02006-01-06 00:21:36 -08003797 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003798 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003799 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003800 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003801
NeilBrownacb180b2009-04-14 16:28:34 +10003802 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003803 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003804}
3805
NeilBrown80ca3a42006-07-10 04:44:18 -07003806static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003807
NeilBrowne464eaf2006-03-27 01:18:14 -08003808static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003809min_sync_show(mddev_t *mddev, char *page)
3810{
3811 return sprintf(page, "%llu\n",
3812 (unsigned long long)mddev->resync_min);
3813}
3814static ssize_t
3815min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3816{
3817 unsigned long long min;
3818 if (strict_strtoull(buf, 10, &min))
3819 return -EINVAL;
3820 if (min > mddev->resync_max)
3821 return -EINVAL;
3822 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3823 return -EBUSY;
3824
3825 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003826 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003827 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003828 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003829 return -EINVAL;
3830 }
3831 mddev->resync_min = min;
3832
3833 return len;
3834}
3835
3836static struct md_sysfs_entry md_min_sync =
3837__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3838
3839static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003840max_sync_show(mddev_t *mddev, char *page)
3841{
3842 if (mddev->resync_max == MaxSector)
3843 return sprintf(page, "max\n");
3844 else
3845 return sprintf(page, "%llu\n",
3846 (unsigned long long)mddev->resync_max);
3847}
3848static ssize_t
3849max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3850{
3851 if (strncmp(buf, "max", 3) == 0)
3852 mddev->resync_max = MaxSector;
3853 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003854 unsigned long long max;
3855 if (strict_strtoull(buf, 10, &max))
3856 return -EINVAL;
3857 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003858 return -EINVAL;
3859 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003860 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003861 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3862 return -EBUSY;
3863
3864 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003865 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003866 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003867 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003868 return -EINVAL;
3869 }
3870 mddev->resync_max = max;
3871 }
3872 wake_up(&mddev->recovery_wait);
3873 return len;
3874}
3875
3876static struct md_sysfs_entry md_max_sync =
3877__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3878
3879static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003880suspend_lo_show(mddev_t *mddev, char *page)
3881{
3882 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3883}
3884
3885static ssize_t
3886suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3887{
3888 char *e;
3889 unsigned long long new = simple_strtoull(buf, &e, 10);
3890
NeilBrownb8d966e2009-07-01 11:14:04 +10003891 if (mddev->pers == NULL ||
3892 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003893 return -EINVAL;
3894 if (buf == e || (*e && *e != '\n'))
3895 return -EINVAL;
3896 if (new >= mddev->suspend_hi ||
3897 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3898 mddev->suspend_lo = new;
3899 mddev->pers->quiesce(mddev, 2);
3900 return len;
3901 } else
3902 return -EINVAL;
3903}
3904static struct md_sysfs_entry md_suspend_lo =
3905__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3906
3907
3908static ssize_t
3909suspend_hi_show(mddev_t *mddev, char *page)
3910{
3911 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3912}
3913
3914static ssize_t
3915suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3916{
3917 char *e;
3918 unsigned long long new = simple_strtoull(buf, &e, 10);
3919
NeilBrownb8d966e2009-07-01 11:14:04 +10003920 if (mddev->pers == NULL ||
3921 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003922 return -EINVAL;
3923 if (buf == e || (*e && *e != '\n'))
3924 return -EINVAL;
3925 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3926 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3927 mddev->suspend_hi = new;
3928 mddev->pers->quiesce(mddev, 1);
3929 mddev->pers->quiesce(mddev, 0);
3930 return len;
3931 } else
3932 return -EINVAL;
3933}
3934static struct md_sysfs_entry md_suspend_hi =
3935__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3936
NeilBrown08a02ec2007-05-09 02:35:38 -07003937static ssize_t
3938reshape_position_show(mddev_t *mddev, char *page)
3939{
3940 if (mddev->reshape_position != MaxSector)
3941 return sprintf(page, "%llu\n",
3942 (unsigned long long)mddev->reshape_position);
3943 strcpy(page, "none\n");
3944 return 5;
3945}
3946
3947static ssize_t
3948reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3949{
3950 char *e;
3951 unsigned long long new = simple_strtoull(buf, &e, 10);
3952 if (mddev->pers)
3953 return -EBUSY;
3954 if (buf == e || (*e && *e != '\n'))
3955 return -EINVAL;
3956 mddev->reshape_position = new;
3957 mddev->delta_disks = 0;
3958 mddev->new_level = mddev->level;
3959 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003960 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003961 return len;
3962}
3963
3964static struct md_sysfs_entry md_reshape_position =
3965__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3966 reshape_position_store);
3967
Dan Williamsb522adc2009-03-31 15:00:31 +11003968static ssize_t
3969array_size_show(mddev_t *mddev, char *page)
3970{
3971 if (mddev->external_size)
3972 return sprintf(page, "%llu\n",
3973 (unsigned long long)mddev->array_sectors/2);
3974 else
3975 return sprintf(page, "default\n");
3976}
3977
3978static ssize_t
3979array_size_store(mddev_t *mddev, const char *buf, size_t len)
3980{
3981 sector_t sectors;
3982
3983 if (strncmp(buf, "default", 7) == 0) {
3984 if (mddev->pers)
3985 sectors = mddev->pers->size(mddev, 0, 0);
3986 else
3987 sectors = mddev->array_sectors;
3988
3989 mddev->external_size = 0;
3990 } else {
3991 if (strict_blocks_to_sectors(buf, &sectors) < 0)
3992 return -EINVAL;
3993 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10003994 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11003995
3996 mddev->external_size = 1;
3997 }
3998
3999 mddev->array_sectors = sectors;
4000 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004001 if (mddev->pers)
4002 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004003
4004 return len;
4005}
4006
4007static struct md_sysfs_entry md_array_size =
4008__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4009 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004010
NeilBrowneae17012005-11-08 21:39:23 -08004011static struct attribute *md_default_attrs[] = {
4012 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004013 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004014 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004015 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004016 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004017 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004018 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004019 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004020 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004021 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004022 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004023 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004024 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004025 NULL,
4026};
4027
4028static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004029 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004030 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004031 &md_sync_min.attr,
4032 &md_sync_max.attr,
4033 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004034 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004035 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004036 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004037 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004038 &md_suspend_lo.attr,
4039 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004040 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004041 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004042 NULL,
4043};
NeilBrown411036f2005-11-08 21:39:40 -08004044static struct attribute_group md_redundancy_group = {
4045 .name = NULL,
4046 .attrs = md_redundancy_attrs,
4047};
4048
NeilBrowneae17012005-11-08 21:39:23 -08004049
4050static ssize_t
4051md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4052{
4053 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4054 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004055 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004056
4057 if (!entry->show)
4058 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004059 rv = mddev_lock(mddev);
4060 if (!rv) {
4061 rv = entry->show(mddev, page);
4062 mddev_unlock(mddev);
4063 }
NeilBrown96de1e62005-11-08 21:39:39 -08004064 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004065}
4066
4067static ssize_t
4068md_attr_store(struct kobject *kobj, struct attribute *attr,
4069 const char *page, size_t length)
4070{
4071 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4072 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004073 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004074
4075 if (!entry->store)
4076 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004077 if (!capable(CAP_SYS_ADMIN))
4078 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004079 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004080 if (mddev->hold_active == UNTIL_IOCTL)
4081 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004082 if (!rv) {
4083 rv = entry->store(mddev, page, length);
4084 mddev_unlock(mddev);
4085 }
NeilBrown96de1e62005-11-08 21:39:39 -08004086 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004087}
4088
4089static void md_free(struct kobject *ko)
4090{
4091 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004092
4093 if (mddev->sysfs_state)
4094 sysfs_put(mddev->sysfs_state);
4095
4096 if (mddev->gendisk) {
4097 del_gendisk(mddev->gendisk);
4098 put_disk(mddev->gendisk);
4099 }
4100 if (mddev->queue)
4101 blk_cleanup_queue(mddev->queue);
4102
NeilBrowneae17012005-11-08 21:39:23 -08004103 kfree(mddev);
4104}
4105
4106static struct sysfs_ops md_sysfs_ops = {
4107 .show = md_attr_show,
4108 .store = md_attr_store,
4109};
4110static struct kobj_type md_ktype = {
4111 .release = md_free,
4112 .sysfs_ops = &md_sysfs_ops,
4113 .default_attrs = md_default_attrs,
4114};
4115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116int mdp_major = 0;
4117
Dan Williams5fd3a172009-03-04 00:57:25 -07004118static void mddev_delayed_delete(struct work_struct *ws)
4119{
4120 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4121
NeilBrown43a70502009-12-14 12:49:55 +11004122 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004123 kobject_del(&mddev->kobj);
4124 kobject_put(&mddev->kobj);
4125}
4126
NeilBrownefeb53c2009-01-09 08:31:10 +11004127static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004129 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 mddev_t *mddev = mddev_find(dev);
4131 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004132 int partitioned;
4133 int shift;
4134 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004135 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004138 return -ENODEV;
4139
4140 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4141 shift = partitioned ? MdpMinorShift : 0;
4142 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
NeilBrownd3374822009-01-09 08:31:10 +11004144 /* wait for any previous instance if this device
4145 * to be completed removed (mddev_delayed_delete).
4146 */
4147 flush_scheduled_work();
4148
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004149 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004150 error = -EEXIST;
4151 if (mddev->gendisk)
4152 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004153
4154 if (name) {
4155 /* Need to ensure that 'name' is not a duplicate.
4156 */
4157 mddev_t *mddev2;
4158 spin_lock(&all_mddevs_lock);
4159
4160 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4161 if (mddev2->gendisk &&
4162 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4163 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004164 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004165 }
4166 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 }
NeilBrown8b765392009-01-09 08:31:08 +11004168
NeilBrown0909dc42009-07-01 12:27:21 +10004169 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004170 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004171 if (!mddev->queue)
4172 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004173 mddev->queue->queuedata = mddev;
4174
NeilBrown8b765392009-01-09 08:31:08 +11004175 /* Can be unlocked because the queue is new: no concurrency */
4176 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4177
NeilBrown409c57f2009-03-31 14:39:39 +11004178 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 disk = alloc_disk(1 << shift);
4181 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004182 blk_cleanup_queue(mddev->queue);
4183 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004184 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004186 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004188 if (name)
4189 strcpy(disk->disk_name, name);
4190 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004192 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 disk->fops = &md_fops;
4195 disk->private_data = mddev;
4196 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004197 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004198 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004199 * remove it now.
4200 */
4201 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 add_disk(disk);
4203 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004204 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4205 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004206 if (error) {
4207 /* This isn't possible, but as kobject_init_and_add is marked
4208 * __must_check, we must do something with the result
4209 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004210 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4211 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004212 error = 0;
4213 }
NeilBrown43a70502009-12-14 12:49:55 +11004214 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4215 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004216 abort:
4217 mutex_unlock(&disks_mutex);
4218 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004219 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004220 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4221 }
NeilBrownd3374822009-01-09 08:31:10 +11004222 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004223 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004224}
4225
4226static struct kobject *md_probe(dev_t dev, int *part, void *data)
4227{
4228 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return NULL;
4230}
4231
NeilBrownefeb53c2009-01-09 08:31:10 +11004232static int add_named_array(const char *val, struct kernel_param *kp)
4233{
4234 /* val must be "md_*" where * is not all digits.
4235 * We allocate an array with a large free minor number, and
4236 * set the name to val. val must not already be an active name.
4237 */
4238 int len = strlen(val);
4239 char buf[DISK_NAME_LEN];
4240
4241 while (len && val[len-1] == '\n')
4242 len--;
4243 if (len >= DISK_NAME_LEN)
4244 return -E2BIG;
4245 strlcpy(buf, val, len+1);
4246 if (strncmp(buf, "md_", 3) != 0)
4247 return -EINVAL;
4248 return md_alloc(0, buf);
4249}
4250
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251static void md_safemode_timeout(unsigned long data)
4252{
4253 mddev_t *mddev = (mddev_t *) data;
4254
Neil Brown0fd62b82008-06-28 08:31:36 +10004255 if (!atomic_read(&mddev->writes_pending)) {
4256 mddev->safemode = 1;
4257 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004258 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 md_wakeup_thread(mddev->thread);
4261}
4262
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004263static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265static int do_md_run(mddev_t * mddev)
4266{
NeilBrown2604b702006-01-06 00:20:36 -08004267 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 mdk_rdev_t *rdev;
4269 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004270 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
NeilBrowna757e642005-04-16 15:26:42 -07004272 if (list_empty(&mddev->disks))
4273 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
4276 if (mddev->pers)
4277 return -EBUSY;
4278
NeilBrownb6eb1272010-04-15 10:13:47 +10004279 /* These two calls synchronise us with the
4280 * sysfs_remove_group calls in mddev_unlock,
4281 * so they must have completed.
4282 */
4283 mutex_lock(&mddev->open_mutex);
4284 mutex_unlock(&mddev->open_mutex);
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 /*
4287 * Analyze all RAID superblock(s)
4288 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004289 if (!mddev->raid_disks) {
4290 if (!mddev->persistent)
4291 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004292 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
NeilBrownd9d166c2006-01-06 00:20:51 -08004295 if (mddev->level != LEVEL_NONE)
4296 request_module("md-level-%d", mddev->level);
4297 else if (mddev->clevel[0])
4298 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
4300 /*
4301 * Drop all container device buffers, from now on
4302 * the only valid external interface is through the md
4303 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004305 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004306 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 continue;
4308 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004309 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004310
4311 /* perform some consistency tests on the device.
4312 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004313 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004314 */
Andre Noll0f420352008-07-11 22:02:23 +10004315 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004316 if (mddev->dev_sectors &&
4317 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004318 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004319 printk("md: %s: data overlaps metadata\n",
4320 mdname(mddev));
4321 return -EINVAL;
4322 }
4323 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004324 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004325 > rdev->data_offset) {
4326 printk("md: %s: metadata overlaps data\n",
4327 mdname(mddev));
4328 return -EINVAL;
4329 }
4330 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004331 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 }
4333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
4336 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004337 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004338 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004340 if (mddev->level != LEVEL_NONE)
4341 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4342 mddev->level);
4343 else
4344 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4345 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 return -EINVAL;
4347 }
NeilBrown2604b702006-01-06 00:20:36 -08004348 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004350 if (mddev->level != pers->level) {
4351 mddev->level = pers->level;
4352 mddev->new_level = pers->level;
4353 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004354 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
NeilBrownf6705572006-03-27 01:18:11 -08004356 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004357 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004358 /* This personality cannot handle reshaping... */
4359 mddev->pers = NULL;
4360 module_put(pers->owner);
4361 return -EINVAL;
4362 }
4363
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004364 if (pers->sync_request) {
4365 /* Warn if this is a potentially silly
4366 * configuration.
4367 */
4368 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4369 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004370 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004371
4372 list_for_each_entry(rdev, &mddev->disks, same_set)
4373 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004374 if (rdev < rdev2 &&
4375 rdev->bdev->bd_contains ==
4376 rdev2->bdev->bd_contains) {
4377 printk(KERN_WARNING
4378 "%s: WARNING: %s appears to be"
4379 " on the same physical disk as"
4380 " %s.\n",
4381 mdname(mddev),
4382 bdevname(rdev->bdev,b),
4383 bdevname(rdev2->bdev,b2));
4384 warned = 1;
4385 }
4386 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004387
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004388 if (warned)
4389 printk(KERN_WARNING
4390 "True protection against single-disk"
4391 " failure might be compromised.\n");
4392 }
4393
NeilBrown657390d2005-08-26 18:34:16 -07004394 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004395 /* may be over-ridden by personality */
4396 mddev->resync_max_sectors = mddev->dev_sectors;
4397
NeilBrowna9701a32005-11-08 21:39:34 -08004398 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004399 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
NeilBrown0f9552b52009-12-30 12:08:50 +11004401 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004402 mddev->ro = 2; /* read-only, but switch on first write */
4403
NeilBrownb15c2e52006-01-06 00:20:16 -08004404 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004405 if (err)
4406 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004407 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4408 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4409 " but 'external_size' not in effect?\n", __func__);
4410 printk(KERN_ERR
4411 "md: invalid array_size %llu > default size %llu\n",
4412 (unsigned long long)mddev->array_sectors / 2,
4413 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4414 err = -EINVAL;
4415 mddev->pers->stop(mddev);
4416 }
4417 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004418 err = bitmap_create(mddev);
4419 if (err) {
4420 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4421 mdname(mddev), err);
4422 mddev->pers->stop(mddev);
4423 }
4424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 module_put(mddev->pers->owner);
4427 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004428 bitmap_destroy(mddev);
4429 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004431 if (mddev->pers->sync_request) {
4432 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4433 printk(KERN_WARNING
4434 "md: cannot register extra attributes for %s\n",
4435 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004436 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004437 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004438 mddev->ro = 0;
4439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004441 atomic_set(&mddev->max_corr_read_errors,
4442 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 mddev->safemode = 0;
4444 mddev->safemode_timer.function = md_safemode_timeout;
4445 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004446 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004448
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004449 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004450 if (rdev->raid_disk >= 0) {
4451 char nm[20];
4452 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004453 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4454 printk("md: cannot register %s for %s\n",
4455 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4459
NeilBrown850b2b42006-10-03 01:15:46 -07004460 if (mddev->flags)
4461 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462
Andre Nollf233ea52008-07-21 17:05:22 +10004463 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
NeilBrown0b8c9de2006-07-10 04:44:15 -07004465 md_wakeup_thread(mddev->thread);
4466 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004467
NeilBrown449aad32009-08-03 10:59:58 +10004468 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004469 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004470 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004471 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004472 if (mddev->sysfs_action)
4473 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004474 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004475 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 return 0;
4477}
4478
4479static int restart_array(mddev_t *mddev)
4480{
4481 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Andre Noll80fab1d2008-07-11 22:02:21 +10004483 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004485 return -ENXIO;
4486 if (!mddev->pers)
4487 return -EINVAL;
4488 if (!mddev->ro)
4489 return -EBUSY;
4490 mddev->safemode = 0;
4491 mddev->ro = 0;
4492 set_disk_ro(disk, 0);
4493 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4494 mdname(mddev));
4495 /* Kick recovery or resync if necessary */
4496 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4497 md_wakeup_thread(mddev->thread);
4498 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004499 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004500 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501}
4502
NeilBrownacc55e22006-06-26 00:27:47 -07004503/* similar to deny_write_access, but accounts for our holding a reference
4504 * to the file ourselves */
4505static int deny_bitmap_write_access(struct file * file)
4506{
4507 struct inode *inode = file->f_mapping->host;
4508
4509 spin_lock(&inode->i_lock);
4510 if (atomic_read(&inode->i_writecount) > 1) {
4511 spin_unlock(&inode->i_lock);
4512 return -ETXTBSY;
4513 }
4514 atomic_set(&inode->i_writecount, -1);
4515 spin_unlock(&inode->i_lock);
4516
4517 return 0;
4518}
4519
NeilBrown43a70502009-12-14 12:49:55 +11004520void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004521{
4522 struct inode *inode = file->f_mapping->host;
4523
4524 spin_lock(&inode->i_lock);
4525 atomic_set(&inode->i_writecount, 1);
4526 spin_unlock(&inode->i_lock);
4527}
4528
NeilBrown9e653b62006-06-26 00:27:58 -07004529/* mode:
4530 * 0 - completely stop and dis-assemble array
4531 * 1 - switch to readonly
4532 * 2 - stop but do not disassemble array
4533 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004534static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 int err = 0;
4537 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004538 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
NeilBrownc8c00a62009-08-10 12:50:52 +10004540 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004541 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004542 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004543 err = -EBUSY;
4544 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545
4546 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004547 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4549 md_unregister_thread(mddev->sync_thread);
4550 mddev->sync_thread = NULL;
4551 }
4552
4553 del_timer_sync(&mddev->safemode_timer);
4554
NeilBrown9e653b62006-06-26 00:27:58 -07004555 switch(mode) {
4556 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004558 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 goto out;
4560 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004561 break;
4562 case 0: /* disassemble */
4563 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004564 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004565 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (mddev->ro)
4567 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004570 mddev->queue->merge_bvec_fn = NULL;
4571 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004572 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 module_put(mddev->pers->owner);
NeilBrowna64c8762010-04-14 17:15:37 +10004574 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4575 mddev->to_remove = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004577 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004578 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004579
NeilBrownc4647292009-05-07 12:51:06 +10004580 list_for_each_entry(rdev, &mddev->disks, same_set)
4581 if (rdev->raid_disk >= 0) {
4582 char nm[20];
4583 sprintf(nm, "rd%d", rdev->raid_disk);
4584 sysfs_remove_link(&mddev->kobj, nm);
4585 }
4586
NeilBrown0d4ca602006-12-10 02:20:44 -08004587 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004588 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (mddev->ro)
4591 mddev->ro = 0;
4592 }
NeilBrown850b2b42006-10-03 01:15:46 -07004593 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 /* mark array as shutdown cleanly */
4595 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004596 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
NeilBrown9e653b62006-06-26 00:27:58 -07004598 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004600 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004601 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004603out:
4604 mutex_unlock(&mddev->open_mutex);
4605 if (err)
4606 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 /*
4608 * Free resources if final stop
4609 */
NeilBrown9e653b62006-06-26 00:27:58 -07004610 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4613
NeilBrown978f9462006-02-02 14:28:05 -08004614 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004615 if (mddev->bitmap_info.file) {
4616 restore_bitmap_write_access(mddev->bitmap_info.file);
4617 fput(mddev->bitmap_info.file);
4618 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004619 }
NeilBrownc3d97142009-12-14 12:49:52 +11004620 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 export_array(mddev);
4623
Andre Nollf233ea52008-07-21 17:05:22 +10004624 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004625 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004626 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004627 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004628 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004629 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004630 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004631 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004632 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004633 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004634 mddev->level = LEVEL_NONE;
4635 mddev->clevel[0] = 0;
4636 mddev->flags = 0;
4637 mddev->ro = 0;
4638 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004639 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004640 mddev->ctime = mddev->utime = 0;
4641 mddev->layout = 0;
4642 mddev->max_disks = 0;
4643 mddev->events = 0;
4644 mddev->delta_disks = 0;
4645 mddev->new_level = LEVEL_NONE;
4646 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004647 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004648 mddev->curr_resync = 0;
4649 mddev->resync_mismatches = 0;
4650 mddev->suspend_lo = mddev->suspend_hi = 0;
4651 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4652 mddev->recovery = 0;
4653 mddev->in_sync = 0;
4654 mddev->changed = 0;
4655 mddev->degraded = 0;
4656 mddev->barriers_work = 0;
4657 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004658 mddev->bitmap_info.offset = 0;
4659 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004660 mddev->bitmap_info.chunksize = 0;
4661 mddev->bitmap_info.daemon_sleep = 0;
4662 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004663 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004664 if (mddev->hold_active == UNTIL_STOP)
4665 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004666
NeilBrowna8a55c32006-06-26 00:27:39 -07004667 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4669 mdname(mddev));
4670 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004671 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004672 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004673 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 return err;
4675}
4676
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004677#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678static void autorun_array(mddev_t *mddev)
4679{
4680 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 int err;
4682
NeilBrowna757e642005-04-16 15:26:42 -07004683 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685
4686 printk(KERN_INFO "md: running: ");
4687
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004688 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 char b[BDEVNAME_SIZE];
4690 printk("<%s>", bdevname(rdev->bdev,b));
4691 }
4692 printk("\n");
4693
NeilBrownd710e132008-10-13 11:55:12 +11004694 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 if (err) {
4696 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004697 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 }
4699}
4700
4701/*
4702 * lets try to run arrays based on all disks that have arrived
4703 * until now. (those are in pending_raid_disks)
4704 *
4705 * the method: pick the first pending disk, collect all disks with
4706 * the same UUID, remove all from the pending list and put them into
4707 * the 'same_array' list. Then order this list based on superblock
4708 * update time (freshest comes first), kick out 'old' disks and
4709 * compare superblocks. If everything's fine then run it.
4710 *
4711 * If "unit" is allocated, then bump its reference count
4712 */
4713static void autorun_devices(int part)
4714{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004715 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 mddev_t *mddev;
4717 char b[BDEVNAME_SIZE];
4718
4719 printk(KERN_INFO "md: autorun ...\n");
4720 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004721 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004723 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 rdev0 = list_entry(pending_raid_disks.next,
4725 mdk_rdev_t, same_set);
4726
4727 printk(KERN_INFO "md: considering %s ...\n",
4728 bdevname(rdev0->bdev,b));
4729 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004730 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 if (super_90_load(rdev, rdev0, 0) >= 0) {
4732 printk(KERN_INFO "md: adding %s ...\n",
4733 bdevname(rdev->bdev,b));
4734 list_move(&rdev->same_set, &candidates);
4735 }
4736 /*
4737 * now we have a set of devices, with all of them having
4738 * mostly sane superblocks. It's time to allocate the
4739 * mddev.
4740 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004741 if (part) {
4742 dev = MKDEV(mdp_major,
4743 rdev0->preferred_minor << MdpMinorShift);
4744 unit = MINOR(dev) >> MdpMinorShift;
4745 } else {
4746 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4747 unit = MINOR(dev);
4748 }
4749 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4751 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4752 break;
4753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754
4755 md_probe(dev, NULL, NULL);
4756 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004757 if (!mddev || !mddev->gendisk) {
4758 if (mddev)
4759 mddev_put(mddev);
4760 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 "md: cannot allocate memory for md drive.\n");
4762 break;
4763 }
4764 if (mddev_lock(mddev))
4765 printk(KERN_WARNING "md: %s locked, cannot run\n",
4766 mdname(mddev));
4767 else if (mddev->raid_disks || mddev->major_version
4768 || !list_empty(&mddev->disks)) {
4769 printk(KERN_WARNING
4770 "md: %s already running, cannot run %s\n",
4771 mdname(mddev), bdevname(rdev0->bdev,b));
4772 mddev_unlock(mddev);
4773 } else {
4774 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004775 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004776 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 list_del_init(&rdev->same_set);
4778 if (bind_rdev_to_array(rdev, mddev))
4779 export_rdev(rdev);
4780 }
4781 autorun_array(mddev);
4782 mddev_unlock(mddev);
4783 }
4784 /* on success, candidates will be empty, on error
4785 * it won't...
4786 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004787 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004788 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 mddev_put(mddev);
4792 }
4793 printk(KERN_INFO "md: ... autorun DONE.\n");
4794}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004795#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797static int get_version(void __user * arg)
4798{
4799 mdu_version_t ver;
4800
4801 ver.major = MD_MAJOR_VERSION;
4802 ver.minor = MD_MINOR_VERSION;
4803 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4804
4805 if (copy_to_user(arg, &ver, sizeof(ver)))
4806 return -EFAULT;
4807
4808 return 0;
4809}
4810
4811static int get_array_info(mddev_t * mddev, void __user * arg)
4812{
4813 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004814 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816
NeilBrowna9f326e2009-09-23 18:06:41 +10004817 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004818 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004820 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 failed++;
4822 else {
4823 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004824 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004825 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 else
4827 spare++;
4828 }
4829 }
4830
4831 info.major_version = mddev->major_version;
4832 info.minor_version = mddev->minor_version;
4833 info.patch_version = MD_PATCHLEVEL_VERSION;
4834 info.ctime = mddev->ctime;
4835 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004836 info.size = mddev->dev_sectors / 2;
4837 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004838 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 info.nr_disks = nr;
4840 info.raid_disks = mddev->raid_disks;
4841 info.md_minor = mddev->md_minor;
4842 info.not_persistent= !mddev->persistent;
4843
4844 info.utime = mddev->utime;
4845 info.state = 0;
4846 if (mddev->in_sync)
4847 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004848 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004849 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004850 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851 info.working_disks = working;
4852 info.failed_disks = failed;
4853 info.spare_disks = spare;
4854
4855 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004856 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
4858 if (copy_to_user(arg, &info, sizeof(info)))
4859 return -EFAULT;
4860
4861 return 0;
4862}
4863
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004864static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004865{
4866 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4867 char *ptr, *buf = NULL;
4868 int err = -ENOMEM;
4869
Dan Williamsb5470dc2008-06-27 21:44:04 -07004870 if (md_allow_write(mddev))
4871 file = kmalloc(sizeof(*file), GFP_NOIO);
4872 else
4873 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004874
NeilBrown32a76272005-06-21 17:17:14 -07004875 if (!file)
4876 goto out;
4877
4878 /* bitmap disabled, zero the first byte and copy out */
4879 if (!mddev->bitmap || !mddev->bitmap->file) {
4880 file->pathname[0] = '\0';
4881 goto copy_out;
4882 }
4883
4884 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4885 if (!buf)
4886 goto out;
4887
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004888 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4889 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004890 goto out;
4891
4892 strcpy(file->pathname, ptr);
4893
4894copy_out:
4895 err = 0;
4896 if (copy_to_user(arg, file, sizeof(*file)))
4897 err = -EFAULT;
4898out:
4899 kfree(buf);
4900 kfree(file);
4901 return err;
4902}
4903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904static int get_disk_info(mddev_t * mddev, void __user * arg)
4905{
4906 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 mdk_rdev_t *rdev;
4908
4909 if (copy_from_user(&info, arg, sizeof(info)))
4910 return -EFAULT;
4911
Andre Noll26ef3792008-07-11 22:02:21 +10004912 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 if (rdev) {
4914 info.major = MAJOR(rdev->bdev->bd_dev);
4915 info.minor = MINOR(rdev->bdev->bd_dev);
4916 info.raid_disk = rdev->raid_disk;
4917 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004918 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004920 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 info.state |= (1<<MD_DISK_ACTIVE);
4922 info.state |= (1<<MD_DISK_SYNC);
4923 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004924 if (test_bit(WriteMostly, &rdev->flags))
4925 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 } else {
4927 info.major = info.minor = 0;
4928 info.raid_disk = -1;
4929 info.state = (1<<MD_DISK_REMOVED);
4930 }
4931
4932 if (copy_to_user(arg, &info, sizeof(info)))
4933 return -EFAULT;
4934
4935 return 0;
4936}
4937
4938static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4939{
4940 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4941 mdk_rdev_t *rdev;
4942 dev_t dev = MKDEV(info->major,info->minor);
4943
4944 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4945 return -EOVERFLOW;
4946
4947 if (!mddev->raid_disks) {
4948 int err;
4949 /* expecting a device which has a superblock */
4950 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4951 if (IS_ERR(rdev)) {
4952 printk(KERN_WARNING
4953 "md: md_import_device returned %ld\n",
4954 PTR_ERR(rdev));
4955 return PTR_ERR(rdev);
4956 }
4957 if (!list_empty(&mddev->disks)) {
4958 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4959 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004960 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 .load_super(rdev, rdev0, mddev->minor_version);
4962 if (err < 0) {
4963 printk(KERN_WARNING
4964 "md: %s has different UUID to %s\n",
4965 bdevname(rdev->bdev,b),
4966 bdevname(rdev0->bdev,b2));
4967 export_rdev(rdev);
4968 return -EINVAL;
4969 }
4970 }
4971 err = bind_rdev_to_array(rdev, mddev);
4972 if (err)
4973 export_rdev(rdev);
4974 return err;
4975 }
4976
4977 /*
4978 * add_new_disk can be used once the array is assembled
4979 * to add "hot spares". They must already have a superblock
4980 * written
4981 */
4982 if (mddev->pers) {
4983 int err;
4984 if (!mddev->pers->hot_add_disk) {
4985 printk(KERN_WARNING
4986 "%s: personality does not support diskops!\n",
4987 mdname(mddev));
4988 return -EINVAL;
4989 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07004990 if (mddev->persistent)
4991 rdev = md_import_device(dev, mddev->major_version,
4992 mddev->minor_version);
4993 else
4994 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (IS_ERR(rdev)) {
4996 printk(KERN_WARNING
4997 "md: md_import_device returned %ld\n",
4998 PTR_ERR(rdev));
4999 return PTR_ERR(rdev);
5000 }
NeilBrown41158c72005-06-21 17:17:25 -07005001 /* set save_raid_disk if appropriate */
5002 if (!mddev->persistent) {
5003 if (info->state & (1<<MD_DISK_SYNC) &&
5004 info->raid_disk < mddev->raid_disks)
5005 rdev->raid_disk = info->raid_disk;
5006 else
5007 rdev->raid_disk = -1;
5008 } else
5009 super_types[mddev->major_version].
5010 validate_super(mddev, rdev);
5011 rdev->saved_raid_disk = rdev->raid_disk;
5012
NeilBrownb2d444d2005-11-08 21:39:31 -08005013 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005014 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5015 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005016 else
5017 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005018
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 rdev->raid_disk = -1;
5020 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005021 if (!err && !mddev->pers->hot_remove_disk) {
5022 /* If there is hot_add_disk but no hot_remove_disk
5023 * then added disks for geometry changes,
5024 * and should be added immediately.
5025 */
5026 super_types[mddev->major_version].
5027 validate_super(mddev, rdev);
5028 err = mddev->pers->hot_add_disk(mddev, rdev);
5029 if (err)
5030 unbind_rdev_from_array(rdev);
5031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 if (err)
5033 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005034 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005035 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005036
NeilBrown17571282006-12-10 02:20:52 -08005037 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005038 if (mddev->degraded)
5039 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005040 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005041 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 return err;
5043 }
5044
5045 /* otherwise, add_new_disk is only allowed
5046 * for major_version==0 superblocks
5047 */
5048 if (mddev->major_version != 0) {
5049 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5050 mdname(mddev));
5051 return -EINVAL;
5052 }
5053
5054 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5055 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005056 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 if (IS_ERR(rdev)) {
5058 printk(KERN_WARNING
5059 "md: error, md_import_device() returned %ld\n",
5060 PTR_ERR(rdev));
5061 return PTR_ERR(rdev);
5062 }
5063 rdev->desc_nr = info->number;
5064 if (info->raid_disk < mddev->raid_disks)
5065 rdev->raid_disk = info->raid_disk;
5066 else
5067 rdev->raid_disk = -1;
5068
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005070 if (info->state & (1<<MD_DISK_SYNC))
5071 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005073 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5074 set_bit(WriteMostly, &rdev->flags);
5075
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 if (!mddev->persistent) {
5077 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005078 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 } else
Andre Noll0f420352008-07-11 22:02:23 +10005080 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005081 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
NeilBrown2bf071b2006-01-06 00:20:55 -08005083 err = bind_rdev_to_array(rdev, mddev);
5084 if (err) {
5085 export_rdev(rdev);
5086 return err;
5087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 }
5089
5090 return 0;
5091}
5092
5093static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5094{
5095 char b[BDEVNAME_SIZE];
5096 mdk_rdev_t *rdev;
5097
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 rdev = find_rdev(mddev, dev);
5099 if (!rdev)
5100 return -ENXIO;
5101
5102 if (rdev->raid_disk >= 0)
5103 goto busy;
5104
5105 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005106 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005107 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108
5109 return 0;
5110busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005111 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 bdevname(rdev->bdev,b), mdname(mddev));
5113 return -EBUSY;
5114}
5115
5116static int hot_add_disk(mddev_t * mddev, dev_t dev)
5117{
5118 char b[BDEVNAME_SIZE];
5119 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 mdk_rdev_t *rdev;
5121
5122 if (!mddev->pers)
5123 return -ENODEV;
5124
5125 if (mddev->major_version != 0) {
5126 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5127 " version-0 superblocks.\n",
5128 mdname(mddev));
5129 return -EINVAL;
5130 }
5131 if (!mddev->pers->hot_add_disk) {
5132 printk(KERN_WARNING
5133 "%s: personality does not support diskops!\n",
5134 mdname(mddev));
5135 return -EINVAL;
5136 }
5137
NeilBrownd710e132008-10-13 11:55:12 +11005138 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 if (IS_ERR(rdev)) {
5140 printk(KERN_WARNING
5141 "md: error, md_import_device() returned %ld\n",
5142 PTR_ERR(rdev));
5143 return -EINVAL;
5144 }
5145
5146 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005147 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 else
Andre Noll0f420352008-07-11 22:02:23 +10005149 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150
NeilBrown8190e752009-06-18 08:48:58 +10005151 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152
NeilBrownb2d444d2005-11-08 21:39:31 -08005153 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 printk(KERN_WARNING
5155 "md: can not hot-add faulty %s disk to %s!\n",
5156 bdevname(rdev->bdev,b), mdname(mddev));
5157 err = -EINVAL;
5158 goto abort_export;
5159 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005160 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005162 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005163 err = bind_rdev_to_array(rdev, mddev);
5164 if (err)
5165 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166
5167 /*
5168 * The rest should better be atomic, we can have disk failures
5169 * noticed in interrupt contexts ...
5170 */
5171
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 rdev->raid_disk = -1;
5173
NeilBrown850b2b42006-10-03 01:15:46 -07005174 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175
5176 /*
5177 * Kick recovery, maybe this spare has to be added to the
5178 * array immediately.
5179 */
5180 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5181 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005182 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 return 0;
5184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185abort_export:
5186 export_rdev(rdev);
5187 return err;
5188}
5189
NeilBrown32a76272005-06-21 17:17:14 -07005190static int set_bitmap_file(mddev_t *mddev, int fd)
5191{
5192 int err;
5193
NeilBrown36fa3062005-09-09 16:23:45 -07005194 if (mddev->pers) {
5195 if (!mddev->pers->quiesce)
5196 return -EBUSY;
5197 if (mddev->recovery || mddev->sync_thread)
5198 return -EBUSY;
5199 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005200 }
5201
NeilBrown36fa3062005-09-09 16:23:45 -07005202
5203 if (fd >= 0) {
5204 if (mddev->bitmap)
5205 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005206 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005207
NeilBrownc3d97142009-12-14 12:49:52 +11005208 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005209 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5210 mdname(mddev));
5211 return -EBADF;
5212 }
5213
NeilBrownc3d97142009-12-14 12:49:52 +11005214 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005215 if (err) {
5216 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5217 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005218 fput(mddev->bitmap_info.file);
5219 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005220 return err;
5221 }
NeilBrownc3d97142009-12-14 12:49:52 +11005222 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005223 } else if (mddev->bitmap == NULL)
5224 return -ENOENT; /* cannot remove what isn't there */
5225 err = 0;
5226 if (mddev->pers) {
5227 mddev->pers->quiesce(mddev, 1);
5228 if (fd >= 0)
5229 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005230 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005231 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005232 fd = -1; /* make sure to put the file */
5233 }
NeilBrown36fa3062005-09-09 16:23:45 -07005234 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005235 }
5236 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005237 if (mddev->bitmap_info.file) {
5238 restore_bitmap_write_access(mddev->bitmap_info.file);
5239 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005240 }
NeilBrownc3d97142009-12-14 12:49:52 +11005241 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005242 }
5243
NeilBrown32a76272005-06-21 17:17:14 -07005244 return err;
5245}
5246
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247/*
5248 * set_array_info is used two different ways
5249 * The original usage is when creating a new array.
5250 * In this usage, raid_disks is > 0 and it together with
5251 * level, size, not_persistent,layout,chunksize determine the
5252 * shape of the array.
5253 * This will always create an array with a type-0.90.0 superblock.
5254 * The newer usage is when assembling an array.
5255 * In this case raid_disks will be 0, and the major_version field is
5256 * use to determine which style super-blocks are to be found on the devices.
5257 * The minor and patch _version numbers are also kept incase the
5258 * super_block handler wishes to interpret them.
5259 */
5260static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5261{
5262
5263 if (info->raid_disks == 0) {
5264 /* just setting version number for superblock loading */
5265 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005266 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 super_types[info->major_version].name == NULL) {
5268 /* maybe try to auto-load a module? */
5269 printk(KERN_INFO
5270 "md: superblock version %d not known\n",
5271 info->major_version);
5272 return -EINVAL;
5273 }
5274 mddev->major_version = info->major_version;
5275 mddev->minor_version = info->minor_version;
5276 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005277 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005278 /* ensure mddev_put doesn't delete this now that there
5279 * is some minimal configuration.
5280 */
5281 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 return 0;
5283 }
5284 mddev->major_version = MD_MAJOR_VERSION;
5285 mddev->minor_version = MD_MINOR_VERSION;
5286 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5287 mddev->ctime = get_seconds();
5288
5289 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005290 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005291 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 mddev->raid_disks = info->raid_disks;
5293 /* don't set md_minor, it is determined by which /dev/md* was
5294 * openned
5295 */
5296 if (info->state & (1<<MD_SB_CLEAN))
5297 mddev->recovery_cp = MaxSector;
5298 else
5299 mddev->recovery_cp = 0;
5300 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005301 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302
5303 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005304 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
5306 mddev->max_disks = MD_SB_DISKS;
5307
NeilBrowne6910632008-02-06 01:39:51 -08005308 if (mddev->persistent)
5309 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005310 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311
NeilBrownc3d97142009-12-14 12:49:52 +11005312 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5313 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005314
NeilBrownf6705572006-03-27 01:18:11 -08005315 mddev->reshape_position = MaxSector;
5316
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 /*
5318 * Generate a 128 bit UUID
5319 */
5320 get_random_bytes(mddev->uuid, 16);
5321
NeilBrownf6705572006-03-27 01:18:11 -08005322 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005323 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005324 mddev->new_layout = mddev->layout;
5325 mddev->delta_disks = 0;
5326
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 return 0;
5328}
5329
Dan Williams1f403622009-03-31 14:59:03 +11005330void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5331{
Dan Williamsb522adc2009-03-31 15:00:31 +11005332 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5333
5334 if (mddev->external_size)
5335 return;
5336
Dan Williams1f403622009-03-31 14:59:03 +11005337 mddev->array_sectors = array_sectors;
5338}
5339EXPORT_SYMBOL(md_set_array_sectors);
5340
Andre Nolld71f9f82008-07-11 22:02:22 +10005341static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005342{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005343 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005344 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005345 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005346
5347 if (mddev->pers->resize == NULL)
5348 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005349 /* The "num_sectors" is the number of sectors of each device that
5350 * is used. This can only make sense for arrays with redundancy.
5351 * linear and raid0 always use whatever space is available. We can only
5352 * consider changing this number if no resync or reconstruction is
5353 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005354 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005355 * of each device. If num_sectors is zero, we find the largest size
5356 * that fits.
5357
NeilBrowna35b0d62006-01-06 00:20:49 -08005358 */
5359 if (mddev->sync_thread)
5360 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005361 if (mddev->bitmap)
5362 /* Sorry, cannot grow a bitmap yet, just remove it,
5363 * grow, and re-add.
5364 */
5365 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005366 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005367 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005368
Andre Nolld71f9f82008-07-11 22:02:22 +10005369 if (fit && (num_sectors == 0 || num_sectors > avail))
5370 num_sectors = avail;
5371 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005372 return -ENOSPC;
5373 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005374 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005375 if (!rv)
5376 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005377 return rv;
5378}
5379
NeilBrownda943b992006-01-06 00:20:54 -08005380static int update_raid_disks(mddev_t *mddev, int raid_disks)
5381{
5382 int rv;
5383 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005384 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005385 return -EINVAL;
5386 if (raid_disks <= 0 ||
5387 raid_disks >= mddev->max_disks)
5388 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005389 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005390 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005391 mddev->delta_disks = raid_disks - mddev->raid_disks;
5392
5393 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005394 return rv;
5395}
5396
5397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398/*
5399 * update_array_info is used to change the configuration of an
5400 * on-line array.
5401 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5402 * fields in the info are checked against the array.
5403 * Any differences that cannot be handled will cause an error.
5404 * Normally, only one change can be managed at a time.
5405 */
5406static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5407{
5408 int rv = 0;
5409 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005410 int state = 0;
5411
5412 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005413 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005414 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415
5416 if (mddev->major_version != info->major_version ||
5417 mddev->minor_version != info->minor_version ||
5418/* mddev->patch_version != info->patch_version || */
5419 mddev->ctime != info->ctime ||
5420 mddev->level != info->level ||
5421/* mddev->layout != info->layout || */
5422 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005423 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005424 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5425 ((state^info->state) & 0xfffffe00)
5426 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 return -EINVAL;
5428 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005429 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5430 cnt++;
5431 if (mddev->raid_disks != info->raid_disks)
5432 cnt++;
5433 if (mddev->layout != info->layout)
5434 cnt++;
5435 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5436 cnt++;
5437 if (cnt == 0)
5438 return 0;
5439 if (cnt > 1)
5440 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441
5442 if (mddev->layout != info->layout) {
5443 /* Change layout
5444 * we don't need to do anything at the md level, the
5445 * personality will take care of it all.
5446 */
NeilBrown50ac1682009-06-18 08:47:55 +10005447 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005449 else {
5450 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005451 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005452 if (rv)
5453 mddev->new_layout = mddev->layout;
5454 return rv;
5455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005457 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005458 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459
NeilBrownda943b992006-01-06 00:20:54 -08005460 if (mddev->raid_disks != info->raid_disks)
5461 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462
NeilBrown36fa3062005-09-09 16:23:45 -07005463 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5464 if (mddev->pers->quiesce == NULL)
5465 return -EINVAL;
5466 if (mddev->recovery || mddev->sync_thread)
5467 return -EBUSY;
5468 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5469 /* add the bitmap */
5470 if (mddev->bitmap)
5471 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005472 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005473 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005474 mddev->bitmap_info.offset =
5475 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005476 mddev->pers->quiesce(mddev, 1);
5477 rv = bitmap_create(mddev);
5478 if (rv)
5479 bitmap_destroy(mddev);
5480 mddev->pers->quiesce(mddev, 0);
5481 } else {
5482 /* remove the bitmap */
5483 if (!mddev->bitmap)
5484 return -ENOENT;
5485 if (mddev->bitmap->file)
5486 return -EINVAL;
5487 mddev->pers->quiesce(mddev, 1);
5488 bitmap_destroy(mddev);
5489 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005490 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005491 }
5492 }
NeilBrown850b2b42006-10-03 01:15:46 -07005493 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 return rv;
5495}
5496
5497static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5498{
5499 mdk_rdev_t *rdev;
5500
5501 if (mddev->pers == NULL)
5502 return -ENODEV;
5503
5504 rdev = find_rdev(mddev, dev);
5505 if (!rdev)
5506 return -ENODEV;
5507
5508 md_error(mddev, rdev);
5509 return 0;
5510}
5511
Andre Noll2f9618c2008-04-25 18:57:58 +02005512/*
5513 * We have a problem here : there is no easy way to give a CHS
5514 * virtual geometry. We currently pretend that we have a 2 heads
5515 * 4 sectors (with a BIG number of cylinders...). This drives
5516 * dosfs just mad... ;-)
5517 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005518static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5519{
5520 mddev_t *mddev = bdev->bd_disk->private_data;
5521
5522 geo->heads = 2;
5523 geo->sectors = 4;
5524 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5525 return 0;
5526}
5527
Al Viroa39907f2008-03-02 10:31:15 -05005528static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 unsigned int cmd, unsigned long arg)
5530{
5531 int err = 0;
5532 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005534 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535
5536 if (!capable(CAP_SYS_ADMIN))
5537 return -EACCES;
5538
5539 /*
5540 * Commands dealing with the RAID driver but not any
5541 * particular array:
5542 */
5543 switch (cmd)
5544 {
5545 case RAID_VERSION:
5546 err = get_version(argp);
5547 goto done;
5548
5549 case PRINT_RAID_DEBUG:
5550 err = 0;
5551 md_print_devices();
5552 goto done;
5553
5554#ifndef MODULE
5555 case RAID_AUTORUN:
5556 err = 0;
5557 autostart_arrays(arg);
5558 goto done;
5559#endif
5560 default:;
5561 }
5562
5563 /*
5564 * Commands creating/starting a new array:
5565 */
5566
Al Viroa39907f2008-03-02 10:31:15 -05005567 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
5569 if (!mddev) {
5570 BUG();
5571 goto abort;
5572 }
5573
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 err = mddev_lock(mddev);
5575 if (err) {
5576 printk(KERN_INFO
5577 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5578 err, cmd);
5579 goto abort;
5580 }
5581
5582 switch (cmd)
5583 {
5584 case SET_ARRAY_INFO:
5585 {
5586 mdu_array_info_t info;
5587 if (!arg)
5588 memset(&info, 0, sizeof(info));
5589 else if (copy_from_user(&info, argp, sizeof(info))) {
5590 err = -EFAULT;
5591 goto abort_unlock;
5592 }
5593 if (mddev->pers) {
5594 err = update_array_info(mddev, &info);
5595 if (err) {
5596 printk(KERN_WARNING "md: couldn't update"
5597 " array info. %d\n", err);
5598 goto abort_unlock;
5599 }
5600 goto done_unlock;
5601 }
5602 if (!list_empty(&mddev->disks)) {
5603 printk(KERN_WARNING
5604 "md: array %s already has disks!\n",
5605 mdname(mddev));
5606 err = -EBUSY;
5607 goto abort_unlock;
5608 }
5609 if (mddev->raid_disks) {
5610 printk(KERN_WARNING
5611 "md: array %s already initialised!\n",
5612 mdname(mddev));
5613 err = -EBUSY;
5614 goto abort_unlock;
5615 }
5616 err = set_array_info(mddev, &info);
5617 if (err) {
5618 printk(KERN_WARNING "md: couldn't set"
5619 " array info. %d\n", err);
5620 goto abort_unlock;
5621 }
5622 }
5623 goto done_unlock;
5624
5625 default:;
5626 }
5627
5628 /*
5629 * Commands querying/configuring an existing array:
5630 */
NeilBrown32a76272005-06-21 17:17:14 -07005631 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005632 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005633 if ((!mddev->raid_disks && !mddev->external)
5634 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5635 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5636 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 err = -ENODEV;
5638 goto abort_unlock;
5639 }
5640
5641 /*
5642 * Commands even a read-only array can execute:
5643 */
5644 switch (cmd)
5645 {
5646 case GET_ARRAY_INFO:
5647 err = get_array_info(mddev, argp);
5648 goto done_unlock;
5649
NeilBrown32a76272005-06-21 17:17:14 -07005650 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005651 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005652 goto done_unlock;
5653
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 case GET_DISK_INFO:
5655 err = get_disk_info(mddev, argp);
5656 goto done_unlock;
5657
5658 case RESTART_ARRAY_RW:
5659 err = restart_array(mddev);
5660 goto done_unlock;
5661
5662 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005663 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 goto done_unlock;
5665
5666 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005667 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 goto done_unlock;
5669
Dan Williamse2218352010-05-12 08:25:37 +10005670 case BLKROSET:
5671 if (get_user(ro, (int __user *)(arg))) {
5672 err = -EFAULT;
5673 goto done_unlock;
5674 }
5675 err = -EINVAL;
5676
5677 /* if the bdev is going readonly the value of mddev->ro
5678 * does not matter, no writes are coming
5679 */
5680 if (ro)
5681 goto done_unlock;
5682
5683 /* are we are already prepared for writes? */
5684 if (mddev->ro != 1)
5685 goto done_unlock;
5686
5687 /* transitioning to readauto need only happen for
5688 * arrays that call md_write_start
5689 */
5690 if (mddev->pers) {
5691 err = restart_array(mddev);
5692 if (err == 0) {
5693 mddev->ro = 2;
5694 set_disk_ro(mddev->gendisk, 0);
5695 }
5696 }
5697 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 }
5699
5700 /*
5701 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005702 * superblock, so we do not allow them on read-only arrays.
5703 * However non-MD ioctls (e.g. get-size) will still come through
5704 * here and hit the 'default' below, so only disallow
5705 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005707 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005708 if (mddev->ro == 2) {
5709 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005710 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005711 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5712 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005713 } else {
5714 err = -EROFS;
5715 goto abort_unlock;
5716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 }
5718
5719 switch (cmd)
5720 {
5721 case ADD_NEW_DISK:
5722 {
5723 mdu_disk_info_t info;
5724 if (copy_from_user(&info, argp, sizeof(info)))
5725 err = -EFAULT;
5726 else
5727 err = add_new_disk(mddev, &info);
5728 goto done_unlock;
5729 }
5730
5731 case HOT_REMOVE_DISK:
5732 err = hot_remove_disk(mddev, new_decode_dev(arg));
5733 goto done_unlock;
5734
5735 case HOT_ADD_DISK:
5736 err = hot_add_disk(mddev, new_decode_dev(arg));
5737 goto done_unlock;
5738
5739 case SET_DISK_FAULTY:
5740 err = set_disk_faulty(mddev, new_decode_dev(arg));
5741 goto done_unlock;
5742
5743 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005744 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 goto done_unlock;
5746
NeilBrown32a76272005-06-21 17:17:14 -07005747 case SET_BITMAP_FILE:
5748 err = set_bitmap_file(mddev, (int)arg);
5749 goto done_unlock;
5750
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 err = -EINVAL;
5753 goto abort_unlock;
5754 }
5755
5756done_unlock:
5757abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005758 if (mddev->hold_active == UNTIL_IOCTL &&
5759 err != -EINVAL)
5760 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 mddev_unlock(mddev);
5762
5763 return err;
5764done:
5765 if (err)
5766 MD_BUG();
5767abort:
5768 return err;
5769}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005770#ifdef CONFIG_COMPAT
5771static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5772 unsigned int cmd, unsigned long arg)
5773{
5774 switch (cmd) {
5775 case HOT_REMOVE_DISK:
5776 case HOT_ADD_DISK:
5777 case SET_DISK_FAULTY:
5778 case SET_BITMAP_FILE:
5779 /* These take in integer arg, do not convert */
5780 break;
5781 default:
5782 arg = (unsigned long)compat_ptr(arg);
5783 break;
5784 }
5785
5786 return md_ioctl(bdev, mode, cmd, arg);
5787}
5788#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789
Al Viroa39907f2008-03-02 10:31:15 -05005790static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791{
5792 /*
5793 * Succeed if we can lock the mddev, which confirms that
5794 * it isn't being stopped right now.
5795 */
NeilBrownd3374822009-01-09 08:31:10 +11005796 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 int err;
5798
NeilBrownd3374822009-01-09 08:31:10 +11005799 if (mddev->gendisk != bdev->bd_disk) {
5800 /* we are racing with mddev_put which is discarding this
5801 * bd_disk.
5802 */
5803 mddev_put(mddev);
5804 /* Wait until bdev->bd_disk is definitely gone */
5805 flush_scheduled_work();
5806 /* Then retry the open from the top */
5807 return -ERESTARTSYS;
5808 }
5809 BUG_ON(mddev != bdev->bd_disk->private_data);
5810
NeilBrownc8c00a62009-08-10 12:50:52 +10005811 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 goto out;
5813
5814 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005815 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005816 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
Al Viroa39907f2008-03-02 10:31:15 -05005818 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 out:
5820 return err;
5821}
5822
Al Viroa39907f2008-03-02 10:31:15 -05005823static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824{
Al Viroa39907f2008-03-02 10:31:15 -05005825 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005827 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005828 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005829 mddev_put(mddev);
5830
5831 return 0;
5832}
5833
Linus Torvalds44ce62942007-05-09 18:51:36 -07005834static int md_media_changed(struct gendisk *disk)
5835{
5836 mddev_t *mddev = disk->private_data;
5837
5838 return mddev->changed;
5839}
5840
5841static int md_revalidate(struct gendisk *disk)
5842{
5843 mddev_t *mddev = disk->private_data;
5844
5845 mddev->changed = 0;
5846 return 0;
5847}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005848static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005849{
5850 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005851 .open = md_open,
5852 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005853 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005854#ifdef CONFIG_COMPAT
5855 .compat_ioctl = md_compat_ioctl,
5856#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005857 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005858 .media_changed = md_media_changed,
5859 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005860};
5861
Adrian Bunk75c96f82005-05-05 16:16:09 -07005862static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863{
5864 mdk_thread_t *thread = arg;
5865
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 /*
5867 * md_thread is a 'system-thread', it's priority should be very
5868 * high. We avoid resource deadlocks individually in each
5869 * raid personality. (RAID5 does preallocation) We also use RR and
5870 * the very same RT priority as kswapd, thus we will never get
5871 * into a priority inversion deadlock.
5872 *
5873 * we definitely have to have equal or higher priority than
5874 * bdflush, otherwise bdflush will deadlock if there are too
5875 * many dirty RAID5 blocks.
5876 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005877
NeilBrown6985c432005-10-19 21:23:47 -07005878 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005879 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880
NeilBrown93588e22005-11-15 00:09:12 -08005881 /* We need to wait INTERRUPTIBLE so that
5882 * we don't add to the load-average.
5883 * That means we need to be sure no signals are
5884 * pending
5885 */
5886 if (signal_pending(current))
5887 flush_signals(current);
5888
5889 wait_event_interruptible_timeout
5890 (thread->wqueue,
5891 test_bit(THREAD_WAKEUP, &thread->flags)
5892 || kthread_should_stop(),
5893 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894
5895 clear_bit(THREAD_WAKEUP, &thread->flags);
5896
NeilBrown787453c2005-11-08 21:39:43 -08005897 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005899
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 return 0;
5901}
5902
5903void md_wakeup_thread(mdk_thread_t *thread)
5904{
5905 if (thread) {
5906 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5907 set_bit(THREAD_WAKEUP, &thread->flags);
5908 wake_up(&thread->wqueue);
5909 }
5910}
5911
5912mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5913 const char *name)
5914{
5915 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916
NeilBrown9ffae0c2006-01-06 00:20:32 -08005917 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 if (!thread)
5919 return NULL;
5920
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 init_waitqueue_head(&thread->wqueue);
5922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 thread->run = run;
5924 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005925 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005926 thread->tsk = kthread_run(md_thread, thread,
5927 "%s_%s",
5928 mdname(thread->mddev),
5929 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005930 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 kfree(thread);
5932 return NULL;
5933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 return thread;
5935}
5936
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937void md_unregister_thread(mdk_thread_t *thread)
5938{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005939 if (!thread)
5940 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005941 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005942
5943 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 kfree(thread);
5945}
5946
5947void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5948{
5949 if (!mddev) {
5950 MD_BUG();
5951 return;
5952 }
5953
NeilBrownb2d444d2005-11-08 21:39:31 -08005954 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005956
5957 if (mddev->external)
5958 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005959/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5961 mdname(mddev),
5962 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5963 __builtin_return_address(0),__builtin_return_address(1),
5964 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005965*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005966 if (!mddev->pers)
5967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 if (!mddev->pers->error_handler)
5969 return;
5970 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005971 if (mddev->degraded)
5972 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11005973 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5975 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5976 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005977 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978}
5979
5980/* seq_file implementation /proc/mdstat */
5981
5982static void status_unused(struct seq_file *seq)
5983{
5984 int i = 0;
5985 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986
5987 seq_printf(seq, "unused devices: ");
5988
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005989 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 char b[BDEVNAME_SIZE];
5991 i++;
5992 seq_printf(seq, "%s ",
5993 bdevname(rdev->bdev,b));
5994 }
5995 if (!i)
5996 seq_printf(seq, "<none>");
5997
5998 seq_printf(seq, "\n");
5999}
6000
6001
6002static void status_resync(struct seq_file *seq, mddev_t * mddev)
6003{
NeilBrowndd71cf62009-05-07 12:49:35 +10006004 sector_t max_sectors, resync, res;
6005 unsigned long dt, db;
6006 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006007 int scale;
6008 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
NeilBrowndd71cf62009-05-07 12:49:35 +10006010 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011
6012 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006013 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006015 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016
6017 /*
6018 * Should not happen.
6019 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006020 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 MD_BUG();
6022 return;
6023 }
NeilBrown4588b422006-03-27 01:18:04 -08006024 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006025 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006026 * u32, as those are the requirements for sector_div.
6027 * Thus 'scale' must be at least 10
6028 */
6029 scale = 10;
6030 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006031 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006032 scale++;
6033 }
6034 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006035 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006036
6037 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 {
NeilBrown4588b422006-03-27 01:18:04 -08006039 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040 seq_printf(seq, "[");
6041 for (i = 0; i < x; i++)
6042 seq_printf(seq, "=");
6043 seq_printf(seq, ">");
6044 for (i = 0; i < y; i++)
6045 seq_printf(seq, ".");
6046 seq_printf(seq, "] ");
6047 }
NeilBrown4588b422006-03-27 01:18:04 -08006048 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006049 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6050 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006051 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6052 "check" :
6053 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6054 "resync" : "recovery"))),
6055 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006056 (unsigned long long) resync/2,
6057 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058
6059 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006060 * dt: time from mark until now
6061 * db: blocks written from mark until now
6062 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006063 *
6064 * rt is a sector_t, so could be 32bit or 64bit.
6065 * So we divide before multiply in case it is 32bit and close
6066 * to the limit.
6067 * We scale the divisor (db) by 32 to avoid loosing precision
6068 * near the end of resync when the number of remaining sectors
6069 * is close to 'db'.
6070 * We then divide rt by 32 after multiplying by db to compensate.
6071 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 */
6073 dt = ((jiffies - mddev->resync_mark) / HZ);
6074 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006075 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6076 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077
NeilBrowndd71cf62009-05-07 12:49:35 +10006078 rt = max_sectors - resync; /* number of remaining sectors */
6079 sector_div(rt, db/32+1);
6080 rt *= dt;
6081 rt >>= 5;
6082
6083 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6084 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006085
NeilBrownff4e8d92006-07-10 04:44:16 -07006086 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087}
6088
6089static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6090{
6091 struct list_head *tmp;
6092 loff_t l = *pos;
6093 mddev_t *mddev;
6094
6095 if (l >= 0x10000)
6096 return NULL;
6097 if (!l--)
6098 /* header */
6099 return (void*)1;
6100
6101 spin_lock(&all_mddevs_lock);
6102 list_for_each(tmp,&all_mddevs)
6103 if (!l--) {
6104 mddev = list_entry(tmp, mddev_t, all_mddevs);
6105 mddev_get(mddev);
6106 spin_unlock(&all_mddevs_lock);
6107 return mddev;
6108 }
6109 spin_unlock(&all_mddevs_lock);
6110 if (!l--)
6111 return (void*)2;/* tail */
6112 return NULL;
6113}
6114
6115static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6116{
6117 struct list_head *tmp;
6118 mddev_t *next_mddev, *mddev = v;
6119
6120 ++*pos;
6121 if (v == (void*)2)
6122 return NULL;
6123
6124 spin_lock(&all_mddevs_lock);
6125 if (v == (void*)1)
6126 tmp = all_mddevs.next;
6127 else
6128 tmp = mddev->all_mddevs.next;
6129 if (tmp != &all_mddevs)
6130 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6131 else {
6132 next_mddev = (void*)2;
6133 *pos = 0x10000;
6134 }
6135 spin_unlock(&all_mddevs_lock);
6136
6137 if (v != (void*)1)
6138 mddev_put(mddev);
6139 return next_mddev;
6140
6141}
6142
6143static void md_seq_stop(struct seq_file *seq, void *v)
6144{
6145 mddev_t *mddev = v;
6146
6147 if (mddev && v != (void*)1 && v != (void*)2)
6148 mddev_put(mddev);
6149}
6150
NeilBrownd7603b72006-01-06 00:20:30 -08006151struct mdstat_info {
6152 int event;
6153};
6154
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155static int md_seq_show(struct seq_file *seq, void *v)
6156{
6157 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006158 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006159 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006160 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006161 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006162
6163 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006164 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165 seq_printf(seq, "Personalities : ");
6166 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006167 list_for_each_entry(pers, &pers_list, list)
6168 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169
6170 spin_unlock(&pers_lock);
6171 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006172 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 return 0;
6174 }
6175 if (v == (void*)2) {
6176 status_unused(seq);
6177 return 0;
6178 }
6179
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006180 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006181 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006182
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6184 seq_printf(seq, "%s : %sactive", mdname(mddev),
6185 mddev->pers ? "" : "in");
6186 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006187 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006189 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006190 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191 seq_printf(seq, " %s", mddev->pers->name);
6192 }
6193
Andre Nolldd8ac332009-03-31 14:33:13 +11006194 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006195 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 char b[BDEVNAME_SIZE];
6197 seq_printf(seq, " %s[%d]",
6198 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006199 if (test_bit(WriteMostly, &rdev->flags))
6200 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006201 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 seq_printf(seq, "(F)");
6203 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006204 } else if (rdev->raid_disk < 0)
6205 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006206 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 }
6208
6209 if (!list_empty(&mddev->disks)) {
6210 if (mddev->pers)
6211 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006212 (unsigned long long)
6213 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 else
6215 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006216 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006218 if (mddev->persistent) {
6219 if (mddev->major_version != 0 ||
6220 mddev->minor_version != 90) {
6221 seq_printf(seq," super %d.%d",
6222 mddev->major_version,
6223 mddev->minor_version);
6224 }
NeilBrowne6910632008-02-06 01:39:51 -08006225 } else if (mddev->external)
6226 seq_printf(seq, " super external:%s",
6227 mddev->metadata_type);
6228 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006229 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230
6231 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006232 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006234 if (mddev->pers->sync_request) {
6235 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006236 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006237 seq_printf(seq, "\n ");
6238 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6239 seq_printf(seq, "\tresync=DELAYED\n ");
6240 else if (mddev->recovery_cp < MaxSector)
6241 seq_printf(seq, "\tresync=PENDING\n ");
6242 }
NeilBrown32a76272005-06-21 17:17:14 -07006243 } else
6244 seq_printf(seq, "\n ");
6245
6246 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006247 unsigned long chunk_kb;
6248 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006249 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006250 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006251 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6252 "%lu%s chunk",
6253 bitmap->pages - bitmap->missing_pages,
6254 bitmap->pages,
6255 (bitmap->pages - bitmap->missing_pages)
6256 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006257 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006258 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006259 if (bitmap->file) {
6260 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006261 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006262 }
NeilBrown78d742d2005-06-21 17:17:15 -07006263
NeilBrown32a76272005-06-21 17:17:14 -07006264 seq_printf(seq, "\n");
6265 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 }
6267
6268 seq_printf(seq, "\n");
6269 }
6270 mddev_unlock(mddev);
6271
6272 return 0;
6273}
6274
Jan Engelhardt110518b2009-05-07 12:49:37 +10006275static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 .start = md_seq_start,
6277 .next = md_seq_next,
6278 .stop = md_seq_stop,
6279 .show = md_seq_show,
6280};
6281
6282static int md_seq_open(struct inode *inode, struct file *file)
6283{
6284 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006285 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6286 if (mi == NULL)
6287 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288
6289 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006290 if (error)
6291 kfree(mi);
6292 else {
6293 struct seq_file *p = file->private_data;
6294 p->private = mi;
6295 mi->event = atomic_read(&md_event_count);
6296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297 return error;
6298}
6299
NeilBrownd7603b72006-01-06 00:20:30 -08006300static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6301{
6302 struct seq_file *m = filp->private_data;
6303 struct mdstat_info *mi = m->private;
6304 int mask;
6305
6306 poll_wait(filp, &md_event_waiters, wait);
6307
6308 /* always allow read */
6309 mask = POLLIN | POLLRDNORM;
6310
6311 if (mi->event != atomic_read(&md_event_count))
6312 mask |= POLLERR | POLLPRI;
6313 return mask;
6314}
6315
Arjan van de Venfa027c22007-02-12 00:55:33 -08006316static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006317 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 .open = md_seq_open,
6319 .read = seq_read,
6320 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006321 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006322 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323};
6324
NeilBrown2604b702006-01-06 00:20:36 -08006325int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006328 list_add_tail(&p->list, &pers_list);
6329 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 spin_unlock(&pers_lock);
6331 return 0;
6332}
6333
NeilBrown2604b702006-01-06 00:20:36 -08006334int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335{
NeilBrown2604b702006-01-06 00:20:36 -08006336 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006338 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339 spin_unlock(&pers_lock);
6340 return 0;
6341}
6342
NeilBrowneea1bf32009-03-31 14:27:02 +11006343static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006344{
6345 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006347 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348
6349 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006350 rcu_read_lock();
6351 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006353 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6354 (int)part_stat_read(&disk->part0, sectors[1]) -
6355 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006356 /* sync IO will cause sync_io to increase before the disk_stats
6357 * as sync_io is counted when a request starts, and
6358 * disk_stats is counted when it completes.
6359 * So resync activity will cause curr_events to be smaller than
6360 * when there was no such activity.
6361 * non-sync IO will cause disk_stat to increase without
6362 * increasing sync_io so curr_events will (eventually)
6363 * be larger than it was before. Once it becomes
6364 * substantially larger, the test below will cause
6365 * the array to appear non-idle, and resync will slow
6366 * down.
6367 * If there is a lot of outstanding resync activity when
6368 * we set last_event to curr_events, then all that activity
6369 * completing might cause the array to appear non-idle
6370 * and resync will be slowed down even though there might
6371 * not have been non-resync activity. This will only
6372 * happen once though. 'last_events' will soon reflect
6373 * the state where there is little or no outstanding
6374 * resync requests, and further resync activity will
6375 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006376 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006378 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 rdev->last_events = curr_events;
6380 idle = 0;
6381 }
6382 }
NeilBrown4b809912008-07-21 17:05:25 +10006383 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 return idle;
6385}
6386
6387void md_done_sync(mddev_t *mddev, int blocks, int ok)
6388{
6389 /* another "blocks" (512byte) blocks have been synced */
6390 atomic_sub(blocks, &mddev->recovery_active);
6391 wake_up(&mddev->recovery_wait);
6392 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006393 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 md_wakeup_thread(mddev->thread);
6395 // stop recovery, signal do_sync ....
6396 }
6397}
6398
6399
NeilBrown06d91a52005-06-21 17:17:12 -07006400/* md_write_start(mddev, bi)
6401 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006402 * in superblock) before writing, schedule a superblock update
6403 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006404 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006405void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406{
Neil Brown0fd62b82008-06-28 08:31:36 +10006407 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006408 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006409 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006410
NeilBrownf91de922005-11-08 21:39:36 -08006411 BUG_ON(mddev->ro == 1);
6412 if (mddev->ro == 2) {
6413 /* need to switch to read/write */
6414 mddev->ro = 0;
6415 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6416 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006417 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006418 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006419 }
NeilBrown06d91a52005-06-21 17:17:12 -07006420 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006421 if (mddev->safemode == 1)
6422 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006423 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006424 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006425 if (mddev->in_sync) {
6426 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006427 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006428 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006429 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006430 }
NeilBrowna9701a32005-11-08 21:39:34 -08006431 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006432 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006433 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006434 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006435 wait_event(mddev->sb_wait,
6436 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6437 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438}
6439
6440void md_write_end(mddev_t *mddev)
6441{
6442 if (atomic_dec_and_test(&mddev->writes_pending)) {
6443 if (mddev->safemode == 2)
6444 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006445 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6447 }
6448}
6449
NeilBrown2a2275d2007-01-26 00:57:11 -08006450/* md_allow_write(mddev)
6451 * Calling this ensures that the array is marked 'active' so that writes
6452 * may proceed without blocking. It is important to call this before
6453 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6454 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006455 *
6456 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6457 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006458 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006459int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006460{
6461 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006462 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006463 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006464 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006465 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006466 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006467
6468 spin_lock_irq(&mddev->write_lock);
6469 if (mddev->in_sync) {
6470 mddev->in_sync = 0;
6471 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6472 if (mddev->safemode_delay &&
6473 mddev->safemode == 0)
6474 mddev->safemode = 1;
6475 spin_unlock_irq(&mddev->write_lock);
6476 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006477 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006478 } else
6479 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006480
6481 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6482 return -EAGAIN;
6483 else
6484 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006485}
6486EXPORT_SYMBOL_GPL(md_allow_write);
6487
Linus Torvalds1da177e2005-04-16 15:20:36 -07006488#define SYNC_MARKS 10
6489#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006490void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491{
6492 mddev_t *mddev2;
6493 unsigned int currspeed = 0,
6494 window;
NeilBrown57afd892005-06-21 17:17:13 -07006495 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 unsigned long mark[SYNC_MARKS];
6497 sector_t mark_cnt[SYNC_MARKS];
6498 int last_mark,m;
6499 struct list_head *tmp;
6500 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006501 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006502 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006503 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504
6505 /* just incase thread restarts... */
6506 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6507 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006508 if (mddev->ro) /* never try to sync a read-only array */
6509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006510
NeilBrown61df9d92006-10-03 01:15:57 -07006511 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6512 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6513 desc = "data-check";
6514 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6515 desc = "requested-resync";
6516 else
6517 desc = "resync";
6518 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6519 desc = "reshape";
6520 else
6521 desc = "recovery";
6522
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523 /* we overload curr_resync somewhat here.
6524 * 0 == not engaged in resync at all
6525 * 2 == checking that there is no conflict with another sync
6526 * 1 == like 2, but have yielded to allow conflicting resync to
6527 * commense
6528 * other == active in resync - this many blocks
6529 *
6530 * Before starting a resync we must have set curr_resync to
6531 * 2, and then checked that every "conflicting" array has curr_resync
6532 * less than ours. When we find one that is the same or higher
6533 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6534 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6535 * This will mean we have to start checking from the beginning again.
6536 *
6537 */
6538
6539 do {
6540 mddev->curr_resync = 2;
6541
6542 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006543 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006544 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006545
6546 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006548 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549 if (mddev2 == mddev)
6550 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006551 if (!mddev->parallel_resync
6552 && mddev2->curr_resync
6553 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554 DEFINE_WAIT(wq);
6555 if (mddev < mddev2 && mddev->curr_resync == 2) {
6556 /* arbitrarily yield */
6557 mddev->curr_resync = 1;
6558 wake_up(&resync_wait);
6559 }
6560 if (mddev > mddev2 && mddev->curr_resync == 1)
6561 /* no need to wait here, we can wait the next
6562 * time 'round when curr_resync == 2
6563 */
6564 continue;
NeilBrown97441972008-09-19 11:49:54 +10006565 /* We need to wait 'interruptible' so as not to
6566 * contribute to the load average, and not to
6567 * be caught by 'softlockup'
6568 */
6569 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006570 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006571 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006572 printk(KERN_INFO "md: delaying %s of %s"
6573 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006575 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006577 if (signal_pending(current))
6578 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 schedule();
6580 finish_wait(&resync_wait, &wq);
6581 goto try_again;
6582 }
6583 finish_wait(&resync_wait, &wq);
6584 }
6585 }
6586 } while (mddev->curr_resync < 2);
6587
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006588 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006589 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006591 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006592 */
6593 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006594 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006595 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006596 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6597 j = mddev->resync_min;
6598 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006599 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006600
NeilBrownccfcc3c2006-03-27 01:18:09 -08006601 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006602 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006603 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006605 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006606 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006607 rcu_read_lock();
6608 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006609 if (rdev->raid_disk >= 0 &&
6610 !test_bit(Faulty, &rdev->flags) &&
6611 !test_bit(In_sync, &rdev->flags) &&
6612 rdev->recovery_offset < j)
6613 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006614 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616
NeilBrown61df9d92006-10-03 01:15:57 -07006617 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6618 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6619 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006620 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006621 "(but not more than %d KB/sec) for %s.\n",
6622 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623
NeilBrowneea1bf32009-03-31 14:27:02 +11006624 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006625
NeilBrown57afd892005-06-21 17:17:13 -07006626 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 for (m = 0; m < SYNC_MARKS; m++) {
6628 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006629 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 }
6631 last_mark = 0;
6632 mddev->resync_mark = mark[last_mark];
6633 mddev->resync_mark_cnt = mark_cnt[last_mark];
6634
6635 /*
6636 * Tune reconstruction:
6637 */
6638 window = 32*(PAGE_SIZE/512);
6639 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6640 window/2,(unsigned long long) max_sectors/2);
6641
6642 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 last_check = 0;
6644
6645 if (j>2) {
6646 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006647 "md: resuming %s of %s from checkpoint.\n",
6648 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 mddev->curr_resync = j;
6650 }
NeilBrownefa59332009-12-14 12:49:49 +11006651 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652
6653 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006654 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
NeilBrown57afd892005-06-21 17:17:13 -07006656 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006657
NeilBrown7a91ee12009-05-26 12:57:21 +10006658 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6659 ((mddev->curr_resync > mddev->curr_resync_completed &&
6660 (mddev->curr_resync - mddev->curr_resync_completed)
6661 > (max_sectors >> 4)) ||
6662 (j - mddev->curr_resync_completed)*2
6663 >= mddev->resync_max - mddev->curr_resync_completed
6664 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006665 /* time to update curr_resync_completed */
6666 blk_unplug(mddev->queue);
6667 wait_event(mddev->recovery_wait,
6668 atomic_read(&mddev->recovery_active) == 0);
6669 mddev->curr_resync_completed =
6670 mddev->curr_resync;
6671 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006672 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006673 }
NeilBrownacb180b2009-04-14 16:28:34 +10006674
NeilBrowne62e58a2009-07-01 13:15:35 +10006675 while (j >= mddev->resync_max && !kthread_should_stop()) {
6676 /* As this condition is controlled by user-space,
6677 * we can block indefinitely, so use '_interruptible'
6678 * to avoid triggering warnings.
6679 */
6680 flush_signals(current); /* just in case */
6681 wait_event_interruptible(mddev->recovery_wait,
6682 mddev->resync_max > j
6683 || kthread_should_stop());
6684 }
NeilBrownacb180b2009-04-14 16:28:34 +10006685
6686 if (kthread_should_stop())
6687 goto interrupted;
6688
NeilBrown57afd892005-06-21 17:17:13 -07006689 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006690 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006691 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006692 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693 goto out;
6694 }
NeilBrown57afd892005-06-21 17:17:13 -07006695
6696 if (!skipped) { /* actual IO requested */
6697 io_sectors += sectors;
6698 atomic_add(sectors, &mddev->recovery_active);
6699 }
6700
Linus Torvalds1da177e2005-04-16 15:20:36 -07006701 j += sectors;
6702 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006703 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006704 if (last_check == 0)
6705 /* this is the earliers that rebuilt will be
6706 * visible in /proc/mdstat
6707 */
6708 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006709
6710 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 continue;
6712
NeilBrown57afd892005-06-21 17:17:13 -07006713 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006714
NeilBrowndfc70642008-05-23 13:04:39 -07006715 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 break;
6717
6718 repeat:
6719 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6720 /* step marks */
6721 int next = (last_mark+1) % SYNC_MARKS;
6722
6723 mddev->resync_mark = mark[next];
6724 mddev->resync_mark_cnt = mark_cnt[next];
6725 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006726 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727 last_mark = next;
6728 }
6729
6730
NeilBrownc6207272008-02-06 01:39:52 -08006731 if (kthread_should_stop())
6732 goto interrupted;
6733
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734
6735 /*
6736 * this loop exits only if either when we are slower than
6737 * the 'hard' speed limit, or the system was IO-idle for
6738 * a jiffy.
6739 * the system might be non-idle CPU-wise, but we only care
6740 * about not overloading the IO subsystem. (things like an
6741 * e2fsck being done on the RAID array should execute fast)
6742 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006743 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 cond_resched();
6745
NeilBrown57afd892005-06-21 17:17:13 -07006746 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6747 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748
NeilBrown88202a02006-01-06 00:21:36 -08006749 if (currspeed > speed_min(mddev)) {
6750 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006751 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006752 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 goto repeat;
6754 }
6755 }
6756 }
NeilBrown61df9d92006-10-03 01:15:57 -07006757 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758 /*
6759 * this also signals 'finished resyncing' to md_stop
6760 */
6761 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006762 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763
6764 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6765
6766 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006767 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
NeilBrowndfc70642008-05-23 13:04:39 -07006769 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006770 mddev->curr_resync > 2) {
6771 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6772 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6773 if (mddev->curr_resync >= mddev->recovery_cp) {
6774 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006775 "md: checkpointing %s of %s.\n",
6776 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006777 mddev->recovery_cp = mddev->curr_resync;
6778 }
6779 } else
6780 mddev->recovery_cp = MaxSector;
6781 } else {
6782 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6783 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006784 rcu_read_lock();
6785 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006786 if (rdev->raid_disk >= 0 &&
6787 !test_bit(Faulty, &rdev->flags) &&
6788 !test_bit(In_sync, &rdev->flags) &&
6789 rdev->recovery_offset < mddev->curr_resync)
6790 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006791 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 }
NeilBrown17571282006-12-10 02:20:52 -08006794 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006795
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006797 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6798 /* We completed so min/max setting can be forgotten if used. */
6799 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6800 mddev->resync_min = 0;
6801 mddev->resync_max = MaxSector;
6802 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6803 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006804 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006805 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6806 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006807 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 wake_up(&resync_wait);
6809 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6810 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006811 return;
6812
6813 interrupted:
6814 /*
6815 * got a signal, exit.
6816 */
6817 printk(KERN_INFO
6818 "md: md_do_sync() got signal ... exiting\n");
6819 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6820 goto out;
6821
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822}
NeilBrown29269552006-03-27 01:18:10 -08006823EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006824
6825
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006826static int remove_and_add_spares(mddev_t *mddev)
6827{
6828 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006829 int spares = 0;
6830
NeilBrown97e4f422009-03-31 14:33:13 +11006831 mddev->curr_resync_completed = 0;
6832
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006833 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006834 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006835 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006836 (test_bit(Faulty, &rdev->flags) ||
6837 ! test_bit(In_sync, &rdev->flags)) &&
6838 atomic_read(&rdev->nr_pending)==0) {
6839 if (mddev->pers->hot_remove_disk(
6840 mddev, rdev->raid_disk)==0) {
6841 char nm[20];
6842 sprintf(nm,"rd%d", rdev->raid_disk);
6843 sysfs_remove_link(&mddev->kobj, nm);
6844 rdev->raid_disk = -1;
6845 }
6846 }
6847
NeilBrown4044ba52009-01-09 08:31:11 +11006848 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006849 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006850 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006851 !test_bit(In_sync, &rdev->flags) &&
6852 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006853 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006854 if (rdev->raid_disk < 0
6855 && !test_bit(Faulty, &rdev->flags)) {
6856 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006857 if (mddev->pers->
6858 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006859 char nm[20];
6860 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006861 if (sysfs_create_link(&mddev->kobj,
6862 &rdev->kobj, nm))
6863 printk(KERN_WARNING
6864 "md: cannot register "
6865 "%s for %s\n",
6866 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006867 spares++;
6868 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006869 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006870 } else
6871 break;
6872 }
NeilBrowndfc70642008-05-23 13:04:39 -07006873 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006874 }
6875 return spares;
6876}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877/*
6878 * This routine is regularly called by all per-raid-array threads to
6879 * deal with generic issues like resync and super-block update.
6880 * Raid personalities that don't have a thread (linear/raid0) do not
6881 * need this as they never do any recovery or update the superblock.
6882 *
6883 * It does not do any resync itself, but rather "forks" off other threads
6884 * to do that as needed.
6885 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6886 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006887 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006888 * and wakeups up this thread which will reap the thread and finish up.
6889 * This thread also removes any faulty devices (with nr_pending == 0).
6890 *
6891 * The overall approach is:
6892 * 1/ if the superblock needs updating, update it.
6893 * 2/ If a recovery thread is running, don't do anything else.
6894 * 3/ If recovery has finished, clean up, possibly marking spares active.
6895 * 4/ If there are any faulty devices, remove them.
6896 * 5/ If array is degraded, try to add spares devices
6897 * 6/ If array has spares or is not in-sync, start a resync thread.
6898 */
6899void md_check_recovery(mddev_t *mddev)
6900{
6901 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902
6903
NeilBrown5f404022005-06-21 17:17:16 -07006904 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006905 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906
6907 if (mddev->ro)
6908 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006909
6910 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006911 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006912 printk(KERN_INFO "md: %s in immediate safe mode\n",
6913 mdname(mddev));
6914 mddev->safemode = 2;
6915 }
6916 flush_signals(current);
6917 }
6918
NeilBrownc89a8ee2008-08-05 15:54:13 +10006919 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6920 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006922 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006924 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006925 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006926 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6927 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006928 ))
6929 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006930
NeilBrowndf5b89b2006-03-27 01:18:20 -08006931 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006932 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006933
NeilBrownc89a8ee2008-08-05 15:54:13 +10006934 if (mddev->ro) {
6935 /* Only thing we do on a ro array is remove
6936 * failed devices.
6937 */
6938 remove_and_add_spares(mddev);
6939 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6940 goto unlock;
6941 }
6942
NeilBrown31a59e32008-04-30 00:52:30 -07006943 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006944 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006945 spin_lock_irq(&mddev->write_lock);
6946 if (mddev->safemode &&
6947 !atomic_read(&mddev->writes_pending) &&
6948 !mddev->in_sync &&
6949 mddev->recovery_cp == MaxSector) {
6950 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006951 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006952 if (mddev->persistent)
6953 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6954 }
6955 if (mddev->safemode == 1)
6956 mddev->safemode = 0;
6957 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006958 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006959 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006960 }
NeilBrownfca4d842005-06-21 17:17:11 -07006961
NeilBrown850b2b42006-10-03 01:15:46 -07006962 if (mddev->flags)
6963 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006964
Linus Torvalds1da177e2005-04-16 15:20:36 -07006965 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6966 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6967 /* resync/recovery still happening */
6968 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6969 goto unlock;
6970 }
6971 if (mddev->sync_thread) {
6972 /* resync has finished, collect result */
6973 md_unregister_thread(mddev->sync_thread);
6974 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006975 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6976 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 /* success...*/
6978 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006979 if (mddev->pers->spare_active(mddev))
6980 sysfs_notify(&mddev->kobj, NULL,
6981 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 }
NeilBrowncea9c222009-03-31 15:15:05 +11006983 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6984 mddev->pers->finish_reshape)
6985 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07006986 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07006987
6988 /* if array is no-longer degraded, then any saved_raid_disk
6989 * information must be scrapped
6990 */
6991 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006992 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07006993 rdev->saved_raid_disk = -1;
6994
Linus Torvalds1da177e2005-04-16 15:20:36 -07006995 mddev->recovery = 0;
6996 /* flag recovery needed just to double check */
6997 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11006998 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08006999 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 goto unlock;
7001 }
Neil Brown72a23c22008-06-28 08:31:41 +10007002 /* Set RUNNING before clearing NEEDED to avoid
7003 * any transients in the value of "sync_action".
7004 */
7005 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7006 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007007 /* Clear some bits that don't mean anything, but
7008 * might be left set
7009 */
NeilBrown24dd4692005-11-08 21:39:26 -08007010 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7011 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007013 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7014 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 /* no recovery is running.
7016 * remove any failed drives, then
7017 * add spares if possible.
7018 * Spare are also removed and re-added, to allow
7019 * the personality to fail the re-add.
7020 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007022 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007023 if (mddev->pers->check_reshape == NULL ||
7024 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007025 /* Cannot proceed */
7026 goto unlock;
7027 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007028 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007029 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007030 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7031 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007032 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007033 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007034 } else if (mddev->recovery_cp < MaxSector) {
7035 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007036 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007037 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7038 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007040
Linus Torvalds1da177e2005-04-16 15:20:36 -07007041 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007042 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7043 /* We are adding a device or devices to an array
7044 * which has the bitmap stored on all devices.
7045 * So make sure all bitmap pages get written
7046 */
7047 bitmap_write_all(mddev->bitmap);
7048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 mddev->sync_thread = md_register_thread(md_do_sync,
7050 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007051 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007052 if (!mddev->sync_thread) {
7053 printk(KERN_ERR "%s: could not start resync"
7054 " thread...\n",
7055 mdname(mddev));
7056 /* leave the spares where they are, it shouldn't hurt */
7057 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007058 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007059 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007060 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007061 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 }
7063 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007064 if (!mddev->sync_thread) {
7065 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7066 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7067 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007068 if (mddev->sysfs_action)
7069 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 mddev_unlock(mddev);
7072 }
7073}
7074
Dan Williams6bfe0b42008-04-30 00:52:32 -07007075void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7076{
NeilBrown3c0ee632008-10-21 13:25:28 +11007077 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007078 wait_event_timeout(rdev->blocked_wait,
7079 !test_bit(Blocked, &rdev->flags),
7080 msecs_to_jiffies(5000));
7081 rdev_dec_pending(rdev, mddev);
7082}
7083EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7084
Adrian Bunk75c96f82005-05-05 16:16:09 -07007085static int md_notify_reboot(struct notifier_block *this,
7086 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087{
7088 struct list_head *tmp;
7089 mddev_t *mddev;
7090
7091 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7092
7093 printk(KERN_INFO "md: stopping all md devices.\n");
7094
NeilBrown29ac4aa2008-02-06 01:39:58 -08007095 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007096 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007097 /* Force a switch to readonly even array
7098 * appears to still be in use. Hence
7099 * the '100'.
7100 */
NeilBrownd710e132008-10-13 11:55:12 +11007101 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007102 mddev_unlock(mddev);
7103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007104 /*
7105 * certain more exotic SCSI devices are known to be
7106 * volatile wrt too early system reboots. While the
7107 * right place to handle this issue is the given
7108 * driver, we do want to have a safe RAID driver ...
7109 */
7110 mdelay(1000*1);
7111 }
7112 return NOTIFY_DONE;
7113}
7114
Adrian Bunk75c96f82005-05-05 16:16:09 -07007115static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 .notifier_call = md_notify_reboot,
7117 .next = NULL,
7118 .priority = INT_MAX, /* before any real devices */
7119};
7120
7121static void md_geninit(void)
7122{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007123 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7124
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007125 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007126}
7127
Adrian Bunk75c96f82005-05-05 16:16:09 -07007128static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007129{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007130 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 return -1;
7132 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007133 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007134 return -1;
7135 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007136 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007137 md_probe, NULL, NULL);
7138 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 md_probe, NULL, NULL);
7140
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007142 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007143
7144 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146}
7147
7148
7149#ifndef MODULE
7150
7151/*
7152 * Searches all registered partitions for autorun RAID arrays
7153 * at boot time.
7154 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007155
7156static LIST_HEAD(all_detected_devices);
7157struct detected_devices_node {
7158 struct list_head list;
7159 dev_t dev;
7160};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161
7162void md_autodetect_dev(dev_t dev)
7163{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007164 struct detected_devices_node *node_detected_dev;
7165
7166 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7167 if (node_detected_dev) {
7168 node_detected_dev->dev = dev;
7169 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7170 } else {
7171 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7172 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174}
7175
7176
7177static void autostart_arrays(int part)
7178{
7179 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007180 struct detected_devices_node *node_detected_dev;
7181 dev_t dev;
7182 int i_scanned, i_passed;
7183
7184 i_scanned = 0;
7185 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007186
7187 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7188
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007189 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7190 i_scanned++;
7191 node_detected_dev = list_entry(all_detected_devices.next,
7192 struct detected_devices_node, list);
7193 list_del(&node_detected_dev->list);
7194 dev = node_detected_dev->dev;
7195 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007196 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197 if (IS_ERR(rdev))
7198 continue;
7199
NeilBrownb2d444d2005-11-08 21:39:31 -08007200 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201 MD_BUG();
7202 continue;
7203 }
NeilBrownd0fae182008-03-04 14:29:31 -08007204 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007205 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007206 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007207 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007208
7209 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7210 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007211
7212 autorun_devices(part);
7213}
7214
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007215#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216
7217static __exit void md_exit(void)
7218{
7219 mddev_t *mddev;
7220 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007221
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007222 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007223 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007225 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226 unregister_blkdev(mdp_major, "mdp");
7227 unregister_reboot_notifier(&md_notifier);
7228 unregister_sysctl_table(raid_table_header);
7229 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007230 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007232 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007233 }
7234}
7235
Dan Williams685784a2007-07-09 11:56:42 -07007236subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237module_exit(md_exit)
7238
NeilBrownf91de922005-11-08 21:39:36 -08007239static int get_ro(char *buffer, struct kernel_param *kp)
7240{
7241 return sprintf(buffer, "%d", start_readonly);
7242}
7243static int set_ro(const char *val, struct kernel_param *kp)
7244{
7245 char *e;
7246 int num = simple_strtoul(val, &e, 10);
7247 if (*val && (*e == '\0' || *e == '\n')) {
7248 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007249 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007250 }
7251 return -EINVAL;
7252}
7253
NeilBrown80ca3a42006-07-10 04:44:18 -07007254module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7255module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007256
NeilBrownefeb53c2009-01-09 08:31:10 +11007257module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007258
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259EXPORT_SYMBOL(register_md_personality);
7260EXPORT_SYMBOL(unregister_md_personality);
7261EXPORT_SYMBOL(md_error);
7262EXPORT_SYMBOL(md_done_sync);
7263EXPORT_SYMBOL(md_write_start);
7264EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265EXPORT_SYMBOL(md_register_thread);
7266EXPORT_SYMBOL(md_unregister_thread);
7267EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268EXPORT_SYMBOL(md_check_recovery);
7269MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007270MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007271MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007272MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);