blob: 3a2710a2e104ffa5ee553948fc1652c7358d983d [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
NeilBrown850b2b42006-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
NeilBrown850b2b42006-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 */
NeilBrown850b2b42006-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 */
NeilBrown850b2b42006-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);
NeilBrown850b2b42006-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);
NeilBrown850b2b42006-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 }
NeilBrown850b2b42006-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);
NeilBrowna4bd82d2010-03-29 13:23:10 +11003312static int md_set_readonly(mddev_t * mddev, int is_open);
NeilBrown9e653b62006-06-26 00:27:58 -07003313static int do_md_run(mddev_t * mddev);
3314static int restart_array(mddev_t *mddev);
3315
3316static ssize_t
3317array_state_store(mddev_t *mddev, const char *buf, size_t len)
3318{
3319 int err = -EINVAL;
3320 enum array_state st = match_word(buf, array_states);
3321 switch(st) {
3322 case bad_word:
3323 break;
3324 case clear:
3325 /* stopping an active array */
NeilBrownf2ea68c2008-07-21 17:05:25 +10003326 if (atomic_read(&mddev->openers) > 0)
NeilBrowne6910632008-02-06 01:39:51 -08003327 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003328 err = do_md_stop(mddev, 0, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003329 break;
3330 case inactive:
3331 /* stopping an active array */
3332 if (mddev->pers) {
NeilBrownf2ea68c2008-07-21 17:05:25 +10003333 if (atomic_read(&mddev->openers) > 0)
NeilBrown9e653b62006-06-26 00:27:58 -07003334 return -EBUSY;
Neil Browndf5b20c2008-07-11 22:02:22 +10003335 err = do_md_stop(mddev, 2, 0);
NeilBrowne6910632008-02-06 01:39:51 -08003336 } else
3337 err = 0; /* already inactive */
NeilBrown9e653b62006-06-26 00:27:58 -07003338 break;
3339 case suspended:
3340 break; /* not supported yet */
3341 case readonly:
3342 if (mddev->pers)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003343 err = md_set_readonly(mddev, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003344 else {
3345 mddev->ro = 1;
NeilBrown648b6292008-04-30 00:52:30 -07003346 set_disk_ro(mddev->gendisk, 1);
NeilBrown9e653b62006-06-26 00:27:58 -07003347 err = do_md_run(mddev);
3348 }
3349 break;
3350 case read_auto:
NeilBrown9e653b62006-06-26 00:27:58 -07003351 if (mddev->pers) {
NeilBrown80268ee2008-10-13 11:55:12 +11003352 if (mddev->ro == 0)
NeilBrowna4bd82d2010-03-29 13:23:10 +11003353 err = md_set_readonly(mddev, 0);
NeilBrown80268ee2008-10-13 11:55:12 +11003354 else if (mddev->ro == 1)
NeilBrown648b6292008-04-30 00:52:30 -07003355 err = restart_array(mddev);
3356 if (err == 0) {
3357 mddev->ro = 2;
3358 set_disk_ro(mddev->gendisk, 0);
3359 }
NeilBrown9e653b62006-06-26 00:27:58 -07003360 } else {
3361 mddev->ro = 2;
3362 err = do_md_run(mddev);
3363 }
3364 break;
3365 case clean:
3366 if (mddev->pers) {
3367 restart_array(mddev);
3368 spin_lock_irq(&mddev->write_lock);
3369 if (atomic_read(&mddev->writes_pending) == 0) {
NeilBrowne6910632008-02-06 01:39:51 -08003370 if (mddev->in_sync == 0) {
3371 mddev->in_sync = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07003372 if (mddev->safemode == 1)
3373 mddev->safemode = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003374 if (mddev->persistent)
3375 set_bit(MD_CHANGE_CLEAN,
3376 &mddev->flags);
3377 }
3378 err = 0;
3379 } else
3380 err = -EBUSY;
NeilBrown9e653b62006-06-26 00:27:58 -07003381 spin_unlock_irq(&mddev->write_lock);
NeilBrown5bf29592009-05-07 12:50:57 +10003382 } else
3383 err = -EINVAL;
NeilBrown9e653b62006-06-26 00:27:58 -07003384 break;
3385 case active:
3386 if (mddev->pers) {
3387 restart_array(mddev);
NeilBrowne6910632008-02-06 01:39:51 -08003388 if (mddev->external)
3389 clear_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown9e653b62006-06-26 00:27:58 -07003390 wake_up(&mddev->sb_wait);
3391 err = 0;
3392 } else {
3393 mddev->ro = 0;
NeilBrown648b6292008-04-30 00:52:30 -07003394 set_disk_ro(mddev->gendisk, 0);
NeilBrown9e653b62006-06-26 00:27:58 -07003395 err = do_md_run(mddev);
3396 }
3397 break;
3398 case write_pending:
3399 case active_idle:
3400 /* these cannot be set */
3401 break;
3402 }
3403 if (err)
3404 return err;
Neil Brown0fd62b82008-06-28 08:31:36 +10003405 else {
NeilBrownb62b7592008-10-21 13:25:21 +11003406 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown9e653b62006-06-26 00:27:58 -07003407 return len;
Neil Brown0fd62b82008-06-28 08:31:36 +10003408 }
NeilBrown9e653b62006-06-26 00:27:58 -07003409}
NeilBrown80ca3a42006-07-10 04:44:18 -07003410static struct md_sysfs_entry md_array_state =
3411__ATTR(array_state, S_IRUGO|S_IWUSR, array_state_show, array_state_store);
NeilBrown9e653b62006-06-26 00:27:58 -07003412
NeilBrown6d7ff732006-01-06 00:21:16 -08003413static ssize_t
Robert Becker1e509152009-12-14 12:49:58 +11003414max_corrected_read_errors_show(mddev_t *mddev, char *page) {
3415 return sprintf(page, "%d\n",
3416 atomic_read(&mddev->max_corr_read_errors));
3417}
3418
3419static ssize_t
3420max_corrected_read_errors_store(mddev_t *mddev, const char *buf, size_t len)
3421{
3422 char *e;
3423 unsigned long n = simple_strtoul(buf, &e, 10);
3424
3425 if (*buf && (*e == 0 || *e == '\n')) {
3426 atomic_set(&mddev->max_corr_read_errors, n);
3427 return len;
3428 }
3429 return -EINVAL;
3430}
3431
3432static struct md_sysfs_entry max_corr_read_errors =
3433__ATTR(max_read_errors, S_IRUGO|S_IWUSR, max_corrected_read_errors_show,
3434 max_corrected_read_errors_store);
3435
3436static ssize_t
NeilBrown6d7ff732006-01-06 00:21:16 -08003437null_show(mddev_t *mddev, char *page)
3438{
3439 return -EINVAL;
3440}
3441
3442static ssize_t
3443new_dev_store(mddev_t *mddev, const char *buf, size_t len)
3444{
3445 /* buf must be %d:%d\n? giving major and minor numbers */
3446 /* The new device is added to the array.
3447 * If the array has a persistent superblock, we read the
3448 * superblock to initialise info and check validity.
3449 * Otherwise, only checking done is that in bind_rdev_to_array,
3450 * which mainly checks size.
3451 */
3452 char *e;
3453 int major = simple_strtoul(buf, &e, 10);
3454 int minor;
3455 dev_t dev;
3456 mdk_rdev_t *rdev;
3457 int err;
3458
3459 if (!*buf || *e != ':' || !e[1] || e[1] == '\n')
3460 return -EINVAL;
3461 minor = simple_strtoul(e+1, &e, 10);
3462 if (*e && *e != '\n')
3463 return -EINVAL;
3464 dev = MKDEV(major, minor);
3465 if (major != MAJOR(dev) ||
3466 minor != MINOR(dev))
3467 return -EOVERFLOW;
3468
3469
3470 if (mddev->persistent) {
3471 rdev = md_import_device(dev, mddev->major_version,
3472 mddev->minor_version);
3473 if (!IS_ERR(rdev) && !list_empty(&mddev->disks)) {
3474 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
3475 mdk_rdev_t, same_set);
3476 err = super_types[mddev->major_version]
3477 .load_super(rdev, rdev0, mddev->minor_version);
3478 if (err < 0)
3479 goto out;
3480 }
NeilBrownc5d79ad2008-02-06 01:39:54 -08003481 } else if (mddev->external)
3482 rdev = md_import_device(dev, -2, -1);
3483 else
NeilBrown6d7ff732006-01-06 00:21:16 -08003484 rdev = md_import_device(dev, -1, -1);
3485
3486 if (IS_ERR(rdev))
3487 return PTR_ERR(rdev);
3488 err = bind_rdev_to_array(rdev, mddev);
3489 out:
3490 if (err)
3491 export_rdev(rdev);
3492 return err ? err : len;
3493}
3494
3495static struct md_sysfs_entry md_new_device =
NeilBrown80ca3a42006-07-10 04:44:18 -07003496__ATTR(new_dev, S_IWUSR, null_show, new_dev_store);
NeilBrown3b343802006-01-06 00:20:47 -08003497
3498static ssize_t
Paul Clements9b1d1da2006-10-03 01:15:49 -07003499bitmap_store(mddev_t *mddev, const char *buf, size_t len)
3500{
3501 char *end;
3502 unsigned long chunk, end_chunk;
3503
3504 if (!mddev->bitmap)
3505 goto out;
3506 /* buf should be <chunk> <chunk> ... or <chunk>-<chunk> ... (range) */
3507 while (*buf) {
3508 chunk = end_chunk = simple_strtoul(buf, &end, 0);
3509 if (buf == end) break;
3510 if (*end == '-') { /* range */
3511 buf = end + 1;
3512 end_chunk = simple_strtoul(buf, &end, 0);
3513 if (buf == end) break;
3514 }
3515 if (*end && !isspace(*end)) break;
3516 bitmap_dirty_bits(mddev->bitmap, chunk, end_chunk);
André Goddard Rosae7d28602009-12-14 18:01:06 -08003517 buf = skip_spaces(end);
Paul Clements9b1d1da2006-10-03 01:15:49 -07003518 }
3519 bitmap_unplug(mddev->bitmap); /* flush the bits to disk */
3520out:
3521 return len;
3522}
3523
3524static struct md_sysfs_entry md_bitmap =
3525__ATTR(bitmap_set_bits, S_IWUSR, null_show, bitmap_store);
3526
3527static ssize_t
NeilBrowna35b0d62006-01-06 00:20:49 -08003528size_show(mddev_t *mddev, char *page)
3529{
Andre Noll58c0fed2009-03-31 14:33:13 +11003530 return sprintf(page, "%llu\n",
3531 (unsigned long long)mddev->dev_sectors / 2);
NeilBrowna35b0d62006-01-06 00:20:49 -08003532}
3533
Andre Nolld71f9f82008-07-11 22:02:22 +10003534static int update_size(mddev_t *mddev, sector_t num_sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003535
3536static ssize_t
3537size_store(mddev_t *mddev, const char *buf, size_t len)
3538{
3539 /* If array is inactive, we can reduce the component size, but
3540 * not increase it (except from 0).
3541 * If array is active, we can try an on-line resize
3542 */
Dan Williamsb522adc2009-03-31 15:00:31 +11003543 sector_t sectors;
3544 int err = strict_blocks_to_sectors(buf, &sectors);
NeilBrowna35b0d62006-01-06 00:20:49 -08003545
Andre Noll58c0fed2009-03-31 14:33:13 +11003546 if (err < 0)
3547 return err;
NeilBrowna35b0d62006-01-06 00:20:49 -08003548 if (mddev->pers) {
Andre Noll58c0fed2009-03-31 14:33:13 +11003549 err = update_size(mddev, sectors);
NeilBrown850b2b42006-10-03 01:15:46 -07003550 md_update_sb(mddev, 1);
NeilBrowna35b0d62006-01-06 00:20:49 -08003551 } else {
Andre Noll58c0fed2009-03-31 14:33:13 +11003552 if (mddev->dev_sectors == 0 ||
3553 mddev->dev_sectors > sectors)
3554 mddev->dev_sectors = sectors;
NeilBrowna35b0d62006-01-06 00:20:49 -08003555 else
3556 err = -ENOSPC;
3557 }
3558 return err ? err : len;
3559}
3560
3561static struct md_sysfs_entry md_size =
NeilBrown80ca3a42006-07-10 04:44:18 -07003562__ATTR(component_size, S_IRUGO|S_IWUSR, size_show, size_store);
NeilBrowna35b0d62006-01-06 00:20:49 -08003563
NeilBrown8bb93aa2006-01-06 00:20:50 -08003564
3565/* Metdata version.
NeilBrowne6910632008-02-06 01:39:51 -08003566 * This is one of
3567 * 'none' for arrays with no metadata (good luck...)
3568 * 'external' for arrays with externally managed metadata,
NeilBrown8bb93aa2006-01-06 00:20:50 -08003569 * or N.M for internally known formats
3570 */
3571static ssize_t
3572metadata_show(mddev_t *mddev, char *page)
3573{
3574 if (mddev->persistent)
3575 return sprintf(page, "%d.%d\n",
3576 mddev->major_version, mddev->minor_version);
NeilBrowne6910632008-02-06 01:39:51 -08003577 else if (mddev->external)
3578 return sprintf(page, "external:%s\n", mddev->metadata_type);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003579 else
3580 return sprintf(page, "none\n");
3581}
3582
3583static ssize_t
3584metadata_store(mddev_t *mddev, const char *buf, size_t len)
3585{
3586 int major, minor;
3587 char *e;
NeilBrownea43ddd2008-10-13 11:55:11 +11003588 /* Changing the details of 'external' metadata is
3589 * always permitted. Otherwise there must be
3590 * no devices attached to the array.
3591 */
3592 if (mddev->external && strncmp(buf, "external:", 9) == 0)
3593 ;
3594 else if (!list_empty(&mddev->disks))
NeilBrown8bb93aa2006-01-06 00:20:50 -08003595 return -EBUSY;
3596
3597 if (cmd_match(buf, "none")) {
3598 mddev->persistent = 0;
NeilBrowne6910632008-02-06 01:39:51 -08003599 mddev->external = 0;
3600 mddev->major_version = 0;
3601 mddev->minor_version = 90;
3602 return len;
3603 }
3604 if (strncmp(buf, "external:", 9) == 0) {
NeilBrown20a49ff2008-02-06 01:39:57 -08003605 size_t namelen = len-9;
NeilBrowne6910632008-02-06 01:39:51 -08003606 if (namelen >= sizeof(mddev->metadata_type))
3607 namelen = sizeof(mddev->metadata_type)-1;
3608 strncpy(mddev->metadata_type, buf+9, namelen);
3609 mddev->metadata_type[namelen] = 0;
3610 if (namelen && mddev->metadata_type[namelen-1] == '\n')
3611 mddev->metadata_type[--namelen] = 0;
3612 mddev->persistent = 0;
3613 mddev->external = 1;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003614 mddev->major_version = 0;
3615 mddev->minor_version = 90;
3616 return len;
3617 }
3618 major = simple_strtoul(buf, &e, 10);
3619 if (e==buf || *e != '.')
3620 return -EINVAL;
3621 buf = e+1;
3622 minor = simple_strtoul(buf, &e, 10);
NeilBrown3f9d7b02006-12-22 01:11:41 -08003623 if (e==buf || (*e && *e != '\n') )
NeilBrown8bb93aa2006-01-06 00:20:50 -08003624 return -EINVAL;
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07003625 if (major >= ARRAY_SIZE(super_types) || super_types[major].name == NULL)
NeilBrown8bb93aa2006-01-06 00:20:50 -08003626 return -ENOENT;
3627 mddev->major_version = major;
3628 mddev->minor_version = minor;
3629 mddev->persistent = 1;
NeilBrowne6910632008-02-06 01:39:51 -08003630 mddev->external = 0;
NeilBrown8bb93aa2006-01-06 00:20:50 -08003631 return len;
3632}
3633
3634static struct md_sysfs_entry md_metadata =
NeilBrown80ca3a42006-07-10 04:44:18 -07003635__ATTR(metadata_version, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
NeilBrown8bb93aa2006-01-06 00:20:50 -08003636
NeilBrowna35b0d62006-01-06 00:20:49 -08003637static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003638action_show(mddev_t *mddev, char *page)
NeilBrown24dd4692005-11-08 21:39:26 -08003639{
NeilBrown7eec3142005-11-08 21:39:44 -08003640 char *type = "idle";
NeilBrownb6a9ce62009-05-26 09:41:17 +10003641 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
3642 type = "frozen";
3643 else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
NeilBrown2b12ab62007-10-16 23:30:53 -07003644 (!mddev->ro && test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))) {
NeilBrownccfcc3c2006-03-27 01:18:09 -08003645 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3646 type = "reshape";
3647 else if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
NeilBrown24dd4692005-11-08 21:39:26 -08003648 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
3649 type = "resync";
3650 else if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
3651 type = "check";
3652 else
3653 type = "repair";
Neil Brown72a23c22008-06-28 08:31:41 +10003654 } else if (test_bit(MD_RECOVERY_RECOVER, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003655 type = "recover";
3656 }
3657 return sprintf(page, "%s\n", type);
3658}
3659
3660static ssize_t
NeilBrown7eec3142005-11-08 21:39:44 -08003661action_store(mddev_t *mddev, const char *page, size_t len)
NeilBrown24dd4692005-11-08 21:39:26 -08003662{
NeilBrown7eec3142005-11-08 21:39:44 -08003663 if (!mddev->pers || !mddev->pers->sync_request)
3664 return -EINVAL;
3665
NeilBrownb6a9ce62009-05-26 09:41:17 +10003666 if (cmd_match(page, "frozen"))
3667 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3668 else
3669 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
3670
3671 if (cmd_match(page, "idle") || cmd_match(page, "frozen")) {
NeilBrown7eec3142005-11-08 21:39:44 -08003672 if (mddev->sync_thread) {
3673 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
3674 md_unregister_thread(mddev->sync_thread);
3675 mddev->sync_thread = NULL;
3676 mddev->recovery = 0;
3677 }
NeilBrown03c902e2006-01-06 00:20:46 -08003678 } else if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
3679 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
NeilBrown24dd4692005-11-08 21:39:26 -08003680 return -EBUSY;
Neil Brown72a23c22008-06-28 08:31:41 +10003681 else if (cmd_match(page, "resync"))
NeilBrown7eec3142005-11-08 21:39:44 -08003682 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10003683 else if (cmd_match(page, "recover")) {
3684 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
3685 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3686 } else if (cmd_match(page, "reshape")) {
NeilBrown16484bf2006-03-27 01:18:13 -08003687 int err;
3688 if (mddev->pers->start_reshape == NULL)
3689 return -EINVAL;
3690 err = mddev->pers->start_reshape(mddev);
3691 if (err)
3692 return err;
Neil Browna99ac972008-06-28 08:31:43 +10003693 sysfs_notify(&mddev->kobj, NULL, "degraded");
NeilBrown16484bf2006-03-27 01:18:13 -08003694 } else {
NeilBrownbce74da2006-01-06 00:20:41 -08003695 if (cmd_match(page, "check"))
NeilBrown7eec3142005-11-08 21:39:44 -08003696 set_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrown2adc7d42006-05-20 14:59:57 -07003697 else if (!cmd_match(page, "repair"))
NeilBrown7eec3142005-11-08 21:39:44 -08003698 return -EINVAL;
3699 set_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
3700 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
NeilBrown7eec3142005-11-08 21:39:44 -08003701 }
NeilBrown03c902e2006-01-06 00:20:46 -08003702 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08003703 md_wakeup_thread(mddev->thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11003704 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrown24dd4692005-11-08 21:39:26 -08003705 return len;
3706}
3707
NeilBrown9d888832005-11-08 21:39:26 -08003708static ssize_t
NeilBrown96de1e62005-11-08 21:39:39 -08003709mismatch_cnt_show(mddev_t *mddev, char *page)
NeilBrown9d888832005-11-08 21:39:26 -08003710{
3711 return sprintf(page, "%llu\n",
3712 (unsigned long long) mddev->resync_mismatches);
3713}
3714
NeilBrown80ca3a42006-07-10 04:44:18 -07003715static struct md_sysfs_entry md_scan_mode =
3716__ATTR(sync_action, S_IRUGO|S_IWUSR, action_show, action_store);
NeilBrown24dd4692005-11-08 21:39:26 -08003717
NeilBrown96de1e62005-11-08 21:39:39 -08003718
NeilBrown80ca3a42006-07-10 04:44:18 -07003719static struct md_sysfs_entry md_mismatches = __ATTR_RO(mismatch_cnt);
NeilBrown9d888832005-11-08 21:39:26 -08003720
NeilBrown88202a02006-01-06 00:21:36 -08003721static ssize_t
3722sync_min_show(mddev_t *mddev, char *page)
3723{
3724 return sprintf(page, "%d (%s)\n", speed_min(mddev),
3725 mddev->sync_speed_min ? "local": "system");
3726}
3727
3728static ssize_t
3729sync_min_store(mddev_t *mddev, const char *buf, size_t len)
3730{
3731 int min;
3732 char *e;
3733 if (strncmp(buf, "system", 6)==0) {
3734 mddev->sync_speed_min = 0;
3735 return len;
3736 }
3737 min = simple_strtoul(buf, &e, 10);
3738 if (buf == e || (*e && *e != '\n') || min <= 0)
3739 return -EINVAL;
3740 mddev->sync_speed_min = min;
3741 return len;
3742}
3743
3744static struct md_sysfs_entry md_sync_min =
3745__ATTR(sync_speed_min, S_IRUGO|S_IWUSR, sync_min_show, sync_min_store);
3746
3747static ssize_t
3748sync_max_show(mddev_t *mddev, char *page)
3749{
3750 return sprintf(page, "%d (%s)\n", speed_max(mddev),
3751 mddev->sync_speed_max ? "local": "system");
3752}
3753
3754static ssize_t
3755sync_max_store(mddev_t *mddev, const char *buf, size_t len)
3756{
3757 int max;
3758 char *e;
3759 if (strncmp(buf, "system", 6)==0) {
3760 mddev->sync_speed_max = 0;
3761 return len;
3762 }
3763 max = simple_strtoul(buf, &e, 10);
3764 if (buf == e || (*e && *e != '\n') || max <= 0)
3765 return -EINVAL;
3766 mddev->sync_speed_max = max;
3767 return len;
3768}
3769
3770static struct md_sysfs_entry md_sync_max =
3771__ATTR(sync_speed_max, S_IRUGO|S_IWUSR, sync_max_show, sync_max_store);
3772
Iustin Popd7f3d292007-10-16 23:30:54 -07003773static ssize_t
3774degraded_show(mddev_t *mddev, char *page)
3775{
3776 return sprintf(page, "%d\n", mddev->degraded);
3777}
3778static struct md_sysfs_entry md_degraded = __ATTR_RO(degraded);
NeilBrown88202a02006-01-06 00:21:36 -08003779
3780static ssize_t
Bernd Schubert90b08712008-05-23 13:04:38 -07003781sync_force_parallel_show(mddev_t *mddev, char *page)
3782{
3783 return sprintf(page, "%d\n", mddev->parallel_resync);
3784}
3785
3786static ssize_t
3787sync_force_parallel_store(mddev_t *mddev, const char *buf, size_t len)
3788{
3789 long n;
3790
3791 if (strict_strtol(buf, 10, &n))
3792 return -EINVAL;
3793
3794 if (n != 0 && n != 1)
3795 return -EINVAL;
3796
3797 mddev->parallel_resync = n;
3798
3799 if (mddev->sync_thread)
3800 wake_up(&resync_wait);
3801
3802 return len;
3803}
3804
3805/* force parallel resync, even with shared block devices */
3806static struct md_sysfs_entry md_sync_force_parallel =
3807__ATTR(sync_force_parallel, S_IRUGO|S_IWUSR,
3808 sync_force_parallel_show, sync_force_parallel_store);
3809
3810static ssize_t
NeilBrown88202a02006-01-06 00:21:36 -08003811sync_speed_show(mddev_t *mddev, char *page)
3812{
3813 unsigned long resync, dt, db;
NeilBrownd1a7c502009-03-31 15:24:32 +11003814 if (mddev->curr_resync == 0)
3815 return sprintf(page, "none\n");
Andre Noll9687a602008-03-25 22:24:09 +01003816 resync = mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active);
3817 dt = (jiffies - mddev->resync_mark) / HZ;
NeilBrown88202a02006-01-06 00:21:36 -08003818 if (!dt) dt++;
Andre Noll9687a602008-03-25 22:24:09 +01003819 db = resync - mddev->resync_mark_cnt;
3820 return sprintf(page, "%lu\n", db/dt/2); /* K/sec */
NeilBrown88202a02006-01-06 00:21:36 -08003821}
3822
NeilBrown80ca3a42006-07-10 04:44:18 -07003823static struct md_sysfs_entry md_sync_speed = __ATTR_RO(sync_speed);
NeilBrown88202a02006-01-06 00:21:36 -08003824
3825static ssize_t
3826sync_completed_show(mddev_t *mddev, char *page)
3827{
Andre Noll58c0fed2009-03-31 14:33:13 +11003828 unsigned long max_sectors, resync;
NeilBrown88202a02006-01-06 00:21:36 -08003829
NeilBrownacb180b2009-04-14 16:28:34 +10003830 if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3831 return sprintf(page, "none\n");
3832
NeilBrown88202a02006-01-06 00:21:36 -08003833 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11003834 max_sectors = mddev->resync_max_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003835 else
Andre Noll58c0fed2009-03-31 14:33:13 +11003836 max_sectors = mddev->dev_sectors;
NeilBrown88202a02006-01-06 00:21:36 -08003837
NeilBrownacb180b2009-04-14 16:28:34 +10003838 resync = mddev->curr_resync_completed;
Andre Noll58c0fed2009-03-31 14:33:13 +11003839 return sprintf(page, "%lu / %lu\n", resync, max_sectors);
NeilBrown88202a02006-01-06 00:21:36 -08003840}
3841
NeilBrown80ca3a42006-07-10 04:44:18 -07003842static struct md_sysfs_entry md_sync_completed = __ATTR_RO(sync_completed);
NeilBrown88202a02006-01-06 00:21:36 -08003843
NeilBrowne464eaf2006-03-27 01:18:14 -08003844static ssize_t
Neil Brown5e96ee62008-06-28 08:31:24 +10003845min_sync_show(mddev_t *mddev, char *page)
3846{
3847 return sprintf(page, "%llu\n",
3848 (unsigned long long)mddev->resync_min);
3849}
3850static ssize_t
3851min_sync_store(mddev_t *mddev, const char *buf, size_t len)
3852{
3853 unsigned long long min;
3854 if (strict_strtoull(buf, 10, &min))
3855 return -EINVAL;
3856 if (min > mddev->resync_max)
3857 return -EINVAL;
3858 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3859 return -EBUSY;
3860
3861 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003862 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003863 sector_t temp = min;
Andre Noll9d8f0362009-06-18 08:45:01 +10003864 if (sector_div(temp, mddev->chunk_sectors))
Neil Brown5e96ee62008-06-28 08:31:24 +10003865 return -EINVAL;
3866 }
3867 mddev->resync_min = min;
3868
3869 return len;
3870}
3871
3872static struct md_sysfs_entry md_min_sync =
3873__ATTR(sync_min, S_IRUGO|S_IWUSR, min_sync_show, min_sync_store);
3874
3875static ssize_t
NeilBrownc6207272008-02-06 01:39:52 -08003876max_sync_show(mddev_t *mddev, char *page)
3877{
3878 if (mddev->resync_max == MaxSector)
3879 return sprintf(page, "max\n");
3880 else
3881 return sprintf(page, "%llu\n",
3882 (unsigned long long)mddev->resync_max);
3883}
3884static ssize_t
3885max_sync_store(mddev_t *mddev, const char *buf, size_t len)
3886{
3887 if (strncmp(buf, "max", 3) == 0)
3888 mddev->resync_max = MaxSector;
3889 else {
Neil Brown5e96ee62008-06-28 08:31:24 +10003890 unsigned long long max;
3891 if (strict_strtoull(buf, 10, &max))
3892 return -EINVAL;
3893 if (max < mddev->resync_min)
NeilBrownc6207272008-02-06 01:39:52 -08003894 return -EINVAL;
3895 if (max < mddev->resync_max &&
NeilBrown4d484a42009-08-13 10:41:50 +10003896 mddev->ro == 0 &&
NeilBrownc6207272008-02-06 01:39:52 -08003897 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3898 return -EBUSY;
3899
3900 /* Must be a multiple of chunk_size */
Andre Noll9d8f0362009-06-18 08:45:01 +10003901 if (mddev->chunk_sectors) {
raz ben yehuda2ac06c32009-06-16 17:01:42 +10003902 sector_t temp = max;
Andre Noll9d8f0362009-06-18 08:45:01 +10003903 if (sector_div(temp, mddev->chunk_sectors))
NeilBrownc6207272008-02-06 01:39:52 -08003904 return -EINVAL;
3905 }
3906 mddev->resync_max = max;
3907 }
3908 wake_up(&mddev->recovery_wait);
3909 return len;
3910}
3911
3912static struct md_sysfs_entry md_max_sync =
3913__ATTR(sync_max, S_IRUGO|S_IWUSR, max_sync_show, max_sync_store);
3914
3915static ssize_t
NeilBrowne464eaf2006-03-27 01:18:14 -08003916suspend_lo_show(mddev_t *mddev, char *page)
3917{
3918 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_lo);
3919}
3920
3921static ssize_t
3922suspend_lo_store(mddev_t *mddev, const char *buf, size_t len)
3923{
3924 char *e;
3925 unsigned long long new = simple_strtoull(buf, &e, 10);
3926
NeilBrownb8d966e2009-07-01 11:14:04 +10003927 if (mddev->pers == NULL ||
3928 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003929 return -EINVAL;
3930 if (buf == e || (*e && *e != '\n'))
3931 return -EINVAL;
3932 if (new >= mddev->suspend_hi ||
3933 (new > mddev->suspend_lo && new < mddev->suspend_hi)) {
3934 mddev->suspend_lo = new;
3935 mddev->pers->quiesce(mddev, 2);
3936 return len;
3937 } else
3938 return -EINVAL;
3939}
3940static struct md_sysfs_entry md_suspend_lo =
3941__ATTR(suspend_lo, S_IRUGO|S_IWUSR, suspend_lo_show, suspend_lo_store);
3942
3943
3944static ssize_t
3945suspend_hi_show(mddev_t *mddev, char *page)
3946{
3947 return sprintf(page, "%llu\n", (unsigned long long)mddev->suspend_hi);
3948}
3949
3950static ssize_t
3951suspend_hi_store(mddev_t *mddev, const char *buf, size_t len)
3952{
3953 char *e;
3954 unsigned long long new = simple_strtoull(buf, &e, 10);
3955
NeilBrownb8d966e2009-07-01 11:14:04 +10003956 if (mddev->pers == NULL ||
3957 mddev->pers->quiesce == NULL)
NeilBrowne464eaf2006-03-27 01:18:14 -08003958 return -EINVAL;
3959 if (buf == e || (*e && *e != '\n'))
3960 return -EINVAL;
3961 if ((new <= mddev->suspend_lo && mddev->suspend_lo >= mddev->suspend_hi) ||
3962 (new > mddev->suspend_lo && new > mddev->suspend_hi)) {
3963 mddev->suspend_hi = new;
3964 mddev->pers->quiesce(mddev, 1);
3965 mddev->pers->quiesce(mddev, 0);
3966 return len;
3967 } else
3968 return -EINVAL;
3969}
3970static struct md_sysfs_entry md_suspend_hi =
3971__ATTR(suspend_hi, S_IRUGO|S_IWUSR, suspend_hi_show, suspend_hi_store);
3972
NeilBrown08a02ec2007-05-09 02:35:38 -07003973static ssize_t
3974reshape_position_show(mddev_t *mddev, char *page)
3975{
3976 if (mddev->reshape_position != MaxSector)
3977 return sprintf(page, "%llu\n",
3978 (unsigned long long)mddev->reshape_position);
3979 strcpy(page, "none\n");
3980 return 5;
3981}
3982
3983static ssize_t
3984reshape_position_store(mddev_t *mddev, const char *buf, size_t len)
3985{
3986 char *e;
3987 unsigned long long new = simple_strtoull(buf, &e, 10);
3988 if (mddev->pers)
3989 return -EBUSY;
3990 if (buf == e || (*e && *e != '\n'))
3991 return -EINVAL;
3992 mddev->reshape_position = new;
3993 mddev->delta_disks = 0;
3994 mddev->new_level = mddev->level;
3995 mddev->new_layout = mddev->layout;
Andre Noll664e7c42009-06-18 08:45:27 +10003996 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrown08a02ec2007-05-09 02:35:38 -07003997 return len;
3998}
3999
4000static struct md_sysfs_entry md_reshape_position =
4001__ATTR(reshape_position, S_IRUGO|S_IWUSR, reshape_position_show,
4002 reshape_position_store);
4003
Dan Williamsb522adc2009-03-31 15:00:31 +11004004static ssize_t
4005array_size_show(mddev_t *mddev, char *page)
4006{
4007 if (mddev->external_size)
4008 return sprintf(page, "%llu\n",
4009 (unsigned long long)mddev->array_sectors/2);
4010 else
4011 return sprintf(page, "default\n");
4012}
4013
4014static ssize_t
4015array_size_store(mddev_t *mddev, const char *buf, size_t len)
4016{
4017 sector_t sectors;
4018
4019 if (strncmp(buf, "default", 7) == 0) {
4020 if (mddev->pers)
4021 sectors = mddev->pers->size(mddev, 0, 0);
4022 else
4023 sectors = mddev->array_sectors;
4024
4025 mddev->external_size = 0;
4026 } else {
4027 if (strict_blocks_to_sectors(buf, &sectors) < 0)
4028 return -EINVAL;
4029 if (mddev->pers && mddev->pers->size(mddev, 0, 0) < sectors)
NeilBrown2b69c832009-05-26 09:41:17 +10004030 return -E2BIG;
Dan Williamsb522adc2009-03-31 15:00:31 +11004031
4032 mddev->external_size = 1;
4033 }
4034
4035 mddev->array_sectors = sectors;
4036 set_capacity(mddev->gendisk, mddev->array_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10004037 if (mddev->pers)
4038 revalidate_disk(mddev->gendisk);
Dan Williamsb522adc2009-03-31 15:00:31 +11004039
4040 return len;
4041}
4042
4043static struct md_sysfs_entry md_array_size =
4044__ATTR(array_size, S_IRUGO|S_IWUSR, array_size_show,
4045 array_size_store);
NeilBrowne464eaf2006-03-27 01:18:14 -08004046
NeilBrowneae17012005-11-08 21:39:23 -08004047static struct attribute *md_default_attrs[] = {
4048 &md_level.attr,
NeilBrownd4dbd022006-06-26 00:27:59 -07004049 &md_layout.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004050 &md_raid_disks.attr,
NeilBrown3b343802006-01-06 00:20:47 -08004051 &md_chunk_size.attr,
NeilBrowna35b0d62006-01-06 00:20:49 -08004052 &md_size.attr,
NeilBrowna94213b2006-06-26 00:28:00 -07004053 &md_resync_start.attr,
NeilBrown8bb93aa2006-01-06 00:20:50 -08004054 &md_metadata.attr,
NeilBrown6d7ff732006-01-06 00:21:16 -08004055 &md_new_device.attr,
NeilBrown16f17b32006-06-26 00:27:37 -07004056 &md_safe_delay.attr,
NeilBrown9e653b62006-06-26 00:27:58 -07004057 &md_array_state.attr,
NeilBrown08a02ec2007-05-09 02:35:38 -07004058 &md_reshape_position.attr,
Dan Williamsb522adc2009-03-31 15:00:31 +11004059 &md_array_size.attr,
Robert Becker1e509152009-12-14 12:49:58 +11004060 &max_corr_read_errors.attr,
NeilBrown411036f2005-11-08 21:39:40 -08004061 NULL,
4062};
4063
4064static struct attribute *md_redundancy_attrs[] = {
NeilBrown24dd4692005-11-08 21:39:26 -08004065 &md_scan_mode.attr,
NeilBrown9d888832005-11-08 21:39:26 -08004066 &md_mismatches.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004067 &md_sync_min.attr,
4068 &md_sync_max.attr,
4069 &md_sync_speed.attr,
Bernd Schubert90b08712008-05-23 13:04:38 -07004070 &md_sync_force_parallel.attr,
NeilBrown88202a02006-01-06 00:21:36 -08004071 &md_sync_completed.attr,
Neil Brown5e96ee62008-06-28 08:31:24 +10004072 &md_min_sync.attr,
NeilBrownc6207272008-02-06 01:39:52 -08004073 &md_max_sync.attr,
NeilBrowne464eaf2006-03-27 01:18:14 -08004074 &md_suspend_lo.attr,
4075 &md_suspend_hi.attr,
Paul Clements9b1d1da2006-10-03 01:15:49 -07004076 &md_bitmap.attr,
Iustin Popd7f3d292007-10-16 23:30:54 -07004077 &md_degraded.attr,
NeilBrowneae17012005-11-08 21:39:23 -08004078 NULL,
4079};
NeilBrown411036f2005-11-08 21:39:40 -08004080static struct attribute_group md_redundancy_group = {
4081 .name = NULL,
4082 .attrs = md_redundancy_attrs,
4083};
4084
NeilBrowneae17012005-11-08 21:39:23 -08004085
4086static ssize_t
4087md_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
4088{
4089 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4090 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004091 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004092
4093 if (!entry->show)
4094 return -EIO;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004095 rv = mddev_lock(mddev);
4096 if (!rv) {
4097 rv = entry->show(mddev, page);
4098 mddev_unlock(mddev);
4099 }
NeilBrown96de1e62005-11-08 21:39:39 -08004100 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004101}
4102
4103static ssize_t
4104md_attr_store(struct kobject *kobj, struct attribute *attr,
4105 const char *page, size_t length)
4106{
4107 struct md_sysfs_entry *entry = container_of(attr, struct md_sysfs_entry, attr);
4108 mddev_t *mddev = container_of(kobj, struct mddev_s, kobj);
NeilBrown96de1e62005-11-08 21:39:39 -08004109 ssize_t rv;
NeilBrowneae17012005-11-08 21:39:23 -08004110
4111 if (!entry->store)
4112 return -EIO;
NeilBrown67463ac2006-07-10 04:44:19 -07004113 if (!capable(CAP_SYS_ADMIN))
4114 return -EACCES;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004115 rv = mddev_lock(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11004116 if (mddev->hold_active == UNTIL_IOCTL)
4117 mddev->hold_active = 0;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07004118 if (!rv) {
4119 rv = entry->store(mddev, page, length);
4120 mddev_unlock(mddev);
4121 }
NeilBrown96de1e62005-11-08 21:39:39 -08004122 return rv;
NeilBrowneae17012005-11-08 21:39:23 -08004123}
4124
4125static void md_free(struct kobject *ko)
4126{
4127 mddev_t *mddev = container_of(ko, mddev_t, kobj);
NeilBrowna21d1502009-01-09 08:31:09 +11004128
4129 if (mddev->sysfs_state)
4130 sysfs_put(mddev->sysfs_state);
4131
4132 if (mddev->gendisk) {
4133 del_gendisk(mddev->gendisk);
4134 put_disk(mddev->gendisk);
4135 }
4136 if (mddev->queue)
4137 blk_cleanup_queue(mddev->queue);
4138
NeilBrowneae17012005-11-08 21:39:23 -08004139 kfree(mddev);
4140}
4141
4142static struct sysfs_ops md_sysfs_ops = {
4143 .show = md_attr_show,
4144 .store = md_attr_store,
4145};
4146static struct kobj_type md_ktype = {
4147 .release = md_free,
4148 .sysfs_ops = &md_sysfs_ops,
4149 .default_attrs = md_default_attrs,
4150};
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152int mdp_major = 0;
4153
Dan Williams5fd3a172009-03-04 00:57:25 -07004154static void mddev_delayed_delete(struct work_struct *ws)
4155{
4156 mddev_t *mddev = container_of(ws, mddev_t, del_work);
4157
NeilBrown43a70502009-12-14 12:49:55 +11004158 sysfs_remove_group(&mddev->kobj, &md_bitmap_group);
Dan Williams5fd3a172009-03-04 00:57:25 -07004159 kobject_del(&mddev->kobj);
4160 kobject_put(&mddev->kobj);
4161}
4162
NeilBrownefeb53c2009-01-09 08:31:10 +11004163static int md_alloc(dev_t dev, char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004165 static DEFINE_MUTEX(disks_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 mddev_t *mddev = mddev_find(dev);
4167 struct gendisk *disk;
NeilBrownefeb53c2009-01-09 08:31:10 +11004168 int partitioned;
4169 int shift;
4170 int unit;
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004171 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
4173 if (!mddev)
NeilBrownefeb53c2009-01-09 08:31:10 +11004174 return -ENODEV;
4175
4176 partitioned = (MAJOR(mddev->unit) != MD_MAJOR);
4177 shift = partitioned ? MdpMinorShift : 0;
4178 unit = MINOR(mddev->unit) >> shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179
NeilBrownd3374822009-01-09 08:31:10 +11004180 /* wait for any previous instance if this device
4181 * to be completed removed (mddev_delayed_delete).
4182 */
4183 flush_scheduled_work();
4184
Arjan van de Ven48c9c272006-03-27 01:18:20 -08004185 mutex_lock(&disks_mutex);
NeilBrown0909dc42009-07-01 12:27:21 +10004186 error = -EEXIST;
4187 if (mddev->gendisk)
4188 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004189
4190 if (name) {
4191 /* Need to ensure that 'name' is not a duplicate.
4192 */
4193 mddev_t *mddev2;
4194 spin_lock(&all_mddevs_lock);
4195
4196 list_for_each_entry(mddev2, &all_mddevs, all_mddevs)
4197 if (mddev2->gendisk &&
4198 strcmp(mddev2->gendisk->disk_name, name) == 0) {
4199 spin_unlock(&all_mddevs_lock);
NeilBrown0909dc42009-07-01 12:27:21 +10004200 goto abort;
NeilBrownefeb53c2009-01-09 08:31:10 +11004201 }
4202 spin_unlock(&all_mddevs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
NeilBrown8b765392009-01-09 08:31:08 +11004204
NeilBrown0909dc42009-07-01 12:27:21 +10004205 error = -ENOMEM;
NeilBrown8b765392009-01-09 08:31:08 +11004206 mddev->queue = blk_alloc_queue(GFP_KERNEL);
NeilBrown0909dc42009-07-01 12:27:21 +10004207 if (!mddev->queue)
4208 goto abort;
NeilBrown409c57f2009-03-31 14:39:39 +11004209 mddev->queue->queuedata = mddev;
4210
NeilBrown8b765392009-01-09 08:31:08 +11004211 /* Can be unlocked because the queue is new: no concurrency */
4212 queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, mddev->queue);
4213
NeilBrown409c57f2009-03-31 14:39:39 +11004214 blk_queue_make_request(mddev->queue, md_make_request);
NeilBrown8b765392009-01-09 08:31:08 +11004215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 disk = alloc_disk(1 << shift);
4217 if (!disk) {
NeilBrown8b765392009-01-09 08:31:08 +11004218 blk_cleanup_queue(mddev->queue);
4219 mddev->queue = NULL;
NeilBrown0909dc42009-07-01 12:27:21 +10004220 goto abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 }
NeilBrownefeb53c2009-01-09 08:31:10 +11004222 disk->major = MAJOR(mddev->unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 disk->first_minor = unit << shift;
NeilBrownefeb53c2009-01-09 08:31:10 +11004224 if (name)
4225 strcpy(disk->disk_name, name);
4226 else if (partitioned)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 sprintf(disk->disk_name, "md_d%d", unit);
Greg Kroah-Hartmance7b0f42005-06-20 21:15:16 -07004228 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 sprintf(disk->disk_name, "md%d", unit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 disk->fops = &md_fops;
4231 disk->private_data = mddev;
4232 disk->queue = mddev->queue;
NeilBrown92850bb2008-10-21 13:25:32 +11004233 /* Allow extended partitions. This makes the
NeilBrownd3374822009-01-09 08:31:10 +11004234 * 'mdp' device redundant, but we can't really
NeilBrown92850bb2008-10-21 13:25:32 +11004235 * remove it now.
4236 */
4237 disk->flags |= GENHD_FL_EXT_DEVT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 add_disk(disk);
4239 mddev->gendisk = disk;
Tejun Heoed9e1982008-08-25 19:56:05 +09004240 error = kobject_init_and_add(&mddev->kobj, &md_ktype,
4241 &disk_to_dev(disk)->kobj, "%s", "md");
NeilBrown0909dc42009-07-01 12:27:21 +10004242 if (error) {
4243 /* This isn't possible, but as kobject_init_and_add is marked
4244 * __must_check, we must do something with the result
4245 */
NeilBrown5e55e2f2007-03-26 21:32:14 -08004246 printk(KERN_WARNING "md: cannot register %s/md - name in use\n",
4247 disk->disk_name);
NeilBrown0909dc42009-07-01 12:27:21 +10004248 error = 0;
4249 }
NeilBrown43a70502009-12-14 12:49:55 +11004250 if (sysfs_create_group(&mddev->kobj, &md_bitmap_group))
4251 printk(KERN_DEBUG "pointless warning\n");
NeilBrown0909dc42009-07-01 12:27:21 +10004252 abort:
4253 mutex_unlock(&disks_mutex);
4254 if (!error) {
Greg Kroah-Hartman3830c622007-12-17 15:54:39 -04004255 kobject_uevent(&mddev->kobj, KOBJ_ADD);
NeilBrownb62b7592008-10-21 13:25:21 +11004256 mddev->sysfs_state = sysfs_get_dirent(mddev->kobj.sd, "array_state");
4257 }
NeilBrownd3374822009-01-09 08:31:10 +11004258 mddev_put(mddev);
NeilBrown0909dc42009-07-01 12:27:21 +10004259 return error;
NeilBrownefeb53c2009-01-09 08:31:10 +11004260}
4261
4262static struct kobject *md_probe(dev_t dev, int *part, void *data)
4263{
4264 md_alloc(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return NULL;
4266}
4267
NeilBrownefeb53c2009-01-09 08:31:10 +11004268static int add_named_array(const char *val, struct kernel_param *kp)
4269{
4270 /* val must be "md_*" where * is not all digits.
4271 * We allocate an array with a large free minor number, and
4272 * set the name to val. val must not already be an active name.
4273 */
4274 int len = strlen(val);
4275 char buf[DISK_NAME_LEN];
4276
4277 while (len && val[len-1] == '\n')
4278 len--;
4279 if (len >= DISK_NAME_LEN)
4280 return -E2BIG;
4281 strlcpy(buf, val, len+1);
4282 if (strncmp(buf, "md_", 3) != 0)
4283 return -EINVAL;
4284 return md_alloc(0, buf);
4285}
4286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287static void md_safemode_timeout(unsigned long data)
4288{
4289 mddev_t *mddev = (mddev_t *) data;
4290
Neil Brown0fd62b82008-06-28 08:31:36 +10004291 if (!atomic_read(&mddev->writes_pending)) {
4292 mddev->safemode = 1;
4293 if (mddev->external)
NeilBrownb62b7592008-10-21 13:25:21 +11004294 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10004295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 md_wakeup_thread(mddev->thread);
4297}
4298
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004299static int start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
NeilBrownfe60b012010-03-29 11:10:42 +11004301static int md_run(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
NeilBrown2604b702006-01-06 00:20:36 -08004303 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 mdk_rdev_t *rdev;
NeilBrown2604b702006-01-06 00:20:36 -08004305 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306
NeilBrowna757e642005-04-16 15:26:42 -07004307 if (list_empty(&mddev->disks))
4308 /* cannot run an array with no devices.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 if (mddev->pers)
4312 return -EBUSY;
4313
NeilBrownb6eb1272010-04-15 10:13:47 +10004314 /* These two calls synchronise us with the
4315 * sysfs_remove_group calls in mddev_unlock,
4316 * so they must have completed.
4317 */
4318 mutex_lock(&mddev->open_mutex);
4319 mutex_unlock(&mddev->open_mutex);
4320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 /*
4322 * Analyze all RAID superblock(s)
4323 */
NeilBrown1ec4a932008-02-06 01:39:53 -08004324 if (!mddev->raid_disks) {
4325 if (!mddev->persistent)
4326 return -EINVAL;
NeilBrowna757e642005-04-16 15:26:42 -07004327 analyze_sbs(mddev);
NeilBrown1ec4a932008-02-06 01:39:53 -08004328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329
NeilBrownd9d166c2006-01-06 00:20:51 -08004330 if (mddev->level != LEVEL_NONE)
4331 request_module("md-level-%d", mddev->level);
4332 else if (mddev->clevel[0])
4333 request_module("md-%s", mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
4335 /*
4336 * Drop all container device buffers, from now on
4337 * the only valid external interface is through the md
4338 * device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004340 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrownb2d444d2005-11-08 21:39:31 -08004341 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 continue;
4343 sync_blockdev(rdev->bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07004344 invalidate_bdev(rdev->bdev);
NeilBrownf0d76d72007-07-17 04:06:12 -07004345
4346 /* perform some consistency tests on the device.
4347 * We don't want the data to overlap the metadata,
Andre Noll58c0fed2009-03-31 14:33:13 +11004348 * Internal Bitmap issues have been handled elsewhere.
NeilBrownf0d76d72007-07-17 04:06:12 -07004349 */
Andre Noll0f420352008-07-11 22:02:23 +10004350 if (rdev->data_offset < rdev->sb_start) {
Andre Noll58c0fed2009-03-31 14:33:13 +11004351 if (mddev->dev_sectors &&
4352 rdev->data_offset + mddev->dev_sectors
Andre Noll0f420352008-07-11 22:02:23 +10004353 > rdev->sb_start) {
NeilBrownf0d76d72007-07-17 04:06:12 -07004354 printk("md: %s: data overlaps metadata\n",
4355 mdname(mddev));
4356 return -EINVAL;
4357 }
4358 } else {
Andre Noll0f420352008-07-11 22:02:23 +10004359 if (rdev->sb_start + rdev->sb_size/512
NeilBrownf0d76d72007-07-17 04:06:12 -07004360 > rdev->data_offset) {
4361 printk("md: %s: metadata overlaps data\n",
4362 mdname(mddev));
4363 return -EINVAL;
4364 }
4365 }
NeilBrown3c0ee632008-10-21 13:25:28 +11004366 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 }
4368
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 spin_lock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004370 pers = find_pers(mddev->level, mddev->clevel);
NeilBrown2604b702006-01-06 00:20:36 -08004371 if (!pers || !try_module_get(pers->owner)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 spin_unlock(&pers_lock);
NeilBrownd9d166c2006-01-06 00:20:51 -08004373 if (mddev->level != LEVEL_NONE)
4374 printk(KERN_WARNING "md: personality for level %d is not loaded!\n",
4375 mddev->level);
4376 else
4377 printk(KERN_WARNING "md: personality for level %s is not loaded!\n",
4378 mddev->clevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 return -EINVAL;
4380 }
NeilBrown2604b702006-01-06 00:20:36 -08004381 mddev->pers = pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 spin_unlock(&pers_lock);
NeilBrown34817e82009-03-31 14:39:38 +11004383 if (mddev->level != pers->level) {
4384 mddev->level = pers->level;
4385 mddev->new_level = pers->level;
4386 }
NeilBrownd9d166c2006-01-06 00:20:51 -08004387 strlcpy(mddev->clevel, pers->name, sizeof(mddev->clevel));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
NeilBrownf6705572006-03-27 01:18:11 -08004389 if (mddev->reshape_position != MaxSector &&
NeilBrown63c70c42006-03-27 01:18:13 -08004390 pers->start_reshape == NULL) {
NeilBrownf6705572006-03-27 01:18:11 -08004391 /* This personality cannot handle reshaping... */
4392 mddev->pers = NULL;
4393 module_put(pers->owner);
4394 return -EINVAL;
4395 }
4396
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004397 if (pers->sync_request) {
4398 /* Warn if this is a potentially silly
4399 * configuration.
4400 */
4401 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
4402 mdk_rdev_t *rdev2;
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004403 int warned = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004404
4405 list_for_each_entry(rdev, &mddev->disks, same_set)
4406 list_for_each_entry(rdev2, &mddev->disks, same_set) {
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004407 if (rdev < rdev2 &&
4408 rdev->bdev->bd_contains ==
4409 rdev2->bdev->bd_contains) {
4410 printk(KERN_WARNING
4411 "%s: WARNING: %s appears to be"
4412 " on the same physical disk as"
4413 " %s.\n",
4414 mdname(mddev),
4415 bdevname(rdev->bdev,b),
4416 bdevname(rdev2->bdev,b2));
4417 warned = 1;
4418 }
4419 }
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004420
NeilBrown7dd5e7c32007-02-28 20:11:35 -08004421 if (warned)
4422 printk(KERN_WARNING
4423 "True protection against single-disk"
4424 " failure might be compromised.\n");
4425 }
4426
NeilBrown657390d2005-08-26 18:34:16 -07004427 mddev->recovery = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11004428 /* may be over-ridden by personality */
4429 mddev->resync_max_sectors = mddev->dev_sectors;
4430
NeilBrowna9701a32005-11-08 21:39:34 -08004431 mddev->barriers_work = 1;
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08004432 mddev->ok_start_degraded = start_dirty_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433
NeilBrown0f9552b2009-12-30 12:08:50 +11004434 if (start_readonly && mddev->ro == 0)
NeilBrownf91de922005-11-08 21:39:36 -08004435 mddev->ro = 2; /* read-only, but switch on first write */
4436
NeilBrownb15c2e52006-01-06 00:20:16 -08004437 err = mddev->pers->run(mddev);
Andre Noll13e53df2008-03-26 00:07:03 +01004438 if (err)
4439 printk(KERN_ERR "md: pers->run() failed ...\n");
Dan Williamsb522adc2009-03-31 15:00:31 +11004440 else if (mddev->pers->size(mddev, 0, 0) < mddev->array_sectors) {
4441 WARN_ONCE(!mddev->external_size, "%s: default size too small,"
4442 " but 'external_size' not in effect?\n", __func__);
4443 printk(KERN_ERR
4444 "md: invalid array_size %llu > default size %llu\n",
4445 (unsigned long long)mddev->array_sectors / 2,
4446 (unsigned long long)mddev->pers->size(mddev, 0, 0) / 2);
4447 err = -EINVAL;
4448 mddev->pers->stop(mddev);
4449 }
4450 if (err == 0 && mddev->pers->sync_request) {
NeilBrownb15c2e52006-01-06 00:20:16 -08004451 err = bitmap_create(mddev);
4452 if (err) {
4453 printk(KERN_ERR "%s: failed to create bitmap (%d)\n",
4454 mdname(mddev), err);
4455 mddev->pers->stop(mddev);
4456 }
4457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 module_put(mddev->pers->owner);
4460 mddev->pers = NULL;
NeilBrown32a76272005-06-21 17:17:14 -07004461 bitmap_destroy(mddev);
4462 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
NeilBrown5e55e2f2007-03-26 21:32:14 -08004464 if (mddev->pers->sync_request) {
4465 if (sysfs_create_group(&mddev->kobj, &md_redundancy_group))
4466 printk(KERN_WARNING
4467 "md: cannot register extra attributes for %s\n",
4468 mdname(mddev));
NeilBrown0c3573f2009-01-09 08:31:05 +11004469 mddev->sysfs_action = sysfs_get_dirent(mddev->kobj.sd, "sync_action");
NeilBrown5e55e2f2007-03-26 21:32:14 -08004470 } else if (mddev->ro == 2) /* auto-readonly not meaningful */
NeilBrownfd9d49c2005-11-08 21:39:42 -08004471 mddev->ro = 0;
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 atomic_set(&mddev->writes_pending,0);
Robert Becker1e509152009-12-14 12:49:58 +11004474 atomic_set(&mddev->max_corr_read_errors,
4475 MD_DEFAULT_MAX_CORRECTED_READ_ERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 mddev->safemode = 0;
4477 mddev->safemode_timer.function = md_safemode_timeout;
4478 mddev->safemode_timer.data = (unsigned long) mddev;
NeilBrown16f17b32006-06-26 00:27:37 -07004479 mddev->safemode_delay = (200 * HZ)/1000 +1; /* 200 msec delay */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 mddev->in_sync = 1;
NeilBrown86e6ffd2005-11-08 21:39:24 -08004481
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004482 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown86e6ffd2005-11-08 21:39:24 -08004483 if (rdev->raid_disk >= 0) {
4484 char nm[20];
4485 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08004486 if (sysfs_create_link(&mddev->kobj, &rdev->kobj, nm))
4487 printk("md: cannot register %s for %s\n",
4488 nm, mdname(mddev));
NeilBrown86e6ffd2005-11-08 21:39:24 -08004489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490
4491 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4492
NeilBrown850b2b42006-10-03 01:15:46 -07004493 if (mddev->flags)
4494 md_update_sb(mddev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495
NeilBrown0b8c9de2006-07-10 04:44:15 -07004496 md_wakeup_thread(mddev->thread);
4497 md_wakeup_thread(mddev->sync_thread); /* possibly kick off a reshape */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004498
NeilBrownd7603b72006-01-06 00:20:30 -08004499 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004500 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0c3573f2009-01-09 08:31:05 +11004501 if (mddev->sysfs_action)
4502 sysfs_notify_dirent(mddev->sysfs_action);
Neil Browna99ac972008-06-28 08:31:43 +10004503 sysfs_notify(&mddev->kobj, NULL, "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 return 0;
4505}
4506
NeilBrownfe60b012010-03-29 11:10:42 +11004507static int do_md_run(mddev_t *mddev)
4508{
4509 int err;
4510
4511 err = md_run(mddev);
4512 if (err)
4513 goto out;
4514
4515 set_capacity(mddev->gendisk, mddev->array_sectors);
4516 revalidate_disk(mddev->gendisk);
4517 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
4518out:
4519 return err;
4520}
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522static int restart_array(mddev_t *mddev)
4523{
4524 struct gendisk *disk = mddev->gendisk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
Andre Noll80fab1d2008-07-11 22:02:21 +10004526 /* Complain if it has no devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 if (list_empty(&mddev->disks))
Andre Noll80fab1d2008-07-11 22:02:21 +10004528 return -ENXIO;
4529 if (!mddev->pers)
4530 return -EINVAL;
4531 if (!mddev->ro)
4532 return -EBUSY;
4533 mddev->safemode = 0;
4534 mddev->ro = 0;
4535 set_disk_ro(disk, 0);
4536 printk(KERN_INFO "md: %s switched to read-write mode.\n",
4537 mdname(mddev));
4538 /* Kick recovery or resync if necessary */
4539 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4540 md_wakeup_thread(mddev->thread);
4541 md_wakeup_thread(mddev->sync_thread);
NeilBrownb62b7592008-10-21 13:25:21 +11004542 sysfs_notify_dirent(mddev->sysfs_state);
Andre Noll80fab1d2008-07-11 22:02:21 +10004543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544}
4545
NeilBrownacc55e22006-06-26 00:27:47 -07004546/* similar to deny_write_access, but accounts for our holding a reference
4547 * to the file ourselves */
4548static int deny_bitmap_write_access(struct file * file)
4549{
4550 struct inode *inode = file->f_mapping->host;
4551
4552 spin_lock(&inode->i_lock);
4553 if (atomic_read(&inode->i_writecount) > 1) {
4554 spin_unlock(&inode->i_lock);
4555 return -ETXTBSY;
4556 }
4557 atomic_set(&inode->i_writecount, -1);
4558 spin_unlock(&inode->i_lock);
4559
4560 return 0;
4561}
4562
NeilBrown43a70502009-12-14 12:49:55 +11004563void restore_bitmap_write_access(struct file *file)
NeilBrownacc55e22006-06-26 00:27:47 -07004564{
4565 struct inode *inode = file->f_mapping->host;
4566
4567 spin_lock(&inode->i_lock);
4568 atomic_set(&inode->i_writecount, 1);
4569 spin_unlock(&inode->i_lock);
4570}
4571
NeilBrown6177b472010-03-29 11:37:13 +11004572static void md_clean(mddev_t *mddev)
4573{
4574 mddev->array_sectors = 0;
4575 mddev->external_size = 0;
4576 mddev->dev_sectors = 0;
4577 mddev->raid_disks = 0;
4578 mddev->recovery_cp = 0;
4579 mddev->resync_min = 0;
4580 mddev->resync_max = MaxSector;
4581 mddev->reshape_position = MaxSector;
4582 mddev->external = 0;
4583 mddev->persistent = 0;
4584 mddev->level = LEVEL_NONE;
4585 mddev->clevel[0] = 0;
4586 mddev->flags = 0;
4587 mddev->ro = 0;
4588 mddev->metadata_type[0] = 0;
4589 mddev->chunk_sectors = 0;
4590 mddev->ctime = mddev->utime = 0;
4591 mddev->layout = 0;
4592 mddev->max_disks = 0;
4593 mddev->events = 0;
4594 mddev->delta_disks = 0;
4595 mddev->new_level = LEVEL_NONE;
4596 mddev->new_layout = 0;
4597 mddev->new_chunk_sectors = 0;
4598 mddev->curr_resync = 0;
4599 mddev->resync_mismatches = 0;
4600 mddev->suspend_lo = mddev->suspend_hi = 0;
4601 mddev->sync_speed_min = mddev->sync_speed_max = 0;
4602 mddev->recovery = 0;
4603 mddev->in_sync = 0;
4604 mddev->degraded = 0;
4605 mddev->barriers_work = 0;
4606 mddev->safemode = 0;
4607 mddev->bitmap_info.offset = 0;
4608 mddev->bitmap_info.default_offset = 0;
4609 mddev->bitmap_info.chunksize = 0;
4610 mddev->bitmap_info.daemon_sleep = 0;
4611 mddev->bitmap_info.max_write_behind = 0;
4612}
4613
NeilBrowna047e122010-03-29 12:07:53 +11004614static void md_stop_writes(mddev_t *mddev)
4615{
4616 if (mddev->sync_thread) {
4617 set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4618 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4619 md_unregister_thread(mddev->sync_thread);
4620 mddev->sync_thread = NULL;
4621 }
4622
4623 del_timer_sync(&mddev->safemode_timer);
4624
4625 bitmap_flush(mddev);
4626 md_super_wait(mddev);
4627
4628 if (!mddev->in_sync || mddev->flags) {
4629 /* mark array as shutdown cleanly */
4630 mddev->in_sync = 1;
4631 md_update_sb(mddev, 1);
4632 }
4633}
4634
NeilBrown6177b472010-03-29 11:37:13 +11004635static void md_stop(mddev_t *mddev)
4636{
4637 mddev->pers->stop(mddev);
4638 if (mddev->pers->sync_request && mddev->to_remove == NULL)
4639 mddev->to_remove = &md_redundancy_group;
4640 module_put(mddev->pers->owner);
4641 mddev->pers = NULL;
4642
4643}
4644
NeilBrowna4bd82d2010-03-29 13:23:10 +11004645static int md_set_readonly(mddev_t *mddev, int is_open)
4646{
4647 int err = 0;
4648 mutex_lock(&mddev->open_mutex);
4649 if (atomic_read(&mddev->openers) > is_open) {
4650 printk("md: %s still in use.\n",mdname(mddev));
4651 err = -EBUSY;
4652 goto out;
4653 }
4654 if (mddev->pers) {
4655 md_stop_writes(mddev);
4656
4657 err = -ENXIO;
4658 if (mddev->ro==1)
4659 goto out;
4660 mddev->ro = 1;
4661 set_disk_ro(mddev->gendisk, 1);
4662 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
4663 sysfs_notify_dirent(mddev->sysfs_state);
4664 err = 0;
4665 }
4666out:
4667 mutex_unlock(&mddev->open_mutex);
4668 return err;
4669}
4670
NeilBrown9e653b62006-06-26 00:27:58 -07004671/* mode:
4672 * 0 - completely stop and dis-assemble array
NeilBrown9e653b62006-06-26 00:27:58 -07004673 * 2 - stop but do not disassemble array
4674 */
Neil Browndf5b20c2008-07-11 22:02:22 +10004675static int do_md_stop(mddev_t * mddev, int mode, int is_open)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676{
4677 int err = 0;
4678 struct gendisk *disk = mddev->gendisk;
NeilBrownc4647292009-05-07 12:51:06 +10004679 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680
NeilBrownc8c00a62009-08-10 12:50:52 +10004681 mutex_lock(&mddev->open_mutex);
NeilBrownf2ea68c2008-07-21 17:05:25 +10004682 if (atomic_read(&mddev->openers) > is_open) {
Neil Browndf5b20c2008-07-11 22:02:22 +10004683 printk("md: %s still in use.\n",mdname(mddev));
NeilBrownc8c00a62009-08-10 12:50:52 +10004684 err = -EBUSY;
4685 } else if (mddev->pers) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686
NeilBrowna047e122010-03-29 12:07:53 +11004687 md_stop_writes(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
NeilBrowna4bd82d2010-03-29 13:23:10 +11004689 if (mddev->ro)
4690 set_disk_ro(disk, 0);
NeilBrown409c57f2009-03-31 14:39:39 +11004691
NeilBrowna4bd82d2010-03-29 13:23:10 +11004692 md_stop(mddev);
4693 mddev->queue->merge_bvec_fn = NULL;
4694 mddev->queue->unplug_fn = NULL;
4695 mddev->queue->backing_dev_info.congested_fn = NULL;
NeilBrown6177b472010-03-29 11:37:13 +11004696
NeilBrowna4bd82d2010-03-29 13:23:10 +11004697 /* tell userspace to handle 'inactive' */
4698 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown0d4ca602006-12-10 02:20:44 -08004699
NeilBrowna4bd82d2010-03-29 13:23:10 +11004700 list_for_each_entry(rdev, &mddev->disks, same_set)
4701 if (rdev->raid_disk >= 0) {
4702 char nm[20];
4703 sprintf(nm, "rd%d", rdev->raid_disk);
4704 sysfs_remove_link(&mddev->kobj, nm);
4705 }
NeilBrownc4647292009-05-07 12:51:06 +10004706
NeilBrowna4bd82d2010-03-29 13:23:10 +11004707 set_capacity(disk, 0);
4708 revalidate_disk(disk);
NeilBrown0d4ca602006-12-10 02:20:44 -08004709
NeilBrowna4bd82d2010-03-29 13:23:10 +11004710 if (mddev->ro)
4711 mddev->ro = 0;
4712
NeilBrown5fd6c1d2006-06-26 00:27:40 -07004713 clear_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
NeilBrown80ffb3c2009-08-18 10:35:26 +10004714 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 }
NeilBrownc8c00a62009-08-10 12:50:52 +10004716 mutex_unlock(&mddev->open_mutex);
4717 if (err)
4718 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 /*
4720 * Free resources if final stop
4721 */
NeilBrown9e653b62006-06-26 00:27:58 -07004722 if (mode == 0) {
NeilBrown0d4ca602006-12-10 02:20:44 -08004723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 printk(KERN_INFO "md: %s stopped.\n", mdname(mddev));
4725
NeilBrown978f9462006-02-02 14:28:05 -08004726 bitmap_destroy(mddev);
NeilBrownc3d97142009-12-14 12:49:52 +11004727 if (mddev->bitmap_info.file) {
4728 restore_bitmap_write_access(mddev->bitmap_info.file);
4729 fput(mddev->bitmap_info.file);
4730 mddev->bitmap_info.file = NULL;
NeilBrown978f9462006-02-02 14:28:05 -08004731 }
NeilBrownc3d97142009-12-14 12:49:52 +11004732 mddev->bitmap_info.offset = 0;
NeilBrown978f9462006-02-02 14:28:05 -08004733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 export_array(mddev);
4735
NeilBrown6177b472010-03-29 11:37:13 +11004736 md_clean(mddev);
NeilBrown934d9c22008-10-28 17:01:23 +11004737 kobject_uevent(&disk_to_dev(mddev->gendisk)->kobj, KOBJ_CHANGE);
NeilBrownefeb53c2009-01-09 08:31:10 +11004738 if (mddev->hold_active == UNTIL_STOP)
4739 mddev->hold_active = 0;
NeilBrown9e653b62006-06-26 00:27:58 -07004740
NeilBrowna4bd82d2010-03-29 13:23:10 +11004741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 err = 0;
Martin K. Petersen3f9d99c2009-03-31 14:27:02 +11004743 blk_integrity_unregister(disk);
NeilBrownd7603b72006-01-06 00:20:30 -08004744 md_new_event(mddev);
NeilBrownb62b7592008-10-21 13:25:21 +11004745 sysfs_notify_dirent(mddev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 return err;
4747}
4748
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004749#ifndef MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750static void autorun_array(mddev_t *mddev)
4751{
4752 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 int err;
4754
NeilBrowna757e642005-04-16 15:26:42 -07004755 if (list_empty(&mddev->disks))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 printk(KERN_INFO "md: running: ");
4759
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004760 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 char b[BDEVNAME_SIZE];
4762 printk("<%s>", bdevname(rdev->bdev,b));
4763 }
4764 printk("\n");
4765
NeilBrownd710e132008-10-13 11:55:12 +11004766 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 if (err) {
4768 printk(KERN_WARNING "md: do_md_run() returned %d\n", err);
NeilBrownd710e132008-10-13 11:55:12 +11004769 do_md_stop(mddev, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 }
4771}
4772
4773/*
4774 * lets try to run arrays based on all disks that have arrived
4775 * until now. (those are in pending_raid_disks)
4776 *
4777 * the method: pick the first pending disk, collect all disks with
4778 * the same UUID, remove all from the pending list and put them into
4779 * the 'same_array' list. Then order this list based on superblock
4780 * update time (freshest comes first), kick out 'old' disks and
4781 * compare superblocks. If everything's fine then run it.
4782 *
4783 * If "unit" is allocated, then bump its reference count
4784 */
4785static void autorun_devices(int part)
4786{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004787 mdk_rdev_t *rdev0, *rdev, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 mddev_t *mddev;
4789 char b[BDEVNAME_SIZE];
4790
4791 printk(KERN_INFO "md: autorun ...\n");
4792 while (!list_empty(&pending_raid_disks)) {
NeilBrowne8703fe2006-10-03 01:15:59 -07004793 int unit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 dev_t dev;
NeilBrownad01c9e2006-03-27 01:18:07 -08004795 LIST_HEAD(candidates);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 rdev0 = list_entry(pending_raid_disks.next,
4797 mdk_rdev_t, same_set);
4798
4799 printk(KERN_INFO "md: considering %s ...\n",
4800 bdevname(rdev0->bdev,b));
4801 INIT_LIST_HEAD(&candidates);
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004802 rdev_for_each_list(rdev, tmp, &pending_raid_disks)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803 if (super_90_load(rdev, rdev0, 0) >= 0) {
4804 printk(KERN_INFO "md: adding %s ...\n",
4805 bdevname(rdev->bdev,b));
4806 list_move(&rdev->same_set, &candidates);
4807 }
4808 /*
4809 * now we have a set of devices, with all of them having
4810 * mostly sane superblocks. It's time to allocate the
4811 * mddev.
4812 */
NeilBrowne8703fe2006-10-03 01:15:59 -07004813 if (part) {
4814 dev = MKDEV(mdp_major,
4815 rdev0->preferred_minor << MdpMinorShift);
4816 unit = MINOR(dev) >> MdpMinorShift;
4817 } else {
4818 dev = MKDEV(MD_MAJOR, rdev0->preferred_minor);
4819 unit = MINOR(dev);
4820 }
4821 if (rdev0->preferred_minor != unit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822 printk(KERN_INFO "md: unit number in %s is bad: %d\n",
4823 bdevname(rdev0->bdev, b), rdev0->preferred_minor);
4824 break;
4825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
4827 md_probe(dev, NULL, NULL);
4828 mddev = mddev_find(dev);
Neil Brown9bbbca32008-06-28 08:31:17 +10004829 if (!mddev || !mddev->gendisk) {
4830 if (mddev)
4831 mddev_put(mddev);
4832 printk(KERN_ERR
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 "md: cannot allocate memory for md drive.\n");
4834 break;
4835 }
4836 if (mddev_lock(mddev))
4837 printk(KERN_WARNING "md: %s locked, cannot run\n",
4838 mdname(mddev));
4839 else if (mddev->raid_disks || mddev->major_version
4840 || !list_empty(&mddev->disks)) {
4841 printk(KERN_WARNING
4842 "md: %s already running, cannot run %s\n",
4843 mdname(mddev), bdevname(rdev0->bdev,b));
4844 mddev_unlock(mddev);
4845 } else {
4846 printk(KERN_INFO "md: created %s\n", mdname(mddev));
NeilBrown1ec4a932008-02-06 01:39:53 -08004847 mddev->persistent = 1;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004848 rdev_for_each_list(rdev, tmp, &candidates) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 list_del_init(&rdev->same_set);
4850 if (bind_rdev_to_array(rdev, mddev))
4851 export_rdev(rdev);
4852 }
4853 autorun_array(mddev);
4854 mddev_unlock(mddev);
4855 }
4856 /* on success, candidates will be empty, on error
4857 * it won't...
4858 */
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004859 rdev_for_each_list(rdev, tmp, &candidates) {
NeilBrown4b809912008-07-21 17:05:25 +10004860 list_del_init(&rdev->same_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 export_rdev(rdev);
NeilBrown4b809912008-07-21 17:05:25 +10004862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 mddev_put(mddev);
4864 }
4865 printk(KERN_INFO "md: ... autorun DONE.\n");
4866}
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08004867#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869static int get_version(void __user * arg)
4870{
4871 mdu_version_t ver;
4872
4873 ver.major = MD_MAJOR_VERSION;
4874 ver.minor = MD_MINOR_VERSION;
4875 ver.patchlevel = MD_PATCHLEVEL_VERSION;
4876
4877 if (copy_to_user(arg, &ver, sizeof(ver)))
4878 return -EFAULT;
4879
4880 return 0;
4881}
4882
4883static int get_array_info(mddev_t * mddev, void __user * arg)
4884{
4885 mdu_array_info_t info;
NeilBrowna9f326e2009-09-23 18:06:41 +10004886 int nr,working,insync,failed,spare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
NeilBrowna9f326e2009-09-23 18:06:41 +10004889 nr=working=insync=failed=spare=0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11004890 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 nr++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004892 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 failed++;
4894 else {
4895 working++;
NeilBrownb2d444d2005-11-08 21:39:31 -08004896 if (test_bit(In_sync, &rdev->flags))
NeilBrowna9f326e2009-09-23 18:06:41 +10004897 insync++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 else
4899 spare++;
4900 }
4901 }
4902
4903 info.major_version = mddev->major_version;
4904 info.minor_version = mddev->minor_version;
4905 info.patch_version = MD_PATCHLEVEL_VERSION;
4906 info.ctime = mddev->ctime;
4907 info.level = mddev->level;
Andre Noll58c0fed2009-03-31 14:33:13 +11004908 info.size = mddev->dev_sectors / 2;
4909 if (info.size != mddev->dev_sectors / 2) /* overflow */
NeilBrown284ae7c2006-02-03 03:03:40 -08004910 info.size = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 info.nr_disks = nr;
4912 info.raid_disks = mddev->raid_disks;
4913 info.md_minor = mddev->md_minor;
4914 info.not_persistent= !mddev->persistent;
4915
4916 info.utime = mddev->utime;
4917 info.state = 0;
4918 if (mddev->in_sync)
4919 info.state = (1<<MD_SB_CLEAN);
NeilBrownc3d97142009-12-14 12:49:52 +11004920 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07004921 info.state = (1<<MD_SB_BITMAP_PRESENT);
NeilBrowna9f326e2009-09-23 18:06:41 +10004922 info.active_disks = insync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 info.working_disks = working;
4924 info.failed_disks = failed;
4925 info.spare_disks = spare;
4926
4927 info.layout = mddev->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10004928 info.chunk_size = mddev->chunk_sectors << 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929
4930 if (copy_to_user(arg, &info, sizeof(info)))
4931 return -EFAULT;
4932
4933 return 0;
4934}
4935
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01004936static int get_bitmap_file(mddev_t * mddev, void __user * arg)
NeilBrown32a76272005-06-21 17:17:14 -07004937{
4938 mdu_bitmap_file_t *file = NULL; /* too big for stack allocation */
4939 char *ptr, *buf = NULL;
4940 int err = -ENOMEM;
4941
Dan Williamsb5470dc2008-06-27 21:44:04 -07004942 if (md_allow_write(mddev))
4943 file = kmalloc(sizeof(*file), GFP_NOIO);
4944 else
4945 file = kmalloc(sizeof(*file), GFP_KERNEL);
NeilBrown2a2275d2007-01-26 00:57:11 -08004946
NeilBrown32a76272005-06-21 17:17:14 -07004947 if (!file)
4948 goto out;
4949
4950 /* bitmap disabled, zero the first byte and copy out */
4951 if (!mddev->bitmap || !mddev->bitmap->file) {
4952 file->pathname[0] = '\0';
4953 goto copy_out;
4954 }
4955
4956 buf = kmalloc(sizeof(file->pathname), GFP_KERNEL);
4957 if (!buf)
4958 goto out;
4959
Christoph Hellwig6bcfd602008-05-23 13:04:34 -07004960 ptr = d_path(&mddev->bitmap->file->f_path, buf, sizeof(file->pathname));
4961 if (IS_ERR(ptr))
NeilBrown32a76272005-06-21 17:17:14 -07004962 goto out;
4963
4964 strcpy(file->pathname, ptr);
4965
4966copy_out:
4967 err = 0;
4968 if (copy_to_user(arg, file, sizeof(*file)))
4969 err = -EFAULT;
4970out:
4971 kfree(buf);
4972 kfree(file);
4973 return err;
4974}
4975
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976static int get_disk_info(mddev_t * mddev, void __user * arg)
4977{
4978 mdu_disk_info_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 mdk_rdev_t *rdev;
4980
4981 if (copy_from_user(&info, arg, sizeof(info)))
4982 return -EFAULT;
4983
Andre Noll26ef3792008-07-11 22:02:21 +10004984 rdev = find_rdev_nr(mddev, info.number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985 if (rdev) {
4986 info.major = MAJOR(rdev->bdev->bd_dev);
4987 info.minor = MINOR(rdev->bdev->bd_dev);
4988 info.raid_disk = rdev->raid_disk;
4989 info.state = 0;
NeilBrownb2d444d2005-11-08 21:39:31 -08004990 if (test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 info.state |= (1<<MD_DISK_FAULTY);
NeilBrownb2d444d2005-11-08 21:39:31 -08004992 else if (test_bit(In_sync, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993 info.state |= (1<<MD_DISK_ACTIVE);
4994 info.state |= (1<<MD_DISK_SYNC);
4995 }
NeilBrown8ddf9ef2005-09-09 16:23:45 -07004996 if (test_bit(WriteMostly, &rdev->flags))
4997 info.state |= (1<<MD_DISK_WRITEMOSTLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 } else {
4999 info.major = info.minor = 0;
5000 info.raid_disk = -1;
5001 info.state = (1<<MD_DISK_REMOVED);
5002 }
5003
5004 if (copy_to_user(arg, &info, sizeof(info)))
5005 return -EFAULT;
5006
5007 return 0;
5008}
5009
5010static int add_new_disk(mddev_t * mddev, mdu_disk_info_t *info)
5011{
5012 char b[BDEVNAME_SIZE], b2[BDEVNAME_SIZE];
5013 mdk_rdev_t *rdev;
5014 dev_t dev = MKDEV(info->major,info->minor);
5015
5016 if (info->major != MAJOR(dev) || info->minor != MINOR(dev))
5017 return -EOVERFLOW;
5018
5019 if (!mddev->raid_disks) {
5020 int err;
5021 /* expecting a device which has a superblock */
5022 rdev = md_import_device(dev, mddev->major_version, mddev->minor_version);
5023 if (IS_ERR(rdev)) {
5024 printk(KERN_WARNING
5025 "md: md_import_device returned %ld\n",
5026 PTR_ERR(rdev));
5027 return PTR_ERR(rdev);
5028 }
5029 if (!list_empty(&mddev->disks)) {
5030 mdk_rdev_t *rdev0 = list_entry(mddev->disks.next,
5031 mdk_rdev_t, same_set);
NeilBrowna9f326e2009-09-23 18:06:41 +10005032 err = super_types[mddev->major_version]
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 .load_super(rdev, rdev0, mddev->minor_version);
5034 if (err < 0) {
5035 printk(KERN_WARNING
5036 "md: %s has different UUID to %s\n",
5037 bdevname(rdev->bdev,b),
5038 bdevname(rdev0->bdev,b2));
5039 export_rdev(rdev);
5040 return -EINVAL;
5041 }
5042 }
5043 err = bind_rdev_to_array(rdev, mddev);
5044 if (err)
5045 export_rdev(rdev);
5046 return err;
5047 }
5048
5049 /*
5050 * add_new_disk can be used once the array is assembled
5051 * to add "hot spares". They must already have a superblock
5052 * written
5053 */
5054 if (mddev->pers) {
5055 int err;
5056 if (!mddev->pers->hot_add_disk) {
5057 printk(KERN_WARNING
5058 "%s: personality does not support diskops!\n",
5059 mdname(mddev));
5060 return -EINVAL;
5061 }
NeilBrown7b1e35f2005-09-09 16:23:50 -07005062 if (mddev->persistent)
5063 rdev = md_import_device(dev, mddev->major_version,
5064 mddev->minor_version);
5065 else
5066 rdev = md_import_device(dev, -1, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 if (IS_ERR(rdev)) {
5068 printk(KERN_WARNING
5069 "md: md_import_device returned %ld\n",
5070 PTR_ERR(rdev));
5071 return PTR_ERR(rdev);
5072 }
NeilBrown41158c72005-06-21 17:17:25 -07005073 /* set save_raid_disk if appropriate */
5074 if (!mddev->persistent) {
5075 if (info->state & (1<<MD_DISK_SYNC) &&
5076 info->raid_disk < mddev->raid_disks)
5077 rdev->raid_disk = info->raid_disk;
5078 else
5079 rdev->raid_disk = -1;
5080 } else
5081 super_types[mddev->major_version].
5082 validate_super(mddev, rdev);
5083 rdev->saved_raid_disk = rdev->raid_disk;
5084
NeilBrownb2d444d2005-11-08 21:39:31 -08005085 clear_bit(In_sync, &rdev->flags); /* just to be sure */
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005086 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5087 set_bit(WriteMostly, &rdev->flags);
NeilBrown575a80f2009-03-31 14:33:13 +11005088 else
5089 clear_bit(WriteMostly, &rdev->flags);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005090
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 rdev->raid_disk = -1;
5092 err = bind_rdev_to_array(rdev, mddev);
NeilBrown7c7546c2006-06-26 00:27:41 -07005093 if (!err && !mddev->pers->hot_remove_disk) {
5094 /* If there is hot_add_disk but no hot_remove_disk
5095 * then added disks for geometry changes,
5096 * and should be added immediately.
5097 */
5098 super_types[mddev->major_version].
5099 validate_super(mddev, rdev);
5100 err = mddev->pers->hot_add_disk(mddev, rdev);
5101 if (err)
5102 unbind_rdev_from_array(rdev);
5103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 if (err)
5105 export_rdev(rdev);
Neil Brown52664732008-06-28 08:31:44 +10005106 else
NeilBrown3c0ee632008-10-21 13:25:28 +11005107 sysfs_notify_dirent(rdev->sysfs_state);
NeilBrownc3617772005-06-21 17:17:10 -07005108
NeilBrown17571282006-12-10 02:20:52 -08005109 md_update_sb(mddev, 1);
Neil Brown72a23c22008-06-28 08:31:41 +10005110 if (mddev->degraded)
5111 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc3617772005-06-21 17:17:10 -07005112 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown005eca52005-08-22 13:11:08 -07005113 md_wakeup_thread(mddev->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 return err;
5115 }
5116
5117 /* otherwise, add_new_disk is only allowed
5118 * for major_version==0 superblocks
5119 */
5120 if (mddev->major_version != 0) {
5121 printk(KERN_WARNING "%s: ADD_NEW_DISK not supported\n",
5122 mdname(mddev));
5123 return -EINVAL;
5124 }
5125
5126 if (!(info->state & (1<<MD_DISK_FAULTY))) {
5127 int err;
NeilBrownd710e132008-10-13 11:55:12 +11005128 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 if (IS_ERR(rdev)) {
5130 printk(KERN_WARNING
5131 "md: error, md_import_device() returned %ld\n",
5132 PTR_ERR(rdev));
5133 return PTR_ERR(rdev);
5134 }
5135 rdev->desc_nr = info->number;
5136 if (info->raid_disk < mddev->raid_disks)
5137 rdev->raid_disk = info->raid_disk;
5138 else
5139 rdev->raid_disk = -1;
5140
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 if (rdev->raid_disk < mddev->raid_disks)
NeilBrownb2d444d2005-11-08 21:39:31 -08005142 if (info->state & (1<<MD_DISK_SYNC))
5143 set_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144
NeilBrown8ddf9ef2005-09-09 16:23:45 -07005145 if (info->state & (1<<MD_DISK_WRITEMOSTLY))
5146 set_bit(WriteMostly, &rdev->flags);
5147
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 if (!mddev->persistent) {
5149 printk(KERN_INFO "md: nonpersistent superblock ...\n");
Andre Noll0f420352008-07-11 22:02:23 +10005150 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 } else
Andre Noll0f420352008-07-11 22:02:23 +10005152 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
NeilBrown8190e752009-06-18 08:48:58 +10005153 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154
NeilBrown2bf071b2006-01-06 00:20:55 -08005155 err = bind_rdev_to_array(rdev, mddev);
5156 if (err) {
5157 export_rdev(rdev);
5158 return err;
5159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
5161
5162 return 0;
5163}
5164
5165static int hot_remove_disk(mddev_t * mddev, dev_t dev)
5166{
5167 char b[BDEVNAME_SIZE];
5168 mdk_rdev_t *rdev;
5169
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 rdev = find_rdev(mddev, dev);
5171 if (!rdev)
5172 return -ENXIO;
5173
5174 if (rdev->raid_disk >= 0)
5175 goto busy;
5176
5177 kick_rdev_from_array(rdev);
NeilBrown850b2b42006-10-03 01:15:46 -07005178 md_update_sb(mddev, 1);
NeilBrownd7603b72006-01-06 00:20:30 -08005179 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180
5181 return 0;
5182busy:
Nick Andrewfdefa4d2008-04-21 22:42:58 +00005183 printk(KERN_WARNING "md: cannot remove active disk %s from %s ...\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 bdevname(rdev->bdev,b), mdname(mddev));
5185 return -EBUSY;
5186}
5187
5188static int hot_add_disk(mddev_t * mddev, dev_t dev)
5189{
5190 char b[BDEVNAME_SIZE];
5191 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 mdk_rdev_t *rdev;
5193
5194 if (!mddev->pers)
5195 return -ENODEV;
5196
5197 if (mddev->major_version != 0) {
5198 printk(KERN_WARNING "%s: HOT_ADD may only be used with"
5199 " version-0 superblocks.\n",
5200 mdname(mddev));
5201 return -EINVAL;
5202 }
5203 if (!mddev->pers->hot_add_disk) {
5204 printk(KERN_WARNING
5205 "%s: personality does not support diskops!\n",
5206 mdname(mddev));
5207 return -EINVAL;
5208 }
5209
NeilBrownd710e132008-10-13 11:55:12 +11005210 rdev = md_import_device(dev, -1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 if (IS_ERR(rdev)) {
5212 printk(KERN_WARNING
5213 "md: error, md_import_device() returned %ld\n",
5214 PTR_ERR(rdev));
5215 return -EINVAL;
5216 }
5217
5218 if (mddev->persistent)
Andre Noll0f420352008-07-11 22:02:23 +10005219 rdev->sb_start = calc_dev_sboffset(rdev->bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 else
Andre Noll0f420352008-07-11 22:02:23 +10005221 rdev->sb_start = rdev->bdev->bd_inode->i_size / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222
NeilBrown8190e752009-06-18 08:48:58 +10005223 rdev->sectors = rdev->sb_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224
NeilBrownb2d444d2005-11-08 21:39:31 -08005225 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226 printk(KERN_WARNING
5227 "md: can not hot-add faulty %s disk to %s!\n",
5228 bdevname(rdev->bdev,b), mdname(mddev));
5229 err = -EINVAL;
5230 goto abort_export;
5231 }
NeilBrownb2d444d2005-11-08 21:39:31 -08005232 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 rdev->desc_nr = -1;
NeilBrown58427302006-10-06 00:44:04 -07005234 rdev->saved_raid_disk = -1;
NeilBrown2bf071b2006-01-06 00:20:55 -08005235 err = bind_rdev_to_array(rdev, mddev);
5236 if (err)
5237 goto abort_export;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
5239 /*
5240 * The rest should better be atomic, we can have disk failures
5241 * noticed in interrupt contexts ...
5242 */
5243
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244 rdev->raid_disk = -1;
5245
NeilBrown850b2b42006-10-03 01:15:46 -07005246 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247
5248 /*
5249 * Kick recovery, maybe this spare has to be added to the
5250 * array immediately.
5251 */
5252 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5253 md_wakeup_thread(mddev->thread);
NeilBrownd7603b72006-01-06 00:20:30 -08005254 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 return 0;
5256
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257abort_export:
5258 export_rdev(rdev);
5259 return err;
5260}
5261
NeilBrown32a76272005-06-21 17:17:14 -07005262static int set_bitmap_file(mddev_t *mddev, int fd)
5263{
5264 int err;
5265
NeilBrown36fa3062005-09-09 16:23:45 -07005266 if (mddev->pers) {
5267 if (!mddev->pers->quiesce)
5268 return -EBUSY;
5269 if (mddev->recovery || mddev->sync_thread)
5270 return -EBUSY;
5271 /* we should be able to change the bitmap.. */
NeilBrown32a76272005-06-21 17:17:14 -07005272 }
5273
NeilBrown36fa3062005-09-09 16:23:45 -07005274
5275 if (fd >= 0) {
5276 if (mddev->bitmap)
5277 return -EEXIST; /* cannot add when bitmap is present */
NeilBrownc3d97142009-12-14 12:49:52 +11005278 mddev->bitmap_info.file = fget(fd);
NeilBrown36fa3062005-09-09 16:23:45 -07005279
NeilBrownc3d97142009-12-14 12:49:52 +11005280 if (mddev->bitmap_info.file == NULL) {
NeilBrown36fa3062005-09-09 16:23:45 -07005281 printk(KERN_ERR "%s: error: failed to get bitmap file\n",
5282 mdname(mddev));
5283 return -EBADF;
5284 }
5285
NeilBrownc3d97142009-12-14 12:49:52 +11005286 err = deny_bitmap_write_access(mddev->bitmap_info.file);
NeilBrown36fa3062005-09-09 16:23:45 -07005287 if (err) {
5288 printk(KERN_ERR "%s: error: bitmap file is already in use\n",
5289 mdname(mddev));
NeilBrownc3d97142009-12-14 12:49:52 +11005290 fput(mddev->bitmap_info.file);
5291 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005292 return err;
5293 }
NeilBrownc3d97142009-12-14 12:49:52 +11005294 mddev->bitmap_info.offset = 0; /* file overrides offset */
NeilBrown36fa3062005-09-09 16:23:45 -07005295 } else if (mddev->bitmap == NULL)
5296 return -ENOENT; /* cannot remove what isn't there */
5297 err = 0;
5298 if (mddev->pers) {
5299 mddev->pers->quiesce(mddev, 1);
5300 if (fd >= 0)
5301 err = bitmap_create(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005302 if (fd < 0 || err) {
NeilBrown36fa3062005-09-09 16:23:45 -07005303 bitmap_destroy(mddev);
NeilBrownd7375ab322006-06-26 00:27:43 -07005304 fd = -1; /* make sure to put the file */
5305 }
NeilBrown36fa3062005-09-09 16:23:45 -07005306 mddev->pers->quiesce(mddev, 0);
NeilBrownd7375ab322006-06-26 00:27:43 -07005307 }
5308 if (fd < 0) {
NeilBrownc3d97142009-12-14 12:49:52 +11005309 if (mddev->bitmap_info.file) {
5310 restore_bitmap_write_access(mddev->bitmap_info.file);
5311 fput(mddev->bitmap_info.file);
NeilBrownacc55e22006-06-26 00:27:47 -07005312 }
NeilBrownc3d97142009-12-14 12:49:52 +11005313 mddev->bitmap_info.file = NULL;
NeilBrown36fa3062005-09-09 16:23:45 -07005314 }
5315
NeilBrown32a76272005-06-21 17:17:14 -07005316 return err;
5317}
5318
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319/*
5320 * set_array_info is used two different ways
5321 * The original usage is when creating a new array.
5322 * In this usage, raid_disks is > 0 and it together with
5323 * level, size, not_persistent,layout,chunksize determine the
5324 * shape of the array.
5325 * This will always create an array with a type-0.90.0 superblock.
5326 * The newer usage is when assembling an array.
5327 * In this case raid_disks will be 0, and the major_version field is
5328 * use to determine which style super-blocks are to be found on the devices.
5329 * The minor and patch _version numbers are also kept incase the
5330 * super_block handler wishes to interpret them.
5331 */
5332static int set_array_info(mddev_t * mddev, mdu_array_info_t *info)
5333{
5334
5335 if (info->raid_disks == 0) {
5336 /* just setting version number for superblock loading */
5337 if (info->major_version < 0 ||
Ahmed S. Darwish50511da2007-05-09 02:35:34 -07005338 info->major_version >= ARRAY_SIZE(super_types) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 super_types[info->major_version].name == NULL) {
5340 /* maybe try to auto-load a module? */
5341 printk(KERN_INFO
5342 "md: superblock version %d not known\n",
5343 info->major_version);
5344 return -EINVAL;
5345 }
5346 mddev->major_version = info->major_version;
5347 mddev->minor_version = info->minor_version;
5348 mddev->patch_version = info->patch_version;
NeilBrown3f9d7b02006-12-22 01:11:41 -08005349 mddev->persistent = !info->not_persistent;
NeilBrowncbd19982009-12-30 12:08:49 +11005350 /* ensure mddev_put doesn't delete this now that there
5351 * is some minimal configuration.
5352 */
5353 mddev->ctime = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 return 0;
5355 }
5356 mddev->major_version = MD_MAJOR_VERSION;
5357 mddev->minor_version = MD_MINOR_VERSION;
5358 mddev->patch_version = MD_PATCHLEVEL_VERSION;
5359 mddev->ctime = get_seconds();
5360
5361 mddev->level = info->level;
NeilBrown17115e02006-01-16 22:14:57 -08005362 mddev->clevel[0] = 0;
Andre Noll58c0fed2009-03-31 14:33:13 +11005363 mddev->dev_sectors = 2 * (sector_t)info->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 mddev->raid_disks = info->raid_disks;
5365 /* don't set md_minor, it is determined by which /dev/md* was
5366 * openned
5367 */
5368 if (info->state & (1<<MD_SB_CLEAN))
5369 mddev->recovery_cp = MaxSector;
5370 else
5371 mddev->recovery_cp = 0;
5372 mddev->persistent = ! info->not_persistent;
NeilBrowne6910632008-02-06 01:39:51 -08005373 mddev->external = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374
5375 mddev->layout = info->layout;
Andre Noll9d8f0362009-06-18 08:45:01 +10005376 mddev->chunk_sectors = info->chunk_size >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
5378 mddev->max_disks = MD_SB_DISKS;
5379
NeilBrowne6910632008-02-06 01:39:51 -08005380 if (mddev->persistent)
5381 mddev->flags = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07005382 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383
NeilBrownc3d97142009-12-14 12:49:52 +11005384 mddev->bitmap_info.default_offset = MD_SB_BYTES >> 9;
5385 mddev->bitmap_info.offset = 0;
NeilBrownb2a27032005-11-28 13:44:12 -08005386
NeilBrownf6705572006-03-27 01:18:11 -08005387 mddev->reshape_position = MaxSector;
5388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 /*
5390 * Generate a 128 bit UUID
5391 */
5392 get_random_bytes(mddev->uuid, 16);
5393
NeilBrownf6705572006-03-27 01:18:11 -08005394 mddev->new_level = mddev->level;
Andre Noll664e7c42009-06-18 08:45:27 +10005395 mddev->new_chunk_sectors = mddev->chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08005396 mddev->new_layout = mddev->layout;
5397 mddev->delta_disks = 0;
5398
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 return 0;
5400}
5401
Dan Williams1f403622009-03-31 14:59:03 +11005402void md_set_array_sectors(mddev_t *mddev, sector_t array_sectors)
5403{
Dan Williamsb522adc2009-03-31 15:00:31 +11005404 WARN(!mddev_is_locked(mddev), "%s: unlocked mddev!\n", __func__);
5405
5406 if (mddev->external_size)
5407 return;
5408
Dan Williams1f403622009-03-31 14:59:03 +11005409 mddev->array_sectors = array_sectors;
5410}
5411EXPORT_SYMBOL(md_set_array_sectors);
5412
Andre Nolld71f9f82008-07-11 22:02:22 +10005413static int update_size(mddev_t *mddev, sector_t num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005414{
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005415 mdk_rdev_t *rdev;
NeilBrowna35b0d62006-01-06 00:20:49 -08005416 int rv;
Andre Nolld71f9f82008-07-11 22:02:22 +10005417 int fit = (num_sectors == 0);
NeilBrowna35b0d62006-01-06 00:20:49 -08005418
5419 if (mddev->pers->resize == NULL)
5420 return -EINVAL;
Andre Nolld71f9f82008-07-11 22:02:22 +10005421 /* The "num_sectors" is the number of sectors of each device that
5422 * is used. This can only make sense for arrays with redundancy.
5423 * linear and raid0 always use whatever space is available. We can only
5424 * consider changing this number if no resync or reconstruction is
5425 * happening, and if the new size is acceptable. It must fit before the
Andre Noll0f420352008-07-11 22:02:23 +10005426 * sb_start or, if that is <data_offset, it must fit before the size
Andre Nolld71f9f82008-07-11 22:02:22 +10005427 * of each device. If num_sectors is zero, we find the largest size
5428 * that fits.
5429
NeilBrowna35b0d62006-01-06 00:20:49 -08005430 */
5431 if (mddev->sync_thread)
5432 return -EBUSY;
NeilBrowndba034e2008-08-05 15:54:13 +10005433 if (mddev->bitmap)
5434 /* Sorry, cannot grow a bitmap yet, just remove it,
5435 * grow, and re-add.
5436 */
5437 return -EBUSY;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11005438 list_for_each_entry(rdev, &mddev->disks, same_set) {
Andre Nolldd8ac332009-03-31 14:33:13 +11005439 sector_t avail = rdev->sectors;
NeilBrown01ab5662006-10-28 10:38:30 -07005440
Andre Nolld71f9f82008-07-11 22:02:22 +10005441 if (fit && (num_sectors == 0 || num_sectors > avail))
5442 num_sectors = avail;
5443 if (avail < num_sectors)
NeilBrowna35b0d62006-01-06 00:20:49 -08005444 return -ENOSPC;
5445 }
Andre Nolld71f9f82008-07-11 22:02:22 +10005446 rv = mddev->pers->resize(mddev, num_sectors);
NeilBrown449aad32009-08-03 10:59:58 +10005447 if (!rv)
5448 revalidate_disk(mddev->gendisk);
NeilBrowna35b0d62006-01-06 00:20:49 -08005449 return rv;
5450}
5451
NeilBrownda943b992006-01-06 00:20:54 -08005452static int update_raid_disks(mddev_t *mddev, int raid_disks)
5453{
5454 int rv;
5455 /* change the number of raid disks */
NeilBrown63c70c42006-03-27 01:18:13 -08005456 if (mddev->pers->check_reshape == NULL)
NeilBrownda943b992006-01-06 00:20:54 -08005457 return -EINVAL;
5458 if (raid_disks <= 0 ||
NeilBrown233fca32010-04-14 17:02:09 +10005459 (mddev->max_disks && raid_disks >= mddev->max_disks))
NeilBrownda943b992006-01-06 00:20:54 -08005460 return -EINVAL;
NeilBrown63c70c42006-03-27 01:18:13 -08005461 if (mddev->sync_thread || mddev->reshape_position != MaxSector)
NeilBrownda943b992006-01-06 00:20:54 -08005462 return -EBUSY;
NeilBrown63c70c42006-03-27 01:18:13 -08005463 mddev->delta_disks = raid_disks - mddev->raid_disks;
5464
5465 rv = mddev->pers->check_reshape(mddev);
NeilBrownda943b992006-01-06 00:20:54 -08005466 return rv;
5467}
5468
5469
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470/*
5471 * update_array_info is used to change the configuration of an
5472 * on-line array.
5473 * The version, ctime,level,size,raid_disks,not_persistent, layout,chunk_size
5474 * fields in the info are checked against the array.
5475 * Any differences that cannot be handled will cause an error.
5476 * Normally, only one change can be managed at a time.
5477 */
5478static int update_array_info(mddev_t *mddev, mdu_array_info_t *info)
5479{
5480 int rv = 0;
5481 int cnt = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005482 int state = 0;
5483
5484 /* calculate expected state,ignoring low bits */
NeilBrownc3d97142009-12-14 12:49:52 +11005485 if (mddev->bitmap && mddev->bitmap_info.offset)
NeilBrown36fa3062005-09-09 16:23:45 -07005486 state |= (1 << MD_SB_BITMAP_PRESENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488 if (mddev->major_version != info->major_version ||
5489 mddev->minor_version != info->minor_version ||
5490/* mddev->patch_version != info->patch_version || */
5491 mddev->ctime != info->ctime ||
5492 mddev->level != info->level ||
5493/* mddev->layout != info->layout || */
5494 !mddev->persistent != info->not_persistent||
Andre Noll9d8f0362009-06-18 08:45:01 +10005495 mddev->chunk_sectors != info->chunk_size >> 9 ||
NeilBrown36fa3062005-09-09 16:23:45 -07005496 /* ignore bottom 8 bits of state, and allow SB_BITMAP_PRESENT to change */
5497 ((state^info->state) & 0xfffffe00)
5498 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 return -EINVAL;
5500 /* Check there is only one change */
Andre Noll58c0fed2009-03-31 14:33:13 +11005501 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
5502 cnt++;
5503 if (mddev->raid_disks != info->raid_disks)
5504 cnt++;
5505 if (mddev->layout != info->layout)
5506 cnt++;
5507 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT))
5508 cnt++;
5509 if (cnt == 0)
5510 return 0;
5511 if (cnt > 1)
5512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513
5514 if (mddev->layout != info->layout) {
5515 /* Change layout
5516 * we don't need to do anything at the md level, the
5517 * personality will take care of it all.
5518 */
NeilBrown50ac1682009-06-18 08:47:55 +10005519 if (mddev->pers->check_reshape == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10005521 else {
5522 mddev->new_layout = info->layout;
NeilBrown50ac1682009-06-18 08:47:55 +10005523 rv = mddev->pers->check_reshape(mddev);
NeilBrown597a7112009-06-18 08:47:42 +10005524 if (rv)
5525 mddev->new_layout = mddev->layout;
5526 return rv;
5527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 }
Andre Noll58c0fed2009-03-31 14:33:13 +11005529 if (info->size >= 0 && mddev->dev_sectors / 2 != info->size)
Andre Nolld71f9f82008-07-11 22:02:22 +10005530 rv = update_size(mddev, (sector_t)info->size * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531
NeilBrownda943b992006-01-06 00:20:54 -08005532 if (mddev->raid_disks != info->raid_disks)
5533 rv = update_raid_disks(mddev, info->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534
NeilBrown36fa3062005-09-09 16:23:45 -07005535 if ((state ^ info->state) & (1<<MD_SB_BITMAP_PRESENT)) {
5536 if (mddev->pers->quiesce == NULL)
5537 return -EINVAL;
5538 if (mddev->recovery || mddev->sync_thread)
5539 return -EBUSY;
5540 if (info->state & (1<<MD_SB_BITMAP_PRESENT)) {
5541 /* add the bitmap */
5542 if (mddev->bitmap)
5543 return -EEXIST;
NeilBrownc3d97142009-12-14 12:49:52 +11005544 if (mddev->bitmap_info.default_offset == 0)
NeilBrown36fa3062005-09-09 16:23:45 -07005545 return -EINVAL;
NeilBrownc3d97142009-12-14 12:49:52 +11005546 mddev->bitmap_info.offset =
5547 mddev->bitmap_info.default_offset;
NeilBrown36fa3062005-09-09 16:23:45 -07005548 mddev->pers->quiesce(mddev, 1);
5549 rv = bitmap_create(mddev);
5550 if (rv)
5551 bitmap_destroy(mddev);
5552 mddev->pers->quiesce(mddev, 0);
5553 } else {
5554 /* remove the bitmap */
5555 if (!mddev->bitmap)
5556 return -ENOENT;
5557 if (mddev->bitmap->file)
5558 return -EINVAL;
5559 mddev->pers->quiesce(mddev, 1);
5560 bitmap_destroy(mddev);
5561 mddev->pers->quiesce(mddev, 0);
NeilBrownc3d97142009-12-14 12:49:52 +11005562 mddev->bitmap_info.offset = 0;
NeilBrown36fa3062005-09-09 16:23:45 -07005563 }
5564 }
NeilBrown850b2b42006-10-03 01:15:46 -07005565 md_update_sb(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 return rv;
5567}
5568
5569static int set_disk_faulty(mddev_t *mddev, dev_t dev)
5570{
5571 mdk_rdev_t *rdev;
5572
5573 if (mddev->pers == NULL)
5574 return -ENODEV;
5575
5576 rdev = find_rdev(mddev, dev);
5577 if (!rdev)
5578 return -ENODEV;
5579
5580 md_error(mddev, rdev);
5581 return 0;
5582}
5583
Andre Noll2f9618c2008-04-25 18:57:58 +02005584/*
5585 * We have a problem here : there is no easy way to give a CHS
5586 * virtual geometry. We currently pretend that we have a 2 heads
5587 * 4 sectors (with a BIG number of cylinders...). This drives
5588 * dosfs just mad... ;-)
5589 */
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005590static int md_getgeo(struct block_device *bdev, struct hd_geometry *geo)
5591{
5592 mddev_t *mddev = bdev->bd_disk->private_data;
5593
5594 geo->heads = 2;
5595 geo->sectors = 4;
NeilBrown49ce6ce2010-03-29 10:51:42 +11005596 geo->cylinders = mddev->array_sectors / 8;
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005597 return 0;
5598}
5599
Al Viroa39907f2008-03-02 10:31:15 -05005600static int md_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 unsigned int cmd, unsigned long arg)
5602{
5603 int err = 0;
5604 void __user *argp = (void __user *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 mddev_t *mddev = NULL;
Dan Williamse2218352010-05-12 08:25:37 +10005606 int ro;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607
5608 if (!capable(CAP_SYS_ADMIN))
5609 return -EACCES;
5610
5611 /*
5612 * Commands dealing with the RAID driver but not any
5613 * particular array:
5614 */
5615 switch (cmd)
5616 {
5617 case RAID_VERSION:
5618 err = get_version(argp);
5619 goto done;
5620
5621 case PRINT_RAID_DEBUG:
5622 err = 0;
5623 md_print_devices();
5624 goto done;
5625
5626#ifndef MODULE
5627 case RAID_AUTORUN:
5628 err = 0;
5629 autostart_arrays(arg);
5630 goto done;
5631#endif
5632 default:;
5633 }
5634
5635 /*
5636 * Commands creating/starting a new array:
5637 */
5638
Al Viroa39907f2008-03-02 10:31:15 -05005639 mddev = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640
5641 if (!mddev) {
5642 BUG();
5643 goto abort;
5644 }
5645
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 err = mddev_lock(mddev);
5647 if (err) {
5648 printk(KERN_INFO
5649 "md: ioctl lock interrupted, reason %d, cmd %d\n",
5650 err, cmd);
5651 goto abort;
5652 }
5653
5654 switch (cmd)
5655 {
5656 case SET_ARRAY_INFO:
5657 {
5658 mdu_array_info_t info;
5659 if (!arg)
5660 memset(&info, 0, sizeof(info));
5661 else if (copy_from_user(&info, argp, sizeof(info))) {
5662 err = -EFAULT;
5663 goto abort_unlock;
5664 }
5665 if (mddev->pers) {
5666 err = update_array_info(mddev, &info);
5667 if (err) {
5668 printk(KERN_WARNING "md: couldn't update"
5669 " array info. %d\n", err);
5670 goto abort_unlock;
5671 }
5672 goto done_unlock;
5673 }
5674 if (!list_empty(&mddev->disks)) {
5675 printk(KERN_WARNING
5676 "md: array %s already has disks!\n",
5677 mdname(mddev));
5678 err = -EBUSY;
5679 goto abort_unlock;
5680 }
5681 if (mddev->raid_disks) {
5682 printk(KERN_WARNING
5683 "md: array %s already initialised!\n",
5684 mdname(mddev));
5685 err = -EBUSY;
5686 goto abort_unlock;
5687 }
5688 err = set_array_info(mddev, &info);
5689 if (err) {
5690 printk(KERN_WARNING "md: couldn't set"
5691 " array info. %d\n", err);
5692 goto abort_unlock;
5693 }
5694 }
5695 goto done_unlock;
5696
5697 default:;
5698 }
5699
5700 /*
5701 * Commands querying/configuring an existing array:
5702 */
NeilBrown32a76272005-06-21 17:17:14 -07005703 /* if we are not initialised yet, only ADD_NEW_DISK, STOP_ARRAY,
NeilBrown3f9d7b02006-12-22 01:11:41 -08005704 * RUN_ARRAY, and GET_ and SET_BITMAP_FILE are allowed */
NeilBrowna17184a2008-02-06 01:39:55 -08005705 if ((!mddev->raid_disks && !mddev->external)
5706 && cmd != ADD_NEW_DISK && cmd != STOP_ARRAY
5707 && cmd != RUN_ARRAY && cmd != SET_BITMAP_FILE
5708 && cmd != GET_BITMAP_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 err = -ENODEV;
5710 goto abort_unlock;
5711 }
5712
5713 /*
5714 * Commands even a read-only array can execute:
5715 */
5716 switch (cmd)
5717 {
5718 case GET_ARRAY_INFO:
5719 err = get_array_info(mddev, argp);
5720 goto done_unlock;
5721
NeilBrown32a76272005-06-21 17:17:14 -07005722 case GET_BITMAP_FILE:
viro@ZenIV.linux.org.uk87162a22005-09-09 20:36:43 +01005723 err = get_bitmap_file(mddev, argp);
NeilBrown32a76272005-06-21 17:17:14 -07005724 goto done_unlock;
5725
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 case GET_DISK_INFO:
5727 err = get_disk_info(mddev, argp);
5728 goto done_unlock;
5729
5730 case RESTART_ARRAY_RW:
5731 err = restart_array(mddev);
5732 goto done_unlock;
5733
5734 case STOP_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005735 err = do_md_stop(mddev, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 goto done_unlock;
5737
5738 case STOP_ARRAY_RO:
NeilBrowna4bd82d2010-03-29 13:23:10 +11005739 err = md_set_readonly(mddev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 goto done_unlock;
5741
Dan Williamse2218352010-05-12 08:25:37 +10005742 case BLKROSET:
5743 if (get_user(ro, (int __user *)(arg))) {
5744 err = -EFAULT;
5745 goto done_unlock;
5746 }
5747 err = -EINVAL;
5748
5749 /* if the bdev is going readonly the value of mddev->ro
5750 * does not matter, no writes are coming
5751 */
5752 if (ro)
5753 goto done_unlock;
5754
5755 /* are we are already prepared for writes? */
5756 if (mddev->ro != 1)
5757 goto done_unlock;
5758
5759 /* transitioning to readauto need only happen for
5760 * arrays that call md_write_start
5761 */
5762 if (mddev->pers) {
5763 err = restart_array(mddev);
5764 if (err == 0) {
5765 mddev->ro = 2;
5766 set_disk_ro(mddev->gendisk, 0);
5767 }
5768 }
5769 goto done_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 }
5771
5772 /*
5773 * The remaining ioctls are changing the state of the
NeilBrownf91de922005-11-08 21:39:36 -08005774 * superblock, so we do not allow them on read-only arrays.
5775 * However non-MD ioctls (e.g. get-size) will still come through
5776 * here and hit the 'default' below, so only disallow
5777 * 'md' ioctls, and switch to rw mode if started auto-readonly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 */
Andre Nollbb57fc62008-04-25 19:06:35 +02005779 if (_IOC_TYPE(cmd) == MD_MAJOR && mddev->ro && mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08005780 if (mddev->ro == 2) {
5781 mddev->ro = 0;
NeilBrownb62b7592008-10-21 13:25:21 +11005782 sysfs_notify_dirent(mddev->sysfs_state);
Neil Brown0fd62b82008-06-28 08:31:36 +10005783 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5784 md_wakeup_thread(mddev->thread);
NeilBrownf91de922005-11-08 21:39:36 -08005785 } else {
5786 err = -EROFS;
5787 goto abort_unlock;
5788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 }
5790
5791 switch (cmd)
5792 {
5793 case ADD_NEW_DISK:
5794 {
5795 mdu_disk_info_t info;
5796 if (copy_from_user(&info, argp, sizeof(info)))
5797 err = -EFAULT;
5798 else
5799 err = add_new_disk(mddev, &info);
5800 goto done_unlock;
5801 }
5802
5803 case HOT_REMOVE_DISK:
5804 err = hot_remove_disk(mddev, new_decode_dev(arg));
5805 goto done_unlock;
5806
5807 case HOT_ADD_DISK:
5808 err = hot_add_disk(mddev, new_decode_dev(arg));
5809 goto done_unlock;
5810
5811 case SET_DISK_FAULTY:
5812 err = set_disk_faulty(mddev, new_decode_dev(arg));
5813 goto done_unlock;
5814
5815 case RUN_ARRAY:
NeilBrownd710e132008-10-13 11:55:12 +11005816 err = do_md_run(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 goto done_unlock;
5818
NeilBrown32a76272005-06-21 17:17:14 -07005819 case SET_BITMAP_FILE:
5820 err = set_bitmap_file(mddev, (int)arg);
5821 goto done_unlock;
5822
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824 err = -EINVAL;
5825 goto abort_unlock;
5826 }
5827
5828done_unlock:
5829abort_unlock:
NeilBrownd3374822009-01-09 08:31:10 +11005830 if (mddev->hold_active == UNTIL_IOCTL &&
5831 err != -EINVAL)
5832 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005833 mddev_unlock(mddev);
5834
5835 return err;
5836done:
5837 if (err)
5838 MD_BUG();
5839abort:
5840 return err;
5841}
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005842#ifdef CONFIG_COMPAT
5843static int md_compat_ioctl(struct block_device *bdev, fmode_t mode,
5844 unsigned int cmd, unsigned long arg)
5845{
5846 switch (cmd) {
5847 case HOT_REMOVE_DISK:
5848 case HOT_ADD_DISK:
5849 case SET_DISK_FAULTY:
5850 case SET_BITMAP_FILE:
5851 /* These take in integer arg, do not convert */
5852 break;
5853 default:
5854 arg = (unsigned long)compat_ptr(arg);
5855 break;
5856 }
5857
5858 return md_ioctl(bdev, mode, cmd, arg);
5859}
5860#endif /* CONFIG_COMPAT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005861
Al Viroa39907f2008-03-02 10:31:15 -05005862static int md_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863{
5864 /*
5865 * Succeed if we can lock the mddev, which confirms that
5866 * it isn't being stopped right now.
5867 */
NeilBrownd3374822009-01-09 08:31:10 +11005868 mddev_t *mddev = mddev_find(bdev->bd_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 int err;
5870
NeilBrownd3374822009-01-09 08:31:10 +11005871 if (mddev->gendisk != bdev->bd_disk) {
5872 /* we are racing with mddev_put which is discarding this
5873 * bd_disk.
5874 */
5875 mddev_put(mddev);
5876 /* Wait until bdev->bd_disk is definitely gone */
5877 flush_scheduled_work();
5878 /* Then retry the open from the top */
5879 return -ERESTARTSYS;
5880 }
5881 BUG_ON(mddev != bdev->bd_disk->private_data);
5882
NeilBrownc8c00a62009-08-10 12:50:52 +10005883 if ((err = mutex_lock_interruptible(&mddev->open_mutex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005884 goto out;
5885
5886 err = 0;
NeilBrownf2ea68c2008-07-21 17:05:25 +10005887 atomic_inc(&mddev->openers);
NeilBrownc8c00a62009-08-10 12:50:52 +10005888 mutex_unlock(&mddev->open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 out:
5891 return err;
5892}
5893
Al Viroa39907f2008-03-02 10:31:15 -05005894static int md_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895{
Al Viroa39907f2008-03-02 10:31:15 -05005896 mddev_t *mddev = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897
Eric Sesterhenn52e5f9d2006-10-03 23:33:23 +02005898 BUG_ON(!mddev);
NeilBrownf2ea68c2008-07-21 17:05:25 +10005899 atomic_dec(&mddev->openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 mddev_put(mddev);
5901
5902 return 0;
5903}
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07005904static const struct block_device_operations md_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905{
5906 .owner = THIS_MODULE,
Al Viroa39907f2008-03-02 10:31:15 -05005907 .open = md_open,
5908 .release = md_release,
NeilBrownb492b852009-05-26 12:57:36 +10005909 .ioctl = md_ioctl,
Arnd Bergmannaa98aa32009-12-14 12:50:05 +11005910#ifdef CONFIG_COMPAT
5911 .compat_ioctl = md_compat_ioctl,
5912#endif
Christoph Hellwiga885c8c2006-01-08 01:02:50 -08005913 .getgeo = md_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914};
5915
Adrian Bunk75c96f82005-05-05 16:16:09 -07005916static int md_thread(void * arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917{
5918 mdk_thread_t *thread = arg;
5919
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 /*
5921 * md_thread is a 'system-thread', it's priority should be very
5922 * high. We avoid resource deadlocks individually in each
5923 * raid personality. (RAID5 does preallocation) We also use RR and
5924 * the very same RT priority as kswapd, thus we will never get
5925 * into a priority inversion deadlock.
5926 *
5927 * we definitely have to have equal or higher priority than
5928 * bdflush, otherwise bdflush will deadlock if there are too
5929 * many dirty RAID5 blocks.
5930 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931
NeilBrown6985c432005-10-19 21:23:47 -07005932 allow_signal(SIGKILL);
NeilBrowna6fb0932005-09-09 16:23:56 -07005933 while (!kthread_should_stop()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934
NeilBrown93588e22005-11-15 00:09:12 -08005935 /* We need to wait INTERRUPTIBLE so that
5936 * we don't add to the load-average.
5937 * That means we need to be sure no signals are
5938 * pending
5939 */
5940 if (signal_pending(current))
5941 flush_signals(current);
5942
5943 wait_event_interruptible_timeout
5944 (thread->wqueue,
5945 test_bit(THREAD_WAKEUP, &thread->flags)
5946 || kthread_should_stop(),
5947 thread->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
5949 clear_bit(THREAD_WAKEUP, &thread->flags);
5950
NeilBrown787453c2005-11-08 21:39:43 -08005951 thread->run(thread->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005952 }
NeilBrowna6fb0932005-09-09 16:23:56 -07005953
Linus Torvalds1da177e2005-04-16 15:20:36 -07005954 return 0;
5955}
5956
5957void md_wakeup_thread(mdk_thread_t *thread)
5958{
5959 if (thread) {
5960 dprintk("md: waking up MD thread %s.\n", thread->tsk->comm);
5961 set_bit(THREAD_WAKEUP, &thread->flags);
5962 wake_up(&thread->wqueue);
5963 }
5964}
5965
5966mdk_thread_t *md_register_thread(void (*run) (mddev_t *), mddev_t *mddev,
5967 const char *name)
5968{
5969 mdk_thread_t *thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
NeilBrown9ffae0c2006-01-06 00:20:32 -08005971 thread = kzalloc(sizeof(mdk_thread_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005972 if (!thread)
5973 return NULL;
5974
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 init_waitqueue_head(&thread->wqueue);
5976
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 thread->run = run;
5978 thread->mddev = mddev;
NeilBrown32a76272005-06-21 17:17:14 -07005979 thread->timeout = MAX_SCHEDULE_TIMEOUT;
NeilBrown0da3c612009-09-23 18:09:45 +10005980 thread->tsk = kthread_run(md_thread, thread,
5981 "%s_%s",
5982 mdname(thread->mddev),
5983 name ?: mddev->pers->name);
NeilBrowna6fb0932005-09-09 16:23:56 -07005984 if (IS_ERR(thread->tsk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 kfree(thread);
5986 return NULL;
5987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 return thread;
5989}
5990
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991void md_unregister_thread(mdk_thread_t *thread)
5992{
NeilBrowne0cf8f02009-03-31 14:39:39 +11005993 if (!thread)
5994 return;
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07005995 dprintk("interrupting MD-thread pid %d\n", task_pid_nr(thread->tsk));
NeilBrowna6fb0932005-09-09 16:23:56 -07005996
5997 kthread_stop(thread->tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 kfree(thread);
5999}
6000
6001void md_error(mddev_t *mddev, mdk_rdev_t *rdev)
6002{
6003 if (!mddev) {
6004 MD_BUG();
6005 return;
6006 }
6007
NeilBrownb2d444d2005-11-08 21:39:31 -08006008 if (!rdev || test_bit(Faulty, &rdev->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 return;
Dan Williams6bfe0b42008-04-30 00:52:32 -07006010
6011 if (mddev->external)
6012 set_bit(Blocked, &rdev->flags);
NeilBrown32a76272005-06-21 17:17:14 -07006013/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 dprintk("md_error dev:%s, rdev:(%d:%d), (caller: %p,%p,%p,%p).\n",
6015 mdname(mddev),
6016 MAJOR(rdev->bdev->bd_dev), MINOR(rdev->bdev->bd_dev),
6017 __builtin_return_address(0),__builtin_return_address(1),
6018 __builtin_return_address(2),__builtin_return_address(3));
NeilBrown32a76272005-06-21 17:17:14 -07006019*/
Andrew Mortond0a0a5e2006-07-10 04:44:20 -07006020 if (!mddev->pers)
6021 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 if (!mddev->pers->error_handler)
6023 return;
6024 mddev->pers->error_handler(mddev,rdev);
Neil Brown72a23c22008-06-28 08:31:41 +10006025 if (mddev->degraded)
6026 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownc0cc75f2010-03-22 10:28:51 +11006027 sysfs_notify_dirent(rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6029 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6030 md_wakeup_thread(mddev->thread);
NeilBrownc331eb02006-05-30 21:27:13 -07006031 md_new_event_inintr(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032}
6033
6034/* seq_file implementation /proc/mdstat */
6035
6036static void status_unused(struct seq_file *seq)
6037{
6038 int i = 0;
6039 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
6041 seq_printf(seq, "unused devices: ");
6042
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006043 list_for_each_entry(rdev, &pending_raid_disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 char b[BDEVNAME_SIZE];
6045 i++;
6046 seq_printf(seq, "%s ",
6047 bdevname(rdev->bdev,b));
6048 }
6049 if (!i)
6050 seq_printf(seq, "<none>");
6051
6052 seq_printf(seq, "\n");
6053}
6054
6055
6056static void status_resync(struct seq_file *seq, mddev_t * mddev)
6057{
NeilBrowndd71cf62009-05-07 12:49:35 +10006058 sector_t max_sectors, resync, res;
6059 unsigned long dt, db;
6060 sector_t rt;
NeilBrown4588b422006-03-27 01:18:04 -08006061 int scale;
6062 unsigned int per_milli;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006063
NeilBrowndd71cf62009-05-07 12:49:35 +10006064 resync = mddev->curr_resync - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065
6066 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
NeilBrowndd71cf62009-05-07 12:49:35 +10006067 max_sectors = mddev->resync_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 else
NeilBrowndd71cf62009-05-07 12:49:35 +10006069 max_sectors = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006070
6071 /*
6072 * Should not happen.
6073 */
NeilBrowndd71cf62009-05-07 12:49:35 +10006074 if (!max_sectors) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 MD_BUG();
6076 return;
6077 }
NeilBrown4588b422006-03-27 01:18:04 -08006078 /* Pick 'scale' such that (resync>>scale)*1000 will fit
NeilBrowndd71cf62009-05-07 12:49:35 +10006079 * in a sector_t, and (max_sectors>>scale) will fit in a
NeilBrown4588b422006-03-27 01:18:04 -08006080 * u32, as those are the requirements for sector_div.
6081 * Thus 'scale' must be at least 10
6082 */
6083 scale = 10;
6084 if (sizeof(sector_t) > sizeof(unsigned long)) {
NeilBrowndd71cf62009-05-07 12:49:35 +10006085 while ( max_sectors/2 > (1ULL<<(scale+32)))
NeilBrown4588b422006-03-27 01:18:04 -08006086 scale++;
6087 }
6088 res = (resync>>scale)*1000;
NeilBrowndd71cf62009-05-07 12:49:35 +10006089 sector_div(res, (u32)((max_sectors>>scale)+1));
NeilBrown4588b422006-03-27 01:18:04 -08006090
6091 per_milli = res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 {
NeilBrown4588b422006-03-27 01:18:04 -08006093 int i, x = per_milli/50, y = 20-x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 seq_printf(seq, "[");
6095 for (i = 0; i < x; i++)
6096 seq_printf(seq, "=");
6097 seq_printf(seq, ">");
6098 for (i = 0; i < y; i++)
6099 seq_printf(seq, ".");
6100 seq_printf(seq, "] ");
6101 }
NeilBrown4588b422006-03-27 01:18:04 -08006102 seq_printf(seq, " %s =%3u.%u%% (%llu/%llu)",
NeilBrownccfcc3c2006-03-27 01:18:09 -08006103 (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)?
6104 "reshape" :
NeilBrown61df9d92006-10-03 01:15:57 -07006105 (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)?
6106 "check" :
6107 (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ?
6108 "resync" : "recovery"))),
6109 per_milli/10, per_milli % 10,
NeilBrowndd71cf62009-05-07 12:49:35 +10006110 (unsigned long long) resync/2,
6111 (unsigned long long) max_sectors/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112
6113 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006114 * dt: time from mark until now
6115 * db: blocks written from mark until now
6116 * rt: remaining time
NeilBrowndd71cf62009-05-07 12:49:35 +10006117 *
6118 * rt is a sector_t, so could be 32bit or 64bit.
6119 * So we divide before multiply in case it is 32bit and close
6120 * to the limit.
6121 * We scale the divisor (db) by 32 to avoid loosing precision
6122 * near the end of resync when the number of remaining sectors
6123 * is close to 'db'.
6124 * We then divide rt by 32 after multiplying by db to compensate.
6125 * The '+1' avoids division by zero if db is very small.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006126 */
6127 dt = ((jiffies - mddev->resync_mark) / HZ);
6128 if (!dt) dt++;
NeilBrownff4e8d92006-07-10 04:44:16 -07006129 db = (mddev->curr_mark_cnt - atomic_read(&mddev->recovery_active))
6130 - mddev->resync_mark_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006131
NeilBrowndd71cf62009-05-07 12:49:35 +10006132 rt = max_sectors - resync; /* number of remaining sectors */
6133 sector_div(rt, db/32+1);
6134 rt *= dt;
6135 rt >>= 5;
6136
6137 seq_printf(seq, " finish=%lu.%lumin", (unsigned long)rt / 60,
6138 ((unsigned long)rt % 60)/6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006139
NeilBrownff4e8d92006-07-10 04:44:16 -07006140 seq_printf(seq, " speed=%ldK/sec", db/2/dt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141}
6142
6143static void *md_seq_start(struct seq_file *seq, loff_t *pos)
6144{
6145 struct list_head *tmp;
6146 loff_t l = *pos;
6147 mddev_t *mddev;
6148
6149 if (l >= 0x10000)
6150 return NULL;
6151 if (!l--)
6152 /* header */
6153 return (void*)1;
6154
6155 spin_lock(&all_mddevs_lock);
6156 list_for_each(tmp,&all_mddevs)
6157 if (!l--) {
6158 mddev = list_entry(tmp, mddev_t, all_mddevs);
6159 mddev_get(mddev);
6160 spin_unlock(&all_mddevs_lock);
6161 return mddev;
6162 }
6163 spin_unlock(&all_mddevs_lock);
6164 if (!l--)
6165 return (void*)2;/* tail */
6166 return NULL;
6167}
6168
6169static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
6170{
6171 struct list_head *tmp;
6172 mddev_t *next_mddev, *mddev = v;
6173
6174 ++*pos;
6175 if (v == (void*)2)
6176 return NULL;
6177
6178 spin_lock(&all_mddevs_lock);
6179 if (v == (void*)1)
6180 tmp = all_mddevs.next;
6181 else
6182 tmp = mddev->all_mddevs.next;
6183 if (tmp != &all_mddevs)
6184 next_mddev = mddev_get(list_entry(tmp,mddev_t,all_mddevs));
6185 else {
6186 next_mddev = (void*)2;
6187 *pos = 0x10000;
6188 }
6189 spin_unlock(&all_mddevs_lock);
6190
6191 if (v != (void*)1)
6192 mddev_put(mddev);
6193 return next_mddev;
6194
6195}
6196
6197static void md_seq_stop(struct seq_file *seq, void *v)
6198{
6199 mddev_t *mddev = v;
6200
6201 if (mddev && v != (void*)1 && v != (void*)2)
6202 mddev_put(mddev);
6203}
6204
NeilBrownd7603b72006-01-06 00:20:30 -08006205struct mdstat_info {
6206 int event;
6207};
6208
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209static int md_seq_show(struct seq_file *seq, void *v)
6210{
6211 mddev_t *mddev = v;
Andre Nolldd8ac332009-03-31 14:33:13 +11006212 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006213 mdk_rdev_t *rdev;
NeilBrownd7603b72006-01-06 00:20:30 -08006214 struct mdstat_info *mi = seq->private;
NeilBrown32a76272005-06-21 17:17:14 -07006215 struct bitmap *bitmap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216
6217 if (v == (void*)1) {
NeilBrown2604b702006-01-06 00:20:36 -08006218 struct mdk_personality *pers;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 seq_printf(seq, "Personalities : ");
6220 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006221 list_for_each_entry(pers, &pers_list, list)
6222 seq_printf(seq, "[%s] ", pers->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
6224 spin_unlock(&pers_lock);
6225 seq_printf(seq, "\n");
NeilBrownd7603b72006-01-06 00:20:30 -08006226 mi->event = atomic_read(&md_event_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227 return 0;
6228 }
6229 if (v == (void*)2) {
6230 status_unused(seq);
6231 return 0;
6232 }
6233
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006234 if (mddev_lock(mddev) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 return -EINTR;
Ingo Molnar5dc5cf72006-04-20 02:43:23 -07006236
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 if (mddev->pers || mddev->raid_disks || !list_empty(&mddev->disks)) {
6238 seq_printf(seq, "%s : %sactive", mdname(mddev),
6239 mddev->pers ? "" : "in");
6240 if (mddev->pers) {
NeilBrownf91de922005-11-08 21:39:36 -08006241 if (mddev->ro==1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242 seq_printf(seq, " (read-only)");
NeilBrownf91de922005-11-08 21:39:36 -08006243 if (mddev->ro==2)
NeilBrown52720ae2008-03-10 11:43:47 -07006244 seq_printf(seq, " (auto-read-only)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 seq_printf(seq, " %s", mddev->pers->name);
6246 }
6247
Andre Nolldd8ac332009-03-31 14:33:13 +11006248 sectors = 0;
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006249 list_for_each_entry(rdev, &mddev->disks, same_set) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006250 char b[BDEVNAME_SIZE];
6251 seq_printf(seq, " %s[%d]",
6252 bdevname(rdev->bdev,b), rdev->desc_nr);
NeilBrown8ddf9ef2005-09-09 16:23:45 -07006253 if (test_bit(WriteMostly, &rdev->flags))
6254 seq_printf(seq, "(W)");
NeilBrownb2d444d2005-11-08 21:39:31 -08006255 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 seq_printf(seq, "(F)");
6257 continue;
NeilBrownb325a322005-09-09 16:24:00 -07006258 } else if (rdev->raid_disk < 0)
6259 seq_printf(seq, "(S)"); /* spare */
Andre Nolldd8ac332009-03-31 14:33:13 +11006260 sectors += rdev->sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 }
6262
6263 if (!list_empty(&mddev->disks)) {
6264 if (mddev->pers)
6265 seq_printf(seq, "\n %llu blocks",
Andre Nollf233ea52008-07-21 17:05:22 +10006266 (unsigned long long)
6267 mddev->array_sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 else
6269 seq_printf(seq, "\n %llu blocks",
Andre Nolldd8ac332009-03-31 14:33:13 +11006270 (unsigned long long)sectors / 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 }
NeilBrown1cd6bf12005-09-09 16:24:00 -07006272 if (mddev->persistent) {
6273 if (mddev->major_version != 0 ||
6274 mddev->minor_version != 90) {
6275 seq_printf(seq," super %d.%d",
6276 mddev->major_version,
6277 mddev->minor_version);
6278 }
NeilBrowne6910632008-02-06 01:39:51 -08006279 } else if (mddev->external)
6280 seq_printf(seq, " super external:%s",
6281 mddev->metadata_type);
6282 else
NeilBrown1cd6bf12005-09-09 16:24:00 -07006283 seq_printf(seq, " super non-persistent");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284
6285 if (mddev->pers) {
NeilBrownd710e132008-10-13 11:55:12 +11006286 mddev->pers->status(seq, mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006287 seq_printf(seq, "\n ");
NeilBrown8e1b39d2005-11-08 21:39:41 -08006288 if (mddev->pers->sync_request) {
6289 if (mddev->curr_resync > 2) {
NeilBrownd710e132008-10-13 11:55:12 +11006290 status_resync(seq, mddev);
NeilBrown8e1b39d2005-11-08 21:39:41 -08006291 seq_printf(seq, "\n ");
6292 } else if (mddev->curr_resync == 1 || mddev->curr_resync == 2)
6293 seq_printf(seq, "\tresync=DELAYED\n ");
6294 else if (mddev->recovery_cp < MaxSector)
6295 seq_printf(seq, "\tresync=PENDING\n ");
6296 }
NeilBrown32a76272005-06-21 17:17:14 -07006297 } else
6298 seq_printf(seq, "\n ");
6299
6300 if ((bitmap = mddev->bitmap)) {
NeilBrown32a76272005-06-21 17:17:14 -07006301 unsigned long chunk_kb;
6302 unsigned long flags;
NeilBrown32a76272005-06-21 17:17:14 -07006303 spin_lock_irqsave(&bitmap->lock, flags);
NeilBrown42a04b52009-12-14 12:49:53 +11006304 chunk_kb = mddev->bitmap_info.chunksize >> 10;
NeilBrown32a76272005-06-21 17:17:14 -07006305 seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
6306 "%lu%s chunk",
6307 bitmap->pages - bitmap->missing_pages,
6308 bitmap->pages,
6309 (bitmap->pages - bitmap->missing_pages)
6310 << (PAGE_SHIFT - 10),
NeilBrown42a04b52009-12-14 12:49:53 +11006311 chunk_kb ? chunk_kb : mddev->bitmap_info.chunksize,
NeilBrown32a76272005-06-21 17:17:14 -07006312 chunk_kb ? "KB" : "B");
NeilBrown78d742d2005-06-21 17:17:15 -07006313 if (bitmap->file) {
6314 seq_printf(seq, ", file: ");
Jan Blunckc32c2f62008-02-14 19:38:43 -08006315 seq_path(seq, &bitmap->file->f_path, " \t\n");
NeilBrown32a76272005-06-21 17:17:14 -07006316 }
NeilBrown78d742d2005-06-21 17:17:15 -07006317
NeilBrown32a76272005-06-21 17:17:14 -07006318 seq_printf(seq, "\n");
6319 spin_unlock_irqrestore(&bitmap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006320 }
6321
6322 seq_printf(seq, "\n");
6323 }
6324 mddev_unlock(mddev);
6325
6326 return 0;
6327}
6328
Jan Engelhardt110518b2009-05-07 12:49:37 +10006329static const struct seq_operations md_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 .start = md_seq_start,
6331 .next = md_seq_next,
6332 .stop = md_seq_stop,
6333 .show = md_seq_show,
6334};
6335
6336static int md_seq_open(struct inode *inode, struct file *file)
6337{
6338 int error;
NeilBrownd7603b72006-01-06 00:20:30 -08006339 struct mdstat_info *mi = kmalloc(sizeof(*mi), GFP_KERNEL);
6340 if (mi == NULL)
6341 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006342
6343 error = seq_open(file, &md_seq_ops);
NeilBrownd7603b72006-01-06 00:20:30 -08006344 if (error)
6345 kfree(mi);
6346 else {
6347 struct seq_file *p = file->private_data;
6348 p->private = mi;
6349 mi->event = atomic_read(&md_event_count);
6350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006351 return error;
6352}
6353
NeilBrownd7603b72006-01-06 00:20:30 -08006354static unsigned int mdstat_poll(struct file *filp, poll_table *wait)
6355{
6356 struct seq_file *m = filp->private_data;
6357 struct mdstat_info *mi = m->private;
6358 int mask;
6359
6360 poll_wait(filp, &md_event_waiters, wait);
6361
6362 /* always allow read */
6363 mask = POLLIN | POLLRDNORM;
6364
6365 if (mi->event != atomic_read(&md_event_count))
6366 mask |= POLLERR | POLLPRI;
6367 return mask;
6368}
6369
Arjan van de Venfa027c22007-02-12 00:55:33 -08006370static const struct file_operations md_seq_fops = {
Akinobu Mitae24650c2006-10-17 00:09:38 -07006371 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006372 .open = md_seq_open,
6373 .read = seq_read,
6374 .llseek = seq_lseek,
Martin Peschkec3f94b402007-05-09 02:35:35 -07006375 .release = seq_release_private,
NeilBrownd7603b72006-01-06 00:20:30 -08006376 .poll = mdstat_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377};
6378
NeilBrown2604b702006-01-06 00:20:36 -08006379int register_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006380{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006382 list_add_tail(&p->list, &pers_list);
6383 printk(KERN_INFO "md: %s personality registered for level %d\n", p->name, p->level);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384 spin_unlock(&pers_lock);
6385 return 0;
6386}
6387
NeilBrown2604b702006-01-06 00:20:36 -08006388int unregister_md_personality(struct mdk_personality *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389{
NeilBrown2604b702006-01-06 00:20:36 -08006390 printk(KERN_INFO "md: %s personality unregistered\n", p->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 spin_lock(&pers_lock);
NeilBrown2604b702006-01-06 00:20:36 -08006392 list_del_init(&p->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006393 spin_unlock(&pers_lock);
6394 return 0;
6395}
6396
NeilBrowneea1bf32009-03-31 14:27:02 +11006397static int is_mddev_idle(mddev_t *mddev, int init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398{
6399 mdk_rdev_t * rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006400 int idle;
NeilBrowneea1bf32009-03-31 14:27:02 +11006401 int curr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006402
6403 idle = 1;
NeilBrown4b809912008-07-21 17:05:25 +10006404 rcu_read_lock();
6405 rdev_for_each_rcu(rdev, mddev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 struct gendisk *disk = rdev->bdev->bd_contains->bd_disk;
NeilBrowneea1bf32009-03-31 14:27:02 +11006407 curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
6408 (int)part_stat_read(&disk->part0, sectors[1]) -
6409 atomic_read(&disk->sync_io);
NeilBrown713f6ab2007-07-17 04:06:12 -07006410 /* sync IO will cause sync_io to increase before the disk_stats
6411 * as sync_io is counted when a request starts, and
6412 * disk_stats is counted when it completes.
6413 * So resync activity will cause curr_events to be smaller than
6414 * when there was no such activity.
6415 * non-sync IO will cause disk_stat to increase without
6416 * increasing sync_io so curr_events will (eventually)
6417 * be larger than it was before. Once it becomes
6418 * substantially larger, the test below will cause
6419 * the array to appear non-idle, and resync will slow
6420 * down.
6421 * If there is a lot of outstanding resync activity when
6422 * we set last_event to curr_events, then all that activity
6423 * completing might cause the array to appear non-idle
6424 * and resync will be slowed down even though there might
6425 * not have been non-resync activity. This will only
6426 * happen once though. 'last_events' will soon reflect
6427 * the state where there is little or no outstanding
6428 * resync requests, and further resync activity will
6429 * always make curr_events less than last_events.
NeilBrownc0e48522005-11-18 01:11:01 -08006430 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431 */
NeilBrowneea1bf32009-03-31 14:27:02 +11006432 if (init || curr_events - rdev->last_events > 64) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006433 rdev->last_events = curr_events;
6434 idle = 0;
6435 }
6436 }
NeilBrown4b809912008-07-21 17:05:25 +10006437 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07006438 return idle;
6439}
6440
6441void md_done_sync(mddev_t *mddev, int blocks, int ok)
6442{
6443 /* another "blocks" (512byte) blocks have been synced */
6444 atomic_sub(blocks, &mddev->recovery_active);
6445 wake_up(&mddev->recovery_wait);
6446 if (!ok) {
NeilBrowndfc70642008-05-23 13:04:39 -07006447 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006448 md_wakeup_thread(mddev->thread);
6449 // stop recovery, signal do_sync ....
6450 }
6451}
6452
6453
NeilBrown06d91a52005-06-21 17:17:12 -07006454/* md_write_start(mddev, bi)
6455 * If we need to update some array metadata (e.g. 'active' flag
NeilBrown3d310eb2005-06-21 17:17:26 -07006456 * in superblock) before writing, schedule a superblock update
6457 * and wait for it to complete.
NeilBrown06d91a52005-06-21 17:17:12 -07006458 */
NeilBrown3d310eb2005-06-21 17:17:26 -07006459void md_write_start(mddev_t *mddev, struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006460{
Neil Brown0fd62b82008-06-28 08:31:36 +10006461 int did_change = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006462 if (bio_data_dir(bi) != WRITE)
NeilBrown3d310eb2005-06-21 17:17:26 -07006463 return;
NeilBrown06d91a52005-06-21 17:17:12 -07006464
NeilBrownf91de922005-11-08 21:39:36 -08006465 BUG_ON(mddev->ro == 1);
6466 if (mddev->ro == 2) {
6467 /* need to switch to read/write */
6468 mddev->ro = 0;
6469 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6470 md_wakeup_thread(mddev->thread);
NeilBrown25156192008-03-04 14:29:32 -08006471 md_wakeup_thread(mddev->sync_thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006472 did_change = 1;
NeilBrownf91de922005-11-08 21:39:36 -08006473 }
NeilBrown06d91a52005-06-21 17:17:12 -07006474 atomic_inc(&mddev->writes_pending);
NeilBrown31a59e32008-04-30 00:52:30 -07006475 if (mddev->safemode == 1)
6476 mddev->safemode = 0;
NeilBrown06d91a52005-06-21 17:17:12 -07006477 if (mddev->in_sync) {
NeilBrowna9701a32005-11-08 21:39:34 -08006478 spin_lock_irq(&mddev->write_lock);
NeilBrown3d310eb2005-06-21 17:17:26 -07006479 if (mddev->in_sync) {
6480 mddev->in_sync = 0;
NeilBrown850b2b42006-10-03 01:15:46 -07006481 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrown3d310eb2005-06-21 17:17:26 -07006482 md_wakeup_thread(mddev->thread);
Neil Brown0fd62b82008-06-28 08:31:36 +10006483 did_change = 1;
NeilBrown3d310eb2005-06-21 17:17:26 -07006484 }
NeilBrowna9701a32005-11-08 21:39:34 -08006485 spin_unlock_irq(&mddev->write_lock);
NeilBrown06d91a52005-06-21 17:17:12 -07006486 }
Neil Brown0fd62b82008-06-28 08:31:36 +10006487 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11006488 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown09a44cc2008-05-23 13:04:36 -07006489 wait_event(mddev->sb_wait,
6490 !test_bit(MD_CHANGE_CLEAN, &mddev->flags) &&
6491 !test_bit(MD_CHANGE_PENDING, &mddev->flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492}
6493
6494void md_write_end(mddev_t *mddev)
6495{
6496 if (atomic_dec_and_test(&mddev->writes_pending)) {
6497 if (mddev->safemode == 2)
6498 md_wakeup_thread(mddev->thread);
NeilBrown16f17b32006-06-26 00:27:37 -07006499 else if (mddev->safemode_delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006500 mod_timer(&mddev->safemode_timer, jiffies + mddev->safemode_delay);
6501 }
6502}
6503
NeilBrown2a2275d2007-01-26 00:57:11 -08006504/* md_allow_write(mddev)
6505 * Calling this ensures that the array is marked 'active' so that writes
6506 * may proceed without blocking. It is important to call this before
6507 * attempting a GFP_KERNEL allocation while holding the mddev lock.
6508 * Must be called with mddev_lock held.
Dan Williamsb5470dc2008-06-27 21:44:04 -07006509 *
6510 * In the ->external case MD_CHANGE_CLEAN can not be cleared until mddev->lock
6511 * is dropped, so return -EAGAIN after notifying userspace.
NeilBrown2a2275d2007-01-26 00:57:11 -08006512 */
Dan Williamsb5470dc2008-06-27 21:44:04 -07006513int md_allow_write(mddev_t *mddev)
NeilBrown2a2275d2007-01-26 00:57:11 -08006514{
6515 if (!mddev->pers)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006516 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006517 if (mddev->ro)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006518 return 0;
Neil Brown1a0fd492008-06-28 08:31:27 +10006519 if (!mddev->pers->sync_request)
Dan Williamsb5470dc2008-06-27 21:44:04 -07006520 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006521
6522 spin_lock_irq(&mddev->write_lock);
6523 if (mddev->in_sync) {
6524 mddev->in_sync = 0;
6525 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
6526 if (mddev->safemode_delay &&
6527 mddev->safemode == 0)
6528 mddev->safemode = 1;
6529 spin_unlock_irq(&mddev->write_lock);
6530 md_update_sb(mddev, 0);
NeilBrownb62b7592008-10-21 13:25:21 +11006531 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrown2a2275d2007-01-26 00:57:11 -08006532 } else
6533 spin_unlock_irq(&mddev->write_lock);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006534
6535 if (test_bit(MD_CHANGE_CLEAN, &mddev->flags))
6536 return -EAGAIN;
6537 else
6538 return 0;
NeilBrown2a2275d2007-01-26 00:57:11 -08006539}
6540EXPORT_SYMBOL_GPL(md_allow_write);
6541
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542#define SYNC_MARKS 10
6543#define SYNC_MARK_STEP (3*HZ)
NeilBrown29269552006-03-27 01:18:10 -08006544void md_do_sync(mddev_t *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006545{
6546 mddev_t *mddev2;
6547 unsigned int currspeed = 0,
6548 window;
NeilBrown57afd892005-06-21 17:17:13 -07006549 sector_t max_sectors,j, io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550 unsigned long mark[SYNC_MARKS];
6551 sector_t mark_cnt[SYNC_MARKS];
6552 int last_mark,m;
6553 struct list_head *tmp;
6554 sector_t last_check;
NeilBrown57afd892005-06-21 17:17:13 -07006555 int skipped = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006556 mdk_rdev_t *rdev;
NeilBrown61df9d92006-10-03 01:15:57 -07006557 char *desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558
6559 /* just incase thread restarts... */
6560 if (test_bit(MD_RECOVERY_DONE, &mddev->recovery))
6561 return;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006562 if (mddev->ro) /* never try to sync a read-only array */
6563 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006564
NeilBrown61df9d92006-10-03 01:15:57 -07006565 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6566 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
6567 desc = "data-check";
6568 else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6569 desc = "requested-resync";
6570 else
6571 desc = "resync";
6572 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6573 desc = "reshape";
6574 else
6575 desc = "recovery";
6576
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577 /* we overload curr_resync somewhat here.
6578 * 0 == not engaged in resync at all
6579 * 2 == checking that there is no conflict with another sync
6580 * 1 == like 2, but have yielded to allow conflicting resync to
6581 * commense
6582 * other == active in resync - this many blocks
6583 *
6584 * Before starting a resync we must have set curr_resync to
6585 * 2, and then checked that every "conflicting" array has curr_resync
6586 * less than ours. When we find one that is the same or higher
6587 * we wait on resync_wait. To avoid deadlock, we reduce curr_resync
6588 * to 1 if we choose to yield (based arbitrarily on address of mddev structure).
6589 * This will mean we have to start checking from the beginning again.
6590 *
6591 */
6592
6593 do {
6594 mddev->curr_resync = 2;
6595
6596 try_again:
NeilBrown404e4b42009-12-30 15:25:23 +11006597 if (kthread_should_stop())
NeilBrown6985c432005-10-19 21:23:47 -07006598 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
NeilBrown404e4b42009-12-30 15:25:23 +11006599
6600 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 goto skip;
NeilBrown29ac4aa2008-02-06 01:39:58 -08006602 for_each_mddev(mddev2, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 if (mddev2 == mddev)
6604 continue;
Bernd Schubert90b08712008-05-23 13:04:38 -07006605 if (!mddev->parallel_resync
6606 && mddev2->curr_resync
6607 && match_mddev_units(mddev, mddev2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608 DEFINE_WAIT(wq);
6609 if (mddev < mddev2 && mddev->curr_resync == 2) {
6610 /* arbitrarily yield */
6611 mddev->curr_resync = 1;
6612 wake_up(&resync_wait);
6613 }
6614 if (mddev > mddev2 && mddev->curr_resync == 1)
6615 /* no need to wait here, we can wait the next
6616 * time 'round when curr_resync == 2
6617 */
6618 continue;
NeilBrown97441972008-09-19 11:49:54 +10006619 /* We need to wait 'interruptible' so as not to
6620 * contribute to the load average, and not to
6621 * be caught by 'softlockup'
6622 */
6623 prepare_to_wait(&resync_wait, &wq, TASK_INTERRUPTIBLE);
NeilBrown787453c2005-11-08 21:39:43 -08006624 if (!kthread_should_stop() &&
NeilBrown8712e552005-10-26 01:58:58 -07006625 mddev2->curr_resync >= mddev->curr_resync) {
NeilBrown61df9d92006-10-03 01:15:57 -07006626 printk(KERN_INFO "md: delaying %s of %s"
6627 " until %s has finished (they"
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 " share one or more physical units)\n",
NeilBrown61df9d92006-10-03 01:15:57 -07006629 desc, mdname(mddev), mdname(mddev2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 mddev_put(mddev2);
NeilBrown97441972008-09-19 11:49:54 +10006631 if (signal_pending(current))
6632 flush_signals(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 schedule();
6634 finish_wait(&resync_wait, &wq);
6635 goto try_again;
6636 }
6637 finish_wait(&resync_wait, &wq);
6638 }
6639 }
6640 } while (mddev->curr_resync < 2);
6641
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006642 j = 0;
NeilBrown9d888832005-11-08 21:39:26 -08006643 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644 /* resync follows the size requested by the personality,
NeilBrown57afd892005-06-21 17:17:13 -07006645 * which defaults to physical size, but can be virtual size
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 */
6647 max_sectors = mddev->resync_max_sectors;
NeilBrown9d888832005-11-08 21:39:26 -08006648 mddev->resync_mismatches = 0;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006649 /* we don't use the checkpoint if there's a bitmap */
Neil Brown5e96ee62008-06-28 08:31:24 +10006650 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6651 j = mddev->resync_min;
6652 else if (!mddev->bitmap)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006653 j = mddev->recovery_cp;
Neil Brown5e96ee62008-06-28 08:31:24 +10006654
NeilBrownccfcc3c2006-03-27 01:18:09 -08006655 } else if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
Andre Noll58c0fed2009-03-31 14:33:13 +11006656 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006657 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006658 /* recovery follows the physical size of devices */
Andre Noll58c0fed2009-03-31 14:33:13 +11006659 max_sectors = mddev->dev_sectors;
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006660 j = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006661 rcu_read_lock();
6662 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006663 if (rdev->raid_disk >= 0 &&
6664 !test_bit(Faulty, &rdev->flags) &&
6665 !test_bit(In_sync, &rdev->flags) &&
6666 rdev->recovery_offset < j)
6667 j = rdev->recovery_offset;
Dan Williams4e59ca72009-12-12 21:17:06 -07006668 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
NeilBrown61df9d92006-10-03 01:15:57 -07006671 printk(KERN_INFO "md: %s of RAID array %s\n", desc, mdname(mddev));
6672 printk(KERN_INFO "md: minimum _guaranteed_ speed:"
6673 " %d KB/sec/disk.\n", speed_min(mddev));
Adrian Bunk338cec32005-09-10 00:26:54 -07006674 printk(KERN_INFO "md: using maximum available idle IO bandwidth "
NeilBrown61df9d92006-10-03 01:15:57 -07006675 "(but not more than %d KB/sec) for %s.\n",
6676 speed_max(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006677
NeilBrowneea1bf32009-03-31 14:27:02 +11006678 is_mddev_idle(mddev, 1); /* this initializes IO event counters */
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006679
NeilBrown57afd892005-06-21 17:17:13 -07006680 io_sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006681 for (m = 0; m < SYNC_MARKS; m++) {
6682 mark[m] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006683 mark_cnt[m] = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006684 }
6685 last_mark = 0;
6686 mddev->resync_mark = mark[last_mark];
6687 mddev->resync_mark_cnt = mark_cnt[last_mark];
6688
6689 /*
6690 * Tune reconstruction:
6691 */
6692 window = 32*(PAGE_SIZE/512);
6693 printk(KERN_INFO "md: using %dk window, over a total of %llu blocks.\n",
6694 window/2,(unsigned long long) max_sectors/2);
6695
6696 atomic_set(&mddev->recovery_active, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006697 last_check = 0;
6698
6699 if (j>2) {
6700 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006701 "md: resuming %s of %s from checkpoint.\n",
6702 desc, mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 mddev->curr_resync = j;
6704 }
NeilBrownefa59332009-12-14 12:49:49 +11006705 mddev->curr_resync_completed = mddev->curr_resync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006706
6707 while (j < max_sectors) {
NeilBrown57afd892005-06-21 17:17:13 -07006708 sector_t sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
NeilBrown57afd892005-06-21 17:17:13 -07006710 skipped = 0;
NeilBrown97e4f422009-03-31 14:33:13 +11006711
NeilBrown7a91ee12009-05-26 12:57:21 +10006712 if (!test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
6713 ((mddev->curr_resync > mddev->curr_resync_completed &&
6714 (mddev->curr_resync - mddev->curr_resync_completed)
6715 > (max_sectors >> 4)) ||
6716 (j - mddev->curr_resync_completed)*2
6717 >= mddev->resync_max - mddev->curr_resync_completed
6718 )) {
NeilBrown97e4f422009-03-31 14:33:13 +11006719 /* time to update curr_resync_completed */
6720 blk_unplug(mddev->queue);
6721 wait_event(mddev->recovery_wait,
6722 atomic_read(&mddev->recovery_active) == 0);
6723 mddev->curr_resync_completed =
6724 mddev->curr_resync;
6725 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
NeilBrownacb180b2009-04-14 16:28:34 +10006726 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
NeilBrown97e4f422009-03-31 14:33:13 +11006727 }
NeilBrownacb180b2009-04-14 16:28:34 +10006728
NeilBrowne62e58a2009-07-01 13:15:35 +10006729 while (j >= mddev->resync_max && !kthread_should_stop()) {
6730 /* As this condition is controlled by user-space,
6731 * we can block indefinitely, so use '_interruptible'
6732 * to avoid triggering warnings.
6733 */
6734 flush_signals(current); /* just in case */
6735 wait_event_interruptible(mddev->recovery_wait,
6736 mddev->resync_max > j
6737 || kthread_should_stop());
6738 }
NeilBrownacb180b2009-04-14 16:28:34 +10006739
6740 if (kthread_should_stop())
6741 goto interrupted;
6742
NeilBrown57afd892005-06-21 17:17:13 -07006743 sectors = mddev->pers->sync_request(mddev, j, &skipped,
NeilBrownc6207272008-02-06 01:39:52 -08006744 currspeed < speed_min(mddev));
NeilBrown57afd892005-06-21 17:17:13 -07006745 if (sectors == 0) {
NeilBrowndfc70642008-05-23 13:04:39 -07006746 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006747 goto out;
6748 }
NeilBrown57afd892005-06-21 17:17:13 -07006749
6750 if (!skipped) { /* actual IO requested */
6751 io_sectors += sectors;
6752 atomic_add(sectors, &mddev->recovery_active);
6753 }
6754
Linus Torvalds1da177e2005-04-16 15:20:36 -07006755 j += sectors;
6756 if (j>1) mddev->curr_resync = j;
NeilBrownff4e8d92006-07-10 04:44:16 -07006757 mddev->curr_mark_cnt = io_sectors;
NeilBrownd7603b72006-01-06 00:20:30 -08006758 if (last_check == 0)
6759 /* this is the earliers that rebuilt will be
6760 * visible in /proc/mdstat
6761 */
6762 md_new_event(mddev);
NeilBrown57afd892005-06-21 17:17:13 -07006763
6764 if (last_check + window > io_sectors || j == max_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006765 continue;
6766
NeilBrown57afd892005-06-21 17:17:13 -07006767 last_check = io_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768
NeilBrowndfc70642008-05-23 13:04:39 -07006769 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
Linus Torvalds1da177e2005-04-16 15:20:36 -07006770 break;
6771
6772 repeat:
6773 if (time_after_eq(jiffies, mark[last_mark] + SYNC_MARK_STEP )) {
6774 /* step marks */
6775 int next = (last_mark+1) % SYNC_MARKS;
6776
6777 mddev->resync_mark = mark[next];
6778 mddev->resync_mark_cnt = mark_cnt[next];
6779 mark[next] = jiffies;
NeilBrown57afd892005-06-21 17:17:13 -07006780 mark_cnt[next] = io_sectors - atomic_read(&mddev->recovery_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006781 last_mark = next;
6782 }
6783
6784
NeilBrownc6207272008-02-06 01:39:52 -08006785 if (kthread_should_stop())
6786 goto interrupted;
6787
Linus Torvalds1da177e2005-04-16 15:20:36 -07006788
6789 /*
6790 * this loop exits only if either when we are slower than
6791 * the 'hard' speed limit, or the system was IO-idle for
6792 * a jiffy.
6793 * the system might be non-idle CPU-wise, but we only care
6794 * about not overloading the IO subsystem. (things like an
6795 * e2fsck being done on the RAID array should execute fast)
6796 */
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006797 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798 cond_resched();
6799
NeilBrown57afd892005-06-21 17:17:13 -07006800 currspeed = ((unsigned long)(io_sectors-mddev->resync_mark_cnt))/2
6801 /((jiffies-mddev->resync_mark)/HZ +1) +1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006802
NeilBrown88202a02006-01-06 00:21:36 -08006803 if (currspeed > speed_min(mddev)) {
6804 if ((currspeed > speed_max(mddev)) ||
NeilBrowneea1bf32009-03-31 14:27:02 +11006805 !is_mddev_idle(mddev, 0)) {
NeilBrownc0e48522005-11-18 01:11:01 -08006806 msleep(500);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006807 goto repeat;
6808 }
6809 }
6810 }
NeilBrown61df9d92006-10-03 01:15:57 -07006811 printk(KERN_INFO "md: %s: %s done.\n",mdname(mddev), desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006812 /*
6813 * this also signals 'finished resyncing' to md_stop
6814 */
6815 out:
Alan D. Brunelle2ad8b1e2007-11-07 14:26:56 -05006816 blk_unplug(mddev->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006817
6818 wait_event(mddev->recovery_wait, !atomic_read(&mddev->recovery_active));
6819
6820 /* tell personality that we are finished */
NeilBrown57afd892005-06-21 17:17:13 -07006821 mddev->pers->sync_request(mddev, max_sectors, &skipped, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822
NeilBrowndfc70642008-05-23 13:04:39 -07006823 if (!test_bit(MD_RECOVERY_CHECK, &mddev->recovery) &&
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006824 mddev->curr_resync > 2) {
6825 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
6826 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6827 if (mddev->curr_resync >= mddev->recovery_cp) {
6828 printk(KERN_INFO
NeilBrown61df9d92006-10-03 01:15:57 -07006829 "md: checkpointing %s of %s.\n",
6830 desc, mdname(mddev));
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006831 mddev->recovery_cp = mddev->curr_resync;
6832 }
6833 } else
6834 mddev->recovery_cp = MaxSector;
6835 } else {
6836 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6837 mddev->curr_resync = MaxSector;
Dan Williams4e59ca72009-12-12 21:17:06 -07006838 rcu_read_lock();
6839 list_for_each_entry_rcu(rdev, &mddev->disks, same_set)
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006840 if (rdev->raid_disk >= 0 &&
6841 !test_bit(Faulty, &rdev->flags) &&
6842 !test_bit(In_sync, &rdev->flags) &&
6843 rdev->recovery_offset < mddev->curr_resync)
6844 rdev->recovery_offset = mddev->curr_resync;
Dan Williams4e59ca72009-12-12 21:17:06 -07006845 rcu_read_unlock();
NeilBrown5fd6c1d2006-06-26 00:27:40 -07006846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006847 }
NeilBrown17571282006-12-10 02:20:52 -08006848 set_bit(MD_CHANGE_DEVS, &mddev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006849
Linus Torvalds1da177e2005-04-16 15:20:36 -07006850 skip:
NeilBrownc07b70a2009-12-14 12:49:48 +11006851 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
6852 /* We completed so min/max setting can be forgotten if used. */
6853 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6854 mddev->resync_min = 0;
6855 mddev->resync_max = MaxSector;
6856 } else if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
6857 mddev->resync_min = mddev->curr_resync_completed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858 mddev->curr_resync = 0;
NeilBrownefa59332009-12-14 12:49:49 +11006859 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6860 mddev->curr_resync_completed = 0;
NeilBrownc6207272008-02-06 01:39:52 -08006861 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006862 wake_up(&resync_wait);
6863 set_bit(MD_RECOVERY_DONE, &mddev->recovery);
6864 md_wakeup_thread(mddev->thread);
NeilBrownc6207272008-02-06 01:39:52 -08006865 return;
6866
6867 interrupted:
6868 /*
6869 * got a signal, exit.
6870 */
6871 printk(KERN_INFO
6872 "md: md_do_sync() got signal ... exiting\n");
6873 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
6874 goto out;
6875
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876}
NeilBrown29269552006-03-27 01:18:10 -08006877EXPORT_SYMBOL_GPL(md_do_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878
6879
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006880static int remove_and_add_spares(mddev_t *mddev)
6881{
6882 mdk_rdev_t *rdev;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006883 int spares = 0;
6884
NeilBrown97e4f422009-03-31 14:33:13 +11006885 mddev->curr_resync_completed = 0;
6886
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006887 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006888 if (rdev->raid_disk >= 0 &&
Dan Williams6bfe0b42008-04-30 00:52:32 -07006889 !test_bit(Blocked, &rdev->flags) &&
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006890 (test_bit(Faulty, &rdev->flags) ||
6891 ! test_bit(In_sync, &rdev->flags)) &&
6892 atomic_read(&rdev->nr_pending)==0) {
6893 if (mddev->pers->hot_remove_disk(
6894 mddev, rdev->raid_disk)==0) {
6895 char nm[20];
6896 sprintf(nm,"rd%d", rdev->raid_disk);
6897 sysfs_remove_link(&mddev->kobj, nm);
6898 rdev->raid_disk = -1;
6899 }
6900 }
6901
NeilBrown4044ba52009-01-09 08:31:11 +11006902 if (mddev->degraded && ! mddev->ro && !mddev->recovery_disabled) {
Cheng Renquan159ec1f2009-01-09 08:31:08 +11006903 list_for_each_entry(rdev, &mddev->disks, same_set) {
NeilBrowndfc70642008-05-23 13:04:39 -07006904 if (rdev->raid_disk >= 0 &&
Dan Williamse5427132008-07-28 23:28:06 -07006905 !test_bit(In_sync, &rdev->flags) &&
6906 !test_bit(Blocked, &rdev->flags))
NeilBrowndfc70642008-05-23 13:04:39 -07006907 spares++;
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006908 if (rdev->raid_disk < 0
6909 && !test_bit(Faulty, &rdev->flags)) {
6910 rdev->recovery_offset = 0;
Neil Brown199050e2008-06-28 08:31:33 +10006911 if (mddev->pers->
6912 hot_add_disk(mddev, rdev) == 0) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006913 char nm[20];
6914 sprintf(nm, "rd%d", rdev->raid_disk);
NeilBrown5e55e2f2007-03-26 21:32:14 -08006915 if (sysfs_create_link(&mddev->kobj,
6916 &rdev->kobj, nm))
6917 printk(KERN_WARNING
6918 "md: cannot register "
6919 "%s for %s\n",
6920 nm, mdname(mddev));
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006921 spares++;
6922 md_new_event(mddev);
NeilBrown93be75f2009-12-14 12:50:06 +11006923 set_bit(MD_CHANGE_DEVS, &mddev->flags);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006924 } else
6925 break;
6926 }
NeilBrowndfc70642008-05-23 13:04:39 -07006927 }
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006928 }
6929 return spares;
6930}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006931/*
6932 * This routine is regularly called by all per-raid-array threads to
6933 * deal with generic issues like resync and super-block update.
6934 * Raid personalities that don't have a thread (linear/raid0) do not
6935 * need this as they never do any recovery or update the superblock.
6936 *
6937 * It does not do any resync itself, but rather "forks" off other threads
6938 * to do that as needed.
6939 * When it is determined that resync is needed, we set MD_RECOVERY_RUNNING in
6940 * "->recovery" and create a thread at ->sync_thread.
NeilBrowndfc70642008-05-23 13:04:39 -07006941 * When the thread finishes it sets MD_RECOVERY_DONE
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 * and wakeups up this thread which will reap the thread and finish up.
6943 * This thread also removes any faulty devices (with nr_pending == 0).
6944 *
6945 * The overall approach is:
6946 * 1/ if the superblock needs updating, update it.
6947 * 2/ If a recovery thread is running, don't do anything else.
6948 * 3/ If recovery has finished, clean up, possibly marking spares active.
6949 * 4/ If there are any faulty devices, remove them.
6950 * 5/ If array is degraded, try to add spares devices
6951 * 6/ If array has spares or is not in-sync, start a resync thread.
6952 */
6953void md_check_recovery(mddev_t *mddev)
6954{
6955 mdk_rdev_t *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006956
6957
NeilBrown5f404022005-06-21 17:17:16 -07006958 if (mddev->bitmap)
NeilBrownaa5cbd12009-12-14 12:49:46 +11006959 bitmap_daemon_work(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960
6961 if (mddev->ro)
6962 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006963
6964 if (signal_pending(current)) {
NeilBrown31a59e32008-04-30 00:52:30 -07006965 if (mddev->pers->sync_request && !mddev->external) {
NeilBrownfca4d842005-06-21 17:17:11 -07006966 printk(KERN_INFO "md: %s in immediate safe mode\n",
6967 mdname(mddev));
6968 mddev->safemode = 2;
6969 }
6970 flush_signals(current);
6971 }
6972
NeilBrownc89a8ee2008-08-05 15:54:13 +10006973 if (mddev->ro && !test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
6974 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 if ( ! (
NeilBrowne6910632008-02-06 01:39:51 -08006976 (mddev->flags && !mddev->external) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07006977 test_bit(MD_RECOVERY_NEEDED, &mddev->recovery) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006978 test_bit(MD_RECOVERY_DONE, &mddev->recovery) ||
NeilBrown31a59e32008-04-30 00:52:30 -07006979 (mddev->external == 0 && mddev->safemode == 1) ||
NeilBrownfca4d842005-06-21 17:17:11 -07006980 (mddev->safemode == 2 && ! atomic_read(&mddev->writes_pending)
6981 && !mddev->in_sync && mddev->recovery_cp == MaxSector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006982 ))
6983 return;
NeilBrownfca4d842005-06-21 17:17:11 -07006984
NeilBrowndf5b89b2006-03-27 01:18:20 -08006985 if (mddev_trylock(mddev)) {
NeilBrownb4c4c7b2007-02-28 20:11:48 -08006986 int spares = 0;
NeilBrownfca4d842005-06-21 17:17:11 -07006987
NeilBrownc89a8ee2008-08-05 15:54:13 +10006988 if (mddev->ro) {
6989 /* Only thing we do on a ro array is remove
6990 * failed devices.
6991 */
6992 remove_and_add_spares(mddev);
6993 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
6994 goto unlock;
6995 }
6996
NeilBrown31a59e32008-04-30 00:52:30 -07006997 if (!mddev->external) {
Neil Brown0fd62b82008-06-28 08:31:36 +10006998 int did_change = 0;
NeilBrown31a59e32008-04-30 00:52:30 -07006999 spin_lock_irq(&mddev->write_lock);
7000 if (mddev->safemode &&
7001 !atomic_read(&mddev->writes_pending) &&
7002 !mddev->in_sync &&
7003 mddev->recovery_cp == MaxSector) {
7004 mddev->in_sync = 1;
Neil Brown0fd62b82008-06-28 08:31:36 +10007005 did_change = 1;
NeilBrown31a59e32008-04-30 00:52:30 -07007006 if (mddev->persistent)
7007 set_bit(MD_CHANGE_CLEAN, &mddev->flags);
7008 }
7009 if (mddev->safemode == 1)
7010 mddev->safemode = 0;
7011 spin_unlock_irq(&mddev->write_lock);
Neil Brown0fd62b82008-06-28 08:31:36 +10007012 if (did_change)
NeilBrownb62b7592008-10-21 13:25:21 +11007013 sysfs_notify_dirent(mddev->sysfs_state);
NeilBrownfca4d842005-06-21 17:17:11 -07007014 }
NeilBrownfca4d842005-06-21 17:17:11 -07007015
NeilBrown850b2b42006-10-03 01:15:46 -07007016 if (mddev->flags)
7017 md_update_sb(mddev, 0);
NeilBrown06d91a52005-06-21 17:17:12 -07007018
Linus Torvalds1da177e2005-04-16 15:20:36 -07007019 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
7020 !test_bit(MD_RECOVERY_DONE, &mddev->recovery)) {
7021 /* resync/recovery still happening */
7022 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7023 goto unlock;
7024 }
7025 if (mddev->sync_thread) {
7026 /* resync has finished, collect result */
7027 md_unregister_thread(mddev->sync_thread);
7028 mddev->sync_thread = NULL;
Dan Williams56ac36d2008-08-07 10:02:47 -07007029 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery) &&
7030 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031 /* success...*/
7032 /* activate any spares */
Neil Browna99ac972008-06-28 08:31:43 +10007033 if (mddev->pers->spare_active(mddev))
7034 sysfs_notify(&mddev->kobj, NULL,
7035 "degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 }
NeilBrowncea9c222009-03-31 15:15:05 +11007037 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
7038 mddev->pers->finish_reshape)
7039 mddev->pers->finish_reshape(mddev);
NeilBrown850b2b42006-10-03 01:15:46 -07007040 md_update_sb(mddev, 1);
NeilBrown41158c72005-06-21 17:17:25 -07007041
7042 /* if array is no-longer degraded, then any saved_raid_disk
7043 * information must be scrapped
7044 */
7045 if (!mddev->degraded)
Cheng Renquan159ec1f2009-01-09 08:31:08 +11007046 list_for_each_entry(rdev, &mddev->disks, same_set)
NeilBrown41158c72005-06-21 17:17:25 -07007047 rdev->saved_raid_disk = -1;
7048
Linus Torvalds1da177e2005-04-16 15:20:36 -07007049 mddev->recovery = 0;
7050 /* flag recovery needed just to double check */
7051 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown0c3573f2009-01-09 08:31:05 +11007052 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007053 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007054 goto unlock;
7055 }
Neil Brown72a23c22008-06-28 08:31:41 +10007056 /* Set RUNNING before clearing NEEDED to avoid
7057 * any transients in the value of "sync_action".
7058 */
7059 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7060 clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007061 /* Clear some bits that don't mean anything, but
7062 * might be left set
7063 */
NeilBrown24dd4692005-11-08 21:39:26 -08007064 clear_bit(MD_RECOVERY_INTR, &mddev->recovery);
7065 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007066
NeilBrown5fd6c1d2006-06-26 00:27:40 -07007067 if (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery))
7068 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 /* no recovery is running.
7070 * remove any failed drives, then
7071 * add spares if possible.
7072 * Spare are also removed and re-added, to allow
7073 * the personality to fail the re-add.
7074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007075
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007076 if (mddev->reshape_position != MaxSector) {
NeilBrown50ac1682009-06-18 08:47:55 +10007077 if (mddev->pers->check_reshape == NULL ||
7078 mddev->pers->check_reshape(mddev) != 0)
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007079 /* Cannot proceed */
7080 goto unlock;
7081 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007082 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrownb4c4c7b2007-02-28 20:11:48 -08007083 } else if ((spares = remove_and_add_spares(mddev))) {
NeilBrown24dd4692005-11-08 21:39:26 -08007084 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7085 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
Dan Williams56ac36d2008-08-07 10:02:47 -07007086 clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007087 set_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007088 } else if (mddev->recovery_cp < MaxSector) {
7089 set_bit(MD_RECOVERY_SYNC, &mddev->recovery);
Neil Brown72a23c22008-06-28 08:31:41 +10007090 clear_bit(MD_RECOVERY_RECOVER, &mddev->recovery);
NeilBrown24dd4692005-11-08 21:39:26 -08007091 } else if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
7092 /* nothing to be done ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007093 goto unlock;
NeilBrown24dd4692005-11-08 21:39:26 -08007094
Linus Torvalds1da177e2005-04-16 15:20:36 -07007095 if (mddev->pers->sync_request) {
NeilBrowna654b9d82005-06-21 17:17:27 -07007096 if (spares && mddev->bitmap && ! mddev->bitmap->file) {
7097 /* We are adding a device or devices to an array
7098 * which has the bitmap stored on all devices.
7099 * So make sure all bitmap pages get written
7100 */
7101 bitmap_write_all(mddev->bitmap);
7102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007103 mddev->sync_thread = md_register_thread(md_do_sync,
7104 mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007105 "resync");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007106 if (!mddev->sync_thread) {
7107 printk(KERN_ERR "%s: could not start resync"
7108 " thread...\n",
7109 mdname(mddev));
7110 /* leave the spares where they are, it shouldn't hurt */
7111 mddev->recovery = 0;
NeilBrownd7603b72006-01-06 00:20:30 -08007112 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113 md_wakeup_thread(mddev->sync_thread);
NeilBrown0c3573f2009-01-09 08:31:05 +11007114 sysfs_notify_dirent(mddev->sysfs_action);
NeilBrownd7603b72006-01-06 00:20:30 -08007115 md_new_event(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007116 }
7117 unlock:
Neil Brown72a23c22008-06-28 08:31:41 +10007118 if (!mddev->sync_thread) {
7119 clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7120 if (test_and_clear_bit(MD_RECOVERY_RECOVER,
7121 &mddev->recovery))
NeilBrown0c3573f2009-01-09 08:31:05 +11007122 if (mddev->sysfs_action)
7123 sysfs_notify_dirent(mddev->sysfs_action);
Neil Brown72a23c22008-06-28 08:31:41 +10007124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 mddev_unlock(mddev);
7126 }
7127}
7128
Dan Williams6bfe0b42008-04-30 00:52:32 -07007129void md_wait_for_blocked_rdev(mdk_rdev_t *rdev, mddev_t *mddev)
7130{
NeilBrown3c0ee632008-10-21 13:25:28 +11007131 sysfs_notify_dirent(rdev->sysfs_state);
Dan Williams6bfe0b42008-04-30 00:52:32 -07007132 wait_event_timeout(rdev->blocked_wait,
7133 !test_bit(Blocked, &rdev->flags),
7134 msecs_to_jiffies(5000));
7135 rdev_dec_pending(rdev, mddev);
7136}
7137EXPORT_SYMBOL(md_wait_for_blocked_rdev);
7138
Adrian Bunk75c96f82005-05-05 16:16:09 -07007139static int md_notify_reboot(struct notifier_block *this,
7140 unsigned long code, void *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007141{
7142 struct list_head *tmp;
7143 mddev_t *mddev;
7144
7145 if ((code == SYS_DOWN) || (code == SYS_HALT) || (code == SYS_POWER_OFF)) {
7146
7147 printk(KERN_INFO "md: stopping all md devices.\n");
7148
NeilBrown29ac4aa2008-02-06 01:39:58 -08007149 for_each_mddev(mddev, tmp)
Neil Brownc71d4882006-05-26 10:39:25 +10007150 if (mddev_trylock(mddev)) {
NeilBrown2b250002008-08-05 15:54:13 +10007151 /* Force a switch to readonly even array
7152 * appears to still be in use. Hence
7153 * the '100'.
7154 */
NeilBrowna4bd82d2010-03-29 13:23:10 +11007155 md_set_readonly(mddev, 100);
Neil Brownc71d4882006-05-26 10:39:25 +10007156 mddev_unlock(mddev);
7157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158 /*
7159 * certain more exotic SCSI devices are known to be
7160 * volatile wrt too early system reboots. While the
7161 * right place to handle this issue is the given
7162 * driver, we do want to have a safe RAID driver ...
7163 */
7164 mdelay(1000*1);
7165 }
7166 return NOTIFY_DONE;
7167}
7168
Adrian Bunk75c96f82005-05-05 16:16:09 -07007169static struct notifier_block md_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170 .notifier_call = md_notify_reboot,
7171 .next = NULL,
7172 .priority = INT_MAX, /* before any real devices */
7173};
7174
7175static void md_geninit(void)
7176{
Linus Torvalds1da177e2005-04-16 15:20:36 -07007177 dprintk("md: sizeof(mdp_super_t) = %d\n", (int)sizeof(mdp_super_t));
7178
Denis V. Lunevc7705f3442008-04-29 01:02:35 -07007179 proc_create("mdstat", S_IRUGO, NULL, &md_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180}
7181
Adrian Bunk75c96f82005-05-05 16:16:09 -07007182static int __init md_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007183{
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007184 if (register_blkdev(MD_MAJOR, "md"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185 return -1;
7186 if ((mdp_major=register_blkdev(0, "mdp"))<=0) {
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007187 unregister_blkdev(MD_MAJOR, "md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007188 return -1;
7189 }
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007190 blk_register_region(MKDEV(MD_MAJOR, 0), 1UL<<MINORBITS, THIS_MODULE,
NeilBrowne8703fe2006-10-03 01:15:59 -07007191 md_probe, NULL, NULL);
7192 blk_register_region(MKDEV(mdp_major, 0), 1UL<<MINORBITS, THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007193 md_probe, NULL, NULL);
7194
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195 register_reboot_notifier(&md_notifier);
Eric W. Biederman0b4d4142007-02-14 00:34:09 -08007196 raid_table_header = register_sysctl_table(raid_root_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007197
7198 md_geninit();
NeilBrownd710e132008-10-13 11:55:12 +11007199 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007200}
7201
7202
7203#ifndef MODULE
7204
7205/*
7206 * Searches all registered partitions for autorun RAID arrays
7207 * at boot time.
7208 */
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007209
7210static LIST_HEAD(all_detected_devices);
7211struct detected_devices_node {
7212 struct list_head list;
7213 dev_t dev;
7214};
Linus Torvalds1da177e2005-04-16 15:20:36 -07007215
7216void md_autodetect_dev(dev_t dev)
7217{
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007218 struct detected_devices_node *node_detected_dev;
7219
7220 node_detected_dev = kzalloc(sizeof(*node_detected_dev), GFP_KERNEL);
7221 if (node_detected_dev) {
7222 node_detected_dev->dev = dev;
7223 list_add_tail(&node_detected_dev->list, &all_detected_devices);
7224 } else {
7225 printk(KERN_CRIT "md: md_autodetect_dev: kzalloc failed"
7226 ", skipping dev(%d,%d)\n", MAJOR(dev), MINOR(dev));
7227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007228}
7229
7230
7231static void autostart_arrays(int part)
7232{
7233 mdk_rdev_t *rdev;
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007234 struct detected_devices_node *node_detected_dev;
7235 dev_t dev;
7236 int i_scanned, i_passed;
7237
7238 i_scanned = 0;
7239 i_passed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007240
7241 printk(KERN_INFO "md: Autodetecting RAID arrays.\n");
7242
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007243 while (!list_empty(&all_detected_devices) && i_scanned < INT_MAX) {
7244 i_scanned++;
7245 node_detected_dev = list_entry(all_detected_devices.next,
7246 struct detected_devices_node, list);
7247 list_del(&node_detected_dev->list);
7248 dev = node_detected_dev->dev;
7249 kfree(node_detected_dev);
NeilBrowndf968c42007-07-17 04:06:11 -07007250 rdev = md_import_device(dev,0, 90);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007251 if (IS_ERR(rdev))
7252 continue;
7253
NeilBrownb2d444d2005-11-08 21:39:31 -08007254 if (test_bit(Faulty, &rdev->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255 MD_BUG();
7256 continue;
7257 }
NeilBrownd0fae182008-03-04 14:29:31 -08007258 set_bit(AutoDetected, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007259 list_add(&rdev->same_set, &pending_raid_disks);
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007260 i_passed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007261 }
Michael J. Evans4d936ec2007-10-16 23:30:52 -07007262
7263 printk(KERN_INFO "md: Scanned %d and added %d devices.\n",
7264 i_scanned, i_passed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007265
7266 autorun_devices(part);
7267}
7268
Jeff Garzikfdee8ae2006-12-10 02:20:50 -08007269#endif /* !MODULE */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007270
7271static __exit void md_exit(void)
7272{
7273 mddev_t *mddev;
7274 struct list_head *tmp;
Greg Kroah-Hartman8ab5e4c2005-06-20 21:15:16 -07007275
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007276 blk_unregister_region(MKDEV(MD_MAJOR,0), 1U << MINORBITS);
NeilBrowne8703fe2006-10-03 01:15:59 -07007277 blk_unregister_region(MKDEV(mdp_major,0), 1U << MINORBITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007278
Christoph Hellwig3dbd8c22009-03-31 14:27:02 +11007279 unregister_blkdev(MD_MAJOR,"md");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007280 unregister_blkdev(mdp_major, "mdp");
7281 unregister_reboot_notifier(&md_notifier);
7282 unregister_sysctl_table(raid_table_header);
7283 remove_proc_entry("mdstat", NULL);
NeilBrown29ac4aa2008-02-06 01:39:58 -08007284 for_each_mddev(mddev, tmp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007285 export_array(mddev);
NeilBrownd3374822009-01-09 08:31:10 +11007286 mddev->hold_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007287 }
7288}
7289
Dan Williams685784a2007-07-09 11:56:42 -07007290subsys_initcall(md_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291module_exit(md_exit)
7292
NeilBrownf91de922005-11-08 21:39:36 -08007293static int get_ro(char *buffer, struct kernel_param *kp)
7294{
7295 return sprintf(buffer, "%d", start_readonly);
7296}
7297static int set_ro(const char *val, struct kernel_param *kp)
7298{
7299 char *e;
7300 int num = simple_strtoul(val, &e, 10);
7301 if (*val && (*e == '\0' || *e == '\n')) {
7302 start_readonly = num;
NeilBrown4dbcdc72006-01-06 00:20:52 -08007303 return 0;
NeilBrownf91de922005-11-08 21:39:36 -08007304 }
7305 return -EINVAL;
7306}
7307
NeilBrown80ca3a42006-07-10 04:44:18 -07007308module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
7309module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007310
NeilBrownefeb53c2009-01-09 08:31:10 +11007311module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
NeilBrownf91de922005-11-08 21:39:36 -08007312
Linus Torvalds1da177e2005-04-16 15:20:36 -07007313EXPORT_SYMBOL(register_md_personality);
7314EXPORT_SYMBOL(unregister_md_personality);
7315EXPORT_SYMBOL(md_error);
7316EXPORT_SYMBOL(md_done_sync);
7317EXPORT_SYMBOL(md_write_start);
7318EXPORT_SYMBOL(md_write_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007319EXPORT_SYMBOL(md_register_thread);
7320EXPORT_SYMBOL(md_unregister_thread);
7321EXPORT_SYMBOL(md_wakeup_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007322EXPORT_SYMBOL(md_check_recovery);
7323MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11007324MODULE_DESCRIPTION("MD RAID framework");
NeilBrownaa1595e2005-08-04 12:53:32 -07007325MODULE_ALIAS("md");
NeilBrown72008652005-08-26 18:34:15 -07007326MODULE_ALIAS_BLOCKDEV_MAJOR(MD_MAJOR);