blob: c5a1b0725c9fc4f7d25405050429fefebd26882f [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);
Maciej Trelab7103102010-04-14 16:58:16 +10002388 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002389 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2390 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002391 } else if (rdev->mddev->pers) {
2392 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002393 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002394 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002395 */
2396
2397 if (rdev->raid_disk != -1)
2398 return -EBUSY;
2399
2400 if (rdev->mddev->pers->hot_add_disk == NULL)
2401 return -EINVAL;
2402
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002403 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002404 if (rdev2->raid_disk == slot)
2405 return -EEXIST;
2406
2407 rdev->raid_disk = slot;
2408 if (test_bit(In_sync, &rdev->flags))
2409 rdev->saved_raid_disk = slot;
2410 else
2411 rdev->saved_raid_disk = -1;
2412 err = rdev->mddev->pers->
2413 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002414 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002415 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002416 return err;
Neil Brown52664732008-06-28 08:31:44 +10002417 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002418 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002419 sprintf(nm, "rd%d", rdev->raid_disk);
2420 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2421 printk(KERN_WARNING
2422 "md: cannot register "
2423 "%s for %s\n",
2424 nm, mdname(rdev->mddev));
2425
2426 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002427 } else {
2428 if (slot >= rdev->mddev->raid_disks)
2429 return -ENOSPC;
2430 rdev->raid_disk = slot;
2431 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002432 clear_bit(Faulty, &rdev->flags);
2433 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002434 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002435 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002436 }
NeilBrown014236d2006-01-06 00:20:55 -08002437 return len;
2438}
2439
2440
2441static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002442__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002443
NeilBrown93c8cad2006-01-06 00:20:56 -08002444static ssize_t
2445offset_show(mdk_rdev_t *rdev, char *page)
2446{
Andrew Morton6961ece2006-01-06 00:20:59 -08002447 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002448}
2449
2450static ssize_t
2451offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2452{
2453 char *e;
2454 unsigned long long offset = simple_strtoull(buf, &e, 10);
2455 if (e==buf || (*e && *e != '\n'))
2456 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002457 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002458 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002459 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002460 /* Must set offset before size, so overlap checks
2461 * can be sane */
2462 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002463 rdev->data_offset = offset;
2464 return len;
2465}
2466
2467static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002468__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002469
NeilBrown83303b62006-01-06 00:21:06 -08002470static ssize_t
2471rdev_size_show(mdk_rdev_t *rdev, char *page)
2472{
Andre Nolldd8ac332009-03-31 14:33:13 +11002473 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002474}
2475
NeilBrownc5d79ad2008-02-06 01:39:54 -08002476static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2477{
2478 /* check if two start/length pairs overlap */
2479 if (s1+l1 <= s2)
2480 return 0;
2481 if (s2+l2 <= s1)
2482 return 0;
2483 return 1;
2484}
2485
Dan Williamsb522adc2009-03-31 15:00:31 +11002486static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2487{
2488 unsigned long long blocks;
2489 sector_t new;
2490
2491 if (strict_strtoull(buf, 10, &blocks) < 0)
2492 return -EINVAL;
2493
2494 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2495 return -EINVAL; /* sector conversion overflow */
2496
2497 new = blocks * 2;
2498 if (new != blocks * 2)
2499 return -EINVAL; /* unsigned long long to sector_t overflow */
2500
2501 *sectors = new;
2502 return 0;
2503}
2504
NeilBrown83303b62006-01-06 00:21:06 -08002505static ssize_t
2506rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2507{
NeilBrown27c529b2008-03-04 14:29:33 -08002508 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002509 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002510 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002511
Dan Williamsb522adc2009-03-31 15:00:31 +11002512 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002513 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002514 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002515 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002516 sectors = super_types[my_mddev->major_version].
2517 rdev_size_change(rdev, sectors);
2518 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002519 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002520 } else if (!sectors)
2521 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2522 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002523 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002524 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002525 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002526
Andre Nolldd8ac332009-03-31 14:33:13 +11002527 rdev->sectors = sectors;
2528 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002529 /* need to check that all other rdevs with the same ->bdev
2530 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002531 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002532 * we have to change it back, we will have the lock again.
2533 */
2534 mddev_t *mddev;
2535 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002536 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002537
NeilBrown27c529b2008-03-04 14:29:33 -08002538 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002539 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002540 mdk_rdev_t *rdev2;
2541
2542 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002543 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002544 if (test_bit(AllReserved, &rdev2->flags) ||
2545 (rdev->bdev == rdev2->bdev &&
2546 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002547 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002548 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002549 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002550 overlap = 1;
2551 break;
2552 }
2553 mddev_unlock(mddev);
2554 if (overlap) {
2555 mddev_put(mddev);
2556 break;
2557 }
2558 }
NeilBrown27c529b2008-03-04 14:29:33 -08002559 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002560 if (overlap) {
2561 /* Someone else could have slipped in a size
2562 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002563 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002564 * safe, and trust userspace not to race with
2565 * itself
2566 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002567 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002568 return -EBUSY;
2569 }
2570 }
NeilBrown83303b62006-01-06 00:21:06 -08002571 return len;
2572}
2573
2574static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002575__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002576
Dan Williams06e3c812009-12-12 21:17:12 -07002577
2578static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2579{
2580 unsigned long long recovery_start = rdev->recovery_offset;
2581
2582 if (test_bit(In_sync, &rdev->flags) ||
2583 recovery_start == MaxSector)
2584 return sprintf(page, "none\n");
2585
2586 return sprintf(page, "%llu\n", recovery_start);
2587}
2588
2589static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2590{
2591 unsigned long long recovery_start;
2592
2593 if (cmd_match(buf, "none"))
2594 recovery_start = MaxSector;
2595 else if (strict_strtoull(buf, 10, &recovery_start))
2596 return -EINVAL;
2597
2598 if (rdev->mddev->pers &&
2599 rdev->raid_disk >= 0)
2600 return -EBUSY;
2601
2602 rdev->recovery_offset = recovery_start;
2603 if (recovery_start == MaxSector)
2604 set_bit(In_sync, &rdev->flags);
2605 else
2606 clear_bit(In_sync, &rdev->flags);
2607 return len;
2608}
2609
2610static struct rdev_sysfs_entry rdev_recovery_start =
2611__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2612
NeilBrown86e6ffd2005-11-08 21:39:24 -08002613static struct attribute *rdev_default_attrs[] = {
2614 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002615 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002616 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002617 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002618 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002619 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002620 NULL,
2621};
2622static ssize_t
2623rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2624{
2625 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2626 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002627 mddev_t *mddev = rdev->mddev;
2628 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002629
2630 if (!entry->show)
2631 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002632
2633 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2634 if (!rv) {
2635 if (rdev->mddev == NULL)
2636 rv = -EBUSY;
2637 else
2638 rv = entry->show(rdev, page);
2639 mddev_unlock(mddev);
2640 }
2641 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002642}
2643
2644static ssize_t
2645rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2646 const char *page, size_t length)
2647{
2648 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2649 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002650 ssize_t rv;
2651 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002652
2653 if (!entry->store)
2654 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002655 if (!capable(CAP_SYS_ADMIN))
2656 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002657 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002658 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002659 if (rdev->mddev == NULL)
2660 rv = -EBUSY;
2661 else
2662 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002663 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002664 }
2665 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002666}
2667
2668static void rdev_free(struct kobject *ko)
2669{
2670 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2671 kfree(rdev);
2672}
2673static struct sysfs_ops rdev_sysfs_ops = {
2674 .show = rdev_attr_show,
2675 .store = rdev_attr_store,
2676};
2677static struct kobj_type rdev_ktype = {
2678 .release = rdev_free,
2679 .sysfs_ops = &rdev_sysfs_ops,
2680 .default_attrs = rdev_default_attrs,
2681};
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*
2684 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2685 *
2686 * mark the device faulty if:
2687 *
2688 * - the device is nonexistent (zero size)
2689 * - the device has no valid superblock
2690 *
2691 * a faulty rdev _never_ has rdev->sb set.
2692 */
2693static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2694{
2695 char b[BDEVNAME_SIZE];
2696 int err;
2697 mdk_rdev_t *rdev;
2698 sector_t size;
2699
NeilBrown9ffae0c2006-01-06 00:20:32 -08002700 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 if (!rdev) {
2702 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2703 return ERR_PTR(-ENOMEM);
2704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 if ((err = alloc_disk_sb(rdev)))
2707 goto abort_free;
2708
NeilBrownc5d79ad2008-02-06 01:39:54 -08002709 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 if (err)
2711 goto abort_free;
2712
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002713 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002716 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002717 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002718 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002720 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002721 rdev->last_read_error.tv_sec = 0;
2722 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002724 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002725 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2728 if (!size) {
2729 printk(KERN_WARNING
2730 "md: %s has zero or unknown size, marking faulty!\n",
2731 bdevname(rdev->bdev,b));
2732 err = -EINVAL;
2733 goto abort_free;
2734 }
2735
2736 if (super_format >= 0) {
2737 err = super_types[super_format].
2738 load_super(rdev, NULL, super_minor);
2739 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002740 printk(KERN_WARNING
2741 "md: %s does not have a valid v%d.%d "
2742 "superblock, not importing!\n",
2743 bdevname(rdev->bdev,b),
2744 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 goto abort_free;
2746 }
2747 if (err < 0) {
2748 printk(KERN_WARNING
2749 "md: could not read %s's sb, not importing!\n",
2750 bdevname(rdev->bdev,b));
2751 goto abort_free;
2752 }
2753 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002756 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
2758 return rdev;
2759
2760abort_free:
2761 if (rdev->sb_page) {
2762 if (rdev->bdev)
2763 unlock_rdev(rdev);
2764 free_disk_sb(rdev);
2765 }
2766 kfree(rdev);
2767 return ERR_PTR(err);
2768}
2769
2770/*
2771 * Check a full RAID array for plausibility
2772 */
2773
2774
NeilBrowna757e642005-04-16 15:26:42 -07002775static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776{
2777 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002778 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 char b[BDEVNAME_SIZE];
2780
2781 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002782 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 switch (super_types[mddev->major_version].
2784 load_super(rdev, freshest, mddev->minor_version)) {
2785 case 1:
2786 freshest = rdev;
2787 break;
2788 case 0:
2789 break;
2790 default:
2791 printk( KERN_ERR \
2792 "md: fatal superblock inconsistency in %s"
2793 " -- removing from array\n",
2794 bdevname(rdev->bdev,b));
2795 kick_rdev_from_array(rdev);
2796 }
2797
2798
2799 super_types[mddev->major_version].
2800 validate_super(mddev, freshest);
2801
2802 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002803 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002804 if (mddev->max_disks &&
2805 (rdev->desc_nr >= mddev->max_disks ||
2806 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002807 printk(KERN_WARNING
2808 "md: %s: %s: only %d devices permitted\n",
2809 mdname(mddev), bdevname(rdev->bdev, b),
2810 mddev->max_disks);
2811 kick_rdev_from_array(rdev);
2812 continue;
2813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (rdev != freshest)
2815 if (super_types[mddev->major_version].
2816 validate_super(mddev, rdev)) {
2817 printk(KERN_WARNING "md: kicking non-fresh %s"
2818 " from array!\n",
2819 bdevname(rdev->bdev,b));
2820 kick_rdev_from_array(rdev);
2821 continue;
2822 }
2823 if (mddev->level == LEVEL_MULTIPATH) {
2824 rdev->desc_nr = i++;
2825 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002826 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002827 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002828 rdev->raid_disk = -1;
2829 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 }
2831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832}
2833
NeilBrown72e02072009-12-14 12:49:55 +11002834/* Read a fixed-point number.
2835 * Numbers in sysfs attributes should be in "standard" units where
2836 * possible, so time should be in seconds.
2837 * However we internally use a a much smaller unit such as
2838 * milliseconds or jiffies.
2839 * This function takes a decimal number with a possible fractional
2840 * component, and produces an integer which is the result of
2841 * multiplying that number by 10^'scale'.
2842 * all without any floating-point arithmetic.
2843 */
2844int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2845{
2846 unsigned long result = 0;
2847 long decimals = -1;
2848 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2849 if (*cp == '.')
2850 decimals = 0;
2851 else if (decimals < scale) {
2852 unsigned int value;
2853 value = *cp - '0';
2854 result = result * 10 + value;
2855 if (decimals >= 0)
2856 decimals++;
2857 }
2858 cp++;
2859 }
2860 if (*cp == '\n')
2861 cp++;
2862 if (*cp)
2863 return -EINVAL;
2864 if (decimals < 0)
2865 decimals = 0;
2866 while (decimals < scale) {
2867 result *= 10;
2868 decimals ++;
2869 }
2870 *res = result;
2871 return 0;
2872}
2873
2874
NeilBrown19052c02008-08-05 15:54:13 +10002875static void md_safemode_timeout(unsigned long data);
2876
NeilBrowneae17012005-11-08 21:39:23 -08002877static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002878safe_delay_show(mddev_t *mddev, char *page)
2879{
2880 int msec = (mddev->safemode_delay*1000)/HZ;
2881 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2882}
2883static ssize_t
2884safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2885{
NeilBrown16f17b32006-06-26 00:27:37 -07002886 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002887
NeilBrown72e02072009-12-14 12:49:55 +11002888 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002889 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002890 if (msec == 0)
2891 mddev->safemode_delay = 0;
2892 else {
NeilBrown19052c02008-08-05 15:54:13 +10002893 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002894 mddev->safemode_delay = (msec*HZ)/1000;
2895 if (mddev->safemode_delay == 0)
2896 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002897 if (mddev->safemode_delay < old_delay)
2898 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002899 }
2900 return len;
2901}
2902static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002903__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002904
2905static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002906level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002907{
NeilBrown2604b702006-01-06 00:20:36 -08002908 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002909 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002910 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002911 else if (mddev->clevel[0])
2912 return sprintf(page, "%s\n", mddev->clevel);
2913 else if (mddev->level != LEVEL_NONE)
2914 return sprintf(page, "%d\n", mddev->level);
2915 else
2916 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002917}
2918
NeilBrownd9d166c2006-01-06 00:20:51 -08002919static ssize_t
2920level_store(mddev_t *mddev, const char *buf, size_t len)
2921{
NeilBrown245f46c2009-03-31 14:39:39 +11002922 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002923 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002924 struct mdk_personality *pers;
2925 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002926 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002927
2928 if (mddev->pers == NULL) {
2929 if (len == 0)
2930 return 0;
2931 if (len >= sizeof(mddev->clevel))
2932 return -ENOSPC;
2933 strncpy(mddev->clevel, buf, len);
2934 if (mddev->clevel[len-1] == '\n')
2935 len--;
2936 mddev->clevel[len] = 0;
2937 mddev->level = LEVEL_NONE;
2938 return rv;
2939 }
2940
2941 /* request to change the personality. Need to ensure:
2942 * - array is not engaged in resync/recovery/reshape
2943 * - old personality can be suspended
2944 * - new personality will access other array.
2945 */
2946
2947 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002948 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002949
2950 if (!mddev->pers->quiesce) {
2951 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2952 mdname(mddev), mddev->pers->name);
2953 return -EINVAL;
2954 }
2955
2956 /* Now find the new personality */
2957 if (len == 0 || len >= sizeof(level))
2958 return -EINVAL;
2959 strncpy(level, buf, len);
2960 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002961 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002962 level[len] = 0;
2963
2964 request_module("md-%s", level);
2965 spin_lock(&pers_lock);
2966 pers = find_pers(LEVEL_NONE, level);
2967 if (!pers || !try_module_get(pers->owner)) {
2968 spin_unlock(&pers_lock);
2969 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2970 return -EINVAL;
2971 }
2972 spin_unlock(&pers_lock);
2973
2974 if (pers == mddev->pers) {
2975 /* Nothing to do! */
2976 module_put(pers->owner);
2977 return rv;
2978 }
2979 if (!pers->takeover) {
2980 module_put(pers->owner);
2981 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2982 mdname(mddev), level);
2983 return -EINVAL;
2984 }
2985
2986 /* ->takeover must set new_* and/or delta_disks
2987 * if it succeeds, and may set them when it fails.
2988 */
2989 priv = pers->takeover(mddev);
2990 if (IS_ERR(priv)) {
2991 mddev->new_level = mddev->level;
2992 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10002993 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11002994 mddev->raid_disks -= mddev->delta_disks;
2995 mddev->delta_disks = 0;
2996 module_put(pers->owner);
2997 printk(KERN_WARNING "md: %s: %s would not accept array\n",
2998 mdname(mddev), level);
2999 return PTR_ERR(priv);
3000 }
3001
3002 /* Looks like we have a winner */
3003 mddev_suspend(mddev);
3004 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003005
3006 if (mddev->pers->sync_request == NULL &&
3007 pers->sync_request != NULL) {
3008 /* need to add the md_redundancy_group */
3009 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3010 printk(KERN_WARNING
3011 "md: cannot register extra attributes for %s\n",
3012 mdname(mddev));
3013 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3014 }
3015 if (mddev->pers->sync_request != NULL &&
3016 pers->sync_request == NULL) {
3017 /* need to remove the md_redundancy_group */
3018 if (mddev->to_remove == NULL)
3019 mddev->to_remove = &md_redundancy_group;
3020 }
3021
Trela Maciej54071b32010-03-08 16:02:42 +11003022 if (mddev->pers->sync_request == NULL &&
3023 mddev->external) {
3024 /* We are converting from a no-redundancy array
3025 * to a redundancy array and metadata is managed
3026 * externally so we need to be sure that writes
3027 * won't block due to a need to transition
3028 * clean->dirty
3029 * until external management is started.
3030 */
3031 mddev->in_sync = 0;
3032 mddev->safemode_delay = 0;
3033 mddev->safemode = 0;
3034 }
3035
NeilBrown245f46c2009-03-31 14:39:39 +11003036 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10003037 /* Invalidate devices that are now superfluous */
3038 list_for_each_entry(rdev, &mddev->disks, same_set)
3039 if (rdev->raid_disk >= mddev->raid_disks) {
3040 rdev->raid_disk = -1;
3041 clear_bit(In_sync, &rdev->flags);
3042 }
NeilBrown245f46c2009-03-31 14:39:39 +11003043 mddev->pers = pers;
3044 mddev->private = priv;
3045 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3046 mddev->level = mddev->new_level;
3047 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003048 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003049 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003050 if (mddev->pers->sync_request == NULL) {
3051 /* this is now an array without redundancy, so
3052 * it must always be in_sync
3053 */
3054 mddev->in_sync = 1;
3055 del_timer_sync(&mddev->safemode_timer);
3056 }
NeilBrown245f46c2009-03-31 14:39:39 +11003057 pers->run(mddev);
3058 mddev_resume(mddev);
3059 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3060 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3061 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003062 sysfs_notify(&mddev->kobj, NULL, "level");
NeilBrownd9d166c2006-01-06 00:20:51 -08003063 return rv;
3064}
3065
3066static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003067__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003068
NeilBrownd4dbd022006-06-26 00:27:59 -07003069
3070static ssize_t
3071layout_show(mddev_t *mddev, char *page)
3072{
3073 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003074 if (mddev->reshape_position != MaxSector &&
3075 mddev->layout != mddev->new_layout)
3076 return sprintf(page, "%d (%d)\n",
3077 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003078 return sprintf(page, "%d\n", mddev->layout);
3079}
3080
3081static ssize_t
3082layout_store(mddev_t *mddev, const char *buf, size_t len)
3083{
3084 char *e;
3085 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003086
3087 if (!*buf || (*e && *e != '\n'))
3088 return -EINVAL;
3089
NeilBrownb3546032009-03-31 14:56:41 +11003090 if (mddev->pers) {
3091 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003092 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003093 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003094 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003095 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003096 if (err) {
3097 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003098 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003099 }
NeilBrownb3546032009-03-31 14:56:41 +11003100 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003101 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003102 if (mddev->reshape_position == MaxSector)
3103 mddev->layout = n;
3104 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003105 return len;
3106}
3107static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003108__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003109
3110
NeilBrowneae17012005-11-08 21:39:23 -08003111static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003112raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003113{
NeilBrownbb636542005-11-08 21:39:45 -08003114 if (mddev->raid_disks == 0)
3115 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003116 if (mddev->reshape_position != MaxSector &&
3117 mddev->delta_disks != 0)
3118 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3119 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003120 return sprintf(page, "%d\n", mddev->raid_disks);
3121}
3122
NeilBrownda943b992006-01-06 00:20:54 -08003123static int update_raid_disks(mddev_t *mddev, int raid_disks);
3124
3125static ssize_t
3126raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3127{
NeilBrownda943b992006-01-06 00:20:54 -08003128 char *e;
3129 int rv = 0;
3130 unsigned long n = simple_strtoul(buf, &e, 10);
3131
3132 if (!*buf || (*e && *e != '\n'))
3133 return -EINVAL;
3134
3135 if (mddev->pers)
3136 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003137 else if (mddev->reshape_position != MaxSector) {
3138 int olddisks = mddev->raid_disks - mddev->delta_disks;
3139 mddev->delta_disks = n - olddisks;
3140 mddev->raid_disks = n;
3141 } else
NeilBrownda943b992006-01-06 00:20:54 -08003142 mddev->raid_disks = n;
3143 return rv ? rv : len;
3144}
3145static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003146__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003147
NeilBrown24dd4692005-11-08 21:39:26 -08003148static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003149chunk_size_show(mddev_t *mddev, char *page)
3150{
NeilBrown08a02ec2007-05-09 02:35:38 -07003151 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003152 mddev->chunk_sectors != mddev->new_chunk_sectors)
3153 return sprintf(page, "%d (%d)\n",
3154 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003155 mddev->chunk_sectors << 9);
3156 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003157}
3158
3159static ssize_t
3160chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3161{
NeilBrown3b343802006-01-06 00:20:47 -08003162 char *e;
3163 unsigned long n = simple_strtoul(buf, &e, 10);
3164
NeilBrown3b343802006-01-06 00:20:47 -08003165 if (!*buf || (*e && *e != '\n'))
3166 return -EINVAL;
3167
NeilBrownb3546032009-03-31 14:56:41 +11003168 if (mddev->pers) {
3169 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003170 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003171 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003172 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003173 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003174 if (err) {
3175 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003176 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003177 }
NeilBrownb3546032009-03-31 14:56:41 +11003178 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003179 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003180 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003181 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003182 }
NeilBrown3b343802006-01-06 00:20:47 -08003183 return len;
3184}
3185static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003186__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003187
NeilBrowna94213b2006-06-26 00:28:00 -07003188static ssize_t
3189resync_start_show(mddev_t *mddev, char *page)
3190{
NeilBrownd1a7c502009-03-31 15:24:32 +11003191 if (mddev->recovery_cp == MaxSector)
3192 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003193 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3194}
3195
3196static ssize_t
3197resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3198{
NeilBrowna94213b2006-06-26 00:28:00 -07003199 char *e;
3200 unsigned long long n = simple_strtoull(buf, &e, 10);
3201
3202 if (mddev->pers)
3203 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003204 if (cmd_match(buf, "none"))
3205 n = MaxSector;
3206 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003207 return -EINVAL;
3208
3209 mddev->recovery_cp = n;
3210 return len;
3211}
3212static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003213__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003214
NeilBrown9e653b62006-06-26 00:27:58 -07003215/*
3216 * The array state can be:
3217 *
3218 * clear
3219 * No devices, no size, no level
3220 * Equivalent to STOP_ARRAY ioctl
3221 * inactive
3222 * May have some settings, but array is not active
3223 * all IO results in error
3224 * When written, doesn't tear down array, but just stops it
3225 * suspended (not supported yet)
3226 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003227 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003228 * readonly
3229 * no resync can happen. no superblocks get written.
3230 * write requests fail
3231 * read-auto
3232 * like readonly, but behaves like 'clean' on a write request.
3233 *
3234 * clean - no pending writes, but otherwise active.
3235 * When written to inactive array, starts without resync
3236 * If a write request arrives then
3237 * if metadata is known, mark 'dirty' and switch to 'active'.
3238 * if not known, block and switch to write-pending
3239 * If written to an active array that has pending writes, then fails.
3240 * active
3241 * fully active: IO and resync can be happening.
3242 * When written to inactive array, starts with resync
3243 *
3244 * write-pending
3245 * clean, but writes are blocked waiting for 'active' to be written.
3246 *
3247 * active-idle
3248 * like active, but no writes have been seen for a while (100msec).
3249 *
3250 */
3251enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3252 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003253static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003254 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3255 "write-pending", "active-idle", NULL };
3256
3257static int match_word(const char *word, char **list)
3258{
3259 int n;
3260 for (n=0; list[n]; n++)
3261 if (cmd_match(word, list[n]))
3262 break;
3263 return n;
3264}
3265
3266static ssize_t
3267array_state_show(mddev_t *mddev, char *page)
3268{
3269 enum array_state st = inactive;
3270
3271 if (mddev->pers)
3272 switch(mddev->ro) {
3273 case 1:
3274 st = readonly;
3275 break;
3276 case 2:
3277 st = read_auto;
3278 break;
3279 case 0:
3280 if (mddev->in_sync)
3281 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003282 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3283 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003284 else if (mddev->safemode)
3285 st = active_idle;
3286 else
3287 st = active;
3288 }
3289 else {
3290 if (list_empty(&mddev->disks) &&
3291 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003292 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003293 st = clear;
3294 else
3295 st = inactive;
3296 }
3297 return sprintf(page, "%s\n", array_states[st]);
3298}
3299
Neil Browndf5b20c2008-07-11 22:02:22 +10003300static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003301static int do_md_run(mddev_t * mddev);
3302static int restart_array(mddev_t *mddev);
3303
3304static ssize_t
3305array_state_store(mddev_t *mddev, const char *buf, size_t len)
3306{
3307 int err = -EINVAL;
3308 enum array_state st = match_word(buf, array_states);
3309 switch(st) {
3310 case bad_word:
3311 break;
3312 case clear:
3313 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003314 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003315 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003316 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003317 break;
3318 case inactive:
3319 /* stopping an active array */
3320 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003321 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003322 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003323 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003324 } else
3325 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003326 break;
3327 case suspended:
3328 break; /* not supported yet */
3329 case readonly:
3330 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003331 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003332 else {
3333 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003334 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003335 err = do_md_run(mddev);
3336 }
3337 break;
3338 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003339 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003340 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003341 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003342 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003343 err = restart_array(mddev);
3344 if (err == 0) {
3345 mddev->ro = 2;
3346 set_disk_ro(mddev->gendisk, 0);
3347 }
NeilBrown9e653b62006-06-26 00:27:58 -07003348 } else {
3349 mddev->ro = 2;
3350 err = do_md_run(mddev);
3351 }
3352 break;
3353 case clean:
3354 if (mddev->pers) {
3355 restart_array(mddev);
3356 spin_lock_irq(&mddev->write_lock);
3357 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003358 if (mddev->in_sync == 0) {
3359 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003360 if (mddev->safemode == 1)
3361 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003362 if (mddev->persistent)
3363 set_bit(MD_CHANGE_CLEAN,
3364 &mddev->flags);
3365 }
3366 err = 0;
3367 } else
3368 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003369 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003370 } else
3371 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003372 break;
3373 case active:
3374 if (mddev->pers) {
3375 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003376 if (mddev->external)
3377 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003378 wake_up(&mddev->sb_wait);
3379 err = 0;
3380 } else {
3381 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003382 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003383 err = do_md_run(mddev);
3384 }
3385 break;
3386 case write_pending:
3387 case active_idle:
3388 /* these cannot be set */
3389 break;
3390 }
3391 if (err)
3392 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003393 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003394 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003395 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003396 }
NeilBrown9e653b62006-06-26 00:27:58 -07003397}
NeilBrown80ca3a42006-07-10 04:44:18 -07003398static struct md_sysfs_entry md_array_state =
3399__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003400
NeilBrown6d7ff732006-01-06 00:21:16 -08003401static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003402max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3403 return sprintf(page, "%d\n",
3404 atomic_read(&mddev->max_corr_read_errors));
3405}
3406
3407static ssize_t
3408max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3409{
3410 char *e;
3411 unsigned long n = simple_strtoul(buf, &e, 10);
3412
3413 if (*buf && (*e == 0 || *e == '\n')) {
3414 atomic_set(&mddev->max_corr_read_errors, n);
3415 return len;
3416 }
3417 return -EINVAL;
3418}
3419
3420static struct md_sysfs_entry max_corr_read_errors =
3421__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3422 max_corrected_read_errors_store);
3423
3424static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003425null_show(mddev_t *mddev, char *page)
3426{
3427 return -EINVAL;
3428}
3429
3430static ssize_t
3431new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3432{
3433 /* buf must be %d:%d\n? giving major and minor numbers */
3434 /* The new device is added to the array.
3435 * If the array has a persistent superblock, we read the
3436 * superblock to initialise info and check validity.
3437 * Otherwise, only checking done is that in bind_rdev_to_array,
3438 * which mainly checks size.
3439 */
3440 char *e;
3441 int major = simple_strtoul(buf, &e, 10);
3442 int minor;
3443 dev_t dev;
3444 mdk_rdev_t *rdev;
3445 int err;
3446
3447 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3448 return -EINVAL;
3449 minor = simple_strtoul(e+1, &e, 10);
3450 if (*e && *e != '\n')
3451 return -EINVAL;
3452 dev = MKDEV(major, minor);
3453 if (major != MAJOR(dev) ||
3454 minor != MINOR(dev))
3455 return -EOVERFLOW;
3456
3457
3458 if (mddev->persistent) {
3459 rdev = md_import_device(dev, mddev->major_version,
3460 mddev->minor_version);
3461 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3462 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3463 mdk_rdev_t, same_set);
3464 err = super_types[mddev->major_version]
3465 .load_super(rdev, rdev0, mddev->minor_version);
3466 if (err < 0)
3467 goto out;
3468 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003469 } else if (mddev->external)
3470 rdev = md_import_device(dev, -2, -1);
3471 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003472 rdev = md_import_device(dev, -1, -1);
3473
3474 if (IS_ERR(rdev))
3475 return PTR_ERR(rdev);
3476 err = bind_rdev_to_array(rdev, mddev);
3477 out:
3478 if (err)
3479 export_rdev(rdev);
3480 return err ? err : len;
3481}
3482
3483static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003484__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003485
3486static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003487bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3488{
3489 char *end;
3490 unsigned long chunk, end_chunk;
3491
3492 if (!mddev->bitmap)
3493 goto out;
3494 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3495 while (*buf) {
3496 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3497 if (buf == end) break;
3498 if (*end == '-') { /* range */
3499 buf = end + 1;
3500 end_chunk = simple_strtoul(buf, &end, 0);
3501 if (buf == end) break;
3502 }
3503 if (*end && !isspace(*end)) break;
3504 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003505 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003506 }
3507 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3508out:
3509 return len;
3510}
3511
3512static struct md_sysfs_entry md_bitmap =
3513__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3514
3515static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003516size_show(mddev_t *mddev, char *page)
3517{
Andre Noll58c0fed2009-03-31 14:33:13 +11003518 return sprintf(page, "%llu\n",
3519 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003520}
3521
Andre Nolld71f9f82008-07-11 22:02:22 +10003522static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003523
3524static ssize_t
3525size_store(mddev_t *mddev, const char *buf, size_t len)
3526{
3527 /* If array is inactive, we can reduce the component size, but
3528 * not increase it (except from 0).
3529 * If array is active, we can try an on-line resize
3530 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003531 sector_t sectors;
3532 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003533
Andre Noll58c0fed2009-03-31 14:33:13 +11003534 if (err < 0)
3535 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003536 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003537 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003538 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003539 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003540 if (mddev->dev_sectors == 0 ||
3541 mddev->dev_sectors > sectors)
3542 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003543 else
3544 err = -ENOSPC;
3545 }
3546 return err ? err : len;
3547}
3548
3549static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003550__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003551
NeilBrown8bb93aa2006-01-06 00:20:50 -08003552
3553/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003554 * This is one of
3555 * 'none' for arrays with no metadata (good luck...)
3556 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003557 * or N.M for internally known formats
3558 */
3559static ssize_t
3560metadata_show(mddev_t *mddev, char *page)
3561{
3562 if (mddev->persistent)
3563 return sprintf(page, "%d.%d\n",
3564 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003565 else if (mddev->external)
3566 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003567 else
3568 return sprintf(page, "none\n");
3569}
3570
3571static ssize_t
3572metadata_store(mddev_t *mddev, const char *buf, size_t len)
3573{
3574 int major, minor;
3575 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003576 /* Changing the details of 'external' metadata is
3577 * always permitted. Otherwise there must be
3578 * no devices attached to the array.
3579 */
3580 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3581 ;
3582 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003583 return -EBUSY;
3584
3585 if (cmd_match(buf, "none")) {
3586 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003587 mddev->external = 0;
3588 mddev->major_version = 0;
3589 mddev->minor_version = 90;
3590 return len;
3591 }
3592 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003593 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003594 if (namelen >= sizeof(mddev->metadata_type))
3595 namelen = sizeof(mddev->metadata_type)-1;
3596 strncpy(mddev->metadata_type, buf+9, namelen);
3597 mddev->metadata_type[namelen] = 0;
3598 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3599 mddev->metadata_type[--namelen] = 0;
3600 mddev->persistent = 0;
3601 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003602 mddev->major_version = 0;
3603 mddev->minor_version = 90;
3604 return len;
3605 }
3606 major = simple_strtoul(buf, &e, 10);
3607 if (e==buf || *e != '.')
3608 return -EINVAL;
3609 buf = e+1;
3610 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003611 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003612 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003613 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003614 return -ENOENT;
3615 mddev->major_version = major;
3616 mddev->minor_version = minor;
3617 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003618 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003619 return len;
3620}
3621
3622static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003623__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003624
NeilBrowna35b0d62006-01-06 00:20:49 -08003625static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003626action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003627{
NeilBrown7eec3142005-11-08 21:39:44 -08003628 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003629 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3630 type = "frozen";
3631 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003632 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003633 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3634 type = "reshape";
3635 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003636 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3637 type = "resync";
3638 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3639 type = "check";
3640 else
3641 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003642 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003643 type = "recover";
3644 }
3645 return sprintf(page, "%s\n", type);
3646}
3647
3648static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003649action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003650{
NeilBrown7eec3142005-11-08 21:39:44 -08003651 if (!mddev->pers || !mddev->pers->sync_request)
3652 return -EINVAL;
3653
NeilBrownb6a9ce62009-05-26 09:41:17 +10003654 if (cmd_match(page, "frozen"))
3655 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3656 else
3657 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3658
3659 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003660 if (mddev->sync_thread) {
3661 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3662 md_unregister_thread(mddev->sync_thread);
3663 mddev->sync_thread = NULL;
3664 mddev->recovery = 0;
3665 }
NeilBrown03c902e2006-01-06 00:20:46 -08003666 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3667 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003668 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003669 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003670 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003671 else if (cmd_match(page, "recover")) {
3672 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3673 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3674 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003675 int err;
3676 if (mddev->pers->start_reshape == NULL)
3677 return -EINVAL;
3678 err = mddev->pers->start_reshape(mddev);
3679 if (err)
3680 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003681 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003682 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003683 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003684 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003685 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003686 return -EINVAL;
3687 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3688 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003689 }
NeilBrown03c902e2006-01-06 00:20:46 -08003690 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003691 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003692 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003693 return len;
3694}
3695
NeilBrown9d888832005-11-08 21:39:26 -08003696static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003697mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003698{
3699 return sprintf(page, "%llu\n",
3700 (unsigned long long) mddev->resync_mismatches);
3701}
3702
NeilBrown80ca3a42006-07-10 04:44:18 -07003703static struct md_sysfs_entry md_scan_mode =
3704__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003705
NeilBrown96de1e62005-11-08 21:39:39 -08003706
NeilBrown80ca3a42006-07-10 04:44:18 -07003707static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003708
NeilBrown88202a02006-01-06 00:21:36 -08003709static ssize_t
3710sync_min_show(mddev_t *mddev, char *page)
3711{
3712 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3713 mddev->sync_speed_min ? "local": "system");
3714}
3715
3716static ssize_t
3717sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3718{
3719 int min;
3720 char *e;
3721 if (strncmp(buf, "system", 6)==0) {
3722 mddev->sync_speed_min = 0;
3723 return len;
3724 }
3725 min = simple_strtoul(buf, &e, 10);
3726 if (buf == e || (*e && *e != '\n') || min <= 0)
3727 return -EINVAL;
3728 mddev->sync_speed_min = min;
3729 return len;
3730}
3731
3732static struct md_sysfs_entry md_sync_min =
3733__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3734
3735static ssize_t
3736sync_max_show(mddev_t *mddev, char *page)
3737{
3738 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3739 mddev->sync_speed_max ? "local": "system");
3740}
3741
3742static ssize_t
3743sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3744{
3745 int max;
3746 char *e;
3747 if (strncmp(buf, "system", 6)==0) {
3748 mddev->sync_speed_max = 0;
3749 return len;
3750 }
3751 max = simple_strtoul(buf, &e, 10);
3752 if (buf == e || (*e && *e != '\n') || max <= 0)
3753 return -EINVAL;
3754 mddev->sync_speed_max = max;
3755 return len;
3756}
3757
3758static struct md_sysfs_entry md_sync_max =
3759__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3760
Iustin Popd7f3d292007-10-16 23:30:54 -07003761static ssize_t
3762degraded_show(mddev_t *mddev, char *page)
3763{
3764 return sprintf(page, "%d\n", mddev->degraded);
3765}
3766static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003767
3768static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003769sync_force_parallel_show(mddev_t *mddev, char *page)
3770{
3771 return sprintf(page, "%d\n", mddev->parallel_resync);
3772}
3773
3774static ssize_t
3775sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3776{
3777 long n;
3778
3779 if (strict_strtol(buf, 10, &n))
3780 return -EINVAL;
3781
3782 if (n != 0 && n != 1)
3783 return -EINVAL;
3784
3785 mddev->parallel_resync = n;
3786
3787 if (mddev->sync_thread)
3788 wake_up(&resync_wait);
3789
3790 return len;
3791}
3792
3793/* force parallel resync, even with shared block devices */
3794static struct md_sysfs_entry md_sync_force_parallel =
3795__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3796 sync_force_parallel_show, sync_force_parallel_store);
3797
3798static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003799sync_speed_show(mddev_t *mddev, char *page)
3800{
3801 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003802 if (mddev->curr_resync == 0)
3803 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003804 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3805 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003806 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003807 db = resync - mddev->resync_mark_cnt;
3808 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003809}
3810
NeilBrown80ca3a42006-07-10 04:44:18 -07003811static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003812
3813static ssize_t
3814sync_completed_show(mddev_t *mddev, char *page)
3815{
Andre Noll58c0fed2009-03-31 14:33:13 +11003816 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003817
NeilBrownacb180b2009-04-14 16:28:34 +10003818 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3819 return sprintf(page, "none\n");
3820
NeilBrown88202a02006-01-06 00:21:36 -08003821 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003822 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003823 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003824 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003825
NeilBrownacb180b2009-04-14 16:28:34 +10003826 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003827 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003828}
3829
NeilBrown80ca3a42006-07-10 04:44:18 -07003830static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003831
NeilBrowne464eaf2006-03-27 01:18:14 -08003832static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003833min_sync_show(mddev_t *mddev, char *page)
3834{
3835 return sprintf(page, "%llu\n",
3836 (unsigned long long)mddev->resync_min);
3837}
3838static ssize_t
3839min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3840{
3841 unsigned long long min;
3842 if (strict_strtoull(buf, 10, &min))
3843 return -EINVAL;
3844 if (min > mddev->resync_max)
3845 return -EINVAL;
3846 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3847 return -EBUSY;
3848
3849 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003850 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003851 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003852 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003853 return -EINVAL;
3854 }
3855 mddev->resync_min = min;
3856
3857 return len;
3858}
3859
3860static struct md_sysfs_entry md_min_sync =
3861__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3862
3863static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003864max_sync_show(mddev_t *mddev, char *page)
3865{
3866 if (mddev->resync_max == MaxSector)
3867 return sprintf(page, "max\n");
3868 else
3869 return sprintf(page, "%llu\n",
3870 (unsigned long long)mddev->resync_max);
3871}
3872static ssize_t
3873max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3874{
3875 if (strncmp(buf, "max", 3) == 0)
3876 mddev->resync_max = MaxSector;
3877 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003878 unsigned long long max;
3879 if (strict_strtoull(buf, 10, &max))
3880 return -EINVAL;
3881 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003882 return -EINVAL;
3883 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003884 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003885 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3886 return -EBUSY;
3887
3888 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003889 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003890 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003891 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003892 return -EINVAL;
3893 }
3894 mddev->resync_max = max;
3895 }
3896 wake_up(&mddev->recovery_wait);
3897 return len;
3898}
3899
3900static struct md_sysfs_entry md_max_sync =
3901__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3902
3903static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003904suspend_lo_show(mddev_t *mddev, char *page)
3905{
3906 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3907}
3908
3909static ssize_t
3910suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3911{
3912 char *e;
3913 unsigned long long new = simple_strtoull(buf, &e, 10);
3914
NeilBrownb8d966e2009-07-01 11:14:04 +10003915 if (mddev->pers == NULL ||
3916 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003917 return -EINVAL;
3918 if (buf == e || (*e && *e != '\n'))
3919 return -EINVAL;
3920 if (new >= mddev->suspend_hi ||
3921 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3922 mddev->suspend_lo = new;
3923 mddev->pers->quiesce(mddev, 2);
3924 return len;
3925 } else
3926 return -EINVAL;
3927}
3928static struct md_sysfs_entry md_suspend_lo =
3929__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3930
3931
3932static ssize_t
3933suspend_hi_show(mddev_t *mddev, char *page)
3934{
3935 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3936}
3937
3938static ssize_t
3939suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3940{
3941 char *e;
3942 unsigned long long new = simple_strtoull(buf, &e, 10);
3943
NeilBrownb8d966e2009-07-01 11:14:04 +10003944 if (mddev->pers == NULL ||
3945 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003946 return -EINVAL;
3947 if (buf == e || (*e && *e != '\n'))
3948 return -EINVAL;
3949 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3950 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3951 mddev->suspend_hi = new;
3952 mddev->pers->quiesce(mddev, 1);
3953 mddev->pers->quiesce(mddev, 0);
3954 return len;
3955 } else
3956 return -EINVAL;
3957}
3958static struct md_sysfs_entry md_suspend_hi =
3959__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3960
NeilBrown08a02ec2007-05-09 02:35:38 -07003961static ssize_t
3962reshape_position_show(mddev_t *mddev, char *page)
3963{
3964 if (mddev->reshape_position != MaxSector)
3965 return sprintf(page, "%llu\n",
3966 (unsigned long long)mddev->reshape_position);
3967 strcpy(page, "none\n");
3968 return 5;
3969}
3970
3971static ssize_t
3972reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3973{
3974 char *e;
3975 unsigned long long new = simple_strtoull(buf, &e, 10);
3976 if (mddev->pers)
3977 return -EBUSY;
3978 if (buf == e || (*e && *e != '\n'))
3979 return -EINVAL;
3980 mddev->reshape_position = new;
3981 mddev->delta_disks = 0;
3982 mddev->new_level = mddev->level;
3983 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003984 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003985 return len;
3986}
3987
3988static struct md_sysfs_entry md_reshape_position =
3989__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
3990 reshape_position_store);
3991
Dan Williamsb522adc2009-03-31 15:00:31 +11003992static ssize_t
3993array_size_show(mddev_t *mddev, char *page)
3994{
3995 if (mddev->external_size)
3996 return sprintf(page, "%llu\n",
3997 (unsigned long long)mddev->array_sectors/2);
3998 else
3999 return sprintf(page, "default\n");
4000}
4001
4002static ssize_t
4003array_size_store(mddev_t *mddev, const char *buf, size_t len)
4004{
4005 sector_t sectors;
4006
4007 if (strncmp(buf, "default", 7) == 0) {
4008 if (mddev->pers)
4009 sectors = mddev->pers->size(mddev, 0, 0);
4010 else
4011 sectors = mddev->array_sectors;
4012
4013 mddev->external_size = 0;
4014 } else {
4015 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4016 return -EINVAL;
4017 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004018 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004019
4020 mddev->external_size = 1;
4021 }
4022
4023 mddev->array_sectors = sectors;
4024 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004025 if (mddev->pers)
4026 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004027
4028 return len;
4029}
4030
4031static struct md_sysfs_entry md_array_size =
4032__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4033 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004034
NeilBrowneae17012005-11-08 21:39:23 -08004035static struct attribute *md_default_attrs[] = {
4036 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004037 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004038 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004039 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004040 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004041 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004042 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004043 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004044 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004045 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004046 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004047 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004048 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004049 NULL,
4050};
4051
4052static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004053 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004054 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004055 &md_sync_min.attr,
4056 &md_sync_max.attr,
4057 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004058 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004059 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004060 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004061 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004062 &md_suspend_lo.attr,
4063 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004064 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004065 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004066 NULL,
4067};
NeilBrown411036f2005-11-08 21:39:40 -08004068static struct attribute_group md_redundancy_group = {
4069 .name = NULL,
4070 .attrs = md_redundancy_attrs,
4071};
4072
NeilBrowneae17012005-11-08 21:39:23 -08004073
4074static ssize_t
4075md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4076{
4077 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4078 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004079 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004080
4081 if (!entry->show)
4082 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004083 rv = mddev_lock(mddev);
4084 if (!rv) {
4085 rv = entry->show(mddev, page);
4086 mddev_unlock(mddev);
4087 }
NeilBrown96de1e62005-11-08 21:39:39 -08004088 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004089}
4090
4091static ssize_t
4092md_attr_store(struct kobject *kobj, struct attribute *attr,
4093 const char *page, size_t length)
4094{
4095 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4096 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004097 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004098
4099 if (!entry->store)
4100 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004101 if (!capable(CAP_SYS_ADMIN))
4102 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004103 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004104 if (mddev->hold_active == UNTIL_IOCTL)
4105 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004106 if (!rv) {
4107 rv = entry->store(mddev, page, length);
4108 mddev_unlock(mddev);
4109 }
NeilBrown96de1e62005-11-08 21:39:39 -08004110 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004111}
4112
4113static void md_free(struct kobject *ko)
4114{
4115 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004116
4117 if (mddev->sysfs_state)
4118 sysfs_put(mddev->sysfs_state);
4119
4120 if (mddev->gendisk) {
4121 del_gendisk(mddev->gendisk);
4122 put_disk(mddev->gendisk);
4123 }
4124 if (mddev->queue)
4125 blk_cleanup_queue(mddev->queue);
4126
NeilBrowneae17012005-11-08 21:39:23 -08004127 kfree(mddev);
4128}
4129
4130static struct sysfs_ops md_sysfs_ops = {
4131 .show = md_attr_show,
4132 .store = md_attr_store,
4133};
4134static struct kobj_type md_ktype = {
4135 .release = md_free,
4136 .sysfs_ops = &md_sysfs_ops,
4137 .default_attrs = md_default_attrs,
4138};
4139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140int mdp_major = 0;
4141
Dan Williams5fd3a172009-03-04 00:57:25 -07004142static void mddev_delayed_delete(struct work_struct *ws)
4143{
4144 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4145
NeilBrown43a70502009-12-14 12:49:55 +11004146 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004147 kobject_del(&mddev->kobj);
4148 kobject_put(&mddev->kobj);
4149}
4150
NeilBrownefeb53c2009-01-09 08:31:10 +11004151static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004153 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 mddev_t *mddev = mddev_find(dev);
4155 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004156 int partitioned;
4157 int shift;
4158 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004159 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
4161 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004162 return -ENODEV;
4163
4164 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4165 shift = partitioned ? MdpMinorShift : 0;
4166 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
NeilBrownd3374822009-01-09 08:31:10 +11004168 /* wait for any previous instance if this device
4169 * to be completed removed (mddev_delayed_delete).
4170 */
4171 flush_scheduled_work();
4172
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004173 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004174 error = -EEXIST;
4175 if (mddev->gendisk)
4176 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004177
4178 if (name) {
4179 /* Need to ensure that 'name' is not a duplicate.
4180 */
4181 mddev_t *mddev2;
4182 spin_lock(&all_mddevs_lock);
4183
4184 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4185 if (mddev2->gendisk &&
4186 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4187 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004188 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004189 }
4190 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 }
NeilBrown8b765392009-01-09 08:31:08 +11004192
NeilBrown0909dc42009-07-01 12:27:21 +10004193 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004194 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004195 if (!mddev->queue)
4196 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004197 mddev->queue->queuedata = mddev;
4198
NeilBrown8b765392009-01-09 08:31:08 +11004199 /* Can be unlocked because the queue is new: no concurrency */
4200 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4201
NeilBrown409c57f2009-03-31 14:39:39 +11004202 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004203
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 disk = alloc_disk(1 << shift);
4205 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004206 blk_cleanup_queue(mddev->queue);
4207 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004208 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004210 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004212 if (name)
4213 strcpy(disk->disk_name, name);
4214 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004216 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 disk->fops = &md_fops;
4219 disk->private_data = mddev;
4220 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004221 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004222 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004223 * remove it now.
4224 */
4225 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 add_disk(disk);
4227 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004228 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4229 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004230 if (error) {
4231 /* This isn't possible, but as kobject_init_and_add is marked
4232 * __must_check, we must do something with the result
4233 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004234 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4235 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004236 error = 0;
4237 }
NeilBrown43a70502009-12-14 12:49:55 +11004238 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4239 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004240 abort:
4241 mutex_unlock(&disks_mutex);
4242 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004243 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004244 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4245 }
NeilBrownd3374822009-01-09 08:31:10 +11004246 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004247 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004248}
4249
4250static struct kobject *md_probe(dev_t dev, int *part, void *data)
4251{
4252 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return NULL;
4254}
4255
NeilBrownefeb53c2009-01-09 08:31:10 +11004256static int add_named_array(const char *val, struct kernel_param *kp)
4257{
4258 /* val must be "md_*" where * is not all digits.
4259 * We allocate an array with a large free minor number, and
4260 * set the name to val. val must not already be an active name.
4261 */
4262 int len = strlen(val);
4263 char buf[DISK_NAME_LEN];
4264
4265 while (len && val[len-1] == '\n')
4266 len--;
4267 if (len >= DISK_NAME_LEN)
4268 return -E2BIG;
4269 strlcpy(buf, val, len+1);
4270 if (strncmp(buf, "md_", 3) != 0)
4271 return -EINVAL;
4272 return md_alloc(0, buf);
4273}
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275static void md_safemode_timeout(unsigned long data)
4276{
4277 mddev_t *mddev = (mddev_t *) data;
4278
Neil Brown0fd62b82008-06-28 08:31:36 +10004279 if (!atomic_read(&mddev->writes_pending)) {
4280 mddev->safemode = 1;
4281 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004282 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 md_wakeup_thread(mddev->thread);
4285}
4286
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004287static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289static int do_md_run(mddev_t * mddev)
4290{
NeilBrown2604b702006-01-06 00:20:36 -08004291 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 mdk_rdev_t *rdev;
4293 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004294 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295
NeilBrowna757e642005-04-16 15:26:42 -07004296 if (list_empty(&mddev->disks))
4297 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
4300 if (mddev->pers)
4301 return -EBUSY;
4302
NeilBrownb6eb1272010-04-15 10:13:47 +10004303 /* These two calls synchronise us with the
4304 * sysfs_remove_group calls in mddev_unlock,
4305 * so they must have completed.
4306 */
4307 mutex_lock(&mddev->open_mutex);
4308 mutex_unlock(&mddev->open_mutex);
4309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 /*
4311 * Analyze all RAID superblock(s)
4312 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004313 if (!mddev->raid_disks) {
4314 if (!mddev->persistent)
4315 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004316 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
NeilBrownd9d166c2006-01-06 00:20:51 -08004319 if (mddev->level != LEVEL_NONE)
4320 request_module("md-level-%d", mddev->level);
4321 else if (mddev->clevel[0])
4322 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323
4324 /*
4325 * Drop all container device buffers, from now on
4326 * the only valid external interface is through the md
4327 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004329 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004330 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 continue;
4332 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004333 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004334
4335 /* perform some consistency tests on the device.
4336 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004337 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004338 */
Andre Noll0f420352008-07-11 22:02:23 +10004339 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004340 if (mddev->dev_sectors &&
4341 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004342 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004343 printk("md: %s: data overlaps metadata\n",
4344 mdname(mddev));
4345 return -EINVAL;
4346 }
4347 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004348 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004349 > rdev->data_offset) {
4350 printk("md: %s: metadata overlaps data\n",
4351 mdname(mddev));
4352 return -EINVAL;
4353 }
4354 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004355 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 }
4357
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
4360 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004361 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004362 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004364 if (mddev->level != LEVEL_NONE)
4365 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4366 mddev->level);
4367 else
4368 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4369 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 return -EINVAL;
4371 }
NeilBrown2604b702006-01-06 00:20:36 -08004372 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004374 if (mddev->level != pers->level) {
4375 mddev->level = pers->level;
4376 mddev->new_level = pers->level;
4377 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004378 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
NeilBrownf6705572006-03-27 01:18:11 -08004380 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004381 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004382 /* This personality cannot handle reshaping... */
4383 mddev->pers = NULL;
4384 module_put(pers->owner);
4385 return -EINVAL;
4386 }
4387
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004388 if (pers->sync_request) {
4389 /* Warn if this is a potentially silly
4390 * configuration.
4391 */
4392 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4393 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004394 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004395
4396 list_for_each_entry(rdev, &mddev->disks, same_set)
4397 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004398 if (rdev < rdev2 &&
4399 rdev->bdev->bd_contains ==
4400 rdev2->bdev->bd_contains) {
4401 printk(KERN_WARNING
4402 "%s: WARNING: %s appears to be"
4403 " on the same physical disk as"
4404 " %s.\n",
4405 mdname(mddev),
4406 bdevname(rdev->bdev,b),
4407 bdevname(rdev2->bdev,b2));
4408 warned = 1;
4409 }
4410 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004411
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004412 if (warned)
4413 printk(KERN_WARNING
4414 "True protection against single-disk"
4415 " failure might be compromised.\n");
4416 }
4417
NeilBrown657390d2005-08-26 18:34:16 -07004418 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004419 /* may be over-ridden by personality */
4420 mddev->resync_max_sectors = mddev->dev_sectors;
4421
NeilBrowna9701a32005-11-08 21:39:34 -08004422 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004423 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
NeilBrown0f9552b2009-12-30 12:08:50 +11004425 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004426 mddev->ro = 2; /* read-only, but switch on first write */
4427
NeilBrownb15c2e52006-01-06 00:20:16 -08004428 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004429 if (err)
4430 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004431 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4432 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4433 " but 'external_size' not in effect?\n", __func__);
4434 printk(KERN_ERR
4435 "md: invalid array_size %llu > default size %llu\n",
4436 (unsigned long long)mddev->array_sectors / 2,
4437 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4438 err = -EINVAL;
4439 mddev->pers->stop(mddev);
4440 }
4441 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004442 err = bitmap_create(mddev);
4443 if (err) {
4444 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4445 mdname(mddev), err);
4446 mddev->pers->stop(mddev);
4447 }
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 module_put(mddev->pers->owner);
4451 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004452 bitmap_destroy(mddev);
4453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004455 if (mddev->pers->sync_request) {
4456 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4457 printk(KERN_WARNING
4458 "md: cannot register extra attributes for %s\n",
4459 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004460 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004461 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004462 mddev->ro = 0;
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004465 atomic_set(&mddev->max_corr_read_errors,
4466 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 mddev->safemode = 0;
4468 mddev->safemode_timer.function = md_safemode_timeout;
4469 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004470 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004472
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004473 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004474 if (rdev->raid_disk >= 0) {
4475 char nm[20];
4476 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004477 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4478 printk("md: cannot register %s for %s\n",
4479 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
4482 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4483
NeilBrown850b2b42006-10-03 01:15:46 -07004484 if (mddev->flags)
4485 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Andre Nollf233ea52008-07-21 17:05:22 +10004487 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
NeilBrown0b8c9de2006-07-10 04:44:15 -07004489 md_wakeup_thread(mddev->thread);
4490 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004491
NeilBrown449aad32009-08-03 10:59:58 +10004492 revalidate_disk(mddev->gendisk);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004493 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004494 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004495 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004496 if (mddev->sysfs_action)
4497 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004498 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004499 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 return 0;
4501}
4502
4503static int restart_array(mddev_t *mddev)
4504{
4505 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
Andre Noll80fab1d2008-07-11 22:02:21 +10004507 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004509 return -ENXIO;
4510 if (!mddev->pers)
4511 return -EINVAL;
4512 if (!mddev->ro)
4513 return -EBUSY;
4514 mddev->safemode = 0;
4515 mddev->ro = 0;
4516 set_disk_ro(disk, 0);
4517 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4518 mdname(mddev));
4519 /* Kick recovery or resync if necessary */
4520 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4521 md_wakeup_thread(mddev->thread);
4522 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004523 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004524 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525}
4526
NeilBrownacc55e22006-06-26 00:27:47 -07004527/* similar to deny_write_access, but accounts for our holding a reference
4528 * to the file ourselves */
4529static int deny_bitmap_write_access(struct file * file)
4530{
4531 struct inode *inode = file->f_mapping->host;
4532
4533 spin_lock(&inode->i_lock);
4534 if (atomic_read(&inode->i_writecount) > 1) {
4535 spin_unlock(&inode->i_lock);
4536 return -ETXTBSY;
4537 }
4538 atomic_set(&inode->i_writecount, -1);
4539 spin_unlock(&inode->i_lock);
4540
4541 return 0;
4542}
4543
NeilBrown43a70502009-12-14 12:49:55 +11004544void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004545{
4546 struct inode *inode = file->f_mapping->host;
4547
4548 spin_lock(&inode->i_lock);
4549 atomic_set(&inode->i_writecount, 1);
4550 spin_unlock(&inode->i_lock);
4551}
4552
NeilBrown9e653b62006-06-26 00:27:58 -07004553/* mode:
4554 * 0 - completely stop and dis-assemble array
4555 * 1 - switch to readonly
4556 * 2 - stop but do not disassemble array
4557 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004558static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559{
4560 int err = 0;
4561 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004562 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
NeilBrownc8c00a62009-08-10 12:50:52 +10004564 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004565 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004566 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004567 err = -EBUSY;
4568 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569
4570 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004571 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4573 md_unregister_thread(mddev->sync_thread);
4574 mddev->sync_thread = NULL;
4575 }
4576
4577 del_timer_sync(&mddev->safemode_timer);
4578
NeilBrown9e653b62006-06-26 00:27:58 -07004579 switch(mode) {
4580 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004582 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 goto out;
4584 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004585 break;
4586 case 0: /* disassemble */
4587 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004588 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004589 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 if (mddev->ro)
4591 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004594 mddev->queue->merge_bvec_fn = NULL;
4595 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004596 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 module_put(mddev->pers->owner);
NeilBrowna64c8762010-04-14 17:15:37 +10004598 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4599 mddev->to_remove = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004601 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004602 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004603
NeilBrownc4647292009-05-07 12:51:06 +10004604 list_for_each_entry(rdev, &mddev->disks, same_set)
4605 if (rdev->raid_disk >= 0) {
4606 char nm[20];
4607 sprintf(nm, "rd%d", rdev->raid_disk);
4608 sysfs_remove_link(&mddev->kobj, nm);
4609 }
4610
NeilBrown0d4ca602006-12-10 02:20:44 -08004611 set_capacity(disk, 0);
Linus Torvalds44ce6292007-05-09 18:51:36 -07004612 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 if (mddev->ro)
4615 mddev->ro = 0;
4616 }
NeilBrown850b2b42006-10-03 01:15:46 -07004617 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 /* mark array as shutdown cleanly */
4619 mddev->in_sync = 1;
NeilBrown850b2b42006-10-03 01:15:46 -07004620 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 }
NeilBrown9e653b62006-06-26 00:27:58 -07004622 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004624 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004625 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004627out:
4628 mutex_unlock(&mddev->open_mutex);
4629 if (err)
4630 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 /*
4632 * Free resources if final stop
4633 */
NeilBrown9e653b62006-06-26 00:27:58 -07004634 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4637
NeilBrown978f9462006-02-02 14:28:05 -08004638 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004639 if (mddev->bitmap_info.file) {
4640 restore_bitmap_write_access(mddev->bitmap_info.file);
4641 fput(mddev->bitmap_info.file);
4642 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004643 }
NeilBrownc3d97142009-12-14 12:49:52 +11004644 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 export_array(mddev);
4647
Andre Nollf233ea52008-07-21 17:05:22 +10004648 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004649 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004650 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004651 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004652 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004653 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004654 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004655 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004656 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004657 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004658 mddev->level = LEVEL_NONE;
4659 mddev->clevel[0] = 0;
4660 mddev->flags = 0;
4661 mddev->ro = 0;
4662 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004663 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004664 mddev->ctime = mddev->utime = 0;
4665 mddev->layout = 0;
4666 mddev->max_disks = 0;
4667 mddev->events = 0;
4668 mddev->delta_disks = 0;
4669 mddev->new_level = LEVEL_NONE;
4670 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004671 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004672 mddev->curr_resync = 0;
4673 mddev->resync_mismatches = 0;
4674 mddev->suspend_lo = mddev->suspend_hi = 0;
4675 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4676 mddev->recovery = 0;
4677 mddev->in_sync = 0;
4678 mddev->changed = 0;
4679 mddev->degraded = 0;
4680 mddev->barriers_work = 0;
4681 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004682 mddev->bitmap_info.offset = 0;
4683 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004684 mddev->bitmap_info.chunksize = 0;
4685 mddev->bitmap_info.daemon_sleep = 0;
4686 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004687 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004688 if (mddev->hold_active == UNTIL_STOP)
4689 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004690
NeilBrowna8a55c32006-06-26 00:27:39 -07004691 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4693 mdname(mddev));
4694 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004695 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004696 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004697 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 return err;
4699}
4700
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004701#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702static void autorun_array(mddev_t *mddev)
4703{
4704 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 int err;
4706
NeilBrowna757e642005-04-16 15:26:42 -07004707 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709
4710 printk(KERN_INFO "md: running: ");
4711
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004712 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 char b[BDEVNAME_SIZE];
4714 printk("<%s>", bdevname(rdev->bdev,b));
4715 }
4716 printk("\n");
4717
NeilBrownd710e132008-10-13 11:55:12 +11004718 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 if (err) {
4720 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004721 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 }
4723}
4724
4725/*
4726 * lets try to run arrays based on all disks that have arrived
4727 * until now. (those are in pending_raid_disks)
4728 *
4729 * the method: pick the first pending disk, collect all disks with
4730 * the same UUID, remove all from the pending list and put them into
4731 * the 'same_array' list. Then order this list based on superblock
4732 * update time (freshest comes first), kick out 'old' disks and
4733 * compare superblocks. If everything's fine then run it.
4734 *
4735 * If "unit" is allocated, then bump its reference count
4736 */
4737static void autorun_devices(int part)
4738{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004739 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 mddev_t *mddev;
4741 char b[BDEVNAME_SIZE];
4742
4743 printk(KERN_INFO "md: autorun ...\n");
4744 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004745 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004747 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 rdev0 = list_entry(pending_raid_disks.next,
4749 mdk_rdev_t, same_set);
4750
4751 printk(KERN_INFO "md: considering %s ...\n",
4752 bdevname(rdev0->bdev,b));
4753 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004754 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 if (super_90_load(rdev, rdev0, 0) >= 0) {
4756 printk(KERN_INFO "md: adding %s ...\n",
4757 bdevname(rdev->bdev,b));
4758 list_move(&rdev->same_set, &candidates);
4759 }
4760 /*
4761 * now we have a set of devices, with all of them having
4762 * mostly sane superblocks. It's time to allocate the
4763 * mddev.
4764 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004765 if (part) {
4766 dev = MKDEV(mdp_major,
4767 rdev0->preferred_minor << MdpMinorShift);
4768 unit = MINOR(dev) >> MdpMinorShift;
4769 } else {
4770 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4771 unit = MINOR(dev);
4772 }
4773 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4775 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4776 break;
4777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778
4779 md_probe(dev, NULL, NULL);
4780 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004781 if (!mddev || !mddev->gendisk) {
4782 if (mddev)
4783 mddev_put(mddev);
4784 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 "md: cannot allocate memory for md drive.\n");
4786 break;
4787 }
4788 if (mddev_lock(mddev))
4789 printk(KERN_WARNING "md: %s locked, cannot run\n",
4790 mdname(mddev));
4791 else if (mddev->raid_disks || mddev->major_version
4792 || !list_empty(&mddev->disks)) {
4793 printk(KERN_WARNING
4794 "md: %s already running, cannot run %s\n",
4795 mdname(mddev), bdevname(rdev0->bdev,b));
4796 mddev_unlock(mddev);
4797 } else {
4798 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004799 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004800 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 list_del_init(&rdev->same_set);
4802 if (bind_rdev_to_array(rdev, mddev))
4803 export_rdev(rdev);
4804 }
4805 autorun_array(mddev);
4806 mddev_unlock(mddev);
4807 }
4808 /* on success, candidates will be empty, on error
4809 * it won't...
4810 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004811 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004812 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815 mddev_put(mddev);
4816 }
4817 printk(KERN_INFO "md: ... autorun DONE.\n");
4818}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004819#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821static int get_version(void __user * arg)
4822{
4823 mdu_version_t ver;
4824
4825 ver.major = MD_MAJOR_VERSION;
4826 ver.minor = MD_MINOR_VERSION;
4827 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4828
4829 if (copy_to_user(arg, &ver, sizeof(ver)))
4830 return -EFAULT;
4831
4832 return 0;
4833}
4834
4835static int get_array_info(mddev_t * mddev, void __user * arg)
4836{
4837 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004838 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
NeilBrowna9f326e2009-09-23 18:06:41 +10004841 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004842 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004844 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004845 failed++;
4846 else {
4847 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004848 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004849 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 else
4851 spare++;
4852 }
4853 }
4854
4855 info.major_version = mddev->major_version;
4856 info.minor_version = mddev->minor_version;
4857 info.patch_version = MD_PATCHLEVEL_VERSION;
4858 info.ctime = mddev->ctime;
4859 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004860 info.size = mddev->dev_sectors / 2;
4861 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004862 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 info.nr_disks = nr;
4864 info.raid_disks = mddev->raid_disks;
4865 info.md_minor = mddev->md_minor;
4866 info.not_persistent= !mddev->persistent;
4867
4868 info.utime = mddev->utime;
4869 info.state = 0;
4870 if (mddev->in_sync)
4871 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004872 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004873 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004874 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 info.working_disks = working;
4876 info.failed_disks = failed;
4877 info.spare_disks = spare;
4878
4879 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004880 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 if (copy_to_user(arg, &info, sizeof(info)))
4883 return -EFAULT;
4884
4885 return 0;
4886}
4887
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004888static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004889{
4890 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4891 char *ptr, *buf = NULL;
4892 int err = -ENOMEM;
4893
Dan Williamsb5470dc2008-06-27 21:44:04 -07004894 if (md_allow_write(mddev))
4895 file = kmalloc(sizeof(*file), GFP_NOIO);
4896 else
4897 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004898
NeilBrown32a76272005-06-21 17:17:14 -07004899 if (!file)
4900 goto out;
4901
4902 /* bitmap disabled, zero the first byte and copy out */
4903 if (!mddev->bitmap || !mddev->bitmap->file) {
4904 file->pathname[0] = '\0';
4905 goto copy_out;
4906 }
4907
4908 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4909 if (!buf)
4910 goto out;
4911
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004912 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4913 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004914 goto out;
4915
4916 strcpy(file->pathname, ptr);
4917
4918copy_out:
4919 err = 0;
4920 if (copy_to_user(arg, file, sizeof(*file)))
4921 err = -EFAULT;
4922out:
4923 kfree(buf);
4924 kfree(file);
4925 return err;
4926}
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928static int get_disk_info(mddev_t * mddev, void __user * arg)
4929{
4930 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 mdk_rdev_t *rdev;
4932
4933 if (copy_from_user(&info, arg, sizeof(info)))
4934 return -EFAULT;
4935
Andre Noll26ef3792008-07-11 22:02:21 +10004936 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 if (rdev) {
4938 info.major = MAJOR(rdev->bdev->bd_dev);
4939 info.minor = MINOR(rdev->bdev->bd_dev);
4940 info.raid_disk = rdev->raid_disk;
4941 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004942 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004944 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 info.state |= (1<<MD_DISK_ACTIVE);
4946 info.state |= (1<<MD_DISK_SYNC);
4947 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004948 if (test_bit(WriteMostly, &rdev->flags))
4949 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 } else {
4951 info.major = info.minor = 0;
4952 info.raid_disk = -1;
4953 info.state = (1<<MD_DISK_REMOVED);
4954 }
4955
4956 if (copy_to_user(arg, &info, sizeof(info)))
4957 return -EFAULT;
4958
4959 return 0;
4960}
4961
4962static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4963{
4964 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4965 mdk_rdev_t *rdev;
4966 dev_t dev = MKDEV(info->major,info->minor);
4967
4968 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4969 return -EOVERFLOW;
4970
4971 if (!mddev->raid_disks) {
4972 int err;
4973 /* expecting a device which has a superblock */
4974 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4975 if (IS_ERR(rdev)) {
4976 printk(KERN_WARNING
4977 "md: md_import_device returned %ld\n",
4978 PTR_ERR(rdev));
4979 return PTR_ERR(rdev);
4980 }
4981 if (!list_empty(&mddev->disks)) {
4982 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4983 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004984 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 .load_super(rdev, rdev0, mddev->minor_version);
4986 if (err < 0) {
4987 printk(KERN_WARNING
4988 "md: %s has different UUID to %s\n",
4989 bdevname(rdev->bdev,b),
4990 bdevname(rdev0->bdev,b2));
4991 export_rdev(rdev);
4992 return -EINVAL;
4993 }
4994 }
4995 err = bind_rdev_to_array(rdev, mddev);
4996 if (err)
4997 export_rdev(rdev);
4998 return err;
4999 }
5000
5001 /*
5002 * add_new_disk can be used once the array is assembled
5003 * to add "hot spares". They must already have a superblock
5004 * written
5005 */
5006 if (mddev->pers) {
5007 int err;
5008 if (!mddev->pers->hot_add_disk) {
5009 printk(KERN_WARNING
5010 "%s: personality does not support diskops!\n",
5011 mdname(mddev));
5012 return -EINVAL;
5013 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005014 if (mddev->persistent)
5015 rdev = md_import_device(dev, mddev->major_version,
5016 mddev->minor_version);
5017 else
5018 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005019 if (IS_ERR(rdev)) {
5020 printk(KERN_WARNING
5021 "md: md_import_device returned %ld\n",
5022 PTR_ERR(rdev));
5023 return PTR_ERR(rdev);
5024 }
NeilBrown41158c72005-06-21 17:17:25 -07005025 /* set save_raid_disk if appropriate */
5026 if (!mddev->persistent) {
5027 if (info->state & (1<<MD_DISK_SYNC) &&
5028 info->raid_disk < mddev->raid_disks)
5029 rdev->raid_disk = info->raid_disk;
5030 else
5031 rdev->raid_disk = -1;
5032 } else
5033 super_types[mddev->major_version].
5034 validate_super(mddev, rdev);
5035 rdev->saved_raid_disk = rdev->raid_disk;
5036
NeilBrownb2d444d2005-11-08 21:39:31 -08005037 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005038 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5039 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005040 else
5041 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005042
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 rdev->raid_disk = -1;
5044 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005045 if (!err && !mddev->pers->hot_remove_disk) {
5046 /* If there is hot_add_disk but no hot_remove_disk
5047 * then added disks for geometry changes,
5048 * and should be added immediately.
5049 */
5050 super_types[mddev->major_version].
5051 validate_super(mddev, rdev);
5052 err = mddev->pers->hot_add_disk(mddev, rdev);
5053 if (err)
5054 unbind_rdev_from_array(rdev);
5055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 if (err)
5057 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005058 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005059 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005060
NeilBrown17571282006-12-10 02:20:52 -08005061 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005062 if (mddev->degraded)
5063 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005064 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005065 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 return err;
5067 }
5068
5069 /* otherwise, add_new_disk is only allowed
5070 * for major_version==0 superblocks
5071 */
5072 if (mddev->major_version != 0) {
5073 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5074 mdname(mddev));
5075 return -EINVAL;
5076 }
5077
5078 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5079 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005080 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 if (IS_ERR(rdev)) {
5082 printk(KERN_WARNING
5083 "md: error, md_import_device() returned %ld\n",
5084 PTR_ERR(rdev));
5085 return PTR_ERR(rdev);
5086 }
5087 rdev->desc_nr = info->number;
5088 if (info->raid_disk < mddev->raid_disks)
5089 rdev->raid_disk = info->raid_disk;
5090 else
5091 rdev->raid_disk = -1;
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005094 if (info->state & (1<<MD_DISK_SYNC))
5095 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005097 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5098 set_bit(WriteMostly, &rdev->flags);
5099
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 if (!mddev->persistent) {
5101 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005102 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 } else
Andre Noll0f420352008-07-11 22:02:23 +10005104 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005105 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106
NeilBrown2bf071b2006-01-06 00:20:55 -08005107 err = bind_rdev_to_array(rdev, mddev);
5108 if (err) {
5109 export_rdev(rdev);
5110 return err;
5111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 }
5113
5114 return 0;
5115}
5116
5117static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5118{
5119 char b[BDEVNAME_SIZE];
5120 mdk_rdev_t *rdev;
5121
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 rdev = find_rdev(mddev, dev);
5123 if (!rdev)
5124 return -ENXIO;
5125
5126 if (rdev->raid_disk >= 0)
5127 goto busy;
5128
5129 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005130 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005131 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132
5133 return 0;
5134busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005135 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 bdevname(rdev->bdev,b), mdname(mddev));
5137 return -EBUSY;
5138}
5139
5140static int hot_add_disk(mddev_t * mddev, dev_t dev)
5141{
5142 char b[BDEVNAME_SIZE];
5143 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 mdk_rdev_t *rdev;
5145
5146 if (!mddev->pers)
5147 return -ENODEV;
5148
5149 if (mddev->major_version != 0) {
5150 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5151 " version-0 superblocks.\n",
5152 mdname(mddev));
5153 return -EINVAL;
5154 }
5155 if (!mddev->pers->hot_add_disk) {
5156 printk(KERN_WARNING
5157 "%s: personality does not support diskops!\n",
5158 mdname(mddev));
5159 return -EINVAL;
5160 }
5161
NeilBrownd710e132008-10-13 11:55:12 +11005162 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 if (IS_ERR(rdev)) {
5164 printk(KERN_WARNING
5165 "md: error, md_import_device() returned %ld\n",
5166 PTR_ERR(rdev));
5167 return -EINVAL;
5168 }
5169
5170 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005171 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 else
Andre Noll0f420352008-07-11 22:02:23 +10005173 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174
NeilBrown8190e752009-06-18 08:48:58 +10005175 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176
NeilBrownb2d444d2005-11-08 21:39:31 -08005177 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 printk(KERN_WARNING
5179 "md: can not hot-add faulty %s disk to %s!\n",
5180 bdevname(rdev->bdev,b), mdname(mddev));
5181 err = -EINVAL;
5182 goto abort_export;
5183 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005184 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005186 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005187 err = bind_rdev_to_array(rdev, mddev);
5188 if (err)
5189 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190
5191 /*
5192 * The rest should better be atomic, we can have disk failures
5193 * noticed in interrupt contexts ...
5194 */
5195
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 rdev->raid_disk = -1;
5197
NeilBrown850b2b42006-10-03 01:15:46 -07005198 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
5200 /*
5201 * Kick recovery, maybe this spare has to be added to the
5202 * array immediately.
5203 */
5204 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5205 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005206 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 return 0;
5208
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209abort_export:
5210 export_rdev(rdev);
5211 return err;
5212}
5213
NeilBrown32a76272005-06-21 17:17:14 -07005214static int set_bitmap_file(mddev_t *mddev, int fd)
5215{
5216 int err;
5217
NeilBrown36fa3062005-09-09 16:23:45 -07005218 if (mddev->pers) {
5219 if (!mddev->pers->quiesce)
5220 return -EBUSY;
5221 if (mddev->recovery || mddev->sync_thread)
5222 return -EBUSY;
5223 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005224 }
5225
NeilBrown36fa3062005-09-09 16:23:45 -07005226
5227 if (fd >= 0) {
5228 if (mddev->bitmap)
5229 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005230 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005231
NeilBrownc3d97142009-12-14 12:49:52 +11005232 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005233 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5234 mdname(mddev));
5235 return -EBADF;
5236 }
5237
NeilBrownc3d97142009-12-14 12:49:52 +11005238 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005239 if (err) {
5240 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5241 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005242 fput(mddev->bitmap_info.file);
5243 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005244 return err;
5245 }
NeilBrownc3d97142009-12-14 12:49:52 +11005246 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005247 } else if (mddev->bitmap == NULL)
5248 return -ENOENT; /* cannot remove what isn't there */
5249 err = 0;
5250 if (mddev->pers) {
5251 mddev->pers->quiesce(mddev, 1);
5252 if (fd >= 0)
5253 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005254 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005255 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005256 fd = -1; /* make sure to put the file */
5257 }
NeilBrown36fa3062005-09-09 16:23:45 -07005258 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005259 }
5260 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005261 if (mddev->bitmap_info.file) {
5262 restore_bitmap_write_access(mddev->bitmap_info.file);
5263 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005264 }
NeilBrownc3d97142009-12-14 12:49:52 +11005265 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005266 }
5267
NeilBrown32a76272005-06-21 17:17:14 -07005268 return err;
5269}
5270
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271/*
5272 * set_array_info is used two different ways
5273 * The original usage is when creating a new array.
5274 * In this usage, raid_disks is > 0 and it together with
5275 * level, size, not_persistent,layout,chunksize determine the
5276 * shape of the array.
5277 * This will always create an array with a type-0.90.0 superblock.
5278 * The newer usage is when assembling an array.
5279 * In this case raid_disks will be 0, and the major_version field is
5280 * use to determine which style super-blocks are to be found on the devices.
5281 * The minor and patch _version numbers are also kept incase the
5282 * super_block handler wishes to interpret them.
5283 */
5284static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5285{
5286
5287 if (info->raid_disks == 0) {
5288 /* just setting version number for superblock loading */
5289 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005290 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 super_types[info->major_version].name == NULL) {
5292 /* maybe try to auto-load a module? */
5293 printk(KERN_INFO
5294 "md: superblock version %d not known\n",
5295 info->major_version);
5296 return -EINVAL;
5297 }
5298 mddev->major_version = info->major_version;
5299 mddev->minor_version = info->minor_version;
5300 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005301 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005302 /* ensure mddev_put doesn't delete this now that there
5303 * is some minimal configuration.
5304 */
5305 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 return 0;
5307 }
5308 mddev->major_version = MD_MAJOR_VERSION;
5309 mddev->minor_version = MD_MINOR_VERSION;
5310 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5311 mddev->ctime = get_seconds();
5312
5313 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005314 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005315 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 mddev->raid_disks = info->raid_disks;
5317 /* don't set md_minor, it is determined by which /dev/md* was
5318 * openned
5319 */
5320 if (info->state & (1<<MD_SB_CLEAN))
5321 mddev->recovery_cp = MaxSector;
5322 else
5323 mddev->recovery_cp = 0;
5324 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005325 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326
5327 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005328 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329
5330 mddev->max_disks = MD_SB_DISKS;
5331
NeilBrowne6910632008-02-06 01:39:51 -08005332 if (mddev->persistent)
5333 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005334 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
NeilBrownc3d97142009-12-14 12:49:52 +11005336 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5337 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005338
NeilBrownf6705572006-03-27 01:18:11 -08005339 mddev->reshape_position = MaxSector;
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 /*
5342 * Generate a 128 bit UUID
5343 */
5344 get_random_bytes(mddev->uuid, 16);
5345
NeilBrownf6705572006-03-27 01:18:11 -08005346 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005347 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005348 mddev->new_layout = mddev->layout;
5349 mddev->delta_disks = 0;
5350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 return 0;
5352}
5353
Dan Williams1f403622009-03-31 14:59:03 +11005354void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5355{
Dan Williamsb522adc2009-03-31 15:00:31 +11005356 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5357
5358 if (mddev->external_size)
5359 return;
5360
Dan Williams1f403622009-03-31 14:59:03 +11005361 mddev->array_sectors = array_sectors;
5362}
5363EXPORT_SYMBOL(md_set_array_sectors);
5364
Andre Nolld71f9f82008-07-11 22:02:22 +10005365static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005366{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005367 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005368 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005369 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005370
5371 if (mddev->pers->resize == NULL)
5372 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005373 /* The "num_sectors" is the number of sectors of each device that
5374 * is used. This can only make sense for arrays with redundancy.
5375 * linear and raid0 always use whatever space is available. We can only
5376 * consider changing this number if no resync or reconstruction is
5377 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005378 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005379 * of each device. If num_sectors is zero, we find the largest size
5380 * that fits.
5381
NeilBrowna35b0d62006-01-06 00:20:49 -08005382 */
5383 if (mddev->sync_thread)
5384 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005385 if (mddev->bitmap)
5386 /* Sorry, cannot grow a bitmap yet, just remove it,
5387 * grow, and re-add.
5388 */
5389 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005390 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005391 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005392
Andre Nolld71f9f82008-07-11 22:02:22 +10005393 if (fit && (num_sectors == 0 || num_sectors > avail))
5394 num_sectors = avail;
5395 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005396 return -ENOSPC;
5397 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005398 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005399 if (!rv)
5400 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005401 return rv;
5402}
5403
NeilBrownda943b992006-01-06 00:20:54 -08005404static int update_raid_disks(mddev_t *mddev, int raid_disks)
5405{
5406 int rv;
5407 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005408 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005409 return -EINVAL;
5410 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005411 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005412 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005413 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005414 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005415 mddev->delta_disks = raid_disks - mddev->raid_disks;
5416
5417 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005418 return rv;
5419}
5420
5421
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422/*
5423 * update_array_info is used to change the configuration of an
5424 * on-line array.
5425 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5426 * fields in the info are checked against the array.
5427 * Any differences that cannot be handled will cause an error.
5428 * Normally, only one change can be managed at a time.
5429 */
5430static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5431{
5432 int rv = 0;
5433 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005434 int state = 0;
5435
5436 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005437 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005438 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
5440 if (mddev->major_version != info->major_version ||
5441 mddev->minor_version != info->minor_version ||
5442/* mddev->patch_version != info->patch_version || */
5443 mddev->ctime != info->ctime ||
5444 mddev->level != info->level ||
5445/* mddev->layout != info->layout || */
5446 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005447 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005448 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5449 ((state^info->state) & 0xfffffe00)
5450 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 return -EINVAL;
5452 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005453 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5454 cnt++;
5455 if (mddev->raid_disks != info->raid_disks)
5456 cnt++;
5457 if (mddev->layout != info->layout)
5458 cnt++;
5459 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5460 cnt++;
5461 if (cnt == 0)
5462 return 0;
5463 if (cnt > 1)
5464 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465
5466 if (mddev->layout != info->layout) {
5467 /* Change layout
5468 * we don't need to do anything at the md level, the
5469 * personality will take care of it all.
5470 */
NeilBrown50ac1682009-06-18 08:47:55 +10005471 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005473 else {
5474 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005475 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005476 if (rv)
5477 mddev->new_layout = mddev->layout;
5478 return rv;
5479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005481 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005482 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
NeilBrownda943b992006-01-06 00:20:54 -08005484 if (mddev->raid_disks != info->raid_disks)
5485 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486
NeilBrown36fa3062005-09-09 16:23:45 -07005487 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5488 if (mddev->pers->quiesce == NULL)
5489 return -EINVAL;
5490 if (mddev->recovery || mddev->sync_thread)
5491 return -EBUSY;
5492 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5493 /* add the bitmap */
5494 if (mddev->bitmap)
5495 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005496 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005497 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005498 mddev->bitmap_info.offset =
5499 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005500 mddev->pers->quiesce(mddev, 1);
5501 rv = bitmap_create(mddev);
5502 if (rv)
5503 bitmap_destroy(mddev);
5504 mddev->pers->quiesce(mddev, 0);
5505 } else {
5506 /* remove the bitmap */
5507 if (!mddev->bitmap)
5508 return -ENOENT;
5509 if (mddev->bitmap->file)
5510 return -EINVAL;
5511 mddev->pers->quiesce(mddev, 1);
5512 bitmap_destroy(mddev);
5513 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005514 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005515 }
5516 }
NeilBrown850b2b42006-10-03 01:15:46 -07005517 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 return rv;
5519}
5520
5521static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5522{
5523 mdk_rdev_t *rdev;
5524
5525 if (mddev->pers == NULL)
5526 return -ENODEV;
5527
5528 rdev = find_rdev(mddev, dev);
5529 if (!rdev)
5530 return -ENODEV;
5531
5532 md_error(mddev, rdev);
5533 return 0;
5534}
5535
Andre Noll2f9618c2008-04-25 18:57:58 +02005536/*
5537 * We have a problem here : there is no easy way to give a CHS
5538 * virtual geometry. We currently pretend that we have a 2 heads
5539 * 4 sectors (with a BIG number of cylinders...). This drives
5540 * dosfs just mad... ;-)
5541 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005542static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5543{
5544 mddev_t *mddev = bdev->bd_disk->private_data;
5545
5546 geo->heads = 2;
5547 geo->sectors = 4;
5548 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5549 return 0;
5550}
5551
Al Viroa39907f2008-03-02 10:31:15 -05005552static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 unsigned int cmd, unsigned long arg)
5554{
5555 int err = 0;
5556 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005558 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559
5560 if (!capable(CAP_SYS_ADMIN))
5561 return -EACCES;
5562
5563 /*
5564 * Commands dealing with the RAID driver but not any
5565 * particular array:
5566 */
5567 switch (cmd)
5568 {
5569 case RAID_VERSION:
5570 err = get_version(argp);
5571 goto done;
5572
5573 case PRINT_RAID_DEBUG:
5574 err = 0;
5575 md_print_devices();
5576 goto done;
5577
5578#ifndef MODULE
5579 case RAID_AUTORUN:
5580 err = 0;
5581 autostart_arrays(arg);
5582 goto done;
5583#endif
5584 default:;
5585 }
5586
5587 /*
5588 * Commands creating/starting a new array:
5589 */
5590
Al Viroa39907f2008-03-02 10:31:15 -05005591 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
5593 if (!mddev) {
5594 BUG();
5595 goto abort;
5596 }
5597
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 err = mddev_lock(mddev);
5599 if (err) {
5600 printk(KERN_INFO
5601 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5602 err, cmd);
5603 goto abort;
5604 }
5605
5606 switch (cmd)
5607 {
5608 case SET_ARRAY_INFO:
5609 {
5610 mdu_array_info_t info;
5611 if (!arg)
5612 memset(&info, 0, sizeof(info));
5613 else if (copy_from_user(&info, argp, sizeof(info))) {
5614 err = -EFAULT;
5615 goto abort_unlock;
5616 }
5617 if (mddev->pers) {
5618 err = update_array_info(mddev, &info);
5619 if (err) {
5620 printk(KERN_WARNING "md: couldn't update"
5621 " array info. %d\n", err);
5622 goto abort_unlock;
5623 }
5624 goto done_unlock;
5625 }
5626 if (!list_empty(&mddev->disks)) {
5627 printk(KERN_WARNING
5628 "md: array %s already has disks!\n",
5629 mdname(mddev));
5630 err = -EBUSY;
5631 goto abort_unlock;
5632 }
5633 if (mddev->raid_disks) {
5634 printk(KERN_WARNING
5635 "md: array %s already initialised!\n",
5636 mdname(mddev));
5637 err = -EBUSY;
5638 goto abort_unlock;
5639 }
5640 err = set_array_info(mddev, &info);
5641 if (err) {
5642 printk(KERN_WARNING "md: couldn't set"
5643 " array info. %d\n", err);
5644 goto abort_unlock;
5645 }
5646 }
5647 goto done_unlock;
5648
5649 default:;
5650 }
5651
5652 /*
5653 * Commands querying/configuring an existing array:
5654 */
NeilBrown32a76272005-06-21 17:17:14 -07005655 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005656 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005657 if ((!mddev->raid_disks && !mddev->external)
5658 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5659 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5660 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 err = -ENODEV;
5662 goto abort_unlock;
5663 }
5664
5665 /*
5666 * Commands even a read-only array can execute:
5667 */
5668 switch (cmd)
5669 {
5670 case GET_ARRAY_INFO:
5671 err = get_array_info(mddev, argp);
5672 goto done_unlock;
5673
NeilBrown32a76272005-06-21 17:17:14 -07005674 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005675 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005676 goto done_unlock;
5677
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 case GET_DISK_INFO:
5679 err = get_disk_info(mddev, argp);
5680 goto done_unlock;
5681
5682 case RESTART_ARRAY_RW:
5683 err = restart_array(mddev);
5684 goto done_unlock;
5685
5686 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005687 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 goto done_unlock;
5689
5690 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005691 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 goto done_unlock;
5693
Dan Williamse2218352010-05-12 08:25:37 +10005694 case BLKROSET:
5695 if (get_user(ro, (int __user *)(arg))) {
5696 err = -EFAULT;
5697 goto done_unlock;
5698 }
5699 err = -EINVAL;
5700
5701 /* if the bdev is going readonly the value of mddev->ro
5702 * does not matter, no writes are coming
5703 */
5704 if (ro)
5705 goto done_unlock;
5706
5707 /* are we are already prepared for writes? */
5708 if (mddev->ro != 1)
5709 goto done_unlock;
5710
5711 /* transitioning to readauto need only happen for
5712 * arrays that call md_write_start
5713 */
5714 if (mddev->pers) {
5715 err = restart_array(mddev);
5716 if (err == 0) {
5717 mddev->ro = 2;
5718 set_disk_ro(mddev->gendisk, 0);
5719 }
5720 }
5721 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 }
5723
5724 /*
5725 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005726 * superblock, so we do not allow them on read-only arrays.
5727 * However non-MD ioctls (e.g. get-size) will still come through
5728 * here and hit the 'default' below, so only disallow
5729 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005731 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005732 if (mddev->ro == 2) {
5733 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005734 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005735 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5736 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005737 } else {
5738 err = -EROFS;
5739 goto abort_unlock;
5740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 }
5742
5743 switch (cmd)
5744 {
5745 case ADD_NEW_DISK:
5746 {
5747 mdu_disk_info_t info;
5748 if (copy_from_user(&info, argp, sizeof(info)))
5749 err = -EFAULT;
5750 else
5751 err = add_new_disk(mddev, &info);
5752 goto done_unlock;
5753 }
5754
5755 case HOT_REMOVE_DISK:
5756 err = hot_remove_disk(mddev, new_decode_dev(arg));
5757 goto done_unlock;
5758
5759 case HOT_ADD_DISK:
5760 err = hot_add_disk(mddev, new_decode_dev(arg));
5761 goto done_unlock;
5762
5763 case SET_DISK_FAULTY:
5764 err = set_disk_faulty(mddev, new_decode_dev(arg));
5765 goto done_unlock;
5766
5767 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005768 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 goto done_unlock;
5770
NeilBrown32a76272005-06-21 17:17:14 -07005771 case SET_BITMAP_FILE:
5772 err = set_bitmap_file(mddev, (int)arg);
5773 goto done_unlock;
5774
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 err = -EINVAL;
5777 goto abort_unlock;
5778 }
5779
5780done_unlock:
5781abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005782 if (mddev->hold_active == UNTIL_IOCTL &&
5783 err != -EINVAL)
5784 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 mddev_unlock(mddev);
5786
5787 return err;
5788done:
5789 if (err)
5790 MD_BUG();
5791abort:
5792 return err;
5793}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005794#ifdef CONFIG_COMPAT
5795static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5796 unsigned int cmd, unsigned long arg)
5797{
5798 switch (cmd) {
5799 case HOT_REMOVE_DISK:
5800 case HOT_ADD_DISK:
5801 case SET_DISK_FAULTY:
5802 case SET_BITMAP_FILE:
5803 /* These take in integer arg, do not convert */
5804 break;
5805 default:
5806 arg = (unsigned long)compat_ptr(arg);
5807 break;
5808 }
5809
5810 return md_ioctl(bdev, mode, cmd, arg);
5811}
5812#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
Al Viroa39907f2008-03-02 10:31:15 -05005814static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815{
5816 /*
5817 * Succeed if we can lock the mddev, which confirms that
5818 * it isn't being stopped right now.
5819 */
NeilBrownd3374822009-01-09 08:31:10 +11005820 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005821 int err;
5822
NeilBrownd3374822009-01-09 08:31:10 +11005823 if (mddev->gendisk != bdev->bd_disk) {
5824 /* we are racing with mddev_put which is discarding this
5825 * bd_disk.
5826 */
5827 mddev_put(mddev);
5828 /* Wait until bdev->bd_disk is definitely gone */
5829 flush_scheduled_work();
5830 /* Then retry the open from the top */
5831 return -ERESTARTSYS;
5832 }
5833 BUG_ON(mddev != bdev->bd_disk->private_data);
5834
NeilBrownc8c00a62009-08-10 12:50:52 +10005835 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836 goto out;
5837
5838 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005839 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005840 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005841
Al Viroa39907f2008-03-02 10:31:15 -05005842 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 out:
5844 return err;
5845}
5846
Al Viroa39907f2008-03-02 10:31:15 -05005847static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005848{
Al Viroa39907f2008-03-02 10:31:15 -05005849 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005851 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005852 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 mddev_put(mddev);
5854
5855 return 0;
5856}
5857
Linus Torvalds44ce6292007-05-09 18:51:36 -07005858static int md_media_changed(struct gendisk *disk)
5859{
5860 mddev_t *mddev = disk->private_data;
5861
5862 return mddev->changed;
5863}
5864
5865static int md_revalidate(struct gendisk *disk)
5866{
5867 mddev_t *mddev = disk->private_data;
5868
5869 mddev->changed = 0;
5870 return 0;
5871}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005872static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873{
5874 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005875 .open = md_open,
5876 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005877 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005878#ifdef CONFIG_COMPAT
5879 .compat_ioctl = md_compat_ioctl,
5880#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005881 .getgeo = md_getgeo,
Linus Torvalds44ce6292007-05-09 18:51:36 -07005882 .media_changed = md_media_changed,
5883 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884};
5885
Adrian Bunk75c96f82005-05-05 16:16:09 -07005886static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005887{
5888 mdk_thread_t *thread = arg;
5889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 /*
5891 * md_thread is a 'system-thread', it's priority should be very
5892 * high. We avoid resource deadlocks individually in each
5893 * raid personality. (RAID5 does preallocation) We also use RR and
5894 * the very same RT priority as kswapd, thus we will never get
5895 * into a priority inversion deadlock.
5896 *
5897 * we definitely have to have equal or higher priority than
5898 * bdflush, otherwise bdflush will deadlock if there are too
5899 * many dirty RAID5 blocks.
5900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901
NeilBrown6985c432005-10-19 21:23:47 -07005902 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005903 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904
NeilBrown93588e22005-11-15 00:09:12 -08005905 /* We need to wait INTERRUPTIBLE so that
5906 * we don't add to the load-average.
5907 * That means we need to be sure no signals are
5908 * pending
5909 */
5910 if (signal_pending(current))
5911 flush_signals(current);
5912
5913 wait_event_interruptible_timeout
5914 (thread->wqueue,
5915 test_bit(THREAD_WAKEUP, &thread->flags)
5916 || kthread_should_stop(),
5917 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
5919 clear_bit(THREAD_WAKEUP, &thread->flags);
5920
NeilBrown787453c2005-11-08 21:39:43 -08005921 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005922 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005923
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 return 0;
5925}
5926
5927void md_wakeup_thread(mdk_thread_t *thread)
5928{
5929 if (thread) {
5930 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5931 set_bit(THREAD_WAKEUP, &thread->flags);
5932 wake_up(&thread->wqueue);
5933 }
5934}
5935
5936mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5937 const char *name)
5938{
5939 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005940
NeilBrown9ffae0c2006-01-06 00:20:32 -08005941 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942 if (!thread)
5943 return NULL;
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945 init_waitqueue_head(&thread->wqueue);
5946
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 thread->run = run;
5948 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005949 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005950 thread->tsk = kthread_run(md_thread, thread,
5951 "%s_%s",
5952 mdname(thread->mddev),
5953 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005954 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 kfree(thread);
5956 return NULL;
5957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 return thread;
5959}
5960
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961void md_unregister_thread(mdk_thread_t *thread)
5962{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005963 if (!thread)
5964 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005965 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005966
5967 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 kfree(thread);
5969}
5970
5971void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5972{
5973 if (!mddev) {
5974 MD_BUG();
5975 return;
5976 }
5977
NeilBrownb2d444d2005-11-08 21:39:31 -08005978 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005980
5981 if (mddev->external)
5982 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005983/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5985 mdname(mddev),
5986 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5987 __builtin_return_address(0),__builtin_return_address(1),
5988 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005989*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005990 if (!mddev->pers)
5991 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 if (!mddev->pers->error_handler)
5993 return;
5994 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005995 if (mddev->degraded)
5996 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11005997 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5999 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6000 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07006001 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002}
6003
6004/* seq_file implementation /proc/mdstat */
6005
6006static void status_unused(struct seq_file *seq)
6007{
6008 int i = 0;
6009 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010
6011 seq_printf(seq, "unused devices: ");
6012
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006013 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 char b[BDEVNAME_SIZE];
6015 i++;
6016 seq_printf(seq, "%s ",
6017 bdevname(rdev->bdev,b));
6018 }
6019 if (!i)
6020 seq_printf(seq, "<none>");
6021
6022 seq_printf(seq, "\n");
6023}
6024
6025
6026static void status_resync(struct seq_file *seq, mddev_t * mddev)
6027{
NeilBrowndd71cf62009-05-07 12:49:35 +10006028 sector_t max_sectors, resync, res;
6029 unsigned long dt, db;
6030 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006031 int scale;
6032 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033
NeilBrowndd71cf62009-05-07 12:49:35 +10006034 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035
6036 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006037 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006039 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
6041 /*
6042 * Should not happen.
6043 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006044 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 MD_BUG();
6046 return;
6047 }
NeilBrown4588b422006-03-27 01:18:04 -08006048 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006049 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006050 * u32, as those are the requirements for sector_div.
6051 * Thus 'scale' must be at least 10
6052 */
6053 scale = 10;
6054 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006055 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006056 scale++;
6057 }
6058 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006059 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006060
6061 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006062 {
NeilBrown4588b422006-03-27 01:18:04 -08006063 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006064 seq_printf(seq, "[");
6065 for (i = 0; i < x; i++)
6066 seq_printf(seq, "=");
6067 seq_printf(seq, ">");
6068 for (i = 0; i < y; i++)
6069 seq_printf(seq, ".");
6070 seq_printf(seq, "] ");
6071 }
NeilBrown4588b422006-03-27 01:18:04 -08006072 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006073 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6074 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006075 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6076 "check" :
6077 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6078 "resync" : "recovery"))),
6079 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006080 (unsigned long long) resync/2,
6081 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082
6083 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084 * dt: time from mark until now
6085 * db: blocks written from mark until now
6086 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006087 *
6088 * rt is a sector_t, so could be 32bit or 64bit.
6089 * So we divide before multiply in case it is 32bit and close
6090 * to the limit.
6091 * We scale the divisor (db) by 32 to avoid loosing precision
6092 * near the end of resync when the number of remaining sectors
6093 * is close to 'db'.
6094 * We then divide rt by 32 after multiplying by db to compensate.
6095 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 */
6097 dt = ((jiffies - mddev->resync_mark) / HZ);
6098 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006099 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6100 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101
NeilBrowndd71cf62009-05-07 12:49:35 +10006102 rt = max_sectors - resync; /* number of remaining sectors */
6103 sector_div(rt, db/32+1);
6104 rt *= dt;
6105 rt >>= 5;
6106
6107 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6108 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006109
NeilBrownff4e8d92006-07-10 04:44:16 -07006110 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006111}
6112
6113static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6114{
6115 struct list_head *tmp;
6116 loff_t l = *pos;
6117 mddev_t *mddev;
6118
6119 if (l >= 0x10000)
6120 return NULL;
6121 if (!l--)
6122 /* header */
6123 return (void*)1;
6124
6125 spin_lock(&all_mddevs_lock);
6126 list_for_each(tmp,&all_mddevs)
6127 if (!l--) {
6128 mddev = list_entry(tmp, mddev_t, all_mddevs);
6129 mddev_get(mddev);
6130 spin_unlock(&all_mddevs_lock);
6131 return mddev;
6132 }
6133 spin_unlock(&all_mddevs_lock);
6134 if (!l--)
6135 return (void*)2;/* tail */
6136 return NULL;
6137}
6138
6139static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6140{
6141 struct list_head *tmp;
6142 mddev_t *next_mddev, *mddev = v;
6143
6144 ++*pos;
6145 if (v == (void*)2)
6146 return NULL;
6147
6148 spin_lock(&all_mddevs_lock);
6149 if (v == (void*)1)
6150 tmp = all_mddevs.next;
6151 else
6152 tmp = mddev->all_mddevs.next;
6153 if (tmp != &all_mddevs)
6154 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6155 else {
6156 next_mddev = (void*)2;
6157 *pos = 0x10000;
6158 }
6159 spin_unlock(&all_mddevs_lock);
6160
6161 if (v != (void*)1)
6162 mddev_put(mddev);
6163 return next_mddev;
6164
6165}
6166
6167static void md_seq_stop(struct seq_file *seq, void *v)
6168{
6169 mddev_t *mddev = v;
6170
6171 if (mddev && v != (void*)1 && v != (void*)2)
6172 mddev_put(mddev);
6173}
6174
NeilBrownd7603b72006-01-06 00:20:30 -08006175struct mdstat_info {
6176 int event;
6177};
6178
Linus Torvalds1da177e2005-04-16 15:20:36 -07006179static int md_seq_show(struct seq_file *seq, void *v)
6180{
6181 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006182 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006183 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006184 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006185 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186
6187 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006188 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006189 seq_printf(seq, "Personalities : ");
6190 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006191 list_for_each_entry(pers, &pers_list, list)
6192 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193
6194 spin_unlock(&pers_lock);
6195 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006196 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197 return 0;
6198 }
6199 if (v == (void*)2) {
6200 status_unused(seq);
6201 return 0;
6202 }
6203
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006204 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006206
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6208 seq_printf(seq, "%s : %sactive", mdname(mddev),
6209 mddev->pers ? "" : "in");
6210 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006211 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006213 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006214 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 seq_printf(seq, " %s", mddev->pers->name);
6216 }
6217
Andre Nolldd8ac332009-03-31 14:33:13 +11006218 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006219 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220 char b[BDEVNAME_SIZE];
6221 seq_printf(seq, " %s[%d]",
6222 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006223 if (test_bit(WriteMostly, &rdev->flags))
6224 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006225 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 seq_printf(seq, "(F)");
6227 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006228 } else if (rdev->raid_disk < 0)
6229 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006230 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 }
6232
6233 if (!list_empty(&mddev->disks)) {
6234 if (mddev->pers)
6235 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006236 (unsigned long long)
6237 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006238 else
6239 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006240 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006242 if (mddev->persistent) {
6243 if (mddev->major_version != 0 ||
6244 mddev->minor_version != 90) {
6245 seq_printf(seq," super %d.%d",
6246 mddev->major_version,
6247 mddev->minor_version);
6248 }
NeilBrowne6910632008-02-06 01:39:51 -08006249 } else if (mddev->external)
6250 seq_printf(seq, " super external:%s",
6251 mddev->metadata_type);
6252 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006253 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254
6255 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006256 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006258 if (mddev->pers->sync_request) {
6259 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006260 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006261 seq_printf(seq, "\n ");
6262 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6263 seq_printf(seq, "\tresync=DELAYED\n ");
6264 else if (mddev->recovery_cp < MaxSector)
6265 seq_printf(seq, "\tresync=PENDING\n ");
6266 }
NeilBrown32a76272005-06-21 17:17:14 -07006267 } else
6268 seq_printf(seq, "\n ");
6269
6270 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006271 unsigned long chunk_kb;
6272 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006273 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006274 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006275 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6276 "%lu%s chunk",
6277 bitmap->pages - bitmap->missing_pages,
6278 bitmap->pages,
6279 (bitmap->pages - bitmap->missing_pages)
6280 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006281 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006282 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006283 if (bitmap->file) {
6284 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006285 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006286 }
NeilBrown78d742d2005-06-21 17:17:15 -07006287
NeilBrown32a76272005-06-21 17:17:14 -07006288 seq_printf(seq, "\n");
6289 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290 }
6291
6292 seq_printf(seq, "\n");
6293 }
6294 mddev_unlock(mddev);
6295
6296 return 0;
6297}
6298
Jan Engelhardt110518b2009-05-07 12:49:37 +10006299static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006300 .start = md_seq_start,
6301 .next = md_seq_next,
6302 .stop = md_seq_stop,
6303 .show = md_seq_show,
6304};
6305
6306static int md_seq_open(struct inode *inode, struct file *file)
6307{
6308 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006309 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6310 if (mi == NULL)
6311 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312
6313 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006314 if (error)
6315 kfree(mi);
6316 else {
6317 struct seq_file *p = file->private_data;
6318 p->private = mi;
6319 mi->event = atomic_read(&md_event_count);
6320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006321 return error;
6322}
6323
NeilBrownd7603b72006-01-06 00:20:30 -08006324static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6325{
6326 struct seq_file *m = filp->private_data;
6327 struct mdstat_info *mi = m->private;
6328 int mask;
6329
6330 poll_wait(filp, &md_event_waiters, wait);
6331
6332 /* always allow read */
6333 mask = POLLIN | POLLRDNORM;
6334
6335 if (mi->event != atomic_read(&md_event_count))
6336 mask |= POLLERR | POLLPRI;
6337 return mask;
6338}
6339
Arjan van de Venfa027c22007-02-12 00:55:33 -08006340static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006341 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 .open = md_seq_open,
6343 .read = seq_read,
6344 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006345 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006346 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347};
6348
NeilBrown2604b702006-01-06 00:20:36 -08006349int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006352 list_add_tail(&p->list, &pers_list);
6353 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 spin_unlock(&pers_lock);
6355 return 0;
6356}
6357
NeilBrown2604b702006-01-06 00:20:36 -08006358int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359{
NeilBrown2604b702006-01-06 00:20:36 -08006360 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006362 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 spin_unlock(&pers_lock);
6364 return 0;
6365}
6366
NeilBrowneea1bf32009-03-31 14:27:02 +11006367static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006368{
6369 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006371 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372
6373 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006374 rcu_read_lock();
6375 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006377 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6378 (int)part_stat_read(&disk->part0, sectors[1]) -
6379 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006380 /* sync IO will cause sync_io to increase before the disk_stats
6381 * as sync_io is counted when a request starts, and
6382 * disk_stats is counted when it completes.
6383 * So resync activity will cause curr_events to be smaller than
6384 * when there was no such activity.
6385 * non-sync IO will cause disk_stat to increase without
6386 * increasing sync_io so curr_events will (eventually)
6387 * be larger than it was before. Once it becomes
6388 * substantially larger, the test below will cause
6389 * the array to appear non-idle, and resync will slow
6390 * down.
6391 * If there is a lot of outstanding resync activity when
6392 * we set last_event to curr_events, then all that activity
6393 * completing might cause the array to appear non-idle
6394 * and resync will be slowed down even though there might
6395 * not have been non-resync activity. This will only
6396 * happen once though. 'last_events' will soon reflect
6397 * the state where there is little or no outstanding
6398 * resync requests, and further resync activity will
6399 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006400 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006402 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403 rdev->last_events = curr_events;
6404 idle = 0;
6405 }
6406 }
NeilBrown4b809912008-07-21 17:05:25 +10006407 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006408 return idle;
6409}
6410
6411void md_done_sync(mddev_t *mddev, int blocks, int ok)
6412{
6413 /* another "blocks" (512byte) blocks have been synced */
6414 atomic_sub(blocks, &mddev->recovery_active);
6415 wake_up(&mddev->recovery_wait);
6416 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006417 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006418 md_wakeup_thread(mddev->thread);
6419 // stop recovery, signal do_sync ....
6420 }
6421}
6422
6423
NeilBrown06d91a52005-06-21 17:17:12 -07006424/* md_write_start(mddev, bi)
6425 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006426 * in superblock) before writing, schedule a superblock update
6427 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006428 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006429void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006430{
Neil Brown0fd62b82008-06-28 08:31:36 +10006431 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006432 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006433 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006434
NeilBrownf91de922005-11-08 21:39:36 -08006435 BUG_ON(mddev->ro == 1);
6436 if (mddev->ro == 2) {
6437 /* need to switch to read/write */
6438 mddev->ro = 0;
6439 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6440 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006441 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006442 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006443 }
NeilBrown06d91a52005-06-21 17:17:12 -07006444 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006445 if (mddev->safemode == 1)
6446 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006447 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006448 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006449 if (mddev->in_sync) {
6450 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006451 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006452 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006453 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006454 }
NeilBrowna9701a32005-11-08 21:39:34 -08006455 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006456 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006457 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006458 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006459 wait_event(mddev->sb_wait,
6460 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6461 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006462}
6463
6464void md_write_end(mddev_t *mddev)
6465{
6466 if (atomic_dec_and_test(&mddev->writes_pending)) {
6467 if (mddev->safemode == 2)
6468 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006469 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6471 }
6472}
6473
NeilBrown2a2275d2007-01-26 00:57:11 -08006474/* md_allow_write(mddev)
6475 * Calling this ensures that the array is marked 'active' so that writes
6476 * may proceed without blocking. It is important to call this before
6477 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6478 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006479 *
6480 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6481 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006482 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006483int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006484{
6485 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006486 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006487 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006488 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006489 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006490 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006491
6492 spin_lock_irq(&mddev->write_lock);
6493 if (mddev->in_sync) {
6494 mddev->in_sync = 0;
6495 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6496 if (mddev->safemode_delay &&
6497 mddev->safemode == 0)
6498 mddev->safemode = 1;
6499 spin_unlock_irq(&mddev->write_lock);
6500 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006501 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006502 } else
6503 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006504
6505 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6506 return -EAGAIN;
6507 else
6508 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006509}
6510EXPORT_SYMBOL_GPL(md_allow_write);
6511
Linus Torvalds1da177e2005-04-16 15:20:36 -07006512#define SYNC_MARKS 10
6513#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006514void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006515{
6516 mddev_t *mddev2;
6517 unsigned int currspeed = 0,
6518 window;
NeilBrown57afd892005-06-21 17:17:13 -07006519 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006520 unsigned long mark[SYNC_MARKS];
6521 sector_t mark_cnt[SYNC_MARKS];
6522 int last_mark,m;
6523 struct list_head *tmp;
6524 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006525 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006526 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006527 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528
6529 /* just incase thread restarts... */
6530 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6531 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006532 if (mddev->ro) /* never try to sync a read-only array */
6533 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534
NeilBrown61df9d92006-10-03 01:15:57 -07006535 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6536 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6537 desc = "data-check";
6538 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6539 desc = "requested-resync";
6540 else
6541 desc = "resync";
6542 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6543 desc = "reshape";
6544 else
6545 desc = "recovery";
6546
Linus Torvalds1da177e2005-04-16 15:20:36 -07006547 /* we overload curr_resync somewhat here.
6548 * 0 == not engaged in resync at all
6549 * 2 == checking that there is no conflict with another sync
6550 * 1 == like 2, but have yielded to allow conflicting resync to
6551 * commense
6552 * other == active in resync - this many blocks
6553 *
6554 * Before starting a resync we must have set curr_resync to
6555 * 2, and then checked that every "conflicting" array has curr_resync
6556 * less than ours. When we find one that is the same or higher
6557 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6558 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6559 * This will mean we have to start checking from the beginning again.
6560 *
6561 */
6562
6563 do {
6564 mddev->curr_resync = 2;
6565
6566 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006567 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006568 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006569
6570 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006572 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573 if (mddev2 == mddev)
6574 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006575 if (!mddev->parallel_resync
6576 && mddev2->curr_resync
6577 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006578 DEFINE_WAIT(wq);
6579 if (mddev < mddev2 && mddev->curr_resync == 2) {
6580 /* arbitrarily yield */
6581 mddev->curr_resync = 1;
6582 wake_up(&resync_wait);
6583 }
6584 if (mddev > mddev2 && mddev->curr_resync == 1)
6585 /* no need to wait here, we can wait the next
6586 * time 'round when curr_resync == 2
6587 */
6588 continue;
NeilBrown97441972008-09-19 11:49:54 +10006589 /* We need to wait 'interruptible' so as not to
6590 * contribute to the load average, and not to
6591 * be caught by 'softlockup'
6592 */
6593 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006594 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006595 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006596 printk(KERN_INFO "md: delaying %s of %s"
6597 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006599 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006601 if (signal_pending(current))
6602 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 schedule();
6604 finish_wait(&resync_wait, &wq);
6605 goto try_again;
6606 }
6607 finish_wait(&resync_wait, &wq);
6608 }
6609 }
6610 } while (mddev->curr_resync < 2);
6611
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006612 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006613 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006615 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616 */
6617 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006618 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006619 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006620 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6621 j = mddev->resync_min;
6622 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006623 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006624
NeilBrownccfcc3c2006-03-27 01:18:09 -08006625 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006626 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006627 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006629 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006630 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006631 rcu_read_lock();
6632 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006633 if (rdev->raid_disk >= 0 &&
6634 !test_bit(Faulty, &rdev->flags) &&
6635 !test_bit(In_sync, &rdev->flags) &&
6636 rdev->recovery_offset < j)
6637 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006638 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
NeilBrown61df9d92006-10-03 01:15:57 -07006641 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6642 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6643 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006644 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006645 "(but not more than %d KB/sec) for %s.\n",
6646 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647
NeilBrowneea1bf32009-03-31 14:27:02 +11006648 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006649
NeilBrown57afd892005-06-21 17:17:13 -07006650 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 for (m = 0; m < SYNC_MARKS; m++) {
6652 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006653 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 }
6655 last_mark = 0;
6656 mddev->resync_mark = mark[last_mark];
6657 mddev->resync_mark_cnt = mark_cnt[last_mark];
6658
6659 /*
6660 * Tune reconstruction:
6661 */
6662 window = 32*(PAGE_SIZE/512);
6663 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6664 window/2,(unsigned long long) max_sectors/2);
6665
6666 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667 last_check = 0;
6668
6669 if (j>2) {
6670 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006671 "md: resuming %s of %s from checkpoint.\n",
6672 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673 mddev->curr_resync = j;
6674 }
NeilBrownefa59332009-12-14 12:49:49 +11006675 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676
6677 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006678 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006679
NeilBrown57afd892005-06-21 17:17:13 -07006680 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006681
NeilBrown7a91ee12009-05-26 12:57:21 +10006682 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6683 ((mddev->curr_resync > mddev->curr_resync_completed &&
6684 (mddev->curr_resync - mddev->curr_resync_completed)
6685 > (max_sectors >> 4)) ||
6686 (j - mddev->curr_resync_completed)*2
6687 >= mddev->resync_max - mddev->curr_resync_completed
6688 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006689 /* time to update curr_resync_completed */
6690 blk_unplug(mddev->queue);
6691 wait_event(mddev->recovery_wait,
6692 atomic_read(&mddev->recovery_active) == 0);
6693 mddev->curr_resync_completed =
6694 mddev->curr_resync;
6695 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006696 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006697 }
NeilBrownacb180b2009-04-14 16:28:34 +10006698
NeilBrowne62e58a2009-07-01 13:15:35 +10006699 while (j >= mddev->resync_max && !kthread_should_stop()) {
6700 /* As this condition is controlled by user-space,
6701 * we can block indefinitely, so use '_interruptible'
6702 * to avoid triggering warnings.
6703 */
6704 flush_signals(current); /* just in case */
6705 wait_event_interruptible(mddev->recovery_wait,
6706 mddev->resync_max > j
6707 || kthread_should_stop());
6708 }
NeilBrownacb180b2009-04-14 16:28:34 +10006709
6710 if (kthread_should_stop())
6711 goto interrupted;
6712
NeilBrown57afd892005-06-21 17:17:13 -07006713 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006714 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006715 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006716 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006717 goto out;
6718 }
NeilBrown57afd892005-06-21 17:17:13 -07006719
6720 if (!skipped) { /* actual IO requested */
6721 io_sectors += sectors;
6722 atomic_add(sectors, &mddev->recovery_active);
6723 }
6724
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 j += sectors;
6726 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006727 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006728 if (last_check == 0)
6729 /* this is the earliers that rebuilt will be
6730 * visible in /proc/mdstat
6731 */
6732 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006733
6734 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006735 continue;
6736
NeilBrown57afd892005-06-21 17:17:13 -07006737 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
NeilBrowndfc70642008-05-23 13:04:39 -07006739 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740 break;
6741
6742 repeat:
6743 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6744 /* step marks */
6745 int next = (last_mark+1) % SYNC_MARKS;
6746
6747 mddev->resync_mark = mark[next];
6748 mddev->resync_mark_cnt = mark_cnt[next];
6749 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006750 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 last_mark = next;
6752 }
6753
6754
NeilBrownc6207272008-02-06 01:39:52 -08006755 if (kthread_should_stop())
6756 goto interrupted;
6757
Linus Torvalds1da177e2005-04-16 15:20:36 -07006758
6759 /*
6760 * this loop exits only if either when we are slower than
6761 * the 'hard' speed limit, or the system was IO-idle for
6762 * a jiffy.
6763 * the system might be non-idle CPU-wise, but we only care
6764 * about not overloading the IO subsystem. (things like an
6765 * e2fsck being done on the RAID array should execute fast)
6766 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006767 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 cond_resched();
6769
NeilBrown57afd892005-06-21 17:17:13 -07006770 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6771 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772
NeilBrown88202a02006-01-06 00:21:36 -08006773 if (currspeed > speed_min(mddev)) {
6774 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006775 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006776 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006777 goto repeat;
6778 }
6779 }
6780 }
NeilBrown61df9d92006-10-03 01:15:57 -07006781 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 /*
6783 * this also signals 'finished resyncing' to md_stop
6784 */
6785 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006786 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
6788 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6789
6790 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006791 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006792
NeilBrowndfc70642008-05-23 13:04:39 -07006793 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006794 mddev->curr_resync > 2) {
6795 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6796 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6797 if (mddev->curr_resync >= mddev->recovery_cp) {
6798 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006799 "md: checkpointing %s of %s.\n",
6800 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006801 mddev->recovery_cp = mddev->curr_resync;
6802 }
6803 } else
6804 mddev->recovery_cp = MaxSector;
6805 } else {
6806 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6807 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006808 rcu_read_lock();
6809 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006810 if (rdev->raid_disk >= 0 &&
6811 !test_bit(Faulty, &rdev->flags) &&
6812 !test_bit(In_sync, &rdev->flags) &&
6813 rdev->recovery_offset < mddev->curr_resync)
6814 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006815 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817 }
NeilBrown17571282006-12-10 02:20:52 -08006818 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006821 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6822 /* We completed so min/max setting can be forgotten if used. */
6823 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6824 mddev->resync_min = 0;
6825 mddev->resync_max = MaxSector;
6826 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6827 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006829 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6830 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006831 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006832 wake_up(&resync_wait);
6833 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6834 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006835 return;
6836
6837 interrupted:
6838 /*
6839 * got a signal, exit.
6840 */
6841 printk(KERN_INFO
6842 "md: md_do_sync() got signal ... exiting\n");
6843 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6844 goto out;
6845
Linus Torvalds1da177e2005-04-16 15:20:36 -07006846}
NeilBrown29269552006-03-27 01:18:10 -08006847EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006848
6849
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006850static int remove_and_add_spares(mddev_t *mddev)
6851{
6852 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006853 int spares = 0;
6854
NeilBrown97e4f422009-03-31 14:33:13 +11006855 mddev->curr_resync_completed = 0;
6856
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006857 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006858 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006859 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006860 (test_bit(Faulty, &rdev->flags) ||
6861 ! test_bit(In_sync, &rdev->flags)) &&
6862 atomic_read(&rdev->nr_pending)==0) {
6863 if (mddev->pers->hot_remove_disk(
6864 mddev, rdev->raid_disk)==0) {
6865 char nm[20];
6866 sprintf(nm,"rd%d", rdev->raid_disk);
6867 sysfs_remove_link(&mddev->kobj, nm);
6868 rdev->raid_disk = -1;
6869 }
6870 }
6871
NeilBrown4044ba52009-01-09 08:31:11 +11006872 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006873 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006874 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006875 !test_bit(In_sync, &rdev->flags) &&
6876 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006877 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006878 if (rdev->raid_disk < 0
6879 && !test_bit(Faulty, &rdev->flags)) {
6880 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006881 if (mddev->pers->
6882 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006883 char nm[20];
6884 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006885 if (sysfs_create_link(&mddev->kobj,
6886 &rdev->kobj, nm))
6887 printk(KERN_WARNING
6888 "md: cannot register "
6889 "%s for %s\n",
6890 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006891 spares++;
6892 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006893 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006894 } else
6895 break;
6896 }
NeilBrowndfc70642008-05-23 13:04:39 -07006897 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006898 }
6899 return spares;
6900}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901/*
6902 * This routine is regularly called by all per-raid-array threads to
6903 * deal with generic issues like resync and super-block update.
6904 * Raid personalities that don't have a thread (linear/raid0) do not
6905 * need this as they never do any recovery or update the superblock.
6906 *
6907 * It does not do any resync itself, but rather "forks" off other threads
6908 * to do that as needed.
6909 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6910 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006911 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912 * and wakeups up this thread which will reap the thread and finish up.
6913 * This thread also removes any faulty devices (with nr_pending == 0).
6914 *
6915 * The overall approach is:
6916 * 1/ if the superblock needs updating, update it.
6917 * 2/ If a recovery thread is running, don't do anything else.
6918 * 3/ If recovery has finished, clean up, possibly marking spares active.
6919 * 4/ If there are any faulty devices, remove them.
6920 * 5/ If array is degraded, try to add spares devices
6921 * 6/ If array has spares or is not in-sync, start a resync thread.
6922 */
6923void md_check_recovery(mddev_t *mddev)
6924{
6925 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006926
6927
NeilBrown5f404022005-06-21 17:17:16 -07006928 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006929 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006930
6931 if (mddev->ro)
6932 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006933
6934 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006935 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006936 printk(KERN_INFO "md: %s in immediate safe mode\n",
6937 mdname(mddev));
6938 mddev->safemode = 2;
6939 }
6940 flush_signals(current);
6941 }
6942
NeilBrownc89a8ee2008-08-05 15:54:13 +10006943 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6944 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006945 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006946 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006947 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006948 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006949 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006950 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6951 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006952 ))
6953 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006954
NeilBrowndf5b89b2006-03-27 01:18:20 -08006955 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006956 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006957
NeilBrownc89a8ee2008-08-05 15:54:13 +10006958 if (mddev->ro) {
6959 /* Only thing we do on a ro array is remove
6960 * failed devices.
6961 */
6962 remove_and_add_spares(mddev);
6963 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6964 goto unlock;
6965 }
6966
NeilBrown31a59e32008-04-30 00:52:30 -07006967 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006968 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006969 spin_lock_irq(&mddev->write_lock);
6970 if (mddev->safemode &&
6971 !atomic_read(&mddev->writes_pending) &&
6972 !mddev->in_sync &&
6973 mddev->recovery_cp == MaxSector) {
6974 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006975 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006976 if (mddev->persistent)
6977 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6978 }
6979 if (mddev->safemode == 1)
6980 mddev->safemode = 0;
6981 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006982 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006983 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006984 }
NeilBrownfca4d842005-06-21 17:17:11 -07006985
NeilBrown850b2b42006-10-03 01:15:46 -07006986 if (mddev->flags)
6987 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006988
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6990 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6991 /* resync/recovery still happening */
6992 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6993 goto unlock;
6994 }
6995 if (mddev->sync_thread) {
6996 /* resync has finished, collect result */
6997 md_unregister_thread(mddev->sync_thread);
6998 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006999 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7000 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 /* success...*/
7002 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007003 if (mddev->pers->spare_active(mddev))
7004 sysfs_notify(&mddev->kobj, NULL,
7005 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 }
NeilBrowncea9c222009-03-31 15:15:05 +11007007 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7008 mddev->pers->finish_reshape)
7009 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07007010 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007011
7012 /* if array is no-longer degraded, then any saved_raid_disk
7013 * information must be scrapped
7014 */
7015 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007016 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007017 rdev->saved_raid_disk = -1;
7018
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 mddev->recovery = 0;
7020 /* flag recovery needed just to double check */
7021 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007022 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007023 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007024 goto unlock;
7025 }
Neil Brown72a23c22008-06-28 08:31:41 +10007026 /* Set RUNNING before clearing NEEDED to avoid
7027 * any transients in the value of "sync_action".
7028 */
7029 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7030 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007031 /* Clear some bits that don't mean anything, but
7032 * might be left set
7033 */
NeilBrown24dd4692005-11-08 21:39:26 -08007034 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7035 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007037 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7038 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039 /* no recovery is running.
7040 * remove any failed drives, then
7041 * add spares if possible.
7042 * Spare are also removed and re-added, to allow
7043 * the personality to fail the re-add.
7044 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007045
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007046 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007047 if (mddev->pers->check_reshape == NULL ||
7048 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007049 /* Cannot proceed */
7050 goto unlock;
7051 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007052 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007053 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007054 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7055 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007056 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007057 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007058 } else if (mddev->recovery_cp < MaxSector) {
7059 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007060 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007061 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7062 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007064
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007066 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7067 /* We are adding a device or devices to an array
7068 * which has the bitmap stored on all devices.
7069 * So make sure all bitmap pages get written
7070 */
7071 bitmap_write_all(mddev->bitmap);
7072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 mddev->sync_thread = md_register_thread(md_do_sync,
7074 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007075 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 if (!mddev->sync_thread) {
7077 printk(KERN_ERR "%s: could not start resync"
7078 " thread...\n",
7079 mdname(mddev));
7080 /* leave the spares where they are, it shouldn't hurt */
7081 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007082 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007084 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007085 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 }
7087 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007088 if (!mddev->sync_thread) {
7089 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7090 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7091 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007092 if (mddev->sysfs_action)
7093 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 mddev_unlock(mddev);
7096 }
7097}
7098
Dan Williams6bfe0b42008-04-30 00:52:32 -07007099void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7100{
NeilBrown3c0ee632008-10-21 13:25:28 +11007101 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007102 wait_event_timeout(rdev->blocked_wait,
7103 !test_bit(Blocked, &rdev->flags),
7104 msecs_to_jiffies(5000));
7105 rdev_dec_pending(rdev, mddev);
7106}
7107EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7108
Adrian Bunk75c96f82005-05-05 16:16:09 -07007109static int md_notify_reboot(struct notifier_block *this,
7110 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007111{
7112 struct list_head *tmp;
7113 mddev_t *mddev;
7114
7115 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7116
7117 printk(KERN_INFO "md: stopping all md devices.\n");
7118
NeilBrown29ac4aa2008-02-06 01:39:58 -08007119 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007120 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007121 /* Force a switch to readonly even array
7122 * appears to still be in use. Hence
7123 * the '100'.
7124 */
NeilBrownd710e132008-10-13 11:55:12 +11007125 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007126 mddev_unlock(mddev);
7127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128 /*
7129 * certain more exotic SCSI devices are known to be
7130 * volatile wrt too early system reboots. While the
7131 * right place to handle this issue is the given
7132 * driver, we do want to have a safe RAID driver ...
7133 */
7134 mdelay(1000*1);
7135 }
7136 return NOTIFY_DONE;
7137}
7138
Adrian Bunk75c96f82005-05-05 16:16:09 -07007139static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140 .notifier_call = md_notify_reboot,
7141 .next = NULL,
7142 .priority = INT_MAX, /* before any real devices */
7143};
7144
7145static void md_geninit(void)
7146{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007147 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7148
Denis V. Lunevc7705f32008-04-29 01:02:35 -07007149 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150}
7151
Adrian Bunk75c96f82005-05-05 16:16:09 -07007152static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007153{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007154 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155 return -1;
7156 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007157 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 return -1;
7159 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007160 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007161 md_probe, NULL, NULL);
7162 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163 md_probe, NULL, NULL);
7164
Linus Torvalds1da177e2005-04-16 15:20:36 -07007165 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007166 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167
7168 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170}
7171
7172
7173#ifndef MODULE
7174
7175/*
7176 * Searches all registered partitions for autorun RAID arrays
7177 * at boot time.
7178 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007179
7180static LIST_HEAD(all_detected_devices);
7181struct detected_devices_node {
7182 struct list_head list;
7183 dev_t dev;
7184};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185
7186void md_autodetect_dev(dev_t dev)
7187{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007188 struct detected_devices_node *node_detected_dev;
7189
7190 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7191 if (node_detected_dev) {
7192 node_detected_dev->dev = dev;
7193 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7194 } else {
7195 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7196 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198}
7199
7200
7201static void autostart_arrays(int part)
7202{
7203 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007204 struct detected_devices_node *node_detected_dev;
7205 dev_t dev;
7206 int i_scanned, i_passed;
7207
7208 i_scanned = 0;
7209 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007210
7211 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7212
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007213 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7214 i_scanned++;
7215 node_detected_dev = list_entry(all_detected_devices.next,
7216 struct detected_devices_node, list);
7217 list_del(&node_detected_dev->list);
7218 dev = node_detected_dev->dev;
7219 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007220 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221 if (IS_ERR(rdev))
7222 continue;
7223
NeilBrownb2d444d2005-11-08 21:39:31 -08007224 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007225 MD_BUG();
7226 continue;
7227 }
NeilBrownd0fae182008-03-04 14:29:31 -08007228 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007229 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007230 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007232
7233 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7234 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007235
7236 autorun_devices(part);
7237}
7238
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007239#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
7241static __exit void md_exit(void)
7242{
7243 mddev_t *mddev;
7244 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007245
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007246 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007247 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007249 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007250 unregister_blkdev(mdp_major, "mdp");
7251 unregister_reboot_notifier(&md_notifier);
7252 unregister_sysctl_table(raid_table_header);
7253 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007254 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007256 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007257 }
7258}
7259
Dan Williams685784a2007-07-09 11:56:42 -07007260subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261module_exit(md_exit)
7262
NeilBrownf91de922005-11-08 21:39:36 -08007263static int get_ro(char *buffer, struct kernel_param *kp)
7264{
7265 return sprintf(buffer, "%d", start_readonly);
7266}
7267static int set_ro(const char *val, struct kernel_param *kp)
7268{
7269 char *e;
7270 int num = simple_strtoul(val, &e, 10);
7271 if (*val && (*e == '\0' || *e == '\n')) {
7272 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007273 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007274 }
7275 return -EINVAL;
7276}
7277
NeilBrown80ca3a42006-07-10 04:44:18 -07007278module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7279module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007280
NeilBrownefeb53c2009-01-09 08:31:10 +11007281module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007282
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283EXPORT_SYMBOL(register_md_personality);
7284EXPORT_SYMBOL(unregister_md_personality);
7285EXPORT_SYMBOL(md_error);
7286EXPORT_SYMBOL(md_done_sync);
7287EXPORT_SYMBOL(md_write_start);
7288EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007289EXPORT_SYMBOL(md_register_thread);
7290EXPORT_SYMBOL(md_unregister_thread);
7291EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007292EXPORT_SYMBOL(md_check_recovery);
7293MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007294MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007295MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007296MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);