blob: f2b30019b1cb4b1f6a2ff2e06f053b3b1c1910c7 [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{
NeilBrown49077322010-03-25 16:20:56 +1100217 const int rw = bio_data_dir(bio);
NeilBrown409c57f2009-03-31 14:39:39 +1100218 mddev_t *mddev = q->queuedata;
219 int rv;
NeilBrown49077322010-03-25 16:20:56 +1100220 int cpu;
221
NeilBrown409c57f2009-03-31 14:39:39 +1100222 if (mddev == NULL || mddev->pers == NULL) {
223 bio_io_error(bio);
224 return 0;
225 }
226 rcu_read_lock();
NeilBrowna2826aa2009-12-14 12:49:49 +1100227 if (mddev->suspended || mddev->barrier) {
NeilBrown409c57f2009-03-31 14:39:39 +1100228 DEFINE_WAIT(__wait);
229 for (;;) {
230 prepare_to_wait(&mddev->sb_wait, &__wait,
231 TASK_UNINTERRUPTIBLE);
NeilBrowna2826aa2009-12-14 12:49:49 +1100232 if (!mddev->suspended && !mddev->barrier)
NeilBrown409c57f2009-03-31 14:39:39 +1100233 break;
234 rcu_read_unlock();
235 schedule();
236 rcu_read_lock();
237 }
238 finish_wait(&mddev->sb_wait, &__wait);
239 }
240 atomic_inc(&mddev->active_io);
241 rcu_read_unlock();
NeilBrown49077322010-03-25 16:20:56 +1100242
NeilBrown409c57f2009-03-31 14:39:39 +1100243 rv = mddev->pers->make_request(q, bio);
NeilBrown49077322010-03-25 16:20:56 +1100244
245 cpu = part_stat_lock();
246 part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
247 part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
248 bio_sectors(bio));
249 part_stat_unlock();
250
NeilBrown409c57f2009-03-31 14:39:39 +1100251 if (atomic_dec_and_test(&mddev->active_io) && mddev->suspended)
252 wake_up(&mddev->sb_wait);
253
254 return rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
NeilBrown409c57f2009-03-31 14:39:39 +1100257static void mddev_suspend(mddev_t *mddev)
258{
259 BUG_ON(mddev->suspended);
260 mddev->suspended = 1;
261 synchronize_rcu();
262 wait_event(mddev->sb_wait, atomic_read(&mddev->active_io) == 0);
263 mddev->pers->quiesce(mddev, 1);
264 md_unregister_thread(mddev->thread);
265 mddev->thread = NULL;
266 /* we now know that no code is executing in the personality module,
267 * except possibly the tail end of a ->bi_end_io function, but that
268 * is certain to complete before the module has a chance to get
269 * unloaded
270 */
271}
272
273static void mddev_resume(mddev_t *mddev)
274{
275 mddev->suspended = 0;
276 wake_up(&mddev->sb_wait);
277 mddev->pers->quiesce(mddev, 0);
278}
279
NeilBrown3fa841d2009-09-23 18:10:29 +1000280int mddev_congested(mddev_t *mddev, int bits)
281{
NeilBrowna2826aa2009-12-14 12:49:49 +1100282 if (mddev->barrier)
283 return 1;
NeilBrown3fa841d2009-09-23 18:10:29 +1000284 return mddev->suspended;
285}
286EXPORT_SYMBOL(mddev_congested);
287
NeilBrowna2826aa2009-12-14 12:49:49 +1100288/*
289 * Generic barrier handling for md
290 */
291
292#define POST_REQUEST_BARRIER ((void*)1)
293
294static void md_end_barrier(struct bio *bio, int err)
295{
296 mdk_rdev_t *rdev = bio->bi_private;
297 mddev_t *mddev = rdev->mddev;
298 if (err == -EOPNOTSUPP && mddev->barrier != POST_REQUEST_BARRIER)
299 set_bit(BIO_EOPNOTSUPP, &mddev->barrier->bi_flags);
300
301 rdev_dec_pending(rdev, mddev);
302
303 if (atomic_dec_and_test(&mddev->flush_pending)) {
304 if (mddev->barrier == POST_REQUEST_BARRIER) {
305 /* This was a post-request barrier */
306 mddev->barrier = NULL;
307 wake_up(&mddev->sb_wait);
308 } else
309 /* The pre-request barrier has finished */
310 schedule_work(&mddev->barrier_work);
311 }
312 bio_put(bio);
313}
314
315static void submit_barriers(mddev_t *mddev)
316{
317 mdk_rdev_t *rdev;
318
319 rcu_read_lock();
320 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
321 if (rdev->raid_disk >= 0 &&
322 !test_bit(Faulty, &rdev->flags)) {
323 /* Take two references, one is dropped
324 * when request finishes, one after
325 * we reclaim rcu_read_lock
326 */
327 struct bio *bi;
328 atomic_inc(&rdev->nr_pending);
329 atomic_inc(&rdev->nr_pending);
330 rcu_read_unlock();
331 bi = bio_alloc(GFP_KERNEL, 0);
332 bi->bi_end_io = md_end_barrier;
333 bi->bi_private = rdev;
334 bi->bi_bdev = rdev->bdev;
335 atomic_inc(&mddev->flush_pending);
336 submit_bio(WRITE_BARRIER, bi);
337 rcu_read_lock();
338 rdev_dec_pending(rdev, mddev);
339 }
340 rcu_read_unlock();
341}
342
343static void md_submit_barrier(struct work_struct *ws)
344{
345 mddev_t *mddev = container_of(ws, mddev_t, barrier_work);
346 struct bio *bio = mddev->barrier;
347
348 atomic_set(&mddev->flush_pending, 1);
349
350 if (test_bit(BIO_EOPNOTSUPP, &bio->bi_flags))
351 bio_endio(bio, -EOPNOTSUPP);
352 else if (bio->bi_size == 0)
353 /* an empty barrier - all done */
354 bio_endio(bio, 0);
355 else {
356 bio->bi_rw &= ~(1<<BIO_RW_BARRIER);
357 if (mddev->pers->make_request(mddev->queue, bio))
358 generic_make_request(bio);
359 mddev->barrier = POST_REQUEST_BARRIER;
360 submit_barriers(mddev);
361 }
362 if (atomic_dec_and_test(&mddev->flush_pending)) {
363 mddev->barrier = NULL;
364 wake_up(&mddev->sb_wait);
365 }
366}
367
368void md_barrier_request(mddev_t *mddev, struct bio *bio)
369{
370 spin_lock_irq(&mddev->write_lock);
371 wait_event_lock_irq(mddev->sb_wait,
372 !mddev->barrier,
373 mddev->write_lock, /*nothing*/);
374 mddev->barrier = bio;
375 spin_unlock_irq(&mddev->write_lock);
376
377 atomic_set(&mddev->flush_pending, 1);
378 INIT_WORK(&mddev->barrier_work, md_submit_barrier);
379
380 submit_barriers(mddev);
381
382 if (atomic_dec_and_test(&mddev->flush_pending))
383 schedule_work(&mddev->barrier_work);
384}
385EXPORT_SYMBOL(md_barrier_request);
NeilBrown409c57f2009-03-31 14:39:39 +1100386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387static inline mddev_t *mddev_get(mddev_t *mddev)
388{
389 atomic_inc(&mddev->active);
390 return mddev;
391}
392
Dan Williams5fd3a172009-03-04 00:57:25 -0700393static void mddev_delayed_delete(struct work_struct *ws);
NeilBrownd3374822009-01-09 08:31:10 +1100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395static void mddev_put(mddev_t *mddev)
396{
397 if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
398 return;
NeilBrownd3374822009-01-09 08:31:10 +1100399 if (!mddev->raid_disks && list_empty(&mddev->disks) &&
NeilBrowncbd19982009-12-30 12:08:49 +1100400 mddev->ctime == 0 && !mddev->hold_active) {
401 /* Array is not configured at all, and not held active,
402 * so destroy it */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 list_del(&mddev->all_mddevs);
NeilBrownd3374822009-01-09 08:31:10 +1100404 if (mddev->gendisk) {
405 /* we did a probe so need to clean up.
406 * Call schedule_work inside the spinlock
407 * so that flush_scheduled_work() after
408 * mddev_find will succeed in waiting for the
409 * work to be done.
410 */
411 INIT_WORK(&mddev->del_work, mddev_delayed_delete);
412 schedule_work(&mddev->del_work);
413 } else
414 kfree(mddev);
415 }
416 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static mddev_t * mddev_find(dev_t unit)
420{
421 mddev_t *mddev, *new = NULL;
422
423 retry:
424 spin_lock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
NeilBrownefeb53c2009-01-09 08:31:10 +1100426 if (unit) {
427 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
428 if (mddev->unit == unit) {
429 mddev_get(mddev);
430 spin_unlock(&all_mddevs_lock);
431 kfree(new);
432 return mddev;
433 }
434
435 if (new) {
436 list_add(&new->all_mddevs, &all_mddevs);
437 spin_unlock(&all_mddevs_lock);
438 new->hold_active = UNTIL_IOCTL;
439 return new;
440 }
441 } else if (new) {
442 /* find an unused unit number */
443 static int next_minor = 512;
444 int start = next_minor;
445 int is_free = 0;
446 int dev = 0;
447 while (!is_free) {
448 dev = MKDEV(MD_MAJOR, next_minor);
449 next_minor++;
450 if (next_minor > MINORMASK)
451 next_minor = 0;
452 if (next_minor == start) {
453 /* Oh dear, all in use. */
454 spin_unlock(&all_mddevs_lock);
455 kfree(new);
456 return NULL;
457 }
458
459 is_free = 1;
460 list_for_each_entry(mddev, &all_mddevs, all_mddevs)
461 if (mddev->unit == dev) {
462 is_free = 0;
463 break;
464 }
465 }
466 new->unit = dev;
467 new->md_minor = MINOR(dev);
468 new->hold_active = UNTIL_STOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 list_add(&new->all_mddevs, &all_mddevs);
470 spin_unlock(&all_mddevs_lock);
471 return new;
472 }
473 spin_unlock(&all_mddevs_lock);
474
NeilBrown9ffae0c2006-01-06 00:20:32 -0800475 new = kzalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (!new)
477 return NULL;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 new->unit = unit;
480 if (MAJOR(unit) == MD_MAJOR)
481 new->md_minor = MINOR(unit);
482 else
483 new->md_minor = MINOR(unit) >> MdpMinorShift;
484
NeilBrownc8c00a62009-08-10 12:50:52 +1000485 mutex_init(&new->open_mutex);
NeilBrowndf5b89b2006-03-27 01:18:20 -0800486 mutex_init(&new->reconfig_mutex);
NeilBrownc3d97142009-12-14 12:49:52 +1100487 mutex_init(&new->bitmap_info.mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 INIT_LIST_HEAD(&new->disks);
489 INIT_LIST_HEAD(&new->all_mddevs);
490 init_timer(&new->safemode_timer);
491 atomic_set(&new->active, 1);
NeilBrownf2ea68c2008-07-21 17:05:25 +1000492 atomic_set(&new->openers, 0);
NeilBrown409c57f2009-03-31 14:39:39 +1100493 atomic_set(&new->active_io, 0);
NeilBrown06d91a52005-06-21 17:17:12 -0700494 spin_lock_init(&new->write_lock);
NeilBrowna2826aa2009-12-14 12:49:49 +1100495 atomic_set(&new->flush_pending, 0);
NeilBrown3d310eb2005-06-21 17:17:26 -0700496 init_waitqueue_head(&new->sb_wait);
Dan Williamsa6d81132008-06-05 22:45:53 -0700497 init_waitqueue_head(&new->recovery_wait);
NeilBrown08a02ec2007-05-09 02:35:38 -0700498 new->reshape_position = MaxSector;
Neil Brown5e96ee62008-06-28 08:31:24 +1000499 new->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -0800500 new->resync_max = MaxSector;
NeilBrownd897dbf2008-04-30 00:52:29 -0700501 new->level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 goto retry;
504}
505
506static inline int mddev_lock(mddev_t * mddev)
507{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800508 return mutex_lock_interruptible(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
Dan Williamsb522adc2009-03-31 15:00:31 +1100511static inline int mddev_is_locked(mddev_t *mddev)
512{
513 return mutex_is_locked(&mddev->reconfig_mutex);
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static inline int mddev_trylock(mddev_t * mddev)
517{
NeilBrowndf5b89b2006-03-27 01:18:20 -0800518 return mutex_trylock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
NeilBrownb6eb1272010-04-15 10:13:47 +1000521static struct attribute_group md_redundancy_group;
522
NeilBrowna64c8762010-04-14 17:15:37 +1000523static void mddev_unlock(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
NeilBrowna64c8762010-04-14 17:15:37 +1000525 if (mddev->to_remove) {
NeilBrownb6eb1272010-04-15 10:13:47 +1000526 /* These cannot be removed under reconfig_mutex as
527 * an access to the files will try to take reconfig_mutex
528 * while holding the file unremovable, which leads to
529 * a deadlock.
530 * So hold open_mutex instead - we are allowed to take
531 * it while holding reconfig_mutex, and md_run can
532 * use it to wait for the remove to complete.
533 */
NeilBrowna64c8762010-04-14 17:15:37 +1000534 struct attribute_group *to_remove = mddev->to_remove;
535 mddev->to_remove = NULL;
NeilBrownb6eb1272010-04-15 10:13:47 +1000536 mutex_lock(&mddev->open_mutex);
537 mutex_unlock(&mddev->reconfig_mutex);
538
NeilBrowna64c8762010-04-14 17:15:37 +1000539 if (to_remove != &md_redundancy_group)
540 sysfs_remove_group(&mddev->kobj, to_remove);
541 if (mddev->pers == NULL ||
542 mddev->pers->sync_request == NULL) {
543 sysfs_remove_group(&mddev->kobj, &md_redundancy_group);
544 if (mddev->sysfs_action)
545 sysfs_put(mddev->sysfs_action);
546 mddev->sysfs_action = NULL;
547 }
NeilBrownb6eb1272010-04-15 10:13:47 +1000548 mutex_unlock(&mddev->open_mutex);
549 } else
550 mutex_unlock(&mddev->reconfig_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
NeilBrown005eca52005-08-22 13:11:08 -0700552 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
NeilBrown2989ddb2006-01-06 00:20:43 -0800555static mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100557 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
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->desc_nr == nr)
561 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return NULL;
564}
565
566static mdk_rdev_t * find_rdev(mddev_t * mddev, dev_t dev)
567{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 mdk_rdev_t *rdev;
569
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100570 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (rdev->bdev->bd_dev == dev)
572 return rdev;
Cheng Renquan159ec1f2009-01-09 08:31:08 +1100573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return NULL;
575}
576
NeilBrownd9d166c2006-01-06 00:20:51 -0800577static struct mdk_personality *find_pers(int level, char *clevel)
NeilBrown2604b702006-01-06 00:20:36 -0800578{
579 struct mdk_personality *pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800580 list_for_each_entry(pers, &pers_list, list) {
581 if (level != LEVEL_NONE && pers->level == level)
NeilBrown2604b702006-01-06 00:20:36 -0800582 return pers;
NeilBrownd9d166c2006-01-06 00:20:51 -0800583 if (strcmp(pers->name, clevel)==0)
584 return pers;
585 }
NeilBrown2604b702006-01-06 00:20:36 -0800586 return NULL;
587}
588
Andre Nollb73df2d2008-07-11 22:02:23 +1000589/* return the offset of the super block in 512byte sectors */
Jesper Juhl77933d72005-07-27 11:46:09 -0700590static inline sector_t calc_dev_sboffset(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Andre Nollb73df2d2008-07-11 22:02:23 +1000592 sector_t num_sectors = bdev->bd_inode->i_size / 512;
593 return MD_NEW_SIZE_SECTORS(num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596static int alloc_disk_sb(mdk_rdev_t * rdev)
597{
598 if (rdev->sb_page)
599 MD_BUG();
600
601 rdev->sb_page = alloc_page(GFP_KERNEL);
602 if (!rdev->sb_page) {
603 printk(KERN_ALERT "md: out of memory.\n");
Andre Nollebc24332008-07-11 22:02:20 +1000604 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 return 0;
608}
609
610static void free_disk_sb(mdk_rdev_t * rdev)
611{
612 if (rdev->sb_page) {
NeilBrown2d1f3b52006-01-06 00:20:31 -0800613 put_page(rdev->sb_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 rdev->sb_loaded = 0;
615 rdev->sb_page = NULL;
Andre Noll0f420352008-07-11 22:02:23 +1000616 rdev->sb_start = 0;
Andre Nolldd8ac332009-03-31 14:33:13 +1100617 rdev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619}
620
621
NeilBrown6712ecf2007-09-27 12:47:43 +0200622static void super_written(struct bio *bio, int error)
NeilBrown7bfa19f2005-06-21 17:17:28 -0700623{
624 mdk_rdev_t *rdev = bio->bi_private;
NeilBrowna9701a32005-11-08 21:39:34 -0800625 mddev_t *mddev = rdev->mddev;
NeilBrown7bfa19f2005-06-21 17:17:28 -0700626
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700627 if (error || !test_bit(BIO_UPTODATE, &bio->bi_flags)) {
628 printk("md: super_written gets error=%d, uptodate=%d\n",
629 error, test_bit(BIO_UPTODATE, &bio->bi_flags));
630 WARN_ON(test_bit(BIO_UPTODATE, &bio->bi_flags));
NeilBrowna9701a32005-11-08 21:39:34 -0800631 md_error(mddev, rdev);
NeilBrown3a0f5bb2006-10-03 01:16:03 -0700632 }
NeilBrown7bfa19f2005-06-21 17:17:28 -0700633
NeilBrowna9701a32005-11-08 21:39:34 -0800634 if (atomic_dec_and_test(&mddev->pending_writes))
635 wake_up(&mddev->sb_wait);
Neil Brownf8b58ed2005-06-27 22:29:34 -0700636 bio_put(bio);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700637}
638
NeilBrown6712ecf2007-09-27 12:47:43 +0200639static void super_written_barrier(struct bio *bio, int error)
NeilBrowna9701a32005-11-08 21:39:34 -0800640{
641 struct bio *bio2 = bio->bi_private;
642 mdk_rdev_t *rdev = bio2->bi_private;
643 mddev_t *mddev = rdev->mddev;
NeilBrowna9701a32005-11-08 21:39:34 -0800644
645 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
646 error == -EOPNOTSUPP) {
647 unsigned long flags;
648 /* barriers don't appear to be supported :-( */
649 set_bit(BarriersNotsupp, &rdev->flags);
650 mddev->barriers_work = 0;
651 spin_lock_irqsave(&mddev->write_lock, flags);
652 bio2->bi_next = mddev->biolist;
653 mddev->biolist = bio2;
654 spin_unlock_irqrestore(&mddev->write_lock, flags);
655 wake_up(&mddev->sb_wait);
656 bio_put(bio);
NeilBrown6712ecf2007-09-27 12:47:43 +0200657 } else {
658 bio_put(bio2);
659 bio->bi_private = rdev;
660 super_written(bio, error);
NeilBrowna9701a32005-11-08 21:39:34 -0800661 }
NeilBrowna9701a32005-11-08 21:39:34 -0800662}
663
NeilBrown7bfa19f2005-06-21 17:17:28 -0700664void md_super_write(mddev_t *mddev, mdk_rdev_t *rdev,
665 sector_t sector, int size, struct page *page)
666{
667 /* write first size bytes of page to sector of rdev
668 * Increment mddev->pending_writes before returning
669 * and decrement it on completion, waking up sb_wait
670 * if zero is reached.
671 * If an error occurred, call md_error
NeilBrowna9701a32005-11-08 21:39:34 -0800672 *
673 * As we might need to resubmit the request if BIO_RW_BARRIER
674 * causes ENOTSUPP, we allocate a spare bio...
NeilBrown7bfa19f2005-06-21 17:17:28 -0700675 */
676 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Jens Axboe93dbb392009-02-16 10:25:40 +0100677 int rw = (1<<BIO_RW) | (1<<BIO_RW_SYNCIO) | (1<<BIO_RW_UNPLUG);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700678
679 bio->bi_bdev = rdev->bdev;
680 bio->bi_sector = sector;
681 bio_add_page(bio, page, size, 0);
682 bio->bi_private = rdev;
683 bio->bi_end_io = super_written;
NeilBrowna9701a32005-11-08 21:39:34 -0800684 bio->bi_rw = rw;
685
NeilBrown7bfa19f2005-06-21 17:17:28 -0700686 atomic_inc(&mddev->pending_writes);
NeilBrowna9701a32005-11-08 21:39:34 -0800687 if (!test_bit(BarriersNotsupp, &rdev->flags)) {
688 struct bio *rbio;
689 rw |= (1<<BIO_RW_BARRIER);
690 rbio = bio_clone(bio, GFP_NOIO);
691 rbio->bi_private = bio;
692 rbio->bi_end_io = super_written_barrier;
693 submit_bio(rw, rbio);
694 } else
695 submit_bio(rw, bio);
696}
697
698void md_super_wait(mddev_t *mddev)
699{
700 /* wait for all superblock writes that were scheduled to complete.
701 * if any had to be retried (due to BARRIER problems), retry them
702 */
703 DEFINE_WAIT(wq);
704 for(;;) {
705 prepare_to_wait(&mddev->sb_wait, &wq, TASK_UNINTERRUPTIBLE);
706 if (atomic_read(&mddev->pending_writes)==0)
707 break;
708 while (mddev->biolist) {
709 struct bio *bio;
710 spin_lock_irq(&mddev->write_lock);
711 bio = mddev->biolist;
712 mddev->biolist = bio->bi_next ;
713 bio->bi_next = NULL;
714 spin_unlock_irq(&mddev->write_lock);
715 submit_bio(bio->bi_rw, bio);
716 }
717 schedule();
718 }
719 finish_wait(&mddev->sb_wait, &wq);
NeilBrown7bfa19f2005-06-21 17:17:28 -0700720}
721
NeilBrown6712ecf2007-09-27 12:47:43 +0200722static void bi_complete(struct bio *bio, int error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 complete((struct completion*)bio->bi_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
NeilBrowna654b9d82005-06-21 17:17:27 -0700727int sync_page_io(struct block_device *bdev, sector_t sector, int size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct page *page, int rw)
729{
Neil Brownbaaa2c52005-04-16 15:23:54 -0700730 struct bio *bio = bio_alloc(GFP_NOIO, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct completion event;
732 int ret;
733
Jens Axboe93dbb392009-02-16 10:25:40 +0100734 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 bio->bi_bdev = bdev;
737 bio->bi_sector = sector;
738 bio_add_page(bio, page, size, 0);
739 init_completion(&event);
740 bio->bi_private = &event;
741 bio->bi_end_io = bi_complete;
742 submit_bio(rw, bio);
743 wait_for_completion(&event);
744
745 ret = test_bit(BIO_UPTODATE, &bio->bi_flags);
746 bio_put(bio);
747 return ret;
748}
NeilBrowna8745db2006-01-06 00:20:34 -0800749EXPORT_SYMBOL_GPL(sync_page_io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
NeilBrown0002b272005-09-09 16:23:53 -0700751static int read_disk_sb(mdk_rdev_t * rdev, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 char b[BDEVNAME_SIZE];
754 if (!rdev->sb_page) {
755 MD_BUG();
756 return -EINVAL;
757 }
758 if (rdev->sb_loaded)
759 return 0;
760
761
Andre Noll0f420352008-07-11 22:02:23 +1000762 if (!sync_page_io(rdev->bdev, rdev->sb_start, size, rdev->sb_page, READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto fail;
764 rdev->sb_loaded = 1;
765 return 0;
766
767fail:
768 printk(KERN_WARNING "md: disabled device %s, could not read superblock.\n",
769 bdevname(rdev->bdev,b));
770 return -EINVAL;
771}
772
773static int uuid_equal(mdp_super_t *sb1, mdp_super_t *sb2)
774{
Andre Noll05710462008-07-11 22:02:20 +1000775 return sb1->set_uuid0 == sb2->set_uuid0 &&
776 sb1->set_uuid1 == sb2->set_uuid1 &&
777 sb1->set_uuid2 == sb2->set_uuid2 &&
778 sb1->set_uuid3 == sb2->set_uuid3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781static int sb_equal(mdp_super_t *sb1, mdp_super_t *sb2)
782{
783 int ret;
784 mdp_super_t *tmp1, *tmp2;
785
786 tmp1 = kmalloc(sizeof(*tmp1),GFP_KERNEL);
787 tmp2 = kmalloc(sizeof(*tmp2),GFP_KERNEL);
788
789 if (!tmp1 || !tmp2) {
790 ret = 0;
Andre Noll35020f12008-03-23 15:10:33 +0100791 printk(KERN_INFO "md.c sb_equal(): failed to allocate memory!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 goto abort;
793 }
794
795 *tmp1 = *sb1;
796 *tmp2 = *sb2;
797
798 /*
799 * nr_disks is not constant
800 */
801 tmp1->nr_disks = 0;
802 tmp2->nr_disks = 0;
803
Andre Nollce0c8e02008-07-11 22:02:20 +1000804 ret = (memcmp(tmp1, tmp2, MD_SB_GENERIC_CONSTANT_WORDS * 4) == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805abort:
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700806 kfree(tmp1);
807 kfree(tmp2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return ret;
809}
810
NeilBrown4d167f02007-05-09 02:35:37 -0700811
812static u32 md_csum_fold(u32 csum)
813{
814 csum = (csum & 0xffff) + (csum >> 16);
815 return (csum & 0xffff) + (csum >> 16);
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818static unsigned int calc_sb_csum(mdp_super_t * sb)
819{
NeilBrown4d167f02007-05-09 02:35:37 -0700820 u64 newcsum = 0;
821 u32 *sb32 = (u32*)sb;
822 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 unsigned int disk_csum, csum;
824
825 disk_csum = sb->sb_csum;
826 sb->sb_csum = 0;
NeilBrown4d167f02007-05-09 02:35:37 -0700827
828 for (i = 0; i < MD_SB_BYTES/4 ; i++)
829 newcsum += sb32[i];
830 csum = (newcsum & 0xffffffff) + (newcsum>>32);
831
832
833#ifdef CONFIG_ALPHA
834 /* This used to use csum_partial, which was wrong for several
835 * reasons including that different results are returned on
836 * different architectures. It isn't critical that we get exactly
837 * the same return value as before (we always csum_fold before
838 * testing, and that removes any differences). However as we
839 * know that csum_partial always returned a 16bit value on
840 * alphas, do a fold to maximise conformity to previous behaviour.
841 */
842 sb->sb_csum = md_csum_fold(disk_csum);
843#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 sb->sb_csum = disk_csum;
NeilBrown4d167f02007-05-09 02:35:37 -0700845#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return csum;
847}
848
849
850/*
851 * Handle superblock details.
852 * We want to be able to handle multiple superblock formats
853 * so we have a common interface to them all, and an array of
854 * different handlers.
855 * We rely on user-space to write the initial superblock, and support
856 * reading and updating of superblocks.
857 * Interface methods are:
858 * int load_super(mdk_rdev_t *dev, mdk_rdev_t *refdev, int minor_version)
859 * loads and validates a superblock on dev.
860 * if refdev != NULL, compare superblocks on both devices
861 * Return:
862 * 0 - dev has a superblock that is compatible with refdev
863 * 1 - dev has a superblock that is compatible and newer than refdev
864 * so dev should be used as the refdev in future
865 * -EINVAL superblock incompatible or invalid
866 * -othererror e.g. -EIO
867 *
868 * int validate_super(mddev_t *mddev, mdk_rdev_t *dev)
869 * Verify that dev is acceptable into mddev.
870 * The first time, mddev->raid_disks will be 0, and data from
871 * dev should be merged in. Subsequent calls check that dev
872 * is new enough. Return 0 or -EINVAL
873 *
874 * void sync_super(mddev_t *mddev, mdk_rdev_t *dev)
875 * Update the superblock for rdev with data in mddev
876 * This does not write to disc.
877 *
878 */
879
880struct super_type {
Chris Webb0cd17fe2008-06-28 08:31:46 +1000881 char *name;
882 struct module *owner;
883 int (*load_super)(mdk_rdev_t *rdev, mdk_rdev_t *refdev,
884 int minor_version);
885 int (*validate_super)(mddev_t *mddev, mdk_rdev_t *rdev);
886 void (*sync_super)(mddev_t *mddev, mdk_rdev_t *rdev);
887 unsigned long long (*rdev_size_change)(mdk_rdev_t *rdev,
Andre Noll15f4a5f2008-07-21 14:42:12 +1000888 sector_t num_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889};
890
891/*
Andre Noll0894cc32009-06-18 08:49:23 +1000892 * Check that the given mddev has no bitmap.
893 *
894 * This function is called from the run method of all personalities that do not
895 * support bitmaps. It prints an error message and returns non-zero if mddev
896 * has a bitmap. Otherwise, it returns 0.
897 *
898 */
899int md_check_no_bitmap(mddev_t *mddev)
900{
NeilBrownc3d97142009-12-14 12:49:52 +1100901 if (!mddev->bitmap_info.file && !mddev->bitmap_info.offset)
Andre Noll0894cc32009-06-18 08:49:23 +1000902 return 0;
903 printk(KERN_ERR "%s: bitmaps are not supported for %s\n",
904 mdname(mddev), mddev->pers->name);
905 return 1;
906}
907EXPORT_SYMBOL(md_check_no_bitmap);
908
909/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 * load_super for 0.90.0
911 */
912static int super_90_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
913{
914 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
915 mdp_super_t *sb;
916 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /*
Andre Noll0f420352008-07-11 22:02:23 +1000919 * Calculate the position of the superblock (512byte sectors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * it's at the end of the disk.
921 *
922 * It also happens to be a multiple of 4Kb.
923 */
Andre Noll0f420352008-07-11 22:02:23 +1000924 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
NeilBrown0002b272005-09-09 16:23:53 -0700926 ret = read_disk_sb(rdev, MD_SB_BYTES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (ret) return ret;
928
929 ret = -EINVAL;
930
931 bdevname(rdev->bdev, b);
932 sb = (mdp_super_t*)page_address(rdev->sb_page);
933
934 if (sb->md_magic != MD_SB_MAGIC) {
935 printk(KERN_ERR "md: invalid raid superblock magic on %s\n",
936 b);
937 goto abort;
938 }
939
940 if (sb->major_version != 0 ||
NeilBrownf6705572006-03-27 01:18:11 -0800941 sb->minor_version < 90 ||
942 sb->minor_version > 91) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 printk(KERN_WARNING "Bad version number %d.%d on %s\n",
944 sb->major_version, sb->minor_version,
945 b);
946 goto abort;
947 }
948
949 if (sb->raid_disks <= 0)
950 goto abort;
951
NeilBrown4d167f02007-05-09 02:35:37 -0700952 if (md_csum_fold(calc_sb_csum(sb)) != md_csum_fold(sb->sb_csum)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 printk(KERN_WARNING "md: invalid superblock checksum on %s\n",
954 b);
955 goto abort;
956 }
957
958 rdev->preferred_minor = sb->md_minor;
959 rdev->data_offset = 0;
NeilBrown0002b272005-09-09 16:23:53 -0700960 rdev->sb_size = MD_SB_BYTES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (sb->level == LEVEL_MULTIPATH)
963 rdev->desc_nr = -1;
964 else
965 rdev->desc_nr = sb->this_disk.number;
966
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700967 if (!refdev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -0700969 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 __u64 ev1, ev2;
971 mdp_super_t *refsb = (mdp_super_t*)page_address(refdev->sb_page);
972 if (!uuid_equal(refsb, sb)) {
973 printk(KERN_WARNING "md: %s has different UUID to %s\n",
974 b, bdevname(refdev->bdev,b2));
975 goto abort;
976 }
977 if (!sb_equal(refsb, sb)) {
978 printk(KERN_WARNING "md: %s has same UUID"
979 " but different superblock to %s\n",
980 b, bdevname(refdev->bdev, b2));
981 goto abort;
982 }
983 ev1 = md_event(sb);
984 ev2 = md_event(refsb);
985 if (ev1 > ev2)
986 ret = 1;
987 else
988 ret = 0;
989 }
NeilBrown8190e752009-06-18 08:48:58 +1000990 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Andre Nolldd8ac332009-03-31 14:33:13 +1100992 if (rdev->sectors < sb->size * 2 && sb->level > 1)
NeilBrown2bf071b2006-01-06 00:20:55 -0800993 /* "this cannot possibly happen" ... */
994 ret = -EINVAL;
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 abort:
997 return ret;
998}
999
1000/*
1001 * validate_super for 0.90.0
1002 */
1003static int super_90_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1004{
1005 mdp_disk_t *desc;
1006 mdp_super_t *sb = (mdp_super_t *)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001007 __u64 ev1 = md_event(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
NeilBrown41158c72005-06-21 17:17:25 -07001009 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001010 clear_bit(Faulty, &rdev->flags);
1011 clear_bit(In_sync, &rdev->flags);
1012 clear_bit(WriteMostly, &rdev->flags);
1013 clear_bit(BarriersNotsupp, &rdev->flags);
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (mddev->raid_disks == 0) {
1016 mddev->major_version = 0;
1017 mddev->minor_version = sb->minor_version;
1018 mddev->patch_version = sb->patch_version;
NeilBrowne6910632008-02-06 01:39:51 -08001019 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001020 mddev->chunk_sectors = sb->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 mddev->ctime = sb->ctime;
1022 mddev->utime = sb->utime;
1023 mddev->level = sb->level;
NeilBrownd9d166c2006-01-06 00:20:51 -08001024 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 mddev->layout = sb->layout;
1026 mddev->raid_disks = sb->raid_disks;
Andre Noll58c0fed2009-03-31 14:33:13 +11001027 mddev->dev_sectors = sb->size * 2;
NeilBrown07d84d102006-06-26 00:27:56 -07001028 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001029 mddev->bitmap_info.offset = 0;
1030 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
NeilBrownf6705572006-03-27 01:18:11 -08001032 if (mddev->minor_version >= 91) {
1033 mddev->reshape_position = sb->reshape_position;
1034 mddev->delta_disks = sb->delta_disks;
1035 mddev->new_level = sb->new_level;
1036 mddev->new_layout = sb->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001037 mddev->new_chunk_sectors = sb->new_chunk >> 9;
NeilBrownf6705572006-03-27 01:18:11 -08001038 } else {
1039 mddev->reshape_position = MaxSector;
1040 mddev->delta_disks = 0;
1041 mddev->new_level = mddev->level;
1042 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001043 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (sb->state & (1<<MD_SB_CLEAN))
1047 mddev->recovery_cp = MaxSector;
1048 else {
1049 if (sb->events_hi == sb->cp_events_hi &&
1050 sb->events_lo == sb->cp_events_lo) {
1051 mddev->recovery_cp = sb->recovery_cp;
1052 } else
1053 mddev->recovery_cp = 0;
1054 }
1055
1056 memcpy(mddev->uuid+0, &sb->set_uuid0, 4);
1057 memcpy(mddev->uuid+4, &sb->set_uuid1, 4);
1058 memcpy(mddev->uuid+8, &sb->set_uuid2, 4);
1059 memcpy(mddev->uuid+12,&sb->set_uuid3, 4);
1060
1061 mddev->max_disks = MD_SB_DISKS;
NeilBrowna654b9d82005-06-21 17:17:27 -07001062
1063 if (sb->state & (1<<MD_SB_BITMAP_PRESENT) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001064 mddev->bitmap_info.file == NULL)
1065 mddev->bitmap_info.offset =
1066 mddev->bitmap_info.default_offset;
NeilBrowna654b9d82005-06-21 17:17:27 -07001067
NeilBrown41158c72005-06-21 17:17:25 -07001068 } else if (mddev->pers == NULL) {
1069 /* Insist on good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 ++ev1;
1071 if (ev1 < mddev->events)
1072 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001073 } else if (mddev->bitmap) {
1074 /* if adding to array with a bitmap, then we can accept an
1075 * older device ... but not too old.
1076 */
NeilBrown41158c72005-06-21 17:17:25 -07001077 if (ev1 < mddev->bitmap->events_cleared)
1078 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001079 } else {
1080 if (ev1 < mddev->events)
1081 /* just a hot-add of a new device, leave raid_disk at -1 */
1082 return 0;
1083 }
NeilBrown41158c72005-06-21 17:17:25 -07001084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (mddev->level != LEVEL_MULTIPATH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 desc = sb->disks + rdev->desc_nr;
1087
1088 if (desc->state & (1<<MD_DISK_FAULTY))
NeilBrownb2d444d2005-11-08 21:39:31 -08001089 set_bit(Faulty, &rdev->flags);
NeilBrown7c7546c2006-06-26 00:27:41 -07001090 else if (desc->state & (1<<MD_DISK_SYNC) /* &&
1091 desc->raid_disk < mddev->raid_disks */) {
NeilBrownb2d444d2005-11-08 21:39:31 -08001092 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 rdev->raid_disk = desc->raid_disk;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001094 } else if (desc->state & (1<<MD_DISK_ACTIVE)) {
1095 /* active but not in sync implies recovery up to
1096 * reshape position. We don't know exactly where
1097 * that is, so set to zero for now */
1098 if (mddev->minor_version >= 91) {
1099 rdev->recovery_offset = 0;
1100 rdev->raid_disk = desc->raid_disk;
1101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001103 if (desc->state & (1<<MD_DISK_WRITEMOSTLY))
1104 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001105 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001106 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return 0;
1108}
1109
1110/*
1111 * sync_super for 0.90.0
1112 */
1113static void super_90_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1114{
1115 mdp_super_t *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 mdk_rdev_t *rdev2;
1117 int next_spare = mddev->raid_disks;
NeilBrown19133a42005-11-08 21:39:35 -08001118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* make rdev->sb match mddev data..
1121 *
1122 * 1/ zero out disks
1123 * 2/ Add info for each disk, keeping track of highest desc_nr (next_spare);
1124 * 3/ any empty disks < next_spare become removed
1125 *
1126 * disks[0] gets initialised to REMOVED because
1127 * we cannot be sure from other fields if it has
1128 * been initialised or not.
1129 */
1130 int i;
1131 int active=0, working=0,failed=0,spare=0,nr_disks=0;
1132
NeilBrown61181562005-09-09 16:24:02 -07001133 rdev->sb_size = MD_SB_BYTES;
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 sb = (mdp_super_t*)page_address(rdev->sb_page);
1136
1137 memset(sb, 0, sizeof(*sb));
1138
1139 sb->md_magic = MD_SB_MAGIC;
1140 sb->major_version = mddev->major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 sb->patch_version = mddev->patch_version;
1142 sb->gvalid_words = 0; /* ignored */
1143 memcpy(&sb->set_uuid0, mddev->uuid+0, 4);
1144 memcpy(&sb->set_uuid1, mddev->uuid+4, 4);
1145 memcpy(&sb->set_uuid2, mddev->uuid+8, 4);
1146 memcpy(&sb->set_uuid3, mddev->uuid+12,4);
1147
1148 sb->ctime = mddev->ctime;
1149 sb->level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11001150 sb->size = mddev->dev_sectors / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 sb->raid_disks = mddev->raid_disks;
1152 sb->md_minor = mddev->md_minor;
NeilBrowne6910632008-02-06 01:39:51 -08001153 sb->not_persistent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 sb->utime = mddev->utime;
1155 sb->state = 0;
1156 sb->events_hi = (mddev->events>>32);
1157 sb->events_lo = (u32)mddev->events;
1158
NeilBrownf6705572006-03-27 01:18:11 -08001159 if (mddev->reshape_position == MaxSector)
1160 sb->minor_version = 90;
1161 else {
1162 sb->minor_version = 91;
1163 sb->reshape_position = mddev->reshape_position;
1164 sb->new_level = mddev->new_level;
1165 sb->delta_disks = mddev->delta_disks;
1166 sb->new_layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001167 sb->new_chunk = mddev->new_chunk_sectors << 9;
NeilBrownf6705572006-03-27 01:18:11 -08001168 }
1169 mddev->minor_version = sb->minor_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 if (mddev->in_sync)
1171 {
1172 sb->recovery_cp = mddev->recovery_cp;
1173 sb->cp_events_hi = (mddev->events>>32);
1174 sb->cp_events_lo = (u32)mddev->events;
1175 if (mddev->recovery_cp == MaxSector)
1176 sb->state = (1<< MD_SB_CLEAN);
1177 } else
1178 sb->recovery_cp = 0;
1179
1180 sb->layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10001181 sb->chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
NeilBrownc3d97142009-12-14 12:49:52 +11001183 if (mddev->bitmap && mddev->bitmap_info.file == NULL)
NeilBrowna654b9d82005-06-21 17:17:27 -07001184 sb->state |= (1<<MD_SB_BITMAP_PRESENT);
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 sb->disks[0].state = (1<<MD_DISK_REMOVED);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001187 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 mdp_disk_t *d;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001189 int desc_nr;
NeilBrown0261cd9f2009-11-13 17:40:48 +11001190 int is_active = test_bit(In_sync, &rdev2->flags);
1191
1192 if (rdev2->raid_disk >= 0 &&
1193 sb->minor_version >= 91)
1194 /* we have nowhere to store the recovery_offset,
1195 * but if it is not below the reshape_position,
1196 * we can piggy-back on that.
1197 */
1198 is_active = 1;
1199 if (rdev2->raid_disk < 0 ||
1200 test_bit(Faulty, &rdev2->flags))
1201 is_active = 0;
1202 if (is_active)
NeilBrown86e6ffd2005-11-08 21:39:24 -08001203 desc_nr = rdev2->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 else
NeilBrown86e6ffd2005-11-08 21:39:24 -08001205 desc_nr = next_spare++;
NeilBrown19133a42005-11-08 21:39:35 -08001206 rdev2->desc_nr = desc_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 d = &sb->disks[rdev2->desc_nr];
1208 nr_disks++;
1209 d->number = rdev2->desc_nr;
1210 d->major = MAJOR(rdev2->bdev->bd_dev);
1211 d->minor = MINOR(rdev2->bdev->bd_dev);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001212 if (is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 d->raid_disk = rdev2->raid_disk;
1214 else
1215 d->raid_disk = rdev2->desc_nr; /* compatibility */
NeilBrown1be78922006-03-27 01:18:03 -08001216 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 d->state = (1<<MD_DISK_FAULTY);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001218 else if (is_active) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 d->state = (1<<MD_DISK_ACTIVE);
NeilBrown0261cd9f2009-11-13 17:40:48 +11001220 if (test_bit(In_sync, &rdev2->flags))
1221 d->state |= (1<<MD_DISK_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 active++;
1223 working++;
1224 } else {
1225 d->state = 0;
1226 spare++;
1227 working++;
1228 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001229 if (test_bit(WriteMostly, &rdev2->flags))
1230 d->state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /* now set the "removed" and "faulty" bits on any missing devices */
1233 for (i=0 ; i < mddev->raid_disks ; i++) {
1234 mdp_disk_t *d = &sb->disks[i];
1235 if (d->state == 0 && d->number == 0) {
1236 d->number = i;
1237 d->raid_disk = i;
1238 d->state = (1<<MD_DISK_REMOVED);
1239 d->state |= (1<<MD_DISK_FAULTY);
1240 failed++;
1241 }
1242 }
1243 sb->nr_disks = nr_disks;
1244 sb->active_disks = active;
1245 sb->working_disks = working;
1246 sb->failed_disks = failed;
1247 sb->spare_disks = spare;
1248
1249 sb->this_disk = sb->disks[rdev->desc_nr];
1250 sb->sb_csum = calc_sb_csum(sb);
1251}
1252
1253/*
Chris Webb0cd17fe2008-06-28 08:31:46 +10001254 * rdev_size_change for 0.90.0
1255 */
1256static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001257super_90_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001258{
Andre Noll58c0fed2009-03-31 14:33:13 +11001259 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001260 return 0; /* component must fit device */
NeilBrownc3d97142009-12-14 12:49:52 +11001261 if (rdev->mddev->bitmap_info.offset)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001262 return 0; /* can't move bitmap */
Andre Noll0f420352008-07-11 22:02:23 +10001263 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001264 if (!num_sectors || num_sectors > rdev->sb_start)
1265 num_sectors = rdev->sb_start;
Andre Noll0f420352008-07-11 22:02:23 +10001266 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001267 rdev->sb_page);
1268 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001269 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001270}
1271
1272
1273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * version 1 superblock
1275 */
1276
NeilBrown1c05b4b2006-10-21 10:24:08 -07001277static __le32 calc_sb_1_csum(struct mdp_superblock_1 * sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
NeilBrown1c05b4b2006-10-21 10:24:08 -07001279 __le32 disk_csum;
1280 u32 csum;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 unsigned long long newcsum;
1282 int size = 256 + le32_to_cpu(sb->max_dev)*2;
NeilBrown1c05b4b2006-10-21 10:24:08 -07001283 __le32 *isuper = (__le32*)sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int i;
1285
1286 disk_csum = sb->sb_csum;
1287 sb->sb_csum = 0;
1288 newcsum = 0;
1289 for (i=0; size>=4; size -= 4 )
1290 newcsum += le32_to_cpu(*isuper++);
1291
1292 if (size == 2)
NeilBrown1c05b4b2006-10-21 10:24:08 -07001293 newcsum += le16_to_cpu(*(__le16*) isuper);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 csum = (newcsum & 0xffffffff) + (newcsum >> 32);
1296 sb->sb_csum = disk_csum;
1297 return cpu_to_le32(csum);
1298}
1299
1300static int super_1_load(mdk_rdev_t *rdev, mdk_rdev_t *refdev, int minor_version)
1301{
1302 struct mdp_superblock_1 *sb;
1303 int ret;
Andre Noll0f420352008-07-11 22:02:23 +10001304 sector_t sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
NeilBrown0002b272005-09-09 16:23:53 -07001306 int bmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /*
Andre Noll0f420352008-07-11 22:02:23 +10001309 * Calculate the position of the superblock in 512byte sectors.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 * It is always aligned to a 4K boundary and
1311 * depeding on minor_version, it can be:
1312 * 0: At least 8K, but less than 12K, from end of device
1313 * 1: At start of device
1314 * 2: 4K from start of device.
1315 */
1316 switch(minor_version) {
1317 case 0:
Andre Noll0f420352008-07-11 22:02:23 +10001318 sb_start = rdev->bdev->bd_inode->i_size >> 9;
1319 sb_start -= 8*2;
1320 sb_start &= ~(sector_t)(4*2-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 case 1:
Andre Noll0f420352008-07-11 22:02:23 +10001323 sb_start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325 case 2:
Andre Noll0f420352008-07-11 22:02:23 +10001326 sb_start = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 break;
1328 default:
1329 return -EINVAL;
1330 }
Andre Noll0f420352008-07-11 22:02:23 +10001331 rdev->sb_start = sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
NeilBrown0002b272005-09-09 16:23:53 -07001333 /* superblock is rarely larger than 1K, but it can be larger,
1334 * and it is safe to read 4k, so we do that
1335 */
1336 ret = read_disk_sb(rdev, 4096);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 if (ret) return ret;
1338
1339
1340 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1341
1342 if (sb->magic != cpu_to_le32(MD_SB_MAGIC) ||
1343 sb->major_version != cpu_to_le32(1) ||
1344 le32_to_cpu(sb->max_dev) > (4096-256)/2 ||
Andre Noll0f420352008-07-11 22:02:23 +10001345 le64_to_cpu(sb->super_offset) != rdev->sb_start ||
NeilBrown71c08052005-09-09 16:23:51 -07001346 (le32_to_cpu(sb->feature_map) & ~MD_FEATURE_ALL) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return -EINVAL;
1348
1349 if (calc_sb_1_csum(sb) != sb->sb_csum) {
1350 printk("md: invalid superblock checksum on %s\n",
1351 bdevname(rdev->bdev,b));
1352 return -EINVAL;
1353 }
1354 if (le64_to_cpu(sb->data_size) < 10) {
1355 printk("md: data_size too small on %s\n",
1356 bdevname(rdev->bdev,b));
1357 return -EINVAL;
1358 }
NeilBrowne11e93f2007-05-09 02:35:36 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 rdev->preferred_minor = 0xffff;
1361 rdev->data_offset = le64_to_cpu(sb->data_offset);
NeilBrown4dbcdc72006-01-06 00:20:52 -08001362 atomic_set(&rdev->corrected_errors, le32_to_cpu(sb->cnt_corrected_read));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
NeilBrown0002b272005-09-09 16:23:53 -07001364 rdev->sb_size = le32_to_cpu(sb->max_dev) * 2 + 256;
Martin K. Petersene1defc42009-05-22 17:17:49 -04001365 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
NeilBrown0002b272005-09-09 16:23:53 -07001366 if (rdev->sb_size & bmask)
NeilBrowna1801f82008-03-04 14:29:31 -08001367 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1368
1369 if (minor_version
Andre Noll0f420352008-07-11 22:02:23 +10001370 && rdev->data_offset < sb_start + (rdev->sb_size/512))
NeilBrowna1801f82008-03-04 14:29:31 -08001371 return -EINVAL;
NeilBrown0002b272005-09-09 16:23:53 -07001372
NeilBrown31b65a02006-07-10 04:44:14 -07001373 if (sb->level == cpu_to_le32(LEVEL_MULTIPATH))
1374 rdev->desc_nr = -1;
1375 else
1376 rdev->desc_nr = le32_to_cpu(sb->dev_number);
1377
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001378 if (!refdev) {
NeilBrown8ed75462006-02-03 03:03:41 -08001379 ret = 1;
Harvey Harrison9a7b2b02008-04-28 02:15:49 -07001380 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 __u64 ev1, ev2;
1382 struct mdp_superblock_1 *refsb =
1383 (struct mdp_superblock_1*)page_address(refdev->sb_page);
1384
1385 if (memcmp(sb->set_uuid, refsb->set_uuid, 16) != 0 ||
1386 sb->level != refsb->level ||
1387 sb->layout != refsb->layout ||
1388 sb->chunksize != refsb->chunksize) {
1389 printk(KERN_WARNING "md: %s has strangely different"
1390 " superblock to %s\n",
1391 bdevname(rdev->bdev,b),
1392 bdevname(refdev->bdev,b2));
1393 return -EINVAL;
1394 }
1395 ev1 = le64_to_cpu(sb->events);
1396 ev2 = le64_to_cpu(refsb->events);
1397
1398 if (ev1 > ev2)
NeilBrown8ed75462006-02-03 03:03:41 -08001399 ret = 1;
1400 else
1401 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
NeilBrowna1801f82008-03-04 14:29:31 -08001403 if (minor_version)
Andre Nolldd8ac332009-03-31 14:33:13 +11001404 rdev->sectors = (rdev->bdev->bd_inode->i_size >> 9) -
1405 le64_to_cpu(sb->data_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 else
Andre Nolldd8ac332009-03-31 14:33:13 +11001407 rdev->sectors = rdev->sb_start;
1408 if (rdev->sectors < le64_to_cpu(sb->data_size))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return -EINVAL;
Andre Nolldd8ac332009-03-31 14:33:13 +11001410 rdev->sectors = le64_to_cpu(sb->data_size);
Andre Nolldd8ac332009-03-31 14:33:13 +11001411 if (le64_to_cpu(sb->size) > rdev->sectors)
NeilBrown2bf071b2006-01-06 00:20:55 -08001412 return -EINVAL;
NeilBrown8ed75462006-02-03 03:03:41 -08001413 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416static int super_1_validate(mddev_t *mddev, mdk_rdev_t *rdev)
1417{
1418 struct mdp_superblock_1 *sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
NeilBrown07d84d102006-06-26 00:27:56 -07001419 __u64 ev1 = le64_to_cpu(sb->events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
NeilBrown41158c72005-06-21 17:17:25 -07001421 rdev->raid_disk = -1;
NeilBrownc5d79ad2008-02-06 01:39:54 -08001422 clear_bit(Faulty, &rdev->flags);
1423 clear_bit(In_sync, &rdev->flags);
1424 clear_bit(WriteMostly, &rdev->flags);
1425 clear_bit(BarriersNotsupp, &rdev->flags);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (mddev->raid_disks == 0) {
1428 mddev->major_version = 1;
1429 mddev->patch_version = 0;
NeilBrowne6910632008-02-06 01:39:51 -08001430 mddev->external = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10001431 mddev->chunk_sectors = le32_to_cpu(sb->chunksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 mddev->ctime = le64_to_cpu(sb->ctime) & ((1ULL << 32)-1);
1433 mddev->utime = le64_to_cpu(sb->utime) & ((1ULL << 32)-1);
1434 mddev->level = le32_to_cpu(sb->level);
NeilBrownd9d166c2006-01-06 00:20:51 -08001435 mddev->clevel[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 mddev->layout = le32_to_cpu(sb->layout);
1437 mddev->raid_disks = le32_to_cpu(sb->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001438 mddev->dev_sectors = le64_to_cpu(sb->size);
NeilBrown07d84d102006-06-26 00:27:56 -07001439 mddev->events = ev1;
NeilBrownc3d97142009-12-14 12:49:52 +11001440 mddev->bitmap_info.offset = 0;
1441 mddev->bitmap_info.default_offset = 1024 >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 mddev->recovery_cp = le64_to_cpu(sb->resync_offset);
1444 memcpy(mddev->uuid, sb->set_uuid, 16);
1445
1446 mddev->max_disks = (4096-256)/2;
NeilBrowna654b9d82005-06-21 17:17:27 -07001447
NeilBrown71c08052005-09-09 16:23:51 -07001448 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_BITMAP_OFFSET) &&
NeilBrownc3d97142009-12-14 12:49:52 +11001449 mddev->bitmap_info.file == NULL )
1450 mddev->bitmap_info.offset =
1451 (__s32)le32_to_cpu(sb->bitmap_offset);
NeilBrowne11e93f2007-05-09 02:35:36 -07001452
NeilBrownf6705572006-03-27 01:18:11 -08001453 if ((le32_to_cpu(sb->feature_map) & MD_FEATURE_RESHAPE_ACTIVE)) {
1454 mddev->reshape_position = le64_to_cpu(sb->reshape_position);
1455 mddev->delta_disks = le32_to_cpu(sb->delta_disks);
1456 mddev->new_level = le32_to_cpu(sb->new_level);
1457 mddev->new_layout = le32_to_cpu(sb->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10001458 mddev->new_chunk_sectors = le32_to_cpu(sb->new_chunk);
NeilBrownf6705572006-03-27 01:18:11 -08001459 } else {
1460 mddev->reshape_position = MaxSector;
1461 mddev->delta_disks = 0;
1462 mddev->new_level = mddev->level;
1463 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10001464 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08001465 }
1466
NeilBrown41158c72005-06-21 17:17:25 -07001467 } else if (mddev->pers == NULL) {
1468 /* Insist of good event counter while assembling */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 ++ev1;
1470 if (ev1 < mddev->events)
1471 return -EINVAL;
NeilBrown41158c72005-06-21 17:17:25 -07001472 } else if (mddev->bitmap) {
1473 /* If adding to array with a bitmap, then we can accept an
1474 * older device, but not too old.
1475 */
NeilBrown41158c72005-06-21 17:17:25 -07001476 if (ev1 < mddev->bitmap->events_cleared)
1477 return 0;
NeilBrown07d84d102006-06-26 00:27:56 -07001478 } else {
1479 if (ev1 < mddev->events)
1480 /* just a hot-add of a new device, leave raid_disk at -1 */
1481 return 0;
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 if (mddev->level != LEVEL_MULTIPATH) {
1484 int role;
NeilBrown3673f302009-08-03 10:59:56 +10001485 if (rdev->desc_nr < 0 ||
1486 rdev->desc_nr >= le32_to_cpu(sb->max_dev)) {
1487 role = 0xffff;
1488 rdev->desc_nr = -1;
1489 } else
1490 role = le16_to_cpu(sb->dev_roles[rdev->desc_nr]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 switch(role) {
1492 case 0xffff: /* spare */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 break;
1494 case 0xfffe: /* faulty */
NeilBrownb2d444d2005-11-08 21:39:31 -08001495 set_bit(Faulty, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 break;
1497 default:
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001498 if ((le32_to_cpu(sb->feature_map) &
1499 MD_FEATURE_RECOVERY_OFFSET))
1500 rdev->recovery_offset = le64_to_cpu(sb->recovery_offset);
1501 else
1502 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 rdev->raid_disk = role;
1504 break;
1505 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07001506 if (sb->devflags & WriteMostly1)
1507 set_bit(WriteMostly, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001508 } else /* MULTIPATH are always insync */
NeilBrownb2d444d2005-11-08 21:39:31 -08001509 set_bit(In_sync, &rdev->flags);
NeilBrown41158c72005-06-21 17:17:25 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return 0;
1512}
1513
1514static void super_1_sync(mddev_t *mddev, mdk_rdev_t *rdev)
1515{
1516 struct mdp_superblock_1 *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 mdk_rdev_t *rdev2;
1518 int max_dev, i;
1519 /* make rdev->sb match mddev and rdev data. */
1520
1521 sb = (struct mdp_superblock_1*)page_address(rdev->sb_page);
1522
1523 sb->feature_map = 0;
1524 sb->pad0 = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001525 sb->recovery_offset = cpu_to_le64(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 memset(sb->pad1, 0, sizeof(sb->pad1));
1527 memset(sb->pad2, 0, sizeof(sb->pad2));
1528 memset(sb->pad3, 0, sizeof(sb->pad3));
1529
1530 sb->utime = cpu_to_le64((__u64)mddev->utime);
1531 sb->events = cpu_to_le64(mddev->events);
1532 if (mddev->in_sync)
1533 sb->resync_offset = cpu_to_le64(mddev->recovery_cp);
1534 else
1535 sb->resync_offset = cpu_to_le64(0);
1536
NeilBrown1c05b4b2006-10-21 10:24:08 -07001537 sb->cnt_corrected_read = cpu_to_le32(atomic_read(&rdev->corrected_errors));
NeilBrown4dbcdc72006-01-06 00:20:52 -08001538
NeilBrownf0ca3402006-02-02 14:28:04 -08001539 sb->raid_disks = cpu_to_le32(mddev->raid_disks);
Andre Noll58c0fed2009-03-31 14:33:13 +11001540 sb->size = cpu_to_le64(mddev->dev_sectors);
Andre Noll9d8f0362009-06-18 08:45:01 +10001541 sb->chunksize = cpu_to_le32(mddev->chunk_sectors);
NeilBrown62e1e382009-05-26 09:40:59 +10001542 sb->level = cpu_to_le32(mddev->level);
1543 sb->layout = cpu_to_le32(mddev->layout);
NeilBrownf0ca3402006-02-02 14:28:04 -08001544
NeilBrownc3d97142009-12-14 12:49:52 +11001545 if (mddev->bitmap && mddev->bitmap_info.file == NULL) {
1546 sb->bitmap_offset = cpu_to_le32((__u32)mddev->bitmap_info.offset);
NeilBrown71c08052005-09-09 16:23:51 -07001547 sb->feature_map = cpu_to_le32(MD_FEATURE_BITMAP_OFFSET);
NeilBrowna654b9d82005-06-21 17:17:27 -07001548 }
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001549
1550 if (rdev->raid_disk >= 0 &&
NeilBrown97e4f422009-03-31 14:33:13 +11001551 !test_bit(In_sync, &rdev->flags)) {
NeilBrown93be75f2009-12-14 12:50:06 +11001552 sb->feature_map |=
1553 cpu_to_le32(MD_FEATURE_RECOVERY_OFFSET);
1554 sb->recovery_offset =
1555 cpu_to_le64(rdev->recovery_offset);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001556 }
1557
NeilBrownf6705572006-03-27 01:18:11 -08001558 if (mddev->reshape_position != MaxSector) {
1559 sb->feature_map |= cpu_to_le32(MD_FEATURE_RESHAPE_ACTIVE);
1560 sb->reshape_position = cpu_to_le64(mddev->reshape_position);
1561 sb->new_layout = cpu_to_le32(mddev->new_layout);
1562 sb->delta_disks = cpu_to_le32(mddev->delta_disks);
1563 sb->new_level = cpu_to_le32(mddev->new_level);
Andre Noll664e7c42009-06-18 08:45:27 +10001564 sb->new_chunk = cpu_to_le32(mddev->new_chunk_sectors);
NeilBrownf6705572006-03-27 01:18:11 -08001565 }
NeilBrowna654b9d82005-06-21 17:17:27 -07001566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 max_dev = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001568 list_for_each_entry(rdev2, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (rdev2->desc_nr+1 > max_dev)
1570 max_dev = rdev2->desc_nr+1;
NeilBrowna778b732007-05-23 13:58:10 -07001571
NeilBrown70471da2009-08-03 10:59:57 +10001572 if (max_dev > le32_to_cpu(sb->max_dev)) {
1573 int bmask;
NeilBrowna778b732007-05-23 13:58:10 -07001574 sb->max_dev = cpu_to_le32(max_dev);
NeilBrown70471da2009-08-03 10:59:57 +10001575 rdev->sb_size = max_dev * 2 + 256;
1576 bmask = queue_logical_block_size(rdev->bdev->bd_disk->queue)-1;
1577 if (rdev->sb_size & bmask)
1578 rdev->sb_size = (rdev->sb_size | bmask) + 1;
1579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 for (i=0; i<max_dev;i++)
1581 sb->dev_roles[i] = cpu_to_le16(0xfffe);
1582
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001583 list_for_each_entry(rdev2, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 i = rdev2->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08001585 if (test_bit(Faulty, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 sb->dev_roles[i] = cpu_to_le16(0xfffe);
NeilBrownb2d444d2005-11-08 21:39:31 -08001587 else if (test_bit(In_sync, &rdev2->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
NeilBrown93be75f2009-12-14 12:50:06 +11001589 else if (rdev2->raid_disk >= 0)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07001590 sb->dev_roles[i] = cpu_to_le16(rdev2->raid_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 else
1592 sb->dev_roles[i] = cpu_to_le16(0xffff);
1593 }
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 sb->sb_csum = calc_sb_1_csum(sb);
1596}
1597
Chris Webb0cd17fe2008-06-28 08:31:46 +10001598static unsigned long long
Andre Noll15f4a5f2008-07-21 14:42:12 +10001599super_1_rdev_size_change(mdk_rdev_t *rdev, sector_t num_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001600{
1601 struct mdp_superblock_1 *sb;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001602 sector_t max_sectors;
Andre Noll58c0fed2009-03-31 14:33:13 +11001603 if (num_sectors && num_sectors < rdev->mddev->dev_sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10001604 return 0; /* component must fit device */
Andre Noll0f420352008-07-11 22:02:23 +10001605 if (rdev->sb_start < rdev->data_offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001606 /* minor versions 1 and 2; superblock before data */
Andre Noll15f4a5f2008-07-21 14:42:12 +10001607 max_sectors = rdev->bdev->bd_inode->i_size >> 9;
1608 max_sectors -= rdev->data_offset;
1609 if (!num_sectors || num_sectors > max_sectors)
1610 num_sectors = max_sectors;
NeilBrownc3d97142009-12-14 12:49:52 +11001611 } else if (rdev->mddev->bitmap_info.offset) {
Chris Webb0cd17fe2008-06-28 08:31:46 +10001612 /* minor version 0 with bitmap we can't move */
1613 return 0;
1614 } else {
1615 /* minor version 0; superblock after data */
Andre Noll0f420352008-07-11 22:02:23 +10001616 sector_t sb_start;
1617 sb_start = (rdev->bdev->bd_inode->i_size >> 9) - 8*2;
1618 sb_start &= ~(sector_t)(4*2 - 1);
Andre Nolldd8ac332009-03-31 14:33:13 +11001619 max_sectors = rdev->sectors + sb_start - rdev->sb_start;
Andre Noll15f4a5f2008-07-21 14:42:12 +10001620 if (!num_sectors || num_sectors > max_sectors)
1621 num_sectors = max_sectors;
Andre Noll0f420352008-07-11 22:02:23 +10001622 rdev->sb_start = sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001623 }
1624 sb = (struct mdp_superblock_1 *) page_address(rdev->sb_page);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001625 sb->data_size = cpu_to_le64(num_sectors);
Andre Noll0f420352008-07-11 22:02:23 +10001626 sb->super_offset = rdev->sb_start;
Chris Webb0cd17fe2008-06-28 08:31:46 +10001627 sb->sb_csum = calc_sb_1_csum(sb);
Andre Noll0f420352008-07-11 22:02:23 +10001628 md_super_write(rdev->mddev, rdev, rdev->sb_start, rdev->sb_size,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001629 rdev->sb_page);
1630 md_super_wait(rdev->mddev);
Andre Noll15f4a5f2008-07-21 14:42:12 +10001631 return num_sectors / 2; /* kB for sysfs */
Chris Webb0cd17fe2008-06-28 08:31:46 +10001632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Adrian Bunk75c96f82005-05-05 16:16:09 -07001634static struct super_type super_types[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 [0] = {
1636 .name = "0.90.0",
1637 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001638 .load_super = super_90_load,
1639 .validate_super = super_90_validate,
1640 .sync_super = super_90_sync,
1641 .rdev_size_change = super_90_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 },
1643 [1] = {
1644 .name = "md-1",
1645 .owner = THIS_MODULE,
Chris Webb0cd17fe2008-06-28 08:31:46 +10001646 .load_super = super_1_load,
1647 .validate_super = super_1_validate,
1648 .sync_super = super_1_sync,
1649 .rdev_size_change = super_1_rdev_size_change,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 },
1651};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653static int match_mddev_units(mddev_t *mddev1, mddev_t *mddev2)
1654{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001655 mdk_rdev_t *rdev, *rdev2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
NeilBrown4b809912008-07-21 17:05:25 +10001657 rcu_read_lock();
1658 rdev_for_each_rcu(rdev, mddev1)
1659 rdev_for_each_rcu(rdev2, mddev2)
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001660 if (rdev->bdev->bd_contains ==
NeilBrown4b809912008-07-21 17:05:25 +10001661 rdev2->bdev->bd_contains) {
1662 rcu_read_unlock();
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001663 return 1;
NeilBrown4b809912008-07-21 17:05:25 +10001664 }
1665 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return 0;
1667}
1668
1669static LIST_HEAD(pending_raid_disks);
1670
Andre Nollac5e7112009-08-03 10:59:47 +10001671/*
1672 * Try to register data integrity profile for an mddev
1673 *
1674 * This is called when an array is started and after a disk has been kicked
1675 * from the array. It only succeeds if all working and active component devices
1676 * are integrity capable with matching profiles.
1677 */
1678int md_integrity_register(mddev_t *mddev)
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001679{
Andre Nollac5e7112009-08-03 10:59:47 +10001680 mdk_rdev_t *rdev, *reference = NULL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001681
Andre Nollac5e7112009-08-03 10:59:47 +10001682 if (list_empty(&mddev->disks))
1683 return 0; /* nothing to do */
1684 if (blk_get_integrity(mddev->gendisk))
1685 return 0; /* already registered */
1686 list_for_each_entry(rdev, &mddev->disks, same_set) {
1687 /* skip spares and non-functional disks */
1688 if (test_bit(Faulty, &rdev->flags))
1689 continue;
1690 if (rdev->raid_disk < 0)
1691 continue;
1692 /*
1693 * If at least one rdev is not integrity capable, we can not
1694 * enable data integrity for the md device.
1695 */
1696 if (!bdev_get_integrity(rdev->bdev))
1697 return -EINVAL;
1698 if (!reference) {
1699 /* Use the first rdev as the reference */
1700 reference = rdev;
1701 continue;
1702 }
1703 /* does this rdev's profile match the reference profile? */
1704 if (blk_integrity_compare(reference->bdev->bd_disk,
1705 rdev->bdev->bd_disk) < 0)
1706 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001707 }
Andre Nollac5e7112009-08-03 10:59:47 +10001708 /*
1709 * All component devices are integrity capable and have matching
1710 * profiles, register the common profile for the md device.
1711 */
1712 if (blk_integrity_register(mddev->gendisk,
1713 bdev_get_integrity(reference->bdev)) != 0) {
1714 printk(KERN_ERR "md: failed to register integrity for %s\n",
1715 mdname(mddev));
1716 return -EINVAL;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001717 }
Andre Nollac5e7112009-08-03 10:59:47 +10001718 printk(KERN_NOTICE "md: data integrity on %s enabled\n",
1719 mdname(mddev));
1720 return 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001721}
Andre Nollac5e7112009-08-03 10:59:47 +10001722EXPORT_SYMBOL(md_integrity_register);
1723
1724/* Disable data integrity if non-capable/non-matching disk is being added */
1725void md_integrity_add_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
1726{
1727 struct blk_integrity *bi_rdev = bdev_get_integrity(rdev->bdev);
1728 struct blk_integrity *bi_mddev = blk_get_integrity(mddev->gendisk);
1729
1730 if (!bi_mddev) /* nothing to do */
1731 return;
1732 if (rdev->raid_disk < 0) /* skip spares */
1733 return;
1734 if (bi_rdev && blk_integrity_compare(mddev->gendisk,
1735 rdev->bdev->bd_disk) >= 0)
1736 return;
1737 printk(KERN_NOTICE "disabling data integrity on %s\n", mdname(mddev));
1738 blk_integrity_unregister(mddev->gendisk);
1739}
1740EXPORT_SYMBOL(md_integrity_add_rdev);
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1743{
NeilBrown7dd5e7c32007-02-28 20:11:35 -08001744 char b[BDEVNAME_SIZE];
NeilBrownf637b9f2005-11-08 21:39:37 -08001745 struct kobject *ko;
Neil Brown1edf80d2006-01-12 01:05:23 -08001746 char *s;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001747 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 if (rdev->mddev) {
1750 MD_BUG();
1751 return -EINVAL;
1752 }
Dan Williams11e2ede2008-04-30 00:52:32 -07001753
1754 /* prevent duplicates */
1755 if (find_rdev(mddev, rdev->bdev->bd_dev))
1756 return -EEXIST;
1757
Andre Nolldd8ac332009-03-31 14:33:13 +11001758 /* make sure rdev->sectors exceeds mddev->dev_sectors */
1759 if (rdev->sectors && (mddev->dev_sectors == 0 ||
1760 rdev->sectors < mddev->dev_sectors)) {
NeilBrowna778b732007-05-23 13:58:10 -07001761 if (mddev->pers) {
1762 /* Cannot change size, so fail
1763 * If mddev->level <= 0, then we don't care
1764 * about aligning sizes (e.g. linear)
1765 */
1766 if (mddev->level > 0)
1767 return -ENOSPC;
1768 } else
Andre Nolldd8ac332009-03-31 14:33:13 +11001769 mddev->dev_sectors = rdev->sectors;
NeilBrown2bf071b2006-01-06 00:20:55 -08001770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /* Verify rdev->desc_nr is unique.
1773 * If it is -1, assign a free number, else
1774 * check number is not in use
1775 */
1776 if (rdev->desc_nr < 0) {
1777 int choice = 0;
1778 if (mddev->pers) choice = mddev->raid_disks;
1779 while (find_rdev_nr(mddev, choice))
1780 choice++;
1781 rdev->desc_nr = choice;
1782 } else {
1783 if (find_rdev_nr(mddev, rdev->desc_nr))
1784 return -EBUSY;
1785 }
NeilBrownde01dfa2009-02-06 18:02:46 +11001786 if (mddev->max_disks && rdev->desc_nr >= mddev->max_disks) {
1787 printk(KERN_WARNING "md: %s: array is limited to %d devices\n",
1788 mdname(mddev), mddev->max_disks);
1789 return -EBUSY;
1790 }
NeilBrown19133a42005-11-08 21:39:35 -08001791 bdevname(rdev->bdev,b);
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001792 while ( (s=strchr(b, '/')) != NULL)
Neil Brown1edf80d2006-01-12 01:05:23 -08001793 *s = '!';
Greg Kroah-Hartman649316b2007-12-17 23:05:35 -07001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 rdev->mddev = mddev;
NeilBrown19133a42005-11-08 21:39:35 -08001796 printk(KERN_INFO "md: bind<%s>\n", b);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001797
Greg Kroah-Hartmanb2d6db52007-12-17 23:05:35 -07001798 if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
NeilBrown5e55e2f2007-03-26 21:32:14 -08001799 goto fail;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001800
Tejun Heo0762b8b2008-08-25 19:56:12 +09001801 ko = &part_to_dev(rdev->bdev->bd_part)->kobj;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001802 if ((err = sysfs_create_link(&rdev->kobj, ko, "block"))) {
1803 kobject_del(&rdev->kobj);
1804 goto fail;
1805 }
NeilBrown3c0ee632008-10-21 13:25:28 +11001806 rdev->sysfs_state = sysfs_get_dirent(rdev->kobj.sd, "state");
1807
NeilBrown4b809912008-07-21 17:05:25 +10001808 list_add_rcu(&rdev->same_set, &mddev->disks);
NeilBrownc5d79ad2008-02-06 01:39:54 -08001809 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk);
NeilBrown4044ba52009-01-09 08:31:11 +11001810
1811 /* May as well allow recovery to be retried once */
1812 mddev->recovery_disabled = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11001813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return 0;
NeilBrown5e55e2f2007-03-26 21:32:14 -08001815
1816 fail:
1817 printk(KERN_WARNING "md: failed to register dev-%s for %s\n",
1818 b, mdname(mddev));
1819 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
NeilBrown177a99b2008-02-06 01:39:56 -08001822static void md_delayed_delete(struct work_struct *ws)
NeilBrown5792a282007-04-04 19:08:18 -07001823{
1824 mdk_rdev_t *rdev = container_of(ws, mdk_rdev_t, del_work);
1825 kobject_del(&rdev->kobj);
NeilBrown177a99b2008-02-06 01:39:56 -08001826 kobject_put(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001827}
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1830{
1831 char b[BDEVNAME_SIZE];
1832 if (!rdev->mddev) {
1833 MD_BUG();
1834 return;
1835 }
Jun'ichi Nomura5463c792006-03-27 01:17:58 -08001836 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
NeilBrown4b809912008-07-21 17:05:25 +10001837 list_del_rcu(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1839 rdev->mddev = NULL;
NeilBrown86e6ffd2005-11-08 21:39:24 -08001840 sysfs_remove_link(&rdev->kobj, "block");
NeilBrown3c0ee632008-10-21 13:25:28 +11001841 sysfs_put(rdev->sysfs_state);
1842 rdev->sysfs_state = NULL;
NeilBrown5792a282007-04-04 19:08:18 -07001843 /* We need to delay this, otherwise we can deadlock when
NeilBrown4b809912008-07-21 17:05:25 +10001844 * writing to 'remove' to "dev/state". We also need
1845 * to delay it due to rcu usage.
NeilBrown5792a282007-04-04 19:08:18 -07001846 */
NeilBrown4b809912008-07-21 17:05:25 +10001847 synchronize_rcu();
NeilBrown177a99b2008-02-06 01:39:56 -08001848 INIT_WORK(&rdev->del_work, md_delayed_delete);
1849 kobject_get(&rdev->kobj);
NeilBrown5792a282007-04-04 19:08:18 -07001850 schedule_work(&rdev->del_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
1853/*
1854 * prevent the device from being mounted, repartitioned or
1855 * otherwise reused by a RAID array (or any other kernel
1856 * subsystem), by bd_claiming the device.
1857 */
NeilBrownc5d79ad2008-02-06 01:39:54 -08001858static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
1860 int err = 0;
1861 struct block_device *bdev;
1862 char b[BDEVNAME_SIZE];
1863
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001864 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (IS_ERR(bdev)) {
1866 printk(KERN_ERR "md: could not open %s.\n",
1867 __bdevname(dev, b));
1868 return PTR_ERR(bdev);
1869 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001870 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (err) {
1872 printk(KERN_ERR "md: could not bd_claim %s.\n",
1873 bdevname(bdev, b));
Al Viro9a1c3542008-02-22 20:40:24 -05001874 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 return err;
1876 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08001877 if (!shared)
1878 set_bit(AllReserved, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 rdev->bdev = bdev;
1880 return err;
1881}
1882
1883static void unlock_rdev(mdk_rdev_t *rdev)
1884{
1885 struct block_device *bdev = rdev->bdev;
1886 rdev->bdev = NULL;
1887 if (!bdev)
1888 MD_BUG();
1889 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -05001890 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
1893void md_autodetect_dev(dev_t dev);
1894
1895static void export_rdev(mdk_rdev_t * rdev)
1896{
1897 char b[BDEVNAME_SIZE];
1898 printk(KERN_INFO "md: export_rdev(%s)\n",
1899 bdevname(rdev->bdev,b));
1900 if (rdev->mddev)
1901 MD_BUG();
1902 free_disk_sb(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903#ifndef MODULE
NeilBrownd0fae182008-03-04 14:29:31 -08001904 if (test_bit(AutoDetected, &rdev->flags))
1905 md_autodetect_dev(rdev->bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906#endif
1907 unlock_rdev(rdev);
NeilBrown86e6ffd2005-11-08 21:39:24 -08001908 kobject_put(&rdev->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911static void kick_rdev_from_array(mdk_rdev_t * rdev)
1912{
1913 unbind_rdev_from_array(rdev);
1914 export_rdev(rdev);
1915}
1916
1917static void export_array(mddev_t *mddev)
1918{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11001919 mdk_rdev_t *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
NeilBrownd089c6a2008-02-06 01:39:59 -08001921 rdev_for_each(rdev, tmp, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (!rdev->mddev) {
1923 MD_BUG();
1924 continue;
1925 }
1926 kick_rdev_from_array(rdev);
1927 }
1928 if (!list_empty(&mddev->disks))
1929 MD_BUG();
1930 mddev->raid_disks = 0;
1931 mddev->major_version = 0;
1932}
1933
1934static void print_desc(mdp_disk_t *desc)
1935{
1936 printk(" DISK<N:%d,(%d,%d),R:%d,S:%d>\n", desc->number,
1937 desc->major,desc->minor,desc->raid_disk,desc->state);
1938}
1939
Cheng Renquancd2ac932009-01-09 08:31:08 +11001940static void print_sb_90(mdp_super_t *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 int i;
1943
1944 printk(KERN_INFO
1945 "md: SB: (V:%d.%d.%d) ID:<%08x.%08x.%08x.%08x> CT:%08x\n",
1946 sb->major_version, sb->minor_version, sb->patch_version,
1947 sb->set_uuid0, sb->set_uuid1, sb->set_uuid2, sb->set_uuid3,
1948 sb->ctime);
1949 printk(KERN_INFO "md: L%d S%08d ND:%d RD:%d md%d LO:%d CS:%d\n",
1950 sb->level, sb->size, sb->nr_disks, sb->raid_disks,
1951 sb->md_minor, sb->layout, sb->chunk_size);
1952 printk(KERN_INFO "md: UT:%08x ST:%d AD:%d WD:%d"
1953 " FD:%d SD:%d CSUM:%08x E:%08lx\n",
1954 sb->utime, sb->state, sb->active_disks, sb->working_disks,
1955 sb->failed_disks, sb->spare_disks,
1956 sb->sb_csum, (unsigned long)sb->events_lo);
1957
1958 printk(KERN_INFO);
1959 for (i = 0; i < MD_SB_DISKS; i++) {
1960 mdp_disk_t *desc;
1961
1962 desc = sb->disks + i;
1963 if (desc->number || desc->major || desc->minor ||
1964 desc->raid_disk || (desc->state && (desc->state != 4))) {
1965 printk(" D %2d: ", i);
1966 print_desc(desc);
1967 }
1968 }
1969 printk(KERN_INFO "md: THIS: ");
1970 print_desc(&sb->this_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Cheng Renquancd2ac932009-01-09 08:31:08 +11001973static void print_sb_1(struct mdp_superblock_1 *sb)
1974{
1975 __u8 *uuid;
1976
1977 uuid = sb->set_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001978 printk(KERN_INFO
Joe Perches7b75c2f2009-12-14 18:01:12 -08001979 "md: SB: (V:%u) (F:0x%08x) Array-ID:<%pU>\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001980 "md: Name: \"%s\" CT:%llu\n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001981 le32_to_cpu(sb->major_version),
1982 le32_to_cpu(sb->feature_map),
Joe Perches7b75c2f2009-12-14 18:01:12 -08001983 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11001984 sb->set_name,
1985 (unsigned long long)le64_to_cpu(sb->ctime)
1986 & MD_SUPERBLOCK_1_TIME_SEC_MASK);
1987
1988 uuid = sb->device_uuid;
Joe Perchesad361c92009-07-06 13:05:40 -07001989 printk(KERN_INFO
1990 "md: L%u SZ%llu RD:%u LO:%u CS:%u DO:%llu DS:%llu SO:%llu"
Cheng Renquancd2ac932009-01-09 08:31:08 +11001991 " RO:%llu\n"
Joe Perches7b75c2f2009-12-14 18:01:12 -08001992 "md: Dev:%08x UUID: %pU\n"
Joe Perchesad361c92009-07-06 13:05:40 -07001993 "md: (F:0x%08x) UT:%llu Events:%llu ResyncOffset:%llu CSUM:0x%08x\n"
1994 "md: (MaxDev:%u) \n",
Cheng Renquancd2ac932009-01-09 08:31:08 +11001995 le32_to_cpu(sb->level),
1996 (unsigned long long)le64_to_cpu(sb->size),
1997 le32_to_cpu(sb->raid_disks),
1998 le32_to_cpu(sb->layout),
1999 le32_to_cpu(sb->chunksize),
2000 (unsigned long long)le64_to_cpu(sb->data_offset),
2001 (unsigned long long)le64_to_cpu(sb->data_size),
2002 (unsigned long long)le64_to_cpu(sb->super_offset),
2003 (unsigned long long)le64_to_cpu(sb->recovery_offset),
2004 le32_to_cpu(sb->dev_number),
Joe Perches7b75c2f2009-12-14 18:01:12 -08002005 uuid,
Cheng Renquancd2ac932009-01-09 08:31:08 +11002006 sb->devflags,
2007 (unsigned long long)le64_to_cpu(sb->utime) & MD_SUPERBLOCK_1_TIME_SEC_MASK,
2008 (unsigned long long)le64_to_cpu(sb->events),
2009 (unsigned long long)le64_to_cpu(sb->resync_offset),
2010 le32_to_cpu(sb->sb_csum),
2011 le32_to_cpu(sb->max_dev)
2012 );
2013}
2014
2015static void print_rdev(mdk_rdev_t *rdev, int major_version)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 char b[BDEVNAME_SIZE];
Andre Nolldd8ac332009-03-31 14:33:13 +11002018 printk(KERN_INFO "md: rdev %s, Sect:%08llu F:%d S:%d DN:%u\n",
2019 bdevname(rdev->bdev, b), (unsigned long long)rdev->sectors,
NeilBrownb2d444d2005-11-08 21:39:31 -08002020 test_bit(Faulty, &rdev->flags), test_bit(In_sync, &rdev->flags),
2021 rdev->desc_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if (rdev->sb_loaded) {
Cheng Renquancd2ac932009-01-09 08:31:08 +11002023 printk(KERN_INFO "md: rdev superblock (MJ:%d):\n", major_version);
2024 switch (major_version) {
2025 case 0:
2026 print_sb_90((mdp_super_t*)page_address(rdev->sb_page));
2027 break;
2028 case 1:
2029 print_sb_1((struct mdp_superblock_1 *)page_address(rdev->sb_page));
2030 break;
2031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 } else
2033 printk(KERN_INFO "md: no rdev superblock!\n");
2034}
2035
Adrian Bunk5e563412006-06-26 00:27:42 -07002036static void md_print_devices(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002038 struct list_head *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 mdk_rdev_t *rdev;
2040 mddev_t *mddev;
2041 char b[BDEVNAME_SIZE];
2042
2043 printk("\n");
2044 printk("md: **********************************\n");
2045 printk("md: * <COMPLETE RAID STATE PRINTOUT> *\n");
2046 printk("md: **********************************\n");
NeilBrown29ac4aa2008-02-06 01:39:58 -08002047 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
NeilBrown32a76272005-06-21 17:17:14 -07002049 if (mddev->bitmap)
2050 bitmap_print_sb(mddev->bitmap);
2051 else
2052 printk("%s: ", mdname(mddev));
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002053 list_for_each_entry(rdev, &mddev->disks, same_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 printk("<%s>", bdevname(rdev->bdev,b));
2055 printk("\n");
2056
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002057 list_for_each_entry(rdev, &mddev->disks, same_set)
Cheng Renquancd2ac932009-01-09 08:31:08 +11002058 print_rdev(rdev, mddev->major_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
2060 printk("md: **********************************\n");
2061 printk("\n");
2062}
2063
2064
NeilBrown42543762006-06-26 00:27:57 -07002065static void sync_sbs(mddev_t * mddev, int nospares)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
NeilBrown42543762006-06-26 00:27:57 -07002067 /* Update each superblock (in-memory image), but
2068 * if we are allowed to, skip spares which already
2069 * have the right event counter, or have one earlier
2070 * (which would mean they aren't being marked as dirty
2071 * with the rest of the array)
2072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
NeilBrown5e865102009-11-12 12:08:04 +11002075 /* First make sure individual recovery_offsets are correct */
2076 list_for_each_entry(rdev, &mddev->disks, same_set) {
2077 if (rdev->raid_disk >= 0 &&
2078 !test_bit(In_sync, &rdev->flags) &&
2079 mddev->curr_resync_completed > rdev->recovery_offset)
2080 rdev->recovery_offset = mddev->curr_resync_completed;
2081
2082 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002083 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrown42543762006-06-26 00:27:57 -07002084 if (rdev->sb_events == mddev->events ||
2085 (nospares &&
2086 rdev->raid_disk < 0 &&
2087 (rdev->sb_events&1)==0 &&
2088 rdev->sb_events+1 == mddev->events)) {
2089 /* Don't update this superblock */
2090 rdev->sb_loaded = 2;
2091 } else {
2092 super_types[mddev->major_version].
2093 sync_super(mddev, rdev);
2094 rdev->sb_loaded = 1;
2095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097}
2098
NeilBrown850b2b422006-10-03 01:15:46 -07002099static void md_update_sb(mddev_t * mddev, int force_change)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 mdk_rdev_t *rdev;
NeilBrown06d91a52005-06-21 17:17:12 -07002102 int sync_req;
NeilBrown42543762006-06-26 00:27:57 -07002103 int nospares = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
NeilBrown1b57f132009-06-18 08:48:19 +10002105 mddev->utime = get_seconds();
NeilBrown8377bc82008-04-30 00:52:28 -07002106 if (mddev->external)
2107 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108repeat:
NeilBrowna9701a32005-11-08 21:39:34 -08002109 spin_lock_irq(&mddev->write_lock);
NeilBrown84692192006-08-27 01:23:49 -07002110
NeilBrown850b2b422006-10-03 01:15:46 -07002111 set_bit(MD_CHANGE_PENDING, &mddev->flags);
2112 if (test_and_clear_bit(MD_CHANGE_DEVS, &mddev->flags))
2113 force_change = 1;
2114 if (test_and_clear_bit(MD_CHANGE_CLEAN, &mddev->flags))
2115 /* just a clean<-> dirty transition, possibly leave spares alone,
2116 * though if events isn't the right even/odd, we will have to do
2117 * spares after all
2118 */
2119 nospares = 1;
2120 if (force_change)
2121 nospares = 0;
2122 if (mddev->degraded)
NeilBrown84692192006-08-27 01:23:49 -07002123 /* If the array is degraded, then skipping spares is both
2124 * dangerous and fairly pointless.
2125 * Dangerous because a device that was removed from the array
2126 * might have a event_count that still looks up-to-date,
2127 * so it can be re-added without a resync.
2128 * Pointless because if there are any spares to skip,
2129 * then a recovery will happen and soon that array won't
2130 * be degraded any more and the spare can go back to sleep then.
2131 */
NeilBrown850b2b422006-10-03 01:15:46 -07002132 nospares = 0;
NeilBrown84692192006-08-27 01:23:49 -07002133
NeilBrown06d91a52005-06-21 17:17:12 -07002134 sync_req = mddev->in_sync;
NeilBrown42543762006-06-26 00:27:57 -07002135
2136 /* If this is just a dirty<->clean transition, and the array is clean
2137 * and 'events' is odd, we can roll back to the previous clean state */
NeilBrown850b2b422006-10-03 01:15:46 -07002138 if (nospares
NeilBrown42543762006-06-26 00:27:57 -07002139 && (mddev->in_sync && mddev->recovery_cp == MaxSector)
NeilBrown1031be72007-01-26 00:57:02 -08002140 && (mddev->events & 1)
2141 && mddev->events != 1)
NeilBrown42543762006-06-26 00:27:57 -07002142 mddev->events--;
2143 else {
2144 /* otherwise we have to go forward and ... */
2145 mddev->events ++;
2146 if (!mddev->in_sync || mddev->recovery_cp != MaxSector) { /* not clean */
NeilBrown51d56682009-08-13 09:54:02 +10002147 /* .. if the array isn't clean, an 'even' event must also go
2148 * to spares. */
2149 if ((mddev->events&1)==0)
NeilBrown42543762006-06-26 00:27:57 -07002150 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002151 } else {
NeilBrown51d56682009-08-13 09:54:02 +10002152 /* otherwise an 'odd' event must go to spares */
2153 if ((mddev->events&1))
NeilBrown42543762006-06-26 00:27:57 -07002154 nospares = 0;
NeilBrown42543762006-06-26 00:27:57 -07002155 }
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 if (!mddev->events) {
2159 /*
2160 * oops, this 64-bit counter should never wrap.
2161 * Either we are in around ~1 trillion A.C., assuming
2162 * 1 reboot per second, or we have a bug:
2163 */
2164 MD_BUG();
2165 mddev->events --;
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 /*
2169 * do not write anything to disk if using
2170 * nonpersistent superblocks
2171 */
NeilBrown06d91a52005-06-21 17:17:12 -07002172 if (!mddev->persistent) {
NeilBrowne6910632008-02-06 01:39:51 -08002173 if (!mddev->external)
2174 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
2175
NeilBrowna9701a32005-11-08 21:39:34 -08002176 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002177 wake_up(&mddev->sb_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return;
NeilBrown06d91a52005-06-21 17:17:12 -07002179 }
NeilBrowne6910632008-02-06 01:39:51 -08002180 sync_sbs(mddev, nospares);
NeilBrowna9701a32005-11-08 21:39:34 -08002181 spin_unlock_irq(&mddev->write_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 dprintk(KERN_INFO
2184 "md: updating %s RAID superblock on device (in sync %d)\n",
2185 mdname(mddev),mddev->in_sync);
2186
NeilBrown4ad13662007-07-17 04:06:13 -07002187 bitmap_update_sb(mddev->bitmap);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002188 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 char b[BDEVNAME_SIZE];
2190 dprintk(KERN_INFO "md: ");
NeilBrown42543762006-06-26 00:27:57 -07002191 if (rdev->sb_loaded != 1)
2192 continue; /* no noise on spare devices */
NeilBrownb2d444d2005-11-08 21:39:31 -08002193 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 dprintk("(skipping faulty ");
2195
2196 dprintk("%s ", bdevname(rdev->bdev,b));
NeilBrownb2d444d2005-11-08 21:39:31 -08002197 if (!test_bit(Faulty, &rdev->flags)) {
NeilBrown7bfa19f2005-06-21 17:17:28 -07002198 md_super_write(mddev,rdev,
Andre Noll0f420352008-07-11 22:02:23 +10002199 rdev->sb_start, rdev->sb_size,
NeilBrown7bfa19f2005-06-21 17:17:28 -07002200 rdev->sb_page);
2201 dprintk(KERN_INFO "(write) %s's sb offset: %llu\n",
2202 bdevname(rdev->bdev,b),
Andre Noll0f420352008-07-11 22:02:23 +10002203 (unsigned long long)rdev->sb_start);
NeilBrown42543762006-06-26 00:27:57 -07002204 rdev->sb_events = mddev->events;
NeilBrown7bfa19f2005-06-21 17:17:28 -07002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 } else
2207 dprintk(")\n");
NeilBrown7bfa19f2005-06-21 17:17:28 -07002208 if (mddev->level == LEVEL_MULTIPATH)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /* only need to write one superblock... */
2210 break;
2211 }
NeilBrowna9701a32005-11-08 21:39:34 -08002212 md_super_wait(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07002213 /* if there was a failure, MD_CHANGE_DEVS was set, and we re-write super */
NeilBrown7bfa19f2005-06-21 17:17:28 -07002214
NeilBrowna9701a32005-11-08 21:39:34 -08002215 spin_lock_irq(&mddev->write_lock);
NeilBrown850b2b422006-10-03 01:15:46 -07002216 if (mddev->in_sync != sync_req ||
2217 test_bit(MD_CHANGE_DEVS, &mddev->flags)) {
NeilBrown06d91a52005-06-21 17:17:12 -07002218 /* have to write it out again */
NeilBrowna9701a32005-11-08 21:39:34 -08002219 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07002220 goto repeat;
2221 }
NeilBrown850b2b422006-10-03 01:15:46 -07002222 clear_bit(MD_CHANGE_PENDING, &mddev->flags);
NeilBrowna9701a32005-11-08 21:39:34 -08002223 spin_unlock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07002224 wake_up(&mddev->sb_wait);
NeilBrownacb180b2009-04-14 16:28:34 +10002225 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2226 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown06d91a52005-06-21 17:17:12 -07002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Andre Noll7f6ce762008-03-23 18:34:54 +01002230/* words written to sysfs files may, or may not, be \n terminated.
NeilBrownbce74da2006-01-06 00:20:41 -08002231 * We want to accept with case. For this we use cmd_match.
2232 */
2233static int cmd_match(const char *cmd, const char *str)
2234{
2235 /* See if cmd, written into a sysfs file, matches
2236 * str. They must either be the same, or cmd can
2237 * have a trailing newline
2238 */
2239 while (*cmd && *str && *cmd == *str) {
2240 cmd++;
2241 str++;
2242 }
2243 if (*cmd == '\n')
2244 cmd++;
2245 if (*str || *cmd)
2246 return 0;
2247 return 1;
2248}
2249
NeilBrown86e6ffd2005-11-08 21:39:24 -08002250struct rdev_sysfs_entry {
2251 struct attribute attr;
2252 ssize_t (*show)(mdk_rdev_t *, char *);
2253 ssize_t (*store)(mdk_rdev_t *, const char *, size_t);
2254};
2255
2256static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002257state_show(mdk_rdev_t *rdev, char *page)
NeilBrown86e6ffd2005-11-08 21:39:24 -08002258{
2259 char *sep = "";
NeilBrown20a49ff2008-02-06 01:39:57 -08002260 size_t len = 0;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002261
NeilBrownb2d444d2005-11-08 21:39:31 -08002262 if (test_bit(Faulty, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002263 len+= sprintf(page+len, "%sfaulty",sep);
2264 sep = ",";
2265 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002266 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002267 len += sprintf(page+len, "%sin_sync",sep);
2268 sep = ",";
2269 }
NeilBrownf6556752006-06-26 00:28:01 -07002270 if (test_bit(WriteMostly, &rdev->flags)) {
2271 len += sprintf(page+len, "%swrite_mostly",sep);
2272 sep = ",";
2273 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002274 if (test_bit(Blocked, &rdev->flags)) {
2275 len += sprintf(page+len, "%sblocked", sep);
2276 sep = ",";
2277 }
NeilBrownb2d444d2005-11-08 21:39:31 -08002278 if (!test_bit(Faulty, &rdev->flags) &&
2279 !test_bit(In_sync, &rdev->flags)) {
NeilBrown86e6ffd2005-11-08 21:39:24 -08002280 len += sprintf(page+len, "%sspare", sep);
2281 sep = ",";
2282 }
2283 return len+sprintf(page+len, "\n");
2284}
2285
NeilBrown45dc2de2006-06-26 00:27:58 -07002286static ssize_t
2287state_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2288{
2289 /* can write
2290 * faulty - simulates and error
2291 * remove - disconnects the device
NeilBrownf6556752006-06-26 00:28:01 -07002292 * writemostly - sets write_mostly
2293 * -writemostly - clears write_mostly
Dan Williams6bfe0b42008-04-30 00:52:32 -07002294 * blocked - sets the Blocked flag
2295 * -blocked - clears the Blocked flag
NeilBrown6d56e272009-04-14 12:01:57 +10002296 * insync - sets Insync providing device isn't active
NeilBrown45dc2de2006-06-26 00:27:58 -07002297 */
2298 int err = -EINVAL;
2299 if (cmd_match(buf, "faulty") && rdev->mddev->pers) {
2300 md_error(rdev->mddev, rdev);
2301 err = 0;
2302 } else if (cmd_match(buf, "remove")) {
2303 if (rdev->raid_disk >= 0)
2304 err = -EBUSY;
2305 else {
2306 mddev_t *mddev = rdev->mddev;
2307 kick_rdev_from_array(rdev);
NeilBrown3f9d7b02006-12-22 01:11:41 -08002308 if (mddev->pers)
2309 md_update_sb(mddev, 1);
NeilBrown45dc2de2006-06-26 00:27:58 -07002310 md_new_event(mddev);
2311 err = 0;
2312 }
NeilBrownf6556752006-06-26 00:28:01 -07002313 } else if (cmd_match(buf, "writemostly")) {
2314 set_bit(WriteMostly, &rdev->flags);
2315 err = 0;
2316 } else if (cmd_match(buf, "-writemostly")) {
2317 clear_bit(WriteMostly, &rdev->flags);
2318 err = 0;
Dan Williams6bfe0b42008-04-30 00:52:32 -07002319 } else if (cmd_match(buf, "blocked")) {
2320 set_bit(Blocked, &rdev->flags);
2321 err = 0;
2322 } else if (cmd_match(buf, "-blocked")) {
2323 clear_bit(Blocked, &rdev->flags);
2324 wake_up(&rdev->blocked_wait);
2325 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2326 md_wakeup_thread(rdev->mddev->thread);
2327
2328 err = 0;
NeilBrown6d56e272009-04-14 12:01:57 +10002329 } else if (cmd_match(buf, "insync") && rdev->raid_disk == -1) {
2330 set_bit(In_sync, &rdev->flags);
2331 err = 0;
NeilBrown45dc2de2006-06-26 00:27:58 -07002332 }
NeilBrown3c0ee632008-10-21 13:25:28 +11002333 if (!err && rdev->sysfs_state)
2334 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrown45dc2de2006-06-26 00:27:58 -07002335 return err ? err : len;
2336}
NeilBrown80ca3a42006-07-10 04:44:18 -07002337static struct rdev_sysfs_entry rdev_state =
2338__ATTR(state, S_IRUGO|S_IWUSR, state_show, state_store);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002339
2340static ssize_t
NeilBrown4dbcdc72006-01-06 00:20:52 -08002341errors_show(mdk_rdev_t *rdev, char *page)
2342{
2343 return sprintf(page, "%d\n", atomic_read(&rdev->corrected_errors));
2344}
2345
2346static ssize_t
2347errors_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2348{
2349 char *e;
2350 unsigned long n = simple_strtoul(buf, &e, 10);
2351 if (*buf && (*e == 0 || *e == '\n')) {
2352 atomic_set(&rdev->corrected_errors, n);
2353 return len;
2354 }
2355 return -EINVAL;
2356}
2357static struct rdev_sysfs_entry rdev_errors =
NeilBrown80ca3a42006-07-10 04:44:18 -07002358__ATTR(errors, S_IRUGO|S_IWUSR, errors_show, errors_store);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002359
NeilBrown014236d2006-01-06 00:20:55 -08002360static ssize_t
2361slot_show(mdk_rdev_t *rdev, char *page)
2362{
2363 if (rdev->raid_disk < 0)
2364 return sprintf(page, "none\n");
2365 else
2366 return sprintf(page, "%d\n", rdev->raid_disk);
2367}
2368
2369static ssize_t
2370slot_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2371{
2372 char *e;
NeilBrownc303da62008-02-06 01:39:51 -08002373 int err;
2374 char nm[20];
NeilBrown014236d2006-01-06 00:20:55 -08002375 int slot = simple_strtoul(buf, &e, 10);
2376 if (strncmp(buf, "none", 4)==0)
2377 slot = -1;
2378 else if (e==buf || (*e && *e!= '\n'))
2379 return -EINVAL;
Neil Brown6c2fce22008-06-28 08:31:31 +10002380 if (rdev->mddev->pers && slot == -1) {
NeilBrownc303da62008-02-06 01:39:51 -08002381 /* Setting 'slot' on an active array requires also
2382 * updating the 'rd%d' link, and communicating
2383 * with the personality with ->hot_*_disk.
2384 * For now we only support removing
2385 * failed/spare devices. This normally happens automatically,
2386 * but not when the metadata is externally managed.
2387 */
NeilBrownc303da62008-02-06 01:39:51 -08002388 if (rdev->raid_disk == -1)
2389 return -EEXIST;
2390 /* personality does all needed checks */
2391 if (rdev->mddev->pers->hot_add_disk == NULL)
2392 return -EINVAL;
2393 err = rdev->mddev->pers->
2394 hot_remove_disk(rdev->mddev, rdev->raid_disk);
2395 if (err)
2396 return err;
2397 sprintf(nm, "rd%d", rdev->raid_disk);
2398 sysfs_remove_link(&rdev->mddev->kobj, nm);
Maciej Trelab7103102010-04-14 16:58:16 +10002399 rdev->raid_disk = -1;
NeilBrownc303da62008-02-06 01:39:51 -08002400 set_bit(MD_RECOVERY_NEEDED, &rdev->mddev->recovery);
2401 md_wakeup_thread(rdev->mddev->thread);
Neil Brown6c2fce22008-06-28 08:31:31 +10002402 } else if (rdev->mddev->pers) {
2403 mdk_rdev_t *rdev2;
Neil Brown6c2fce22008-06-28 08:31:31 +10002404 /* Activating a spare .. or possibly reactivating
NeilBrown6d56e272009-04-14 12:01:57 +10002405 * if we ever get bitmaps working here.
Neil Brown6c2fce22008-06-28 08:31:31 +10002406 */
2407
2408 if (rdev->raid_disk != -1)
2409 return -EBUSY;
2410
2411 if (rdev->mddev->pers->hot_add_disk == NULL)
2412 return -EINVAL;
2413
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002414 list_for_each_entry(rdev2, &rdev->mddev->disks, same_set)
Neil Brown6c2fce22008-06-28 08:31:31 +10002415 if (rdev2->raid_disk == slot)
2416 return -EEXIST;
2417
2418 rdev->raid_disk = slot;
2419 if (test_bit(In_sync, &rdev->flags))
2420 rdev->saved_raid_disk = slot;
2421 else
2422 rdev->saved_raid_disk = -1;
2423 err = rdev->mddev->pers->
2424 hot_add_disk(rdev->mddev, rdev);
Neil Brown199050e2008-06-28 08:31:33 +10002425 if (err) {
Neil Brown6c2fce22008-06-28 08:31:31 +10002426 rdev->raid_disk = -1;
Neil Brown6c2fce22008-06-28 08:31:31 +10002427 return err;
Neil Brown52664732008-06-28 08:31:44 +10002428 } else
NeilBrown3c0ee632008-10-21 13:25:28 +11002429 sysfs_notify_dirent(rdev->sysfs_state);
Neil Brown6c2fce22008-06-28 08:31:31 +10002430 sprintf(nm, "rd%d", rdev->raid_disk);
2431 if (sysfs_create_link(&rdev->mddev->kobj, &rdev->kobj, nm))
2432 printk(KERN_WARNING
2433 "md: cannot register "
2434 "%s for %s\n",
2435 nm, mdname(rdev->mddev));
2436
2437 /* don't wakeup anyone, leave that to userspace. */
NeilBrownc303da62008-02-06 01:39:51 -08002438 } else {
2439 if (slot >= rdev->mddev->raid_disks)
2440 return -ENOSPC;
2441 rdev->raid_disk = slot;
2442 /* assume it is working */
NeilBrownc5d79ad2008-02-06 01:39:54 -08002443 clear_bit(Faulty, &rdev->flags);
2444 clear_bit(WriteMostly, &rdev->flags);
NeilBrownc303da62008-02-06 01:39:51 -08002445 set_bit(In_sync, &rdev->flags);
NeilBrown3c0ee632008-10-21 13:25:28 +11002446 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc303da62008-02-06 01:39:51 -08002447 }
NeilBrown014236d2006-01-06 00:20:55 -08002448 return len;
2449}
2450
2451
2452static struct rdev_sysfs_entry rdev_slot =
NeilBrown80ca3a42006-07-10 04:44:18 -07002453__ATTR(slot, S_IRUGO|S_IWUSR, slot_show, slot_store);
NeilBrown014236d2006-01-06 00:20:55 -08002454
NeilBrown93c8cad2006-01-06 00:20:56 -08002455static ssize_t
2456offset_show(mdk_rdev_t *rdev, char *page)
2457{
Andrew Morton6961ece2006-01-06 00:20:59 -08002458 return sprintf(page, "%llu\n", (unsigned long long)rdev->data_offset);
NeilBrown93c8cad2006-01-06 00:20:56 -08002459}
2460
2461static ssize_t
2462offset_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2463{
2464 char *e;
2465 unsigned long long offset = simple_strtoull(buf, &e, 10);
2466 if (e==buf || (*e && *e != '\n'))
2467 return -EINVAL;
Neil Brown8ed0a522008-06-28 08:31:29 +10002468 if (rdev->mddev->pers && rdev->raid_disk >= 0)
NeilBrown93c8cad2006-01-06 00:20:56 -08002469 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002470 if (rdev->sectors && rdev->mddev->external)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002471 /* Must set offset before size, so overlap checks
2472 * can be sane */
2473 return -EBUSY;
NeilBrown93c8cad2006-01-06 00:20:56 -08002474 rdev->data_offset = offset;
2475 return len;
2476}
2477
2478static struct rdev_sysfs_entry rdev_offset =
NeilBrown80ca3a42006-07-10 04:44:18 -07002479__ATTR(offset, S_IRUGO|S_IWUSR, offset_show, offset_store);
NeilBrown93c8cad2006-01-06 00:20:56 -08002480
NeilBrown83303b62006-01-06 00:21:06 -08002481static ssize_t
2482rdev_size_show(mdk_rdev_t *rdev, char *page)
2483{
Andre Nolldd8ac332009-03-31 14:33:13 +11002484 return sprintf(page, "%llu\n", (unsigned long long)rdev->sectors / 2);
NeilBrown83303b62006-01-06 00:21:06 -08002485}
2486
NeilBrownc5d79ad2008-02-06 01:39:54 -08002487static int overlaps(sector_t s1, sector_t l1, sector_t s2, sector_t l2)
2488{
2489 /* check if two start/length pairs overlap */
2490 if (s1+l1 <= s2)
2491 return 0;
2492 if (s2+l2 <= s1)
2493 return 0;
2494 return 1;
2495}
2496
Dan Williamsb522adc2009-03-31 15:00:31 +11002497static int strict_blocks_to_sectors(const char *buf, sector_t *sectors)
2498{
2499 unsigned long long blocks;
2500 sector_t new;
2501
2502 if (strict_strtoull(buf, 10, &blocks) < 0)
2503 return -EINVAL;
2504
2505 if (blocks & 1ULL << (8 * sizeof(blocks) - 1))
2506 return -EINVAL; /* sector conversion overflow */
2507
2508 new = blocks * 2;
2509 if (new != blocks * 2)
2510 return -EINVAL; /* unsigned long long to sector_t overflow */
2511
2512 *sectors = new;
2513 return 0;
2514}
2515
NeilBrown83303b62006-01-06 00:21:06 -08002516static ssize_t
2517rdev_size_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2518{
NeilBrown27c529b2008-03-04 14:29:33 -08002519 mddev_t *my_mddev = rdev->mddev;
Andre Nolldd8ac332009-03-31 14:33:13 +11002520 sector_t oldsectors = rdev->sectors;
Dan Williamsb522adc2009-03-31 15:00:31 +11002521 sector_t sectors;
NeilBrown27c529b2008-03-04 14:29:33 -08002522
Dan Williamsb522adc2009-03-31 15:00:31 +11002523 if (strict_blocks_to_sectors(buf, &sectors) < 0)
Neil Brownd7027452008-07-12 10:37:50 +10002524 return -EINVAL;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002525 if (my_mddev->pers && rdev->raid_disk >= 0) {
Neil Brownd7027452008-07-12 10:37:50 +10002526 if (my_mddev->persistent) {
Andre Nolldd8ac332009-03-31 14:33:13 +11002527 sectors = super_types[my_mddev->major_version].
2528 rdev_size_change(rdev, sectors);
2529 if (!sectors)
Chris Webb0cd17fe2008-06-28 08:31:46 +10002530 return -EBUSY;
Andre Nolldd8ac332009-03-31 14:33:13 +11002531 } else if (!sectors)
2532 sectors = (rdev->bdev->bd_inode->i_size >> 9) -
2533 rdev->data_offset;
Chris Webb0cd17fe2008-06-28 08:31:46 +10002534 }
Andre Nolldd8ac332009-03-31 14:33:13 +11002535 if (sectors < my_mddev->dev_sectors)
Chris Webb7d3c6f82008-10-13 11:55:11 +11002536 return -EINVAL; /* component must fit device */
Chris Webb0cd17fe2008-06-28 08:31:46 +10002537
Andre Nolldd8ac332009-03-31 14:33:13 +11002538 rdev->sectors = sectors;
2539 if (sectors > oldsectors && my_mddev->external) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002540 /* need to check that all other rdevs with the same ->bdev
2541 * do not overlap. We need to unlock the mddev to avoid
Andre Nolldd8ac332009-03-31 14:33:13 +11002542 * a deadlock. We have already changed rdev->sectors, and if
NeilBrownc5d79ad2008-02-06 01:39:54 -08002543 * we have to change it back, we will have the lock again.
2544 */
2545 mddev_t *mddev;
2546 int overlap = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002547 struct list_head *tmp;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002548
NeilBrown27c529b2008-03-04 14:29:33 -08002549 mddev_unlock(my_mddev);
NeilBrown29ac4aa2008-02-06 01:39:58 -08002550 for_each_mddev(mddev, tmp) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002551 mdk_rdev_t *rdev2;
2552
2553 mddev_lock(mddev);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002554 list_for_each_entry(rdev2, &mddev->disks, same_set)
NeilBrownc5d79ad2008-02-06 01:39:54 -08002555 if (test_bit(AllReserved, &rdev2->flags) ||
2556 (rdev->bdev == rdev2->bdev &&
2557 rdev != rdev2 &&
Andre Nolldd8ac332009-03-31 14:33:13 +11002558 overlaps(rdev->data_offset, rdev->sectors,
Andre Nolld07bd3b2008-07-21 14:42:07 +10002559 rdev2->data_offset,
Andre Nolldd8ac332009-03-31 14:33:13 +11002560 rdev2->sectors))) {
NeilBrownc5d79ad2008-02-06 01:39:54 -08002561 overlap = 1;
2562 break;
2563 }
2564 mddev_unlock(mddev);
2565 if (overlap) {
2566 mddev_put(mddev);
2567 break;
2568 }
2569 }
NeilBrown27c529b2008-03-04 14:29:33 -08002570 mddev_lock(my_mddev);
NeilBrownc5d79ad2008-02-06 01:39:54 -08002571 if (overlap) {
2572 /* Someone else could have slipped in a size
2573 * change here, but doing so is just silly.
Andre Nolldd8ac332009-03-31 14:33:13 +11002574 * We put oldsectors back because we *know* it is
NeilBrownc5d79ad2008-02-06 01:39:54 -08002575 * safe, and trust userspace not to race with
2576 * itself
2577 */
Andre Nolldd8ac332009-03-31 14:33:13 +11002578 rdev->sectors = oldsectors;
NeilBrownc5d79ad2008-02-06 01:39:54 -08002579 return -EBUSY;
2580 }
2581 }
NeilBrown83303b62006-01-06 00:21:06 -08002582 return len;
2583}
2584
2585static struct rdev_sysfs_entry rdev_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07002586__ATTR(size, S_IRUGO|S_IWUSR, rdev_size_show, rdev_size_store);
NeilBrown83303b62006-01-06 00:21:06 -08002587
Dan Williams06e3c812009-12-12 21:17:12 -07002588
2589static ssize_t recovery_start_show(mdk_rdev_t *rdev, char *page)
2590{
2591 unsigned long long recovery_start = rdev->recovery_offset;
2592
2593 if (test_bit(In_sync, &rdev->flags) ||
2594 recovery_start == MaxSector)
2595 return sprintf(page, "none\n");
2596
2597 return sprintf(page, "%llu\n", recovery_start);
2598}
2599
2600static ssize_t recovery_start_store(mdk_rdev_t *rdev, const char *buf, size_t len)
2601{
2602 unsigned long long recovery_start;
2603
2604 if (cmd_match(buf, "none"))
2605 recovery_start = MaxSector;
2606 else if (strict_strtoull(buf, 10, &recovery_start))
2607 return -EINVAL;
2608
2609 if (rdev->mddev->pers &&
2610 rdev->raid_disk >= 0)
2611 return -EBUSY;
2612
2613 rdev->recovery_offset = recovery_start;
2614 if (recovery_start == MaxSector)
2615 set_bit(In_sync, &rdev->flags);
2616 else
2617 clear_bit(In_sync, &rdev->flags);
2618 return len;
2619}
2620
2621static struct rdev_sysfs_entry rdev_recovery_start =
2622__ATTR(recovery_start, S_IRUGO|S_IWUSR, recovery_start_show, recovery_start_store);
2623
NeilBrown86e6ffd2005-11-08 21:39:24 -08002624static struct attribute *rdev_default_attrs[] = {
2625 &rdev_state.attr,
NeilBrown4dbcdc72006-01-06 00:20:52 -08002626 &rdev_errors.attr,
NeilBrown014236d2006-01-06 00:20:55 -08002627 &rdev_slot.attr,
NeilBrown93c8cad2006-01-06 00:20:56 -08002628 &rdev_offset.attr,
NeilBrown83303b62006-01-06 00:21:06 -08002629 &rdev_size.attr,
Dan Williams06e3c812009-12-12 21:17:12 -07002630 &rdev_recovery_start.attr,
NeilBrown86e6ffd2005-11-08 21:39:24 -08002631 NULL,
2632};
2633static ssize_t
2634rdev_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
2635{
2636 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2637 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002638 mddev_t *mddev = rdev->mddev;
2639 ssize_t rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002640
2641 if (!entry->show)
2642 return -EIO;
NeilBrown27c529b2008-03-04 14:29:33 -08002643
2644 rv = mddev ? mddev_lock(mddev) : -EBUSY;
2645 if (!rv) {
2646 if (rdev->mddev == NULL)
2647 rv = -EBUSY;
2648 else
2649 rv = entry->show(rdev, page);
2650 mddev_unlock(mddev);
2651 }
2652 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002653}
2654
2655static ssize_t
2656rdev_attr_store(struct kobject *kobj, struct attribute *attr,
2657 const char *page, size_t length)
2658{
2659 struct rdev_sysfs_entry *entry = container_of(attr, struct rdev_sysfs_entry, attr);
2660 mdk_rdev_t *rdev = container_of(kobj, mdk_rdev_t, kobj);
NeilBrown27c529b2008-03-04 14:29:33 -08002661 ssize_t rv;
2662 mddev_t *mddev = rdev->mddev;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002663
2664 if (!entry->store)
2665 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07002666 if (!capable(CAP_SYS_ADMIN))
2667 return -EACCES;
NeilBrown27c529b2008-03-04 14:29:33 -08002668 rv = mddev ? mddev_lock(mddev): -EBUSY;
NeilBrownca388052008-02-06 01:39:55 -08002669 if (!rv) {
NeilBrown27c529b2008-03-04 14:29:33 -08002670 if (rdev->mddev == NULL)
2671 rv = -EBUSY;
2672 else
2673 rv = entry->store(rdev, page, length);
Dan Williams6a518302008-04-30 00:52:28 -07002674 mddev_unlock(mddev);
NeilBrownca388052008-02-06 01:39:55 -08002675 }
2676 return rv;
NeilBrown86e6ffd2005-11-08 21:39:24 -08002677}
2678
2679static void rdev_free(struct kobject *ko)
2680{
2681 mdk_rdev_t *rdev = container_of(ko, mdk_rdev_t, kobj);
2682 kfree(rdev);
2683}
2684static struct sysfs_ops rdev_sysfs_ops = {
2685 .show = rdev_attr_show,
2686 .store = rdev_attr_store,
2687};
2688static struct kobj_type rdev_ktype = {
2689 .release = rdev_free,
2690 .sysfs_ops = &rdev_sysfs_ops,
2691 .default_attrs = rdev_default_attrs,
2692};
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694/*
2695 * Import a device. If 'super_format' >= 0, then sanity check the superblock
2696 *
2697 * mark the device faulty if:
2698 *
2699 * - the device is nonexistent (zero size)
2700 * - the device has no valid superblock
2701 *
2702 * a faulty rdev _never_ has rdev->sb set.
2703 */
2704static mdk_rdev_t *md_import_device(dev_t newdev, int super_format, int super_minor)
2705{
2706 char b[BDEVNAME_SIZE];
2707 int err;
2708 mdk_rdev_t *rdev;
2709 sector_t size;
2710
NeilBrown9ffae0c2006-01-06 00:20:32 -08002711 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (!rdev) {
2713 printk(KERN_ERR "md: could not alloc mem for new device!\n");
2714 return ERR_PTR(-ENOMEM);
2715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 if ((err = alloc_disk_sb(rdev)))
2718 goto abort_free;
2719
NeilBrownc5d79ad2008-02-06 01:39:54 -08002720 err = lock_rdev(rdev, newdev, super_format == -2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (err)
2722 goto abort_free;
2723
Greg Kroah-Hartmanf9cb0742007-12-17 23:05:35 -07002724 kobject_init(&rdev->kobj, &rdev_ktype);
NeilBrown86e6ffd2005-11-08 21:39:24 -08002725
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 rdev->desc_nr = -1;
NeilBrown2b6e8452006-10-28 10:38:30 -07002727 rdev->saved_raid_disk = -1;
NeilBrown3f9d7b02006-12-22 01:11:41 -08002728 rdev->raid_disk = -1;
NeilBrownb2d444d2005-11-08 21:39:31 -08002729 rdev->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 rdev->data_offset = 0;
NeilBrown42543762006-06-26 00:27:57 -07002731 rdev->sb_events = 0;
Robert Becker1e509152009-12-14 12:49:58 +11002732 rdev->last_read_error.tv_sec = 0;
2733 rdev->last_read_error.tv_nsec = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 atomic_set(&rdev->nr_pending, 0);
NeilBrownba22dcb2005-11-08 21:39:31 -08002735 atomic_set(&rdev->read_errors, 0);
NeilBrown4dbcdc72006-01-06 00:20:52 -08002736 atomic_set(&rdev->corrected_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 size = rdev->bdev->bd_inode->i_size >> BLOCK_SIZE_BITS;
2739 if (!size) {
2740 printk(KERN_WARNING
2741 "md: %s has zero or unknown size, marking faulty!\n",
2742 bdevname(rdev->bdev,b));
2743 err = -EINVAL;
2744 goto abort_free;
2745 }
2746
2747 if (super_format >= 0) {
2748 err = super_types[super_format].
2749 load_super(rdev, NULL, super_minor);
2750 if (err == -EINVAL) {
NeilBrowndf968c42007-07-17 04:06:11 -07002751 printk(KERN_WARNING
2752 "md: %s does not have a valid v%d.%d "
2753 "superblock, not importing!\n",
2754 bdevname(rdev->bdev,b),
2755 super_format, super_minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 goto abort_free;
2757 }
2758 if (err < 0) {
2759 printk(KERN_WARNING
2760 "md: could not read %s's sb, not importing!\n",
2761 bdevname(rdev->bdev,b));
2762 goto abort_free;
2763 }
2764 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07002765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 INIT_LIST_HEAD(&rdev->same_set);
Dan Williams6bfe0b42008-04-30 00:52:32 -07002767 init_waitqueue_head(&rdev->blocked_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 return rdev;
2770
2771abort_free:
2772 if (rdev->sb_page) {
2773 if (rdev->bdev)
2774 unlock_rdev(rdev);
2775 free_disk_sb(rdev);
2776 }
2777 kfree(rdev);
2778 return ERR_PTR(err);
2779}
2780
2781/*
2782 * Check a full RAID array for plausibility
2783 */
2784
2785
NeilBrowna757e642005-04-16 15:26:42 -07002786static void analyze_sbs(mddev_t * mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 int i;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11002789 mdk_rdev_t *rdev, *freshest, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 char b[BDEVNAME_SIZE];
2791
2792 freshest = NULL;
NeilBrownd089c6a2008-02-06 01:39:59 -08002793 rdev_for_each(rdev, tmp, mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 switch (super_types[mddev->major_version].
2795 load_super(rdev, freshest, mddev->minor_version)) {
2796 case 1:
2797 freshest = rdev;
2798 break;
2799 case 0:
2800 break;
2801 default:
2802 printk( KERN_ERR \
2803 "md: fatal superblock inconsistency in %s"
2804 " -- removing from array\n",
2805 bdevname(rdev->bdev,b));
2806 kick_rdev_from_array(rdev);
2807 }
2808
2809
2810 super_types[mddev->major_version].
2811 validate_super(mddev, freshest);
2812
2813 i = 0;
NeilBrownd089c6a2008-02-06 01:39:59 -08002814 rdev_for_each(rdev, tmp, mddev) {
NeilBrown233fca32010-04-14 17:02:09 +10002815 if (mddev->max_disks &&
2816 (rdev->desc_nr >= mddev->max_disks ||
2817 i > mddev->max_disks)) {
NeilBrownde01dfa2009-02-06 18:02:46 +11002818 printk(KERN_WARNING
2819 "md: %s: %s: only %d devices permitted\n",
2820 mdname(mddev), bdevname(rdev->bdev, b),
2821 mddev->max_disks);
2822 kick_rdev_from_array(rdev);
2823 continue;
2824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (rdev != freshest)
2826 if (super_types[mddev->major_version].
2827 validate_super(mddev, rdev)) {
2828 printk(KERN_WARNING "md: kicking non-fresh %s"
2829 " from array!\n",
2830 bdevname(rdev->bdev,b));
2831 kick_rdev_from_array(rdev);
2832 continue;
2833 }
2834 if (mddev->level == LEVEL_MULTIPATH) {
2835 rdev->desc_nr = i++;
2836 rdev->raid_disk = rdev->desc_nr;
NeilBrownb2d444d2005-11-08 21:39:31 -08002837 set_bit(In_sync, &rdev->flags);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002838 } else if (rdev->raid_disk >= (mddev->raid_disks - min(0, mddev->delta_disks))) {
NeilBrowna778b732007-05-23 13:58:10 -07002839 rdev->raid_disk = -1;
2840 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 }
2842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843}
2844
NeilBrown72e02072009-12-14 12:49:55 +11002845/* Read a fixed-point number.
2846 * Numbers in sysfs attributes should be in "standard" units where
2847 * possible, so time should be in seconds.
2848 * However we internally use a a much smaller unit such as
2849 * milliseconds or jiffies.
2850 * This function takes a decimal number with a possible fractional
2851 * component, and produces an integer which is the result of
2852 * multiplying that number by 10^'scale'.
2853 * all without any floating-point arithmetic.
2854 */
2855int strict_strtoul_scaled(const char *cp, unsigned long *res, int scale)
2856{
2857 unsigned long result = 0;
2858 long decimals = -1;
2859 while (isdigit(*cp) || (*cp == '.' && decimals < 0)) {
2860 if (*cp == '.')
2861 decimals = 0;
2862 else if (decimals < scale) {
2863 unsigned int value;
2864 value = *cp - '0';
2865 result = result * 10 + value;
2866 if (decimals >= 0)
2867 decimals++;
2868 }
2869 cp++;
2870 }
2871 if (*cp == '\n')
2872 cp++;
2873 if (*cp)
2874 return -EINVAL;
2875 if (decimals < 0)
2876 decimals = 0;
2877 while (decimals < scale) {
2878 result *= 10;
2879 decimals ++;
2880 }
2881 *res = result;
2882 return 0;
2883}
2884
2885
NeilBrown19052c02008-08-05 15:54:13 +10002886static void md_safemode_timeout(unsigned long data);
2887
NeilBrowneae17012005-11-08 21:39:23 -08002888static ssize_t
NeilBrown16f17b32006-06-26 00:27:37 -07002889safe_delay_show(mddev_t *mddev, char *page)
2890{
2891 int msec = (mddev->safemode_delay*1000)/HZ;
2892 return sprintf(page, "%d.%03d\n", msec/1000, msec%1000);
2893}
2894static ssize_t
2895safe_delay_store(mddev_t *mddev, const char *cbuf, size_t len)
2896{
NeilBrown16f17b32006-06-26 00:27:37 -07002897 unsigned long msec;
Dan Williams97ce0a72008-09-24 22:48:19 -07002898
NeilBrown72e02072009-12-14 12:49:55 +11002899 if (strict_strtoul_scaled(cbuf, &msec, 3) < 0)
NeilBrown16f17b32006-06-26 00:27:37 -07002900 return -EINVAL;
NeilBrown16f17b32006-06-26 00:27:37 -07002901 if (msec == 0)
2902 mddev->safemode_delay = 0;
2903 else {
NeilBrown19052c02008-08-05 15:54:13 +10002904 unsigned long old_delay = mddev->safemode_delay;
NeilBrown16f17b32006-06-26 00:27:37 -07002905 mddev->safemode_delay = (msec*HZ)/1000;
2906 if (mddev->safemode_delay == 0)
2907 mddev->safemode_delay = 1;
NeilBrown19052c02008-08-05 15:54:13 +10002908 if (mddev->safemode_delay < old_delay)
2909 md_safemode_timeout((unsigned long)mddev);
NeilBrown16f17b32006-06-26 00:27:37 -07002910 }
2911 return len;
2912}
2913static struct md_sysfs_entry md_safe_delay =
NeilBrown80ca3a42006-07-10 04:44:18 -07002914__ATTR(safe_mode_delay, S_IRUGO|S_IWUSR,safe_delay_show, safe_delay_store);
NeilBrown16f17b32006-06-26 00:27:37 -07002915
2916static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08002917level_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08002918{
NeilBrown2604b702006-01-06 00:20:36 -08002919 struct mdk_personality *p = mddev->pers;
NeilBrownd9d166c2006-01-06 00:20:51 -08002920 if (p)
NeilBrowneae17012005-11-08 21:39:23 -08002921 return sprintf(page, "%s\n", p->name);
NeilBrownd9d166c2006-01-06 00:20:51 -08002922 else if (mddev->clevel[0])
2923 return sprintf(page, "%s\n", mddev->clevel);
2924 else if (mddev->level != LEVEL_NONE)
2925 return sprintf(page, "%d\n", mddev->level);
2926 else
2927 return 0;
NeilBrowneae17012005-11-08 21:39:23 -08002928}
2929
NeilBrownd9d166c2006-01-06 00:20:51 -08002930static ssize_t
2931level_store(mddev_t *mddev, const char *buf, size_t len)
2932{
NeilBrown245f46c2009-03-31 14:39:39 +11002933 char level[16];
NeilBrown20a49ff2008-02-06 01:39:57 -08002934 ssize_t rv = len;
NeilBrown245f46c2009-03-31 14:39:39 +11002935 struct mdk_personality *pers;
2936 void *priv;
NeilBrown3a981b02009-08-03 10:59:55 +10002937 mdk_rdev_t *rdev;
NeilBrown245f46c2009-03-31 14:39:39 +11002938
2939 if (mddev->pers == NULL) {
2940 if (len == 0)
2941 return 0;
2942 if (len >= sizeof(mddev->clevel))
2943 return -ENOSPC;
2944 strncpy(mddev->clevel, buf, len);
2945 if (mddev->clevel[len-1] == '\n')
2946 len--;
2947 mddev->clevel[len] = 0;
2948 mddev->level = LEVEL_NONE;
2949 return rv;
2950 }
2951
2952 /* request to change the personality. Need to ensure:
2953 * - array is not engaged in resync/recovery/reshape
2954 * - old personality can be suspended
2955 * - new personality will access other array.
2956 */
2957
2958 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownd9d166c2006-01-06 00:20:51 -08002959 return -EBUSY;
NeilBrown245f46c2009-03-31 14:39:39 +11002960
2961 if (!mddev->pers->quiesce) {
2962 printk(KERN_WARNING "md: %s: %s does not support online personality change\n",
2963 mdname(mddev), mddev->pers->name);
2964 return -EINVAL;
2965 }
2966
2967 /* Now find the new personality */
2968 if (len == 0 || len >= sizeof(level))
2969 return -EINVAL;
2970 strncpy(level, buf, len);
2971 if (level[len-1] == '\n')
NeilBrownd9d166c2006-01-06 00:20:51 -08002972 len--;
NeilBrown245f46c2009-03-31 14:39:39 +11002973 level[len] = 0;
2974
2975 request_module("md-%s", level);
2976 spin_lock(&pers_lock);
2977 pers = find_pers(LEVEL_NONE, level);
2978 if (!pers || !try_module_get(pers->owner)) {
2979 spin_unlock(&pers_lock);
2980 printk(KERN_WARNING "md: personality %s not loaded\n", level);
2981 return -EINVAL;
2982 }
2983 spin_unlock(&pers_lock);
2984
2985 if (pers == mddev->pers) {
2986 /* Nothing to do! */
2987 module_put(pers->owner);
2988 return rv;
2989 }
2990 if (!pers->takeover) {
2991 module_put(pers->owner);
2992 printk(KERN_WARNING "md: %s: %s does not support personality takeover\n",
2993 mdname(mddev), level);
2994 return -EINVAL;
2995 }
2996
2997 /* ->takeover must set new_* and/or delta_disks
2998 * if it succeeds, and may set them when it fails.
2999 */
3000 priv = pers->takeover(mddev);
3001 if (IS_ERR(priv)) {
3002 mddev->new_level = mddev->level;
3003 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003004 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003005 mddev->raid_disks -= mddev->delta_disks;
3006 mddev->delta_disks = 0;
3007 module_put(pers->owner);
3008 printk(KERN_WARNING "md: %s: %s would not accept array\n",
3009 mdname(mddev), level);
3010 return PTR_ERR(priv);
3011 }
3012
3013 /* Looks like we have a winner */
3014 mddev_suspend(mddev);
3015 mddev->pers->stop(mddev);
NeilBrowna64c8762010-04-14 17:15:37 +10003016
3017 if (mddev->pers->sync_request == NULL &&
3018 pers->sync_request != NULL) {
3019 /* need to add the md_redundancy_group */
3020 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
3021 printk(KERN_WARNING
3022 "md: cannot register extra attributes for %s\n",
3023 mdname(mddev));
3024 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
3025 }
3026 if (mddev->pers->sync_request != NULL &&
3027 pers->sync_request == NULL) {
3028 /* need to remove the md_redundancy_group */
3029 if (mddev->to_remove == NULL)
3030 mddev->to_remove = &md_redundancy_group;
3031 }
3032
Trela Maciej54071b32010-03-08 16:02:42 +11003033 if (mddev->pers->sync_request == NULL &&
3034 mddev->external) {
3035 /* We are converting from a no-redundancy array
3036 * to a redundancy array and metadata is managed
3037 * externally so we need to be sure that writes
3038 * won't block due to a need to transition
3039 * clean->dirty
3040 * until external management is started.
3041 */
3042 mddev->in_sync = 0;
3043 mddev->safemode_delay = 0;
3044 mddev->safemode = 0;
3045 }
3046
NeilBrown245f46c2009-03-31 14:39:39 +11003047 module_put(mddev->pers->owner);
NeilBrown3a981b02009-08-03 10:59:55 +10003048 /* Invalidate devices that are now superfluous */
3049 list_for_each_entry(rdev, &mddev->disks, same_set)
3050 if (rdev->raid_disk >= mddev->raid_disks) {
3051 rdev->raid_disk = -1;
3052 clear_bit(In_sync, &rdev->flags);
3053 }
NeilBrown245f46c2009-03-31 14:39:39 +11003054 mddev->pers = pers;
3055 mddev->private = priv;
3056 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
3057 mddev->level = mddev->new_level;
3058 mddev->layout = mddev->new_layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003059 mddev->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown245f46c2009-03-31 14:39:39 +11003060 mddev->delta_disks = 0;
Trela, Maciej9af204c2010-03-08 16:02:44 +11003061 if (mddev->pers->sync_request == NULL) {
3062 /* this is now an array without redundancy, so
3063 * it must always be in_sync
3064 */
3065 mddev->in_sync = 1;
3066 del_timer_sync(&mddev->safemode_timer);
3067 }
NeilBrown245f46c2009-03-31 14:39:39 +11003068 pers->run(mddev);
3069 mddev_resume(mddev);
3070 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3071 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3072 md_wakeup_thread(mddev->thread);
Maciej Trela5cac78612010-04-14 17:17:39 +10003073 sysfs_notify(&mddev->kobj, NULL, "level");
NeilBrownd9d166c2006-01-06 00:20:51 -08003074 return rv;
3075}
3076
3077static struct md_sysfs_entry md_level =
NeilBrown80ca3a42006-07-10 04:44:18 -07003078__ATTR(level, S_IRUGO|S_IWUSR, level_show, level_store);
NeilBrowneae17012005-11-08 21:39:23 -08003079
NeilBrownd4dbd022006-06-26 00:27:59 -07003080
3081static ssize_t
3082layout_show(mddev_t *mddev, char *page)
3083{
3084 /* just a number, not meaningful for all levels */
NeilBrown08a02ec2007-05-09 02:35:38 -07003085 if (mddev->reshape_position != MaxSector &&
3086 mddev->layout != mddev->new_layout)
3087 return sprintf(page, "%d (%d)\n",
3088 mddev->new_layout, mddev->layout);
NeilBrownd4dbd022006-06-26 00:27:59 -07003089 return sprintf(page, "%d\n", mddev->layout);
3090}
3091
3092static ssize_t
3093layout_store(mddev_t *mddev, const char *buf, size_t len)
3094{
3095 char *e;
3096 unsigned long n = simple_strtoul(buf, &e, 10);
NeilBrownd4dbd022006-06-26 00:27:59 -07003097
3098 if (!*buf || (*e && *e != '\n'))
3099 return -EINVAL;
3100
NeilBrownb3546032009-03-31 14:56:41 +11003101 if (mddev->pers) {
3102 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003103 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003104 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003105 mddev->new_layout = n;
NeilBrown50ac1682009-06-18 08:47:55 +10003106 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003107 if (err) {
3108 mddev->new_layout = mddev->layout;
NeilBrownb3546032009-03-31 14:56:41 +11003109 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003110 }
NeilBrownb3546032009-03-31 14:56:41 +11003111 } else {
NeilBrown08a02ec2007-05-09 02:35:38 -07003112 mddev->new_layout = n;
NeilBrownb3546032009-03-31 14:56:41 +11003113 if (mddev->reshape_position == MaxSector)
3114 mddev->layout = n;
3115 }
NeilBrownd4dbd022006-06-26 00:27:59 -07003116 return len;
3117}
3118static struct md_sysfs_entry md_layout =
NeilBrown80ca3a42006-07-10 04:44:18 -07003119__ATTR(layout, S_IRUGO|S_IWUSR, layout_show, layout_store);
NeilBrownd4dbd022006-06-26 00:27:59 -07003120
3121
NeilBrowneae17012005-11-08 21:39:23 -08003122static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003123raid_disks_show(mddev_t *mddev, char *page)
NeilBrowneae17012005-11-08 21:39:23 -08003124{
NeilBrownbb636542005-11-08 21:39:45 -08003125 if (mddev->raid_disks == 0)
3126 return 0;
NeilBrown08a02ec2007-05-09 02:35:38 -07003127 if (mddev->reshape_position != MaxSector &&
3128 mddev->delta_disks != 0)
3129 return sprintf(page, "%d (%d)\n", mddev->raid_disks,
3130 mddev->raid_disks - mddev->delta_disks);
NeilBrowneae17012005-11-08 21:39:23 -08003131 return sprintf(page, "%d\n", mddev->raid_disks);
3132}
3133
NeilBrownda943b992006-01-06 00:20:54 -08003134static int update_raid_disks(mddev_t *mddev, int raid_disks);
3135
3136static ssize_t
3137raid_disks_store(mddev_t *mddev, const char *buf, size_t len)
3138{
NeilBrownda943b992006-01-06 00:20:54 -08003139 char *e;
3140 int rv = 0;
3141 unsigned long n = simple_strtoul(buf, &e, 10);
3142
3143 if (!*buf || (*e && *e != '\n'))
3144 return -EINVAL;
3145
3146 if (mddev->pers)
3147 rv = update_raid_disks(mddev, n);
NeilBrown08a02ec2007-05-09 02:35:38 -07003148 else if (mddev->reshape_position != MaxSector) {
3149 int olddisks = mddev->raid_disks - mddev->delta_disks;
3150 mddev->delta_disks = n - olddisks;
3151 mddev->raid_disks = n;
3152 } else
NeilBrownda943b992006-01-06 00:20:54 -08003153 mddev->raid_disks = n;
3154 return rv ? rv : len;
3155}
3156static struct md_sysfs_entry md_raid_disks =
NeilBrown80ca3a42006-07-10 04:44:18 -07003157__ATTR(raid_disks, S_IRUGO|S_IWUSR, raid_disks_show, raid_disks_store);
NeilBrowneae17012005-11-08 21:39:23 -08003158
NeilBrown24dd4692005-11-08 21:39:26 -08003159static ssize_t
NeilBrown3b343802006-01-06 00:20:47 -08003160chunk_size_show(mddev_t *mddev, char *page)
3161{
NeilBrown08a02ec2007-05-09 02:35:38 -07003162 if (mddev->reshape_position != MaxSector &&
Andre Noll664e7c42009-06-18 08:45:27 +10003163 mddev->chunk_sectors != mddev->new_chunk_sectors)
3164 return sprintf(page, "%d (%d)\n",
3165 mddev->new_chunk_sectors << 9,
Andre Noll9d8f0362009-06-18 08:45:01 +10003166 mddev->chunk_sectors << 9);
3167 return sprintf(page, "%d\n", mddev->chunk_sectors << 9);
NeilBrown3b343802006-01-06 00:20:47 -08003168}
3169
3170static ssize_t
3171chunk_size_store(mddev_t *mddev, const char *buf, size_t len)
3172{
NeilBrown3b343802006-01-06 00:20:47 -08003173 char *e;
3174 unsigned long n = simple_strtoul(buf, &e, 10);
3175
NeilBrown3b343802006-01-06 00:20:47 -08003176 if (!*buf || (*e && *e != '\n'))
3177 return -EINVAL;
3178
NeilBrownb3546032009-03-31 14:56:41 +11003179 if (mddev->pers) {
3180 int err;
NeilBrown50ac1682009-06-18 08:47:55 +10003181 if (mddev->pers->check_reshape == NULL)
NeilBrownb3546032009-03-31 14:56:41 +11003182 return -EBUSY;
NeilBrown597a7112009-06-18 08:47:42 +10003183 mddev->new_chunk_sectors = n >> 9;
NeilBrown50ac1682009-06-18 08:47:55 +10003184 err = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10003185 if (err) {
3186 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11003187 return err;
NeilBrown597a7112009-06-18 08:47:42 +10003188 }
NeilBrownb3546032009-03-31 14:56:41 +11003189 } else {
Andre Noll664e7c42009-06-18 08:45:27 +10003190 mddev->new_chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003191 if (mddev->reshape_position == MaxSector)
Andre Noll9d8f0362009-06-18 08:45:01 +10003192 mddev->chunk_sectors = n >> 9;
NeilBrownb3546032009-03-31 14:56:41 +11003193 }
NeilBrown3b343802006-01-06 00:20:47 -08003194 return len;
3195}
3196static struct md_sysfs_entry md_chunk_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003197__ATTR(chunk_size, S_IRUGO|S_IWUSR, chunk_size_show, chunk_size_store);
NeilBrown3b343802006-01-06 00:20:47 -08003198
NeilBrowna94213b2006-06-26 00:28:00 -07003199static ssize_t
3200resync_start_show(mddev_t *mddev, char *page)
3201{
NeilBrownd1a7c502009-03-31 15:24:32 +11003202 if (mddev->recovery_cp == MaxSector)
3203 return sprintf(page, "none\n");
NeilBrowna94213b2006-06-26 00:28:00 -07003204 return sprintf(page, "%llu\n", (unsigned long long)mddev->recovery_cp);
3205}
3206
3207static ssize_t
3208resync_start_store(mddev_t *mddev, const char *buf, size_t len)
3209{
NeilBrowna94213b2006-06-26 00:28:00 -07003210 char *e;
3211 unsigned long long n = simple_strtoull(buf, &e, 10);
3212
3213 if (mddev->pers)
3214 return -EBUSY;
Dan Williams06e3c812009-12-12 21:17:12 -07003215 if (cmd_match(buf, "none"))
3216 n = MaxSector;
3217 else if (!*buf || (*e && *e != '\n'))
NeilBrowna94213b2006-06-26 00:28:00 -07003218 return -EINVAL;
3219
3220 mddev->recovery_cp = n;
3221 return len;
3222}
3223static struct md_sysfs_entry md_resync_start =
NeilBrown80ca3a42006-07-10 04:44:18 -07003224__ATTR(resync_start, S_IRUGO|S_IWUSR, resync_start_show, resync_start_store);
NeilBrowna94213b2006-06-26 00:28:00 -07003225
NeilBrown9e653b62006-06-26 00:27:58 -07003226/*
3227 * The array state can be:
3228 *
3229 * clear
3230 * No devices, no size, no level
3231 * Equivalent to STOP_ARRAY ioctl
3232 * inactive
3233 * May have some settings, but array is not active
3234 * all IO results in error
3235 * When written, doesn't tear down array, but just stops it
3236 * suspended (not supported yet)
3237 * All IO requests will block. The array can be reconfigured.
Andre Noll910d8cb2008-03-25 21:00:53 +01003238 * Writing this, if accepted, will block until array is quiescent
NeilBrown9e653b62006-06-26 00:27:58 -07003239 * readonly
3240 * no resync can happen. no superblocks get written.
3241 * write requests fail
3242 * read-auto
3243 * like readonly, but behaves like 'clean' on a write request.
3244 *
3245 * clean - no pending writes, but otherwise active.
3246 * When written to inactive array, starts without resync
3247 * If a write request arrives then
3248 * if metadata is known, mark 'dirty' and switch to 'active'.
3249 * if not known, block and switch to write-pending
3250 * If written to an active array that has pending writes, then fails.
3251 * active
3252 * fully active: IO and resync can be happening.
3253 * When written to inactive array, starts with resync
3254 *
3255 * write-pending
3256 * clean, but writes are blocked waiting for 'active' to be written.
3257 *
3258 * active-idle
3259 * like active, but no writes have been seen for a while (100msec).
3260 *
3261 */
3262enum array_state { clear, inactive, suspended, readonly, read_auto, clean, active,
3263 write_pending, active_idle, bad_word};
Adrian Bunk05381952006-06-26 00:28:01 -07003264static char *array_states[] = {
NeilBrown9e653b62006-06-26 00:27:58 -07003265 "clear", "inactive", "suspended", "readonly", "read-auto", "clean", "active",
3266 "write-pending", "active-idle", NULL };
3267
3268static int match_word(const char *word, char **list)
3269{
3270 int n;
3271 for (n=0; list[n]; n++)
3272 if (cmd_match(word, list[n]))
3273 break;
3274 return n;
3275}
3276
3277static ssize_t
3278array_state_show(mddev_t *mddev, char *page)
3279{
3280 enum array_state st = inactive;
3281
3282 if (mddev->pers)
3283 switch(mddev->ro) {
3284 case 1:
3285 st = readonly;
3286 break;
3287 case 2:
3288 st = read_auto;
3289 break;
3290 case 0:
3291 if (mddev->in_sync)
3292 st = clean;
NeilBrowne6910632008-02-06 01:39:51 -08003293 else if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
3294 st = write_pending;
NeilBrown9e653b62006-06-26 00:27:58 -07003295 else if (mddev->safemode)
3296 st = active_idle;
3297 else
3298 st = active;
3299 }
3300 else {
3301 if (list_empty(&mddev->disks) &&
3302 mddev->raid_disks == 0 &&
Andre Noll58c0fed2009-03-31 14:33:13 +11003303 mddev->dev_sectors == 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003304 st = clear;
3305 else
3306 st = inactive;
3307 }
3308 return sprintf(page, "%s\n", array_states[st]);
3309}
3310
Neil Browndf5b20c2008-07-11 22:02:22 +10003311static int do_md_stop(mddev_t * mddev, int ro, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003312static int do_md_run(mddev_t * mddev);
3313static int restart_array(mddev_t *mddev);
3314
3315static ssize_t
3316array_state_store(mddev_t *mddev, const char *buf, size_t len)
3317{
3318 int err = -EINVAL;
3319 enum array_state st = match_word(buf, array_states);
3320 switch(st) {
3321 case bad_word:
3322 break;
3323 case clear:
3324 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003325 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003326 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003327 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003328 break;
3329 case inactive:
3330 /* stopping an active array */
3331 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003332 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003333 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003334 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003335 } else
3336 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003337 break;
3338 case suspended:
3339 break; /* not supported yet */
3340 case readonly:
3341 if (mddev->pers)
Neil Browndf5b20c2008-07-11 22:02:22 +10003342 err = do_md_stop(mddev, 1, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003343 else {
3344 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003345 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003346 err = do_md_run(mddev);
3347 }
3348 break;
3349 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003350 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003351 if (mddev->ro == 0)
Neil Browndf5b20c2008-07-11 22:02:22 +10003352 err = do_md_stop(mddev, 1, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003353 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003354 err = restart_array(mddev);
3355 if (err == 0) {
3356 mddev->ro = 2;
3357 set_disk_ro(mddev->gendisk, 0);
3358 }
NeilBrown9e653b62006-06-26 00:27:58 -07003359 } else {
3360 mddev->ro = 2;
3361 err = do_md_run(mddev);
3362 }
3363 break;
3364 case clean:
3365 if (mddev->pers) {
3366 restart_array(mddev);
3367 spin_lock_irq(&mddev->write_lock);
3368 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003369 if (mddev->in_sync == 0) {
3370 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003371 if (mddev->safemode == 1)
3372 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003373 if (mddev->persistent)
3374 set_bit(MD_CHANGE_CLEAN,
3375 &mddev->flags);
3376 }
3377 err = 0;
3378 } else
3379 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003380 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003381 } else
3382 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003383 break;
3384 case active:
3385 if (mddev->pers) {
3386 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003387 if (mddev->external)
3388 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003389 wake_up(&mddev->sb_wait);
3390 err = 0;
3391 } else {
3392 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003393 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003394 err = do_md_run(mddev);
3395 }
3396 break;
3397 case write_pending:
3398 case active_idle:
3399 /* these cannot be set */
3400 break;
3401 }
3402 if (err)
3403 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003404 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003405 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003406 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003407 }
NeilBrown9e653b62006-06-26 00:27:58 -07003408}
NeilBrown80ca3a42006-07-10 04:44:18 -07003409static struct md_sysfs_entry md_array_state =
3410__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003411
NeilBrown6d7ff7382006-01-06 00:21:16 -08003412static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003413max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3414 return sprintf(page, "%d\n",
3415 atomic_read(&mddev->max_corr_read_errors));
3416}
3417
3418static ssize_t
3419max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3420{
3421 char *e;
3422 unsigned long n = simple_strtoul(buf, &e, 10);
3423
3424 if (*buf && (*e == 0 || *e == '\n')) {
3425 atomic_set(&mddev->max_corr_read_errors, n);
3426 return len;
3427 }
3428 return -EINVAL;
3429}
3430
3431static struct md_sysfs_entry max_corr_read_errors =
3432__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3433 max_corrected_read_errors_store);
3434
3435static ssize_t
NeilBrown6d7ff7382006-01-06 00:21:16 -08003436null_show(mddev_t *mddev, char *page)
3437{
3438 return -EINVAL;
3439}
3440
3441static ssize_t
3442new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3443{
3444 /* buf must be %d:%d\n? giving major and minor numbers */
3445 /* The new device is added to the array.
3446 * If the array has a persistent superblock, we read the
3447 * superblock to initialise info and check validity.
3448 * Otherwise, only checking done is that in bind_rdev_to_array,
3449 * which mainly checks size.
3450 */
3451 char *e;
3452 int major = simple_strtoul(buf, &e, 10);
3453 int minor;
3454 dev_t dev;
3455 mdk_rdev_t *rdev;
3456 int err;
3457
3458 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3459 return -EINVAL;
3460 minor = simple_strtoul(e+1, &e, 10);
3461 if (*e && *e != '\n')
3462 return -EINVAL;
3463 dev = MKDEV(major, minor);
3464 if (major != MAJOR(dev) ||
3465 minor != MINOR(dev))
3466 return -EOVERFLOW;
3467
3468
3469 if (mddev->persistent) {
3470 rdev = md_import_device(dev, mddev->major_version,
3471 mddev->minor_version);
3472 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3473 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3474 mdk_rdev_t, same_set);
3475 err = super_types[mddev->major_version]
3476 .load_super(rdev, rdev0, mddev->minor_version);
3477 if (err < 0)
3478 goto out;
3479 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003480 } else if (mddev->external)
3481 rdev = md_import_device(dev, -2, -1);
3482 else
NeilBrown6d7ff7382006-01-06 00:21:16 -08003483 rdev = md_import_device(dev, -1, -1);
3484
3485 if (IS_ERR(rdev))
3486 return PTR_ERR(rdev);
3487 err = bind_rdev_to_array(rdev, mddev);
3488 out:
3489 if (err)
3490 export_rdev(rdev);
3491 return err ? err : len;
3492}
3493
3494static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003495__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003496
3497static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003498bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3499{
3500 char *end;
3501 unsigned long chunk, end_chunk;
3502
3503 if (!mddev->bitmap)
3504 goto out;
3505 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3506 while (*buf) {
3507 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3508 if (buf == end) break;
3509 if (*end == '-') { /* range */
3510 buf = end + 1;
3511 end_chunk = simple_strtoul(buf, &end, 0);
3512 if (buf == end) break;
3513 }
3514 if (*end && !isspace(*end)) break;
3515 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003516 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003517 }
3518 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3519out:
3520 return len;
3521}
3522
3523static struct md_sysfs_entry md_bitmap =
3524__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3525
3526static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003527size_show(mddev_t *mddev, char *page)
3528{
Andre Noll58c0fed2009-03-31 14:33:13 +11003529 return sprintf(page, "%llu\n",
3530 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003531}
3532
Andre Nolld71f9f82008-07-11 22:02:22 +10003533static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003534
3535static ssize_t
3536size_store(mddev_t *mddev, const char *buf, size_t len)
3537{
3538 /* If array is inactive, we can reduce the component size, but
3539 * not increase it (except from 0).
3540 * If array is active, we can try an on-line resize
3541 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003542 sector_t sectors;
3543 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003544
Andre Noll58c0fed2009-03-31 14:33:13 +11003545 if (err < 0)
3546 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003547 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003548 err = update_size(mddev, sectors);
NeilBrown850b2b422006-10-03 01:15:46 -07003549 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003550 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003551 if (mddev->dev_sectors == 0 ||
3552 mddev->dev_sectors > sectors)
3553 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003554 else
3555 err = -ENOSPC;
3556 }
3557 return err ? err : len;
3558}
3559
3560static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003561__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003562
NeilBrown8bb93aa2006-01-06 00:20:50 -08003563
3564/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003565 * This is one of
3566 * 'none' for arrays with no metadata (good luck...)
3567 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003568 * or N.M for internally known formats
3569 */
3570static ssize_t
3571metadata_show(mddev_t *mddev, char *page)
3572{
3573 if (mddev->persistent)
3574 return sprintf(page, "%d.%d\n",
3575 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003576 else if (mddev->external)
3577 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003578 else
3579 return sprintf(page, "none\n");
3580}
3581
3582static ssize_t
3583metadata_store(mddev_t *mddev, const char *buf, size_t len)
3584{
3585 int major, minor;
3586 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003587 /* Changing the details of 'external' metadata is
3588 * always permitted. Otherwise there must be
3589 * no devices attached to the array.
3590 */
3591 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3592 ;
3593 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003594 return -EBUSY;
3595
3596 if (cmd_match(buf, "none")) {
3597 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003598 mddev->external = 0;
3599 mddev->major_version = 0;
3600 mddev->minor_version = 90;
3601 return len;
3602 }
3603 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003604 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003605 if (namelen >= sizeof(mddev->metadata_type))
3606 namelen = sizeof(mddev->metadata_type)-1;
3607 strncpy(mddev->metadata_type, buf+9, namelen);
3608 mddev->metadata_type[namelen] = 0;
3609 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3610 mddev->metadata_type[--namelen] = 0;
3611 mddev->persistent = 0;
3612 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003613 mddev->major_version = 0;
3614 mddev->minor_version = 90;
3615 return len;
3616 }
3617 major = simple_strtoul(buf, &e, 10);
3618 if (e==buf || *e != '.')
3619 return -EINVAL;
3620 buf = e+1;
3621 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003622 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003623 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003624 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003625 return -ENOENT;
3626 mddev->major_version = major;
3627 mddev->minor_version = minor;
3628 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003629 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003630 return len;
3631}
3632
3633static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003634__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003635
NeilBrowna35b0d62006-01-06 00:20:49 -08003636static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003637action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003638{
NeilBrown7eec3142005-11-08 21:39:44 -08003639 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003640 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3641 type = "frozen";
3642 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003643 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003644 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3645 type = "reshape";
3646 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003647 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3648 type = "resync";
3649 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3650 type = "check";
3651 else
3652 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003653 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003654 type = "recover";
3655 }
3656 return sprintf(page, "%s\n", type);
3657}
3658
3659static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003660action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003661{
NeilBrown7eec3142005-11-08 21:39:44 -08003662 if (!mddev->pers || !mddev->pers->sync_request)
3663 return -EINVAL;
3664
NeilBrownb6a9ce62009-05-26 09:41:17 +10003665 if (cmd_match(page, "frozen"))
3666 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3667 else
3668 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3669
3670 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003671 if (mddev->sync_thread) {
3672 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3673 md_unregister_thread(mddev->sync_thread);
3674 mddev->sync_thread = NULL;
3675 mddev->recovery = 0;
3676 }
NeilBrown03c902e2006-01-06 00:20:46 -08003677 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3678 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003679 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003680 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003681 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003682 else if (cmd_match(page, "recover")) {
3683 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3684 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3685 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003686 int err;
3687 if (mddev->pers->start_reshape == NULL)
3688 return -EINVAL;
3689 err = mddev->pers->start_reshape(mddev);
3690 if (err)
3691 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003692 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003693 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003694 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003695 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003696 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003697 return -EINVAL;
3698 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3699 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003700 }
NeilBrown03c902e2006-01-06 00:20:46 -08003701 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003702 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003703 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003704 return len;
3705}
3706
NeilBrown9d888832005-11-08 21:39:26 -08003707static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003708mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003709{
3710 return sprintf(page, "%llu\n",
3711 (unsigned long long) mddev->resync_mismatches);
3712}
3713
NeilBrown80ca3a42006-07-10 04:44:18 -07003714static struct md_sysfs_entry md_scan_mode =
3715__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003716
NeilBrown96de1e62005-11-08 21:39:39 -08003717
NeilBrown80ca3a42006-07-10 04:44:18 -07003718static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003719
NeilBrown88202a02006-01-06 00:21:36 -08003720static ssize_t
3721sync_min_show(mddev_t *mddev, char *page)
3722{
3723 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3724 mddev->sync_speed_min ? "local": "system");
3725}
3726
3727static ssize_t
3728sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3729{
3730 int min;
3731 char *e;
3732 if (strncmp(buf, "system", 6)==0) {
3733 mddev->sync_speed_min = 0;
3734 return len;
3735 }
3736 min = simple_strtoul(buf, &e, 10);
3737 if (buf == e || (*e && *e != '\n') || min <= 0)
3738 return -EINVAL;
3739 mddev->sync_speed_min = min;
3740 return len;
3741}
3742
3743static struct md_sysfs_entry md_sync_min =
3744__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3745
3746static ssize_t
3747sync_max_show(mddev_t *mddev, char *page)
3748{
3749 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3750 mddev->sync_speed_max ? "local": "system");
3751}
3752
3753static ssize_t
3754sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3755{
3756 int max;
3757 char *e;
3758 if (strncmp(buf, "system", 6)==0) {
3759 mddev->sync_speed_max = 0;
3760 return len;
3761 }
3762 max = simple_strtoul(buf, &e, 10);
3763 if (buf == e || (*e && *e != '\n') || max <= 0)
3764 return -EINVAL;
3765 mddev->sync_speed_max = max;
3766 return len;
3767}
3768
3769static struct md_sysfs_entry md_sync_max =
3770__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3771
Iustin Popd7f3d292007-10-16 23:30:54 -07003772static ssize_t
3773degraded_show(mddev_t *mddev, char *page)
3774{
3775 return sprintf(page, "%d\n", mddev->degraded);
3776}
3777static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003778
3779static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003780sync_force_parallel_show(mddev_t *mddev, char *page)
3781{
3782 return sprintf(page, "%d\n", mddev->parallel_resync);
3783}
3784
3785static ssize_t
3786sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3787{
3788 long n;
3789
3790 if (strict_strtol(buf, 10, &n))
3791 return -EINVAL;
3792
3793 if (n != 0 && n != 1)
3794 return -EINVAL;
3795
3796 mddev->parallel_resync = n;
3797
3798 if (mddev->sync_thread)
3799 wake_up(&resync_wait);
3800
3801 return len;
3802}
3803
3804/* force parallel resync, even with shared block devices */
3805static struct md_sysfs_entry md_sync_force_parallel =
3806__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3807 sync_force_parallel_show, sync_force_parallel_store);
3808
3809static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003810sync_speed_show(mddev_t *mddev, char *page)
3811{
3812 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003813 if (mddev->curr_resync == 0)
3814 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003815 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3816 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003817 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003818 db = resync - mddev->resync_mark_cnt;
3819 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003820}
3821
NeilBrown80ca3a42006-07-10 04:44:18 -07003822static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003823
3824static ssize_t
3825sync_completed_show(mddev_t *mddev, char *page)
3826{
Andre Noll58c0fed2009-03-31 14:33:13 +11003827 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003828
NeilBrownacb180b2009-04-14 16:28:34 +10003829 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3830 return sprintf(page, "none\n");
3831
NeilBrown88202a02006-01-06 00:21:36 -08003832 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003833 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003834 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003835 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003836
NeilBrownacb180b2009-04-14 16:28:34 +10003837 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003838 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003839}
3840
NeilBrown80ca3a42006-07-10 04:44:18 -07003841static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003842
NeilBrowne464eaf2006-03-27 01:18:14 -08003843static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003844min_sync_show(mddev_t *mddev, char *page)
3845{
3846 return sprintf(page, "%llu\n",
3847 (unsigned long long)mddev->resync_min);
3848}
3849static ssize_t
3850min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3851{
3852 unsigned long long min;
3853 if (strict_strtoull(buf, 10, &min))
3854 return -EINVAL;
3855 if (min > mddev->resync_max)
3856 return -EINVAL;
3857 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3858 return -EBUSY;
3859
3860 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003861 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003862 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003863 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003864 return -EINVAL;
3865 }
3866 mddev->resync_min = min;
3867
3868 return len;
3869}
3870
3871static struct md_sysfs_entry md_min_sync =
3872__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3873
3874static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003875max_sync_show(mddev_t *mddev, char *page)
3876{
3877 if (mddev->resync_max == MaxSector)
3878 return sprintf(page, "max\n");
3879 else
3880 return sprintf(page, "%llu\n",
3881 (unsigned long long)mddev->resync_max);
3882}
3883static ssize_t
3884max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3885{
3886 if (strncmp(buf, "max", 3) == 0)
3887 mddev->resync_max = MaxSector;
3888 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003889 unsigned long long max;
3890 if (strict_strtoull(buf, 10, &max))
3891 return -EINVAL;
3892 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003893 return -EINVAL;
3894 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003895 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003896 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3897 return -EBUSY;
3898
3899 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003900 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003901 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003902 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003903 return -EINVAL;
3904 }
3905 mddev->resync_max = max;
3906 }
3907 wake_up(&mddev->recovery_wait);
3908 return len;
3909}
3910
3911static struct md_sysfs_entry md_max_sync =
3912__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3913
3914static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003915suspend_lo_show(mddev_t *mddev, char *page)
3916{
3917 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3918}
3919
3920static ssize_t
3921suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3922{
3923 char *e;
3924 unsigned long long new = simple_strtoull(buf, &e, 10);
3925
NeilBrownb8d966e2009-07-01 11:14:04 +10003926 if (mddev->pers == NULL ||
3927 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003928 return -EINVAL;
3929 if (buf == e || (*e && *e != '\n'))
3930 return -EINVAL;
3931 if (new >= mddev->suspend_hi ||
3932 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3933 mddev->suspend_lo = new;
3934 mddev->pers->quiesce(mddev, 2);
3935 return len;
3936 } else
3937 return -EINVAL;
3938}
3939static struct md_sysfs_entry md_suspend_lo =
3940__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3941
3942
3943static ssize_t
3944suspend_hi_show(mddev_t *mddev, char *page)
3945{
3946 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3947}
3948
3949static ssize_t
3950suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3951{
3952 char *e;
3953 unsigned long long new = simple_strtoull(buf, &e, 10);
3954
NeilBrownb8d966e2009-07-01 11:14:04 +10003955 if (mddev->pers == NULL ||
3956 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003957 return -EINVAL;
3958 if (buf == e || (*e && *e != '\n'))
3959 return -EINVAL;
3960 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3961 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3962 mddev->suspend_hi = new;
3963 mddev->pers->quiesce(mddev, 1);
3964 mddev->pers->quiesce(mddev, 0);
3965 return len;
3966 } else
3967 return -EINVAL;
3968}
3969static struct md_sysfs_entry md_suspend_hi =
3970__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3971
NeilBrown08a02ec2007-05-09 02:35:38 -07003972static ssize_t
3973reshape_position_show(mddev_t *mddev, char *page)
3974{
3975 if (mddev->reshape_position != MaxSector)
3976 return sprintf(page, "%llu\n",
3977 (unsigned long long)mddev->reshape_position);
3978 strcpy(page, "none\n");
3979 return 5;
3980}
3981
3982static ssize_t
3983reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3984{
3985 char *e;
3986 unsigned long long new = simple_strtoull(buf, &e, 10);
3987 if (mddev->pers)
3988 return -EBUSY;
3989 if (buf == e || (*e && *e != '\n'))
3990 return -EINVAL;
3991 mddev->reshape_position = new;
3992 mddev->delta_disks = 0;
3993 mddev->new_level = mddev->level;
3994 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003995 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003996 return len;
3997}
3998
3999static struct md_sysfs_entry md_reshape_position =
4000__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4001 reshape_position_store);
4002
Dan Williamsb522adc2009-03-31 15:00:31 +11004003static ssize_t
4004array_size_show(mddev_t *mddev, char *page)
4005{
4006 if (mddev->external_size)
4007 return sprintf(page, "%llu\n",
4008 (unsigned long long)mddev->array_sectors/2);
4009 else
4010 return sprintf(page, "default\n");
4011}
4012
4013static ssize_t
4014array_size_store(mddev_t *mddev, const char *buf, size_t len)
4015{
4016 sector_t sectors;
4017
4018 if (strncmp(buf, "default", 7) == 0) {
4019 if (mddev->pers)
4020 sectors = mddev->pers->size(mddev, 0, 0);
4021 else
4022 sectors = mddev->array_sectors;
4023
4024 mddev->external_size = 0;
4025 } else {
4026 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4027 return -EINVAL;
4028 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004029 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004030
4031 mddev->external_size = 1;
4032 }
4033
4034 mddev->array_sectors = sectors;
4035 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004036 if (mddev->pers)
4037 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004038
4039 return len;
4040}
4041
4042static struct md_sysfs_entry md_array_size =
4043__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4044 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004045
NeilBrowneae17012005-11-08 21:39:23 -08004046static struct attribute *md_default_attrs[] = {
4047 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004048 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004049 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004050 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004051 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004052 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004053 &md_metadata.attr,
NeilBrown6d7ff7382006-01-06 00:21:16 -08004054 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004055 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004056 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004057 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004058 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004059 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004060 NULL,
4061};
4062
4063static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004064 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004065 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004066 &md_sync_min.attr,
4067 &md_sync_max.attr,
4068 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004069 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004070 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004071 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004072 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004073 &md_suspend_lo.attr,
4074 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004075 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004076 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004077 NULL,
4078};
NeilBrown411036f2005-11-08 21:39:40 -08004079static struct attribute_group md_redundancy_group = {
4080 .name = NULL,
4081 .attrs = md_redundancy_attrs,
4082};
4083
NeilBrowneae17012005-11-08 21:39:23 -08004084
4085static ssize_t
4086md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4087{
4088 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4089 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004090 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004091
4092 if (!entry->show)
4093 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004094 rv = mddev_lock(mddev);
4095 if (!rv) {
4096 rv = entry->show(mddev, page);
4097 mddev_unlock(mddev);
4098 }
NeilBrown96de1e62005-11-08 21:39:39 -08004099 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004100}
4101
4102static ssize_t
4103md_attr_store(struct kobject *kobj, struct attribute *attr,
4104 const char *page, size_t length)
4105{
4106 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4107 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004108 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004109
4110 if (!entry->store)
4111 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004112 if (!capable(CAP_SYS_ADMIN))
4113 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004114 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004115 if (mddev->hold_active == UNTIL_IOCTL)
4116 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004117 if (!rv) {
4118 rv = entry->store(mddev, page, length);
4119 mddev_unlock(mddev);
4120 }
NeilBrown96de1e62005-11-08 21:39:39 -08004121 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004122}
4123
4124static void md_free(struct kobject *ko)
4125{
4126 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004127
4128 if (mddev->sysfs_state)
4129 sysfs_put(mddev->sysfs_state);
4130
4131 if (mddev->gendisk) {
4132 del_gendisk(mddev->gendisk);
4133 put_disk(mddev->gendisk);
4134 }
4135 if (mddev->queue)
4136 blk_cleanup_queue(mddev->queue);
4137
NeilBrowneae17012005-11-08 21:39:23 -08004138 kfree(mddev);
4139}
4140
4141static struct sysfs_ops md_sysfs_ops = {
4142 .show = md_attr_show,
4143 .store = md_attr_store,
4144};
4145static struct kobj_type md_ktype = {
4146 .release = md_free,
4147 .sysfs_ops = &md_sysfs_ops,
4148 .default_attrs = md_default_attrs,
4149};
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151int mdp_major = 0;
4152
Dan Williams5fd3a172009-03-04 00:57:25 -07004153static void mddev_delayed_delete(struct work_struct *ws)
4154{
4155 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4156
NeilBrown43a70502009-12-14 12:49:55 +11004157 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004158 kobject_del(&mddev->kobj);
4159 kobject_put(&mddev->kobj);
4160}
4161
NeilBrownefeb53c2009-01-09 08:31:10 +11004162static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004164 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 mddev_t *mddev = mddev_find(dev);
4166 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004167 int partitioned;
4168 int shift;
4169 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004170 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004173 return -ENODEV;
4174
4175 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4176 shift = partitioned ? MdpMinorShift : 0;
4177 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
NeilBrownd3374822009-01-09 08:31:10 +11004179 /* wait for any previous instance if this device
4180 * to be completed removed (mddev_delayed_delete).
4181 */
4182 flush_scheduled_work();
4183
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004184 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004185 error = -EEXIST;
4186 if (mddev->gendisk)
4187 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004188
4189 if (name) {
4190 /* Need to ensure that 'name' is not a duplicate.
4191 */
4192 mddev_t *mddev2;
4193 spin_lock(&all_mddevs_lock);
4194
4195 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4196 if (mddev2->gendisk &&
4197 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4198 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004199 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004200 }
4201 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
NeilBrown8b765392009-01-09 08:31:08 +11004203
NeilBrown0909dc42009-07-01 12:27:21 +10004204 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004205 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004206 if (!mddev->queue)
4207 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004208 mddev->queue->queuedata = mddev;
4209
NeilBrown8b765392009-01-09 08:31:08 +11004210 /* Can be unlocked because the queue is new: no concurrency */
4211 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4212
NeilBrown409c57f2009-03-31 14:39:39 +11004213 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 disk = alloc_disk(1 << shift);
4216 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004217 blk_cleanup_queue(mddev->queue);
4218 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004219 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004221 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004223 if (name)
4224 strcpy(disk->disk_name, name);
4225 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f462005-06-20 21:15:16 -07004227 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 disk->fops = &md_fops;
4230 disk->private_data = mddev;
4231 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004232 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004233 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004234 * remove it now.
4235 */
4236 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 add_disk(disk);
4238 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004239 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4240 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004241 if (error) {
4242 /* This isn't possible, but as kobject_init_and_add is marked
4243 * __must_check, we must do something with the result
4244 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004245 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4246 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004247 error = 0;
4248 }
NeilBrown43a70502009-12-14 12:49:55 +11004249 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4250 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004251 abort:
4252 mutex_unlock(&disks_mutex);
4253 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004254 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004255 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4256 }
NeilBrownd3374822009-01-09 08:31:10 +11004257 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004258 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004259}
4260
4261static struct kobject *md_probe(dev_t dev, int *part, void *data)
4262{
4263 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 return NULL;
4265}
4266
NeilBrownefeb53c2009-01-09 08:31:10 +11004267static int add_named_array(const char *val, struct kernel_param *kp)
4268{
4269 /* val must be "md_*" where * is not all digits.
4270 * We allocate an array with a large free minor number, and
4271 * set the name to val. val must not already be an active name.
4272 */
4273 int len = strlen(val);
4274 char buf[DISK_NAME_LEN];
4275
4276 while (len && val[len-1] == '\n')
4277 len--;
4278 if (len >= DISK_NAME_LEN)
4279 return -E2BIG;
4280 strlcpy(buf, val, len+1);
4281 if (strncmp(buf, "md_", 3) != 0)
4282 return -EINVAL;
4283 return md_alloc(0, buf);
4284}
4285
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286static void md_safemode_timeout(unsigned long data)
4287{
4288 mddev_t *mddev = (mddev_t *) data;
4289
Neil Brown0fd62b82008-06-28 08:31:36 +10004290 if (!atomic_read(&mddev->writes_pending)) {
4291 mddev->safemode = 1;
4292 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004293 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 md_wakeup_thread(mddev->thread);
4296}
4297
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004298static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
4300static int do_md_run(mddev_t * mddev)
4301{
NeilBrown2604b702006-01-06 00:20:36 -08004302 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 mdk_rdev_t *rdev;
4304 struct gendisk *disk;
NeilBrown2604b702006-01-06 00:20:36 -08004305 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
NeilBrowna757e642005-04-16 15:26:42 -07004307 if (list_empty(&mddev->disks))
4308 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 if (mddev->pers)
4312 return -EBUSY;
4313
NeilBrownb6eb1272010-04-15 10:13:47 +10004314 /* These two calls synchronise us with the
4315 * sysfs_remove_group calls in mddev_unlock,
4316 * so they must have completed.
4317 */
4318 mutex_lock(&mddev->open_mutex);
4319 mutex_unlock(&mddev->open_mutex);
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 /*
4322 * Analyze all RAID superblock(s)
4323 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004324 if (!mddev->raid_disks) {
4325 if (!mddev->persistent)
4326 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004327 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
NeilBrownd9d166c2006-01-06 00:20:51 -08004330 if (mddev->level != LEVEL_NONE)
4331 request_module("md-level-%d", mddev->level);
4332 else if (mddev->clevel[0])
4333 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 /*
4336 * Drop all container device buffers, from now on
4337 * the only valid external interface is through the md
4338 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004340 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004341 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 continue;
4343 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004344 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004345
4346 /* perform some consistency tests on the device.
4347 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004348 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004349 */
Andre Noll0f420352008-07-11 22:02:23 +10004350 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004351 if (mddev->dev_sectors &&
4352 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004353 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004354 printk("md: %s: data overlaps metadata\n",
4355 mdname(mddev));
4356 return -EINVAL;
4357 }
4358 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004359 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004360 > rdev->data_offset) {
4361 printk("md: %s: metadata overlaps data\n",
4362 mdname(mddev));
4363 return -EINVAL;
4364 }
4365 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004366 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 }
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
4371 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004372 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004373 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004375 if (mddev->level != LEVEL_NONE)
4376 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4377 mddev->level);
4378 else
4379 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4380 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 return -EINVAL;
4382 }
NeilBrown2604b702006-01-06 00:20:36 -08004383 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004385 if (mddev->level != pers->level) {
4386 mddev->level = pers->level;
4387 mddev->new_level = pers->level;
4388 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004389 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390
NeilBrownf6705572006-03-27 01:18:11 -08004391 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004392 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004393 /* This personality cannot handle reshaping... */
4394 mddev->pers = NULL;
4395 module_put(pers->owner);
4396 return -EINVAL;
4397 }
4398
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004399 if (pers->sync_request) {
4400 /* Warn if this is a potentially silly
4401 * configuration.
4402 */
4403 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4404 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004405 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004406
4407 list_for_each_entry(rdev, &mddev->disks, same_set)
4408 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004409 if (rdev < rdev2 &&
4410 rdev->bdev->bd_contains ==
4411 rdev2->bdev->bd_contains) {
4412 printk(KERN_WARNING
4413 "%s: WARNING: %s appears to be"
4414 " on the same physical disk as"
4415 " %s.\n",
4416 mdname(mddev),
4417 bdevname(rdev->bdev,b),
4418 bdevname(rdev2->bdev,b2));
4419 warned = 1;
4420 }
4421 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004422
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004423 if (warned)
4424 printk(KERN_WARNING
4425 "True protection against single-disk"
4426 " failure might be compromised.\n");
4427 }
4428
NeilBrown657390d2005-08-26 18:34:16 -07004429 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004430 /* may be over-ridden by personality */
4431 mddev->resync_max_sectors = mddev->dev_sectors;
4432
NeilBrowna9701a32005-11-08 21:39:34 -08004433 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004434 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
NeilBrown0f9552b52009-12-30 12:08:50 +11004436 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004437 mddev->ro = 2; /* read-only, but switch on first write */
4438
NeilBrownb15c2e52006-01-06 00:20:16 -08004439 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004440 if (err)
4441 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004442 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4443 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4444 " but 'external_size' not in effect?\n", __func__);
4445 printk(KERN_ERR
4446 "md: invalid array_size %llu > default size %llu\n",
4447 (unsigned long long)mddev->array_sectors / 2,
4448 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4449 err = -EINVAL;
4450 mddev->pers->stop(mddev);
4451 }
4452 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004453 err = bitmap_create(mddev);
4454 if (err) {
4455 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4456 mdname(mddev), err);
4457 mddev->pers->stop(mddev);
4458 }
4459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 module_put(mddev->pers->owner);
4462 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004463 bitmap_destroy(mddev);
4464 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004466 if (mddev->pers->sync_request) {
4467 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4468 printk(KERN_WARNING
4469 "md: cannot register extra attributes for %s\n",
4470 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004471 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004472 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004473 mddev->ro = 0;
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004476 atomic_set(&mddev->max_corr_read_errors,
4477 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 mddev->safemode = 0;
4479 mddev->safemode_timer.function = md_safemode_timeout;
4480 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004481 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004483
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004484 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004485 if (rdev->raid_disk >= 0) {
4486 char nm[20];
4487 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004488 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4489 printk("md: cannot register %s for %s\n",
4490 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
4493 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4494
NeilBrown850b2b422006-10-03 01:15:46 -07004495 if (mddev->flags)
4496 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Andre Nollf233ea52008-07-21 17:05:22 +10004498 set_capacity(disk, mddev->array_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
NeilBrown0b8c9de2006-07-10 04:44:15 -07004500 md_wakeup_thread(mddev->thread);
4501 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004502
NeilBrown449aad32009-08-03 10:59:58 +10004503 revalidate_disk(mddev->gendisk);
NeilBrownd7603b72006-01-06 00:20:30 -08004504 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004505 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004506 if (mddev->sysfs_action)
4507 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004508 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004509 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 return 0;
4511}
4512
4513static int restart_array(mddev_t *mddev)
4514{
4515 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Andre Noll80fab1d2008-07-11 22:02:21 +10004517 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004519 return -ENXIO;
4520 if (!mddev->pers)
4521 return -EINVAL;
4522 if (!mddev->ro)
4523 return -EBUSY;
4524 mddev->safemode = 0;
4525 mddev->ro = 0;
4526 set_disk_ro(disk, 0);
4527 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4528 mdname(mddev));
4529 /* Kick recovery or resync if necessary */
4530 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4531 md_wakeup_thread(mddev->thread);
4532 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004533 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535}
4536
NeilBrownacc55e22006-06-26 00:27:47 -07004537/* similar to deny_write_access, but accounts for our holding a reference
4538 * to the file ourselves */
4539static int deny_bitmap_write_access(struct file * file)
4540{
4541 struct inode *inode = file->f_mapping->host;
4542
4543 spin_lock(&inode->i_lock);
4544 if (atomic_read(&inode->i_writecount) > 1) {
4545 spin_unlock(&inode->i_lock);
4546 return -ETXTBSY;
4547 }
4548 atomic_set(&inode->i_writecount, -1);
4549 spin_unlock(&inode->i_lock);
4550
4551 return 0;
4552}
4553
NeilBrown43a70502009-12-14 12:49:55 +11004554void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004555{
4556 struct inode *inode = file->f_mapping->host;
4557
4558 spin_lock(&inode->i_lock);
4559 atomic_set(&inode->i_writecount, 1);
4560 spin_unlock(&inode->i_lock);
4561}
4562
NeilBrown9e653b62006-06-26 00:27:58 -07004563/* mode:
4564 * 0 - completely stop and dis-assemble array
4565 * 1 - switch to readonly
4566 * 2 - stop but do not disassemble array
4567 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004568static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
4570 int err = 0;
4571 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004572 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
NeilBrownc8c00a62009-08-10 12:50:52 +10004574 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004575 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004576 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004577 err = -EBUSY;
4578 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
4580 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004581 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4583 md_unregister_thread(mddev->sync_thread);
4584 mddev->sync_thread = NULL;
4585 }
4586
4587 del_timer_sync(&mddev->safemode_timer);
4588
NeilBrown9e653b62006-06-26 00:27:58 -07004589 switch(mode) {
4590 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004592 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 goto out;
4594 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004595 break;
4596 case 0: /* disassemble */
4597 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004598 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004599 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 if (mddev->ro)
4601 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004604 mddev->queue->merge_bvec_fn = NULL;
4605 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004606 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 module_put(mddev->pers->owner);
NeilBrowna64c8762010-04-14 17:15:37 +10004608 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4609 mddev->to_remove = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004611 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004612 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004613
NeilBrownc4647292009-05-07 12:51:06 +10004614 list_for_each_entry(rdev, &mddev->disks, same_set)
4615 if (rdev->raid_disk >= 0) {
4616 char nm[20];
4617 sprintf(nm, "rd%d", rdev->raid_disk);
4618 sysfs_remove_link(&mddev->kobj, nm);
4619 }
4620
NeilBrown0d4ca602006-12-10 02:20:44 -08004621 set_capacity(disk, 0);
NeilBrownb821eaa2010-03-29 11:18:15 +11004622 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 if (mddev->ro)
4625 mddev->ro = 0;
4626 }
NeilBrown850b2b422006-10-03 01:15:46 -07004627 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 /* mark array as shutdown cleanly */
4629 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07004630 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 }
NeilBrown9e653b62006-06-26 00:27:58 -07004632 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004634 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004635 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004637out:
4638 mutex_unlock(&mddev->open_mutex);
4639 if (err)
4640 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 /*
4642 * Free resources if final stop
4643 */
NeilBrown9e653b62006-06-26 00:27:58 -07004644 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4647
NeilBrown978f9462006-02-02 14:28:05 -08004648 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004649 if (mddev->bitmap_info.file) {
4650 restore_bitmap_write_access(mddev->bitmap_info.file);
4651 fput(mddev->bitmap_info.file);
4652 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004653 }
NeilBrownc3d97142009-12-14 12:49:52 +11004654 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 export_array(mddev);
4657
Andre Nollf233ea52008-07-21 17:05:22 +10004658 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004659 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004660 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004661 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004662 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004663 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004664 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004665 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004666 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004667 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004668 mddev->level = LEVEL_NONE;
4669 mddev->clevel[0] = 0;
4670 mddev->flags = 0;
4671 mddev->ro = 0;
4672 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004673 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004674 mddev->ctime = mddev->utime = 0;
4675 mddev->layout = 0;
4676 mddev->max_disks = 0;
4677 mddev->events = 0;
4678 mddev->delta_disks = 0;
4679 mddev->new_level = LEVEL_NONE;
4680 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004681 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004682 mddev->curr_resync = 0;
4683 mddev->resync_mismatches = 0;
4684 mddev->suspend_lo = mddev->suspend_hi = 0;
4685 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4686 mddev->recovery = 0;
4687 mddev->in_sync = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004688 mddev->degraded = 0;
4689 mddev->barriers_work = 0;
4690 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004691 mddev->bitmap_info.offset = 0;
4692 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004693 mddev->bitmap_info.chunksize = 0;
4694 mddev->bitmap_info.daemon_sleep = 0;
4695 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004696 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004697 if (mddev->hold_active == UNTIL_STOP)
4698 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004699
NeilBrowna8a55c32006-06-26 00:27:39 -07004700 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4702 mdname(mddev));
4703 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004704 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004705 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004706 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 return err;
4708}
4709
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004710#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711static void autorun_array(mddev_t *mddev)
4712{
4713 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 int err;
4715
NeilBrowna757e642005-04-16 15:26:42 -07004716 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718
4719 printk(KERN_INFO "md: running: ");
4720
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004721 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 char b[BDEVNAME_SIZE];
4723 printk("<%s>", bdevname(rdev->bdev,b));
4724 }
4725 printk("\n");
4726
NeilBrownd710e132008-10-13 11:55:12 +11004727 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 if (err) {
4729 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004730 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 }
4732}
4733
4734/*
4735 * lets try to run arrays based on all disks that have arrived
4736 * until now. (those are in pending_raid_disks)
4737 *
4738 * the method: pick the first pending disk, collect all disks with
4739 * the same UUID, remove all from the pending list and put them into
4740 * the 'same_array' list. Then order this list based on superblock
4741 * update time (freshest comes first), kick out 'old' disks and
4742 * compare superblocks. If everything's fine then run it.
4743 *
4744 * If "unit" is allocated, then bump its reference count
4745 */
4746static void autorun_devices(int part)
4747{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004748 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 mddev_t *mddev;
4750 char b[BDEVNAME_SIZE];
4751
4752 printk(KERN_INFO "md: autorun ...\n");
4753 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004754 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004756 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 rdev0 = list_entry(pending_raid_disks.next,
4758 mdk_rdev_t, same_set);
4759
4760 printk(KERN_INFO "md: considering %s ...\n",
4761 bdevname(rdev0->bdev,b));
4762 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004763 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 if (super_90_load(rdev, rdev0, 0) >= 0) {
4765 printk(KERN_INFO "md: adding %s ...\n",
4766 bdevname(rdev->bdev,b));
4767 list_move(&rdev->same_set, &candidates);
4768 }
4769 /*
4770 * now we have a set of devices, with all of them having
4771 * mostly sane superblocks. It's time to allocate the
4772 * mddev.
4773 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004774 if (part) {
4775 dev = MKDEV(mdp_major,
4776 rdev0->preferred_minor << MdpMinorShift);
4777 unit = MINOR(dev) >> MdpMinorShift;
4778 } else {
4779 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4780 unit = MINOR(dev);
4781 }
4782 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4784 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4785 break;
4786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
4788 md_probe(dev, NULL, NULL);
4789 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004790 if (!mddev || !mddev->gendisk) {
4791 if (mddev)
4792 mddev_put(mddev);
4793 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 "md: cannot allocate memory for md drive.\n");
4795 break;
4796 }
4797 if (mddev_lock(mddev))
4798 printk(KERN_WARNING "md: %s locked, cannot run\n",
4799 mdname(mddev));
4800 else if (mddev->raid_disks || mddev->major_version
4801 || !list_empty(&mddev->disks)) {
4802 printk(KERN_WARNING
4803 "md: %s already running, cannot run %s\n",
4804 mdname(mddev), bdevname(rdev0->bdev,b));
4805 mddev_unlock(mddev);
4806 } else {
4807 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004808 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004809 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810 list_del_init(&rdev->same_set);
4811 if (bind_rdev_to_array(rdev, mddev))
4812 export_rdev(rdev);
4813 }
4814 autorun_array(mddev);
4815 mddev_unlock(mddev);
4816 }
4817 /* on success, candidates will be empty, on error
4818 * it won't...
4819 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004820 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004821 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 mddev_put(mddev);
4825 }
4826 printk(KERN_INFO "md: ... autorun DONE.\n");
4827}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004828#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830static int get_version(void __user * arg)
4831{
4832 mdu_version_t ver;
4833
4834 ver.major = MD_MAJOR_VERSION;
4835 ver.minor = MD_MINOR_VERSION;
4836 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4837
4838 if (copy_to_user(arg, &ver, sizeof(ver)))
4839 return -EFAULT;
4840
4841 return 0;
4842}
4843
4844static int get_array_info(mddev_t * mddev, void __user * arg)
4845{
4846 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004847 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849
NeilBrowna9f326e2009-09-23 18:06:41 +10004850 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004851 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004853 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 failed++;
4855 else {
4856 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004857 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004858 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859 else
4860 spare++;
4861 }
4862 }
4863
4864 info.major_version = mddev->major_version;
4865 info.minor_version = mddev->minor_version;
4866 info.patch_version = MD_PATCHLEVEL_VERSION;
4867 info.ctime = mddev->ctime;
4868 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004869 info.size = mddev->dev_sectors / 2;
4870 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004871 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 info.nr_disks = nr;
4873 info.raid_disks = mddev->raid_disks;
4874 info.md_minor = mddev->md_minor;
4875 info.not_persistent= !mddev->persistent;
4876
4877 info.utime = mddev->utime;
4878 info.state = 0;
4879 if (mddev->in_sync)
4880 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004881 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004882 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004883 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884 info.working_disks = working;
4885 info.failed_disks = failed;
4886 info.spare_disks = spare;
4887
4888 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004889 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
4891 if (copy_to_user(arg, &info, sizeof(info)))
4892 return -EFAULT;
4893
4894 return 0;
4895}
4896
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004897static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004898{
4899 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4900 char *ptr, *buf = NULL;
4901 int err = -ENOMEM;
4902
Dan Williamsb5470dc2008-06-27 21:44:04 -07004903 if (md_allow_write(mddev))
4904 file = kmalloc(sizeof(*file), GFP_NOIO);
4905 else
4906 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004907
NeilBrown32a76272005-06-21 17:17:14 -07004908 if (!file)
4909 goto out;
4910
4911 /* bitmap disabled, zero the first byte and copy out */
4912 if (!mddev->bitmap || !mddev->bitmap->file) {
4913 file->pathname[0] = '\0';
4914 goto copy_out;
4915 }
4916
4917 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4918 if (!buf)
4919 goto out;
4920
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004921 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4922 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004923 goto out;
4924
4925 strcpy(file->pathname, ptr);
4926
4927copy_out:
4928 err = 0;
4929 if (copy_to_user(arg, file, sizeof(*file)))
4930 err = -EFAULT;
4931out:
4932 kfree(buf);
4933 kfree(file);
4934 return err;
4935}
4936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937static int get_disk_info(mddev_t * mddev, void __user * arg)
4938{
4939 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 mdk_rdev_t *rdev;
4941
4942 if (copy_from_user(&info, arg, sizeof(info)))
4943 return -EFAULT;
4944
Andre Noll26ef3792008-07-11 22:02:21 +10004945 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946 if (rdev) {
4947 info.major = MAJOR(rdev->bdev->bd_dev);
4948 info.minor = MINOR(rdev->bdev->bd_dev);
4949 info.raid_disk = rdev->raid_disk;
4950 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004951 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004953 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 info.state |= (1<<MD_DISK_ACTIVE);
4955 info.state |= (1<<MD_DISK_SYNC);
4956 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004957 if (test_bit(WriteMostly, &rdev->flags))
4958 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959 } else {
4960 info.major = info.minor = 0;
4961 info.raid_disk = -1;
4962 info.state = (1<<MD_DISK_REMOVED);
4963 }
4964
4965 if (copy_to_user(arg, &info, sizeof(info)))
4966 return -EFAULT;
4967
4968 return 0;
4969}
4970
4971static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4972{
4973 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4974 mdk_rdev_t *rdev;
4975 dev_t dev = MKDEV(info->major,info->minor);
4976
4977 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4978 return -EOVERFLOW;
4979
4980 if (!mddev->raid_disks) {
4981 int err;
4982 /* expecting a device which has a superblock */
4983 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4984 if (IS_ERR(rdev)) {
4985 printk(KERN_WARNING
4986 "md: md_import_device returned %ld\n",
4987 PTR_ERR(rdev));
4988 return PTR_ERR(rdev);
4989 }
4990 if (!list_empty(&mddev->disks)) {
4991 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4992 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004993 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 .load_super(rdev, rdev0, mddev->minor_version);
4995 if (err < 0) {
4996 printk(KERN_WARNING
4997 "md: %s has different UUID to %s\n",
4998 bdevname(rdev->bdev,b),
4999 bdevname(rdev0->bdev,b2));
5000 export_rdev(rdev);
5001 return -EINVAL;
5002 }
5003 }
5004 err = bind_rdev_to_array(rdev, mddev);
5005 if (err)
5006 export_rdev(rdev);
5007 return err;
5008 }
5009
5010 /*
5011 * add_new_disk can be used once the array is assembled
5012 * to add "hot spares". They must already have a superblock
5013 * written
5014 */
5015 if (mddev->pers) {
5016 int err;
5017 if (!mddev->pers->hot_add_disk) {
5018 printk(KERN_WARNING
5019 "%s: personality does not support diskops!\n",
5020 mdname(mddev));
5021 return -EINVAL;
5022 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005023 if (mddev->persistent)
5024 rdev = md_import_device(dev, mddev->major_version,
5025 mddev->minor_version);
5026 else
5027 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 if (IS_ERR(rdev)) {
5029 printk(KERN_WARNING
5030 "md: md_import_device returned %ld\n",
5031 PTR_ERR(rdev));
5032 return PTR_ERR(rdev);
5033 }
NeilBrown41158c72005-06-21 17:17:25 -07005034 /* set save_raid_disk if appropriate */
5035 if (!mddev->persistent) {
5036 if (info->state & (1<<MD_DISK_SYNC) &&
5037 info->raid_disk < mddev->raid_disks)
5038 rdev->raid_disk = info->raid_disk;
5039 else
5040 rdev->raid_disk = -1;
5041 } else
5042 super_types[mddev->major_version].
5043 validate_super(mddev, rdev);
5044 rdev->saved_raid_disk = rdev->raid_disk;
5045
NeilBrownb2d444d2005-11-08 21:39:31 -08005046 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005047 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5048 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005049 else
5050 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005051
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 rdev->raid_disk = -1;
5053 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005054 if (!err && !mddev->pers->hot_remove_disk) {
5055 /* If there is hot_add_disk but no hot_remove_disk
5056 * then added disks for geometry changes,
5057 * and should be added immediately.
5058 */
5059 super_types[mddev->major_version].
5060 validate_super(mddev, rdev);
5061 err = mddev->pers->hot_add_disk(mddev, rdev);
5062 if (err)
5063 unbind_rdev_from_array(rdev);
5064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 if (err)
5066 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005067 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005068 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005069
NeilBrown17571282006-12-10 02:20:52 -08005070 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005071 if (mddev->degraded)
5072 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005073 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005074 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 return err;
5076 }
5077
5078 /* otherwise, add_new_disk is only allowed
5079 * for major_version==0 superblocks
5080 */
5081 if (mddev->major_version != 0) {
5082 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5083 mdname(mddev));
5084 return -EINVAL;
5085 }
5086
5087 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5088 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005089 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 if (IS_ERR(rdev)) {
5091 printk(KERN_WARNING
5092 "md: error, md_import_device() returned %ld\n",
5093 PTR_ERR(rdev));
5094 return PTR_ERR(rdev);
5095 }
5096 rdev->desc_nr = info->number;
5097 if (info->raid_disk < mddev->raid_disks)
5098 rdev->raid_disk = info->raid_disk;
5099 else
5100 rdev->raid_disk = -1;
5101
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005103 if (info->state & (1<<MD_DISK_SYNC))
5104 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005106 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5107 set_bit(WriteMostly, &rdev->flags);
5108
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 if (!mddev->persistent) {
5110 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005111 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 } else
Andre Noll0f420352008-07-11 22:02:23 +10005113 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005114 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115
NeilBrown2bf071b2006-01-06 00:20:55 -08005116 err = bind_rdev_to_array(rdev, mddev);
5117 if (err) {
5118 export_rdev(rdev);
5119 return err;
5120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 }
5122
5123 return 0;
5124}
5125
5126static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5127{
5128 char b[BDEVNAME_SIZE];
5129 mdk_rdev_t *rdev;
5130
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 rdev = find_rdev(mddev, dev);
5132 if (!rdev)
5133 return -ENXIO;
5134
5135 if (rdev->raid_disk >= 0)
5136 goto busy;
5137
5138 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005139 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005140 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141
5142 return 0;
5143busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005144 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005145 bdevname(rdev->bdev,b), mdname(mddev));
5146 return -EBUSY;
5147}
5148
5149static int hot_add_disk(mddev_t * mddev, dev_t dev)
5150{
5151 char b[BDEVNAME_SIZE];
5152 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 mdk_rdev_t *rdev;
5154
5155 if (!mddev->pers)
5156 return -ENODEV;
5157
5158 if (mddev->major_version != 0) {
5159 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5160 " version-0 superblocks.\n",
5161 mdname(mddev));
5162 return -EINVAL;
5163 }
5164 if (!mddev->pers->hot_add_disk) {
5165 printk(KERN_WARNING
5166 "%s: personality does not support diskops!\n",
5167 mdname(mddev));
5168 return -EINVAL;
5169 }
5170
NeilBrownd710e132008-10-13 11:55:12 +11005171 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 if (IS_ERR(rdev)) {
5173 printk(KERN_WARNING
5174 "md: error, md_import_device() returned %ld\n",
5175 PTR_ERR(rdev));
5176 return -EINVAL;
5177 }
5178
5179 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005180 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 else
Andre Noll0f420352008-07-11 22:02:23 +10005182 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183
NeilBrown8190e752009-06-18 08:48:58 +10005184 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
NeilBrownb2d444d2005-11-08 21:39:31 -08005186 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187 printk(KERN_WARNING
5188 "md: can not hot-add faulty %s disk to %s!\n",
5189 bdevname(rdev->bdev,b), mdname(mddev));
5190 err = -EINVAL;
5191 goto abort_export;
5192 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005193 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005195 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005196 err = bind_rdev_to_array(rdev, mddev);
5197 if (err)
5198 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
5200 /*
5201 * The rest should better be atomic, we can have disk failures
5202 * noticed in interrupt contexts ...
5203 */
5204
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 rdev->raid_disk = -1;
5206
NeilBrown850b2b422006-10-03 01:15:46 -07005207 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208
5209 /*
5210 * Kick recovery, maybe this spare has to be added to the
5211 * array immediately.
5212 */
5213 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5214 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005215 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216 return 0;
5217
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218abort_export:
5219 export_rdev(rdev);
5220 return err;
5221}
5222
NeilBrown32a76272005-06-21 17:17:14 -07005223static int set_bitmap_file(mddev_t *mddev, int fd)
5224{
5225 int err;
5226
NeilBrown36fa3062005-09-09 16:23:45 -07005227 if (mddev->pers) {
5228 if (!mddev->pers->quiesce)
5229 return -EBUSY;
5230 if (mddev->recovery || mddev->sync_thread)
5231 return -EBUSY;
5232 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005233 }
5234
NeilBrown36fa3062005-09-09 16:23:45 -07005235
5236 if (fd >= 0) {
5237 if (mddev->bitmap)
5238 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005239 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005240
NeilBrownc3d97142009-12-14 12:49:52 +11005241 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005242 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5243 mdname(mddev));
5244 return -EBADF;
5245 }
5246
NeilBrownc3d97142009-12-14 12:49:52 +11005247 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005248 if (err) {
5249 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5250 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005251 fput(mddev->bitmap_info.file);
5252 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005253 return err;
5254 }
NeilBrownc3d97142009-12-14 12:49:52 +11005255 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005256 } else if (mddev->bitmap == NULL)
5257 return -ENOENT; /* cannot remove what isn't there */
5258 err = 0;
5259 if (mddev->pers) {
5260 mddev->pers->quiesce(mddev, 1);
5261 if (fd >= 0)
5262 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005263 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005264 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005265 fd = -1; /* make sure to put the file */
5266 }
NeilBrown36fa3062005-09-09 16:23:45 -07005267 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005268 }
5269 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005270 if (mddev->bitmap_info.file) {
5271 restore_bitmap_write_access(mddev->bitmap_info.file);
5272 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005273 }
NeilBrownc3d97142009-12-14 12:49:52 +11005274 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005275 }
5276
NeilBrown32a76272005-06-21 17:17:14 -07005277 return err;
5278}
5279
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280/*
5281 * set_array_info is used two different ways
5282 * The original usage is when creating a new array.
5283 * In this usage, raid_disks is > 0 and it together with
5284 * level, size, not_persistent,layout,chunksize determine the
5285 * shape of the array.
5286 * This will always create an array with a type-0.90.0 superblock.
5287 * The newer usage is when assembling an array.
5288 * In this case raid_disks will be 0, and the major_version field is
5289 * use to determine which style super-blocks are to be found on the devices.
5290 * The minor and patch _version numbers are also kept incase the
5291 * super_block handler wishes to interpret them.
5292 */
5293static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5294{
5295
5296 if (info->raid_disks == 0) {
5297 /* just setting version number for superblock loading */
5298 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005299 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 super_types[info->major_version].name == NULL) {
5301 /* maybe try to auto-load a module? */
5302 printk(KERN_INFO
5303 "md: superblock version %d not known\n",
5304 info->major_version);
5305 return -EINVAL;
5306 }
5307 mddev->major_version = info->major_version;
5308 mddev->minor_version = info->minor_version;
5309 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005310 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005311 /* ensure mddev_put doesn't delete this now that there
5312 * is some minimal configuration.
5313 */
5314 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 return 0;
5316 }
5317 mddev->major_version = MD_MAJOR_VERSION;
5318 mddev->minor_version = MD_MINOR_VERSION;
5319 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5320 mddev->ctime = get_seconds();
5321
5322 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005323 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005324 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 mddev->raid_disks = info->raid_disks;
5326 /* don't set md_minor, it is determined by which /dev/md* was
5327 * openned
5328 */
5329 if (info->state & (1<<MD_SB_CLEAN))
5330 mddev->recovery_cp = MaxSector;
5331 else
5332 mddev->recovery_cp = 0;
5333 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005334 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335
5336 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005337 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338
5339 mddev->max_disks = MD_SB_DISKS;
5340
NeilBrowne6910632008-02-06 01:39:51 -08005341 if (mddev->persistent)
5342 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005343 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
NeilBrownc3d97142009-12-14 12:49:52 +11005345 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5346 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005347
NeilBrownf6705572006-03-27 01:18:11 -08005348 mddev->reshape_position = MaxSector;
5349
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 /*
5351 * Generate a 128 bit UUID
5352 */
5353 get_random_bytes(mddev->uuid, 16);
5354
NeilBrownf6705572006-03-27 01:18:11 -08005355 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005356 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005357 mddev->new_layout = mddev->layout;
5358 mddev->delta_disks = 0;
5359
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 return 0;
5361}
5362
Dan Williams1f403622009-03-31 14:59:03 +11005363void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5364{
Dan Williamsb522adc2009-03-31 15:00:31 +11005365 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5366
5367 if (mddev->external_size)
5368 return;
5369
Dan Williams1f403622009-03-31 14:59:03 +11005370 mddev->array_sectors = array_sectors;
5371}
5372EXPORT_SYMBOL(md_set_array_sectors);
5373
Andre Nolld71f9f82008-07-11 22:02:22 +10005374static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005375{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005376 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005377 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005378 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005379
5380 if (mddev->pers->resize == NULL)
5381 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005382 /* The "num_sectors" is the number of sectors of each device that
5383 * is used. This can only make sense for arrays with redundancy.
5384 * linear and raid0 always use whatever space is available. We can only
5385 * consider changing this number if no resync or reconstruction is
5386 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005387 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005388 * of each device. If num_sectors is zero, we find the largest size
5389 * that fits.
5390
NeilBrowna35b0d62006-01-06 00:20:49 -08005391 */
5392 if (mddev->sync_thread)
5393 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005394 if (mddev->bitmap)
5395 /* Sorry, cannot grow a bitmap yet, just remove it,
5396 * grow, and re-add.
5397 */
5398 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005399 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005400 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005401
Andre Nolld71f9f82008-07-11 22:02:22 +10005402 if (fit && (num_sectors == 0 || num_sectors > avail))
5403 num_sectors = avail;
5404 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005405 return -ENOSPC;
5406 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005407 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005408 if (!rv)
5409 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005410 return rv;
5411}
5412
NeilBrownda943b992006-01-06 00:20:54 -08005413static int update_raid_disks(mddev_t *mddev, int raid_disks)
5414{
5415 int rv;
5416 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005417 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005418 return -EINVAL;
5419 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005420 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005421 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005422 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005423 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005424 mddev->delta_disks = raid_disks - mddev->raid_disks;
5425
5426 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005427 return rv;
5428}
5429
5430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431/*
5432 * update_array_info is used to change the configuration of an
5433 * on-line array.
5434 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5435 * fields in the info are checked against the array.
5436 * Any differences that cannot be handled will cause an error.
5437 * Normally, only one change can be managed at a time.
5438 */
5439static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5440{
5441 int rv = 0;
5442 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005443 int state = 0;
5444
5445 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005446 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005447 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448
5449 if (mddev->major_version != info->major_version ||
5450 mddev->minor_version != info->minor_version ||
5451/* mddev->patch_version != info->patch_version || */
5452 mddev->ctime != info->ctime ||
5453 mddev->level != info->level ||
5454/* mddev->layout != info->layout || */
5455 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005456 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005457 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5458 ((state^info->state) & 0xfffffe00)
5459 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 return -EINVAL;
5461 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005462 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5463 cnt++;
5464 if (mddev->raid_disks != info->raid_disks)
5465 cnt++;
5466 if (mddev->layout != info->layout)
5467 cnt++;
5468 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5469 cnt++;
5470 if (cnt == 0)
5471 return 0;
5472 if (cnt > 1)
5473 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474
5475 if (mddev->layout != info->layout) {
5476 /* Change layout
5477 * we don't need to do anything at the md level, the
5478 * personality will take care of it all.
5479 */
NeilBrown50ac1682009-06-18 08:47:55 +10005480 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005482 else {
5483 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005484 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005485 if (rv)
5486 mddev->new_layout = mddev->layout;
5487 return rv;
5488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005490 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005491 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492
NeilBrownda943b992006-01-06 00:20:54 -08005493 if (mddev->raid_disks != info->raid_disks)
5494 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495
NeilBrown36fa3062005-09-09 16:23:45 -07005496 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5497 if (mddev->pers->quiesce == NULL)
5498 return -EINVAL;
5499 if (mddev->recovery || mddev->sync_thread)
5500 return -EBUSY;
5501 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5502 /* add the bitmap */
5503 if (mddev->bitmap)
5504 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005505 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005506 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005507 mddev->bitmap_info.offset =
5508 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005509 mddev->pers->quiesce(mddev, 1);
5510 rv = bitmap_create(mddev);
5511 if (rv)
5512 bitmap_destroy(mddev);
5513 mddev->pers->quiesce(mddev, 0);
5514 } else {
5515 /* remove the bitmap */
5516 if (!mddev->bitmap)
5517 return -ENOENT;
5518 if (mddev->bitmap->file)
5519 return -EINVAL;
5520 mddev->pers->quiesce(mddev, 1);
5521 bitmap_destroy(mddev);
5522 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005523 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005524 }
5525 }
NeilBrown850b2b422006-10-03 01:15:46 -07005526 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 return rv;
5528}
5529
5530static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5531{
5532 mdk_rdev_t *rdev;
5533
5534 if (mddev->pers == NULL)
5535 return -ENODEV;
5536
5537 rdev = find_rdev(mddev, dev);
5538 if (!rdev)
5539 return -ENODEV;
5540
5541 md_error(mddev, rdev);
5542 return 0;
5543}
5544
Andre Noll2f9618c2008-04-25 18:57:58 +02005545/*
5546 * We have a problem here : there is no easy way to give a CHS
5547 * virtual geometry. We currently pretend that we have a 2 heads
5548 * 4 sectors (with a BIG number of cylinders...). This drives
5549 * dosfs just mad... ;-)
5550 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005551static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5552{
5553 mddev_t *mddev = bdev->bd_disk->private_data;
5554
5555 geo->heads = 2;
5556 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005557 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005558 return 0;
5559}
5560
Al Viroa39907f2008-03-02 10:31:15 -05005561static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 unsigned int cmd, unsigned long arg)
5563{
5564 int err = 0;
5565 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005567 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568
5569 if (!capable(CAP_SYS_ADMIN))
5570 return -EACCES;
5571
5572 /*
5573 * Commands dealing with the RAID driver but not any
5574 * particular array:
5575 */
5576 switch (cmd)
5577 {
5578 case RAID_VERSION:
5579 err = get_version(argp);
5580 goto done;
5581
5582 case PRINT_RAID_DEBUG:
5583 err = 0;
5584 md_print_devices();
5585 goto done;
5586
5587#ifndef MODULE
5588 case RAID_AUTORUN:
5589 err = 0;
5590 autostart_arrays(arg);
5591 goto done;
5592#endif
5593 default:;
5594 }
5595
5596 /*
5597 * Commands creating/starting a new array:
5598 */
5599
Al Viroa39907f2008-03-02 10:31:15 -05005600 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
5602 if (!mddev) {
5603 BUG();
5604 goto abort;
5605 }
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 err = mddev_lock(mddev);
5608 if (err) {
5609 printk(KERN_INFO
5610 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5611 err, cmd);
5612 goto abort;
5613 }
5614
5615 switch (cmd)
5616 {
5617 case SET_ARRAY_INFO:
5618 {
5619 mdu_array_info_t info;
5620 if (!arg)
5621 memset(&info, 0, sizeof(info));
5622 else if (copy_from_user(&info, argp, sizeof(info))) {
5623 err = -EFAULT;
5624 goto abort_unlock;
5625 }
5626 if (mddev->pers) {
5627 err = update_array_info(mddev, &info);
5628 if (err) {
5629 printk(KERN_WARNING "md: couldn't update"
5630 " array info. %d\n", err);
5631 goto abort_unlock;
5632 }
5633 goto done_unlock;
5634 }
5635 if (!list_empty(&mddev->disks)) {
5636 printk(KERN_WARNING
5637 "md: array %s already has disks!\n",
5638 mdname(mddev));
5639 err = -EBUSY;
5640 goto abort_unlock;
5641 }
5642 if (mddev->raid_disks) {
5643 printk(KERN_WARNING
5644 "md: array %s already initialised!\n",
5645 mdname(mddev));
5646 err = -EBUSY;
5647 goto abort_unlock;
5648 }
5649 err = set_array_info(mddev, &info);
5650 if (err) {
5651 printk(KERN_WARNING "md: couldn't set"
5652 " array info. %d\n", err);
5653 goto abort_unlock;
5654 }
5655 }
5656 goto done_unlock;
5657
5658 default:;
5659 }
5660
5661 /*
5662 * Commands querying/configuring an existing array:
5663 */
NeilBrown32a76272005-06-21 17:17:14 -07005664 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005665 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005666 if ((!mddev->raid_disks && !mddev->external)
5667 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5668 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5669 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 err = -ENODEV;
5671 goto abort_unlock;
5672 }
5673
5674 /*
5675 * Commands even a read-only array can execute:
5676 */
5677 switch (cmd)
5678 {
5679 case GET_ARRAY_INFO:
5680 err = get_array_info(mddev, argp);
5681 goto done_unlock;
5682
NeilBrown32a76272005-06-21 17:17:14 -07005683 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005684 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005685 goto done_unlock;
5686
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 case GET_DISK_INFO:
5688 err = get_disk_info(mddev, argp);
5689 goto done_unlock;
5690
5691 case RESTART_ARRAY_RW:
5692 err = restart_array(mddev);
5693 goto done_unlock;
5694
5695 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005696 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 goto done_unlock;
5698
5699 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005700 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 goto done_unlock;
5702
Dan Williamse2218352010-05-12 08:25:37 +10005703 case BLKROSET:
5704 if (get_user(ro, (int __user *)(arg))) {
5705 err = -EFAULT;
5706 goto done_unlock;
5707 }
5708 err = -EINVAL;
5709
5710 /* if the bdev is going readonly the value of mddev->ro
5711 * does not matter, no writes are coming
5712 */
5713 if (ro)
5714 goto done_unlock;
5715
5716 /* are we are already prepared for writes? */
5717 if (mddev->ro != 1)
5718 goto done_unlock;
5719
5720 /* transitioning to readauto need only happen for
5721 * arrays that call md_write_start
5722 */
5723 if (mddev->pers) {
5724 err = restart_array(mddev);
5725 if (err == 0) {
5726 mddev->ro = 2;
5727 set_disk_ro(mddev->gendisk, 0);
5728 }
5729 }
5730 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 }
5732
5733 /*
5734 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005735 * superblock, so we do not allow them on read-only arrays.
5736 * However non-MD ioctls (e.g. get-size) will still come through
5737 * here and hit the 'default' below, so only disallow
5738 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005740 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005741 if (mddev->ro == 2) {
5742 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005743 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005744 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5745 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005746 } else {
5747 err = -EROFS;
5748 goto abort_unlock;
5749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 }
5751
5752 switch (cmd)
5753 {
5754 case ADD_NEW_DISK:
5755 {
5756 mdu_disk_info_t info;
5757 if (copy_from_user(&info, argp, sizeof(info)))
5758 err = -EFAULT;
5759 else
5760 err = add_new_disk(mddev, &info);
5761 goto done_unlock;
5762 }
5763
5764 case HOT_REMOVE_DISK:
5765 err = hot_remove_disk(mddev, new_decode_dev(arg));
5766 goto done_unlock;
5767
5768 case HOT_ADD_DISK:
5769 err = hot_add_disk(mddev, new_decode_dev(arg));
5770 goto done_unlock;
5771
5772 case SET_DISK_FAULTY:
5773 err = set_disk_faulty(mddev, new_decode_dev(arg));
5774 goto done_unlock;
5775
5776 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005777 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 goto done_unlock;
5779
NeilBrown32a76272005-06-21 17:17:14 -07005780 case SET_BITMAP_FILE:
5781 err = set_bitmap_file(mddev, (int)arg);
5782 goto done_unlock;
5783
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 err = -EINVAL;
5786 goto abort_unlock;
5787 }
5788
5789done_unlock:
5790abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005791 if (mddev->hold_active == UNTIL_IOCTL &&
5792 err != -EINVAL)
5793 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 mddev_unlock(mddev);
5795
5796 return err;
5797done:
5798 if (err)
5799 MD_BUG();
5800abort:
5801 return err;
5802}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005803#ifdef CONFIG_COMPAT
5804static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5805 unsigned int cmd, unsigned long arg)
5806{
5807 switch (cmd) {
5808 case HOT_REMOVE_DISK:
5809 case HOT_ADD_DISK:
5810 case SET_DISK_FAULTY:
5811 case SET_BITMAP_FILE:
5812 /* These take in integer arg, do not convert */
5813 break;
5814 default:
5815 arg = (unsigned long)compat_ptr(arg);
5816 break;
5817 }
5818
5819 return md_ioctl(bdev, mode, cmd, arg);
5820}
5821#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822
Al Viroa39907f2008-03-02 10:31:15 -05005823static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824{
5825 /*
5826 * Succeed if we can lock the mddev, which confirms that
5827 * it isn't being stopped right now.
5828 */
NeilBrownd3374822009-01-09 08:31:10 +11005829 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 int err;
5831
NeilBrownd3374822009-01-09 08:31:10 +11005832 if (mddev->gendisk != bdev->bd_disk) {
5833 /* we are racing with mddev_put which is discarding this
5834 * bd_disk.
5835 */
5836 mddev_put(mddev);
5837 /* Wait until bdev->bd_disk is definitely gone */
5838 flush_scheduled_work();
5839 /* Then retry the open from the top */
5840 return -ERESTARTSYS;
5841 }
5842 BUG_ON(mddev != bdev->bd_disk->private_data);
5843
NeilBrownc8c00a62009-08-10 12:50:52 +10005844 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 goto out;
5846
5847 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005848 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005849 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851 out:
5852 return err;
5853}
5854
Al Viroa39907f2008-03-02 10:31:15 -05005855static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856{
Al Viroa39907f2008-03-02 10:31:15 -05005857 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005859 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005860 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861 mddev_put(mddev);
5862
5863 return 0;
5864}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005865static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866{
5867 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005868 .open = md_open,
5869 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005870 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005871#ifdef CONFIG_COMPAT
5872 .compat_ioctl = md_compat_ioctl,
5873#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005874 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875};
5876
Adrian Bunk75c96f82005-05-05 16:16:09 -07005877static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878{
5879 mdk_thread_t *thread = arg;
5880
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 /*
5882 * md_thread is a 'system-thread', it's priority should be very
5883 * high. We avoid resource deadlocks individually in each
5884 * raid personality. (RAID5 does preallocation) We also use RR and
5885 * the very same RT priority as kswapd, thus we will never get
5886 * into a priority inversion deadlock.
5887 *
5888 * we definitely have to have equal or higher priority than
5889 * bdflush, otherwise bdflush will deadlock if there are too
5890 * many dirty RAID5 blocks.
5891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892
NeilBrown6985c432005-10-19 21:23:47 -07005893 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005894 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
NeilBrown93588e22005-11-15 00:09:12 -08005896 /* We need to wait INTERRUPTIBLE so that
5897 * we don't add to the load-average.
5898 * That means we need to be sure no signals are
5899 * pending
5900 */
5901 if (signal_pending(current))
5902 flush_signals(current);
5903
5904 wait_event_interruptible_timeout
5905 (thread->wqueue,
5906 test_bit(THREAD_WAKEUP, &thread->flags)
5907 || kthread_should_stop(),
5908 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909
5910 clear_bit(THREAD_WAKEUP, &thread->flags);
5911
NeilBrown787453c2005-11-08 21:39:43 -08005912 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005914
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 return 0;
5916}
5917
5918void md_wakeup_thread(mdk_thread_t *thread)
5919{
5920 if (thread) {
5921 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5922 set_bit(THREAD_WAKEUP, &thread->flags);
5923 wake_up(&thread->wqueue);
5924 }
5925}
5926
5927mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5928 const char *name)
5929{
5930 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
NeilBrown9ffae0c2006-01-06 00:20:32 -08005932 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 if (!thread)
5934 return NULL;
5935
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 init_waitqueue_head(&thread->wqueue);
5937
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 thread->run = run;
5939 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005940 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005941 thread->tsk = kthread_run(md_thread, thread,
5942 "%s_%s",
5943 mdname(thread->mddev),
5944 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005945 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 kfree(thread);
5947 return NULL;
5948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949 return thread;
5950}
5951
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952void md_unregister_thread(mdk_thread_t *thread)
5953{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005954 if (!thread)
5955 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005956 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005957
5958 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 kfree(thread);
5960}
5961
5962void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5963{
5964 if (!mddev) {
5965 MD_BUG();
5966 return;
5967 }
5968
NeilBrownb2d444d2005-11-08 21:39:31 -08005969 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005971
5972 if (mddev->external)
5973 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005974/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5976 mdname(mddev),
5977 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5978 __builtin_return_address(0),__builtin_return_address(1),
5979 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005980*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005981 if (!mddev->pers)
5982 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 if (!mddev->pers->error_handler)
5984 return;
5985 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005986 if (mddev->degraded)
5987 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11005988 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5990 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5991 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07005992 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993}
5994
5995/* seq_file implementation /proc/mdstat */
5996
5997static void status_unused(struct seq_file *seq)
5998{
5999 int i = 0;
6000 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001
6002 seq_printf(seq, "unused devices: ");
6003
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006004 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006005 char b[BDEVNAME_SIZE];
6006 i++;
6007 seq_printf(seq, "%s ",
6008 bdevname(rdev->bdev,b));
6009 }
6010 if (!i)
6011 seq_printf(seq, "<none>");
6012
6013 seq_printf(seq, "\n");
6014}
6015
6016
6017static void status_resync(struct seq_file *seq, mddev_t * mddev)
6018{
NeilBrowndd71cf62009-05-07 12:49:35 +10006019 sector_t max_sectors, resync, res;
6020 unsigned long dt, db;
6021 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006022 int scale;
6023 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024
NeilBrowndd71cf62009-05-07 12:49:35 +10006025 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026
6027 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006028 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006030 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
6032 /*
6033 * Should not happen.
6034 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006035 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006036 MD_BUG();
6037 return;
6038 }
NeilBrown4588b422006-03-27 01:18:04 -08006039 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006040 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006041 * u32, as those are the requirements for sector_div.
6042 * Thus 'scale' must be at least 10
6043 */
6044 scale = 10;
6045 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006046 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006047 scale++;
6048 }
6049 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006050 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006051
6052 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 {
NeilBrown4588b422006-03-27 01:18:04 -08006054 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 seq_printf(seq, "[");
6056 for (i = 0; i < x; i++)
6057 seq_printf(seq, "=");
6058 seq_printf(seq, ">");
6059 for (i = 0; i < y; i++)
6060 seq_printf(seq, ".");
6061 seq_printf(seq, "] ");
6062 }
NeilBrown4588b422006-03-27 01:18:04 -08006063 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006064 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6065 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006066 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6067 "check" :
6068 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6069 "resync" : "recovery"))),
6070 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006071 (unsigned long long) resync/2,
6072 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
6074 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 * dt: time from mark until now
6076 * db: blocks written from mark until now
6077 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006078 *
6079 * rt is a sector_t, so could be 32bit or 64bit.
6080 * So we divide before multiply in case it is 32bit and close
6081 * to the limit.
6082 * We scale the divisor (db) by 32 to avoid loosing precision
6083 * near the end of resync when the number of remaining sectors
6084 * is close to 'db'.
6085 * We then divide rt by 32 after multiplying by db to compensate.
6086 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 */
6088 dt = ((jiffies - mddev->resync_mark) / HZ);
6089 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006090 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6091 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092
NeilBrowndd71cf62009-05-07 12:49:35 +10006093 rt = max_sectors - resync; /* number of remaining sectors */
6094 sector_div(rt, db/32+1);
6095 rt *= dt;
6096 rt >>= 5;
6097
6098 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6099 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
NeilBrownff4e8d92006-07-10 04:44:16 -07006101 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006102}
6103
6104static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6105{
6106 struct list_head *tmp;
6107 loff_t l = *pos;
6108 mddev_t *mddev;
6109
6110 if (l >= 0x10000)
6111 return NULL;
6112 if (!l--)
6113 /* header */
6114 return (void*)1;
6115
6116 spin_lock(&all_mddevs_lock);
6117 list_for_each(tmp,&all_mddevs)
6118 if (!l--) {
6119 mddev = list_entry(tmp, mddev_t, all_mddevs);
6120 mddev_get(mddev);
6121 spin_unlock(&all_mddevs_lock);
6122 return mddev;
6123 }
6124 spin_unlock(&all_mddevs_lock);
6125 if (!l--)
6126 return (void*)2;/* tail */
6127 return NULL;
6128}
6129
6130static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6131{
6132 struct list_head *tmp;
6133 mddev_t *next_mddev, *mddev = v;
6134
6135 ++*pos;
6136 if (v == (void*)2)
6137 return NULL;
6138
6139 spin_lock(&all_mddevs_lock);
6140 if (v == (void*)1)
6141 tmp = all_mddevs.next;
6142 else
6143 tmp = mddev->all_mddevs.next;
6144 if (tmp != &all_mddevs)
6145 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6146 else {
6147 next_mddev = (void*)2;
6148 *pos = 0x10000;
6149 }
6150 spin_unlock(&all_mddevs_lock);
6151
6152 if (v != (void*)1)
6153 mddev_put(mddev);
6154 return next_mddev;
6155
6156}
6157
6158static void md_seq_stop(struct seq_file *seq, void *v)
6159{
6160 mddev_t *mddev = v;
6161
6162 if (mddev && v != (void*)1 && v != (void*)2)
6163 mddev_put(mddev);
6164}
6165
NeilBrownd7603b72006-01-06 00:20:30 -08006166struct mdstat_info {
6167 int event;
6168};
6169
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170static int md_seq_show(struct seq_file *seq, void *v)
6171{
6172 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006173 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006174 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006175 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006176 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177
6178 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006179 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180 seq_printf(seq, "Personalities : ");
6181 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006182 list_for_each_entry(pers, &pers_list, list)
6183 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
6185 spin_unlock(&pers_lock);
6186 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006187 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 return 0;
6189 }
6190 if (v == (void*)2) {
6191 status_unused(seq);
6192 return 0;
6193 }
6194
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006195 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006197
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6199 seq_printf(seq, "%s : %sactive", mdname(mddev),
6200 mddev->pers ? "" : "in");
6201 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006202 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006204 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006205 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 seq_printf(seq, " %s", mddev->pers->name);
6207 }
6208
Andre Nolldd8ac332009-03-31 14:33:13 +11006209 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006210 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 char b[BDEVNAME_SIZE];
6212 seq_printf(seq, " %s[%d]",
6213 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006214 if (test_bit(WriteMostly, &rdev->flags))
6215 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006216 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006217 seq_printf(seq, "(F)");
6218 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006219 } else if (rdev->raid_disk < 0)
6220 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006221 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 }
6223
6224 if (!list_empty(&mddev->disks)) {
6225 if (mddev->pers)
6226 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006227 (unsigned long long)
6228 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 else
6230 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006231 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006233 if (mddev->persistent) {
6234 if (mddev->major_version != 0 ||
6235 mddev->minor_version != 90) {
6236 seq_printf(seq," super %d.%d",
6237 mddev->major_version,
6238 mddev->minor_version);
6239 }
NeilBrowne6910632008-02-06 01:39:51 -08006240 } else if (mddev->external)
6241 seq_printf(seq, " super external:%s",
6242 mddev->metadata_type);
6243 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006244 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245
6246 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006247 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006249 if (mddev->pers->sync_request) {
6250 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006251 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006252 seq_printf(seq, "\n ");
6253 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6254 seq_printf(seq, "\tresync=DELAYED\n ");
6255 else if (mddev->recovery_cp < MaxSector)
6256 seq_printf(seq, "\tresync=PENDING\n ");
6257 }
NeilBrown32a76272005-06-21 17:17:14 -07006258 } else
6259 seq_printf(seq, "\n ");
6260
6261 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006262 unsigned long chunk_kb;
6263 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006264 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006265 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006266 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6267 "%lu%s chunk",
6268 bitmap->pages - bitmap->missing_pages,
6269 bitmap->pages,
6270 (bitmap->pages - bitmap->missing_pages)
6271 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006272 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006273 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006274 if (bitmap->file) {
6275 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006276 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006277 }
NeilBrown78d742d2005-06-21 17:17:15 -07006278
NeilBrown32a76272005-06-21 17:17:14 -07006279 seq_printf(seq, "\n");
6280 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281 }
6282
6283 seq_printf(seq, "\n");
6284 }
6285 mddev_unlock(mddev);
6286
6287 return 0;
6288}
6289
Jan Engelhardt110518b2009-05-07 12:49:37 +10006290static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 .start = md_seq_start,
6292 .next = md_seq_next,
6293 .stop = md_seq_stop,
6294 .show = md_seq_show,
6295};
6296
6297static int md_seq_open(struct inode *inode, struct file *file)
6298{
6299 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006300 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6301 if (mi == NULL)
6302 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303
6304 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006305 if (error)
6306 kfree(mi);
6307 else {
6308 struct seq_file *p = file->private_data;
6309 p->private = mi;
6310 mi->event = atomic_read(&md_event_count);
6311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312 return error;
6313}
6314
NeilBrownd7603b72006-01-06 00:20:30 -08006315static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6316{
6317 struct seq_file *m = filp->private_data;
6318 struct mdstat_info *mi = m->private;
6319 int mask;
6320
6321 poll_wait(filp, &md_event_waiters, wait);
6322
6323 /* always allow read */
6324 mask = POLLIN | POLLRDNORM;
6325
6326 if (mi->event != atomic_read(&md_event_count))
6327 mask |= POLLERR | POLLPRI;
6328 return mask;
6329}
6330
Arjan van de Venfa027c22007-02-12 00:55:33 -08006331static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006332 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 .open = md_seq_open,
6334 .read = seq_read,
6335 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006336 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006337 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338};
6339
NeilBrown2604b702006-01-06 00:20:36 -08006340int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006343 list_add_tail(&p->list, &pers_list);
6344 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345 spin_unlock(&pers_lock);
6346 return 0;
6347}
6348
NeilBrown2604b702006-01-06 00:20:36 -08006349int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
NeilBrown2604b702006-01-06 00:20:36 -08006351 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006353 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006354 spin_unlock(&pers_lock);
6355 return 0;
6356}
6357
NeilBrowneea1bf32009-03-31 14:27:02 +11006358static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359{
6360 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006362 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
6364 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006365 rcu_read_lock();
6366 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006368 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6369 (int)part_stat_read(&disk->part0, sectors[1]) -
6370 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006371 /* sync IO will cause sync_io to increase before the disk_stats
6372 * as sync_io is counted when a request starts, and
6373 * disk_stats is counted when it completes.
6374 * So resync activity will cause curr_events to be smaller than
6375 * when there was no such activity.
6376 * non-sync IO will cause disk_stat to increase without
6377 * increasing sync_io so curr_events will (eventually)
6378 * be larger than it was before. Once it becomes
6379 * substantially larger, the test below will cause
6380 * the array to appear non-idle, and resync will slow
6381 * down.
6382 * If there is a lot of outstanding resync activity when
6383 * we set last_event to curr_events, then all that activity
6384 * completing might cause the array to appear non-idle
6385 * and resync will be slowed down even though there might
6386 * not have been non-resync activity. This will only
6387 * happen once though. 'last_events' will soon reflect
6388 * the state where there is little or no outstanding
6389 * resync requests, and further resync activity will
6390 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006391 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006393 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 rdev->last_events = curr_events;
6395 idle = 0;
6396 }
6397 }
NeilBrown4b809912008-07-21 17:05:25 +10006398 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399 return idle;
6400}
6401
6402void md_done_sync(mddev_t *mddev, int blocks, int ok)
6403{
6404 /* another "blocks" (512byte) blocks have been synced */
6405 atomic_sub(blocks, &mddev->recovery_active);
6406 wake_up(&mddev->recovery_wait);
6407 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006408 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006409 md_wakeup_thread(mddev->thread);
6410 // stop recovery, signal do_sync ....
6411 }
6412}
6413
6414
NeilBrown06d91a52005-06-21 17:17:12 -07006415/* md_write_start(mddev, bi)
6416 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006417 * in superblock) before writing, schedule a superblock update
6418 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006419 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006420void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006421{
Neil Brown0fd62b82008-06-28 08:31:36 +10006422 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006423 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006424 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006425
NeilBrownf91de922005-11-08 21:39:36 -08006426 BUG_ON(mddev->ro == 1);
6427 if (mddev->ro == 2) {
6428 /* need to switch to read/write */
6429 mddev->ro = 0;
6430 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6431 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006432 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006433 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006434 }
NeilBrown06d91a52005-06-21 17:17:12 -07006435 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006436 if (mddev->safemode == 1)
6437 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006438 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006439 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006440 if (mddev->in_sync) {
6441 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006442 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006443 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006444 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006445 }
NeilBrowna9701a32005-11-08 21:39:34 -08006446 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006447 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006448 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006449 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006450 wait_event(mddev->sb_wait,
6451 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6452 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453}
6454
6455void md_write_end(mddev_t *mddev)
6456{
6457 if (atomic_dec_and_test(&mddev->writes_pending)) {
6458 if (mddev->safemode == 2)
6459 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006460 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6462 }
6463}
6464
NeilBrown2a2275d2007-01-26 00:57:11 -08006465/* md_allow_write(mddev)
6466 * Calling this ensures that the array is marked 'active' so that writes
6467 * may proceed without blocking. It is important to call this before
6468 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6469 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006470 *
6471 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6472 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006473 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006474int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006475{
6476 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006477 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006478 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006479 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006480 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006481 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006482
6483 spin_lock_irq(&mddev->write_lock);
6484 if (mddev->in_sync) {
6485 mddev->in_sync = 0;
6486 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6487 if (mddev->safemode_delay &&
6488 mddev->safemode == 0)
6489 mddev->safemode = 1;
6490 spin_unlock_irq(&mddev->write_lock);
6491 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006492 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006493 } else
6494 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006495
6496 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6497 return -EAGAIN;
6498 else
6499 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006500}
6501EXPORT_SYMBOL_GPL(md_allow_write);
6502
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503#define SYNC_MARKS 10
6504#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006505void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506{
6507 mddev_t *mddev2;
6508 unsigned int currspeed = 0,
6509 window;
NeilBrown57afd892005-06-21 17:17:13 -07006510 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 unsigned long mark[SYNC_MARKS];
6512 sector_t mark_cnt[SYNC_MARKS];
6513 int last_mark,m;
6514 struct list_head *tmp;
6515 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006516 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006517 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006518 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519
6520 /* just incase thread restarts... */
6521 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6522 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006523 if (mddev->ro) /* never try to sync a read-only array */
6524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006525
NeilBrown61df9d92006-10-03 01:15:57 -07006526 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6527 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6528 desc = "data-check";
6529 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6530 desc = "requested-resync";
6531 else
6532 desc = "resync";
6533 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6534 desc = "reshape";
6535 else
6536 desc = "recovery";
6537
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538 /* we overload curr_resync somewhat here.
6539 * 0 == not engaged in resync at all
6540 * 2 == checking that there is no conflict with another sync
6541 * 1 == like 2, but have yielded to allow conflicting resync to
6542 * commense
6543 * other == active in resync - this many blocks
6544 *
6545 * Before starting a resync we must have set curr_resync to
6546 * 2, and then checked that every "conflicting" array has curr_resync
6547 * less than ours. When we find one that is the same or higher
6548 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6549 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6550 * This will mean we have to start checking from the beginning again.
6551 *
6552 */
6553
6554 do {
6555 mddev->curr_resync = 2;
6556
6557 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006558 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006559 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006560
6561 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006562 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006563 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564 if (mddev2 == mddev)
6565 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006566 if (!mddev->parallel_resync
6567 && mddev2->curr_resync
6568 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006569 DEFINE_WAIT(wq);
6570 if (mddev < mddev2 && mddev->curr_resync == 2) {
6571 /* arbitrarily yield */
6572 mddev->curr_resync = 1;
6573 wake_up(&resync_wait);
6574 }
6575 if (mddev > mddev2 && mddev->curr_resync == 1)
6576 /* no need to wait here, we can wait the next
6577 * time 'round when curr_resync == 2
6578 */
6579 continue;
NeilBrown97441972008-09-19 11:49:54 +10006580 /* We need to wait 'interruptible' so as not to
6581 * contribute to the load average, and not to
6582 * be caught by 'softlockup'
6583 */
6584 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006585 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006586 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006587 printk(KERN_INFO "md: delaying %s of %s"
6588 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006590 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006592 if (signal_pending(current))
6593 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 schedule();
6595 finish_wait(&resync_wait, &wq);
6596 goto try_again;
6597 }
6598 finish_wait(&resync_wait, &wq);
6599 }
6600 }
6601 } while (mddev->curr_resync < 2);
6602
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006603 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006604 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006606 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607 */
6608 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006609 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006610 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006611 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6612 j = mddev->resync_min;
6613 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006614 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006615
NeilBrownccfcc3c2006-03-27 01:18:09 -08006616 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006617 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006618 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006620 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006621 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006622 rcu_read_lock();
6623 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006624 if (rdev->raid_disk >= 0 &&
6625 !test_bit(Faulty, &rdev->flags) &&
6626 !test_bit(In_sync, &rdev->flags) &&
6627 rdev->recovery_offset < j)
6628 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006629 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006631
NeilBrown61df9d92006-10-03 01:15:57 -07006632 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6633 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6634 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006635 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006636 "(but not more than %d KB/sec) for %s.\n",
6637 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638
NeilBrowneea1bf32009-03-31 14:27:02 +11006639 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006640
NeilBrown57afd892005-06-21 17:17:13 -07006641 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642 for (m = 0; m < SYNC_MARKS; m++) {
6643 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006644 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 }
6646 last_mark = 0;
6647 mddev->resync_mark = mark[last_mark];
6648 mddev->resync_mark_cnt = mark_cnt[last_mark];
6649
6650 /*
6651 * Tune reconstruction:
6652 */
6653 window = 32*(PAGE_SIZE/512);
6654 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6655 window/2,(unsigned long long) max_sectors/2);
6656
6657 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 last_check = 0;
6659
6660 if (j>2) {
6661 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006662 "md: resuming %s of %s from checkpoint.\n",
6663 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664 mddev->curr_resync = j;
6665 }
NeilBrownefa59332009-12-14 12:49:49 +11006666 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006667
6668 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006669 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
NeilBrown57afd892005-06-21 17:17:13 -07006671 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006672
NeilBrown7a91ee12009-05-26 12:57:21 +10006673 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6674 ((mddev->curr_resync > mddev->curr_resync_completed &&
6675 (mddev->curr_resync - mddev->curr_resync_completed)
6676 > (max_sectors >> 4)) ||
6677 (j - mddev->curr_resync_completed)*2
6678 >= mddev->resync_max - mddev->curr_resync_completed
6679 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006680 /* time to update curr_resync_completed */
6681 blk_unplug(mddev->queue);
6682 wait_event(mddev->recovery_wait,
6683 atomic_read(&mddev->recovery_active) == 0);
6684 mddev->curr_resync_completed =
6685 mddev->curr_resync;
6686 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006687 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006688 }
NeilBrownacb180b2009-04-14 16:28:34 +10006689
NeilBrowne62e58a2009-07-01 13:15:35 +10006690 while (j >= mddev->resync_max && !kthread_should_stop()) {
6691 /* As this condition is controlled by user-space,
6692 * we can block indefinitely, so use '_interruptible'
6693 * to avoid triggering warnings.
6694 */
6695 flush_signals(current); /* just in case */
6696 wait_event_interruptible(mddev->recovery_wait,
6697 mddev->resync_max > j
6698 || kthread_should_stop());
6699 }
NeilBrownacb180b2009-04-14 16:28:34 +10006700
6701 if (kthread_should_stop())
6702 goto interrupted;
6703
NeilBrown57afd892005-06-21 17:17:13 -07006704 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006705 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006706 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006707 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708 goto out;
6709 }
NeilBrown57afd892005-06-21 17:17:13 -07006710
6711 if (!skipped) { /* actual IO requested */
6712 io_sectors += sectors;
6713 atomic_add(sectors, &mddev->recovery_active);
6714 }
6715
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 j += sectors;
6717 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006718 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006719 if (last_check == 0)
6720 /* this is the earliers that rebuilt will be
6721 * visible in /proc/mdstat
6722 */
6723 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006724
6725 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006726 continue;
6727
NeilBrown57afd892005-06-21 17:17:13 -07006728 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006729
NeilBrowndfc70642008-05-23 13:04:39 -07006730 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731 break;
6732
6733 repeat:
6734 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6735 /* step marks */
6736 int next = (last_mark+1) % SYNC_MARKS;
6737
6738 mddev->resync_mark = mark[next];
6739 mddev->resync_mark_cnt = mark_cnt[next];
6740 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006741 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 last_mark = next;
6743 }
6744
6745
NeilBrownc6207272008-02-06 01:39:52 -08006746 if (kthread_should_stop())
6747 goto interrupted;
6748
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
6750 /*
6751 * this loop exits only if either when we are slower than
6752 * the 'hard' speed limit, or the system was IO-idle for
6753 * a jiffy.
6754 * the system might be non-idle CPU-wise, but we only care
6755 * about not overloading the IO subsystem. (things like an
6756 * e2fsck being done on the RAID array should execute fast)
6757 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006758 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759 cond_resched();
6760
NeilBrown57afd892005-06-21 17:17:13 -07006761 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6762 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006763
NeilBrown88202a02006-01-06 00:21:36 -08006764 if (currspeed > speed_min(mddev)) {
6765 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006766 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006767 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 goto repeat;
6769 }
6770 }
6771 }
NeilBrown61df9d92006-10-03 01:15:57 -07006772 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773 /*
6774 * this also signals 'finished resyncing' to md_stop
6775 */
6776 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006777 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778
6779 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6780
6781 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006782 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783
NeilBrowndfc70642008-05-23 13:04:39 -07006784 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006785 mddev->curr_resync > 2) {
6786 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6787 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6788 if (mddev->curr_resync >= mddev->recovery_cp) {
6789 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006790 "md: checkpointing %s of %s.\n",
6791 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006792 mddev->recovery_cp = mddev->curr_resync;
6793 }
6794 } else
6795 mddev->recovery_cp = MaxSector;
6796 } else {
6797 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6798 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006799 rcu_read_lock();
6800 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006801 if (rdev->raid_disk >= 0 &&
6802 !test_bit(Faulty, &rdev->flags) &&
6803 !test_bit(In_sync, &rdev->flags) &&
6804 rdev->recovery_offset < mddev->curr_resync)
6805 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006806 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808 }
NeilBrown17571282006-12-10 02:20:52 -08006809 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006810
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006812 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6813 /* We completed so min/max setting can be forgotten if used. */
6814 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6815 mddev->resync_min = 0;
6816 mddev->resync_max = MaxSector;
6817 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6818 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006820 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6821 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006822 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823 wake_up(&resync_wait);
6824 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6825 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006826 return;
6827
6828 interrupted:
6829 /*
6830 * got a signal, exit.
6831 */
6832 printk(KERN_INFO
6833 "md: md_do_sync() got signal ... exiting\n");
6834 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6835 goto out;
6836
Linus Torvalds1da177e2005-04-16 15:20:36 -07006837}
NeilBrown29269552006-03-27 01:18:10 -08006838EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839
6840
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006841static int remove_and_add_spares(mddev_t *mddev)
6842{
6843 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006844 int spares = 0;
6845
NeilBrown97e4f422009-03-31 14:33:13 +11006846 mddev->curr_resync_completed = 0;
6847
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006848 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006849 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006850 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006851 (test_bit(Faulty, &rdev->flags) ||
6852 ! test_bit(In_sync, &rdev->flags)) &&
6853 atomic_read(&rdev->nr_pending)==0) {
6854 if (mddev->pers->hot_remove_disk(
6855 mddev, rdev->raid_disk)==0) {
6856 char nm[20];
6857 sprintf(nm,"rd%d", rdev->raid_disk);
6858 sysfs_remove_link(&mddev->kobj, nm);
6859 rdev->raid_disk = -1;
6860 }
6861 }
6862
NeilBrown4044ba52009-01-09 08:31:11 +11006863 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006864 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006865 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006866 !test_bit(In_sync, &rdev->flags) &&
6867 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006868 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006869 if (rdev->raid_disk < 0
6870 && !test_bit(Faulty, &rdev->flags)) {
6871 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006872 if (mddev->pers->
6873 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006874 char nm[20];
6875 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006876 if (sysfs_create_link(&mddev->kobj,
6877 &rdev->kobj, nm))
6878 printk(KERN_WARNING
6879 "md: cannot register "
6880 "%s for %s\n",
6881 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006882 spares++;
6883 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006884 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006885 } else
6886 break;
6887 }
NeilBrowndfc70642008-05-23 13:04:39 -07006888 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006889 }
6890 return spares;
6891}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892/*
6893 * This routine is regularly called by all per-raid-array threads to
6894 * deal with generic issues like resync and super-block update.
6895 * Raid personalities that don't have a thread (linear/raid0) do not
6896 * need this as they never do any recovery or update the superblock.
6897 *
6898 * It does not do any resync itself, but rather "forks" off other threads
6899 * to do that as needed.
6900 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6901 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006902 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006903 * and wakeups up this thread which will reap the thread and finish up.
6904 * This thread also removes any faulty devices (with nr_pending == 0).
6905 *
6906 * The overall approach is:
6907 * 1/ if the superblock needs updating, update it.
6908 * 2/ If a recovery thread is running, don't do anything else.
6909 * 3/ If recovery has finished, clean up, possibly marking spares active.
6910 * 4/ If there are any faulty devices, remove them.
6911 * 5/ If array is degraded, try to add spares devices
6912 * 6/ If array has spares or is not in-sync, start a resync thread.
6913 */
6914void md_check_recovery(mddev_t *mddev)
6915{
6916 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917
6918
NeilBrown5f404022005-06-21 17:17:16 -07006919 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006920 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006921
6922 if (mddev->ro)
6923 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006924
6925 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006926 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006927 printk(KERN_INFO "md: %s in immediate safe mode\n",
6928 mdname(mddev));
6929 mddev->safemode = 2;
6930 }
6931 flush_signals(current);
6932 }
6933
NeilBrownc89a8ee2008-08-05 15:54:13 +10006934 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6935 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006936 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006937 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006938 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006939 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006940 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006941 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6942 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006943 ))
6944 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006945
NeilBrowndf5b89b2006-03-27 01:18:20 -08006946 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006947 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006948
NeilBrownc89a8ee2008-08-05 15:54:13 +10006949 if (mddev->ro) {
6950 /* Only thing we do on a ro array is remove
6951 * failed devices.
6952 */
6953 remove_and_add_spares(mddev);
6954 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6955 goto unlock;
6956 }
6957
NeilBrown31a59e32008-04-30 00:52:30 -07006958 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006959 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006960 spin_lock_irq(&mddev->write_lock);
6961 if (mddev->safemode &&
6962 !atomic_read(&mddev->writes_pending) &&
6963 !mddev->in_sync &&
6964 mddev->recovery_cp == MaxSector) {
6965 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006966 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006967 if (mddev->persistent)
6968 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6969 }
6970 if (mddev->safemode == 1)
6971 mddev->safemode = 0;
6972 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006973 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006974 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006975 }
NeilBrownfca4d842005-06-21 17:17:11 -07006976
NeilBrown850b2b422006-10-03 01:15:46 -07006977 if (mddev->flags)
6978 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006979
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6981 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6982 /* resync/recovery still happening */
6983 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6984 goto unlock;
6985 }
6986 if (mddev->sync_thread) {
6987 /* resync has finished, collect result */
6988 md_unregister_thread(mddev->sync_thread);
6989 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006990 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6991 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006992 /* success...*/
6993 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10006994 if (mddev->pers->spare_active(mddev))
6995 sysfs_notify(&mddev->kobj, NULL,
6996 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 }
NeilBrowncea9c222009-03-31 15:15:05 +11006998 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6999 mddev->pers->finish_reshape)
7000 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07007001 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007002
7003 /* if array is no-longer degraded, then any saved_raid_disk
7004 * information must be scrapped
7005 */
7006 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007007 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007008 rdev->saved_raid_disk = -1;
7009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007010 mddev->recovery = 0;
7011 /* flag recovery needed just to double check */
7012 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007013 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007014 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015 goto unlock;
7016 }
Neil Brown72a23c22008-06-28 08:31:41 +10007017 /* Set RUNNING before clearing NEEDED to avoid
7018 * any transients in the value of "sync_action".
7019 */
7020 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7021 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007022 /* Clear some bits that don't mean anything, but
7023 * might be left set
7024 */
NeilBrown24dd4692005-11-08 21:39:26 -08007025 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7026 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007028 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7029 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 /* no recovery is running.
7031 * remove any failed drives, then
7032 * add spares if possible.
7033 * Spare are also removed and re-added, to allow
7034 * the personality to fail the re-add.
7035 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007037 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007038 if (mddev->pers->check_reshape == NULL ||
7039 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007040 /* Cannot proceed */
7041 goto unlock;
7042 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007043 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007044 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007045 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7046 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007047 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007048 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007049 } else if (mddev->recovery_cp < MaxSector) {
7050 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007051 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007052 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7053 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007055
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007057 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7058 /* We are adding a device or devices to an array
7059 * which has the bitmap stored on all devices.
7060 * So make sure all bitmap pages get written
7061 */
7062 bitmap_write_all(mddev->bitmap);
7063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 mddev->sync_thread = md_register_thread(md_do_sync,
7065 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007066 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 if (!mddev->sync_thread) {
7068 printk(KERN_ERR "%s: could not start resync"
7069 " thread...\n",
7070 mdname(mddev));
7071 /* leave the spares where they are, it shouldn't hurt */
7072 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007073 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007075 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007076 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 }
7078 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007079 if (!mddev->sync_thread) {
7080 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7081 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7082 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007083 if (mddev->sysfs_action)
7084 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007086 mddev_unlock(mddev);
7087 }
7088}
7089
Dan Williams6bfe0b42008-04-30 00:52:32 -07007090void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7091{
NeilBrown3c0ee632008-10-21 13:25:28 +11007092 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007093 wait_event_timeout(rdev->blocked_wait,
7094 !test_bit(Blocked, &rdev->flags),
7095 msecs_to_jiffies(5000));
7096 rdev_dec_pending(rdev, mddev);
7097}
7098EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7099
Adrian Bunk75c96f82005-05-05 16:16:09 -07007100static int md_notify_reboot(struct notifier_block *this,
7101 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102{
7103 struct list_head *tmp;
7104 mddev_t *mddev;
7105
7106 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7107
7108 printk(KERN_INFO "md: stopping all md devices.\n");
7109
NeilBrown29ac4aa2008-02-06 01:39:58 -08007110 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007111 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007112 /* Force a switch to readonly even array
7113 * appears to still be in use. Hence
7114 * the '100'.
7115 */
NeilBrownd710e132008-10-13 11:55:12 +11007116 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007117 mddev_unlock(mddev);
7118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 /*
7120 * certain more exotic SCSI devices are known to be
7121 * volatile wrt too early system reboots. While the
7122 * right place to handle this issue is the given
7123 * driver, we do want to have a safe RAID driver ...
7124 */
7125 mdelay(1000*1);
7126 }
7127 return NOTIFY_DONE;
7128}
7129
Adrian Bunk75c96f82005-05-05 16:16:09 -07007130static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007131 .notifier_call = md_notify_reboot,
7132 .next = NULL,
7133 .priority = INT_MAX, /* before any real devices */
7134};
7135
7136static void md_geninit(void)
7137{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007138 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7139
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007140 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141}
7142
Adrian Bunk75c96f82005-05-05 16:16:09 -07007143static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007145 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 return -1;
7147 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007148 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149 return -1;
7150 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007151 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007152 md_probe, NULL, NULL);
7153 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 md_probe, NULL, NULL);
7155
Linus Torvalds1da177e2005-04-16 15:20:36 -07007156 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007157 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158
7159 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161}
7162
7163
7164#ifndef MODULE
7165
7166/*
7167 * Searches all registered partitions for autorun RAID arrays
7168 * at boot time.
7169 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007170
7171static LIST_HEAD(all_detected_devices);
7172struct detected_devices_node {
7173 struct list_head list;
7174 dev_t dev;
7175};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176
7177void md_autodetect_dev(dev_t dev)
7178{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007179 struct detected_devices_node *node_detected_dev;
7180
7181 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7182 if (node_detected_dev) {
7183 node_detected_dev->dev = dev;
7184 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7185 } else {
7186 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7187 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007189}
7190
7191
7192static void autostart_arrays(int part)
7193{
7194 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007195 struct detected_devices_node *node_detected_dev;
7196 dev_t dev;
7197 int i_scanned, i_passed;
7198
7199 i_scanned = 0;
7200 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201
7202 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7203
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007204 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7205 i_scanned++;
7206 node_detected_dev = list_entry(all_detected_devices.next,
7207 struct detected_devices_node, list);
7208 list_del(&node_detected_dev->list);
7209 dev = node_detected_dev->dev;
7210 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007211 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212 if (IS_ERR(rdev))
7213 continue;
7214
NeilBrownb2d444d2005-11-08 21:39:31 -08007215 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007216 MD_BUG();
7217 continue;
7218 }
NeilBrownd0fae182008-03-04 14:29:31 -08007219 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007221 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007222 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007223
7224 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7225 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226
7227 autorun_devices(part);
7228}
7229
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007230#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231
7232static __exit void md_exit(void)
7233{
7234 mddev_t *mddev;
7235 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007236
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007237 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007238 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007240 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241 unregister_blkdev(mdp_major, "mdp");
7242 unregister_reboot_notifier(&md_notifier);
7243 unregister_sysctl_table(raid_table_header);
7244 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007245 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007247 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 }
7249}
7250
Dan Williams685784a2007-07-09 11:56:42 -07007251subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252module_exit(md_exit)
7253
NeilBrownf91de922005-11-08 21:39:36 -08007254static int get_ro(char *buffer, struct kernel_param *kp)
7255{
7256 return sprintf(buffer, "%d", start_readonly);
7257}
7258static int set_ro(const char *val, struct kernel_param *kp)
7259{
7260 char *e;
7261 int num = simple_strtoul(val, &e, 10);
7262 if (*val && (*e == '\0' || *e == '\n')) {
7263 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007264 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007265 }
7266 return -EINVAL;
7267}
7268
NeilBrown80ca3a42006-07-10 04:44:18 -07007269module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7270module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007271
NeilBrownefeb53c2009-01-09 08:31:10 +11007272module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007273
Linus Torvalds1da177e2005-04-16 15:20:36 -07007274EXPORT_SYMBOL(register_md_personality);
7275EXPORT_SYMBOL(unregister_md_personality);
7276EXPORT_SYMBOL(md_error);
7277EXPORT_SYMBOL(md_done_sync);
7278EXPORT_SYMBOL(md_write_start);
7279EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280EXPORT_SYMBOL(md_register_thread);
7281EXPORT_SYMBOL(md_unregister_thread);
7282EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007283EXPORT_SYMBOL(md_check_recovery);
7284MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007285MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007286MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007287MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);