blob: 117663d2a4e525a67b3477554ac098e8b14f775a [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);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004504 mddev->changed = 1;
NeilBrownd7603b72006-01-06 00:20:30 -08004505 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004506 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004507 if (mddev->sysfs_action)
4508 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004509 sysfs_notify(&mddev->kobj, NULL, "degraded");
Tejun Heoed9e1982008-08-25 19:56:05 +09004510 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return 0;
4512}
4513
4514static int restart_array(mddev_t *mddev)
4515{
4516 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
Andre Noll80fab1d2008-07-11 22:02:21 +10004518 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004520 return -ENXIO;
4521 if (!mddev->pers)
4522 return -EINVAL;
4523 if (!mddev->ro)
4524 return -EBUSY;
4525 mddev->safemode = 0;
4526 mddev->ro = 0;
4527 set_disk_ro(disk, 0);
4528 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4529 mdname(mddev));
4530 /* Kick recovery or resync if necessary */
4531 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4532 md_wakeup_thread(mddev->thread);
4533 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004534 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536}
4537
NeilBrownacc55e22006-06-26 00:27:47 -07004538/* similar to deny_write_access, but accounts for our holding a reference
4539 * to the file ourselves */
4540static int deny_bitmap_write_access(struct file * file)
4541{
4542 struct inode *inode = file->f_mapping->host;
4543
4544 spin_lock(&inode->i_lock);
4545 if (atomic_read(&inode->i_writecount) > 1) {
4546 spin_unlock(&inode->i_lock);
4547 return -ETXTBSY;
4548 }
4549 atomic_set(&inode->i_writecount, -1);
4550 spin_unlock(&inode->i_lock);
4551
4552 return 0;
4553}
4554
NeilBrown43a70502009-12-14 12:49:55 +11004555void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004556{
4557 struct inode *inode = file->f_mapping->host;
4558
4559 spin_lock(&inode->i_lock);
4560 atomic_set(&inode->i_writecount, 1);
4561 spin_unlock(&inode->i_lock);
4562}
4563
NeilBrown9e653b62006-06-26 00:27:58 -07004564/* mode:
4565 * 0 - completely stop and dis-assemble array
4566 * 1 - switch to readonly
4567 * 2 - stop but do not disassemble array
4568 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004569static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
4571 int err = 0;
4572 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004573 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
NeilBrownc8c00a62009-08-10 12:50:52 +10004575 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004576 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004577 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004578 err = -EBUSY;
4579 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
4581 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004582 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4584 md_unregister_thread(mddev->sync_thread);
4585 mddev->sync_thread = NULL;
4586 }
4587
4588 del_timer_sync(&mddev->safemode_timer);
4589
NeilBrown9e653b62006-06-26 00:27:58 -07004590 switch(mode) {
4591 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004593 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 goto out;
4595 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004596 break;
4597 case 0: /* disassemble */
4598 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004599 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004600 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 if (mddev->ro)
4602 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004605 mddev->queue->merge_bvec_fn = NULL;
4606 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004607 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 module_put(mddev->pers->owner);
NeilBrowna64c8762010-04-14 17:15:37 +10004609 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4610 mddev->to_remove = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004612 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004613 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004614
NeilBrownc4647292009-05-07 12:51:06 +10004615 list_for_each_entry(rdev, &mddev->disks, same_set)
4616 if (rdev->raid_disk >= 0) {
4617 char nm[20];
4618 sprintf(nm, "rd%d", rdev->raid_disk);
4619 sysfs_remove_link(&mddev->kobj, nm);
4620 }
4621
NeilBrown0d4ca602006-12-10 02:20:44 -08004622 set_capacity(disk, 0);
Linus Torvalds44ce62942007-05-09 18:51:36 -07004623 mddev->changed = 1;
NeilBrown0d4ca602006-12-10 02:20:44 -08004624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 if (mddev->ro)
4626 mddev->ro = 0;
4627 }
NeilBrown850b2b422006-10-03 01:15:46 -07004628 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 /* mark array as shutdown cleanly */
4630 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07004631 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 }
NeilBrown9e653b62006-06-26 00:27:58 -07004633 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004635 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004636 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004638out:
4639 mutex_unlock(&mddev->open_mutex);
4640 if (err)
4641 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 /*
4643 * Free resources if final stop
4644 */
NeilBrown9e653b62006-06-26 00:27:58 -07004645 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4648
NeilBrown978f9462006-02-02 14:28:05 -08004649 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004650 if (mddev->bitmap_info.file) {
4651 restore_bitmap_write_access(mddev->bitmap_info.file);
4652 fput(mddev->bitmap_info.file);
4653 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004654 }
NeilBrownc3d97142009-12-14 12:49:52 +11004655 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 export_array(mddev);
4658
Andre Nollf233ea52008-07-21 17:05:22 +10004659 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004660 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004661 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004662 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004663 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004664 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004665 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004666 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004667 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004668 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004669 mddev->level = LEVEL_NONE;
4670 mddev->clevel[0] = 0;
4671 mddev->flags = 0;
4672 mddev->ro = 0;
4673 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004674 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004675 mddev->ctime = mddev->utime = 0;
4676 mddev->layout = 0;
4677 mddev->max_disks = 0;
4678 mddev->events = 0;
4679 mddev->delta_disks = 0;
4680 mddev->new_level = LEVEL_NONE;
4681 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004682 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004683 mddev->curr_resync = 0;
4684 mddev->resync_mismatches = 0;
4685 mddev->suspend_lo = mddev->suspend_hi = 0;
4686 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4687 mddev->recovery = 0;
4688 mddev->in_sync = 0;
4689 mddev->changed = 0;
4690 mddev->degraded = 0;
4691 mddev->barriers_work = 0;
4692 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004693 mddev->bitmap_info.offset = 0;
4694 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004695 mddev->bitmap_info.chunksize = 0;
4696 mddev->bitmap_info.daemon_sleep = 0;
4697 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004698 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004699 if (mddev->hold_active == UNTIL_STOP)
4700 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004701
NeilBrowna8a55c32006-06-26 00:27:39 -07004702 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4704 mdname(mddev));
4705 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004706 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004707 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004708 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 return err;
4710}
4711
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004712#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713static void autorun_array(mddev_t *mddev)
4714{
4715 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 int err;
4717
NeilBrowna757e642005-04-16 15:26:42 -07004718 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720
4721 printk(KERN_INFO "md: running: ");
4722
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004723 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 char b[BDEVNAME_SIZE];
4725 printk("<%s>", bdevname(rdev->bdev,b));
4726 }
4727 printk("\n");
4728
NeilBrownd710e132008-10-13 11:55:12 +11004729 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 if (err) {
4731 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004732 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 }
4734}
4735
4736/*
4737 * lets try to run arrays based on all disks that have arrived
4738 * until now. (those are in pending_raid_disks)
4739 *
4740 * the method: pick the first pending disk, collect all disks with
4741 * the same UUID, remove all from the pending list and put them into
4742 * the 'same_array' list. Then order this list based on superblock
4743 * update time (freshest comes first), kick out 'old' disks and
4744 * compare superblocks. If everything's fine then run it.
4745 *
4746 * If "unit" is allocated, then bump its reference count
4747 */
4748static void autorun_devices(int part)
4749{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004750 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 mddev_t *mddev;
4752 char b[BDEVNAME_SIZE];
4753
4754 printk(KERN_INFO "md: autorun ...\n");
4755 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004756 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004758 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 rdev0 = list_entry(pending_raid_disks.next,
4760 mdk_rdev_t, same_set);
4761
4762 printk(KERN_INFO "md: considering %s ...\n",
4763 bdevname(rdev0->bdev,b));
4764 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004765 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 if (super_90_load(rdev, rdev0, 0) >= 0) {
4767 printk(KERN_INFO "md: adding %s ...\n",
4768 bdevname(rdev->bdev,b));
4769 list_move(&rdev->same_set, &candidates);
4770 }
4771 /*
4772 * now we have a set of devices, with all of them having
4773 * mostly sane superblocks. It's time to allocate the
4774 * mddev.
4775 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004776 if (part) {
4777 dev = MKDEV(mdp_major,
4778 rdev0->preferred_minor << MdpMinorShift);
4779 unit = MINOR(dev) >> MdpMinorShift;
4780 } else {
4781 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4782 unit = MINOR(dev);
4783 }
4784 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4786 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4787 break;
4788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
4790 md_probe(dev, NULL, NULL);
4791 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004792 if (!mddev || !mddev->gendisk) {
4793 if (mddev)
4794 mddev_put(mddev);
4795 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 "md: cannot allocate memory for md drive.\n");
4797 break;
4798 }
4799 if (mddev_lock(mddev))
4800 printk(KERN_WARNING "md: %s locked, cannot run\n",
4801 mdname(mddev));
4802 else if (mddev->raid_disks || mddev->major_version
4803 || !list_empty(&mddev->disks)) {
4804 printk(KERN_WARNING
4805 "md: %s already running, cannot run %s\n",
4806 mdname(mddev), bdevname(rdev0->bdev,b));
4807 mddev_unlock(mddev);
4808 } else {
4809 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004810 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004811 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812 list_del_init(&rdev->same_set);
4813 if (bind_rdev_to_array(rdev, mddev))
4814 export_rdev(rdev);
4815 }
4816 autorun_array(mddev);
4817 mddev_unlock(mddev);
4818 }
4819 /* on success, candidates will be empty, on error
4820 * it won't...
4821 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004822 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004823 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 mddev_put(mddev);
4827 }
4828 printk(KERN_INFO "md: ... autorun DONE.\n");
4829}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004830#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832static int get_version(void __user * arg)
4833{
4834 mdu_version_t ver;
4835
4836 ver.major = MD_MAJOR_VERSION;
4837 ver.minor = MD_MINOR_VERSION;
4838 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4839
4840 if (copy_to_user(arg, &ver, sizeof(ver)))
4841 return -EFAULT;
4842
4843 return 0;
4844}
4845
4846static int get_array_info(mddev_t * mddev, void __user * arg)
4847{
4848 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004849 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851
NeilBrowna9f326e2009-09-23 18:06:41 +10004852 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004853 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004855 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 failed++;
4857 else {
4858 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004859 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004860 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 else
4862 spare++;
4863 }
4864 }
4865
4866 info.major_version = mddev->major_version;
4867 info.minor_version = mddev->minor_version;
4868 info.patch_version = MD_PATCHLEVEL_VERSION;
4869 info.ctime = mddev->ctime;
4870 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004871 info.size = mddev->dev_sectors / 2;
4872 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004873 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 info.nr_disks = nr;
4875 info.raid_disks = mddev->raid_disks;
4876 info.md_minor = mddev->md_minor;
4877 info.not_persistent= !mddev->persistent;
4878
4879 info.utime = mddev->utime;
4880 info.state = 0;
4881 if (mddev->in_sync)
4882 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004883 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004884 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004885 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 info.working_disks = working;
4887 info.failed_disks = failed;
4888 info.spare_disks = spare;
4889
4890 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004891 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892
4893 if (copy_to_user(arg, &info, sizeof(info)))
4894 return -EFAULT;
4895
4896 return 0;
4897}
4898
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004899static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004900{
4901 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4902 char *ptr, *buf = NULL;
4903 int err = -ENOMEM;
4904
Dan Williamsb5470dc2008-06-27 21:44:04 -07004905 if (md_allow_write(mddev))
4906 file = kmalloc(sizeof(*file), GFP_NOIO);
4907 else
4908 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004909
NeilBrown32a76272005-06-21 17:17:14 -07004910 if (!file)
4911 goto out;
4912
4913 /* bitmap disabled, zero the first byte and copy out */
4914 if (!mddev->bitmap || !mddev->bitmap->file) {
4915 file->pathname[0] = '\0';
4916 goto copy_out;
4917 }
4918
4919 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4920 if (!buf)
4921 goto out;
4922
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004923 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4924 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004925 goto out;
4926
4927 strcpy(file->pathname, ptr);
4928
4929copy_out:
4930 err = 0;
4931 if (copy_to_user(arg, file, sizeof(*file)))
4932 err = -EFAULT;
4933out:
4934 kfree(buf);
4935 kfree(file);
4936 return err;
4937}
4938
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939static int get_disk_info(mddev_t * mddev, void __user * arg)
4940{
4941 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 mdk_rdev_t *rdev;
4943
4944 if (copy_from_user(&info, arg, sizeof(info)))
4945 return -EFAULT;
4946
Andre Noll26ef3792008-07-11 22:02:21 +10004947 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 if (rdev) {
4949 info.major = MAJOR(rdev->bdev->bd_dev);
4950 info.minor = MINOR(rdev->bdev->bd_dev);
4951 info.raid_disk = rdev->raid_disk;
4952 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004953 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004955 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 info.state |= (1<<MD_DISK_ACTIVE);
4957 info.state |= (1<<MD_DISK_SYNC);
4958 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004959 if (test_bit(WriteMostly, &rdev->flags))
4960 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 } else {
4962 info.major = info.minor = 0;
4963 info.raid_disk = -1;
4964 info.state = (1<<MD_DISK_REMOVED);
4965 }
4966
4967 if (copy_to_user(arg, &info, sizeof(info)))
4968 return -EFAULT;
4969
4970 return 0;
4971}
4972
4973static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4974{
4975 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4976 mdk_rdev_t *rdev;
4977 dev_t dev = MKDEV(info->major,info->minor);
4978
4979 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4980 return -EOVERFLOW;
4981
4982 if (!mddev->raid_disks) {
4983 int err;
4984 /* expecting a device which has a superblock */
4985 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4986 if (IS_ERR(rdev)) {
4987 printk(KERN_WARNING
4988 "md: md_import_device returned %ld\n",
4989 PTR_ERR(rdev));
4990 return PTR_ERR(rdev);
4991 }
4992 if (!list_empty(&mddev->disks)) {
4993 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
4994 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10004995 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 .load_super(rdev, rdev0, mddev->minor_version);
4997 if (err < 0) {
4998 printk(KERN_WARNING
4999 "md: %s has different UUID to %s\n",
5000 bdevname(rdev->bdev,b),
5001 bdevname(rdev0->bdev,b2));
5002 export_rdev(rdev);
5003 return -EINVAL;
5004 }
5005 }
5006 err = bind_rdev_to_array(rdev, mddev);
5007 if (err)
5008 export_rdev(rdev);
5009 return err;
5010 }
5011
5012 /*
5013 * add_new_disk can be used once the array is assembled
5014 * to add "hot spares". They must already have a superblock
5015 * written
5016 */
5017 if (mddev->pers) {
5018 int err;
5019 if (!mddev->pers->hot_add_disk) {
5020 printk(KERN_WARNING
5021 "%s: personality does not support diskops!\n",
5022 mdname(mddev));
5023 return -EINVAL;
5024 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005025 if (mddev->persistent)
5026 rdev = md_import_device(dev, mddev->major_version,
5027 mddev->minor_version);
5028 else
5029 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 if (IS_ERR(rdev)) {
5031 printk(KERN_WARNING
5032 "md: md_import_device returned %ld\n",
5033 PTR_ERR(rdev));
5034 return PTR_ERR(rdev);
5035 }
NeilBrown41158c72005-06-21 17:17:25 -07005036 /* set save_raid_disk if appropriate */
5037 if (!mddev->persistent) {
5038 if (info->state & (1<<MD_DISK_SYNC) &&
5039 info->raid_disk < mddev->raid_disks)
5040 rdev->raid_disk = info->raid_disk;
5041 else
5042 rdev->raid_disk = -1;
5043 } else
5044 super_types[mddev->major_version].
5045 validate_super(mddev, rdev);
5046 rdev->saved_raid_disk = rdev->raid_disk;
5047
NeilBrownb2d444d2005-11-08 21:39:31 -08005048 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005049 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5050 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005051 else
5052 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 rdev->raid_disk = -1;
5055 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005056 if (!err && !mddev->pers->hot_remove_disk) {
5057 /* If there is hot_add_disk but no hot_remove_disk
5058 * then added disks for geometry changes,
5059 * and should be added immediately.
5060 */
5061 super_types[mddev->major_version].
5062 validate_super(mddev, rdev);
5063 err = mddev->pers->hot_add_disk(mddev, rdev);
5064 if (err)
5065 unbind_rdev_from_array(rdev);
5066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 if (err)
5068 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005069 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005070 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005071
NeilBrown17571282006-12-10 02:20:52 -08005072 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005073 if (mddev->degraded)
5074 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005075 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005076 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 return err;
5078 }
5079
5080 /* otherwise, add_new_disk is only allowed
5081 * for major_version==0 superblocks
5082 */
5083 if (mddev->major_version != 0) {
5084 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5085 mdname(mddev));
5086 return -EINVAL;
5087 }
5088
5089 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5090 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005091 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 if (IS_ERR(rdev)) {
5093 printk(KERN_WARNING
5094 "md: error, md_import_device() returned %ld\n",
5095 PTR_ERR(rdev));
5096 return PTR_ERR(rdev);
5097 }
5098 rdev->desc_nr = info->number;
5099 if (info->raid_disk < mddev->raid_disks)
5100 rdev->raid_disk = info->raid_disk;
5101 else
5102 rdev->raid_disk = -1;
5103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005105 if (info->state & (1<<MD_DISK_SYNC))
5106 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005108 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5109 set_bit(WriteMostly, &rdev->flags);
5110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 if (!mddev->persistent) {
5112 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005113 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 } else
Andre Noll0f420352008-07-11 22:02:23 +10005115 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005116 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117
NeilBrown2bf071b2006-01-06 00:20:55 -08005118 err = bind_rdev_to_array(rdev, mddev);
5119 if (err) {
5120 export_rdev(rdev);
5121 return err;
5122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 }
5124
5125 return 0;
5126}
5127
5128static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5129{
5130 char b[BDEVNAME_SIZE];
5131 mdk_rdev_t *rdev;
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 rdev = find_rdev(mddev, dev);
5134 if (!rdev)
5135 return -ENXIO;
5136
5137 if (rdev->raid_disk >= 0)
5138 goto busy;
5139
5140 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005141 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005142 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143
5144 return 0;
5145busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005146 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 bdevname(rdev->bdev,b), mdname(mddev));
5148 return -EBUSY;
5149}
5150
5151static int hot_add_disk(mddev_t * mddev, dev_t dev)
5152{
5153 char b[BDEVNAME_SIZE];
5154 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 mdk_rdev_t *rdev;
5156
5157 if (!mddev->pers)
5158 return -ENODEV;
5159
5160 if (mddev->major_version != 0) {
5161 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5162 " version-0 superblocks.\n",
5163 mdname(mddev));
5164 return -EINVAL;
5165 }
5166 if (!mddev->pers->hot_add_disk) {
5167 printk(KERN_WARNING
5168 "%s: personality does not support diskops!\n",
5169 mdname(mddev));
5170 return -EINVAL;
5171 }
5172
NeilBrownd710e132008-10-13 11:55:12 +11005173 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 if (IS_ERR(rdev)) {
5175 printk(KERN_WARNING
5176 "md: error, md_import_device() returned %ld\n",
5177 PTR_ERR(rdev));
5178 return -EINVAL;
5179 }
5180
5181 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005182 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 else
Andre Noll0f420352008-07-11 22:02:23 +10005184 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185
NeilBrown8190e752009-06-18 08:48:58 +10005186 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187
NeilBrownb2d444d2005-11-08 21:39:31 -08005188 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 printk(KERN_WARNING
5190 "md: can not hot-add faulty %s disk to %s!\n",
5191 bdevname(rdev->bdev,b), mdname(mddev));
5192 err = -EINVAL;
5193 goto abort_export;
5194 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005195 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005197 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005198 err = bind_rdev_to_array(rdev, mddev);
5199 if (err)
5200 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
5202 /*
5203 * The rest should better be atomic, we can have disk failures
5204 * noticed in interrupt contexts ...
5205 */
5206
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 rdev->raid_disk = -1;
5208
NeilBrown850b2b422006-10-03 01:15:46 -07005209 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210
5211 /*
5212 * Kick recovery, maybe this spare has to be added to the
5213 * array immediately.
5214 */
5215 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5216 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005217 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 return 0;
5219
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220abort_export:
5221 export_rdev(rdev);
5222 return err;
5223}
5224
NeilBrown32a76272005-06-21 17:17:14 -07005225static int set_bitmap_file(mddev_t *mddev, int fd)
5226{
5227 int err;
5228
NeilBrown36fa3062005-09-09 16:23:45 -07005229 if (mddev->pers) {
5230 if (!mddev->pers->quiesce)
5231 return -EBUSY;
5232 if (mddev->recovery || mddev->sync_thread)
5233 return -EBUSY;
5234 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005235 }
5236
NeilBrown36fa3062005-09-09 16:23:45 -07005237
5238 if (fd >= 0) {
5239 if (mddev->bitmap)
5240 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005241 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005242
NeilBrownc3d97142009-12-14 12:49:52 +11005243 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005244 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5245 mdname(mddev));
5246 return -EBADF;
5247 }
5248
NeilBrownc3d97142009-12-14 12:49:52 +11005249 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005250 if (err) {
5251 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5252 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005253 fput(mddev->bitmap_info.file);
5254 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005255 return err;
5256 }
NeilBrownc3d97142009-12-14 12:49:52 +11005257 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005258 } else if (mddev->bitmap == NULL)
5259 return -ENOENT; /* cannot remove what isn't there */
5260 err = 0;
5261 if (mddev->pers) {
5262 mddev->pers->quiesce(mddev, 1);
5263 if (fd >= 0)
5264 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005265 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005266 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005267 fd = -1; /* make sure to put the file */
5268 }
NeilBrown36fa3062005-09-09 16:23:45 -07005269 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005270 }
5271 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005272 if (mddev->bitmap_info.file) {
5273 restore_bitmap_write_access(mddev->bitmap_info.file);
5274 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005275 }
NeilBrownc3d97142009-12-14 12:49:52 +11005276 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005277 }
5278
NeilBrown32a76272005-06-21 17:17:14 -07005279 return err;
5280}
5281
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282/*
5283 * set_array_info is used two different ways
5284 * The original usage is when creating a new array.
5285 * In this usage, raid_disks is > 0 and it together with
5286 * level, size, not_persistent,layout,chunksize determine the
5287 * shape of the array.
5288 * This will always create an array with a type-0.90.0 superblock.
5289 * The newer usage is when assembling an array.
5290 * In this case raid_disks will be 0, and the major_version field is
5291 * use to determine which style super-blocks are to be found on the devices.
5292 * The minor and patch _version numbers are also kept incase the
5293 * super_block handler wishes to interpret them.
5294 */
5295static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5296{
5297
5298 if (info->raid_disks == 0) {
5299 /* just setting version number for superblock loading */
5300 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005301 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 super_types[info->major_version].name == NULL) {
5303 /* maybe try to auto-load a module? */
5304 printk(KERN_INFO
5305 "md: superblock version %d not known\n",
5306 info->major_version);
5307 return -EINVAL;
5308 }
5309 mddev->major_version = info->major_version;
5310 mddev->minor_version = info->minor_version;
5311 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005312 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005313 /* ensure mddev_put doesn't delete this now that there
5314 * is some minimal configuration.
5315 */
5316 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 return 0;
5318 }
5319 mddev->major_version = MD_MAJOR_VERSION;
5320 mddev->minor_version = MD_MINOR_VERSION;
5321 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5322 mddev->ctime = get_seconds();
5323
5324 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005325 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005326 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 mddev->raid_disks = info->raid_disks;
5328 /* don't set md_minor, it is determined by which /dev/md* was
5329 * openned
5330 */
5331 if (info->state & (1<<MD_SB_CLEAN))
5332 mddev->recovery_cp = MaxSector;
5333 else
5334 mddev->recovery_cp = 0;
5335 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005336 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337
5338 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005339 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340
5341 mddev->max_disks = MD_SB_DISKS;
5342
NeilBrowne6910632008-02-06 01:39:51 -08005343 if (mddev->persistent)
5344 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005345 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
NeilBrownc3d97142009-12-14 12:49:52 +11005347 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5348 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005349
NeilBrownf6705572006-03-27 01:18:11 -08005350 mddev->reshape_position = MaxSector;
5351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 /*
5353 * Generate a 128 bit UUID
5354 */
5355 get_random_bytes(mddev->uuid, 16);
5356
NeilBrownf6705572006-03-27 01:18:11 -08005357 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005358 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005359 mddev->new_layout = mddev->layout;
5360 mddev->delta_disks = 0;
5361
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 return 0;
5363}
5364
Dan Williams1f403622009-03-31 14:59:03 +11005365void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5366{
Dan Williamsb522adc2009-03-31 15:00:31 +11005367 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5368
5369 if (mddev->external_size)
5370 return;
5371
Dan Williams1f403622009-03-31 14:59:03 +11005372 mddev->array_sectors = array_sectors;
5373}
5374EXPORT_SYMBOL(md_set_array_sectors);
5375
Andre Nolld71f9f82008-07-11 22:02:22 +10005376static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005377{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005378 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005379 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005380 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005381
5382 if (mddev->pers->resize == NULL)
5383 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005384 /* The "num_sectors" is the number of sectors of each device that
5385 * is used. This can only make sense for arrays with redundancy.
5386 * linear and raid0 always use whatever space is available. We can only
5387 * consider changing this number if no resync or reconstruction is
5388 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005389 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005390 * of each device. If num_sectors is zero, we find the largest size
5391 * that fits.
5392
NeilBrowna35b0d62006-01-06 00:20:49 -08005393 */
5394 if (mddev->sync_thread)
5395 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005396 if (mddev->bitmap)
5397 /* Sorry, cannot grow a bitmap yet, just remove it,
5398 * grow, and re-add.
5399 */
5400 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005401 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005402 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005403
Andre Nolld71f9f82008-07-11 22:02:22 +10005404 if (fit && (num_sectors == 0 || num_sectors > avail))
5405 num_sectors = avail;
5406 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005407 return -ENOSPC;
5408 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005409 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005410 if (!rv)
5411 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005412 return rv;
5413}
5414
NeilBrownda943b992006-01-06 00:20:54 -08005415static int update_raid_disks(mddev_t *mddev, int raid_disks)
5416{
5417 int rv;
5418 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005419 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005420 return -EINVAL;
5421 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005422 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005423 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005424 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005425 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005426 mddev->delta_disks = raid_disks - mddev->raid_disks;
5427
5428 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005429 return rv;
5430}
5431
5432
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433/*
5434 * update_array_info is used to change the configuration of an
5435 * on-line array.
5436 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5437 * fields in the info are checked against the array.
5438 * Any differences that cannot be handled will cause an error.
5439 * Normally, only one change can be managed at a time.
5440 */
5441static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5442{
5443 int rv = 0;
5444 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005445 int state = 0;
5446
5447 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005448 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005449 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
5451 if (mddev->major_version != info->major_version ||
5452 mddev->minor_version != info->minor_version ||
5453/* mddev->patch_version != info->patch_version || */
5454 mddev->ctime != info->ctime ||
5455 mddev->level != info->level ||
5456/* mddev->layout != info->layout || */
5457 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005458 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005459 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5460 ((state^info->state) & 0xfffffe00)
5461 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 return -EINVAL;
5463 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005464 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5465 cnt++;
5466 if (mddev->raid_disks != info->raid_disks)
5467 cnt++;
5468 if (mddev->layout != info->layout)
5469 cnt++;
5470 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5471 cnt++;
5472 if (cnt == 0)
5473 return 0;
5474 if (cnt > 1)
5475 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476
5477 if (mddev->layout != info->layout) {
5478 /* Change layout
5479 * we don't need to do anything at the md level, the
5480 * personality will take care of it all.
5481 */
NeilBrown50ac1682009-06-18 08:47:55 +10005482 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005484 else {
5485 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005486 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005487 if (rv)
5488 mddev->new_layout = mddev->layout;
5489 return rv;
5490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005492 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005493 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494
NeilBrownda943b992006-01-06 00:20:54 -08005495 if (mddev->raid_disks != info->raid_disks)
5496 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497
NeilBrown36fa3062005-09-09 16:23:45 -07005498 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5499 if (mddev->pers->quiesce == NULL)
5500 return -EINVAL;
5501 if (mddev->recovery || mddev->sync_thread)
5502 return -EBUSY;
5503 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5504 /* add the bitmap */
5505 if (mddev->bitmap)
5506 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005507 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005508 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005509 mddev->bitmap_info.offset =
5510 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005511 mddev->pers->quiesce(mddev, 1);
5512 rv = bitmap_create(mddev);
5513 if (rv)
5514 bitmap_destroy(mddev);
5515 mddev->pers->quiesce(mddev, 0);
5516 } else {
5517 /* remove the bitmap */
5518 if (!mddev->bitmap)
5519 return -ENOENT;
5520 if (mddev->bitmap->file)
5521 return -EINVAL;
5522 mddev->pers->quiesce(mddev, 1);
5523 bitmap_destroy(mddev);
5524 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005525 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005526 }
5527 }
NeilBrown850b2b422006-10-03 01:15:46 -07005528 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 return rv;
5530}
5531
5532static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5533{
5534 mdk_rdev_t *rdev;
5535
5536 if (mddev->pers == NULL)
5537 return -ENODEV;
5538
5539 rdev = find_rdev(mddev, dev);
5540 if (!rdev)
5541 return -ENODEV;
5542
5543 md_error(mddev, rdev);
5544 return 0;
5545}
5546
Andre Noll2f9618c2008-04-25 18:57:58 +02005547/*
5548 * We have a problem here : there is no easy way to give a CHS
5549 * virtual geometry. We currently pretend that we have a 2 heads
5550 * 4 sectors (with a BIG number of cylinders...). This drives
5551 * dosfs just mad... ;-)
5552 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005553static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5554{
5555 mddev_t *mddev = bdev->bd_disk->private_data;
5556
5557 geo->heads = 2;
5558 geo->sectors = 4;
5559 geo->cylinders = get_capacity(mddev->gendisk) / 8;
5560 return 0;
5561}
5562
Al Viroa39907f2008-03-02 10:31:15 -05005563static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005564 unsigned int cmd, unsigned long arg)
5565{
5566 int err = 0;
5567 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005569 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570
5571 if (!capable(CAP_SYS_ADMIN))
5572 return -EACCES;
5573
5574 /*
5575 * Commands dealing with the RAID driver but not any
5576 * particular array:
5577 */
5578 switch (cmd)
5579 {
5580 case RAID_VERSION:
5581 err = get_version(argp);
5582 goto done;
5583
5584 case PRINT_RAID_DEBUG:
5585 err = 0;
5586 md_print_devices();
5587 goto done;
5588
5589#ifndef MODULE
5590 case RAID_AUTORUN:
5591 err = 0;
5592 autostart_arrays(arg);
5593 goto done;
5594#endif
5595 default:;
5596 }
5597
5598 /*
5599 * Commands creating/starting a new array:
5600 */
5601
Al Viroa39907f2008-03-02 10:31:15 -05005602 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603
5604 if (!mddev) {
5605 BUG();
5606 goto abort;
5607 }
5608
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 err = mddev_lock(mddev);
5610 if (err) {
5611 printk(KERN_INFO
5612 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5613 err, cmd);
5614 goto abort;
5615 }
5616
5617 switch (cmd)
5618 {
5619 case SET_ARRAY_INFO:
5620 {
5621 mdu_array_info_t info;
5622 if (!arg)
5623 memset(&info, 0, sizeof(info));
5624 else if (copy_from_user(&info, argp, sizeof(info))) {
5625 err = -EFAULT;
5626 goto abort_unlock;
5627 }
5628 if (mddev->pers) {
5629 err = update_array_info(mddev, &info);
5630 if (err) {
5631 printk(KERN_WARNING "md: couldn't update"
5632 " array info. %d\n", err);
5633 goto abort_unlock;
5634 }
5635 goto done_unlock;
5636 }
5637 if (!list_empty(&mddev->disks)) {
5638 printk(KERN_WARNING
5639 "md: array %s already has disks!\n",
5640 mdname(mddev));
5641 err = -EBUSY;
5642 goto abort_unlock;
5643 }
5644 if (mddev->raid_disks) {
5645 printk(KERN_WARNING
5646 "md: array %s already initialised!\n",
5647 mdname(mddev));
5648 err = -EBUSY;
5649 goto abort_unlock;
5650 }
5651 err = set_array_info(mddev, &info);
5652 if (err) {
5653 printk(KERN_WARNING "md: couldn't set"
5654 " array info. %d\n", err);
5655 goto abort_unlock;
5656 }
5657 }
5658 goto done_unlock;
5659
5660 default:;
5661 }
5662
5663 /*
5664 * Commands querying/configuring an existing array:
5665 */
NeilBrown32a76272005-06-21 17:17:14 -07005666 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005667 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005668 if ((!mddev->raid_disks && !mddev->external)
5669 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5670 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5671 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 err = -ENODEV;
5673 goto abort_unlock;
5674 }
5675
5676 /*
5677 * Commands even a read-only array can execute:
5678 */
5679 switch (cmd)
5680 {
5681 case GET_ARRAY_INFO:
5682 err = get_array_info(mddev, argp);
5683 goto done_unlock;
5684
NeilBrown32a76272005-06-21 17:17:14 -07005685 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005686 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005687 goto done_unlock;
5688
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 case GET_DISK_INFO:
5690 err = get_disk_info(mddev, argp);
5691 goto done_unlock;
5692
5693 case RESTART_ARRAY_RW:
5694 err = restart_array(mddev);
5695 goto done_unlock;
5696
5697 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005698 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 goto done_unlock;
5700
5701 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005702 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 goto done_unlock;
5704
Dan Williamse2218352010-05-12 08:25:37 +10005705 case BLKROSET:
5706 if (get_user(ro, (int __user *)(arg))) {
5707 err = -EFAULT;
5708 goto done_unlock;
5709 }
5710 err = -EINVAL;
5711
5712 /* if the bdev is going readonly the value of mddev->ro
5713 * does not matter, no writes are coming
5714 */
5715 if (ro)
5716 goto done_unlock;
5717
5718 /* are we are already prepared for writes? */
5719 if (mddev->ro != 1)
5720 goto done_unlock;
5721
5722 /* transitioning to readauto need only happen for
5723 * arrays that call md_write_start
5724 */
5725 if (mddev->pers) {
5726 err = restart_array(mddev);
5727 if (err == 0) {
5728 mddev->ro = 2;
5729 set_disk_ro(mddev->gendisk, 0);
5730 }
5731 }
5732 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 }
5734
5735 /*
5736 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005737 * superblock, so we do not allow them on read-only arrays.
5738 * However non-MD ioctls (e.g. get-size) will still come through
5739 * here and hit the 'default' below, so only disallow
5740 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005742 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005743 if (mddev->ro == 2) {
5744 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005745 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005746 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5747 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005748 } else {
5749 err = -EROFS;
5750 goto abort_unlock;
5751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 }
5753
5754 switch (cmd)
5755 {
5756 case ADD_NEW_DISK:
5757 {
5758 mdu_disk_info_t info;
5759 if (copy_from_user(&info, argp, sizeof(info)))
5760 err = -EFAULT;
5761 else
5762 err = add_new_disk(mddev, &info);
5763 goto done_unlock;
5764 }
5765
5766 case HOT_REMOVE_DISK:
5767 err = hot_remove_disk(mddev, new_decode_dev(arg));
5768 goto done_unlock;
5769
5770 case HOT_ADD_DISK:
5771 err = hot_add_disk(mddev, new_decode_dev(arg));
5772 goto done_unlock;
5773
5774 case SET_DISK_FAULTY:
5775 err = set_disk_faulty(mddev, new_decode_dev(arg));
5776 goto done_unlock;
5777
5778 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005779 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 goto done_unlock;
5781
NeilBrown32a76272005-06-21 17:17:14 -07005782 case SET_BITMAP_FILE:
5783 err = set_bitmap_file(mddev, (int)arg);
5784 goto done_unlock;
5785
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 err = -EINVAL;
5788 goto abort_unlock;
5789 }
5790
5791done_unlock:
5792abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005793 if (mddev->hold_active == UNTIL_IOCTL &&
5794 err != -EINVAL)
5795 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796 mddev_unlock(mddev);
5797
5798 return err;
5799done:
5800 if (err)
5801 MD_BUG();
5802abort:
5803 return err;
5804}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005805#ifdef CONFIG_COMPAT
5806static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5807 unsigned int cmd, unsigned long arg)
5808{
5809 switch (cmd) {
5810 case HOT_REMOVE_DISK:
5811 case HOT_ADD_DISK:
5812 case SET_DISK_FAULTY:
5813 case SET_BITMAP_FILE:
5814 /* These take in integer arg, do not convert */
5815 break;
5816 default:
5817 arg = (unsigned long)compat_ptr(arg);
5818 break;
5819 }
5820
5821 return md_ioctl(bdev, mode, cmd, arg);
5822}
5823#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Al Viroa39907f2008-03-02 10:31:15 -05005825static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005826{
5827 /*
5828 * Succeed if we can lock the mddev, which confirms that
5829 * it isn't being stopped right now.
5830 */
NeilBrownd3374822009-01-09 08:31:10 +11005831 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 int err;
5833
NeilBrownd3374822009-01-09 08:31:10 +11005834 if (mddev->gendisk != bdev->bd_disk) {
5835 /* we are racing with mddev_put which is discarding this
5836 * bd_disk.
5837 */
5838 mddev_put(mddev);
5839 /* Wait until bdev->bd_disk is definitely gone */
5840 flush_scheduled_work();
5841 /* Then retry the open from the top */
5842 return -ERESTARTSYS;
5843 }
5844 BUG_ON(mddev != bdev->bd_disk->private_data);
5845
NeilBrownc8c00a62009-08-10 12:50:52 +10005846 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005847 goto out;
5848
5849 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005850 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005851 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005852
Al Viroa39907f2008-03-02 10:31:15 -05005853 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 out:
5855 return err;
5856}
5857
Al Viroa39907f2008-03-02 10:31:15 -05005858static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859{
Al Viroa39907f2008-03-02 10:31:15 -05005860 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005862 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005863 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 mddev_put(mddev);
5865
5866 return 0;
5867}
5868
Linus Torvalds44ce62942007-05-09 18:51:36 -07005869static int md_media_changed(struct gendisk *disk)
5870{
5871 mddev_t *mddev = disk->private_data;
5872
5873 return mddev->changed;
5874}
5875
5876static int md_revalidate(struct gendisk *disk)
5877{
5878 mddev_t *mddev = disk->private_data;
5879
5880 mddev->changed = 0;
5881 return 0;
5882}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005883static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884{
5885 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005886 .open = md_open,
5887 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005888 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005889#ifdef CONFIG_COMPAT
5890 .compat_ioctl = md_compat_ioctl,
5891#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005892 .getgeo = md_getgeo,
Linus Torvalds44ce62942007-05-09 18:51:36 -07005893 .media_changed = md_media_changed,
5894 .revalidate_disk= md_revalidate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895};
5896
Adrian Bunk75c96f82005-05-05 16:16:09 -07005897static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898{
5899 mdk_thread_t *thread = arg;
5900
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 /*
5902 * md_thread is a 'system-thread', it's priority should be very
5903 * high. We avoid resource deadlocks individually in each
5904 * raid personality. (RAID5 does preallocation) We also use RR and
5905 * the very same RT priority as kswapd, thus we will never get
5906 * into a priority inversion deadlock.
5907 *
5908 * we definitely have to have equal or higher priority than
5909 * bdflush, otherwise bdflush will deadlock if there are too
5910 * many dirty RAID5 blocks.
5911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912
NeilBrown6985c432005-10-19 21:23:47 -07005913 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005914 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915
NeilBrown93588e22005-11-15 00:09:12 -08005916 /* We need to wait INTERRUPTIBLE so that
5917 * we don't add to the load-average.
5918 * That means we need to be sure no signals are
5919 * pending
5920 */
5921 if (signal_pending(current))
5922 flush_signals(current);
5923
5924 wait_event_interruptible_timeout
5925 (thread->wqueue,
5926 test_bit(THREAD_WAKEUP, &thread->flags)
5927 || kthread_should_stop(),
5928 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929
5930 clear_bit(THREAD_WAKEUP, &thread->flags);
5931
NeilBrown787453c2005-11-08 21:39:43 -08005932 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005934
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 return 0;
5936}
5937
5938void md_wakeup_thread(mdk_thread_t *thread)
5939{
5940 if (thread) {
5941 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5942 set_bit(THREAD_WAKEUP, &thread->flags);
5943 wake_up(&thread->wqueue);
5944 }
5945}
5946
5947mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5948 const char *name)
5949{
5950 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005951
NeilBrown9ffae0c2006-01-06 00:20:32 -08005952 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005953 if (!thread)
5954 return NULL;
5955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956 init_waitqueue_head(&thread->wqueue);
5957
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958 thread->run = run;
5959 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005960 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005961 thread->tsk = kthread_run(md_thread, thread,
5962 "%s_%s",
5963 mdname(thread->mddev),
5964 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005965 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 kfree(thread);
5967 return NULL;
5968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005969 return thread;
5970}
5971
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972void md_unregister_thread(mdk_thread_t *thread)
5973{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005974 if (!thread)
5975 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005976 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005977
5978 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005979 kfree(thread);
5980}
5981
5982void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5983{
5984 if (!mddev) {
5985 MD_BUG();
5986 return;
5987 }
5988
NeilBrownb2d444d2005-11-08 21:39:31 -08005989 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005990 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005991
5992 if (mddev->external)
5993 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005994/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5996 mdname(mddev),
5997 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5998 __builtin_return_address(0),__builtin_return_address(1),
5999 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006000*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006001 if (!mddev->pers)
6002 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 if (!mddev->pers->error_handler)
6004 return;
6005 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006006 if (mddev->degraded)
6007 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11006008 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6010 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6011 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07006012 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013}
6014
6015/* seq_file implementation /proc/mdstat */
6016
6017static void status_unused(struct seq_file *seq)
6018{
6019 int i = 0;
6020 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021
6022 seq_printf(seq, "unused devices: ");
6023
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006024 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 char b[BDEVNAME_SIZE];
6026 i++;
6027 seq_printf(seq, "%s ",
6028 bdevname(rdev->bdev,b));
6029 }
6030 if (!i)
6031 seq_printf(seq, "<none>");
6032
6033 seq_printf(seq, "\n");
6034}
6035
6036
6037static void status_resync(struct seq_file *seq, mddev_t * mddev)
6038{
NeilBrowndd71cf62009-05-07 12:49:35 +10006039 sector_t max_sectors, resync, res;
6040 unsigned long dt, db;
6041 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006042 int scale;
6043 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044
NeilBrowndd71cf62009-05-07 12:49:35 +10006045 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046
6047 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006048 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006050 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051
6052 /*
6053 * Should not happen.
6054 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006055 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 MD_BUG();
6057 return;
6058 }
NeilBrown4588b422006-03-27 01:18:04 -08006059 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006060 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006061 * u32, as those are the requirements for sector_div.
6062 * Thus 'scale' must be at least 10
6063 */
6064 scale = 10;
6065 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006066 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006067 scale++;
6068 }
6069 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006070 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006071
6072 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 {
NeilBrown4588b422006-03-27 01:18:04 -08006074 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 seq_printf(seq, "[");
6076 for (i = 0; i < x; i++)
6077 seq_printf(seq, "=");
6078 seq_printf(seq, ">");
6079 for (i = 0; i < y; i++)
6080 seq_printf(seq, ".");
6081 seq_printf(seq, "] ");
6082 }
NeilBrown4588b422006-03-27 01:18:04 -08006083 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006084 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6085 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006086 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6087 "check" :
6088 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6089 "resync" : "recovery"))),
6090 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006091 (unsigned long long) resync/2,
6092 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006093
6094 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 * dt: time from mark until now
6096 * db: blocks written from mark until now
6097 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006098 *
6099 * rt is a sector_t, so could be 32bit or 64bit.
6100 * So we divide before multiply in case it is 32bit and close
6101 * to the limit.
6102 * We scale the divisor (db) by 32 to avoid loosing precision
6103 * near the end of resync when the number of remaining sectors
6104 * is close to 'db'.
6105 * We then divide rt by 32 after multiplying by db to compensate.
6106 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006107 */
6108 dt = ((jiffies - mddev->resync_mark) / HZ);
6109 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006110 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6111 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
NeilBrowndd71cf62009-05-07 12:49:35 +10006113 rt = max_sectors - resync; /* number of remaining sectors */
6114 sector_div(rt, db/32+1);
6115 rt *= dt;
6116 rt >>= 5;
6117
6118 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6119 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
NeilBrownff4e8d92006-07-10 04:44:16 -07006121 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006122}
6123
6124static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6125{
6126 struct list_head *tmp;
6127 loff_t l = *pos;
6128 mddev_t *mddev;
6129
6130 if (l >= 0x10000)
6131 return NULL;
6132 if (!l--)
6133 /* header */
6134 return (void*)1;
6135
6136 spin_lock(&all_mddevs_lock);
6137 list_for_each(tmp,&all_mddevs)
6138 if (!l--) {
6139 mddev = list_entry(tmp, mddev_t, all_mddevs);
6140 mddev_get(mddev);
6141 spin_unlock(&all_mddevs_lock);
6142 return mddev;
6143 }
6144 spin_unlock(&all_mddevs_lock);
6145 if (!l--)
6146 return (void*)2;/* tail */
6147 return NULL;
6148}
6149
6150static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6151{
6152 struct list_head *tmp;
6153 mddev_t *next_mddev, *mddev = v;
6154
6155 ++*pos;
6156 if (v == (void*)2)
6157 return NULL;
6158
6159 spin_lock(&all_mddevs_lock);
6160 if (v == (void*)1)
6161 tmp = all_mddevs.next;
6162 else
6163 tmp = mddev->all_mddevs.next;
6164 if (tmp != &all_mddevs)
6165 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6166 else {
6167 next_mddev = (void*)2;
6168 *pos = 0x10000;
6169 }
6170 spin_unlock(&all_mddevs_lock);
6171
6172 if (v != (void*)1)
6173 mddev_put(mddev);
6174 return next_mddev;
6175
6176}
6177
6178static void md_seq_stop(struct seq_file *seq, void *v)
6179{
6180 mddev_t *mddev = v;
6181
6182 if (mddev && v != (void*)1 && v != (void*)2)
6183 mddev_put(mddev);
6184}
6185
NeilBrownd7603b72006-01-06 00:20:30 -08006186struct mdstat_info {
6187 int event;
6188};
6189
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190static int md_seq_show(struct seq_file *seq, void *v)
6191{
6192 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006193 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006195 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006196 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197
6198 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006199 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200 seq_printf(seq, "Personalities : ");
6201 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006202 list_for_each_entry(pers, &pers_list, list)
6203 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204
6205 spin_unlock(&pers_lock);
6206 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006207 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 return 0;
6209 }
6210 if (v == (void*)2) {
6211 status_unused(seq);
6212 return 0;
6213 }
6214
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006215 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006217
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6219 seq_printf(seq, "%s : %sactive", mdname(mddev),
6220 mddev->pers ? "" : "in");
6221 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006222 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006224 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006225 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 seq_printf(seq, " %s", mddev->pers->name);
6227 }
6228
Andre Nolldd8ac332009-03-31 14:33:13 +11006229 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006230 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231 char b[BDEVNAME_SIZE];
6232 seq_printf(seq, " %s[%d]",
6233 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006234 if (test_bit(WriteMostly, &rdev->flags))
6235 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006236 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 seq_printf(seq, "(F)");
6238 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006239 } else if (rdev->raid_disk < 0)
6240 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006241 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 }
6243
6244 if (!list_empty(&mddev->disks)) {
6245 if (mddev->pers)
6246 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006247 (unsigned long long)
6248 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 else
6250 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006251 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006252 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006253 if (mddev->persistent) {
6254 if (mddev->major_version != 0 ||
6255 mddev->minor_version != 90) {
6256 seq_printf(seq," super %d.%d",
6257 mddev->major_version,
6258 mddev->minor_version);
6259 }
NeilBrowne6910632008-02-06 01:39:51 -08006260 } else if (mddev->external)
6261 seq_printf(seq, " super external:%s",
6262 mddev->metadata_type);
6263 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006264 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
6266 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006267 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006269 if (mddev->pers->sync_request) {
6270 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006271 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006272 seq_printf(seq, "\n ");
6273 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6274 seq_printf(seq, "\tresync=DELAYED\n ");
6275 else if (mddev->recovery_cp < MaxSector)
6276 seq_printf(seq, "\tresync=PENDING\n ");
6277 }
NeilBrown32a76272005-06-21 17:17:14 -07006278 } else
6279 seq_printf(seq, "\n ");
6280
6281 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006282 unsigned long chunk_kb;
6283 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006284 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006285 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006286 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6287 "%lu%s chunk",
6288 bitmap->pages - bitmap->missing_pages,
6289 bitmap->pages,
6290 (bitmap->pages - bitmap->missing_pages)
6291 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006292 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006293 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006294 if (bitmap->file) {
6295 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006296 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006297 }
NeilBrown78d742d2005-06-21 17:17:15 -07006298
NeilBrown32a76272005-06-21 17:17:14 -07006299 seq_printf(seq, "\n");
6300 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301 }
6302
6303 seq_printf(seq, "\n");
6304 }
6305 mddev_unlock(mddev);
6306
6307 return 0;
6308}
6309
Jan Engelhardt110518b2009-05-07 12:49:37 +10006310static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311 .start = md_seq_start,
6312 .next = md_seq_next,
6313 .stop = md_seq_stop,
6314 .show = md_seq_show,
6315};
6316
6317static int md_seq_open(struct inode *inode, struct file *file)
6318{
6319 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006320 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6321 if (mi == NULL)
6322 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006323
6324 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006325 if (error)
6326 kfree(mi);
6327 else {
6328 struct seq_file *p = file->private_data;
6329 p->private = mi;
6330 mi->event = atomic_read(&md_event_count);
6331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006332 return error;
6333}
6334
NeilBrownd7603b72006-01-06 00:20:30 -08006335static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6336{
6337 struct seq_file *m = filp->private_data;
6338 struct mdstat_info *mi = m->private;
6339 int mask;
6340
6341 poll_wait(filp, &md_event_waiters, wait);
6342
6343 /* always allow read */
6344 mask = POLLIN | POLLRDNORM;
6345
6346 if (mi->event != atomic_read(&md_event_count))
6347 mask |= POLLERR | POLLPRI;
6348 return mask;
6349}
6350
Arjan van de Venfa027c22007-02-12 00:55:33 -08006351static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006352 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 .open = md_seq_open,
6354 .read = seq_read,
6355 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006356 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006357 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358};
6359
NeilBrown2604b702006-01-06 00:20:36 -08006360int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006363 list_add_tail(&p->list, &pers_list);
6364 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 spin_unlock(&pers_lock);
6366 return 0;
6367}
6368
NeilBrown2604b702006-01-06 00:20:36 -08006369int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006370{
NeilBrown2604b702006-01-06 00:20:36 -08006371 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006373 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 spin_unlock(&pers_lock);
6375 return 0;
6376}
6377
NeilBrowneea1bf32009-03-31 14:27:02 +11006378static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379{
6380 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006382 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383
6384 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006385 rcu_read_lock();
6386 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006388 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6389 (int)part_stat_read(&disk->part0, sectors[1]) -
6390 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006391 /* sync IO will cause sync_io to increase before the disk_stats
6392 * as sync_io is counted when a request starts, and
6393 * disk_stats is counted when it completes.
6394 * So resync activity will cause curr_events to be smaller than
6395 * when there was no such activity.
6396 * non-sync IO will cause disk_stat to increase without
6397 * increasing sync_io so curr_events will (eventually)
6398 * be larger than it was before. Once it becomes
6399 * substantially larger, the test below will cause
6400 * the array to appear non-idle, and resync will slow
6401 * down.
6402 * If there is a lot of outstanding resync activity when
6403 * we set last_event to curr_events, then all that activity
6404 * completing might cause the array to appear non-idle
6405 * and resync will be slowed down even though there might
6406 * not have been non-resync activity. This will only
6407 * happen once though. 'last_events' will soon reflect
6408 * the state where there is little or no outstanding
6409 * resync requests, and further resync activity will
6410 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006411 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006412 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006413 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006414 rdev->last_events = curr_events;
6415 idle = 0;
6416 }
6417 }
NeilBrown4b809912008-07-21 17:05:25 +10006418 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006419 return idle;
6420}
6421
6422void md_done_sync(mddev_t *mddev, int blocks, int ok)
6423{
6424 /* another "blocks" (512byte) blocks have been synced */
6425 atomic_sub(blocks, &mddev->recovery_active);
6426 wake_up(&mddev->recovery_wait);
6427 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006428 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429 md_wakeup_thread(mddev->thread);
6430 // stop recovery, signal do_sync ....
6431 }
6432}
6433
6434
NeilBrown06d91a52005-06-21 17:17:12 -07006435/* md_write_start(mddev, bi)
6436 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006437 * in superblock) before writing, schedule a superblock update
6438 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006439 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006440void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006441{
Neil Brown0fd62b82008-06-28 08:31:36 +10006442 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006443 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006444 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006445
NeilBrownf91de922005-11-08 21:39:36 -08006446 BUG_ON(mddev->ro == 1);
6447 if (mddev->ro == 2) {
6448 /* need to switch to read/write */
6449 mddev->ro = 0;
6450 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6451 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006452 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006453 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006454 }
NeilBrown06d91a52005-06-21 17:17:12 -07006455 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006456 if (mddev->safemode == 1)
6457 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006458 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006459 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006460 if (mddev->in_sync) {
6461 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006462 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006463 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006464 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006465 }
NeilBrowna9701a32005-11-08 21:39:34 -08006466 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006467 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006468 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006469 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006470 wait_event(mddev->sb_wait,
6471 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6472 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006473}
6474
6475void md_write_end(mddev_t *mddev)
6476{
6477 if (atomic_dec_and_test(&mddev->writes_pending)) {
6478 if (mddev->safemode == 2)
6479 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006480 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006481 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6482 }
6483}
6484
NeilBrown2a2275d2007-01-26 00:57:11 -08006485/* md_allow_write(mddev)
6486 * Calling this ensures that the array is marked 'active' so that writes
6487 * may proceed without blocking. It is important to call this before
6488 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6489 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006490 *
6491 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6492 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006493 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006494int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006495{
6496 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006497 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006498 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006499 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006500 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006501 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006502
6503 spin_lock_irq(&mddev->write_lock);
6504 if (mddev->in_sync) {
6505 mddev->in_sync = 0;
6506 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6507 if (mddev->safemode_delay &&
6508 mddev->safemode == 0)
6509 mddev->safemode = 1;
6510 spin_unlock_irq(&mddev->write_lock);
6511 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006512 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006513 } else
6514 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006515
6516 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6517 return -EAGAIN;
6518 else
6519 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006520}
6521EXPORT_SYMBOL_GPL(md_allow_write);
6522
Linus Torvalds1da177e2005-04-16 15:20:36 -07006523#define SYNC_MARKS 10
6524#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006525void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526{
6527 mddev_t *mddev2;
6528 unsigned int currspeed = 0,
6529 window;
NeilBrown57afd892005-06-21 17:17:13 -07006530 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006531 unsigned long mark[SYNC_MARKS];
6532 sector_t mark_cnt[SYNC_MARKS];
6533 int last_mark,m;
6534 struct list_head *tmp;
6535 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006536 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006537 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006538 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539
6540 /* just incase thread restarts... */
6541 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6542 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006543 if (mddev->ro) /* never try to sync a read-only array */
6544 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545
NeilBrown61df9d92006-10-03 01:15:57 -07006546 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6547 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6548 desc = "data-check";
6549 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6550 desc = "requested-resync";
6551 else
6552 desc = "resync";
6553 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6554 desc = "reshape";
6555 else
6556 desc = "recovery";
6557
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558 /* we overload curr_resync somewhat here.
6559 * 0 == not engaged in resync at all
6560 * 2 == checking that there is no conflict with another sync
6561 * 1 == like 2, but have yielded to allow conflicting resync to
6562 * commense
6563 * other == active in resync - this many blocks
6564 *
6565 * Before starting a resync we must have set curr_resync to
6566 * 2, and then checked that every "conflicting" array has curr_resync
6567 * less than ours. When we find one that is the same or higher
6568 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6569 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6570 * This will mean we have to start checking from the beginning again.
6571 *
6572 */
6573
6574 do {
6575 mddev->curr_resync = 2;
6576
6577 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006578 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006579 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006580
6581 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006582 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006583 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 if (mddev2 == mddev)
6585 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006586 if (!mddev->parallel_resync
6587 && mddev2->curr_resync
6588 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006589 DEFINE_WAIT(wq);
6590 if (mddev < mddev2 && mddev->curr_resync == 2) {
6591 /* arbitrarily yield */
6592 mddev->curr_resync = 1;
6593 wake_up(&resync_wait);
6594 }
6595 if (mddev > mddev2 && mddev->curr_resync == 1)
6596 /* no need to wait here, we can wait the next
6597 * time 'round when curr_resync == 2
6598 */
6599 continue;
NeilBrown97441972008-09-19 11:49:54 +10006600 /* We need to wait 'interruptible' so as not to
6601 * contribute to the load average, and not to
6602 * be caught by 'softlockup'
6603 */
6604 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006605 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006606 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006607 printk(KERN_INFO "md: delaying %s of %s"
6608 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006610 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006612 if (signal_pending(current))
6613 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006614 schedule();
6615 finish_wait(&resync_wait, &wq);
6616 goto try_again;
6617 }
6618 finish_wait(&resync_wait, &wq);
6619 }
6620 }
6621 } while (mddev->curr_resync < 2);
6622
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006623 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006624 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006626 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 */
6628 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006629 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006630 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006631 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6632 j = mddev->resync_min;
6633 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006634 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006635
NeilBrownccfcc3c2006-03-27 01:18:09 -08006636 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006637 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006638 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006640 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006641 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006642 rcu_read_lock();
6643 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006644 if (rdev->raid_disk >= 0 &&
6645 !test_bit(Faulty, &rdev->flags) &&
6646 !test_bit(In_sync, &rdev->flags) &&
6647 rdev->recovery_offset < j)
6648 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006649 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651
NeilBrown61df9d92006-10-03 01:15:57 -07006652 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6653 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6654 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006655 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006656 "(but not more than %d KB/sec) for %s.\n",
6657 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658
NeilBrowneea1bf32009-03-31 14:27:02 +11006659 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006660
NeilBrown57afd892005-06-21 17:17:13 -07006661 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006662 for (m = 0; m < SYNC_MARKS; m++) {
6663 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006664 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665 }
6666 last_mark = 0;
6667 mddev->resync_mark = mark[last_mark];
6668 mddev->resync_mark_cnt = mark_cnt[last_mark];
6669
6670 /*
6671 * Tune reconstruction:
6672 */
6673 window = 32*(PAGE_SIZE/512);
6674 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6675 window/2,(unsigned long long) max_sectors/2);
6676
6677 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678 last_check = 0;
6679
6680 if (j>2) {
6681 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006682 "md: resuming %s of %s from checkpoint.\n",
6683 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 mddev->curr_resync = j;
6685 }
NeilBrownefa59332009-12-14 12:49:49 +11006686 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006687
6688 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006689 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690
NeilBrown57afd892005-06-21 17:17:13 -07006691 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006692
NeilBrown7a91ee12009-05-26 12:57:21 +10006693 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6694 ((mddev->curr_resync > mddev->curr_resync_completed &&
6695 (mddev->curr_resync - mddev->curr_resync_completed)
6696 > (max_sectors >> 4)) ||
6697 (j - mddev->curr_resync_completed)*2
6698 >= mddev->resync_max - mddev->curr_resync_completed
6699 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006700 /* time to update curr_resync_completed */
6701 blk_unplug(mddev->queue);
6702 wait_event(mddev->recovery_wait,
6703 atomic_read(&mddev->recovery_active) == 0);
6704 mddev->curr_resync_completed =
6705 mddev->curr_resync;
6706 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006707 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006708 }
NeilBrownacb180b2009-04-14 16:28:34 +10006709
NeilBrowne62e58a2009-07-01 13:15:35 +10006710 while (j >= mddev->resync_max && !kthread_should_stop()) {
6711 /* As this condition is controlled by user-space,
6712 * we can block indefinitely, so use '_interruptible'
6713 * to avoid triggering warnings.
6714 */
6715 flush_signals(current); /* just in case */
6716 wait_event_interruptible(mddev->recovery_wait,
6717 mddev->resync_max > j
6718 || kthread_should_stop());
6719 }
NeilBrownacb180b2009-04-14 16:28:34 +10006720
6721 if (kthread_should_stop())
6722 goto interrupted;
6723
NeilBrown57afd892005-06-21 17:17:13 -07006724 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006725 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006726 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006727 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006728 goto out;
6729 }
NeilBrown57afd892005-06-21 17:17:13 -07006730
6731 if (!skipped) { /* actual IO requested */
6732 io_sectors += sectors;
6733 atomic_add(sectors, &mddev->recovery_active);
6734 }
6735
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 j += sectors;
6737 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006738 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006739 if (last_check == 0)
6740 /* this is the earliers that rebuilt will be
6741 * visible in /proc/mdstat
6742 */
6743 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006744
6745 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 continue;
6747
NeilBrown57afd892005-06-21 17:17:13 -07006748 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006749
NeilBrowndfc70642008-05-23 13:04:39 -07006750 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006751 break;
6752
6753 repeat:
6754 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6755 /* step marks */
6756 int next = (last_mark+1) % SYNC_MARKS;
6757
6758 mddev->resync_mark = mark[next];
6759 mddev->resync_mark_cnt = mark_cnt[next];
6760 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006761 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 last_mark = next;
6763 }
6764
6765
NeilBrownc6207272008-02-06 01:39:52 -08006766 if (kthread_should_stop())
6767 goto interrupted;
6768
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769
6770 /*
6771 * this loop exits only if either when we are slower than
6772 * the 'hard' speed limit, or the system was IO-idle for
6773 * a jiffy.
6774 * the system might be non-idle CPU-wise, but we only care
6775 * about not overloading the IO subsystem. (things like an
6776 * e2fsck being done on the RAID array should execute fast)
6777 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006778 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 cond_resched();
6780
NeilBrown57afd892005-06-21 17:17:13 -07006781 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6782 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783
NeilBrown88202a02006-01-06 00:21:36 -08006784 if (currspeed > speed_min(mddev)) {
6785 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006786 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006787 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788 goto repeat;
6789 }
6790 }
6791 }
NeilBrown61df9d92006-10-03 01:15:57 -07006792 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793 /*
6794 * this also signals 'finished resyncing' to md_stop
6795 */
6796 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006797 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
6799 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6800
6801 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006802 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803
NeilBrowndfc70642008-05-23 13:04:39 -07006804 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006805 mddev->curr_resync > 2) {
6806 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6807 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6808 if (mddev->curr_resync >= mddev->recovery_cp) {
6809 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006810 "md: checkpointing %s of %s.\n",
6811 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006812 mddev->recovery_cp = mddev->curr_resync;
6813 }
6814 } else
6815 mddev->recovery_cp = MaxSector;
6816 } else {
6817 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6818 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006819 rcu_read_lock();
6820 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006821 if (rdev->raid_disk >= 0 &&
6822 !test_bit(Faulty, &rdev->flags) &&
6823 !test_bit(In_sync, &rdev->flags) &&
6824 rdev->recovery_offset < mddev->curr_resync)
6825 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006826 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006828 }
NeilBrown17571282006-12-10 02:20:52 -08006829 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006830
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006832 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6833 /* We completed so min/max setting can be forgotten if used. */
6834 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6835 mddev->resync_min = 0;
6836 mddev->resync_max = MaxSector;
6837 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6838 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006839 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006840 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6841 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006842 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 wake_up(&resync_wait);
6844 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6845 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006846 return;
6847
6848 interrupted:
6849 /*
6850 * got a signal, exit.
6851 */
6852 printk(KERN_INFO
6853 "md: md_do_sync() got signal ... exiting\n");
6854 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6855 goto out;
6856
Linus Torvalds1da177e2005-04-16 15:20:36 -07006857}
NeilBrown29269552006-03-27 01:18:10 -08006858EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859
6860
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006861static int remove_and_add_spares(mddev_t *mddev)
6862{
6863 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006864 int spares = 0;
6865
NeilBrown97e4f422009-03-31 14:33:13 +11006866 mddev->curr_resync_completed = 0;
6867
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006868 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006869 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006870 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006871 (test_bit(Faulty, &rdev->flags) ||
6872 ! test_bit(In_sync, &rdev->flags)) &&
6873 atomic_read(&rdev->nr_pending)==0) {
6874 if (mddev->pers->hot_remove_disk(
6875 mddev, rdev->raid_disk)==0) {
6876 char nm[20];
6877 sprintf(nm,"rd%d", rdev->raid_disk);
6878 sysfs_remove_link(&mddev->kobj, nm);
6879 rdev->raid_disk = -1;
6880 }
6881 }
6882
NeilBrown4044ba52009-01-09 08:31:11 +11006883 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006884 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006885 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006886 !test_bit(In_sync, &rdev->flags) &&
6887 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006888 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006889 if (rdev->raid_disk < 0
6890 && !test_bit(Faulty, &rdev->flags)) {
6891 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006892 if (mddev->pers->
6893 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006894 char nm[20];
6895 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006896 if (sysfs_create_link(&mddev->kobj,
6897 &rdev->kobj, nm))
6898 printk(KERN_WARNING
6899 "md: cannot register "
6900 "%s for %s\n",
6901 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006902 spares++;
6903 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006904 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006905 } else
6906 break;
6907 }
NeilBrowndfc70642008-05-23 13:04:39 -07006908 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006909 }
6910 return spares;
6911}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912/*
6913 * This routine is regularly called by all per-raid-array threads to
6914 * deal with generic issues like resync and super-block update.
6915 * Raid personalities that don't have a thread (linear/raid0) do not
6916 * need this as they never do any recovery or update the superblock.
6917 *
6918 * It does not do any resync itself, but rather "forks" off other threads
6919 * to do that as needed.
6920 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6921 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006922 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006923 * and wakeups up this thread which will reap the thread and finish up.
6924 * This thread also removes any faulty devices (with nr_pending == 0).
6925 *
6926 * The overall approach is:
6927 * 1/ if the superblock needs updating, update it.
6928 * 2/ If a recovery thread is running, don't do anything else.
6929 * 3/ If recovery has finished, clean up, possibly marking spares active.
6930 * 4/ If there are any faulty devices, remove them.
6931 * 5/ If array is degraded, try to add spares devices
6932 * 6/ If array has spares or is not in-sync, start a resync thread.
6933 */
6934void md_check_recovery(mddev_t *mddev)
6935{
6936 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006937
6938
NeilBrown5f404022005-06-21 17:17:16 -07006939 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006940 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006941
6942 if (mddev->ro)
6943 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006944
6945 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006946 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006947 printk(KERN_INFO "md: %s in immediate safe mode\n",
6948 mdname(mddev));
6949 mddev->safemode = 2;
6950 }
6951 flush_signals(current);
6952 }
6953
NeilBrownc89a8ee2008-08-05 15:54:13 +10006954 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006957 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006958 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006959 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006960 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006961 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6962 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006963 ))
6964 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006965
NeilBrowndf5b89b2006-03-27 01:18:20 -08006966 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006967 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006968
NeilBrownc89a8ee2008-08-05 15:54:13 +10006969 if (mddev->ro) {
6970 /* Only thing we do on a ro array is remove
6971 * failed devices.
6972 */
6973 remove_and_add_spares(mddev);
6974 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6975 goto unlock;
6976 }
6977
NeilBrown31a59e32008-04-30 00:52:30 -07006978 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006979 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006980 spin_lock_irq(&mddev->write_lock);
6981 if (mddev->safemode &&
6982 !atomic_read(&mddev->writes_pending) &&
6983 !mddev->in_sync &&
6984 mddev->recovery_cp == MaxSector) {
6985 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006986 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006987 if (mddev->persistent)
6988 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6989 }
6990 if (mddev->safemode == 1)
6991 mddev->safemode = 0;
6992 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006993 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006994 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006995 }
NeilBrownfca4d842005-06-21 17:17:11 -07006996
NeilBrown850b2b422006-10-03 01:15:46 -07006997 if (mddev->flags)
6998 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006999
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7001 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7002 /* resync/recovery still happening */
7003 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7004 goto unlock;
7005 }
7006 if (mddev->sync_thread) {
7007 /* resync has finished, collect result */
7008 md_unregister_thread(mddev->sync_thread);
7009 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007010 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7011 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012 /* success...*/
7013 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007014 if (mddev->pers->spare_active(mddev))
7015 sysfs_notify(&mddev->kobj, NULL,
7016 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007017 }
NeilBrowncea9c222009-03-31 15:15:05 +11007018 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7019 mddev->pers->finish_reshape)
7020 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07007021 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007022
7023 /* if array is no-longer degraded, then any saved_raid_disk
7024 * information must be scrapped
7025 */
7026 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007027 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007028 rdev->saved_raid_disk = -1;
7029
Linus Torvalds1da177e2005-04-16 15:20:36 -07007030 mddev->recovery = 0;
7031 /* flag recovery needed just to double check */
7032 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007033 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007034 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035 goto unlock;
7036 }
Neil Brown72a23c22008-06-28 08:31:41 +10007037 /* Set RUNNING before clearing NEEDED to avoid
7038 * any transients in the value of "sync_action".
7039 */
7040 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7041 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007042 /* Clear some bits that don't mean anything, but
7043 * might be left set
7044 */
NeilBrown24dd4692005-11-08 21:39:26 -08007045 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7046 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007048 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7049 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007050 /* no recovery is running.
7051 * remove any failed drives, then
7052 * add spares if possible.
7053 * Spare are also removed and re-added, to allow
7054 * the personality to fail the re-add.
7055 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007057 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007058 if (mddev->pers->check_reshape == NULL ||
7059 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007060 /* Cannot proceed */
7061 goto unlock;
7062 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007063 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007064 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007065 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7066 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007067 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007068 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007069 } else if (mddev->recovery_cp < MaxSector) {
7070 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007071 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007072 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7073 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007074 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007075
Linus Torvalds1da177e2005-04-16 15:20:36 -07007076 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007077 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7078 /* We are adding a device or devices to an array
7079 * which has the bitmap stored on all devices.
7080 * So make sure all bitmap pages get written
7081 */
7082 bitmap_write_all(mddev->bitmap);
7083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007084 mddev->sync_thread = md_register_thread(md_do_sync,
7085 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007086 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087 if (!mddev->sync_thread) {
7088 printk(KERN_ERR "%s: could not start resync"
7089 " thread...\n",
7090 mdname(mddev));
7091 /* leave the spares where they are, it shouldn't hurt */
7092 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007093 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007095 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007096 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007097 }
7098 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007099 if (!mddev->sync_thread) {
7100 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7101 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7102 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007103 if (mddev->sysfs_action)
7104 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 mddev_unlock(mddev);
7107 }
7108}
7109
Dan Williams6bfe0b42008-04-30 00:52:32 -07007110void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7111{
NeilBrown3c0ee632008-10-21 13:25:28 +11007112 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007113 wait_event_timeout(rdev->blocked_wait,
7114 !test_bit(Blocked, &rdev->flags),
7115 msecs_to_jiffies(5000));
7116 rdev_dec_pending(rdev, mddev);
7117}
7118EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7119
Adrian Bunk75c96f82005-05-05 16:16:09 -07007120static int md_notify_reboot(struct notifier_block *this,
7121 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122{
7123 struct list_head *tmp;
7124 mddev_t *mddev;
7125
7126 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7127
7128 printk(KERN_INFO "md: stopping all md devices.\n");
7129
NeilBrown29ac4aa2008-02-06 01:39:58 -08007130 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007131 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007132 /* Force a switch to readonly even array
7133 * appears to still be in use. Hence
7134 * the '100'.
7135 */
NeilBrownd710e132008-10-13 11:55:12 +11007136 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007137 mddev_unlock(mddev);
7138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 /*
7140 * certain more exotic SCSI devices are known to be
7141 * volatile wrt too early system reboots. While the
7142 * right place to handle this issue is the given
7143 * driver, we do want to have a safe RAID driver ...
7144 */
7145 mdelay(1000*1);
7146 }
7147 return NOTIFY_DONE;
7148}
7149
Adrian Bunk75c96f82005-05-05 16:16:09 -07007150static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007151 .notifier_call = md_notify_reboot,
7152 .next = NULL,
7153 .priority = INT_MAX, /* before any real devices */
7154};
7155
7156static void md_geninit(void)
7157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7159
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007160 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161}
7162
Adrian Bunk75c96f82005-05-05 16:16:09 -07007163static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007165 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166 return -1;
7167 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007168 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169 return -1;
7170 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007171 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007172 md_probe, NULL, NULL);
7173 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007174 md_probe, NULL, NULL);
7175
Linus Torvalds1da177e2005-04-16 15:20:36 -07007176 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007177 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178
7179 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007180 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181}
7182
7183
7184#ifndef MODULE
7185
7186/*
7187 * Searches all registered partitions for autorun RAID arrays
7188 * at boot time.
7189 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007190
7191static LIST_HEAD(all_detected_devices);
7192struct detected_devices_node {
7193 struct list_head list;
7194 dev_t dev;
7195};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196
7197void md_autodetect_dev(dev_t dev)
7198{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007199 struct detected_devices_node *node_detected_dev;
7200
7201 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7202 if (node_detected_dev) {
7203 node_detected_dev->dev = dev;
7204 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7205 } else {
7206 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7207 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209}
7210
7211
7212static void autostart_arrays(int part)
7213{
7214 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007215 struct detected_devices_node *node_detected_dev;
7216 dev_t dev;
7217 int i_scanned, i_passed;
7218
7219 i_scanned = 0;
7220 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007221
7222 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7223
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007224 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7225 i_scanned++;
7226 node_detected_dev = list_entry(all_detected_devices.next,
7227 struct detected_devices_node, list);
7228 list_del(&node_detected_dev->list);
7229 dev = node_detected_dev->dev;
7230 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007231 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007232 if (IS_ERR(rdev))
7233 continue;
7234
NeilBrownb2d444d2005-11-08 21:39:31 -08007235 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007236 MD_BUG();
7237 continue;
7238 }
NeilBrownd0fae182008-03-04 14:29:31 -08007239 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007241 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007243
7244 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7245 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007246
7247 autorun_devices(part);
7248}
7249
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007250#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251
7252static __exit void md_exit(void)
7253{
7254 mddev_t *mddev;
7255 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007256
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007257 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007258 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007260 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 unregister_blkdev(mdp_major, "mdp");
7262 unregister_reboot_notifier(&md_notifier);
7263 unregister_sysctl_table(raid_table_header);
7264 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007265 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007266 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007267 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007268 }
7269}
7270
Dan Williams685784a2007-07-09 11:56:42 -07007271subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007272module_exit(md_exit)
7273
NeilBrownf91de922005-11-08 21:39:36 -08007274static int get_ro(char *buffer, struct kernel_param *kp)
7275{
7276 return sprintf(buffer, "%d", start_readonly);
7277}
7278static int set_ro(const char *val, struct kernel_param *kp)
7279{
7280 char *e;
7281 int num = simple_strtoul(val, &e, 10);
7282 if (*val && (*e == '\0' || *e == '\n')) {
7283 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007284 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007285 }
7286 return -EINVAL;
7287}
7288
NeilBrown80ca3a42006-07-10 04:44:18 -07007289module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7290module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007291
NeilBrownefeb53c2009-01-09 08:31:10 +11007292module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007293
Linus Torvalds1da177e2005-04-16 15:20:36 -07007294EXPORT_SYMBOL(register_md_personality);
7295EXPORT_SYMBOL(unregister_md_personality);
7296EXPORT_SYMBOL(md_error);
7297EXPORT_SYMBOL(md_done_sync);
7298EXPORT_SYMBOL(md_write_start);
7299EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007300EXPORT_SYMBOL(md_register_thread);
7301EXPORT_SYMBOL(md_unregister_thread);
7302EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007303EXPORT_SYMBOL(md_check_recovery);
7304MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007305MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007306MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007307MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);