blob: e752332268d2f797c413618f786c70b248cb7da6 [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
NeilBrownfe60b012010-03-29 11:10:42 +11004300static int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301{
NeilBrown2604b702006-01-06 00:20:36 -08004302 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004304 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
NeilBrowna757e642005-04-16 15:26:42 -07004306 if (list_empty(&mddev->disks))
4307 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004308 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
4310 if (mddev->pers)
4311 return -EBUSY;
4312
NeilBrownb6eb1272010-04-15 10:13:47 +10004313 /* These two calls synchronise us with the
4314 * sysfs_remove_group calls in mddev_unlock,
4315 * so they must have completed.
4316 */
4317 mutex_lock(&mddev->open_mutex);
4318 mutex_unlock(&mddev->open_mutex);
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 /*
4321 * Analyze all RAID superblock(s)
4322 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004323 if (!mddev->raid_disks) {
4324 if (!mddev->persistent)
4325 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004326 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
NeilBrownd9d166c2006-01-06 00:20:51 -08004329 if (mddev->level != LEVEL_NONE)
4330 request_module("md-level-%d", mddev->level);
4331 else if (mddev->clevel[0])
4332 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333
4334 /*
4335 * Drop all container device buffers, from now on
4336 * the only valid external interface is through the md
4337 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004339 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004340 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 continue;
4342 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004343 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004344
4345 /* perform some consistency tests on the device.
4346 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004347 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004348 */
Andre Noll0f420352008-07-11 22:02:23 +10004349 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004350 if (mddev->dev_sectors &&
4351 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004352 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004353 printk("md: %s: data overlaps metadata\n",
4354 mdname(mddev));
4355 return -EINVAL;
4356 }
4357 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004358 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004359 > rdev->data_offset) {
4360 printk("md: %s: metadata overlaps data\n",
4361 mdname(mddev));
4362 return -EINVAL;
4363 }
4364 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004365 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 }
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004369 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004370 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004372 if (mddev->level != LEVEL_NONE)
4373 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4374 mddev->level);
4375 else
4376 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4377 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 return -EINVAL;
4379 }
NeilBrown2604b702006-01-06 00:20:36 -08004380 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004382 if (mddev->level != pers->level) {
4383 mddev->level = pers->level;
4384 mddev->new_level = pers->level;
4385 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004386 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
NeilBrownf6705572006-03-27 01:18:11 -08004388 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004389 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004390 /* This personality cannot handle reshaping... */
4391 mddev->pers = NULL;
4392 module_put(pers->owner);
4393 return -EINVAL;
4394 }
4395
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004396 if (pers->sync_request) {
4397 /* Warn if this is a potentially silly
4398 * configuration.
4399 */
4400 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4401 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004402 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004403
4404 list_for_each_entry(rdev, &mddev->disks, same_set)
4405 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004406 if (rdev < rdev2 &&
4407 rdev->bdev->bd_contains ==
4408 rdev2->bdev->bd_contains) {
4409 printk(KERN_WARNING
4410 "%s: WARNING: %s appears to be"
4411 " on the same physical disk as"
4412 " %s.\n",
4413 mdname(mddev),
4414 bdevname(rdev->bdev,b),
4415 bdevname(rdev2->bdev,b2));
4416 warned = 1;
4417 }
4418 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004419
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004420 if (warned)
4421 printk(KERN_WARNING
4422 "True protection against single-disk"
4423 " failure might be compromised.\n");
4424 }
4425
NeilBrown657390d2005-08-26 18:34:16 -07004426 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004427 /* may be over-ridden by personality */
4428 mddev->resync_max_sectors = mddev->dev_sectors;
4429
NeilBrowna9701a32005-11-08 21:39:34 -08004430 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004431 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432
NeilBrown0f9552b52009-12-30 12:08:50 +11004433 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004434 mddev->ro = 2; /* read-only, but switch on first write */
4435
NeilBrownb15c2e52006-01-06 00:20:16 -08004436 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004437 if (err)
4438 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004439 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4440 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4441 " but 'external_size' not in effect?\n", __func__);
4442 printk(KERN_ERR
4443 "md: invalid array_size %llu > default size %llu\n",
4444 (unsigned long long)mddev->array_sectors / 2,
4445 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4446 err = -EINVAL;
4447 mddev->pers->stop(mddev);
4448 }
4449 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004450 err = bitmap_create(mddev);
4451 if (err) {
4452 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4453 mdname(mddev), err);
4454 mddev->pers->stop(mddev);
4455 }
4456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 module_put(mddev->pers->owner);
4459 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004460 bitmap_destroy(mddev);
4461 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004463 if (mddev->pers->sync_request) {
4464 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4465 printk(KERN_WARNING
4466 "md: cannot register extra attributes for %s\n",
4467 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004468 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004469 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004470 mddev->ro = 0;
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004473 atomic_set(&mddev->max_corr_read_errors,
4474 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 mddev->safemode = 0;
4476 mddev->safemode_timer.function = md_safemode_timeout;
4477 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004478 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004480
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004481 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004482 if (rdev->raid_disk >= 0) {
4483 char nm[20];
4484 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004485 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4486 printk("md: cannot register %s for %s\n",
4487 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
4490 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4491
NeilBrown850b2b422006-10-03 01:15:46 -07004492 if (mddev->flags)
4493 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
NeilBrown0b8c9de2006-07-10 04:44:15 -07004495 md_wakeup_thread(mddev->thread);
4496 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004497
NeilBrownd7603b72006-01-06 00:20:30 -08004498 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004499 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004500 if (mddev->sysfs_action)
4501 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004502 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 return 0;
4504}
4505
NeilBrownfe60b012010-03-29 11:10:42 +11004506static int do_md_run(mddev_t *mddev)
4507{
4508 int err;
4509
4510 err = md_run(mddev);
4511 if (err)
4512 goto out;
4513
4514 set_capacity(mddev->gendisk, mddev->array_sectors);
4515 revalidate_disk(mddev->gendisk);
4516 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4517out:
4518 return err;
4519}
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521static int restart_array(mddev_t *mddev)
4522{
4523 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Andre Noll80fab1d2008-07-11 22:02:21 +10004525 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004527 return -ENXIO;
4528 if (!mddev->pers)
4529 return -EINVAL;
4530 if (!mddev->ro)
4531 return -EBUSY;
4532 mddev->safemode = 0;
4533 mddev->ro = 0;
4534 set_disk_ro(disk, 0);
4535 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4536 mdname(mddev));
4537 /* Kick recovery or resync if necessary */
4538 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4539 md_wakeup_thread(mddev->thread);
4540 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004541 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543}
4544
NeilBrownacc55e22006-06-26 00:27:47 -07004545/* similar to deny_write_access, but accounts for our holding a reference
4546 * to the file ourselves */
4547static int deny_bitmap_write_access(struct file * file)
4548{
4549 struct inode *inode = file->f_mapping->host;
4550
4551 spin_lock(&inode->i_lock);
4552 if (atomic_read(&inode->i_writecount) > 1) {
4553 spin_unlock(&inode->i_lock);
4554 return -ETXTBSY;
4555 }
4556 atomic_set(&inode->i_writecount, -1);
4557 spin_unlock(&inode->i_lock);
4558
4559 return 0;
4560}
4561
NeilBrown43a70502009-12-14 12:49:55 +11004562void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004563{
4564 struct inode *inode = file->f_mapping->host;
4565
4566 spin_lock(&inode->i_lock);
4567 atomic_set(&inode->i_writecount, 1);
4568 spin_unlock(&inode->i_lock);
4569}
4570
NeilBrown9e653b62006-06-26 00:27:58 -07004571/* mode:
4572 * 0 - completely stop and dis-assemble array
4573 * 1 - switch to readonly
4574 * 2 - stop but do not disassemble array
4575 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004576static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577{
4578 int err = 0;
4579 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004580 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
NeilBrownc8c00a62009-08-10 12:50:52 +10004582 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004583 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004584 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004585 err = -EBUSY;
4586 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 if (mddev->sync_thread) {
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004589 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4591 md_unregister_thread(mddev->sync_thread);
4592 mddev->sync_thread = NULL;
4593 }
4594
4595 del_timer_sync(&mddev->safemode_timer);
4596
NeilBrown9e653b62006-06-26 00:27:58 -07004597 switch(mode) {
4598 case 1: /* readonly */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 err = -ENXIO;
NeilBrownf91de922005-11-08 21:39:36 -08004600 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 goto out;
4602 mddev->ro = 1;
NeilBrown9e653b62006-06-26 00:27:58 -07004603 break;
4604 case 0: /* disassemble */
4605 case 2: /* stop */
NeilBrown6b8b3e82005-08-04 12:53:35 -07004606 bitmap_flush(mddev);
NeilBrowna9701a32005-11-08 21:39:34 -08004607 md_super_wait(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 if (mddev->ro)
4609 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 mddev->pers->stop(mddev);
NeilBrownd1b53802007-02-28 20:11:42 -08004612 mddev->queue->merge_bvec_fn = NULL;
4613 mddev->queue->unplug_fn = NULL;
NeilBrown041ae522007-03-26 21:32:14 -08004614 mddev->queue->backing_dev_info.congested_fn = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 module_put(mddev->pers->owner);
NeilBrowna64c8762010-04-14 17:15:37 +10004616 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4617 mddev->to_remove = &md_redundancy_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 mddev->pers = NULL;
Dan Williams4f54b0e2008-05-23 13:04:37 -07004619 /* tell userspace to handle 'inactive' */
NeilBrownb62b7592008-10-21 13:25:21 +11004620 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004621
NeilBrownc4647292009-05-07 12:51:06 +10004622 list_for_each_entry(rdev, &mddev->disks, same_set)
4623 if (rdev->raid_disk >= 0) {
4624 char nm[20];
4625 sprintf(nm, "rd%d", rdev->raid_disk);
4626 sysfs_remove_link(&mddev->kobj, nm);
4627 }
4628
NeilBrown0d4ca602006-12-10 02:20:44 -08004629 set_capacity(disk, 0);
NeilBrownb821eaa2010-03-29 11:18:15 +11004630 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 if (mddev->ro)
4633 mddev->ro = 0;
4634 }
NeilBrown850b2b422006-10-03 01:15:46 -07004635 if (!mddev->in_sync || mddev->flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 /* mark array as shutdown cleanly */
4637 mddev->in_sync = 1;
NeilBrown850b2b422006-10-03 01:15:46 -07004638 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
NeilBrown9e653b62006-06-26 00:27:58 -07004640 if (mode == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 set_disk_ro(disk, 1);
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004642 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004643 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004645out:
4646 mutex_unlock(&mddev->open_mutex);
4647 if (err)
4648 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 /*
4650 * Free resources if final stop
4651 */
NeilBrown9e653b62006-06-26 00:27:58 -07004652 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4655
NeilBrown978f9462006-02-02 14:28:05 -08004656 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004657 if (mddev->bitmap_info.file) {
4658 restore_bitmap_write_access(mddev->bitmap_info.file);
4659 fput(mddev->bitmap_info.file);
4660 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004661 }
NeilBrownc3d97142009-12-14 12:49:52 +11004662 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 export_array(mddev);
4665
Andre Nollf233ea52008-07-21 17:05:22 +10004666 mddev->array_sectors = 0;
Dan Williamsb522adc2009-03-31 15:00:31 +11004667 mddev->external_size = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004668 mddev->dev_sectors = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004669 mddev->raid_disks = 0;
NeilBrowna94213b2006-06-26 00:28:00 -07004670 mddev->recovery_cp = 0;
Neil Brown5e96ee62008-06-28 08:31:24 +10004671 mddev->resync_min = 0;
NeilBrownc6207272008-02-06 01:39:52 -08004672 mddev->resync_max = MaxSector;
NeilBrown08a02ec2007-05-09 02:35:38 -07004673 mddev->reshape_position = MaxSector;
NeilBrowne6910632008-02-06 01:39:51 -08004674 mddev->external = 0;
NeilBrown1ec4a932008-02-06 01:39:53 -08004675 mddev->persistent = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004676 mddev->level = LEVEL_NONE;
4677 mddev->clevel[0] = 0;
4678 mddev->flags = 0;
4679 mddev->ro = 0;
4680 mddev->metadata_type[0] = 0;
Andre Noll9d8f0362009-06-18 08:45:01 +10004681 mddev->chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004682 mddev->ctime = mddev->utime = 0;
4683 mddev->layout = 0;
4684 mddev->max_disks = 0;
4685 mddev->events = 0;
4686 mddev->delta_disks = 0;
4687 mddev->new_level = LEVEL_NONE;
4688 mddev->new_layout = 0;
Andre Noll664e7c42009-06-18 08:45:27 +10004689 mddev->new_chunk_sectors = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004690 mddev->curr_resync = 0;
4691 mddev->resync_mismatches = 0;
4692 mddev->suspend_lo = mddev->suspend_hi = 0;
4693 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4694 mddev->recovery = 0;
4695 mddev->in_sync = 0;
NeilBrownd897dbf2008-04-30 00:52:29 -07004696 mddev->degraded = 0;
4697 mddev->barriers_work = 0;
4698 mddev->safemode = 0;
NeilBrownc3d97142009-12-14 12:49:52 +11004699 mddev->bitmap_info.offset = 0;
4700 mddev->bitmap_info.default_offset = 0;
NeilBrown42a04b52009-12-14 12:49:53 +11004701 mddev->bitmap_info.chunksize = 0;
4702 mddev->bitmap_info.daemon_sleep = 0;
4703 mddev->bitmap_info.max_write_behind = 0;
NeilBrown934d9c22008-10-28 17:01:23 +11004704 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004705 if (mddev->hold_active == UNTIL_STOP)
4706 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004707
NeilBrowna8a55c32006-06-26 00:27:39 -07004708 } else if (mddev->pers)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 printk(KERN_INFO "md: %s switched to read-only mode.\n",
4710 mdname(mddev));
4711 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004712 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004713 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004714 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 return err;
4716}
4717
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004718#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719static void autorun_array(mddev_t *mddev)
4720{
4721 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 int err;
4723
NeilBrowna757e642005-04-16 15:26:42 -07004724 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
4727 printk(KERN_INFO "md: running: ");
4728
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004729 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 char b[BDEVNAME_SIZE];
4731 printk("<%s>", bdevname(rdev->bdev,b));
4732 }
4733 printk("\n");
4734
NeilBrownd710e132008-10-13 11:55:12 +11004735 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 if (err) {
4737 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004738 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 }
4740}
4741
4742/*
4743 * lets try to run arrays based on all disks that have arrived
4744 * until now. (those are in pending_raid_disks)
4745 *
4746 * the method: pick the first pending disk, collect all disks with
4747 * the same UUID, remove all from the pending list and put them into
4748 * the 'same_array' list. Then order this list based on superblock
4749 * update time (freshest comes first), kick out 'old' disks and
4750 * compare superblocks. If everything's fine then run it.
4751 *
4752 * If "unit" is allocated, then bump its reference count
4753 */
4754static void autorun_devices(int part)
4755{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004756 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 mddev_t *mddev;
4758 char b[BDEVNAME_SIZE];
4759
4760 printk(KERN_INFO "md: autorun ...\n");
4761 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004762 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004764 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 rdev0 = list_entry(pending_raid_disks.next,
4766 mdk_rdev_t, same_set);
4767
4768 printk(KERN_INFO "md: considering %s ...\n",
4769 bdevname(rdev0->bdev,b));
4770 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004771 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 if (super_90_load(rdev, rdev0, 0) >= 0) {
4773 printk(KERN_INFO "md: adding %s ...\n",
4774 bdevname(rdev->bdev,b));
4775 list_move(&rdev->same_set, &candidates);
4776 }
4777 /*
4778 * now we have a set of devices, with all of them having
4779 * mostly sane superblocks. It's time to allocate the
4780 * mddev.
4781 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004782 if (part) {
4783 dev = MKDEV(mdp_major,
4784 rdev0->preferred_minor << MdpMinorShift);
4785 unit = MINOR(dev) >> MdpMinorShift;
4786 } else {
4787 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4788 unit = MINOR(dev);
4789 }
4790 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4792 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4793 break;
4794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795
4796 md_probe(dev, NULL, NULL);
4797 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004798 if (!mddev || !mddev->gendisk) {
4799 if (mddev)
4800 mddev_put(mddev);
4801 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 "md: cannot allocate memory for md drive.\n");
4803 break;
4804 }
4805 if (mddev_lock(mddev))
4806 printk(KERN_WARNING "md: %s locked, cannot run\n",
4807 mdname(mddev));
4808 else if (mddev->raid_disks || mddev->major_version
4809 || !list_empty(&mddev->disks)) {
4810 printk(KERN_WARNING
4811 "md: %s already running, cannot run %s\n",
4812 mdname(mddev), bdevname(rdev0->bdev,b));
4813 mddev_unlock(mddev);
4814 } else {
4815 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004816 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004817 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 list_del_init(&rdev->same_set);
4819 if (bind_rdev_to_array(rdev, mddev))
4820 export_rdev(rdev);
4821 }
4822 autorun_array(mddev);
4823 mddev_unlock(mddev);
4824 }
4825 /* on success, candidates will be empty, on error
4826 * it won't...
4827 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004828 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004829 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 mddev_put(mddev);
4833 }
4834 printk(KERN_INFO "md: ... autorun DONE.\n");
4835}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004836#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838static int get_version(void __user * arg)
4839{
4840 mdu_version_t ver;
4841
4842 ver.major = MD_MAJOR_VERSION;
4843 ver.minor = MD_MINOR_VERSION;
4844 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4845
4846 if (copy_to_user(arg, &ver, sizeof(ver)))
4847 return -EFAULT;
4848
4849 return 0;
4850}
4851
4852static int get_array_info(mddev_t * mddev, void __user * arg)
4853{
4854 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004855 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857
NeilBrowna9f326e2009-09-23 18:06:41 +10004858 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004859 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004861 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 failed++;
4863 else {
4864 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004865 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004866 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867 else
4868 spare++;
4869 }
4870 }
4871
4872 info.major_version = mddev->major_version;
4873 info.minor_version = mddev->minor_version;
4874 info.patch_version = MD_PATCHLEVEL_VERSION;
4875 info.ctime = mddev->ctime;
4876 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004877 info.size = mddev->dev_sectors / 2;
4878 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004879 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880 info.nr_disks = nr;
4881 info.raid_disks = mddev->raid_disks;
4882 info.md_minor = mddev->md_minor;
4883 info.not_persistent= !mddev->persistent;
4884
4885 info.utime = mddev->utime;
4886 info.state = 0;
4887 if (mddev->in_sync)
4888 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004889 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004890 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004891 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 info.working_disks = working;
4893 info.failed_disks = failed;
4894 info.spare_disks = spare;
4895
4896 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004897 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
4899 if (copy_to_user(arg, &info, sizeof(info)))
4900 return -EFAULT;
4901
4902 return 0;
4903}
4904
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004905static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004906{
4907 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4908 char *ptr, *buf = NULL;
4909 int err = -ENOMEM;
4910
Dan Williamsb5470dc2008-06-27 21:44:04 -07004911 if (md_allow_write(mddev))
4912 file = kmalloc(sizeof(*file), GFP_NOIO);
4913 else
4914 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004915
NeilBrown32a76272005-06-21 17:17:14 -07004916 if (!file)
4917 goto out;
4918
4919 /* bitmap disabled, zero the first byte and copy out */
4920 if (!mddev->bitmap || !mddev->bitmap->file) {
4921 file->pathname[0] = '\0';
4922 goto copy_out;
4923 }
4924
4925 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4926 if (!buf)
4927 goto out;
4928
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004929 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4930 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004931 goto out;
4932
4933 strcpy(file->pathname, ptr);
4934
4935copy_out:
4936 err = 0;
4937 if (copy_to_user(arg, file, sizeof(*file)))
4938 err = -EFAULT;
4939out:
4940 kfree(buf);
4941 kfree(file);
4942 return err;
4943}
4944
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945static int get_disk_info(mddev_t * mddev, void __user * arg)
4946{
4947 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948 mdk_rdev_t *rdev;
4949
4950 if (copy_from_user(&info, arg, sizeof(info)))
4951 return -EFAULT;
4952
Andre Noll26ef3792008-07-11 22:02:21 +10004953 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 if (rdev) {
4955 info.major = MAJOR(rdev->bdev->bd_dev);
4956 info.minor = MINOR(rdev->bdev->bd_dev);
4957 info.raid_disk = rdev->raid_disk;
4958 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004959 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004961 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962 info.state |= (1<<MD_DISK_ACTIVE);
4963 info.state |= (1<<MD_DISK_SYNC);
4964 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004965 if (test_bit(WriteMostly, &rdev->flags))
4966 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967 } else {
4968 info.major = info.minor = 0;
4969 info.raid_disk = -1;
4970 info.state = (1<<MD_DISK_REMOVED);
4971 }
4972
4973 if (copy_to_user(arg, &info, sizeof(info)))
4974 return -EFAULT;
4975
4976 return 0;
4977}
4978
4979static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
4980{
4981 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4982 mdk_rdev_t *rdev;
4983 dev_t dev = MKDEV(info->major,info->minor);
4984
4985 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
4986 return -EOVERFLOW;
4987
4988 if (!mddev->raid_disks) {
4989 int err;
4990 /* expecting a device which has a superblock */
4991 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
4992 if (IS_ERR(rdev)) {
4993 printk(KERN_WARNING
4994 "md: md_import_device returned %ld\n",
4995 PTR_ERR(rdev));
4996 return PTR_ERR(rdev);
4997 }
4998 if (!list_empty(&mddev->disks)) {
4999 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5000 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005001 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 .load_super(rdev, rdev0, mddev->minor_version);
5003 if (err < 0) {
5004 printk(KERN_WARNING
5005 "md: %s has different UUID to %s\n",
5006 bdevname(rdev->bdev,b),
5007 bdevname(rdev0->bdev,b2));
5008 export_rdev(rdev);
5009 return -EINVAL;
5010 }
5011 }
5012 err = bind_rdev_to_array(rdev, mddev);
5013 if (err)
5014 export_rdev(rdev);
5015 return err;
5016 }
5017
5018 /*
5019 * add_new_disk can be used once the array is assembled
5020 * to add "hot spares". They must already have a superblock
5021 * written
5022 */
5023 if (mddev->pers) {
5024 int err;
5025 if (!mddev->pers->hot_add_disk) {
5026 printk(KERN_WARNING
5027 "%s: personality does not support diskops!\n",
5028 mdname(mddev));
5029 return -EINVAL;
5030 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005031 if (mddev->persistent)
5032 rdev = md_import_device(dev, mddev->major_version,
5033 mddev->minor_version);
5034 else
5035 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 if (IS_ERR(rdev)) {
5037 printk(KERN_WARNING
5038 "md: md_import_device returned %ld\n",
5039 PTR_ERR(rdev));
5040 return PTR_ERR(rdev);
5041 }
NeilBrown41158c72005-06-21 17:17:25 -07005042 /* set save_raid_disk if appropriate */
5043 if (!mddev->persistent) {
5044 if (info->state & (1<<MD_DISK_SYNC) &&
5045 info->raid_disk < mddev->raid_disks)
5046 rdev->raid_disk = info->raid_disk;
5047 else
5048 rdev->raid_disk = -1;
5049 } else
5050 super_types[mddev->major_version].
5051 validate_super(mddev, rdev);
5052 rdev->saved_raid_disk = rdev->raid_disk;
5053
NeilBrownb2d444d2005-11-08 21:39:31 -08005054 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005055 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5056 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005057 else
5058 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005059
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 rdev->raid_disk = -1;
5061 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005062 if (!err && !mddev->pers->hot_remove_disk) {
5063 /* If there is hot_add_disk but no hot_remove_disk
5064 * then added disks for geometry changes,
5065 * and should be added immediately.
5066 */
5067 super_types[mddev->major_version].
5068 validate_super(mddev, rdev);
5069 err = mddev->pers->hot_add_disk(mddev, rdev);
5070 if (err)
5071 unbind_rdev_from_array(rdev);
5072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 if (err)
5074 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005075 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005076 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005077
NeilBrown17571282006-12-10 02:20:52 -08005078 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005079 if (mddev->degraded)
5080 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005081 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005082 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 return err;
5084 }
5085
5086 /* otherwise, add_new_disk is only allowed
5087 * for major_version==0 superblocks
5088 */
5089 if (mddev->major_version != 0) {
5090 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5091 mdname(mddev));
5092 return -EINVAL;
5093 }
5094
5095 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5096 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005097 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 if (IS_ERR(rdev)) {
5099 printk(KERN_WARNING
5100 "md: error, md_import_device() returned %ld\n",
5101 PTR_ERR(rdev));
5102 return PTR_ERR(rdev);
5103 }
5104 rdev->desc_nr = info->number;
5105 if (info->raid_disk < mddev->raid_disks)
5106 rdev->raid_disk = info->raid_disk;
5107 else
5108 rdev->raid_disk = -1;
5109
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005111 if (info->state & (1<<MD_DISK_SYNC))
5112 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005114 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5115 set_bit(WriteMostly, &rdev->flags);
5116
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 if (!mddev->persistent) {
5118 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005119 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 } else
Andre Noll0f420352008-07-11 22:02:23 +10005121 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005122 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
NeilBrown2bf071b2006-01-06 00:20:55 -08005124 err = bind_rdev_to_array(rdev, mddev);
5125 if (err) {
5126 export_rdev(rdev);
5127 return err;
5128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 }
5130
5131 return 0;
5132}
5133
5134static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5135{
5136 char b[BDEVNAME_SIZE];
5137 mdk_rdev_t *rdev;
5138
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 rdev = find_rdev(mddev, dev);
5140 if (!rdev)
5141 return -ENXIO;
5142
5143 if (rdev->raid_disk >= 0)
5144 goto busy;
5145
5146 kick_rdev_from_array(rdev);
NeilBrown850b2b422006-10-03 01:15:46 -07005147 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005148 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149
5150 return 0;
5151busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005152 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 bdevname(rdev->bdev,b), mdname(mddev));
5154 return -EBUSY;
5155}
5156
5157static int hot_add_disk(mddev_t * mddev, dev_t dev)
5158{
5159 char b[BDEVNAME_SIZE];
5160 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 mdk_rdev_t *rdev;
5162
5163 if (!mddev->pers)
5164 return -ENODEV;
5165
5166 if (mddev->major_version != 0) {
5167 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5168 " version-0 superblocks.\n",
5169 mdname(mddev));
5170 return -EINVAL;
5171 }
5172 if (!mddev->pers->hot_add_disk) {
5173 printk(KERN_WARNING
5174 "%s: personality does not support diskops!\n",
5175 mdname(mddev));
5176 return -EINVAL;
5177 }
5178
NeilBrownd710e132008-10-13 11:55:12 +11005179 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 if (IS_ERR(rdev)) {
5181 printk(KERN_WARNING
5182 "md: error, md_import_device() returned %ld\n",
5183 PTR_ERR(rdev));
5184 return -EINVAL;
5185 }
5186
5187 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005188 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189 else
Andre Noll0f420352008-07-11 22:02:23 +10005190 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191
NeilBrown8190e752009-06-18 08:48:58 +10005192 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193
NeilBrownb2d444d2005-11-08 21:39:31 -08005194 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 printk(KERN_WARNING
5196 "md: can not hot-add faulty %s disk to %s!\n",
5197 bdevname(rdev->bdev,b), mdname(mddev));
5198 err = -EINVAL;
5199 goto abort_export;
5200 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005201 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005203 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005204 err = bind_rdev_to_array(rdev, mddev);
5205 if (err)
5206 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207
5208 /*
5209 * The rest should better be atomic, we can have disk failures
5210 * noticed in interrupt contexts ...
5211 */
5212
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 rdev->raid_disk = -1;
5214
NeilBrown850b2b422006-10-03 01:15:46 -07005215 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216
5217 /*
5218 * Kick recovery, maybe this spare has to be added to the
5219 * array immediately.
5220 */
5221 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5222 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005223 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 return 0;
5225
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226abort_export:
5227 export_rdev(rdev);
5228 return err;
5229}
5230
NeilBrown32a76272005-06-21 17:17:14 -07005231static int set_bitmap_file(mddev_t *mddev, int fd)
5232{
5233 int err;
5234
NeilBrown36fa3062005-09-09 16:23:45 -07005235 if (mddev->pers) {
5236 if (!mddev->pers->quiesce)
5237 return -EBUSY;
5238 if (mddev->recovery || mddev->sync_thread)
5239 return -EBUSY;
5240 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005241 }
5242
NeilBrown36fa3062005-09-09 16:23:45 -07005243
5244 if (fd >= 0) {
5245 if (mddev->bitmap)
5246 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005247 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005248
NeilBrownc3d97142009-12-14 12:49:52 +11005249 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005250 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5251 mdname(mddev));
5252 return -EBADF;
5253 }
5254
NeilBrownc3d97142009-12-14 12:49:52 +11005255 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005256 if (err) {
5257 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5258 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005259 fput(mddev->bitmap_info.file);
5260 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005261 return err;
5262 }
NeilBrownc3d97142009-12-14 12:49:52 +11005263 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005264 } else if (mddev->bitmap == NULL)
5265 return -ENOENT; /* cannot remove what isn't there */
5266 err = 0;
5267 if (mddev->pers) {
5268 mddev->pers->quiesce(mddev, 1);
5269 if (fd >= 0)
5270 err = bitmap_create(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005271 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005272 bitmap_destroy(mddev);
NeilBrownd7375ab2006-06-26 00:27:43 -07005273 fd = -1; /* make sure to put the file */
5274 }
NeilBrown36fa3062005-09-09 16:23:45 -07005275 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab2006-06-26 00:27:43 -07005276 }
5277 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005278 if (mddev->bitmap_info.file) {
5279 restore_bitmap_write_access(mddev->bitmap_info.file);
5280 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005281 }
NeilBrownc3d97142009-12-14 12:49:52 +11005282 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005283 }
5284
NeilBrown32a76272005-06-21 17:17:14 -07005285 return err;
5286}
5287
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288/*
5289 * set_array_info is used two different ways
5290 * The original usage is when creating a new array.
5291 * In this usage, raid_disks is > 0 and it together with
5292 * level, size, not_persistent,layout,chunksize determine the
5293 * shape of the array.
5294 * This will always create an array with a type-0.90.0 superblock.
5295 * The newer usage is when assembling an array.
5296 * In this case raid_disks will be 0, and the major_version field is
5297 * use to determine which style super-blocks are to be found on the devices.
5298 * The minor and patch _version numbers are also kept incase the
5299 * super_block handler wishes to interpret them.
5300 */
5301static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5302{
5303
5304 if (info->raid_disks == 0) {
5305 /* just setting version number for superblock loading */
5306 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005307 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 super_types[info->major_version].name == NULL) {
5309 /* maybe try to auto-load a module? */
5310 printk(KERN_INFO
5311 "md: superblock version %d not known\n",
5312 info->major_version);
5313 return -EINVAL;
5314 }
5315 mddev->major_version = info->major_version;
5316 mddev->minor_version = info->minor_version;
5317 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005318 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005319 /* ensure mddev_put doesn't delete this now that there
5320 * is some minimal configuration.
5321 */
5322 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 return 0;
5324 }
5325 mddev->major_version = MD_MAJOR_VERSION;
5326 mddev->minor_version = MD_MINOR_VERSION;
5327 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5328 mddev->ctime = get_seconds();
5329
5330 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005331 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005332 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 mddev->raid_disks = info->raid_disks;
5334 /* don't set md_minor, it is determined by which /dev/md* was
5335 * openned
5336 */
5337 if (info->state & (1<<MD_SB_CLEAN))
5338 mddev->recovery_cp = MaxSector;
5339 else
5340 mddev->recovery_cp = 0;
5341 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005342 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343
5344 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005345 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346
5347 mddev->max_disks = MD_SB_DISKS;
5348
NeilBrowne6910632008-02-06 01:39:51 -08005349 if (mddev->persistent)
5350 mddev->flags = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07005351 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
NeilBrownc3d97142009-12-14 12:49:52 +11005353 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5354 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005355
NeilBrownf6705572006-03-27 01:18:11 -08005356 mddev->reshape_position = MaxSector;
5357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 /*
5359 * Generate a 128 bit UUID
5360 */
5361 get_random_bytes(mddev->uuid, 16);
5362
NeilBrownf6705572006-03-27 01:18:11 -08005363 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005364 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005365 mddev->new_layout = mddev->layout;
5366 mddev->delta_disks = 0;
5367
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 return 0;
5369}
5370
Dan Williams1f403622009-03-31 14:59:03 +11005371void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5372{
Dan Williamsb522adc2009-03-31 15:00:31 +11005373 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5374
5375 if (mddev->external_size)
5376 return;
5377
Dan Williams1f403622009-03-31 14:59:03 +11005378 mddev->array_sectors = array_sectors;
5379}
5380EXPORT_SYMBOL(md_set_array_sectors);
5381
Andre Nolld71f9f82008-07-11 22:02:22 +10005382static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005383{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005384 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005385 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005386 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005387
5388 if (mddev->pers->resize == NULL)
5389 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005390 /* The "num_sectors" is the number of sectors of each device that
5391 * is used. This can only make sense for arrays with redundancy.
5392 * linear and raid0 always use whatever space is available. We can only
5393 * consider changing this number if no resync or reconstruction is
5394 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005395 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005396 * of each device. If num_sectors is zero, we find the largest size
5397 * that fits.
5398
NeilBrowna35b0d62006-01-06 00:20:49 -08005399 */
5400 if (mddev->sync_thread)
5401 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005402 if (mddev->bitmap)
5403 /* Sorry, cannot grow a bitmap yet, just remove it,
5404 * grow, and re-add.
5405 */
5406 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005407 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005408 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005409
Andre Nolld71f9f82008-07-11 22:02:22 +10005410 if (fit && (num_sectors == 0 || num_sectors > avail))
5411 num_sectors = avail;
5412 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005413 return -ENOSPC;
5414 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005415 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005416 if (!rv)
5417 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005418 return rv;
5419}
5420
NeilBrownda943b992006-01-06 00:20:54 -08005421static int update_raid_disks(mddev_t *mddev, int raid_disks)
5422{
5423 int rv;
5424 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005425 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005426 return -EINVAL;
5427 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005428 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005429 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005430 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005431 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005432 mddev->delta_disks = raid_disks - mddev->raid_disks;
5433
5434 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005435 return rv;
5436}
5437
5438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439/*
5440 * update_array_info is used to change the configuration of an
5441 * on-line array.
5442 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5443 * fields in the info are checked against the array.
5444 * Any differences that cannot be handled will cause an error.
5445 * Normally, only one change can be managed at a time.
5446 */
5447static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5448{
5449 int rv = 0;
5450 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005451 int state = 0;
5452
5453 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005454 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005455 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456
5457 if (mddev->major_version != info->major_version ||
5458 mddev->minor_version != info->minor_version ||
5459/* mddev->patch_version != info->patch_version || */
5460 mddev->ctime != info->ctime ||
5461 mddev->level != info->level ||
5462/* mddev->layout != info->layout || */
5463 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005464 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005465 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5466 ((state^info->state) & 0xfffffe00)
5467 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 return -EINVAL;
5469 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005470 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5471 cnt++;
5472 if (mddev->raid_disks != info->raid_disks)
5473 cnt++;
5474 if (mddev->layout != info->layout)
5475 cnt++;
5476 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5477 cnt++;
5478 if (cnt == 0)
5479 return 0;
5480 if (cnt > 1)
5481 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482
5483 if (mddev->layout != info->layout) {
5484 /* Change layout
5485 * we don't need to do anything at the md level, the
5486 * personality will take care of it all.
5487 */
NeilBrown50ac1682009-06-18 08:47:55 +10005488 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005490 else {
5491 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005492 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005493 if (rv)
5494 mddev->new_layout = mddev->layout;
5495 return rv;
5496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005498 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005499 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500
NeilBrownda943b992006-01-06 00:20:54 -08005501 if (mddev->raid_disks != info->raid_disks)
5502 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503
NeilBrown36fa3062005-09-09 16:23:45 -07005504 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5505 if (mddev->pers->quiesce == NULL)
5506 return -EINVAL;
5507 if (mddev->recovery || mddev->sync_thread)
5508 return -EBUSY;
5509 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5510 /* add the bitmap */
5511 if (mddev->bitmap)
5512 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005513 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005514 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005515 mddev->bitmap_info.offset =
5516 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005517 mddev->pers->quiesce(mddev, 1);
5518 rv = bitmap_create(mddev);
5519 if (rv)
5520 bitmap_destroy(mddev);
5521 mddev->pers->quiesce(mddev, 0);
5522 } else {
5523 /* remove the bitmap */
5524 if (!mddev->bitmap)
5525 return -ENOENT;
5526 if (mddev->bitmap->file)
5527 return -EINVAL;
5528 mddev->pers->quiesce(mddev, 1);
5529 bitmap_destroy(mddev);
5530 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005531 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005532 }
5533 }
NeilBrown850b2b422006-10-03 01:15:46 -07005534 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 return rv;
5536}
5537
5538static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5539{
5540 mdk_rdev_t *rdev;
5541
5542 if (mddev->pers == NULL)
5543 return -ENODEV;
5544
5545 rdev = find_rdev(mddev, dev);
5546 if (!rdev)
5547 return -ENODEV;
5548
5549 md_error(mddev, rdev);
5550 return 0;
5551}
5552
Andre Noll2f9618c2008-04-25 18:57:58 +02005553/*
5554 * We have a problem here : there is no easy way to give a CHS
5555 * virtual geometry. We currently pretend that we have a 2 heads
5556 * 4 sectors (with a BIG number of cylinders...). This drives
5557 * dosfs just mad... ;-)
5558 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005559static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5560{
5561 mddev_t *mddev = bdev->bd_disk->private_data;
5562
5563 geo->heads = 2;
5564 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005565 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005566 return 0;
5567}
5568
Al Viroa39907f2008-03-02 10:31:15 -05005569static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 unsigned int cmd, unsigned long arg)
5571{
5572 int err = 0;
5573 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005575 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576
5577 if (!capable(CAP_SYS_ADMIN))
5578 return -EACCES;
5579
5580 /*
5581 * Commands dealing with the RAID driver but not any
5582 * particular array:
5583 */
5584 switch (cmd)
5585 {
5586 case RAID_VERSION:
5587 err = get_version(argp);
5588 goto done;
5589
5590 case PRINT_RAID_DEBUG:
5591 err = 0;
5592 md_print_devices();
5593 goto done;
5594
5595#ifndef MODULE
5596 case RAID_AUTORUN:
5597 err = 0;
5598 autostart_arrays(arg);
5599 goto done;
5600#endif
5601 default:;
5602 }
5603
5604 /*
5605 * Commands creating/starting a new array:
5606 */
5607
Al Viroa39907f2008-03-02 10:31:15 -05005608 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609
5610 if (!mddev) {
5611 BUG();
5612 goto abort;
5613 }
5614
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 err = mddev_lock(mddev);
5616 if (err) {
5617 printk(KERN_INFO
5618 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5619 err, cmd);
5620 goto abort;
5621 }
5622
5623 switch (cmd)
5624 {
5625 case SET_ARRAY_INFO:
5626 {
5627 mdu_array_info_t info;
5628 if (!arg)
5629 memset(&info, 0, sizeof(info));
5630 else if (copy_from_user(&info, argp, sizeof(info))) {
5631 err = -EFAULT;
5632 goto abort_unlock;
5633 }
5634 if (mddev->pers) {
5635 err = update_array_info(mddev, &info);
5636 if (err) {
5637 printk(KERN_WARNING "md: couldn't update"
5638 " array info. %d\n", err);
5639 goto abort_unlock;
5640 }
5641 goto done_unlock;
5642 }
5643 if (!list_empty(&mddev->disks)) {
5644 printk(KERN_WARNING
5645 "md: array %s already has disks!\n",
5646 mdname(mddev));
5647 err = -EBUSY;
5648 goto abort_unlock;
5649 }
5650 if (mddev->raid_disks) {
5651 printk(KERN_WARNING
5652 "md: array %s already initialised!\n",
5653 mdname(mddev));
5654 err = -EBUSY;
5655 goto abort_unlock;
5656 }
5657 err = set_array_info(mddev, &info);
5658 if (err) {
5659 printk(KERN_WARNING "md: couldn't set"
5660 " array info. %d\n", err);
5661 goto abort_unlock;
5662 }
5663 }
5664 goto done_unlock;
5665
5666 default:;
5667 }
5668
5669 /*
5670 * Commands querying/configuring an existing array:
5671 */
NeilBrown32a76272005-06-21 17:17:14 -07005672 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005673 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005674 if ((!mddev->raid_disks && !mddev->external)
5675 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5676 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5677 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 err = -ENODEV;
5679 goto abort_unlock;
5680 }
5681
5682 /*
5683 * Commands even a read-only array can execute:
5684 */
5685 switch (cmd)
5686 {
5687 case GET_ARRAY_INFO:
5688 err = get_array_info(mddev, argp);
5689 goto done_unlock;
5690
NeilBrown32a76272005-06-21 17:17:14 -07005691 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005692 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005693 goto done_unlock;
5694
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 case GET_DISK_INFO:
5696 err = get_disk_info(mddev, argp);
5697 goto done_unlock;
5698
5699 case RESTART_ARRAY_RW:
5700 err = restart_array(mddev);
5701 goto done_unlock;
5702
5703 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005704 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 goto done_unlock;
5706
5707 case STOP_ARRAY_RO:
NeilBrownd710e132008-10-13 11:55:12 +11005708 err = do_md_stop(mddev, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 goto done_unlock;
5710
Dan Williamse2218352010-05-12 08:25:37 +10005711 case BLKROSET:
5712 if (get_user(ro, (int __user *)(arg))) {
5713 err = -EFAULT;
5714 goto done_unlock;
5715 }
5716 err = -EINVAL;
5717
5718 /* if the bdev is going readonly the value of mddev->ro
5719 * does not matter, no writes are coming
5720 */
5721 if (ro)
5722 goto done_unlock;
5723
5724 /* are we are already prepared for writes? */
5725 if (mddev->ro != 1)
5726 goto done_unlock;
5727
5728 /* transitioning to readauto need only happen for
5729 * arrays that call md_write_start
5730 */
5731 if (mddev->pers) {
5732 err = restart_array(mddev);
5733 if (err == 0) {
5734 mddev->ro = 2;
5735 set_disk_ro(mddev->gendisk, 0);
5736 }
5737 }
5738 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 }
5740
5741 /*
5742 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005743 * superblock, so we do not allow them on read-only arrays.
5744 * However non-MD ioctls (e.g. get-size) will still come through
5745 * here and hit the 'default' below, so only disallow
5746 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005748 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005749 if (mddev->ro == 2) {
5750 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005751 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005752 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5753 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005754 } else {
5755 err = -EROFS;
5756 goto abort_unlock;
5757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 }
5759
5760 switch (cmd)
5761 {
5762 case ADD_NEW_DISK:
5763 {
5764 mdu_disk_info_t info;
5765 if (copy_from_user(&info, argp, sizeof(info)))
5766 err = -EFAULT;
5767 else
5768 err = add_new_disk(mddev, &info);
5769 goto done_unlock;
5770 }
5771
5772 case HOT_REMOVE_DISK:
5773 err = hot_remove_disk(mddev, new_decode_dev(arg));
5774 goto done_unlock;
5775
5776 case HOT_ADD_DISK:
5777 err = hot_add_disk(mddev, new_decode_dev(arg));
5778 goto done_unlock;
5779
5780 case SET_DISK_FAULTY:
5781 err = set_disk_faulty(mddev, new_decode_dev(arg));
5782 goto done_unlock;
5783
5784 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005785 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 goto done_unlock;
5787
NeilBrown32a76272005-06-21 17:17:14 -07005788 case SET_BITMAP_FILE:
5789 err = set_bitmap_file(mddev, (int)arg);
5790 goto done_unlock;
5791
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 err = -EINVAL;
5794 goto abort_unlock;
5795 }
5796
5797done_unlock:
5798abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005799 if (mddev->hold_active == UNTIL_IOCTL &&
5800 err != -EINVAL)
5801 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 mddev_unlock(mddev);
5803
5804 return err;
5805done:
5806 if (err)
5807 MD_BUG();
5808abort:
5809 return err;
5810}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005811#ifdef CONFIG_COMPAT
5812static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5813 unsigned int cmd, unsigned long arg)
5814{
5815 switch (cmd) {
5816 case HOT_REMOVE_DISK:
5817 case HOT_ADD_DISK:
5818 case SET_DISK_FAULTY:
5819 case SET_BITMAP_FILE:
5820 /* These take in integer arg, do not convert */
5821 break;
5822 default:
5823 arg = (unsigned long)compat_ptr(arg);
5824 break;
5825 }
5826
5827 return md_ioctl(bdev, mode, cmd, arg);
5828}
5829#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
Al Viroa39907f2008-03-02 10:31:15 -05005831static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832{
5833 /*
5834 * Succeed if we can lock the mddev, which confirms that
5835 * it isn't being stopped right now.
5836 */
NeilBrownd3374822009-01-09 08:31:10 +11005837 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005838 int err;
5839
NeilBrownd3374822009-01-09 08:31:10 +11005840 if (mddev->gendisk != bdev->bd_disk) {
5841 /* we are racing with mddev_put which is discarding this
5842 * bd_disk.
5843 */
5844 mddev_put(mddev);
5845 /* Wait until bdev->bd_disk is definitely gone */
5846 flush_scheduled_work();
5847 /* Then retry the open from the top */
5848 return -ERESTARTSYS;
5849 }
5850 BUG_ON(mddev != bdev->bd_disk->private_data);
5851
NeilBrownc8c00a62009-08-10 12:50:52 +10005852 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 goto out;
5854
5855 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005856 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005857 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
Linus Torvalds1da177e2005-04-16 15:20:36 -07005859 out:
5860 return err;
5861}
5862
Al Viroa39907f2008-03-02 10:31:15 -05005863static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864{
Al Viroa39907f2008-03-02 10:31:15 -05005865 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005867 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005868 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 mddev_put(mddev);
5870
5871 return 0;
5872}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005873static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874{
5875 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005876 .open = md_open,
5877 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005878 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005879#ifdef CONFIG_COMPAT
5880 .compat_ioctl = md_compat_ioctl,
5881#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005882 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883};
5884
Adrian Bunk75c96f82005-05-05 16:16:09 -07005885static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886{
5887 mdk_thread_t *thread = arg;
5888
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 /*
5890 * md_thread is a 'system-thread', it's priority should be very
5891 * high. We avoid resource deadlocks individually in each
5892 * raid personality. (RAID5 does preallocation) We also use RR and
5893 * the very same RT priority as kswapd, thus we will never get
5894 * into a priority inversion deadlock.
5895 *
5896 * we definitely have to have equal or higher priority than
5897 * bdflush, otherwise bdflush will deadlock if there are too
5898 * many dirty RAID5 blocks.
5899 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900
NeilBrown6985c432005-10-19 21:23:47 -07005901 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005902 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
NeilBrown93588e22005-11-15 00:09:12 -08005904 /* We need to wait INTERRUPTIBLE so that
5905 * we don't add to the load-average.
5906 * That means we need to be sure no signals are
5907 * pending
5908 */
5909 if (signal_pending(current))
5910 flush_signals(current);
5911
5912 wait_event_interruptible_timeout
5913 (thread->wqueue,
5914 test_bit(THREAD_WAKEUP, &thread->flags)
5915 || kthread_should_stop(),
5916 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917
5918 clear_bit(THREAD_WAKEUP, &thread->flags);
5919
NeilBrown787453c2005-11-08 21:39:43 -08005920 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005922
Linus Torvalds1da177e2005-04-16 15:20:36 -07005923 return 0;
5924}
5925
5926void md_wakeup_thread(mdk_thread_t *thread)
5927{
5928 if (thread) {
5929 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5930 set_bit(THREAD_WAKEUP, &thread->flags);
5931 wake_up(&thread->wqueue);
5932 }
5933}
5934
5935mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5936 const char *name)
5937{
5938 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005939
NeilBrown9ffae0c2006-01-06 00:20:32 -08005940 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 if (!thread)
5942 return NULL;
5943
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 init_waitqueue_head(&thread->wqueue);
5945
Linus Torvalds1da177e2005-04-16 15:20:36 -07005946 thread->run = run;
5947 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005948 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005949 thread->tsk = kthread_run(md_thread, thread,
5950 "%s_%s",
5951 mdname(thread->mddev),
5952 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005953 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 kfree(thread);
5955 return NULL;
5956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957 return thread;
5958}
5959
Linus Torvalds1da177e2005-04-16 15:20:36 -07005960void md_unregister_thread(mdk_thread_t *thread)
5961{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005962 if (!thread)
5963 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005964 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005965
5966 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 kfree(thread);
5968}
5969
5970void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
5971{
5972 if (!mddev) {
5973 MD_BUG();
5974 return;
5975 }
5976
NeilBrownb2d444d2005-11-08 21:39:31 -08005977 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07005979
5980 if (mddev->external)
5981 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07005982/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005983 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
5984 mdname(mddev),
5985 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
5986 __builtin_return_address(0),__builtin_return_address(1),
5987 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07005988*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07005989 if (!mddev->pers)
5990 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 if (!mddev->pers->error_handler)
5992 return;
5993 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10005994 if (mddev->degraded)
5995 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11005996 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5998 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5999 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07006000 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006001}
6002
6003/* seq_file implementation /proc/mdstat */
6004
6005static void status_unused(struct seq_file *seq)
6006{
6007 int i = 0;
6008 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009
6010 seq_printf(seq, "unused devices: ");
6011
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006012 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013 char b[BDEVNAME_SIZE];
6014 i++;
6015 seq_printf(seq, "%s ",
6016 bdevname(rdev->bdev,b));
6017 }
6018 if (!i)
6019 seq_printf(seq, "<none>");
6020
6021 seq_printf(seq, "\n");
6022}
6023
6024
6025static void status_resync(struct seq_file *seq, mddev_t * mddev)
6026{
NeilBrowndd71cf62009-05-07 12:49:35 +10006027 sector_t max_sectors, resync, res;
6028 unsigned long dt, db;
6029 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006030 int scale;
6031 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032
NeilBrowndd71cf62009-05-07 12:49:35 +10006033 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
6035 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006036 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006038 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006039
6040 /*
6041 * Should not happen.
6042 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006043 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 MD_BUG();
6045 return;
6046 }
NeilBrown4588b422006-03-27 01:18:04 -08006047 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006048 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006049 * u32, as those are the requirements for sector_div.
6050 * Thus 'scale' must be at least 10
6051 */
6052 scale = 10;
6053 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006054 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006055 scale++;
6056 }
6057 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006058 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006059
6060 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 {
NeilBrown4588b422006-03-27 01:18:04 -08006062 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063 seq_printf(seq, "[");
6064 for (i = 0; i < x; i++)
6065 seq_printf(seq, "=");
6066 seq_printf(seq, ">");
6067 for (i = 0; i < y; i++)
6068 seq_printf(seq, ".");
6069 seq_printf(seq, "] ");
6070 }
NeilBrown4588b422006-03-27 01:18:04 -08006071 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006072 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6073 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006074 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6075 "check" :
6076 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6077 "resync" : "recovery"))),
6078 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006079 (unsigned long long) resync/2,
6080 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081
6082 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006083 * dt: time from mark until now
6084 * db: blocks written from mark until now
6085 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006086 *
6087 * rt is a sector_t, so could be 32bit or 64bit.
6088 * So we divide before multiply in case it is 32bit and close
6089 * to the limit.
6090 * We scale the divisor (db) by 32 to avoid loosing precision
6091 * near the end of resync when the number of remaining sectors
6092 * is close to 'db'.
6093 * We then divide rt by 32 after multiplying by db to compensate.
6094 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006095 */
6096 dt = ((jiffies - mddev->resync_mark) / HZ);
6097 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006098 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6099 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006100
NeilBrowndd71cf62009-05-07 12:49:35 +10006101 rt = max_sectors - resync; /* number of remaining sectors */
6102 sector_div(rt, db/32+1);
6103 rt *= dt;
6104 rt >>= 5;
6105
6106 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6107 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108
NeilBrownff4e8d92006-07-10 04:44:16 -07006109 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006110}
6111
6112static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6113{
6114 struct list_head *tmp;
6115 loff_t l = *pos;
6116 mddev_t *mddev;
6117
6118 if (l >= 0x10000)
6119 return NULL;
6120 if (!l--)
6121 /* header */
6122 return (void*)1;
6123
6124 spin_lock(&all_mddevs_lock);
6125 list_for_each(tmp,&all_mddevs)
6126 if (!l--) {
6127 mddev = list_entry(tmp, mddev_t, all_mddevs);
6128 mddev_get(mddev);
6129 spin_unlock(&all_mddevs_lock);
6130 return mddev;
6131 }
6132 spin_unlock(&all_mddevs_lock);
6133 if (!l--)
6134 return (void*)2;/* tail */
6135 return NULL;
6136}
6137
6138static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6139{
6140 struct list_head *tmp;
6141 mddev_t *next_mddev, *mddev = v;
6142
6143 ++*pos;
6144 if (v == (void*)2)
6145 return NULL;
6146
6147 spin_lock(&all_mddevs_lock);
6148 if (v == (void*)1)
6149 tmp = all_mddevs.next;
6150 else
6151 tmp = mddev->all_mddevs.next;
6152 if (tmp != &all_mddevs)
6153 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6154 else {
6155 next_mddev = (void*)2;
6156 *pos = 0x10000;
6157 }
6158 spin_unlock(&all_mddevs_lock);
6159
6160 if (v != (void*)1)
6161 mddev_put(mddev);
6162 return next_mddev;
6163
6164}
6165
6166static void md_seq_stop(struct seq_file *seq, void *v)
6167{
6168 mddev_t *mddev = v;
6169
6170 if (mddev && v != (void*)1 && v != (void*)2)
6171 mddev_put(mddev);
6172}
6173
NeilBrownd7603b72006-01-06 00:20:30 -08006174struct mdstat_info {
6175 int event;
6176};
6177
Linus Torvalds1da177e2005-04-16 15:20:36 -07006178static int md_seq_show(struct seq_file *seq, void *v)
6179{
6180 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006181 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006183 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006184 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006185
6186 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006187 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006188 seq_printf(seq, "Personalities : ");
6189 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006190 list_for_each_entry(pers, &pers_list, list)
6191 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192
6193 spin_unlock(&pers_lock);
6194 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006195 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006196 return 0;
6197 }
6198 if (v == (void*)2) {
6199 status_unused(seq);
6200 return 0;
6201 }
6202
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006203 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006205
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6207 seq_printf(seq, "%s : %sactive", mdname(mddev),
6208 mddev->pers ? "" : "in");
6209 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006210 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006212 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006213 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 seq_printf(seq, " %s", mddev->pers->name);
6215 }
6216
Andre Nolldd8ac332009-03-31 14:33:13 +11006217 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006218 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 char b[BDEVNAME_SIZE];
6220 seq_printf(seq, " %s[%d]",
6221 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006222 if (test_bit(WriteMostly, &rdev->flags))
6223 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006224 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006225 seq_printf(seq, "(F)");
6226 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006227 } else if (rdev->raid_disk < 0)
6228 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006229 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006230 }
6231
6232 if (!list_empty(&mddev->disks)) {
6233 if (mddev->pers)
6234 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006235 (unsigned long long)
6236 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 else
6238 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006239 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006241 if (mddev->persistent) {
6242 if (mddev->major_version != 0 ||
6243 mddev->minor_version != 90) {
6244 seq_printf(seq," super %d.%d",
6245 mddev->major_version,
6246 mddev->minor_version);
6247 }
NeilBrowne6910632008-02-06 01:39:51 -08006248 } else if (mddev->external)
6249 seq_printf(seq, " super external:%s",
6250 mddev->metadata_type);
6251 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006252 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253
6254 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006255 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006257 if (mddev->pers->sync_request) {
6258 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006259 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006260 seq_printf(seq, "\n ");
6261 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6262 seq_printf(seq, "\tresync=DELAYED\n ");
6263 else if (mddev->recovery_cp < MaxSector)
6264 seq_printf(seq, "\tresync=PENDING\n ");
6265 }
NeilBrown32a76272005-06-21 17:17:14 -07006266 } else
6267 seq_printf(seq, "\n ");
6268
6269 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006270 unsigned long chunk_kb;
6271 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006272 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006273 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006274 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6275 "%lu%s chunk",
6276 bitmap->pages - bitmap->missing_pages,
6277 bitmap->pages,
6278 (bitmap->pages - bitmap->missing_pages)
6279 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006280 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006281 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006282 if (bitmap->file) {
6283 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006284 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006285 }
NeilBrown78d742d2005-06-21 17:17:15 -07006286
NeilBrown32a76272005-06-21 17:17:14 -07006287 seq_printf(seq, "\n");
6288 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 }
6290
6291 seq_printf(seq, "\n");
6292 }
6293 mddev_unlock(mddev);
6294
6295 return 0;
6296}
6297
Jan Engelhardt110518b2009-05-07 12:49:37 +10006298static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299 .start = md_seq_start,
6300 .next = md_seq_next,
6301 .stop = md_seq_stop,
6302 .show = md_seq_show,
6303};
6304
6305static int md_seq_open(struct inode *inode, struct file *file)
6306{
6307 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006308 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6309 if (mi == NULL)
6310 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006311
6312 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006313 if (error)
6314 kfree(mi);
6315 else {
6316 struct seq_file *p = file->private_data;
6317 p->private = mi;
6318 mi->event = atomic_read(&md_event_count);
6319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 return error;
6321}
6322
NeilBrownd7603b72006-01-06 00:20:30 -08006323static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6324{
6325 struct seq_file *m = filp->private_data;
6326 struct mdstat_info *mi = m->private;
6327 int mask;
6328
6329 poll_wait(filp, &md_event_waiters, wait);
6330
6331 /* always allow read */
6332 mask = POLLIN | POLLRDNORM;
6333
6334 if (mi->event != atomic_read(&md_event_count))
6335 mask |= POLLERR | POLLPRI;
6336 return mask;
6337}
6338
Arjan van de Venfa027c22007-02-12 00:55:33 -08006339static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006340 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 .open = md_seq_open,
6342 .read = seq_read,
6343 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006344 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006345 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346};
6347
NeilBrown2604b702006-01-06 00:20:36 -08006348int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006349{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006351 list_add_tail(&p->list, &pers_list);
6352 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 spin_unlock(&pers_lock);
6354 return 0;
6355}
6356
NeilBrown2604b702006-01-06 00:20:36 -08006357int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358{
NeilBrown2604b702006-01-06 00:20:36 -08006359 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006361 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 spin_unlock(&pers_lock);
6363 return 0;
6364}
6365
NeilBrowneea1bf32009-03-31 14:27:02 +11006366static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367{
6368 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006370 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371
6372 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006373 rcu_read_lock();
6374 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006376 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6377 (int)part_stat_read(&disk->part0, sectors[1]) -
6378 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006379 /* sync IO will cause sync_io to increase before the disk_stats
6380 * as sync_io is counted when a request starts, and
6381 * disk_stats is counted when it completes.
6382 * So resync activity will cause curr_events to be smaller than
6383 * when there was no such activity.
6384 * non-sync IO will cause disk_stat to increase without
6385 * increasing sync_io so curr_events will (eventually)
6386 * be larger than it was before. Once it becomes
6387 * substantially larger, the test below will cause
6388 * the array to appear non-idle, and resync will slow
6389 * down.
6390 * If there is a lot of outstanding resync activity when
6391 * we set last_event to curr_events, then all that activity
6392 * completing might cause the array to appear non-idle
6393 * and resync will be slowed down even though there might
6394 * not have been non-resync activity. This will only
6395 * happen once though. 'last_events' will soon reflect
6396 * the state where there is little or no outstanding
6397 * resync requests, and further resync activity will
6398 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006399 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006401 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402 rdev->last_events = curr_events;
6403 idle = 0;
6404 }
6405 }
NeilBrown4b809912008-07-21 17:05:25 +10006406 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006407 return idle;
6408}
6409
6410void md_done_sync(mddev_t *mddev, int blocks, int ok)
6411{
6412 /* another "blocks" (512byte) blocks have been synced */
6413 atomic_sub(blocks, &mddev->recovery_active);
6414 wake_up(&mddev->recovery_wait);
6415 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006416 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006417 md_wakeup_thread(mddev->thread);
6418 // stop recovery, signal do_sync ....
6419 }
6420}
6421
6422
NeilBrown06d91a52005-06-21 17:17:12 -07006423/* md_write_start(mddev, bi)
6424 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006425 * in superblock) before writing, schedule a superblock update
6426 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006427 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006428void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006429{
Neil Brown0fd62b82008-06-28 08:31:36 +10006430 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006431 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006432 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006433
NeilBrownf91de922005-11-08 21:39:36 -08006434 BUG_ON(mddev->ro == 1);
6435 if (mddev->ro == 2) {
6436 /* need to switch to read/write */
6437 mddev->ro = 0;
6438 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6439 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006440 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006441 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006442 }
NeilBrown06d91a52005-06-21 17:17:12 -07006443 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006444 if (mddev->safemode == 1)
6445 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006446 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006447 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006448 if (mddev->in_sync) {
6449 mddev->in_sync = 0;
NeilBrown850b2b422006-10-03 01:15:46 -07006450 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006451 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006452 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006453 }
NeilBrowna9701a32005-11-08 21:39:34 -08006454 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006455 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006456 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006457 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006458 wait_event(mddev->sb_wait,
6459 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6460 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006461}
6462
6463void md_write_end(mddev_t *mddev)
6464{
6465 if (atomic_dec_and_test(&mddev->writes_pending)) {
6466 if (mddev->safemode == 2)
6467 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006468 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006469 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6470 }
6471}
6472
NeilBrown2a2275d2007-01-26 00:57:11 -08006473/* md_allow_write(mddev)
6474 * Calling this ensures that the array is marked 'active' so that writes
6475 * may proceed without blocking. It is important to call this before
6476 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6477 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006478 *
6479 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6480 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006481 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006482int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006483{
6484 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006485 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006486 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006487 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006488 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006489 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006490
6491 spin_lock_irq(&mddev->write_lock);
6492 if (mddev->in_sync) {
6493 mddev->in_sync = 0;
6494 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6495 if (mddev->safemode_delay &&
6496 mddev->safemode == 0)
6497 mddev->safemode = 1;
6498 spin_unlock_irq(&mddev->write_lock);
6499 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006500 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006501 } else
6502 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006503
6504 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6505 return -EAGAIN;
6506 else
6507 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006508}
6509EXPORT_SYMBOL_GPL(md_allow_write);
6510
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511#define SYNC_MARKS 10
6512#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006513void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006514{
6515 mddev_t *mddev2;
6516 unsigned int currspeed = 0,
6517 window;
NeilBrown57afd892005-06-21 17:17:13 -07006518 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519 unsigned long mark[SYNC_MARKS];
6520 sector_t mark_cnt[SYNC_MARKS];
6521 int last_mark,m;
6522 struct list_head *tmp;
6523 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006524 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006525 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006526 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006527
6528 /* just incase thread restarts... */
6529 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6530 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006531 if (mddev->ro) /* never try to sync a read-only array */
6532 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533
NeilBrown61df9d92006-10-03 01:15:57 -07006534 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6535 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6536 desc = "data-check";
6537 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6538 desc = "requested-resync";
6539 else
6540 desc = "resync";
6541 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6542 desc = "reshape";
6543 else
6544 desc = "recovery";
6545
Linus Torvalds1da177e2005-04-16 15:20:36 -07006546 /* we overload curr_resync somewhat here.
6547 * 0 == not engaged in resync at all
6548 * 2 == checking that there is no conflict with another sync
6549 * 1 == like 2, but have yielded to allow conflicting resync to
6550 * commense
6551 * other == active in resync - this many blocks
6552 *
6553 * Before starting a resync we must have set curr_resync to
6554 * 2, and then checked that every "conflicting" array has curr_resync
6555 * less than ours. When we find one that is the same or higher
6556 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6557 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6558 * This will mean we have to start checking from the beginning again.
6559 *
6560 */
6561
6562 do {
6563 mddev->curr_resync = 2;
6564
6565 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006566 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006567 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006568
6569 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006571 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006572 if (mddev2 == mddev)
6573 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006574 if (!mddev->parallel_resync
6575 && mddev2->curr_resync
6576 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 DEFINE_WAIT(wq);
6578 if (mddev < mddev2 && mddev->curr_resync == 2) {
6579 /* arbitrarily yield */
6580 mddev->curr_resync = 1;
6581 wake_up(&resync_wait);
6582 }
6583 if (mddev > mddev2 && mddev->curr_resync == 1)
6584 /* no need to wait here, we can wait the next
6585 * time 'round when curr_resync == 2
6586 */
6587 continue;
NeilBrown97441972008-09-19 11:49:54 +10006588 /* We need to wait 'interruptible' so as not to
6589 * contribute to the load average, and not to
6590 * be caught by 'softlockup'
6591 */
6592 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006593 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006594 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006595 printk(KERN_INFO "md: delaying %s of %s"
6596 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006598 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006599 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006600 if (signal_pending(current))
6601 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 schedule();
6603 finish_wait(&resync_wait, &wq);
6604 goto try_again;
6605 }
6606 finish_wait(&resync_wait, &wq);
6607 }
6608 }
6609 } while (mddev->curr_resync < 2);
6610
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006611 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006612 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006614 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 */
6616 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006617 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006618 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006619 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6620 j = mddev->resync_min;
6621 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006622 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006623
NeilBrownccfcc3c2006-03-27 01:18:09 -08006624 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006625 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006626 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006628 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006629 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006630 rcu_read_lock();
6631 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006632 if (rdev->raid_disk >= 0 &&
6633 !test_bit(Faulty, &rdev->flags) &&
6634 !test_bit(In_sync, &rdev->flags) &&
6635 rdev->recovery_offset < j)
6636 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006637 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
NeilBrown61df9d92006-10-03 01:15:57 -07006640 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6641 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6642 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006643 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006644 "(but not more than %d KB/sec) for %s.\n",
6645 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646
NeilBrowneea1bf32009-03-31 14:27:02 +11006647 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006648
NeilBrown57afd892005-06-21 17:17:13 -07006649 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006650 for (m = 0; m < SYNC_MARKS; m++) {
6651 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006652 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 }
6654 last_mark = 0;
6655 mddev->resync_mark = mark[last_mark];
6656 mddev->resync_mark_cnt = mark_cnt[last_mark];
6657
6658 /*
6659 * Tune reconstruction:
6660 */
6661 window = 32*(PAGE_SIZE/512);
6662 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6663 window/2,(unsigned long long) max_sectors/2);
6664
6665 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666 last_check = 0;
6667
6668 if (j>2) {
6669 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006670 "md: resuming %s of %s from checkpoint.\n",
6671 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 mddev->curr_resync = j;
6673 }
NeilBrownefa59332009-12-14 12:49:49 +11006674 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
6676 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006677 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678
NeilBrown57afd892005-06-21 17:17:13 -07006679 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006680
NeilBrown7a91ee12009-05-26 12:57:21 +10006681 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6682 ((mddev->curr_resync > mddev->curr_resync_completed &&
6683 (mddev->curr_resync - mddev->curr_resync_completed)
6684 > (max_sectors >> 4)) ||
6685 (j - mddev->curr_resync_completed)*2
6686 >= mddev->resync_max - mddev->curr_resync_completed
6687 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006688 /* time to update curr_resync_completed */
6689 blk_unplug(mddev->queue);
6690 wait_event(mddev->recovery_wait,
6691 atomic_read(&mddev->recovery_active) == 0);
6692 mddev->curr_resync_completed =
6693 mddev->curr_resync;
6694 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006695 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006696 }
NeilBrownacb180b2009-04-14 16:28:34 +10006697
NeilBrowne62e58a2009-07-01 13:15:35 +10006698 while (j >= mddev->resync_max && !kthread_should_stop()) {
6699 /* As this condition is controlled by user-space,
6700 * we can block indefinitely, so use '_interruptible'
6701 * to avoid triggering warnings.
6702 */
6703 flush_signals(current); /* just in case */
6704 wait_event_interruptible(mddev->recovery_wait,
6705 mddev->resync_max > j
6706 || kthread_should_stop());
6707 }
NeilBrownacb180b2009-04-14 16:28:34 +10006708
6709 if (kthread_should_stop())
6710 goto interrupted;
6711
NeilBrown57afd892005-06-21 17:17:13 -07006712 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006713 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006714 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006715 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 goto out;
6717 }
NeilBrown57afd892005-06-21 17:17:13 -07006718
6719 if (!skipped) { /* actual IO requested */
6720 io_sectors += sectors;
6721 atomic_add(sectors, &mddev->recovery_active);
6722 }
6723
Linus Torvalds1da177e2005-04-16 15:20:36 -07006724 j += sectors;
6725 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006726 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006727 if (last_check == 0)
6728 /* this is the earliers that rebuilt will be
6729 * visible in /proc/mdstat
6730 */
6731 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006732
6733 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006734 continue;
6735
NeilBrown57afd892005-06-21 17:17:13 -07006736 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737
NeilBrowndfc70642008-05-23 13:04:39 -07006738 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006739 break;
6740
6741 repeat:
6742 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6743 /* step marks */
6744 int next = (last_mark+1) % SYNC_MARKS;
6745
6746 mddev->resync_mark = mark[next];
6747 mddev->resync_mark_cnt = mark_cnt[next];
6748 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006749 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 last_mark = next;
6751 }
6752
6753
NeilBrownc6207272008-02-06 01:39:52 -08006754 if (kthread_should_stop())
6755 goto interrupted;
6756
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757
6758 /*
6759 * this loop exits only if either when we are slower than
6760 * the 'hard' speed limit, or the system was IO-idle for
6761 * a jiffy.
6762 * the system might be non-idle CPU-wise, but we only care
6763 * about not overloading the IO subsystem. (things like an
6764 * e2fsck being done on the RAID array should execute fast)
6765 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006766 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006767 cond_resched();
6768
NeilBrown57afd892005-06-21 17:17:13 -07006769 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6770 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771
NeilBrown88202a02006-01-06 00:21:36 -08006772 if (currspeed > speed_min(mddev)) {
6773 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006774 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006775 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 goto repeat;
6777 }
6778 }
6779 }
NeilBrown61df9d92006-10-03 01:15:57 -07006780 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 /*
6782 * this also signals 'finished resyncing' to md_stop
6783 */
6784 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006785 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786
6787 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6788
6789 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006790 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791
NeilBrowndfc70642008-05-23 13:04:39 -07006792 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006793 mddev->curr_resync > 2) {
6794 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6795 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6796 if (mddev->curr_resync >= mddev->recovery_cp) {
6797 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006798 "md: checkpointing %s of %s.\n",
6799 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006800 mddev->recovery_cp = mddev->curr_resync;
6801 }
6802 } else
6803 mddev->recovery_cp = MaxSector;
6804 } else {
6805 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6806 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006807 rcu_read_lock();
6808 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006809 if (rdev->raid_disk >= 0 &&
6810 !test_bit(Faulty, &rdev->flags) &&
6811 !test_bit(In_sync, &rdev->flags) &&
6812 rdev->recovery_offset < mddev->curr_resync)
6813 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006814 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006816 }
NeilBrown17571282006-12-10 02:20:52 -08006817 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818
Linus Torvalds1da177e2005-04-16 15:20:36 -07006819 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006820 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6821 /* We completed so min/max setting can be forgotten if used. */
6822 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6823 mddev->resync_min = 0;
6824 mddev->resync_max = MaxSector;
6825 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6826 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006828 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6829 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006830 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831 wake_up(&resync_wait);
6832 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6833 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006834 return;
6835
6836 interrupted:
6837 /*
6838 * got a signal, exit.
6839 */
6840 printk(KERN_INFO
6841 "md: md_do_sync() got signal ... exiting\n");
6842 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6843 goto out;
6844
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845}
NeilBrown29269552006-03-27 01:18:10 -08006846EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847
6848
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006849static int remove_and_add_spares(mddev_t *mddev)
6850{
6851 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006852 int spares = 0;
6853
NeilBrown97e4f422009-03-31 14:33:13 +11006854 mddev->curr_resync_completed = 0;
6855
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006856 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006857 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006858 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006859 (test_bit(Faulty, &rdev->flags) ||
6860 ! test_bit(In_sync, &rdev->flags)) &&
6861 atomic_read(&rdev->nr_pending)==0) {
6862 if (mddev->pers->hot_remove_disk(
6863 mddev, rdev->raid_disk)==0) {
6864 char nm[20];
6865 sprintf(nm,"rd%d", rdev->raid_disk);
6866 sysfs_remove_link(&mddev->kobj, nm);
6867 rdev->raid_disk = -1;
6868 }
6869 }
6870
NeilBrown4044ba52009-01-09 08:31:11 +11006871 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006872 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006873 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006874 !test_bit(In_sync, &rdev->flags) &&
6875 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006876 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006877 if (rdev->raid_disk < 0
6878 && !test_bit(Faulty, &rdev->flags)) {
6879 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006880 if (mddev->pers->
6881 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006882 char nm[20];
6883 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006884 if (sysfs_create_link(&mddev->kobj,
6885 &rdev->kobj, nm))
6886 printk(KERN_WARNING
6887 "md: cannot register "
6888 "%s for %s\n",
6889 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006890 spares++;
6891 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006892 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006893 } else
6894 break;
6895 }
NeilBrowndfc70642008-05-23 13:04:39 -07006896 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006897 }
6898 return spares;
6899}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006900/*
6901 * This routine is regularly called by all per-raid-array threads to
6902 * deal with generic issues like resync and super-block update.
6903 * Raid personalities that don't have a thread (linear/raid0) do not
6904 * need this as they never do any recovery or update the superblock.
6905 *
6906 * It does not do any resync itself, but rather "forks" off other threads
6907 * to do that as needed.
6908 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6909 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006910 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 * and wakeups up this thread which will reap the thread and finish up.
6912 * This thread also removes any faulty devices (with nr_pending == 0).
6913 *
6914 * The overall approach is:
6915 * 1/ if the superblock needs updating, update it.
6916 * 2/ If a recovery thread is running, don't do anything else.
6917 * 3/ If recovery has finished, clean up, possibly marking spares active.
6918 * 4/ If there are any faulty devices, remove them.
6919 * 5/ If array is degraded, try to add spares devices
6920 * 6/ If array has spares or is not in-sync, start a resync thread.
6921 */
6922void md_check_recovery(mddev_t *mddev)
6923{
6924 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006925
6926
NeilBrown5f404022005-06-21 17:17:16 -07006927 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006928 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006929
6930 if (mddev->ro)
6931 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006932
6933 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006934 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006935 printk(KERN_INFO "md: %s in immediate safe mode\n",
6936 mdname(mddev));
6937 mddev->safemode = 2;
6938 }
6939 flush_signals(current);
6940 }
6941
NeilBrownc89a8ee2008-08-05 15:54:13 +10006942 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6943 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006944 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006945 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006946 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006947 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006948 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006949 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6950 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006951 ))
6952 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006953
NeilBrowndf5b89b2006-03-27 01:18:20 -08006954 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006955 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006956
NeilBrownc89a8ee2008-08-05 15:54:13 +10006957 if (mddev->ro) {
6958 /* Only thing we do on a ro array is remove
6959 * failed devices.
6960 */
6961 remove_and_add_spares(mddev);
6962 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6963 goto unlock;
6964 }
6965
NeilBrown31a59e32008-04-30 00:52:30 -07006966 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006967 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006968 spin_lock_irq(&mddev->write_lock);
6969 if (mddev->safemode &&
6970 !atomic_read(&mddev->writes_pending) &&
6971 !mddev->in_sync &&
6972 mddev->recovery_cp == MaxSector) {
6973 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10006974 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07006975 if (mddev->persistent)
6976 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6977 }
6978 if (mddev->safemode == 1)
6979 mddev->safemode = 0;
6980 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10006981 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006982 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07006983 }
NeilBrownfca4d842005-06-21 17:17:11 -07006984
NeilBrown850b2b422006-10-03 01:15:46 -07006985 if (mddev->flags)
6986 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07006987
Linus Torvalds1da177e2005-04-16 15:20:36 -07006988 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
6989 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
6990 /* resync/recovery still happening */
6991 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6992 goto unlock;
6993 }
6994 if (mddev->sync_thread) {
6995 /* resync has finished, collect result */
6996 md_unregister_thread(mddev->sync_thread);
6997 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07006998 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
6999 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007000 /* success...*/
7001 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007002 if (mddev->pers->spare_active(mddev))
7003 sysfs_notify(&mddev->kobj, NULL,
7004 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005 }
NeilBrowncea9c222009-03-31 15:15:05 +11007006 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7007 mddev->pers->finish_reshape)
7008 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b422006-10-03 01:15:46 -07007009 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007010
7011 /* if array is no-longer degraded, then any saved_raid_disk
7012 * information must be scrapped
7013 */
7014 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007015 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007016 rdev->saved_raid_disk = -1;
7017
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 mddev->recovery = 0;
7019 /* flag recovery needed just to double check */
7020 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007021 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007022 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007023 goto unlock;
7024 }
Neil Brown72a23c22008-06-28 08:31:41 +10007025 /* Set RUNNING before clearing NEEDED to avoid
7026 * any transients in the value of "sync_action".
7027 */
7028 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7029 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007030 /* Clear some bits that don't mean anything, but
7031 * might be left set
7032 */
NeilBrown24dd4692005-11-08 21:39:26 -08007033 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7034 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007035
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007036 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7037 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007038 /* no recovery is running.
7039 * remove any failed drives, then
7040 * add spares if possible.
7041 * Spare are also removed and re-added, to allow
7042 * the personality to fail the re-add.
7043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007044
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007045 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007046 if (mddev->pers->check_reshape == NULL ||
7047 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007048 /* Cannot proceed */
7049 goto unlock;
7050 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007051 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007052 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007053 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7054 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007055 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007056 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007057 } else if (mddev->recovery_cp < MaxSector) {
7058 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007059 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007060 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7061 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007063
Linus Torvalds1da177e2005-04-16 15:20:36 -07007064 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007065 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7066 /* We are adding a device or devices to an array
7067 * which has the bitmap stored on all devices.
7068 * So make sure all bitmap pages get written
7069 */
7070 bitmap_write_all(mddev->bitmap);
7071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007072 mddev->sync_thread = md_register_thread(md_do_sync,
7073 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007074 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075 if (!mddev->sync_thread) {
7076 printk(KERN_ERR "%s: could not start resync"
7077 " thread...\n",
7078 mdname(mddev));
7079 /* leave the spares where they are, it shouldn't hurt */
7080 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007081 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007083 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007084 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 }
7086 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007087 if (!mddev->sync_thread) {
7088 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7089 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7090 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007091 if (mddev->sysfs_action)
7092 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007094 mddev_unlock(mddev);
7095 }
7096}
7097
Dan Williams6bfe0b42008-04-30 00:52:32 -07007098void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7099{
NeilBrown3c0ee632008-10-21 13:25:28 +11007100 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007101 wait_event_timeout(rdev->blocked_wait,
7102 !test_bit(Blocked, &rdev->flags),
7103 msecs_to_jiffies(5000));
7104 rdev_dec_pending(rdev, mddev);
7105}
7106EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7107
Adrian Bunk75c96f82005-05-05 16:16:09 -07007108static int md_notify_reboot(struct notifier_block *this,
7109 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007110{
7111 struct list_head *tmp;
7112 mddev_t *mddev;
7113
7114 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7115
7116 printk(KERN_INFO "md: stopping all md devices.\n");
7117
NeilBrown29ac4aa2008-02-06 01:39:58 -08007118 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007119 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007120 /* Force a switch to readonly even array
7121 * appears to still be in use. Hence
7122 * the '100'.
7123 */
NeilBrownd710e132008-10-13 11:55:12 +11007124 do_md_stop(mddev, 1, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007125 mddev_unlock(mddev);
7126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 /*
7128 * certain more exotic SCSI devices are known to be
7129 * volatile wrt too early system reboots. While the
7130 * right place to handle this issue is the given
7131 * driver, we do want to have a safe RAID driver ...
7132 */
7133 mdelay(1000*1);
7134 }
7135 return NOTIFY_DONE;
7136}
7137
Adrian Bunk75c96f82005-05-05 16:16:09 -07007138static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007139 .notifier_call = md_notify_reboot,
7140 .next = NULL,
7141 .priority = INT_MAX, /* before any real devices */
7142};
7143
7144static void md_geninit(void)
7145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7147
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007148 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149}
7150
Adrian Bunk75c96f82005-05-05 16:16:09 -07007151static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007152{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007153 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007154 return -1;
7155 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007156 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007157 return -1;
7158 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007159 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007160 md_probe, NULL, NULL);
7161 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162 md_probe, NULL, NULL);
7163
Linus Torvalds1da177e2005-04-16 15:20:36 -07007164 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007165 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166
7167 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007169}
7170
7171
7172#ifndef MODULE
7173
7174/*
7175 * Searches all registered partitions for autorun RAID arrays
7176 * at boot time.
7177 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007178
7179static LIST_HEAD(all_detected_devices);
7180struct detected_devices_node {
7181 struct list_head list;
7182 dev_t dev;
7183};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184
7185void md_autodetect_dev(dev_t dev)
7186{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007187 struct detected_devices_node *node_detected_dev;
7188
7189 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7190 if (node_detected_dev) {
7191 node_detected_dev->dev = dev;
7192 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7193 } else {
7194 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7195 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197}
7198
7199
7200static void autostart_arrays(int part)
7201{
7202 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007203 struct detected_devices_node *node_detected_dev;
7204 dev_t dev;
7205 int i_scanned, i_passed;
7206
7207 i_scanned = 0;
7208 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209
7210 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7211
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007212 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7213 i_scanned++;
7214 node_detected_dev = list_entry(all_detected_devices.next,
7215 struct detected_devices_node, list);
7216 list_del(&node_detected_dev->list);
7217 dev = node_detected_dev->dev;
7218 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007219 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220 if (IS_ERR(rdev))
7221 continue;
7222
NeilBrownb2d444d2005-11-08 21:39:31 -08007223 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007224 MD_BUG();
7225 continue;
7226 }
NeilBrownd0fae182008-03-04 14:29:31 -08007227 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007229 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007230 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007231
7232 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7233 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007234
7235 autorun_devices(part);
7236}
7237
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007238#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239
7240static __exit void md_exit(void)
7241{
7242 mddev_t *mddev;
7243 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007244
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007245 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007246 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007247
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007248 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007249 unregister_blkdev(mdp_major, "mdp");
7250 unregister_reboot_notifier(&md_notifier);
7251 unregister_sysctl_table(raid_table_header);
7252 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007253 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007255 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007256 }
7257}
7258
Dan Williams685784a2007-07-09 11:56:42 -07007259subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007260module_exit(md_exit)
7261
NeilBrownf91de922005-11-08 21:39:36 -08007262static int get_ro(char *buffer, struct kernel_param *kp)
7263{
7264 return sprintf(buffer, "%d", start_readonly);
7265}
7266static int set_ro(const char *val, struct kernel_param *kp)
7267{
7268 char *e;
7269 int num = simple_strtoul(val, &e, 10);
7270 if (*val && (*e == '\0' || *e == '\n')) {
7271 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007272 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007273 }
7274 return -EINVAL;
7275}
7276
NeilBrown80ca3a42006-07-10 04:44:18 -07007277module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7278module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007279
NeilBrownefeb53c2009-01-09 08:31:10 +11007280module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007281
Linus Torvalds1da177e2005-04-16 15:20:36 -07007282EXPORT_SYMBOL(register_md_personality);
7283EXPORT_SYMBOL(unregister_md_personality);
7284EXPORT_SYMBOL(md_error);
7285EXPORT_SYMBOL(md_done_sync);
7286EXPORT_SYMBOL(md_write_start);
7287EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007288EXPORT_SYMBOL(md_register_thread);
7289EXPORT_SYMBOL(md_unregister_thread);
7290EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291EXPORT_SYMBOL(md_check_recovery);
7292MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007293MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007294MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007295MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);